00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "searchjob.h"
00030 #include "kmfolderimap.h"
00031 #include "imapaccountbase.h"
00032 #include "kmsearchpattern.h"
00033 #include "kmfolder.h"
00034 #include "imapjob.h"
00035 #include "kmmsgdict.h"
00036
00037 #include <progressmanager.h>
00038 using KPIM::ProgressItem;
00039 using KPIM::ProgressManager;
00040
00041 #include <kdebug.h>
00042 #include <kurl.h>
00043 #include <kio/scheduler.h>
00044 #include <kio/job.h>
00045 #include <kio/global.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048
00049 #include <qstylesheet.h>
00050
00051 namespace KMail {
00052
00053 SearchJob::SearchJob( KMFolderImap* folder, ImapAccountBase* account,
00054 const KMSearchPattern* pattern, Q_UINT32 serNum )
00055 : FolderJob( 0, tOther, (folder ? folder->folder() : 0) ),
00056 mFolder( folder ), mAccount( account ), mSearchPattern( pattern ),
00057 mSerNum( serNum ), mRemainingMsgs( 0 ), mProgress( 0 ),
00058 mUngetCurrentMsg( false )
00059 {
00060 }
00061
00062 SearchJob::~SearchJob()
00063 {
00064 }
00065
00066 void SearchJob::execute()
00067 {
00068 if ( mSerNum == 0 )
00069 {
00070 searchCompleteFolder();
00071 } else {
00072 searchSingleMessage();
00073 }
00074 }
00075
00076
00077 void SearchJob::searchCompleteFolder()
00078 {
00079
00080 QString searchString = searchStringFromPattern( mSearchPattern );
00081
00082 if ( searchString.isEmpty() )
00083 return slotSearchData( 0, QString::null );
00084
00085
00086 KURL url = mAccount->getUrl();
00087 url.setPath( mFolder->imapPath() + ";SECTION=" + searchString );
00088 QByteArray packedArgs;
00089 QDataStream stream( packedArgs, IO_WriteOnly );
00090 stream << (int) 'E' << url;
00091 KIO::SimpleJob *job = KIO::special( url, packedArgs, false );
00092 if ( mFolder->imapPath() != QString( "/" ) )
00093 {
00094 KIO::Scheduler::assignJobToSlave( mAccount->slave(), job );
00095 connect( job, SIGNAL( infoMessage( KIO::Job*, const QString& ) ),
00096 SLOT( slotSearchData( KIO::Job*, const QString& ) ) );
00097 connect( job, SIGNAL( result( KIO::Job * ) ),
00098 SLOT( slotSearchResult( KIO::Job * ) ) );
00099 }
00100 else
00101 {
00102 slotSearchData( job, QString() );
00103 slotSearchResult( job );
00104 }
00105 }
00106
00107
00108 QString SearchJob::searchStringFromPattern( const KMSearchPattern* pattern )
00109 {
00110 QStringList parts;
00111
00112 mLocalSearchPattern = new KMSearchPattern();
00113 mLocalSearchPattern->setOp( pattern->op() );
00114
00115 for ( QPtrListIterator<KMSearchRule> it( *pattern ) ; it.current() ; ++it )
00116 {
00117
00118 bool accept = true;
00119 QString result;
00120 QString field = (*it)->field();
00121
00122 if ( (*it)->function() == KMSearchRule::FuncContainsNot ) {
00123 result = "NOT ";
00124 } else if ( (*it)->function() == KMSearchRule::FuncIsGreater &&
00125 (*it)->field() == "<size>" ) {
00126 result = "LARGER ";
00127 } else if ( (*it)->function() == KMSearchRule::FuncIsLess &&
00128 (*it)->field() == "<size>" ) {
00129 result = "SMALLER ";
00130 } else if ( (*it)->function() != KMSearchRule::FuncContains ) {
00131
00132 accept = false;
00133 }
00134
00135
00136 if ( (*it)->field() == "<message>" ) {
00137 result += "TEXT \"" + (*it)->contents() + "\"";
00138 } else if ( (*it)->field() == "<body>" ) {
00139 result += "BODY \"" + (*it)->contents() + "\"";
00140 } else if ( (*it)->field() == "<recipients>" ) {
00141 result += " (OR HEADER To \"" + (*it)->contents() + "\" HEADER Cc \"" +
00142 (*it)->contents() + "\" HEADER Bcc \"" + (*it)->contents() + "\")";
00143 } else if ( (*it)->field() == "<size>" ) {
00144 result += (*it)->contents();
00145 } else if ( (*it)->field() == "<age in days>" ||
00146 (*it)->field() == "<status>" ||
00147 (*it)->field() == "<any header>" ) {
00148 accept = false;
00149 } else {
00150 result += "HEADER "+ field + " \"" + (*it)->contents() + "\"";
00151 }
00152
00153 if ( result.isEmpty() ) {
00154 accept = false;
00155 }
00156
00157 if ( accept ) {
00158 parts += result;
00159 } else {
00160 mLocalSearchPattern->append( *it );
00161 }
00162 }
00163
00164 QString search;
00165 if ( !parts.isEmpty() ) {
00166 if ( pattern->op() == KMSearchPattern::OpOr && parts.size() > 1 ) {
00167 search = "(OR " + parts.join(" ") + ")";
00168 } else {
00169
00170 search = parts.join(" ");
00171 }
00172 }
00173
00174 kdDebug(5006) << k_funcinfo << search << ";localSearch=" << mLocalSearchPattern->asString() << endl;
00175 return search;
00176 }
00177
00178
00179 void SearchJob::slotSearchData( KIO::Job* job, const QString& data )
00180 {
00181 if ( job && job->error() ) {
00182
00183 return;
00184 }
00185
00186 if ( mLocalSearchPattern->isEmpty() && data.isEmpty() )
00187 {
00188
00189 QValueList<Q_UINT32> serNums;
00190 emit searchDone( serNums, mSearchPattern, true );
00191 } else
00192 {
00193
00194 mImapSearchHits = QStringList::split( " ", data );
00195
00196 if ( canMapAllUIDs() )
00197 {
00198 slotSearchFolder();
00199 } else
00200 {
00201
00202 connect ( mFolder, SIGNAL( folderComplete( KMFolderImap*, bool ) ),
00203 this, SLOT( slotSearchFolder()) );
00204 mFolder->getFolder();
00205 }
00206 }
00207 }
00208
00209
00210 bool SearchJob::canMapAllUIDs()
00211 {
00212 for ( QStringList::Iterator it = mImapSearchHits.begin();
00213 it != mImapSearchHits.end(); ++it )
00214 {
00215 if ( mFolder->serNumForUID( (*it).toULong() ) == 0 )
00216 return false;
00217 }
00218 return true;
00219 }
00220
00221
00222 void SearchJob::slotSearchFolder()
00223 {
00224 disconnect ( mFolder, SIGNAL( folderComplete( KMFolderImap*, bool ) ),
00225 this, SLOT( slotSearchFolder()) );
00226
00227 if ( mLocalSearchPattern->isEmpty() ) {
00228
00229 QValueList<Q_UINT32> serNums;
00230 for ( QStringList::Iterator it = mImapSearchHits.begin();
00231 it != mImapSearchHits.end(); ++it )
00232 {
00233 ulong serNum = mFolder->serNumForUID( (*it).toULong() );
00234
00235
00236
00237 if ( serNum != 0 )
00238 serNums.append( serNum );
00239 }
00240 emit searchDone( serNums, mSearchPattern, true );
00241 } else {
00242
00243 mRemainingMsgs = mFolder->count();
00244 if ( mRemainingMsgs == 0 ) {
00245 emit searchDone( mSearchSerNums, mSearchPattern, true );
00246 return;
00247 }
00248
00249
00250 bool needToDownload = needsDownload();
00251 if ( needToDownload ) {
00252
00253 QString question = i18n("To execute your search all messages of the folder %1 "
00254 "have to be downloaded from the server. This may take some time. "
00255 "Do you want to continue your search?").arg( mFolder->label() );
00256 if ( KMessageBox::warningContinueCancel( 0, question,
00257 i18n("Continue Search"), i18n("&Search"),
00258 "continuedownloadingforsearch" ) != KMessageBox::Continue )
00259 {
00260 QValueList<Q_UINT32> serNums;
00261 emit searchDone( serNums, mSearchPattern, true );
00262 return;
00263 }
00264 }
00265 unsigned int numMsgs = mRemainingMsgs;
00266
00267 mProgress = ProgressManager::createProgressItem(
00268 "ImapSearchDownload" + ProgressManager::getUniqueID(),
00269 i18n("Downloading emails from IMAP server"),
00270 i18n( "URL: %1" ).arg( QStyleSheet::escape( mFolder->folder()->prettyURL() ) ),
00271 true,
00272 mAccount->useSSL() || mAccount->useTLS() );
00273 mProgress->setTotalItems( numMsgs );
00274 connect ( mProgress, SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
00275 this, SLOT( slotAbortSearch( KPIM::ProgressItem* ) ) );
00276
00277 for ( unsigned int i = 0; i < numMsgs ; ++i ) {
00278 KMMessage * msg = mFolder->getMsg( i );
00279 if ( needToDownload ) {
00280 ImapJob *job = new ImapJob( msg );
00281 job->setParentFolder( mFolder );
00282 job->setParentProgressItem( mProgress );
00283 connect( job, SIGNAL(messageRetrieved(KMMessage*)),
00284 this, SLOT(slotSearchMessageArrived(KMMessage*)) );
00285 job->start();
00286 } else {
00287 slotSearchMessageArrived( msg );
00288 }
00289 }
00290 }
00291 }
00292
00293
00294 void SearchJob::slotSearchMessageArrived( KMMessage* msg )
00295 {
00296 if ( mProgress )
00297 {
00298 mProgress->incCompletedItems();
00299 mProgress->updateProgress();
00300 }
00301 --mRemainingMsgs;
00302 bool matches = false;
00303 if ( msg ) {
00304 if ( mLocalSearchPattern->op() == KMSearchPattern::OpAnd ) {
00305
00306 if ( mLocalSearchPattern->matches( msg ) &&
00307 ( mImapSearchHits.isEmpty() ||
00308 mImapSearchHits.find( QString::number(msg->UID() ) ) != mImapSearchHits.end() ) ) {
00309 Q_UINT32 serNum = msg->getMsgSerNum();
00310 mSearchSerNums.append( serNum );
00311 matches = true;
00312 }
00313 } else if ( mLocalSearchPattern->op() == KMSearchPattern::OpOr ) {
00314
00315 if ( mLocalSearchPattern->matches( msg ) ||
00316 mImapSearchHits.find( QString::number(msg->UID()) ) != mImapSearchHits.end() ) {
00317 Q_UINT32 serNum = msg->getMsgSerNum();
00318 mSearchSerNums.append( serNum );
00319 matches = true;
00320 }
00321 }
00322 int idx = -1;
00323 KMFolder * p = 0;
00324 KMMsgDict::instance()->getLocation( msg, &p, &idx );
00325 if ( idx != -1 && mUngetCurrentMsg )
00326 mFolder->unGetMsg( idx );
00327 }
00328 if ( mSerNum > 0 )
00329 {
00330 emit searchDone( mSerNum, mSearchPattern, matches );
00331 } else {
00332 bool complete = ( mRemainingMsgs == 0 );
00333 if ( complete && mProgress )
00334 {
00335 mProgress->setComplete();
00336 mProgress = 0;
00337 }
00338 if ( matches || complete )
00339 {
00340 emit searchDone( mSearchSerNums, mSearchPattern, complete );
00341 mSearchSerNums.clear();
00342 }
00343 }
00344 }
00345
00346
00347 void SearchJob::slotSearchResult( KIO::Job *job )
00348 {
00349 if ( job->error() )
00350 {
00351 mAccount->handleJobError( job, i18n("Error while searching.") );
00352 if ( mSerNum == 0 )
00353 {
00354
00355 QValueList<Q_UINT32> serNums;
00356 emit searchDone( serNums, mSearchPattern, true );
00357 } else {
00358
00359 emit searchDone( mSerNum, mSearchPattern, false );
00360 }
00361 }
00362 }
00363
00364
00365 void SearchJob::searchSingleMessage()
00366 {
00367 QString searchString = searchStringFromPattern( mSearchPattern );
00368 if ( searchString.isEmpty() )
00369 {
00370
00371 slotSearchDataSingleMessage( 0, QString::null );
00372 } else
00373 {
00374
00375 int idx = -1;
00376 KMFolder *aFolder = 0;
00377 KMMsgDict::instance()->getLocation( mSerNum, &aFolder, &idx );
00378 assert(aFolder && (idx != -1));
00379 KMMsgBase *mb = mFolder->getMsgBase( idx );
00380
00381
00382 searchString += " UID " + QString::number( mb->UID() );
00383 KURL url = mAccount->getUrl();
00384 url.setPath( mFolder->imapPath() + ";SECTION=" + searchString );
00385 QByteArray packedArgs;
00386 QDataStream stream( packedArgs, IO_WriteOnly );
00387 stream << (int) 'E' << url;
00388 KIO::SimpleJob *job = KIO::special( url, packedArgs, false );
00389 KIO::Scheduler::assignJobToSlave(mAccount->slave(), job);
00390 connect( job, SIGNAL(infoMessage(KIO::Job*,const QString&)),
00391 SLOT(slotSearchDataSingleMessage(KIO::Job*,const QString&)) );
00392 connect( job, SIGNAL(result(KIO::Job *)),
00393 SLOT(slotSearchResult(KIO::Job *)) );
00394 }
00395 }
00396
00397
00398 void SearchJob::slotSearchDataSingleMessage( KIO::Job* job, const QString& data )
00399 {
00400 if ( job && job->error() ) {
00401
00402 return;
00403 }
00404
00405 if ( mLocalSearchPattern->isEmpty() ) {
00406
00407 emit searchDone( mSerNum, mSearchPattern, !data.isEmpty() );
00408 return;
00409 }
00410
00411 mImapSearchHits = QStringList::split( " ", data );
00412
00413
00414 int idx = -1;
00415 KMFolder *aFolder = 0;
00416 KMMsgDict::instance()->getLocation( mSerNum, &aFolder, &idx );
00417 assert(aFolder && (idx != -1));
00418 mUngetCurrentMsg = !mFolder->getMsgBase( idx )->isMessage();
00419 KMMessage * msg = mFolder->getMsg( idx );
00420 if ( needsDownload() ) {
00421 ImapJob *job = new ImapJob( msg );
00422 job->setParentFolder( mFolder );
00423 connect( job, SIGNAL(messageRetrieved(KMMessage*)),
00424 this, SLOT(slotSearchMessageArrived(KMMessage*)) );
00425 job->start();
00426 } else {
00427 slotSearchMessageArrived( msg );
00428 }
00429 }
00430
00431
00432 void SearchJob::slotAbortSearch( KPIM::ProgressItem* item )
00433 {
00434 if ( item )
00435 item->setComplete();
00436 mAccount->killAllJobs();
00437 QValueList<Q_UINT32> serNums;
00438 emit searchDone( serNums, mSearchPattern, true );
00439 }
00440
00441
00442 bool SearchJob::needsDownload()
00443 {
00444 for ( QPtrListIterator<KMSearchRule> it( *mLocalSearchPattern ) ; it.current() ; ++it ) {
00445 if ( (*it)->field() != "<status>" ) {
00446 return true;
00447 }
00448 }
00449 return false;
00450 }
00451
00452 }
00453
00454 #include "searchjob.moc"