2007-11-14 22:18:48 -05:00
/****************************************************************
* RShare is distributed under the following license :
*
* Copyright ( C ) 2006 , crypton
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
* of the License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
2009-08-25 16:49:50 -04:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2007-11-14 22:18:48 -05:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-07-23 14:52:58 -04:00
# include <QMessageBox>
2010-12-02 19:54:40 -05:00
# include <QDir>
2011-02-09 16:44:49 -05:00
# include <QTimer>
2011-05-04 06:22:49 -04:00
# include <QShortcut>
2007-11-14 22:18:48 -05:00
2012-09-07 13:30:23 -04:00
# include "rshare.h"
2007-11-14 22:18:48 -05:00
# include "SearchDialog.h"
2010-03-10 16:38:26 -05:00
# include "RetroShareLink.h"
2010-05-23 15:13:41 -04:00
# include "msgs/MessageComposer.h"
2010-07-23 14:52:58 -04:00
# include "gui/RSHumanReadableDelegate.h"
2012-02-19 10:03:07 -05:00
# include "retroshare-gui/RsAutoUpdatePage.h"
2011-11-09 16:26:51 -05:00
# include "gui/common/RsCollectionFile.h"
2011-12-07 08:08:12 -05:00
# include "gui/common/FilesDefs.h"
2013-08-23 17:13:13 -04:00
# include <gui/common/RsUrlHandler.h>
2010-07-23 14:52:58 -04:00
# include "settings/rsharesettings.h"
# include "advsearch/advancedsearchdialog.h"
2012-06-09 10:14:04 -04:00
# include "common/RSTreeWidgetItem.h"
2013-10-19 09:25:06 -04:00
# include "util/QtVersion.h"
2010-01-21 17:34:10 -05:00
2010-08-06 05:40:23 -04:00
# include <retroshare/rsfiles.h>
# include <retroshare/rsturtle.h>
2011-06-20 18:19:34 -04:00
# include <retroshare/rsexpr.h>
2007-11-14 22:18:48 -05:00
/* Images for context menu icons */
# define IMAGE_START ": / images / download.png"
2010-02-09 08:49:37 -05:00
# define IMAGE_REMOVE ": / images / delete.png"
2014-05-29 10:49:45 -04:00
# define IMAGE_REMOVEALL ": / images / deleteall.png"
# define IMAGE_DIRECTORY ": / images / folder16.png"
# define IMAGE_OPENFOLDER ": / images / folderopen.png"
# define IMAGE_LIBRARY ": / images / library.png"
# define IMAGE_COLLCREATE ": / images / library_add.png"
# define IMAGE_COLLMODIF ": / images / library_edit.png"
# define IMAGE_COLLVIEW ": / images / library_view.png"
# define IMAGE_COLLOPEN ": / images / library.png"
# define IMAGE_COPYLINK ": / images / copyrslink.png"
2007-11-14 22:18:48 -05:00
/* Key for UI Preferences */
# define UI_PREF_ADVANCED_SEARCH "UIOptions / AdvancedSearch"
/* indicies for search summary item columns SS_ = Search Summary */
2014-05-29 10:49:45 -04:00
# define SS_KEYWORDS_COL 0
# define SS_RESULTS_COL 1
2007-11-14 22:18:48 -05:00
# define SS_SEARCH_ID_COL 2
2012-04-12 15:30:41 -04:00
# define SS_FILE_TYPE_COL 3
2014-05-29 10:49:45 -04:00
# define SS_COL_COUNT 3 //4 ???
# define SS_DATA_COL SS_KEYWORDS_COL
2011-12-10 18:41:58 -05:00
2012-04-12 15:30:41 -04:00
# define ROLE_KEYWORDS Qt::UserRole
2012-06-09 10:14:04 -04:00
# define ROLE_SORT Qt::UserRole + 1
2007-11-14 22:18:48 -05:00
/* static members */
/* These indices MUST be identical to their equivalent indices in the combobox */
const int SearchDialog : : FILETYPE_IDX_ANY = 0 ;
2008-11-18 17:59:33 -05:00
const int SearchDialog : : FILETYPE_IDX_ARCHIVE = 1 ;
const int SearchDialog : : FILETYPE_IDX_AUDIO = 2 ;
const int SearchDialog : : FILETYPE_IDX_CDIMAGE = 3 ;
const int SearchDialog : : FILETYPE_IDX_DOCUMENT = 4 ;
const int SearchDialog : : FILETYPE_IDX_PICTURE = 5 ;
const int SearchDialog : : FILETYPE_IDX_PROGRAM = 6 ;
const int SearchDialog : : FILETYPE_IDX_VIDEO = 7 ;
2009-08-25 16:49:50 -04:00
const int SearchDialog : : FILETYPE_IDX_DIRECTORY = 8 ;
2007-11-14 22:18:48 -05:00
QMap < int , QString > * SearchDialog : : FileTypeExtensionMap = new QMap < int , QString > ( ) ;
bool SearchDialog : : initialised = false ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
/** Constructor */
SearchDialog : : SearchDialog ( QWidget * parent )
2009-08-25 16:49:50 -04:00
: MainPage ( parent ) ,
advSearchDialog ( NULL ) ,
2007-11-14 22:18:48 -05:00
nextSearchId ( 1 )
{
/* Invoke the Qt Designer generated object setup routine */
ui . setupUi ( this ) ;
2011-02-17 11:30:59 -05:00
m_bProcessSettings = false ;
2014-05-29 10:49:45 -04:00
_queueIsAlreadyTakenCareOf = false ;
2009-08-13 08:16:35 -04:00
ui . lineEdit - > setFocus ( ) ;
2014-05-29 10:49:45 -04:00
collCreateAct = new QAction ( QIcon ( IMAGE_COLLCREATE ) , tr ( " Create Collection... " ) , this ) ;
connect ( collCreateAct , SIGNAL ( triggered ( ) ) , this , SLOT ( collCreate ( ) ) ) ;
collModifAct = new QAction ( QIcon ( IMAGE_COLLMODIF ) , tr ( " Modify Collection... " ) , this ) ;
connect ( collModifAct , SIGNAL ( triggered ( ) ) , this , SLOT ( collModif ( ) ) ) ;
collViewAct = new QAction ( QIcon ( IMAGE_COLLVIEW ) , tr ( " View Collection... " ) , this ) ;
connect ( collViewAct , SIGNAL ( triggered ( ) ) , this , SLOT ( collView ( ) ) ) ;
collOpenAct = new QAction ( QIcon ( IMAGE_COLLOPEN ) , tr ( " Download from collection file... " ) , this ) ;
connect ( collOpenAct , SIGNAL ( triggered ( ) ) , this , SLOT ( collOpen ( ) ) ) ;
2007-11-14 22:18:48 -05:00
/* initialise the filetypes mapping */
if ( ! SearchDialog : : initialised )
{
2014-05-29 10:49:45 -04:00
initialiseFileTypeMappings ( ) ;
2007-11-14 22:18:48 -05:00
}
2009-08-25 16:49:50 -04:00
connect ( ui . toggleAdvancedSearchBtn , SIGNAL ( clicked ( ) ) , this , SLOT ( showAdvSearchDialog ( ) ) ) ;
2007-11-14 22:18:48 -05:00
2014-05-29 10:49:45 -04:00
connect ( ui . searchResultWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( searchResultWidgetCustomPopupMenu ( QPoint ) ) ) ;
2009-08-25 16:49:50 -04:00
2014-05-29 10:49:45 -04:00
connect ( ui . searchSummaryWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( searchSummaryWidgetCustomPopupMenu ( QPoint ) ) ) ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
connect ( ui . lineEdit , SIGNAL ( returnPressed ( void ) ) , this , SLOT ( searchKeywords ( void ) ) ) ;
2010-02-14 10:34:53 -05:00
connect ( ui . lineEdit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( checkText ( const QString & ) ) ) ;
2012-09-07 13:30:23 -04:00
connect ( ui . pushButtonSearch , SIGNAL ( released ( void ) ) , this , SLOT ( searchKeywords ( void ) ) ) ;
2007-11-14 22:18:48 -05:00
connect ( ui . pushButtonDownload , SIGNAL ( released ( void ) ) , this , SLOT ( download ( void ) ) ) ;
2009-05-10 18:34:38 -04:00
connect ( ui . cloaseallsearchresultsButton , SIGNAL ( clicked ( ) ) , this , SLOT ( searchRemoveAll ( ) ) ) ;
connect ( ui . searchResultWidget , SIGNAL ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) , this , SLOT ( download ( ) ) ) ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
connect ( ui . searchSummaryWidget , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) ,
this , SLOT ( selectSearchResults ( void ) ) ) ;
2009-08-25 16:49:50 -04:00
2012-04-12 15:30:41 -04:00
connect ( ui . FileTypeComboBox , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( selectFileType ( int ) ) ) ;
2011-02-16 15:07:06 -05:00
2012-04-12 15:30:41 -04:00
connect ( ui . filterLineEdit , SIGNAL ( textChanged ( QString ) ) , this , SLOT ( filterItems ( ) ) ) ;
2012-10-29 06:59:26 -04:00
connect ( ui . filterLineEdit , SIGNAL ( filterChanged ( int ) ) , this , SLOT ( filterItems ( ) ) ) ;
2007-11-14 22:18:48 -05:00
2012-06-09 10:14:04 -04:00
compareSummaryRole = new RSTreeWidgetItemCompareRole ;
2014-05-29 10:49:45 -04:00
compareSummaryRole - > setRole ( SS_RESULTS_COL , ROLE_SORT ) ;
2012-06-09 10:14:04 -04:00
compareResultRole = new RSTreeWidgetItemCompareRole ;
compareResultRole - > setRole ( SR_SIZE_COL , ROLE_SORT ) ;
compareResultRole - > setRole ( SR_AGE_COL , ROLE_SORT ) ;
2014-05-29 10:49:45 -04:00
compareResultRole - > setRole ( SR_SOURCES_COL , ROLE_SORT ) ;
2012-06-09 10:14:04 -04:00
2007-11-14 22:18:48 -05:00
/* hide the Tree +/- */
2010-02-06 20:37:06 -05:00
ui . searchResultWidget - > setRootIsDecorated ( true ) ;
2008-12-12 16:41:34 -05:00
ui . searchResultWidget - > setColumnHidden ( SR_UID_COL , true ) ;
2007-11-14 22:18:48 -05:00
ui . searchSummaryWidget - > setRootIsDecorated ( false ) ;
2010-05-02 17:37:54 -04:00
// We set some delegates to handle the display of size and date.
2014-05-29 10:49:45 -04:00
// To allow a proper sorting, be careful to pad at right with spaces. This
// is achieved by using QString("%1").arg(number,15,10).
//
ui . searchResultWidget - > setItemDelegateForColumn ( SR_SIZE_COL , new RSHumanReadableSizeDelegate ( ) ) ;
ui . searchResultWidget - > setItemDelegateForColumn ( SR_AGE_COL , new RSHumanReadableAgeDelegate ( ) ) ;
2010-05-02 17:37:54 -04:00
2007-11-14 22:18:48 -05:00
/* make it extended selection */
ui . searchResultWidget - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
/* Set header resize modes and initial section sizes */
2014-05-29 10:49:45 -04:00
ui . searchSummaryWidget - > setColumnCount ( SS_COL_COUNT ) ;
ui . searchSummaryWidget - > setColumnHidden ( SS_SEARCH_ID_COL , true ) ;
2009-08-25 16:49:50 -04:00
QHeaderView * _smheader = ui . searchSummaryWidget - > header ( ) ;
2014-05-29 10:49:45 -04:00
QHeaderView_setSectionResizeMode ( _smheader , SS_KEYWORDS_COL , QHeaderView : : Interactive ) ;
QHeaderView_setSectionResizeMode ( _smheader , SS_RESULTS_COL , QHeaderView : : Interactive ) ;
2009-08-25 16:49:50 -04:00
2014-05-29 10:49:45 -04:00
_smheader - > resizeSection ( SS_KEYWORDS_COL , 160 ) ;
_smheader - > resizeSection ( SS_RESULTS_COL , 50 ) ;
2007-11-14 22:18:48 -05:00
2014-05-29 10:49:45 -04:00
ui . searchResultWidget - > setColumnCount ( SR_COL_COUNT ) ;
2009-08-25 16:49:50 -04:00
_smheader = ui . searchResultWidget - > header ( ) ;
2014-05-29 10:49:45 -04:00
QHeaderView_setSectionResizeMode ( _smheader , SR_NAME_COL , QHeaderView : : Interactive ) ;
QHeaderView_setSectionResizeMode ( _smheader , SR_SIZE_COL , QHeaderView : : Interactive ) ;
QHeaderView_setSectionResizeMode ( _smheader , SR_SOURCES_COL , QHeaderView : : Interactive ) ;
2009-08-25 16:49:50 -04:00
2014-05-29 10:49:45 -04:00
_smheader - > resizeSection ( SR_NAME_COL , 240 ) ;
_smheader - > resizeSection ( SR_SIZE_COL , 75 ) ;
_smheader - > resizeSection ( SR_SOURCES_COL , 75 ) ;
_smheader - > resizeSection ( SR_TYPE_COL , 75 ) ;
_smheader - > resizeSection ( SR_AGE_COL , 90 ) ;
_smheader - > resizeSection ( SR_HASH_COL , 240 ) ;
2008-11-18 17:59:33 -05:00
2008-02-01 13:55:21 -05:00
// set header text aligment
2010-02-27 20:03:37 -05:00
QTreeWidgetItem * headerItem = ui . searchResultWidget - > headerItem ( ) ;
2014-05-29 10:49:45 -04:00
headerItem - > setTextAlignment ( SR_NAME_COL , Qt : : AlignRight | Qt : : AlignRight ) ;
headerItem - > setTextAlignment ( SR_SIZE_COL , Qt : : AlignRight | Qt : : AlignRight ) ;
2010-02-27 20:03:37 -05:00
ui . searchResultWidget - > sortItems ( SR_NAME_COL , Qt : : AscendingOrder ) ;
2011-08-15 19:42:07 -04:00
/* Set initial size the splitter */
QList < int > sizes ;
sizes < < 250 < < width ( ) ; // Qt calculates the right sizes
ui . splitter - > setSizes ( sizes ) ;
2012-10-29 06:59:26 -04:00
/* add filter actions */
ui . filterLineEdit - > addFilter ( QIcon ( ) , tr ( " File Name " ) , SR_NAME_COL ) ;
2014-05-29 10:49:45 -04:00
//ui.filterLineEdit->addFilter(QIcon(), tr("File Size"), SR_SIZE_COL);
2012-10-29 06:59:26 -04:00
ui . filterLineEdit - > setCurrentFilter ( SR_NAME_COL ) ;
2011-08-15 19:42:07 -04:00
// load settings
2011-02-17 11:30:59 -05:00
processSettings ( true ) ;
2010-02-27 20:03:37 -05:00
ui . _ownFiles_CB - > setMinimumWidth ( 20 ) ;
ui . _friendListsearch_SB - > setMinimumWidth ( 20 ) ;
2014-05-29 10:49:45 -04:00
ui . _anonF2Fsearch_CB - > setMinimumWidth ( 20 ) ;
ui . label - > setMinimumWidth ( 20 ) ;
2007-11-14 22:18:48 -05:00
2014-05-29 10:49:45 -04:00
// workaround for Qt bug, be solved in next Qt release 4.7.0
// https://bugreports.qt-project.org/browse/QTBUG-8270
2011-05-04 06:22:49 -04:00
QShortcut * Shortcut = new QShortcut ( QKeySequence ( Qt : : Key_Delete ) , ui . searchSummaryWidget , 0 , 0 , Qt : : WidgetShortcut ) ;
connect ( Shortcut , SIGNAL ( activated ( ) ) , this , SLOT ( searchRemove ( ) ) ) ;
2012-09-07 13:30:23 -04:00
checkText ( ui . lineEdit - > text ( ) ) ;
2007-11-14 22:18:48 -05:00
/* Hide platform specific features */
# ifdef Q_WS_WIN
# endif
}
2011-02-17 11:30:59 -05:00
SearchDialog : : ~ SearchDialog ( )
{
// save settings
processSettings ( false ) ;
2012-06-09 10:14:04 -04:00
if ( compareSummaryRole ) {
delete ( compareSummaryRole ) ;
}
if ( compareResultRole ) {
delete ( compareResultRole ) ;
}
2011-02-17 11:30:59 -05:00
}
void SearchDialog : : processSettings ( bool bLoad )
{
m_bProcessSettings = true ;
QHeaderView * pHeader = ui . searchSummaryWidget - > header ( ) ;
Settings - > beginGroup ( QString ( " SearchDialog " ) ) ;
if ( bLoad ) {
// load settings
// state of SearchSummary tree
pHeader - > restoreState ( Settings - > value ( " SearchSummaryTree " ) . toByteArray ( ) ) ;
// state of splitter
ui . splitter - > restoreState ( Settings - > value ( " Splitter " ) . toByteArray ( ) ) ;
} else {
// save settings
// state of SearchSummary tree
Settings - > setValue ( " SearchSummaryTree " , pHeader - > saveState ( ) ) ;
// state of splitter
Settings - > setValue ( " Splitter " , ui . splitter - > saveState ( ) ) ;
}
Settings - > endGroup ( ) ;
m_bProcessSettings = false ;
}
2010-02-14 10:34:53 -05:00
void SearchDialog : : checkText ( const QString & txt )
{
2012-09-07 13:30:23 -04:00
bool valid ;
QColor color ;
2010-02-14 12:58:22 -05:00
if ( txt . length ( ) < 3 )
2010-02-14 10:34:53 -05:00
{
std : : cout < < " setting palette 1 " < < std : : endl ;
2012-09-07 13:30:23 -04:00
valid = false ;
color = QApplication : : palette ( ) . color ( QPalette : : Disabled , QPalette : : Base ) ;
2010-02-14 10:34:53 -05:00
}
else
{
std : : cout < < " setting palette 2 " < < std : : endl ;
2012-09-07 13:30:23 -04:00
valid = true ;
color = QApplication : : palette ( ) . color ( QPalette : : Active , QPalette : : Base ) ;
2010-02-14 10:34:53 -05:00
}
2012-09-07 13:30:23 -04:00
/* unpolish widget to clear the stylesheet's palette cache */
ui . searchLineFrame - > style ( ) - > unpolish ( ui . searchLineFrame ) ;
QPalette palette = ui . lineEdit - > palette ( ) ;
palette . setColor ( ui . lineEdit - > backgroundRole ( ) , color ) ;
ui . lineEdit - > setPalette ( palette ) ;
ui . searchLineFrame - > setProperty ( " valid " , valid ) ;
Rshare : : refreshStyleSheet ( ui . searchLineFrame , false ) ;
2010-02-14 10:34:53 -05:00
}
2007-11-14 22:18:48 -05:00
void SearchDialog : : initialiseFileTypeMappings ( )
{
/* edit these strings to change the range of extensions recognised by the search */
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_ANY , " " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_AUDIO ,
2010-12-09 15:49:41 -05:00
" aac aif flac iff m3u m4a mid midi mp3 mpa ogg ra ram wav wma " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_ARCHIVE ,
2008-11-18 17:59:33 -05:00
" 7z bz2 gz pkg rar sea sit sitx tar zip " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_CDIMAGE ,
2008-11-18 17:59:33 -05:00
" iso nrg mdf " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_DOCUMENT ,
" doc odt ott rtf pdf ps txt log msg wpd wps " ) ;
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_PICTURE ,
2007-11-19 05:56:12 -05:00
" 3dm 3dmf ai bmp drw dxf eps gif ico indd jpe jpeg jpg mng pcx pcc pct pgm "
" pix png psd psp qxd qxprgb sgi svg tga tif tiff xbm xcf " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_PROGRAM ,
2007-11-14 22:18:48 -05:00
" app bat cgi com bin exe js pif py pl sh vb ws " ) ;
2009-08-25 16:49:50 -04:00
SearchDialog : : FileTypeExtensionMap - > insert ( FILETYPE_IDX_VIDEO ,
2008-11-18 17:59:33 -05:00
" 3gp asf asx avi mov mp4 mkv flv mpeg mpg qt rm swf vob wmv " ) ;
2007-11-14 22:18:48 -05:00
SearchDialog : : initialised = true ;
}
2014-05-29 10:49:45 -04:00
void SearchDialog : : searchResultWidgetCustomPopupMenu ( QPoint /*point*/ )
2007-11-14 22:18:48 -05:00
{
2014-05-29 10:49:45 -04:00
// Block the popup if no results available
2014-10-21 18:33:02 -04:00
if ( ( ui . searchResultWidget - > selectedItems ( ) ) . isEmpty ( ) ) return ;
2014-05-29 10:49:45 -04:00
bool add_CollActions = false ;
QMenu contextMnu ( this ) ;
contextMnu . addAction ( QIcon ( IMAGE_START ) , tr ( " Download " ) , this , SLOT ( download ( ) ) ) ;
contextMnu . addSeparator ( ) ; //--------------------------------------
contextMnu . addAction ( QIcon ( IMAGE_COPYLINK ) , tr ( " Copy RetroShare Link " ) , this , SLOT ( copyResultLink ( ) ) ) ;
contextMnu . addAction ( QIcon ( IMAGE_COPYLINK ) , tr ( " Send RetroShare Link " ) , this , SLOT ( sendLinkTo ( ) ) ) ;
contextMnu . addSeparator ( ) ; //--------------------------------------
QMenu collectionMenu ( tr ( " Collection " ) , this ) ;
collectionMenu . setIcon ( QIcon ( IMAGE_LIBRARY ) ) ;
collectionMenu . addAction ( collCreateAct ) ;
collectionMenu . addAction ( collModifAct ) ;
collectionMenu . addAction ( collViewAct ) ;
collectionMenu . addAction ( collOpenAct ) ;
//contextMnu.addAction(tr("Broadcast on Channel"), this, SLOT(broadcastonchannel()));
//contextMnu.addAction(tr("Recommend to Friends"), this, SLOT(recommendtofriends()));
if ( ui . searchResultWidget - > selectedItems ( ) . size ( ) = = 1 ) {
QList < QTreeWidgetItem * > item = ui . searchResultWidget - > selectedItems ( ) ;
if ( item . at ( 0 ) - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) {
contextMnu . addAction ( QIcon ( IMAGE_OPENFOLDER ) , tr ( " Open Folder " ) , this , SLOT ( openFolderSearch ( ) ) ) ;
if ( item . at ( 0 ) - > text ( SR_NAME_COL ) . endsWith ( RsCollectionFile : : ExtensionString ) ) {
add_CollActions = true ;
} //if (item.at(0)->text(SR_NAME_COL).endsWith(RsCollectionFile::ExtensionString))
} //if (item.at(0)->data(SR_DATA_COL, SR_ROLE_LOCAL).toBool())
} //if (ui.searchResultWidget->selectedItems().size() == 1)
collCreateAct - > setEnabled ( true ) ;
collModifAct - > setEnabled ( add_CollActions ) ;
collViewAct - > setEnabled ( add_CollActions ) ;
collOpenAct - > setEnabled ( true ) ;
contextMnu . addMenu ( & collectionMenu ) ;
contextMnu . exec ( QCursor : : pos ( ) ) ;
2007-11-14 22:18:48 -05:00
}
2014-03-17 16:56:06 -04:00
void SearchDialog : : getSourceFriendsForHash ( const RsFileHash & hash , std : : list < RsPeerId > & srcIds )
2010-05-29 09:17:09 -04:00
{
std : : cerr < < " Searching sources for file " < < hash < < std : : endl ;
srcIds . clear ( ) ;
FileInfo finfo ;
rsFiles - > FileDetails ( hash , RS_FILE_HINTS_REMOTE , finfo ) ;
for ( std : : list < TransferInfo > : : const_iterator it ( finfo . peers . begin ( ) ) ; it ! = finfo . peers . end ( ) ; + + it )
{
std : : cerr < < " adding peerid " < < ( * it ) . peerId < < std : : endl ;
srcIds . push_back ( ( * it ) . peerId ) ;
}
}
2007-11-14 22:18:48 -05:00
void SearchDialog : : download ( )
{
2014-05-29 10:49:45 -04:00
/* should also be able to handle multi-selection */
QList < QTreeWidgetItem * > itemsForDownload = ui . searchResultWidget - > selectedItems ( ) ;
int numdls = itemsForDownload . size ( ) ;
QTreeWidgetItem * item ;
bool attemptDownloadLocal = false ;
for ( int i = 0 ; i < numdls ; + + i ) {
item = itemsForDownload . at ( i ) ;
// call the download
// *
if ( item - > text ( SR_HASH_COL ) . isEmpty ( ) ) { // we have a folder
downloadDirectory ( item , tr ( " " ) ) ;
} else {
std : : cerr < < " SearchDialog::download() Calling File Request " < < std : : endl ;
std : : list < RsPeerId > srcIds ;
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
getSourceFriendsForHash ( hash , srcIds ) ;
if ( ! rsFiles - > FileRequest ( ( item - > text ( SR_NAME_COL ) ) . toUtf8 ( ) . constData ( )
, hash , ( item - > text ( SR_SIZE_COL ) ) . toULongLong ( )
, " " , RS_FILE_REQ_ANONYMOUS_ROUTING , srcIds ) ) {
attemptDownloadLocal = true ;
} else {
std : : cout < < " isuing file request from search dialog: - "
< < ( item - > text ( SR_NAME_COL ) ) . toStdString ( )
< < " - " < < hash < < " - " < < ( item - > text ( SR_SIZE_COL ) ) . toULongLong ( ) < < " -ids= " ;
for ( std : : list < RsPeerId > : : const_iterator it ( srcIds . begin ( ) ) ; it ! = srcIds . end ( ) ; + + it ) {
std : : cout < < * it < < " - " < < std : : endl ;
} //for(std::list<RsPeerId>::const_iterator
} //if(!rsFiles -> FileRequest(
} //if (item->text(SR_HASH_COL).isEmpty())
} //for (int i = 0
if ( attemptDownloadLocal ) {
QMessageBox : : information ( this , tr ( " Download Notice " ) , tr ( " Skipping Local Files " ) ) ;
} //if (attemptDownloadLocal)
}
void SearchDialog : : collCreate ( )
{
std : : vector < DirDetails > dirVec ;
QList < QTreeWidgetItem * > selectedItems = ui . searchResultWidget - > selectedItems ( ) ;
int selectedCount = selectedItems . size ( ) ;
QTreeWidgetItem * item ;
for ( int i = 0 ; i < selectedCount ; + + i ) {
item = selectedItems . at ( i ) ;
if ( ! item - > text ( SR_HASH_COL ) . isEmpty ( ) ) {
std : : string name = item - > text ( SR_NAME_COL ) . toUtf8 ( ) . constData ( ) ;
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
uint64_t count = item - > text ( SR_SIZE_COL ) . toULongLong ( ) ;
DirDetails details ;
details . name = name ;
details . hash = hash ;
details . count = count ;
details . type = DIR_TYPE_FILE ;
dirVec . push_back ( details ) ;
} //if (!item->text(SR_HASH_COL).isEmpty())
} //for (int i = 0; i < numdls; ++i)
RsCollectionFile ( dirVec ) . openNewColl ( this ) ;
}
void SearchDialog : : collModif ( )
{
FileInfo info ;
QList < QTreeWidgetItem * > selectedItems = ui . searchResultWidget - > selectedItems ( ) ;
if ( selectedItems . size ( ) ! = 1 ) return ;
QTreeWidgetItem * item ;
item = selectedItems . at ( 0 ) ;
if ( ! item - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) return ;
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
if ( ! rsFiles - > FileDetails ( hash , RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL
| RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE
| RS_FILE_HINTS_SPEC_ONLY , info ) ) return ;
/* make path for downloaded files */
std : : string path ;
path = info . path ;
/* open file with a suitable application */
QFileInfo qinfo ;
qinfo . setFile ( QString : : fromUtf8 ( path . c_str ( ) ) ) ;
if ( qinfo . exists ( ) ) {
if ( qinfo . absoluteFilePath ( ) . endsWith ( RsCollectionFile : : ExtensionString ) ) {
RsCollectionFile collection ;
collection . openColl ( qinfo . absoluteFilePath ( ) ) ;
} //if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString))
} //if (qinfo.exists())
}
void SearchDialog : : collView ( )
{
FileInfo info ;
QList < QTreeWidgetItem * > selectedItems = ui . searchResultWidget - > selectedItems ( ) ;
if ( selectedItems . size ( ) ! = 1 ) return ;
QTreeWidgetItem * item ;
item = selectedItems . at ( 0 ) ;
if ( ! item - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) return ;
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
if ( ! rsFiles - > FileDetails ( hash , RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL
| RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE
| RS_FILE_HINTS_SPEC_ONLY , info ) ) return ;
/* make path for downloaded files */
std : : string path ;
path = info . path ;
/* open file with a suitable application */
QFileInfo qinfo ;
qinfo . setFile ( QString : : fromUtf8 ( path . c_str ( ) ) ) ;
if ( qinfo . exists ( ) ) {
if ( qinfo . absoluteFilePath ( ) . endsWith ( RsCollectionFile : : ExtensionString ) ) {
RsCollectionFile collection ;
collection . openColl ( qinfo . absoluteFilePath ( ) , true ) ;
} //if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString))
} //if (qinfo.exists())
}
void SearchDialog : : collOpen ( )
{
FileInfo info ;
QList < QTreeWidgetItem * > selectedItems = ui . searchResultWidget - > selectedItems ( ) ;
if ( selectedItems . size ( ) = = 1 ) {
QTreeWidgetItem * item ;
item = selectedItems . at ( 0 ) ;
if ( item - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) {
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
if ( rsFiles - > FileDetails ( hash , RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL
| RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE
| RS_FILE_HINTS_SPEC_ONLY , info ) ) {
/* make path for downloaded files */
std : : string path ;
path = info . path ;
/* open file with a suitable application */
QFileInfo qinfo ;
qinfo . setFile ( QString : : fromUtf8 ( path . c_str ( ) ) ) ;
if ( qinfo . exists ( ) ) {
if ( qinfo . absoluteFilePath ( ) . endsWith ( RsCollectionFile : : ExtensionString ) ) {
RsCollectionFile collection ;
if ( collection . load ( qinfo . absoluteFilePath ( ) , this ) ) {
collection . downloadFiles ( ) ;
return ;
} //if (collection.load(this))
} //if (qinfo.absoluteFilePath().endsWith(RsCollectionFile::ExtensionString))
} //if (qinfo.exists())
} //if (!rsFiles->FileDetails(
} //if (!item->data(SR_DATA_COL, SR_ROLE_LOCAL).toBool())
} //if (selectedItems.size() != 1)
RsCollectionFile collection ;
if ( collection . load ( this ) ) {
collection . downloadFiles ( ) ;
} //if (collection.load(this))
2007-11-14 22:18:48 -05:00
}
2009-09-25 08:17:28 -04:00
void SearchDialog : : downloadDirectory ( const QTreeWidgetItem * item , const QString & base )
{
2010-05-02 17:37:54 -04:00
if ( ! item - > childCount ( ) )
{
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > srcIds ;
2009-09-25 08:17:28 -04:00
QString path = QString : : fromStdString ( rsFiles - > getDownloadDirectory ( ) )
2010-09-12 20:27:50 -04:00
+ " / " + base + " / " ;
2009-09-25 08:17:28 -04:00
QString cleanPath = QDir : : cleanPath ( path ) ;
2014-03-17 16:56:06 -04:00
RsFileHash hash ( item - > text ( SR_HASH_COL ) . toStdString ( ) ) ;
2011-02-19 19:59:47 -05:00
getSourceFriendsForHash ( hash , srcIds ) ;
2010-05-29 09:17:09 -04:00
2011-10-11 19:29:38 -04:00
rsFiles - > FileRequest ( item - > text ( SR_NAME_COL ) . toUtf8 ( ) . constData ( ) ,
2011-02-19 19:59:47 -05:00
hash ,
2010-05-02 17:37:54 -04:00
item - > text ( SR_SIZE_COL ) . toULongLong ( ) ,
2012-11-02 09:52:29 -04:00
cleanPath . toUtf8 ( ) . constData ( ) , RS_FILE_REQ_ANONYMOUS_ROUTING , srcIds ) ;
2009-09-25 08:17:28 -04:00
std : : cout < < " SearchDialog::downloadDirectory(): " \
" issuing file request from search dialog: - "
< < ( item - > text ( SR_NAME_COL ) ) . toStdString ( )
2011-02-19 19:59:47 -05:00
< < " - " < < hash
2010-05-02 17:37:54 -04:00
< < " - " < < ( item - > text ( SR_SIZE_COL ) ) . toULongLong ( )
2009-09-25 08:17:28 -04:00
< < " -ids= " ;
2014-03-17 16:56:06 -04:00
for ( std : : list < RsPeerId > : : const_iterator it ( srcIds . begin ( ) ) ;
2009-09-25 08:17:28 -04:00
it ! = srcIds . end ( ) ; + + it )
std : : cout < < * it < < " - " < < std : : endl ;
} else {
QDir dwlDir ( QString : : fromStdString ( rsFiles - > getDownloadDirectory ( ) ) ) ;
QString path ;
if ( base = = tr ( " " ) )
path = item - > text ( SR_NAME_COL ) ;
else
2010-09-12 20:27:50 -04:00
path = base + " / " + item - > text ( SR_NAME_COL ) ;
2009-09-25 08:17:28 -04:00
QString cleanPath = QDir : : cleanPath ( path ) ;
// create this folder in download path
if ( ! dwlDir . mkpath ( cleanPath ) ) {
std : : cerr < < " SearchDialog::downloadDirectory() - can't create "
< < cleanPath . toStdString ( ) < < " directory " < < std : : endl ;
return ;
}
// recursive call for every child - file or folder
2014-10-21 18:33:02 -04:00
for ( int i = 0 , cnt = item - > childCount ( ) ; i < cnt ; + + i ) {
2009-09-25 08:17:28 -04:00
QTreeWidgetItem * child = item - > child ( i ) ;
downloadDirectory ( child , path ) ;
}
}
}
2007-11-14 22:18:48 -05:00
void SearchDialog : : broadcastonchannel ( )
{
QMessageBox : : warning ( 0 , tr ( " Sorry " ) , tr ( " This function is not yet implemented. " ) ) ;
}
void SearchDialog : : recommendtofriends ( )
{
QMessageBox : : warning ( 0 , tr ( " Sorry " ) , tr ( " This function is not yet implemented. " ) ) ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
}
/** context menu searchTablewidget2 **/
2014-05-29 10:49:45 -04:00
void SearchDialog : : searchSummaryWidgetCustomPopupMenu ( QPoint /*point*/ )
2007-11-14 22:18:48 -05:00
{
// block the popup if no results available
2014-10-21 18:33:02 -04:00
if ( ( ui . searchSummaryWidget - > selectedItems ( ) ) . isEmpty ( ) ) return ;
2009-08-25 16:49:50 -04:00
2010-07-15 07:25:34 -04:00
QMenu contextMnu ( this ) ;
2011-12-10 18:41:58 -05:00
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
QAction * action = contextMnu . addAction ( tr ( " Search again " ) , this , SLOT ( searchAgain ( ) ) ) ;
2012-04-12 15:30:41 -04:00
if ( ! ci | | ci - > data ( SS_DATA_COL , ROLE_KEYWORDS ) . toString ( ) . isEmpty ( ) ) {
2011-12-10 18:41:58 -05:00
action - > setDisabled ( true ) ;
}
2011-05-04 06:22:49 -04:00
contextMnu . addAction ( QIcon ( IMAGE_REMOVE ) , tr ( " Remove " ) , this , SLOT ( searchRemove ( ) ) ) ;
contextMnu . addAction ( QIcon ( IMAGE_REMOVE ) , tr ( " Remove All " ) , this , SLOT ( searchRemoveAll ( ) ) ) ;
contextMnu . addSeparator ( ) ;
2011-12-10 18:41:58 -05:00
action = contextMnu . addAction ( QIcon ( IMAGE_COPYLINK ) , tr ( " Copy RetroShare Link " ) , this , SLOT ( copySearchLink ( ) ) ) ;
2012-04-12 15:30:41 -04:00
if ( ! ci | | ci - > data ( SS_DATA_COL , ROLE_KEYWORDS ) . toString ( ) . isEmpty ( ) ) {
2011-12-10 18:41:58 -05:00
action - > setDisabled ( true ) ;
}
2009-08-25 16:49:50 -04:00
2010-05-14 16:55:44 -04:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
2007-11-14 22:18:48 -05:00
}
/** remove selected search result **/
void SearchDialog : : searchRemove ( )
{
/* get the current search id from the summary window */
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
if ( ! ci )
return ;
/* get the searchId text */
QString searchId = ci - > text ( SS_SEARCH_ID_COL ) ;
std : : cerr < < " SearchDialog::searchRemove(): searchId: " < < searchId . toStdString ( ) ;
std : : cerr < < std : : endl ;
/* show only matching searchIds in main window */
int items = ui . searchResultWidget - > topLevelItemCount ( ) ;
for ( int i = 0 ; i < items ; )
{
/* get item */
QTreeWidgetItem * ti = ui . searchResultWidget - > topLevelItem ( i ) ;
if ( ti - > text ( SR_SEARCH_ID_COL ) = = searchId )
{
/* remove */
delete ( ui . searchResultWidget - > takeTopLevelItem ( i ) ) ;
items - - ;
}
else
{
/* step to the next */
2014-10-21 18:33:02 -04:00
+ + i ;
2007-11-14 22:18:48 -05:00
}
}
int sii = ui . searchSummaryWidget - > indexOfTopLevelItem ( ci ) ;
if ( sii ! = - 1 )
{
delete ( ui . searchSummaryWidget - > takeTopLevelItem ( sii ) ) ;
}
ui . searchResultWidget - > update ( ) ;
ui . searchSummaryWidget - > update ( ) ;
}
/** remove all search results **/
void SearchDialog : : searchRemoveAll ( )
{
2007-11-19 05:56:12 -05:00
ui . searchResultWidget - > clear ( ) ;
ui . searchSummaryWidget - > clear ( ) ;
2010-03-25 15:20:16 -04:00
ui . FileTypeComboBox - > setCurrentIndex ( 0 ) ;
2007-11-19 05:56:12 -05:00
nextSearchId = 1 ;
2007-11-14 22:18:48 -05:00
}
2011-05-04 06:22:49 -04:00
void SearchDialog : : copySearchLink ( )
{
/* get the current search id from the summary window */
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
if ( ! ci )
return ;
/* get the keywords */
2014-05-29 10:49:45 -04:00
QString keywords = ci - > text ( SS_KEYWORDS_COL ) ;
2011-05-04 06:22:49 -04:00
std : : cerr < < " SearchDialog::copySearchLink(): keywords: " < < keywords . toStdString ( ) ;
std : : cerr < < std : : endl ;
RetroShareLink link ;
if ( link . createSearch ( keywords ) ) {
2011-09-11 18:07:24 -04:00
QList < RetroShareLink > urls ;
2011-05-04 06:22:49 -04:00
urls . push_back ( link ) ;
RSLinkClipboard : : copyLinks ( urls ) ;
}
}
2009-05-10 18:34:38 -04:00
2007-11-14 22:18:48 -05:00
/* *****************************************************************
Advanced search implementation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// Event handlers for hide and show events
void SearchDialog : : hideEvent ( QHideEvent * event )
{
showAdvSearchDialog ( false ) ;
MainPage : : hideEvent ( event ) ;
}
void SearchDialog : : toggleAdvancedSearchDialog ( bool toggled )
{
// record the users preference for future reference
QString key ( UI_PREF_ADVANCED_SEARCH ) ;
2010-05-20 17:53:27 -04:00
Settings - > setValue ( key , QVariant ( toggled ) ) ;
2009-08-25 16:49:50 -04:00
2007-11-14 22:18:48 -05:00
showAdvSearchDialog ( toggled ) ;
}
void SearchDialog : : showAdvSearchDialog ( bool show )
{
// instantiate if about to show for the first time
if ( advSearchDialog = = 0 & & show )
{
advSearchDialog = new AdvancedSearchDialog ( ) ;
connect ( advSearchDialog , SIGNAL ( search ( Expression * ) ) ,
this , SLOT ( advancedSearch ( Expression * ) ) ) ;
}
if ( show ) {
advSearchDialog - > show ( ) ;
advSearchDialog - > raise ( ) ;
advSearchDialog - > setFocus ( ) ;
} else if ( advSearchDialog ! = 0 ) {
advSearchDialog - > hide ( ) ;
}
}
2009-09-24 17:08:55 -04:00
// Creates a new entry in the search summary, not to leave it blank whatever happens.
//
2012-04-12 15:30:41 -04:00
void SearchDialog : : initSearchResult ( const QString & txt , qulonglong searchId , int fileType , bool advanced )
2009-09-24 17:08:55 -04:00
{
QString sid_hexa = QString : : number ( searchId , 16 ) ;
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * item2 = new RSTreeWidgetItem ( compareSummaryRole ) ;
2012-04-12 15:30:41 -04:00
if ( fileType = = FILETYPE_IDX_ANY ) {
2014-05-29 10:49:45 -04:00
item2 - > setText ( SS_KEYWORDS_COL , txt ) ;
2012-04-12 15:30:41 -04:00
} else {
2014-05-29 10:49:45 -04:00
item2 - > setText ( SS_KEYWORDS_COL , txt + " ( " + ui . FileTypeComboBox - > itemText ( fileType ) + " ) " ) ;
2012-04-12 15:30:41 -04:00
}
2014-05-29 10:49:45 -04:00
item2 - > setText ( SS_RESULTS_COL , QString : : number ( 0 ) ) ;
item2 - > setData ( SS_RESULTS_COL , ROLE_SORT , 0 ) ;
2009-09-24 17:08:55 -04:00
item2 - > setText ( SS_SEARCH_ID_COL , sid_hexa ) ;
2012-04-12 15:30:41 -04:00
item2 - > setText ( SS_FILE_TYPE_COL , QString : : number ( fileType ) ) ;
2009-09-24 17:08:55 -04:00
2012-04-12 15:30:41 -04:00
if ( ! advanced )
item2 - > setData ( SS_DATA_COL , ROLE_KEYWORDS , txt ) ;
2011-12-10 18:41:58 -05:00
2009-09-24 17:08:55 -04:00
ui . searchSummaryWidget - > addTopLevelItem ( item2 ) ;
ui . searchSummaryWidget - > setCurrentItem ( item2 ) ;
}
2007-11-14 22:18:48 -05:00
void SearchDialog : : advancedSearch ( Expression * expression )
{
2011-08-27 12:15:56 -04:00
advSearchDialog - > hide ( ) ;
2007-11-14 22:18:48 -05:00
/* call to core */
2009-08-25 16:49:50 -04:00
std : : list < DirDetails > results ;
2007-11-14 22:18:48 -05:00
2009-08-25 08:04:43 -04:00
// send a turtle search request
LinearizedExpression e ;
expression - > linearize ( e ) ;
TurtleRequestId req_id = rsTurtle - > turtleSearch ( e ) ;
2009-09-24 17:08:55 -04:00
// This will act before turtle results come to the interface, thanks to the signals scheduling policy.
// The text "bool exp" should be replaced by an appropriate text describing the actual search.
2012-04-12 15:30:41 -04:00
initSearchResult ( " bool exp " , req_id , ui . FileTypeComboBox - > currentIndex ( ) , true ) ;
2009-09-24 17:08:55 -04:00
2012-11-02 09:52:29 -04:00
rsFiles - > SearchBoolExp ( expression , results , RS_FILE_HINTS_REMOTE ) ; // | DIR_FLAGS_NETWORK_WIDE | DIR_FLAGS_BROWSABLE);
2009-08-25 08:04:43 -04:00
/* abstraction to allow reusee of tree rendering code */
2012-04-12 15:30:41 -04:00
resultsToTree ( advSearchDialog - > getSearchAsString ( ) , req_id , results ) ;
2009-08-25 08:04:43 -04:00
2009-08-25 08:17:22 -04:00
// // debug stuff
// Expression *expression2 = LinearizedExpression::toExpr(e) ;
// results.clear() ;
// rsFiles -> SearchBoolExp(expression2, results, DIR_FLAGS_REMOTE | DIR_FLAGS_NETWORK_WIDE | DIR_FLAGS_BROWSABLE);
// resultsToTree((advSearchDialog->getSearchAsString()).toStdString(),req_id+1, results);
2007-11-14 22:18:48 -05:00
}
void SearchDialog : : searchKeywords ( )
2009-08-25 16:49:50 -04:00
{
2011-05-04 06:22:49 -04:00
searchKeywords ( ui . lineEdit - > text ( ) ) ;
}
2011-12-10 18:41:58 -05:00
void SearchDialog : : searchAgain ( )
{
/* get the current search text from the summary window */
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
2012-04-12 15:30:41 -04:00
if ( ! ci | | ci - > data ( SS_DATA_COL , ROLE_KEYWORDS ) . toString ( ) . isEmpty ( ) )
2011-12-10 18:41:58 -05:00
return ;
/* get the search text */
2012-04-12 15:30:41 -04:00
QString txt = ci - > data ( SS_DATA_COL , ROLE_KEYWORDS ) . toString ( ) ;
int fileType = ci - > text ( SS_FILE_TYPE_COL ) . toInt ( ) ;
/* remove the old search */
2011-12-10 18:41:58 -05:00
searchRemove ( ) ;
2012-04-12 15:30:41 -04:00
/* search for the same keywords and filetype again */
ui . FileTypeComboBox - > setCurrentIndex ( fileType ) ;
2011-12-10 18:41:58 -05:00
searchKeywords ( txt ) ;
}
2011-05-04 06:22:49 -04:00
void SearchDialog : : searchKeywords ( const QString & keywords )
{
2012-04-12 15:30:41 -04:00
if ( keywords . length ( ) < 3 )
2010-02-14 10:34:53 -05:00
return ;
2011-06-20 18:19:34 -04:00
QStringList qWords = keywords . split ( " " , QString : : SkipEmptyParts ) ;
std : : list < std : : string > words ;
QStringListIterator qWordsIter ( qWords ) ;
while ( qWordsIter . hasNext ( ) )
2011-08-27 12:15:56 -04:00
words . push_back ( qWordsIter . next ( ) . toUtf8 ( ) . constData ( ) ) ;
2011-06-20 18:19:34 -04:00
int n = words . size ( ) ;
if ( n < 1 )
return ;
2011-06-21 17:08:56 -04:00
NameExpression exprs ( ContainsAllStrings , words , true ) ;
2011-06-20 18:19:34 -04:00
LinearizedExpression lin_exp ;
exprs . linearize ( lin_exp ) ;
2010-02-27 16:10:54 -05:00
TurtleRequestId req_id ;
if ( ui . _anonF2Fsearch_CB - > isChecked ( ) )
2011-06-20 18:19:34 -04:00
{
if ( n = = 1 )
2012-04-12 15:30:41 -04:00
req_id = rsTurtle - > turtleSearch ( words . front ( ) ) ;
2011-06-20 18:19:34 -04:00
else
req_id = rsTurtle - > turtleSearch ( lin_exp ) ;
}
2010-02-27 16:10:54 -05:00
else
2011-08-14 17:25:41 -04:00
req_id = ( ( ( ( uint32_t ) rand ( ) ) < < 16 ) ^ 0x1e2fd5e4 ) + ( ( ( uint32_t ) rand ( ) ) ^ 0x1b19acfe ) ; // generate a random 32 bits request id
2009-08-16 16:10:53 -04:00
2012-04-12 15:30:41 -04:00
initSearchResult ( keywords , req_id , ui . FileTypeComboBox - > currentIndex ( ) , false ) ; // this will act before turtle results come to the interface, thanks to the signals scheduling policy.
2009-09-24 17:08:55 -04:00
2010-02-27 16:10:54 -05:00
if ( ui . _friendListsearch_SB - > isChecked ( ) | | ui . _ownFiles_CB - > isChecked ( ) )
2007-11-14 22:18:48 -05:00
{
2010-02-27 16:10:54 -05:00
/* extract keywords from lineEdit */
2011-06-20 18:19:34 -04:00
// make a compound expression with an AND
//
2007-11-14 22:18:48 -05:00
2010-02-27 16:10:54 -05:00
std : : list < DirDetails > finalResults ;
2009-08-25 16:49:50 -04:00
2010-02-27 16:10:54 -05:00
if ( ui . _friendListsearch_SB - > isChecked ( ) )
{
std : : list < DirDetails > initialResults ;
2007-11-14 22:18:48 -05:00
2012-11-02 09:52:29 -04:00
rsFiles - > SearchBoolExp ( & exprs , initialResults , RS_FILE_HINTS_REMOTE ) ;
2010-02-27 16:10:54 -05:00
/* which extensions do we use? */
DirDetails dd ;
2014-10-21 18:33:02 -04:00
for ( std : : list < DirDetails > : : iterator resultsIter = initialResults . begin ( ) ; resultsIter ! = initialResults . end ( ) ; + + resultsIter )
2010-02-27 16:10:54 -05:00
{
dd = * resultsIter ;
finalResults . push_back ( dd ) ;
}
}
if ( ui . _ownFiles_CB - > isChecked ( ) )
{
std : : list < DirDetails > initialResults ;
2012-11-02 09:52:29 -04:00
rsFiles - > SearchBoolExp ( & exprs , initialResults , RS_FILE_HINTS_LOCAL ) ; // | DIR_FLAGS_NETWORK_WIDE | DIR_FLAGS_BROWSABLE) ;
2010-02-27 16:10:54 -05:00
/* which extensions do we use? */
DirDetails dd ;
2014-10-21 18:33:02 -04:00
for ( std : : list < DirDetails > : : iterator resultsIter = initialResults . begin ( ) ; resultsIter ! = initialResults . end ( ) ; + + resultsIter )
2010-02-27 16:10:54 -05:00
{
dd = * resultsIter ;
finalResults . push_back ( dd ) ;
}
}
/* abstraction to allow reusee of tree rendering code */
2012-04-12 15:30:41 -04:00
resultsToTree ( keywords , req_id , finalResults ) ;
2010-02-27 16:10:54 -05:00
}
2007-11-14 22:18:48 -05:00
}
2009-08-25 16:49:50 -04:00
2009-08-16 16:10:53 -04:00
void SearchDialog : : updateFiles ( qulonglong search_id , FileDetail file )
2007-11-14 22:18:48 -05:00
{
2011-02-09 16:44:49 -05:00
searchResultsQueue . push_back ( std : : pair < qulonglong , FileDetail > ( search_id , file ) ) ;
2009-09-24 17:08:55 -04:00
2011-02-09 16:44:49 -05:00
if ( ! _queueIsAlreadyTakenCareOf )
{
QTimer : : singleShot ( 100 , this , SLOT ( processResultQueue ( ) ) ) ;
_queueIsAlreadyTakenCareOf = true ;
}
}
void SearchDialog : : processResultQueue ( )
{
2011-04-14 17:58:15 -04:00
// This avoids a deadlock when gpg callback asks a passwd.
// Send again in 10 secs.
//
if ( RsAutoUpdatePage : : eventsLocked ( ) )
{
QTimer : : singleShot ( 10000 , this , SLOT ( processResultQueue ( ) ) ) ;
return ;
}
2011-02-09 16:44:49 -05:00
int nb_treated_elements = 0 ;
2012-04-12 15:30:41 -04:00
ui . searchResultWidget - > setSortingEnabled ( false ) ;
while ( ! searchResultsQueue . empty ( ) & & nb_treated_elements + + < 250 )
2009-08-16 16:10:53 -04:00
{
2011-02-09 16:44:49 -05:00
qulonglong search_id = searchResultsQueue . back ( ) . first ;
FileDetail file = searchResultsQueue . back ( ) . second ;
2009-08-16 16:10:53 -04:00
2011-02-09 16:44:49 -05:00
searchResultsQueue . pop_back ( ) ;
2009-08-16 16:10:53 -04:00
2011-02-09 16:44:49 -05:00
# ifdef DEBUG
std : : cout < < " Updating file detail: " < < std : : endl ;
std : : cout < < " size = " < < file . size < < std : : endl ;
std : : cout < < " name = " < < file . name < < std : : endl ;
std : : cout < < " s_id = " < < search_id < < std : : endl ;
# endif
2012-04-12 15:30:41 -04:00
insertFile ( search_id , file ) ;
2009-08-16 16:10:53 -04:00
}
2012-04-12 15:30:41 -04:00
ui . searchResultWidget - > setSortingEnabled ( true ) ;
2011-02-09 16:44:49 -05:00
if ( ! searchResultsQueue . empty ( ) )
2012-04-12 15:30:41 -04:00
QTimer : : singleShot ( 500 , this , SLOT ( processResultQueue ( ) ) ) ;
2011-02-09 16:44:49 -05:00
else
_queueIsAlreadyTakenCareOf = false ;
2009-08-16 16:10:53 -04:00
}
2009-08-25 16:49:50 -04:00
2012-04-12 15:30:41 -04:00
void SearchDialog : : insertDirectory ( const QString & txt , qulonglong searchId , const DirDetails & dir , QTreeWidgetItem * item )
2009-08-25 16:49:50 -04:00
{
2009-09-24 17:08:55 -04:00
QString sid_hexa = QString : : number ( searchId , 16 ) ;
2009-09-25 08:17:28 -04:00
if ( dir . type = = DIR_TYPE_FILE )
2009-09-24 17:08:55 -04:00
{
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * child = new RSTreeWidgetItem ( compareResultRole ) ;
2009-08-25 16:49:50 -04:00
/* translate search result for a file */
2010-02-06 20:37:06 -05:00
2009-09-24 17:08:55 -04:00
child - > setText ( SR_NAME_COL , QString : : fromUtf8 ( dir . name . c_str ( ) ) ) ;
2014-03-17 16:56:06 -04:00
child - > setText ( SR_HASH_COL , QString : : fromStdString ( dir . hash . toStdString ( ) ) ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_SIZE_COL , QString : : number ( dir . count ) ) ;
2012-06-10 19:47:41 -04:00
child - > setData ( SR_SIZE_COL , ROLE_SORT , ( qulonglong ) dir . count ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_AGE_COL , QString : : number ( dir . age ) ) ;
child - > setData ( SR_AGE_COL , ROLE_SORT , dir . age ) ;
2009-08-25 16:49:50 -04:00
child - > setTextAlignment ( SR_SIZE_COL , Qt : : AlignRight ) ;
2014-05-29 10:49:45 -04:00
child - > setText ( SR_SOURCES_COL , QString : : number ( 1 ) ) ;
child - > setData ( SR_SOURCES_COL , ROLE_SORT , 1 ) ;
child - > setTextAlignment ( SR_SOURCES_COL , Qt : : AlignRight ) ;
2010-02-14 20:09:23 -05:00
2009-09-24 17:08:55 -04:00
child - > setText ( SR_SEARCH_ID_COL , sid_hexa ) ;
2011-12-07 08:08:12 -05:00
setIconAndType ( child , QString : : fromUtf8 ( dir . name . c_str ( ) ) ) ;
2009-08-25 16:49:50 -04:00
if ( item = = NULL ) {
ui . searchResultWidget - > addTopLevelItem ( child ) ;
} else {
item - > addChild ( child ) ;
}
2009-09-25 08:17:28 -04:00
}
else
2009-09-24 17:08:55 -04:00
{ /* it is a directory */
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * child = new RSTreeWidgetItem ( compareResultRole ) ;
2010-02-06 20:37:06 -05:00
child - > setIcon ( SR_NAME_COL , QIcon ( IMAGE_DIRECTORY ) ) ;
2009-09-24 17:08:55 -04:00
child - > setText ( SR_NAME_COL , QString : : fromUtf8 ( dir . name . c_str ( ) ) ) ;
2014-03-17 16:56:06 -04:00
child - > setText ( SR_HASH_COL , QString : : fromStdString ( dir . hash . toStdString ( ) ) ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_SIZE_COL , QString : : number ( dir . count ) ) ;
2012-06-10 19:47:41 -04:00
child - > setData ( SR_SIZE_COL , ROLE_SORT , ( qulonglong ) dir . count ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_AGE_COL , QString : : number ( dir . age ) ) ;
child - > setData ( SR_AGE_COL , ROLE_SORT , dir . age ) ;
2009-08-25 16:49:50 -04:00
child - > setTextAlignment ( SR_SIZE_COL , Qt : : AlignRight ) ;
2014-05-29 10:49:45 -04:00
child - > setText ( SR_SOURCES_COL , QString : : number ( 1 ) ) ;
child - > setData ( SR_SOURCES_COL , ROLE_SORT , 1 ) ;
child - > setTextAlignment ( SR_SOURCES_COL , Qt : : AlignRight ) ;
2009-09-24 17:08:55 -04:00
child - > setText ( SR_SEARCH_ID_COL , sid_hexa ) ;
2010-02-06 20:37:06 -05:00
child - > setText ( SR_TYPE_COL , tr ( " Folder " ) ) ;
2009-08-25 16:49:50 -04:00
if ( item = = NULL ) {
ui . searchResultWidget - > addTopLevelItem ( child ) ;
/* add to the summary as well */
int items = ui . searchSummaryWidget - > topLevelItemCount ( ) ;
bool found = false ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < items ; + + i )
2009-08-25 16:49:50 -04:00
{
2009-09-24 17:08:55 -04:00
if ( ui . searchSummaryWidget - > topLevelItem ( i ) - > text ( SS_SEARCH_ID_COL ) = = sid_hexa )
2009-08-25 16:49:50 -04:00
{
// increment result since every item is new
2014-05-29 10:49:45 -04:00
int s = ui . searchSummaryWidget - > topLevelItem ( i ) - > text ( SS_RESULTS_COL ) . toInt ( ) ;
ui . searchSummaryWidget - > topLevelItem ( i ) - > setText ( SS_RESULTS_COL , QString : : number ( s + 1 ) ) ;
ui . searchSummaryWidget - > topLevelItem ( i ) - > setData ( SS_RESULTS_COL , ROLE_SORT , s + 1 ) ;
2009-08-25 16:49:50 -04:00
found = true ;
}
}
if ( ! found )
{
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * item2 = new RSTreeWidgetItem ( compareSummaryRole ) ;
2014-05-29 10:49:45 -04:00
item2 - > setText ( SS_KEYWORDS_COL , txt ) ;
item2 - > setText ( SS_RESULTS_COL , QString : : number ( 1 ) ) ;
item2 - > setData ( SS_RESULTS_COL , ROLE_SORT , 1 ) ;
item2 - > setTextAlignment ( SS_RESULTS_COL , Qt : : AlignRight ) ;
2009-09-24 17:08:55 -04:00
item2 - > setText ( SS_SEARCH_ID_COL , sid_hexa ) ;
2009-08-25 16:49:50 -04:00
ui . searchSummaryWidget - > addTopLevelItem ( item2 ) ;
ui . searchSummaryWidget - > setCurrentItem ( item2 ) ;
}
/* select this search result */
selectSearchResults ( ) ;
} else {
item - > addChild ( child ) ;
}
/* go through all children directories/files for a recursive call */
2014-10-21 18:33:02 -04:00
for ( std : : list < DirStub > : : const_iterator it ( dir . children . begin ( ) ) ; it ! = dir . children . end ( ) ; + + it ) {
2009-08-25 16:49:50 -04:00
DirDetails details ;
2012-11-02 09:52:29 -04:00
rsFiles - > RequestDirDetails ( it - > ref , details , FileSearchFlags ( 0u ) ) ;
2009-08-25 16:49:50 -04:00
insertDirectory ( txt , searchId , details , child ) ;
}
}
}
2012-04-12 15:30:41 -04:00
void SearchDialog : : insertDirectory ( const QString & txt , qulonglong searchId , const DirDetails & dir )
2010-02-18 19:48:42 -05:00
{
2011-04-09 11:06:12 -04:00
return ; // Remove this statement to allow adding directories to the search results.
2010-02-18 19:48:42 -05:00
QString sid_hexa = QString : : number ( searchId , 16 ) ;
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * child = new RSTreeWidgetItem ( compareResultRole ) ;
2010-02-18 19:48:42 -05:00
child - > setIcon ( SR_NAME_COL , QIcon ( IMAGE_DIRECTORY ) ) ;
child - > setText ( SR_NAME_COL , QString : : fromUtf8 ( dir . name . c_str ( ) ) ) ;
2014-03-17 16:56:06 -04:00
child - > setText ( SR_HASH_COL , QString : : fromStdString ( dir . hash . toStdString ( ) ) ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_SIZE_COL , QString : : number ( dir . count ) ) ;
2012-06-10 19:47:41 -04:00
child - > setData ( SR_SIZE_COL , ROLE_SORT , ( qulonglong ) dir . count ) ;
2012-06-09 10:14:04 -04:00
child - > setText ( SR_AGE_COL , QString : : number ( dir . min_age ) ) ;
child - > setData ( SR_AGE_COL , ROLE_SORT , dir . min_age ) ;
2010-02-18 19:48:42 -05:00
child - > setTextAlignment ( SR_SIZE_COL , Qt : : AlignRight ) ;
2014-05-29 10:49:45 -04:00
child - > setText ( SR_SOURCES_COL , QString : : number ( 1 ) ) ;
child - > setData ( SR_SOURCES_COL , ROLE_SORT , 1 ) ;
child - > setTextAlignment ( SR_SOURCES_COL , Qt : : AlignRight ) ;
2010-02-18 19:48:42 -05:00
child - > setText ( SR_SEARCH_ID_COL , sid_hexa ) ;
child - > setText ( SR_TYPE_COL , tr ( " Folder " ) ) ;
ui . searchResultWidget - > addTopLevelItem ( child ) ;
/* add to the summary as well */
int items = ui . searchSummaryWidget - > topLevelItemCount ( ) ;
bool found = false ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < items ; + + i )
2010-02-18 19:48:42 -05:00
{
if ( ui . searchSummaryWidget - > topLevelItem ( i ) - > text ( SS_SEARCH_ID_COL ) = = sid_hexa )
{
// increment result since every item is new
2014-05-29 10:49:45 -04:00
int s = ui . searchSummaryWidget - > topLevelItem ( i ) - > text ( SS_RESULTS_COL ) . toInt ( ) ;
ui . searchSummaryWidget - > topLevelItem ( i ) - > setText ( SS_RESULTS_COL , QString : : number ( s + 1 ) ) ;
ui . searchSummaryWidget - > topLevelItem ( i ) - > setData ( SS_RESULTS_COL , ROLE_SORT , s + 1 ) ;
2010-02-18 19:48:42 -05:00
found = true ;
}
}
if ( ! found )
{
2012-06-09 10:14:04 -04:00
RSTreeWidgetItem * item2 = new RSTreeWidgetItem ( compareSummaryRole ) ;
2014-05-29 10:49:45 -04:00
item2 - > setText ( SS_KEYWORDS_COL , txt ) ;
item2 - > setText ( SS_RESULTS_COL , QString : : number ( 1 ) ) ;
item2 - > setData ( SS_RESULTS_COL , ROLE_SORT , 1 ) ;
item2 - > setTextAlignment ( SS_RESULTS_COL , Qt : : AlignRight ) ;
2010-02-18 19:48:42 -05:00
item2 - > setText ( SS_SEARCH_ID_COL , sid_hexa ) ;
ui . searchSummaryWidget - > addTopLevelItem ( item2 ) ;
ui . searchSummaryWidget - > setCurrentItem ( item2 ) ;
}
2010-02-21 08:38:27 -05:00
selectSearchResults ( ) ;
2010-02-18 19:48:42 -05:00
// TODO: check for duplicate directories
}
2012-04-12 15:30:41 -04:00
void SearchDialog : : insertFile ( qulonglong searchId , const FileDetail & file , int searchType )
2009-08-16 16:10:53 -04:00
{
// algo:
//
2009-08-25 16:49:50 -04:00
// 1 - look in result window whether the file already exist.
// 1.1 - If yes, just increment the source number.
2012-04-12 15:30:41 -04:00
// 2.2 - Otherwise, add an entry.
2009-08-16 16:10:53 -04:00
// 2 - look in the summary whether there exist the same request id.
2009-08-25 16:49:50 -04:00
// 1.1 - If yes, just increment the result number.
2012-04-12 15:30:41 -04:00
// 2.2 - Otherwise, ignore this file
2009-08-16 16:10:53 -04:00
//
2010-02-27 15:29:28 -05:00
//
2012-04-12 15:30:41 -04:00
QString sid_hexa = QString : : number ( searchId , 16 ) ;
//check if search ID is still in the summary list, if not it was already closed by
// the user, so nothing has to be done here
int summaryItemCount = ui . searchSummaryWidget - > topLevelItemCount ( ) ;
int summaryItemIndex = - 1 ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < summaryItemCount ; + + i ) {
2012-04-12 15:30:41 -04:00
if ( ui . searchSummaryWidget - > topLevelItem ( i ) - > text ( SS_SEARCH_ID_COL ) = = sid_hexa ) {
summaryItemIndex = i ;
break ;
}
}
if ( summaryItemIndex = = - 1 )
return ;
/* which extensions do we use? */
int fileTypeIndex = ui . searchSummaryWidget - > topLevelItem ( summaryItemIndex ) - > text ( SS_FILE_TYPE_COL ) . toInt ( ) ;
if ( fileTypeIndex ! = FILETYPE_IDX_ANY ) {
// collect the extensions to use
QStringList extList = FileTypeExtensionMap - > value ( fileTypeIndex ) . split ( " " ) ;
// check this file's extension
QString qName = QString : : fromUtf8 ( file . name . c_str ( ) ) ;
if ( ! extList . contains ( QFileInfo ( qName ) . suffix ( ) , Qt : : CaseInsensitive ) ) {
return ;
}
}
static std : : map < qulonglong , int > nb_results ;
2010-02-27 15:29:28 -05:00
if ( nb_results . find ( searchId ) = = nb_results . end ( ) )
nb_results [ searchId ] = 0 ;
2012-04-12 15:30:41 -04:00
if ( nb_results [ searchId ] > = ui . _max_results_SB - > value ( ) )
2010-02-27 15:29:28 -05:00
return ;
2009-08-16 16:10:53 -04:00
2009-08-25 16:49:50 -04:00
// 1 - look in result window whether the file already exists.
2009-08-16 16:10:53 -04:00
//
bool found = false ;
2010-02-13 13:49:56 -05:00
int sources ;
2010-02-14 13:56:56 -05:00
int friendSource = 0 ;
int anonymousSource = 0 ;
QString modifiedResult ;
2009-08-16 16:10:53 -04:00
2014-03-17 16:56:06 -04:00
QList < QTreeWidgetItem * > itms = ui . searchResultWidget - > findItems ( QString : : fromStdString ( file . hash . toStdString ( ) ) , Qt : : MatchExactly , SR_HASH_COL ) ;
2011-02-09 16:44:49 -05:00
for ( QList < QTreeWidgetItem * > : : const_iterator it ( itms . begin ( ) ) ; it ! = itms . end ( ) ; + + it )
if ( ( * it ) - > text ( SR_SEARCH_ID_COL ) = = sid_hexa )
2009-08-16 16:10:53 -04:00
{
2014-05-29 10:49:45 -04:00
QString resultCount = ( * it ) - > text ( SR_SOURCES_COL ) ;
QStringList modifiedResultCount = resultCount . split ( " / " , QString : : SkipEmptyParts ) ;
2010-02-14 13:56:56 -05:00
if ( searchType = = FRIEND_SEARCH )
{
friendSource = modifiedResultCount . at ( 0 ) . toInt ( ) + 1 ;
anonymousSource = modifiedResultCount . at ( 1 ) . toInt ( ) ;
}
else
{
friendSource = modifiedResultCount . at ( 0 ) . toInt ( ) ;
anonymousSource = modifiedResultCount . at ( 1 ) . toInt ( ) + 1 ;
}
2011-08-27 12:15:56 -04:00
modifiedResult = QString : : number ( friendSource ) + " / " + QString : : number ( anonymousSource ) ;
2013-12-27 15:25:49 -05:00
float fltRes = friendSource + ( float ) anonymousSource / 1000 ;
2014-05-29 10:49:45 -04:00
( * it ) - > setText ( SR_SOURCES_COL , modifiedResult ) ;
( * it ) - > setData ( SR_SOURCES_COL , ROLE_SORT , fltRes ) ;
2011-02-09 16:44:49 -05:00
QTreeWidgetItem * item = ( * it ) ;
2009-08-16 16:10:53 -04:00
found = true ;
2012-02-17 20:50:33 -05:00
if ( ! item - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) {
QColor foreground ;
int sources = friendSource + anonymousSource ;
2012-11-19 11:01:21 -05:00
if ( sources < = 0 )
2010-02-13 13:49:56 -05:00
{
2012-11-19 11:01:21 -05:00
foreground = textColorNoSources ( ) ;
2010-02-13 13:49:56 -05:00
}
2012-11-19 11:01:21 -05:00
else if ( sources < = 10 )
2010-02-13 13:49:56 -05:00
{
2012-11-19 11:01:21 -05:00
QColor colorLow = textColorLowSources ( ) ;
QColor colorHigh = textColorHighSources ( ) ;
float percent = ( float ) ( sources - 1 ) / 10 ; // 100% not used here, see next else
int red = ( int ) ( colorLow . red ( ) + ( float ) ( colorHigh . red ( ) - colorLow . red ( ) ) * percent ) ;
int green = ( int ) ( colorLow . green ( ) + ( float ) ( colorHigh . green ( ) - colorLow . green ( ) ) * percent ) ;
int blue = ( int ) ( colorLow . blue ( ) + ( float ) ( colorHigh . blue ( ) - colorLow . blue ( ) ) * percent ) ;
foreground = QColor ( red , green , blue ) ;
2010-02-13 13:49:56 -05:00
}
2012-02-17 20:50:33 -05:00
else
{
2012-11-19 11:01:21 -05:00
// > 10
foreground = textColorHighSources ( ) ;
2012-02-17 20:50:33 -05:00
}
QBrush brush ( foreground ) ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < item - > columnCount ( ) ; + + i )
2010-02-13 13:49:56 -05:00
{
2012-02-17 20:50:33 -05:00
item - > setForeground ( i , brush ) ;
2010-02-13 13:49:56 -05:00
}
}
2009-09-24 17:08:55 -04:00
break ;
2009-08-16 16:10:53 -04:00
}
2009-08-25 16:49:50 -04:00
2009-08-16 16:10:53 -04:00
if ( ! found )
2007-11-14 22:18:48 -05:00
{
2010-04-10 10:56:34 -04:00
+ + nb_results [ searchId ] ;
2009-08-16 16:10:53 -04:00
/* translate search results */
2010-02-06 20:37:06 -05:00
2012-06-09 10:14:04 -04:00
QTreeWidgetItem * item = new RSTreeWidgetItem ( compareResultRole ) ;
2009-09-24 17:08:55 -04:00
item - > setText ( SR_NAME_COL , QString : : fromUtf8 ( file . name . c_str ( ) ) ) ;
2014-03-17 16:56:06 -04:00
item - > setText ( SR_HASH_COL , QString : : fromStdString ( file . hash . toStdString ( ) ) ) ;
2008-11-18 15:29:40 -05:00
2011-12-07 08:08:12 -05:00
setIconAndType ( item , QString : : fromUtf8 ( file . name . c_str ( ) ) ) ;
2008-11-18 15:29:40 -05:00
2007-11-14 22:18:48 -05:00
/*
2012-04-12 15:30:41 -04:00
* to facilitate downloads we need to save the file size too
2007-11-14 22:18:48 -05:00
*/
2012-06-09 10:14:04 -04:00
item - > setText ( SR_SIZE_COL , QString : : number ( file . size ) ) ;
2012-06-11 17:32:22 -04:00
item - > setData ( SR_SIZE_COL , ROLE_SORT , ( qulonglong ) file . size ) ;
2012-06-09 10:14:04 -04:00
item - > setText ( SR_AGE_COL , QString : : number ( file . age ) ) ;
item - > setData ( SR_AGE_COL , ROLE_SORT , file . age ) ;
2009-08-16 16:10:53 -04:00
item - > setTextAlignment ( SR_SIZE_COL , Qt : : AlignRight ) ;
2010-02-14 13:56:56 -05:00
if ( searchType = = FRIEND_SEARCH )
{
friendSource = 1 ;
anonymousSource = 0 ;
}
else
{
friendSource = 0 ;
anonymousSource = 1 ;
}
2012-02-17 20:50:33 -05:00
modifiedResult = QString : : number ( friendSource ) + " / " + QString : : number ( anonymousSource ) ;
2013-12-27 15:25:49 -05:00
float fltRes = friendSource + ( float ) anonymousSource / 1000 ;
2014-05-29 10:49:45 -04:00
item - > setText ( SR_SOURCES_COL , modifiedResult ) ;
item - > setData ( SR_SOURCES_COL , ROLE_SORT , fltRes ) ;
item - > setTextAlignment ( SR_SOURCES_COL , Qt : : AlignRight ) ;
2009-09-24 17:08:55 -04:00
item - > setText ( SR_SEARCH_ID_COL , sid_hexa ) ;
2012-02-17 20:50:33 -05:00
QColor foreground ;
bool setForeground = false ;
FileInfo fi ;
if ( rsFiles - > FileDetails ( file . hash , RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE | RS_FILE_HINTS_SPEC_ONLY , fi ) ) {
item - > setData ( SR_DATA_COL , SR_ROLE_LOCAL , true ) ;
2012-11-19 11:01:21 -05:00
foreground = textColorLocal ( ) ;
2012-02-17 20:50:33 -05:00
setForeground = true ;
} else {
item - > setData ( SR_DATA_COL , SR_ROLE_LOCAL , false ) ;
2014-05-29 10:49:45 -04:00
sources = item - > text ( SR_SOURCES_COL ) . toInt ( ) ;
2012-02-17 20:50:33 -05:00
if ( sources = = 1 )
{
2012-11-19 11:01:21 -05:00
foreground = ui . searchResultWidget - > palette ( ) . color ( QPalette : : Text ) ;
2012-02-17 20:50:33 -05:00
setForeground = true ;
}
}
if ( setForeground ) {
QBrush brush ( foreground ) ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < item - > columnCount ( ) ; + + i )
2010-02-13 13:49:56 -05:00
{
2012-02-17 20:50:33 -05:00
item - > setForeground ( i , brush ) ;
2010-02-13 13:49:56 -05:00
}
}
2008-08-31 12:50:36 -04:00
2007-11-14 22:18:48 -05:00
ui . searchResultWidget - > addTopLevelItem ( item ) ;
2012-04-12 15:30:41 -04:00
/* hide/show this search result */
hideOrShowSearchResult ( item ) ;
2009-08-16 16:10:53 -04:00
}
2007-11-14 22:18:48 -05:00
2012-04-12 15:30:41 -04:00
/* update the summary as well */
if ( ! found ) // only increment result when it's a new item.
{
2014-05-29 10:49:45 -04:00
int s = ui . searchSummaryWidget - > topLevelItem ( summaryItemIndex ) - > text ( SS_RESULTS_COL ) . toInt ( ) ;
ui . searchSummaryWidget - > topLevelItem ( summaryItemIndex ) - > setText ( SS_RESULTS_COL , QString : : number ( s + 1 ) ) ;
ui . searchSummaryWidget - > topLevelItem ( summaryItemIndex ) - > setData ( SS_RESULTS_COL , ROLE_SORT , s + 1 ) ;
2011-02-16 15:07:06 -05:00
}
2007-11-14 22:18:48 -05:00
}
2012-04-12 15:30:41 -04:00
void SearchDialog : : resultsToTree ( const QString & txt , qulonglong searchId , const std : : list < DirDetails > & results )
2009-08-16 16:10:53 -04:00
{
2009-08-25 16:49:50 -04:00
ui . searchResultWidget - > setSortingEnabled ( false ) ;
2009-08-16 16:10:53 -04:00
/* translate search results */
2009-08-25 16:49:50 -04:00
std : : list < DirDetails > : : const_iterator it ;
2014-10-21 18:33:02 -04:00
for ( it = results . begin ( ) ; it ! = results . end ( ) ; + + it )
2009-08-25 16:49:50 -04:00
if ( it - > type = = DIR_TYPE_FILE ) {
FileDetail fd ;
fd . id = it - > id ;
fd . name = it - > name ;
fd . hash = it - > hash ;
fd . path = it - > path ;
fd . size = it - > count ;
fd . age = it - > age ;
fd . rank = 0 ;
2012-04-12 15:30:41 -04:00
insertFile ( searchId , fd , FRIEND_SEARCH ) ;
2009-08-25 16:49:50 -04:00
} else if ( it - > type = = DIR_TYPE_DIR ) {
2011-08-27 12:15:56 -04:00
// insertDirectory(txt, searchId, *it, NULL);
insertDirectory ( txt , searchId , * it ) ;
2009-08-25 16:49:50 -04:00
}
ui . searchResultWidget - > setSortingEnabled ( true ) ;
2009-08-16 16:10:53 -04:00
}
2010-02-21 08:38:27 -05:00
void SearchDialog : : selectSearchResults ( int index )
2007-11-14 22:18:48 -05:00
{
/* highlight this search in summary window */
2012-04-12 15:30:41 -04:00
QString searchId ;
2007-11-14 22:18:48 -05:00
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
2012-04-12 15:30:41 -04:00
if ( ci ) {
/* get the searchId text */
searchId = ci - > text ( SS_SEARCH_ID_COL ) ;
if ( ui . FileTypeComboBox - > currentIndex ( ) ! = FILETYPE_IDX_ANY )
ui . FileTypeComboBox - > setCurrentIndex ( ci - > text ( SS_FILE_TYPE_COL ) . toInt ( ) ) ;
}
2010-02-14 10:50:38 -05:00
# ifdef DEBUG
2007-11-14 22:18:48 -05:00
std : : cerr < < " SearchDialog::selectSearchResults(): searchId: " < < searchId . toStdString ( ) ;
std : : cerr < < std : : endl ;
2010-02-14 10:50:38 -05:00
# endif
2007-11-14 22:18:48 -05:00
/* show only matching searchIds in main window */
int items = ui . searchResultWidget - > topLevelItemCount ( ) ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < items ; + + i )
2007-11-14 22:18:48 -05:00
{
2012-04-12 15:30:41 -04:00
hideOrShowSearchResult ( ui . searchResultWidget - > topLevelItem ( i ) , searchId , index ) ;
2007-11-14 22:18:48 -05:00
}
ui . searchResultWidget - > update ( ) ;
2012-04-05 17:03:03 -04:00
ui . filterLineEdit - > clear ( ) ;
2007-11-14 22:18:48 -05:00
}
2012-04-12 15:30:41 -04:00
void SearchDialog : : hideOrShowSearchResult ( QTreeWidgetItem * resultItem , QString currentSearchId , int fileTypeIndex )
{
if ( currentSearchId . isEmpty ( ) ) {
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
if ( ci )
/* get the searchId text */
currentSearchId = ci - > text ( SS_SEARCH_ID_COL ) ;
}
if ( resultItem - > text ( SR_SEARCH_ID_COL ) ! = currentSearchId ) {
resultItem - > setHidden ( true ) ;
return ;
}
// check if file type matches
if ( fileTypeIndex = = - 1 )
fileTypeIndex = ui . FileTypeComboBox - > currentIndex ( ) ;
if ( fileTypeIndex ! = FILETYPE_IDX_ANY ) {
if ( ! ( fileTypeIndex = = FILETYPE_IDX_DIRECTORY & & resultItem - > text ( SR_HASH_COL ) . isEmpty ( ) ) ) {
QStringList extList = FileTypeExtensionMap - > value ( fileTypeIndex ) . split ( " " ) ;
if ( ! extList . contains ( QFileInfo ( resultItem - > text ( SR_NAME_COL ) ) . suffix ( ) , Qt : : CaseInsensitive ) ) {
resultItem - > setHidden ( true ) ;
return ;
}
}
}
// file type matches, now filter text
if ( ui . filterLineEdit - > text ( ) . isEmpty ( ) ) {
resultItem - > setHidden ( false ) ;
} else {
2012-10-29 06:59:26 -04:00
int filterColumn = ui . filterLineEdit - > currentFilter ( ) ;
2012-04-12 15:30:41 -04:00
filterItem ( resultItem , ui . filterLineEdit - > text ( ) , filterColumn ) ;
}
}
2011-12-07 08:08:12 -05:00
void SearchDialog : : setIconAndType ( QTreeWidgetItem * item , const QString & filename )
2009-08-25 16:49:50 -04:00
{
2011-12-07 08:08:12 -05:00
item - > setIcon ( SR_NAME_COL , FilesDefs : : getIconFromFilename ( filename ) ) ;
item - > setText ( SR_TYPE_COL , FilesDefs : : getNameFromFilename ( filename ) ) ;
2009-08-25 16:49:50 -04:00
}
2010-01-21 17:34:10 -05:00
2011-05-04 06:22:49 -04:00
void SearchDialog : : copyResultLink ( )
2010-01-21 17:34:10 -05:00
{
/* should also be able to handle multi-selection */
QList < QTreeWidgetItem * > itemsForCopy = ui . searchResultWidget - > selectedItems ( ) ;
int numdls = itemsForCopy . size ( ) ;
QTreeWidgetItem * item ;
2011-09-11 18:07:24 -04:00
QList < RetroShareLink > urls ;
2010-03-10 18:09:35 -05:00
2010-01-21 17:34:10 -05:00
for ( int i = 0 ; i < numdls ; + + i )
2010-03-10 18:09:35 -05:00
{
item = itemsForCopy . at ( i ) ;
// call copy
2010-01-21 17:34:10 -05:00
2010-03-10 18:09:35 -05:00
if ( ! item - > childCount ( ) )
{
2011-05-04 06:22:49 -04:00
std : : cerr < < " SearchDialog::copyResultLink() Calling set retroshare link " ;
2010-03-10 18:09:35 -05:00
std : : cerr < < std : : endl ;
2010-03-10 16:38:26 -05:00
2010-03-10 18:09:35 -05:00
QString fhash = item - > text ( SR_HASH_COL ) ;
2010-05-02 17:37:54 -04:00
qulonglong fsize = item - > text ( SR_SIZE_COL ) . toULongLong ( ) ;
2010-03-10 18:09:35 -05:00
QString fname = item - > text ( SR_NAME_COL ) ;
2010-01-21 17:34:10 -05:00
2011-05-04 06:22:49 -04:00
RetroShareLink link ;
if ( link . createFile ( fname , fsize , fhash ) ) {
std : : cerr < < " new link added to clipboard: " < < link . toString ( ) . toStdString ( ) < < std : : endl ;
2010-03-15 08:57:08 -04:00
urls . push_back ( link ) ;
2011-05-04 06:22:49 -04:00
}
2010-03-10 18:09:35 -05:00
}
}
2010-03-15 08:57:08 -04:00
RSLinkClipboard : : copyLinks ( urls ) ;
2010-01-21 17:34:10 -05:00
}
void SearchDialog : : sendLinkTo ( )
{
2011-05-04 06:22:49 -04:00
copyResultLink ( ) ;
2010-01-21 17:34:10 -05:00
/* create a message */
2010-11-02 17:11:11 -04:00
MessageComposer * nMsgDialog = MessageComposer : : newMsg ( ) ;
if ( nMsgDialog = = NULL ) {
return ;
}
2010-01-21 17:34:10 -05:00
2012-05-04 19:39:36 -04:00
nMsgDialog - > setTitleText ( tr ( " New RetroShare Link(s) " ) ) ;
nMsgDialog - > setMsgText ( RSLinkClipboard : : toHtml ( ) , true ) ;
2010-01-21 17:34:10 -05:00
nMsgDialog - > show ( ) ;
2010-05-02 20:09:55 -04:00
/* window will destroy itself! */
2010-01-21 17:34:10 -05:00
}
2010-02-13 21:31:37 -05:00
2012-04-12 15:30:41 -04:00
void SearchDialog : : selectFileType ( int index )
2010-02-18 19:48:42 -05:00
{
if ( ! FileTypeExtensionMap - > contains ( index ) & & index ! = FILETYPE_IDX_DIRECTORY )
return ;
2011-02-16 15:07:06 -05:00
2012-04-12 15:30:41 -04:00
QString searchId ;
QTreeWidgetItem * ci = ui . searchSummaryWidget - > currentItem ( ) ;
if ( ci ) {
/* get the searchId text */
searchId = ci - > text ( SS_SEARCH_ID_COL ) ;
}
/* show only matching file types in main window */
int items = ui . searchResultWidget - > topLevelItemCount ( ) ;
2014-10-21 18:33:02 -04:00
for ( int i = 0 ; i < items ; + + i )
2012-04-12 15:30:41 -04:00
{
hideOrShowSearchResult ( ui . searchResultWidget - > topLevelItem ( i ) , searchId , index ) ;
}
2011-02-16 15:07:06 -05:00
}
2012-04-12 15:30:41 -04:00
void SearchDialog : : filterItems ( )
2011-02-16 15:07:06 -05:00
{
2012-04-05 17:03:03 -04:00
int count = ui . searchResultWidget - > topLevelItemCount ( ) ;
2014-10-21 18:33:02 -04:00
for ( int index = 0 ; index < count ; + + index ) {
2012-04-12 15:30:41 -04:00
hideOrShowSearchResult ( ui . searchResultWidget - > topLevelItem ( index ) ) ;
2011-02-16 15:07:06 -05:00
}
}
2012-04-05 17:03:03 -04:00
bool SearchDialog : : filterItem ( QTreeWidgetItem * item , const QString & text , int filterColumn )
2011-02-16 15:07:06 -05:00
{
2012-04-05 17:03:03 -04:00
bool visible = true ;
2011-02-16 15:07:06 -05:00
2012-04-05 17:03:03 -04:00
if ( text . isEmpty ( ) = = false ) {
if ( item - > text ( filterColumn ) . contains ( text , Qt : : CaseInsensitive ) = = false ) {
visible = false ;
2011-02-16 15:07:06 -05:00
}
}
2012-04-05 17:03:03 -04:00
int visibleChildCount = 0 ;
int count = item - > childCount ( ) ;
2014-10-21 18:33:02 -04:00
for ( int index = 0 ; index < count ; + + index ) {
2012-04-05 17:03:03 -04:00
if ( filterItem ( item - > child ( index ) , text , filterColumn ) ) {
2014-10-21 18:33:02 -04:00
+ + visibleChildCount ;
2011-02-16 15:07:06 -05:00
}
}
2012-04-05 17:03:03 -04:00
if ( visible | | visibleChildCount ) {
item - > setHidden ( false ) ;
2011-02-16 15:07:06 -05:00
} else {
2012-04-05 17:03:03 -04:00
item - > setHidden ( true ) ;
2011-02-16 15:07:06 -05:00
}
2012-04-05 17:03:03 -04:00
return ( visible | | visibleChildCount ) ;
2011-02-16 15:07:06 -05:00
}
2013-08-23 17:13:13 -04:00
void SearchDialog : : openFolderSearch ( )
{
FileInfo info ;
QList < QTreeWidgetItem * > item = ui . searchResultWidget - > selectedItems ( ) ;
if ( item . at ( 0 ) - > data ( SR_DATA_COL , SR_ROLE_LOCAL ) . toBool ( ) ) {
QString strFileID = item . at ( 0 ) - > text ( SR_HASH_COL ) ;
2014-03-17 16:56:06 -04:00
if ( rsFiles - > FileDetails ( RsFileHash ( strFileID . toStdString ( ) ) , RS_FILE_HINTS_EXTRA | RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_BROWSABLE | RS_FILE_HINTS_NETWORK_WIDE | RS_FILE_HINTS_SPEC_ONLY , info ) ) {
2013-08-23 17:13:13 -04:00
/* make path for downloaded or downloading files */
QFileInfo qinfo ;
std : : string path ;
path = info . path . substr ( 0 , info . path . length ( ) - info . fname . length ( ) ) ;
/* open folder with a suitable application */
qinfo . setFile ( QString : : fromUtf8 ( path . c_str ( ) ) ) ;
if ( qinfo . exists ( ) & & qinfo . isDir ( ) ) {
if ( ! RsUrlHandler : : openUrl ( QUrl : : fromLocalFile ( qinfo . absoluteFilePath ( ) ) ) ) {
std : : cerr < < " openFolderSearch(): can't open folder " < < path < < std : : endl ;
}
}
}
}
}