9 #include "kmfiltermgr.h"
12 #include "filterlog.h"
14 #include "kmfilterdlg.h"
15 #include "kmfolderindex.h"
16 #include "filterimporterexporter.h"
18 #include "kmfoldermgr.h"
19 #include "kmmsgdict.h"
20 #include "messageproperty.h"
21 using KMail::MessageProperty;
30 #include <tqvaluevector.h>
37 KMFilterMgr::KMFilterMgr(
bool popFilter )
39 bPopFilter( popFilter ),
41 mDirtyBufferedFolderTarget( true ),
42 mBufferedFolderTarget( true ),
45 connect( kmkernel, TQT_SIGNAL( folderRemoved(
KMFolder* ) ),
46 this, TQT_SLOT( slotFolderRemoved(
KMFolder* ) ) );
51 KMFilterMgr::~KMFilterMgr()
58 void KMFilterMgr::clear()
60 mDirtyBufferedFolderTarget =
true;
61 for ( TQValueListIterator<KMFilter*> it = mFilters.begin() ;
62 it != mFilters.end() ; ++it ) {
68 void KMFilterMgr::readConfig(
void)
70 KConfig* config = KMKernel::config();
74 KConfigGroupSaver saver(config,
"General");
75 mShowLater = config->readNumEntry(
"popshowDLmsgs",0);
77 mFilters = FilterImporterExporter::readFiltersFromConfig( config, bPopFilter );
81 void KMFilterMgr::writeConfig(
bool withSync)
83 KConfig* config = KMKernel::config();
86 FilterImporterExporter::writeFiltersToConfig( mFilters, config, bPopFilter );
87 KConfigGroupSaver saver(config,
"General");
89 config->writeEntry(
"popshowDLmsgs", mShowLater);
91 if (withSync) config->sync();
94 int KMFilterMgr::processPop(
KMMessage * msg )
const {
95 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
96 it != mFilters.constEnd() ; ++it )
97 if ( (*it)->pattern()->matches( msg ) )
98 return (*it)->action();
102 bool KMFilterMgr::beginFiltering(KMMsgBase *msgBase)
const
104 if (MessageProperty::filtering( msgBase ))
106 MessageProperty::setFiltering( msgBase,
true );
107 MessageProperty::setFilterFolder( msgBase, 0 );
108 if ( FilterLog::instance()->isLogging() ) {
109 FilterLog::instance()->addSeparator();
114 int KMFilterMgr::moveMessage(
KMMessage *msg)
const
116 if (MessageProperty::filterFolder(msg)->moveMsg( msg ) == 0) {
117 if ( kmkernel->folderIsTrash( MessageProperty::filterFolder( msg )))
120 kdDebug(5006) <<
"KMfilterAction - couldn't move msg" << endl;
126 void KMFilterMgr::endFiltering(KMMsgBase *msgBase)
const
128 KMFolder *parent = msgBase->parent();
130 if ( parent == MessageProperty::filterFolder( msgBase ) ) {
131 parent->
take( parent->
find( msgBase ) );
133 else if ( ! MessageProperty::filterFolder( msgBase ) ) {
134 int index = parent->
find( msgBase );
136 parent->
take( index );
140 MessageProperty::setFiltering( msgBase,
false );
143 int KMFilterMgr::process(
KMMessage * msg,
const KMFilter * filter ) {
144 if ( !msg || !filter || !beginFiltering( msg ))
149 if ( FilterLog::instance()->isLogging() ) {
150 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
151 logText.append( filter->pattern()->asString() );
152 FilterLog::instance()->add( logText, FilterLog::patternDesc );
155 if (filter->pattern()->matches( msg )) {
156 if ( FilterLog::instance()->isLogging() ) {
157 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
158 FilterLog::patternResult );
160 if (filter->execActions( msg, stopIt ) == KMFilter::CriticalError)
163 KMFolder *folder = MessageProperty::filterFolder( msg );
167 tempOpenFolder( folder );
168 result = folder->
moveMsg( msg );
177 int KMFilterMgr::process( TQ_UINT32 serNum,
const KMFilter *filter )
185 if ( isMatching( serNum, filter ) ) {
190 if ( !folder || ( idx == -1 ) || ( idx >= folder->
count() ) ) {
194 KMMsgBase *msgBase = folder->
getMsgBase( idx );
195 bool unGet = !msgBase->isMessage();
198 if ( !msg || !beginFiltering( msg ) ) {
203 if ( filter->execActions( msg, stopIt ) == KMFilter::CriticalError ) {
209 KMFolder *targetFolder = MessageProperty::filterFolder( msg );
212 if ( targetFolder ) {
213 tempOpenFolder( targetFolder );
215 result = targetFolder->
moveMsg( msg );
226 int KMFilterMgr::process(
KMMessage * msg, FilterSet
set,
227 bool account, uint accountId ) {
229 return processPop( msg );
231 if (
set == NoSet ) {
232 kdDebug(5006) <<
"KMFilterMgr: process() called with not filter set selected"
238 bool atLeastOneRuleMatched =
false;
240 if (!beginFiltering( msg ))
242 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
243 !stopIt && it != mFilters.constEnd() ; ++it ) {
245 if ( ( ( (
set&Inbound) && (*it)->applyOnInbound() ) &&
247 ( account && (*it)->applyOnAccount( accountId ) ) ) ) ||
248 ( (
set&Outbound) && (*it)->applyOnOutbound() ) ||
249 ( (
set&Explicit) && (*it)->applyOnExplicit() ) ) {
252 if ( FilterLog::instance()->isLogging() ) {
253 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
254 logText.append( (*it)->pattern()->asString() );
255 FilterLog::instance()->add( logText, FilterLog::patternDesc );
257 if ( (*it)->pattern()->matches( msg ) ) {
259 if ( FilterLog::instance()->isLogging() ) {
260 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
261 FilterLog::patternResult );
263 atLeastOneRuleMatched =
true;
265 if ( (*it)->execActions(msg, stopIt) == KMFilter::CriticalError )
271 KMFolder *folder = MessageProperty::filterFolder( msg );
275 if ( atLeastOneRuleMatched )
278 MessageProperty::setFiltering( msg,
false );
280 tempOpenFolder( folder );
287 bool KMFilterMgr::isMatching( TQ_UINT32 serNum,
const KMFilter *filter )
290 if ( FilterLog::instance()->isLogging() ) {
291 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
292 logText.append( filter->pattern()->asString() );
293 FilterLog::instance()->add( logText, FilterLog::patternDesc );
295 if ( filter->pattern()->matches( serNum ) ) {
296 if ( FilterLog::instance()->isLogging() ) {
297 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
298 FilterLog::patternResult );
305 bool KMFilterMgr::atLeastOneFilterAppliesTo(
unsigned int accountID )
const
307 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
308 for ( ; it != mFilters.constEnd() ; ++it ) {
309 if ( (*it)->applyOnAccount( accountID ) ) {
316 bool KMFilterMgr::atLeastOneIncomingFilterAppliesTo(
unsigned int accountID )
const
318 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
319 for ( ; it != mFilters.constEnd() ; ++it ) {
320 if ( (*it)->applyOnInbound() && (*it)->applyOnAccount( accountID ) ) {
327 bool KMFilterMgr::atLeastOneOnlineImapFolderTarget()
329 if (!mDirtyBufferedFolderTarget)
330 return mBufferedFolderTarget;
332 mDirtyBufferedFolderTarget =
false;
334 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
335 for ( ; it != mFilters.constEnd() ; ++it ) {
336 KMFilter *filter = *it;
337 TQPtrListIterator<KMFilterAction> jt( *filter->actions() );
338 for ( jt.toFirst() ; jt.current() ; ++jt ) {
343 KMFolder *folder = kmkernel->imapFolderMgr()->findIdString( name );
345 mBufferedFolderTarget =
true;
350 mBufferedFolderTarget =
false;
355 void KMFilterMgr::ref(
void)
361 void KMFilterMgr::deref(
bool force)
367 if (mRefCount && !force)
369 TQValueVector< KMFolder *>::const_iterator it;
370 for ( it = mOpenFolders.constBegin(); it != mOpenFolders.constEnd(); ++it )
371 (*it)->close(
"filtermgr");
372 mOpenFolders.clear();
377 int KMFilterMgr::tempOpenFolder(
KMFolder* aFolder)
381 int rc = aFolder->
open(
"filermgr");
384 mOpenFolders.append( aFolder );
390 void KMFilterMgr::openDialog( TQWidget *,
bool checkForEmptyFilterList )
398 mEditDialog =
new KMFilterDlg( 0,
"filterdialog", bPopFilter,
399 checkForEmptyFilterList );
406 void KMFilterMgr::createFilter(
const TQCString & field,
const TQString & value )
408 openDialog( 0,
false );
409 mEditDialog->createFilter( field, value );
414 const TQString KMFilterMgr::createUniqueName(
const TQString & name )
416 TQString uniqueName = name;
422 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
423 it != mFilters.constEnd(); ++it ) {
424 if ( !( (*it)->name().compare( uniqueName ) ) ) {
428 uniqueName += TQString(
" (" ) + TQString::number( counter )
439 void KMFilterMgr::appendFilters(
const TQValueList<KMFilter*> &filters,
440 bool replaceIfNameExists )
442 mDirtyBufferedFolderTarget =
true;
444 if ( replaceIfNameExists ) {
445 TQValueListConstIterator<KMFilter*> it1 = filters.constBegin();
446 for ( ; it1 != filters.constEnd() ; ++it1 ) {
447 TQValueListConstIterator<KMFilter*> it2 = mFilters.constBegin();
448 for ( ; it2 != mFilters.constEnd() ; ++it2 ) {
449 if ( (*it1)->name() == (*it2)->name() ) {
450 mFilters.remove( (*it2) );
451 it2 = mFilters.constBegin();
461 void KMFilterMgr::setFilters(
const TQValueList<KMFilter*> &filters )
470 void KMFilterMgr::slotFolderRemoved(
KMFolder * aFolder )
472 folderRemoved( aFolder, 0 );
478 mDirtyBufferedFolderTarget =
true;
480 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
481 for ( ; it != mFilters.constEnd() ; ++it )
482 if ( (*it)->folderRemoved(aFolder, aNewFolder) )
491 void KMFilterMgr::dump(
void)
const
494 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
495 for ( ; it != mFilters.constEnd() ; ++it ) {
496 kdDebug(5006) << (*it)->asString() << endl;
502 void KMFilterMgr::endUpdate(
void)
504 emit filterListUpdated();
507 #include "kmfiltermgr.moc"