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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
// -*- mode: C++; c-file-style: "gnu" -*-
// kmfiltermgr.cpp
// my header
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "kmfiltermgr.h"
// other kmail headers
#include "filterlog.h"
using KMail::FilterLog;
#include "kmfilterdlg.h"
#include "kmfolderindex.h"
#include "filterimporterexporter.h"
using KMail::FilterImporterExporter;
#include "kmfoldermgr.h"
#include "kmmsgdict.h"
#include "messageproperty.h"
using KMail::MessageProperty;
// other KDE headers
#include <kdebug.h>
#include <klocale.h>
#include <kconfig.h>
// other Qt headers
#include <qregexp.h>
#include <qvaluevector.h>
// other headers
#include <assert.h>
//-----------------------------------------------------------------------------
KMFilterMgr::KMFilterMgr( bool popFilter )
: mEditDialog( 0 ),
bPopFilter( popFilter ),
mShowLater( false ),
mDirtyBufferedFolderTarget( true ),
mBufferedFolderTarget( true ),
mRefCount( 0 )
{
if (bPopFilter)
kdDebug(5006) << "pPopFilter set" << endl;
connect( kmkernel, SIGNAL( folderRemoved( KMFolder* ) ),
this, SLOT( slotFolderRemoved( KMFolder* ) ) );
}
//-----------------------------------------------------------------------------
KMFilterMgr::~KMFilterMgr()
{
deref( true );
writeConfig( false );
clear();
}
void KMFilterMgr::clear()
{
mDirtyBufferedFolderTarget = true;
for ( QValueListIterator<KMFilter*> it = mFilters.begin() ;
it != mFilters.end() ; ++it ) {
delete *it;
}
}
//-----------------------------------------------------------------------------
void KMFilterMgr::readConfig(void)
{
KConfig* config = KMKernel::config();
clear();
if (bPopFilter) {
KConfigGroupSaver saver(config, "General");
mShowLater = config->readNumEntry("popshowDLmsgs",0);
}
mFilters = FilterImporterExporter::readFiltersFromConfig( config, bPopFilter );
}
//-----------------------------------------------------------------------------
void KMFilterMgr::writeConfig(bool withSync)
{
KConfig* config = KMKernel::config();
// Now, write out the new stuff:
FilterImporterExporter::writeFiltersToConfig( mFilters, config, bPopFilter );
KConfigGroupSaver saver(config, "General");
if (bPopFilter)
config->writeEntry("popshowDLmsgs", mShowLater);
if (withSync) config->sync();
}
int KMFilterMgr::processPop( KMMessage * msg ) const {
for ( QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
it != mFilters.constEnd() ; ++it )
if ( (*it)->pattern()->matches( msg ) )
return (*it)->action();
return NoAction;
}
bool KMFilterMgr::beginFiltering(KMMsgBase *msgBase) const
{
if (MessageProperty::filtering( msgBase ))
return false;
MessageProperty::setFiltering( msgBase, true );
MessageProperty::setFilterFolder( msgBase, 0 );
if ( FilterLog::instance()->isLogging() ) {
FilterLog::instance()->addSeparator();
}
return true;
}
int KMFilterMgr::moveMessage(KMMessage *msg) const
{
if (MessageProperty::filterFolder(msg)->moveMsg( msg ) == 0) {
if ( kmkernel->folderIsTrash( MessageProperty::filterFolder( msg )))
KMFilterAction::sendMDN( msg, KMime::MDN::Deleted );
} else {
kdDebug(5006) << "KMfilterAction - couldn't move msg" << endl;
return 2;
}
return 0;
}
void KMFilterMgr::endFiltering(KMMsgBase *msgBase) const
{
KMFolder *parent = msgBase->parent();
if ( parent ) {
if ( parent == MessageProperty::filterFolder( msgBase ) ) {
parent->take( parent->find( msgBase ) );
}
else if ( ! MessageProperty::filterFolder( msgBase ) ) {
int index = parent->find( msgBase );
KMMessage *msg = parent->getMsg( index );
parent->take( index );
parent->addMsgKeepUID( msg );
}
}
MessageProperty::setFiltering( msgBase, false );
}
int KMFilterMgr::process( KMMessage * msg, const KMFilter * filter ) {
if ( !msg || !filter || !beginFiltering( msg ))
return 1;
bool stopIt = false;
int result = 1;
if ( FilterLog::instance()->isLogging() ) {
QString logText( i18n( "<b>Evaluating filter rules:</b> " ) );
logText.append( filter->pattern()->asString() );
FilterLog::instance()->add( logText, FilterLog::patternDesc );
}
if (filter->pattern()->matches( msg )) {
if ( FilterLog::instance()->isLogging() ) {
FilterLog::instance()->add( i18n( "<b>Filter rules have matched.</b>" ),
FilterLog::patternResult );
}
if (filter->execActions( msg, stopIt ) == KMFilter::CriticalError)
return 2;
KMFolder *folder = MessageProperty::filterFolder( msg );
endFiltering( msg );
if (folder) {
tempOpenFolder( folder );
result = folder->moveMsg( msg );
}
} else {
endFiltering( msg );
result = 1;
}
return result;
}
int KMFilterMgr::process( Q_UINT32 serNum, const KMFilter *filter )
{
bool stopIt = false;
int result = 1;
if ( !filter )
return 1;
if ( isMatching( serNum, filter ) ) {
KMFolder *folder = 0;
int idx = -1;
// get the message with the serNum
KMMsgDict::instance()->getLocation( serNum, &folder, &idx );
if ( !folder || ( idx == -1 ) || ( idx >= folder->count() ) ) {
return 1;
}
KMFolderOpener openFolder(folder, "filtermgr");
KMMsgBase *msgBase = folder->getMsgBase( idx );
bool unGet = !msgBase->isMessage();
KMMessage *msg = folder->getMsg( idx );
// do the actual filtering stuff
if ( !msg || !beginFiltering( msg ) ) {
if ( unGet )
folder->unGetMsg( idx );
return 1;
}
if ( filter->execActions( msg, stopIt ) == KMFilter::CriticalError ) {
if ( unGet )
folder->unGetMsg( idx );
return 2;
}
KMFolder *targetFolder = MessageProperty::filterFolder( msg );
endFiltering( msg );
if ( targetFolder ) {
tempOpenFolder( targetFolder );
msg->setTransferInProgress( false );
result = targetFolder->moveMsg( msg );
msg->setTransferInProgress( true );
}
if ( unGet )
folder->unGetMsg( idx );
} else {
result = 1;
}
return result;
}
int KMFilterMgr::process( KMMessage * msg, FilterSet set,
bool account, uint accountId ) {
if ( bPopFilter )
return processPop( msg );
if ( set == NoSet ) {
kdDebug(5006) << "KMFilterMgr: process() called with not filter set selected"
<< endl;
return 1;
}
bool stopIt = false;
bool atLeastOneRuleMatched = false;
if (!beginFiltering( msg ))
return 1;
for ( QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
!stopIt && it != mFilters.constEnd() ; ++it ) {
if ( ( ( (set&Inbound) && (*it)->applyOnInbound() ) &&
( !account ||
( account && (*it)->applyOnAccount( accountId ) ) ) ) ||
( (set&Outbound) && (*it)->applyOnOutbound() ) ||
( (set&Explicit) && (*it)->applyOnExplicit() ) ) {
// filter is applicable
if ( FilterLog::instance()->isLogging() ) {
QString logText( i18n( "<b>Evaluating filter rules:</b> " ) );
logText.append( (*it)->pattern()->asString() );
FilterLog::instance()->add( logText, FilterLog::patternDesc );
}
if ( (*it)->pattern()->matches( msg ) ) {
// filter matches
if ( FilterLog::instance()->isLogging() ) {
FilterLog::instance()->add( i18n( "<b>Filter rules have matched.</b>" ),
FilterLog::patternResult );
}
atLeastOneRuleMatched = true;
// execute actions:
if ( (*it)->execActions(msg, stopIt) == KMFilter::CriticalError )
return 2;
}
}
}
KMFolder *folder = MessageProperty::filterFolder( msg );
/* endFilter does a take() and addButKeepUID() to ensure the changed
* message is on disk. This is unnessecary if nothing matched, so just
* reset state and don't update the listview at all. */
if ( atLeastOneRuleMatched )
endFiltering( msg );
else
MessageProperty::setFiltering( msg, false );
if (folder) {
tempOpenFolder( folder );
folder->moveMsg(msg);
return 0;
}
return 1;
}
bool KMFilterMgr::isMatching( Q_UINT32 serNum, const KMFilter *filter )
{
bool result = false;
if ( FilterLog::instance()->isLogging() ) {
QString logText( i18n( "<b>Evaluating filter rules:</b> " ) );
logText.append( filter->pattern()->asString() );
FilterLog::instance()->add( logText, FilterLog::patternDesc );
}
if ( filter->pattern()->matches( serNum ) ) {
if ( FilterLog::instance()->isLogging() ) {
FilterLog::instance()->add( i18n( "<b>Filter rules have matched.</b>" ),
FilterLog::patternResult );
}
result = true;
}
return result;
}
bool KMFilterMgr::atLeastOneFilterAppliesTo( unsigned int accountID ) const
{
QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
for ( ; it != mFilters.constEnd() ; ++it ) {
if ( (*it)->applyOnAccount( accountID ) ) {
return true;
}
}
return false;
}
bool KMFilterMgr::atLeastOneIncomingFilterAppliesTo( unsigned int accountID ) const
{
QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
for ( ; it != mFilters.constEnd() ; ++it ) {
if ( (*it)->applyOnInbound() && (*it)->applyOnAccount( accountID ) ) {
return true;
}
}
return false;
}
bool KMFilterMgr::atLeastOneOnlineImapFolderTarget()
{
if (!mDirtyBufferedFolderTarget)
return mBufferedFolderTarget;
mDirtyBufferedFolderTarget = false;
QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
for ( ; it != mFilters.constEnd() ; ++it ) {
KMFilter *filter = *it;
QPtrListIterator<KMFilterAction> jt( *filter->actions() );
for ( jt.toFirst() ; jt.current() ; ++jt ) {
KMFilterActionWithFolder *f = dynamic_cast<KMFilterActionWithFolder*>(*jt);
if (!f)
continue;
QString name = f->argsAsString();
KMFolder *folder = kmkernel->imapFolderMgr()->findIdString( name );
if (folder) {
mBufferedFolderTarget = true;
return true;
}
}
}
mBufferedFolderTarget = false;
return false;
}
//-----------------------------------------------------------------------------
void KMFilterMgr::ref(void)
{
mRefCount++;
}
//-----------------------------------------------------------------------------
void KMFilterMgr::deref(bool force)
{
if (!force)
mRefCount--;
if (mRefCount < 0)
mRefCount = 0;
if (mRefCount && !force)
return;
QValueVector< KMFolder *>::const_iterator it;
for ( it = mOpenFolders.constBegin(); it != mOpenFolders.constEnd(); ++it )
(*it)->close("filtermgr");
mOpenFolders.clear();
}
//-----------------------------------------------------------------------------
int KMFilterMgr::tempOpenFolder(KMFolder* aFolder)
{
assert( aFolder );
int rc = aFolder->open("filermgr");
if (rc) return rc;
mOpenFolders.append( aFolder );
return 0;
}
//-----------------------------------------------------------------------------
void KMFilterMgr::openDialog( QWidget *, bool checkForEmptyFilterList )
{
if( !mEditDialog )
{
//
// We can't use the parent as long as the dialog is modeless
// and there is one shared dialog for all top level windows.
//
mEditDialog = new KMFilterDlg( 0, "filterdialog", bPopFilter,
checkForEmptyFilterList );
}
mEditDialog->show();
}
//-----------------------------------------------------------------------------
void KMFilterMgr::createFilter( const QCString & field, const QString & value )
{
openDialog( 0, false );
mEditDialog->createFilter( field, value );
}
//-----------------------------------------------------------------------------
const QString KMFilterMgr::createUniqueName( const QString & name )
{
QString uniqueName = name;
int counter = 0;
bool found = true;
while ( found ) {
found = false;
for ( QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
it != mFilters.constEnd(); ++it ) {
if ( !( (*it)->name().compare( uniqueName ) ) ) {
found = true;
++counter;
uniqueName = name;
uniqueName += QString( " (" ) + QString::number( counter )
+ QString( ")" );
break;
}
}
}
return uniqueName;
}
//-----------------------------------------------------------------------------
void KMFilterMgr::appendFilters( const QValueList<KMFilter*> &filters,
bool replaceIfNameExists )
{
mDirtyBufferedFolderTarget = true;
beginUpdate();
if ( replaceIfNameExists ) {
QValueListConstIterator<KMFilter*> it1 = filters.constBegin();
for ( ; it1 != filters.constEnd() ; ++it1 ) {
QValueListConstIterator<KMFilter*> it2 = mFilters.constBegin();
for ( ; it2 != mFilters.constEnd() ; ++it2 ) {
if ( (*it1)->name() == (*it2)->name() ) {
mFilters.remove( (*it2) );
it2 = mFilters.constBegin();
}
}
}
}
mFilters += filters;
writeConfig( true );
endUpdate();
}
void KMFilterMgr::setFilters( const QValueList<KMFilter*> &filters )
{
beginUpdate();
clear();
mFilters = filters;
writeConfig( true );
endUpdate();
}
void KMFilterMgr::slotFolderRemoved( KMFolder * aFolder )
{
folderRemoved( aFolder, 0 );
}
//-----------------------------------------------------------------------------
bool KMFilterMgr::folderRemoved(KMFolder* aFolder, KMFolder* aNewFolder)
{
mDirtyBufferedFolderTarget = true;
bool rem = false;
QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
for ( ; it != mFilters.constEnd() ; ++it )
if ( (*it)->folderRemoved(aFolder, aNewFolder) )
rem = true;
return rem;
}
//-----------------------------------------------------------------------------
#ifndef NDEBUG
void KMFilterMgr::dump(void) const
{
QValueListConstIterator<KMFilter*> it = mFilters.constBegin();
for ( ; it != mFilters.constEnd() ; ++it ) {
kdDebug(5006) << (*it)->asString() << endl;
}
}
#endif
//-----------------------------------------------------------------------------
void KMFilterMgr::endUpdate(void)
{
emit filterListUpdated();
}
#include "kmfiltermgr.moc"
|