1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* Copyright (c) 2004 Carsten Burghardt <burghardt@kde.org>
*
* 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; version 2 of the License
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of this program with any edition of
* the Qt library by Trolltech AS, Norway (or with modified versions
* of Qt that use the same license as Qt), and distribute linked
* combinations including the two. You must obey the GNU General
* Public License in all respects for all of the code used other than
* Qt. If you modify this file, you may extend this exception to
* your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from
* your version.
*/
#include "searchjob.h"
#include "kmfolderimap.h"
#include "imapaccountbase.h"
#include "kmsearchpattern.h"
#include "kmfolder.h"
#include "imapjob.h"
#include "kmmsgdict.h"
#include <progressmanager.h>
using KPIM::ProgressItem;
using KPIM::ProgressManager;
#include <kdebug.h>
#include <kurl.h>
#include <kio/scheduler.h>
#include <kio/job.h>
#include <kio/global.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <tqstylesheet.h>
namespace KMail {
SearchJob::SearchJob( KMFolderImap* folder, ImapAccountBase* account,
const KMSearchPattern* pattern, Q_UINT32 serNum )
: FolderJob( 0, tOther, (folder ? folder->folder() : 0) ),
mFolder( folder ), mAccount( account ), mSearchPattern( pattern ),
mSerNum( serNum ), mRemainingMsgs( 0 ), mProgress( 0 ),
mUngetCurrentMsg( false )
{
}
SearchJob::~SearchJob()
{
}
void SearchJob::execute()
{
if ( mSerNum == 0 )
{
searchCompleteFolder();
} else {
searchSingleMessage();
}
}
//-----------------------------------------------------------------------------
void SearchJob::searchCompleteFolder()
{
// generate imap search command and save local search patterns
TQString searchString = searchStringFromPattern( mSearchPattern );
if ( searchString.isEmpty() ) // skip imap search and download the messages
return slotSearchData( 0, TQString::null );
// do the IMAP search
KURL url = mAccount->getUrl();
url.setPath( mFolder->imapPath() + ";SECTION=" + searchString );
TQByteArray packedArgs;
TQDataStream stream( packedArgs, IO_WriteOnly );
stream << (int) 'E' << url;
KIO::SimpleJob *job = KIO::special( url, packedArgs, false );
if ( mFolder->imapPath() != TQString( "/" ) )
{
KIO::Scheduler::assignJobToSlave( mAccount->slave(), job );
connect( job, TQT_SIGNAL( infoMessage( KIO::Job*, const TQString& ) ),
TQT_SLOT( slotSearchData( KIO::Job*, const TQString& ) ) );
connect( job, TQT_SIGNAL( result( KIO::Job * ) ),
TQT_SLOT( slotSearchResult( KIO::Job * ) ) );
}
else
{ // for the "/ folder" of an imap account, searching blocks the kioslave
slotSearchData( job, TQString() );
slotSearchResult( job );
}
}
//-----------------------------------------------------------------------------
TQString SearchJob::searchStringFromPattern( const KMSearchPattern* pattern )
{
TQStringList parts;
// this is for the search pattern that can only be done local
mLocalSearchPattern = new KMSearchPattern();
mLocalSearchPattern->setOp( pattern->op() );
for ( TQPtrListIterator<KMSearchRule> it( *pattern ) ; it.current() ; ++it )
{
// construct an imap search command
bool accept = true;
TQString result;
TQString field = (*it)->field();
// check if the operation is supported
if ( (*it)->function() == KMSearchRule::FuncContainsNot ) {
result = "NOT ";
} else if ( (*it)->function() == KMSearchRule::FuncIsGreater &&
(*it)->field() == "<size>" ) {
result = "LARGER ";
} else if ( (*it)->function() == KMSearchRule::FuncIsLess &&
(*it)->field() == "<size>" ) {
result = "SMALLER ";
} else if ( (*it)->function() != KMSearchRule::FuncContains ) {
// can't be handled by imap
accept = false;
}
// now see what should be searched
if ( (*it)->field() == "<message>" ) {
result += "TEXT \"" + (*it)->contents() + "\"";
} else if ( (*it)->field() == "<body>" ) {
result += "BODY \"" + (*it)->contents() + "\"";
} else if ( (*it)->field() == "<recipients>" ) {
result += " (OR HEADER To \"" + (*it)->contents() + "\" HEADER Cc \"" +
(*it)->contents() + "\" HEADER Bcc \"" + (*it)->contents() + "\")";
} else if ( (*it)->field() == "<size>" ) {
result += (*it)->contents();
} else if ( (*it)->field() == "<age in days>" ||
(*it)->field() == "<status>" ||
(*it)->field() == "<any header>" ) {
accept = false;
} else {
result += "HEADER "+ field + " \"" + (*it)->contents() + "\"";
}
if ( result.isEmpty() ) {
accept = false;
}
if ( accept ) {
parts += result;
} else {
mLocalSearchPattern->append( *it );
}
}
TQString search;
if ( !parts.isEmpty() ) {
if ( pattern->op() == KMSearchPattern::OpOr && parts.size() > 1 ) {
search = "(OR " + parts.join(" ") + ")";
} else {
// and's are simply joined
search = parts.join(" ");
}
}
kdDebug(5006) << k_funcinfo << search << ";localSearch=" << mLocalSearchPattern->asString() << endl;
return search;
}
//-----------------------------------------------------------------------------
void SearchJob::slotSearchData( KIO::Job* job, const TQString& data )
{
if ( job && job->error() ) {
// error is handled in slotSearchResult
return;
}
if ( mLocalSearchPattern->isEmpty() && data.isEmpty() )
{
// no local search and the server found nothing
TQValueList<Q_UINT32> serNums;
emit searchDone( serNums, mSearchPattern, true );
} else
{
// remember the uids the server found
mImapSearchHits = TQStringList::split( " ", data );
if ( canMapAllUIDs() )
{
slotSearchFolder();
} else
{
// get the folder to make sure we have all messages
connect ( mFolder, TQT_SIGNAL( folderComplete( KMFolderImap*, bool ) ),
this, TQT_SLOT( slotSearchFolder()) );
mFolder->getFolder();
}
}
}
//-----------------------------------------------------------------------------
bool SearchJob::canMapAllUIDs()
{
for ( TQStringList::Iterator it = mImapSearchHits.begin();
it != mImapSearchHits.end(); ++it )
{
if ( mFolder->serNumForUID( (*it).toULong() ) == 0 )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
void SearchJob::slotSearchFolder()
{
disconnect ( mFolder, TQT_SIGNAL( folderComplete( KMFolderImap*, bool ) ),
this, TQT_SLOT( slotSearchFolder()) );
if ( mLocalSearchPattern->isEmpty() ) {
// pure imap search - now get the serial number for the UIDs
TQValueList<Q_UINT32> serNums;
for ( TQStringList::Iterator it = mImapSearchHits.begin();
it != mImapSearchHits.end(); ++it )
{
ulong serNum = mFolder->serNumForUID( (*it).toULong() );
// we need to check that the local folder does contain a message for this UID.
// scenario: server responds with a list of UIDs. While the search was running, filtering or bad juju moved a message locally
// serNumForUID will happily return 0 for the missing message, and KMFolderSearch::addSerNum() will fail its assertion.
if ( serNum != 0 )
serNums.append( serNum );
}
emit searchDone( serNums, mSearchPattern, true );
} else {
// we have search patterns that can not be handled by the server
mRemainingMsgs = mFolder->count();
if ( mRemainingMsgs == 0 ) {
emit searchDone( mSearchSerNums, mSearchPattern, true );
return;
}
// Let's see if all we need is status, that we can do locally. Optimization.
bool needToDownload = needsDownload();
if ( needToDownload ) {
// so we need to download all messages and check
TQString question = i18n("To execute your search all messages of the folder %1 "
"have to be downloaded from the server. This may take some time. "
"Do you want to continue your search?").arg( mFolder->label() );
if ( KMessageBox::warningContinueCancel( 0, question,
i18n("Continue Search"), i18n("&Search"),
"continuedownloadingforsearch" ) != KMessageBox::Continue )
{
TQValueList<Q_UINT32> serNums;
emit searchDone( serNums, mSearchPattern, true );
return;
}
}
unsigned int numMsgs = mRemainingMsgs;
// progress
mProgress = ProgressManager::createProgressItem(
"ImapSearchDownload" + ProgressManager::getUniqueID(),
i18n("Downloading emails from IMAP server"),
i18n( "URL: %1" ).arg( TQStyleSheet::escape( mFolder->folder()->prettyURL() ) ),
true,
mAccount->useSSL() || mAccount->useTLS() );
mProgress->setTotalItems( numMsgs );
connect ( mProgress, TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*)),
this, TQT_SLOT( slotAbortSearch( KPIM::ProgressItem* ) ) );
for ( unsigned int i = 0; i < numMsgs ; ++i ) {
KMMessage * msg = mFolder->getMsg( i );
if ( needToDownload ) {
ImapJob *job = new ImapJob( msg );
job->setParentFolder( mFolder );
job->setParentProgressItem( mProgress );
connect( job, TQT_SIGNAL(messageRetrieved(KMMessage*)),
this, TQT_SLOT(slotSearchMessageArrived(KMMessage*)) );
job->start();
} else {
slotSearchMessageArrived( msg );
}
}
}
}
//-----------------------------------------------------------------------------
void SearchJob::slotSearchMessageArrived( KMMessage* msg )
{
if ( mProgress )
{
mProgress->incCompletedItems();
mProgress->updateProgress();
}
--mRemainingMsgs;
bool matches = false;
if ( msg ) { // messageRetrieved(0) is always possible
if ( mLocalSearchPattern->op() == KMSearchPattern::OpAnd ) {
// imap and local search have to match
if ( mLocalSearchPattern->matches( msg ) &&
( mImapSearchHits.isEmpty() ||
mImapSearchHits.find( TQString::number(msg->UID() ) ) != mImapSearchHits.end() ) ) {
Q_UINT32 serNum = msg->getMsgSerNum();
mSearchSerNums.append( serNum );
matches = true;
}
} else if ( mLocalSearchPattern->op() == KMSearchPattern::OpOr ) {
// imap or local search have to match
if ( mLocalSearchPattern->matches( msg ) ||
mImapSearchHits.find( TQString::number(msg->UID()) ) != mImapSearchHits.end() ) {
Q_UINT32 serNum = msg->getMsgSerNum();
mSearchSerNums.append( serNum );
matches = true;
}
}
int idx = -1;
KMFolder * p = 0;
KMMsgDict::instance()->getLocation( msg, &p, &idx );
if ( idx != -1 && mUngetCurrentMsg )
mFolder->unGetMsg( idx );
}
if ( mSerNum > 0 )
{
emit searchDone( mSerNum, mSearchPattern, matches );
} else {
bool complete = ( mRemainingMsgs == 0 );
if ( complete && mProgress )
{
mProgress->setComplete();
mProgress = 0;
}
if ( matches || complete )
{
emit searchDone( mSearchSerNums, mSearchPattern, complete );
mSearchSerNums.clear();
}
}
}
//-----------------------------------------------------------------------------
void SearchJob::slotSearchResult( KIO::Job *job )
{
if ( job->error() )
{
mAccount->handleJobError( job, i18n("Error while searching.") );
if ( mSerNum == 0 )
{
// folder
TQValueList<Q_UINT32> serNums;
emit searchDone( serNums, mSearchPattern, true );
} else {
// message
emit searchDone( mSerNum, mSearchPattern, false );
}
}
}
//-----------------------------------------------------------------------------
void SearchJob::searchSingleMessage()
{
TQString searchString = searchStringFromPattern( mSearchPattern );
if ( searchString.isEmpty() )
{
// no imap search
slotSearchDataSingleMessage( 0, TQString::null );
} else
{
// imap search
int idx = -1;
KMFolder *aFolder = 0;
KMMsgDict::instance()->getLocation( mSerNum, &aFolder, &idx );
assert(aFolder && (idx != -1));
KMMsgBase *mb = mFolder->getMsgBase( idx );
// only search for that UID
searchString += " UID " + TQString::number( mb->UID() );
KURL url = mAccount->getUrl();
url.setPath( mFolder->imapPath() + ";SECTION=" + searchString );
TQByteArray packedArgs;
TQDataStream stream( packedArgs, IO_WriteOnly );
stream << (int) 'E' << url;
KIO::SimpleJob *job = KIO::special( url, packedArgs, false );
KIO::Scheduler::assignJobToSlave(mAccount->slave(), job);
connect( job, TQT_SIGNAL(infoMessage(KIO::Job*,const TQString&)),
TQT_SLOT(slotSearchDataSingleMessage(KIO::Job*,const TQString&)) );
connect( job, TQT_SIGNAL(result(KIO::Job *)),
TQT_SLOT(slotSearchResult(KIO::Job *)) );
}
}
//-----------------------------------------------------------------------------
void SearchJob::slotSearchDataSingleMessage( KIO::Job* job, const TQString& data )
{
if ( job && job->error() ) {
// error is handled in slotSearchResult
return;
}
if ( mLocalSearchPattern->isEmpty() ) {
// we are done
emit searchDone( mSerNum, mSearchPattern, !data.isEmpty() );
return;
}
// remember what the server found
mImapSearchHits = TQStringList::split( " ", data );
// add the local search
int idx = -1;
KMFolder *aFolder = 0;
KMMsgDict::instance()->getLocation( mSerNum, &aFolder, &idx );
assert(aFolder && (idx != -1));
mUngetCurrentMsg = !mFolder->getMsgBase( idx )->isMessage();
KMMessage * msg = mFolder->getMsg( idx );
if ( needsDownload() ) {
ImapJob *job = new ImapJob( msg );
job->setParentFolder( mFolder );
connect( job, TQT_SIGNAL(messageRetrieved(KMMessage*)),
this, TQT_SLOT(slotSearchMessageArrived(KMMessage*)) );
job->start();
} else {
slotSearchMessageArrived( msg );
}
}
//-----------------------------------------------------------------------------
void SearchJob::slotAbortSearch( KPIM::ProgressItem* item )
{
if ( item )
item->setComplete();
mAccount->killAllJobs();
TQValueList<Q_UINT32> serNums;
emit searchDone( serNums, mSearchPattern, true );
}
//-----------------------------------------------------------------------------
bool SearchJob::needsDownload()
{
for ( TQPtrListIterator<KMSearchRule> it( *mLocalSearchPattern ) ; it.current() ; ++it ) {
if ( (*it)->field() != "<status>" ) {
return true;
}
}
return false;
}
} // namespace KMail
#include "searchjob.moc"
|