8 #include "kmfiltermgr.h"
11 #include "filterlog.h"
13 #include "kmfilterdlg.h"
14 #include "kmfolderindex.h"
15 #include "filterimporterexporter.h"
17 #include "kmfoldermgr.h"
18 #include "kmmsgdict.h"
19 #include "messageproperty.h"
20 using KMail::MessageProperty;
24 #include <tdelocale.h>
25 #include <tdeconfig.h>
29 #include <tqvaluevector.h>
36 KMFilterMgr::KMFilterMgr(
bool popFilter )
38 bPopFilter( popFilter ),
40 mDirtyBufferedFolderTarget( true ),
41 mBufferedFolderTarget( true ),
44 connect( kmkernel, TQ_SIGNAL( folderRemoved(
KMFolder* ) ),
45 this, TQ_SLOT( slotFolderRemoved(
KMFolder* ) ) );
50 KMFilterMgr::~KMFilterMgr()
57 void KMFilterMgr::clear()
59 mDirtyBufferedFolderTarget =
true;
60 for ( TQValueListIterator<KMFilter*> it = mFilters.begin() ;
61 it != mFilters.end() ; ++it ) {
67 void KMFilterMgr::readConfig(
void)
69 TDEConfig* config = KMKernel::config();
73 TDEConfigGroupSaver saver(config,
"General");
74 mShowLater = config->readNumEntry(
"popshowDLmsgs",0);
76 mFilters = FilterImporterExporter::readFiltersFromConfig( config, bPopFilter );
80 void KMFilterMgr::writeConfig(
bool withSync)
82 TDEConfig* config = KMKernel::config();
85 FilterImporterExporter::writeFiltersToConfig( mFilters, config, bPopFilter );
86 TDEConfigGroupSaver saver(config,
"General");
88 config->writeEntry(
"popshowDLmsgs", mShowLater);
90 if (withSync) config->sync();
93 int KMFilterMgr::processPop(
KMMessage * msg )
const {
94 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
95 it != mFilters.constEnd() ; ++it )
96 if ( (*it)->pattern()->matches( msg ) )
97 return (*it)->action();
101 bool KMFilterMgr::beginFiltering(KMMsgBase *msgBase)
const
103 if (MessageProperty::filtering( msgBase ))
105 MessageProperty::setFiltering( msgBase,
true );
106 MessageProperty::setFilterFolder( msgBase, 0 );
107 if ( FilterLog::instance()->isLogging() ) {
108 FilterLog::instance()->addSeparator();
113 int KMFilterMgr::moveMessage(
KMMessage *msg)
const
115 if (MessageProperty::filterFolder(msg)->moveMsg( msg ) == 0) {
116 if ( kmkernel->folderIsTrash( MessageProperty::filterFolder( msg )))
119 kdDebug(5006) <<
"KMfilterAction - couldn't move msg" << endl;
125 void KMFilterMgr::endFiltering(KMMsgBase *msgBase)
const
127 KMFolder *parent = msgBase->parent();
129 if ( parent == MessageProperty::filterFolder( msgBase ) ) {
130 parent->
take( parent->
find( msgBase ) );
132 else if ( ! MessageProperty::filterFolder( msgBase ) ) {
133 int index = parent->
find( msgBase );
135 parent->
take( index );
139 MessageProperty::setFiltering( msgBase,
false );
142 int KMFilterMgr::process(
KMMessage * msg,
const KMFilter * filter ) {
143 if ( !msg || !filter || !beginFiltering( msg ))
148 if ( FilterLog::instance()->isLogging() ) {
149 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
150 logText.append( filter->pattern()->asString() );
151 FilterLog::instance()->add( logText, FilterLog::patternDesc );
154 if (filter->pattern()->matches( msg )) {
155 if ( FilterLog::instance()->isLogging() ) {
156 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
157 FilterLog::patternResult );
159 if (filter->execActions( msg, stopIt ) == KMFilter::CriticalError)
162 KMFolder *folder = MessageProperty::filterFolder( msg );
166 tempOpenFolder( folder );
167 result = folder->
moveMsg( msg );
176 int KMFilterMgr::process( TQ_UINT32 serNum,
const KMFilter *filter )
184 if ( isMatching( serNum, filter ) ) {
189 if ( !folder || ( idx == -1 ) || ( idx >= folder->
count() ) ) {
193 KMMsgBase *msgBase = folder->
getMsgBase( idx );
194 bool unGet = !msgBase->isMessage();
197 if ( !msg || !beginFiltering( msg ) ) {
202 if ( filter->execActions( msg, stopIt ) == KMFilter::CriticalError ) {
208 KMFolder *targetFolder = MessageProperty::filterFolder( msg );
211 if ( targetFolder ) {
212 tempOpenFolder( targetFolder );
214 result = targetFolder->
moveMsg( msg );
225 int KMFilterMgr::process(
KMMessage * msg, FilterSet set,
226 bool account, uint accountId ) {
228 return processPop( msg );
230 if ( set == NoSet ) {
231 kdDebug(5006) <<
"KMFilterMgr: process() called with not filter set selected"
237 bool atLeastOneRuleMatched =
false;
239 if (!beginFiltering( msg ))
241 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
242 !stopIt && it != mFilters.constEnd() ; ++it ) {
244 if ( ( ( (set&Inbound) && (*it)->applyOnInbound() ) &&
246 ( account && (*it)->applyOnAccount( accountId ) ) ) ) ||
247 ( (set&Outbound) && (*it)->applyOnOutbound() ) ||
248 ( (set&Explicit) && (*it)->applyOnExplicit() ) ) {
251 if ( FilterLog::instance()->isLogging() ) {
252 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
253 logText.append( (*it)->pattern()->asString() );
254 FilterLog::instance()->add( logText, FilterLog::patternDesc );
256 if ( (*it)->pattern()->matches( msg ) ) {
258 if ( FilterLog::instance()->isLogging() ) {
259 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
260 FilterLog::patternResult );
262 atLeastOneRuleMatched =
true;
264 if ( (*it)->execActions(msg, stopIt) == KMFilter::CriticalError )
270 KMFolder *folder = MessageProperty::filterFolder( msg );
274 if ( atLeastOneRuleMatched )
277 MessageProperty::setFiltering( msg,
false );
279 tempOpenFolder( folder );
286 bool KMFilterMgr::isMatching( TQ_UINT32 serNum,
const KMFilter *filter )
289 if ( FilterLog::instance()->isLogging() ) {
290 TQString logText( i18n(
"<b>Evaluating filter rules:</b> " ) );
291 logText.append( filter->pattern()->asString() );
292 FilterLog::instance()->add( logText, FilterLog::patternDesc );
294 if ( filter->pattern()->matches( serNum ) ) {
295 if ( FilterLog::instance()->isLogging() ) {
296 FilterLog::instance()->add( i18n(
"<b>Filter rules have matched.</b>" ),
297 FilterLog::patternResult );
304 bool KMFilterMgr::atLeastOneFilterAppliesTo(
unsigned int accountID )
const
306 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
307 for ( ; it != mFilters.constEnd() ; ++it ) {
308 if ( (*it)->applyOnAccount( accountID ) ) {
315 bool KMFilterMgr::atLeastOneIncomingFilterAppliesTo(
unsigned int accountID )
const
317 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
318 for ( ; it != mFilters.constEnd() ; ++it ) {
319 if ( (*it)->applyOnInbound() && (*it)->applyOnAccount( accountID ) ) {
326 bool KMFilterMgr::atLeastOneOnlineImapFolderTarget()
328 if (!mDirtyBufferedFolderTarget)
329 return mBufferedFolderTarget;
331 mDirtyBufferedFolderTarget =
false;
333 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
334 for ( ; it != mFilters.constEnd() ; ++it ) {
335 KMFilter *filter = *it;
336 TQPtrListIterator<KMFilterAction> jt( *filter->actions() );
337 for ( jt.toFirst() ; jt.current() ; ++jt ) {
342 KMFolder *folder = kmkernel->imapFolderMgr()->findIdString( name );
344 mBufferedFolderTarget =
true;
349 mBufferedFolderTarget =
false;
354 void KMFilterMgr::ref(
void)
360 void KMFilterMgr::deref(
bool force)
366 if (mRefCount && !force)
368 TQValueVector< KMFolder *>::const_iterator it;
369 for ( it = mOpenFolders.constBegin(); it != mOpenFolders.constEnd(); ++it )
370 (*it)->close(
"filtermgr");
371 mOpenFolders.clear();
376 int KMFilterMgr::tempOpenFolder(
KMFolder* aFolder)
380 int rc = aFolder->
open(
"filermgr");
383 mOpenFolders.append( aFolder );
389 void KMFilterMgr::openDialog( TQWidget *,
bool checkForEmptyFilterList )
397 mEditDialog =
new KMFilterDlg( 0,
"filterdialog", bPopFilter,
398 checkForEmptyFilterList );
405 void KMFilterMgr::createFilter(
const TQCString & field,
const TQString & value )
407 openDialog( 0,
false );
408 mEditDialog->createFilter( field, value );
413 const TQString KMFilterMgr::createUniqueName(
const TQString & name )
415 TQString uniqueName = name;
421 for ( TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
422 it != mFilters.constEnd(); ++it ) {
423 if ( !( (*it)->name().compare( uniqueName ) ) ) {
427 uniqueName += TQString(
" (" ) + TQString::number( counter )
438 void KMFilterMgr::appendFilters(
const TQValueList<KMFilter*> &filters,
439 bool replaceIfNameExists )
441 mDirtyBufferedFolderTarget =
true;
443 if ( replaceIfNameExists ) {
444 TQValueListConstIterator<KMFilter*> it1 = filters.constBegin();
445 for ( ; it1 != filters.constEnd() ; ++it1 ) {
446 TQValueListConstIterator<KMFilter*> it2 = mFilters.constBegin();
447 for ( ; it2 != mFilters.constEnd() ; ++it2 ) {
448 if ( (*it1)->name() == (*it2)->name() ) {
449 mFilters.remove( (*it2) );
450 it2 = mFilters.constBegin();
460 void KMFilterMgr::setFilters(
const TQValueList<KMFilter*> &filters )
469 void KMFilterMgr::slotFolderRemoved(
KMFolder * aFolder )
471 folderRemoved( aFolder, 0 );
477 mDirtyBufferedFolderTarget =
true;
479 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
480 for ( ; it != mFilters.constEnd() ; ++it )
481 if ( (*it)->folderRemoved(aFolder, aNewFolder) )
490 void KMFilterMgr::dump(
void)
const
493 TQValueListConstIterator<KMFilter*> it = mFilters.constBegin();
494 for ( ; it != mFilters.constEnd() ; ++it ) {
495 kdDebug(5006) << (*it)->asString() << endl;
501 void KMFilterMgr::endUpdate(
void)
503 emit filterListUpdated();
506 #include "kmfiltermgr.moc"
Abstract base class for KMail's filter actions that need a mail folder as parameter,...
virtual const TQString argsAsString() const
Return extra arguments as string.
static void sendMDN(KMMessage *msg, KMime::MDN::DispositionType d, const TQValueList< KMime::MDN::DispositionModifier > &m=TQValueList< KMime::MDN::DispositionModifier >())
Automates the sending of MDNs from filter actions.
RAII for KMFolder::open() / close().
int moveMsg(KMMessage *msg, int *index_return=0)
Detaches the given message from it's current folder and adds it to this folder.
int addMsgKeepUID(KMMessage *msg, int *index_return=0)
(Note(bo): This needs to be fixed better at a later point.) This is overridden by dIMAP because addMs...
KMMsgInfo * unGetMsg(int idx)
Replace KMMessage with KMMsgInfo and delete KMMessage
KMMessage * take(int idx)
Detach message from this folder.
KMMessage * getMsg(int idx)
Read message at given index.
const KMMsgBase * getMsgBase(int idx) const
Provides access to the basic message fields that are also stored in the index.
int count(bool cache=false) const
Number of messages in this folder.
int open(const char *owner)
Open folder for access.
int find(const KMMsgBase *msg) const
Returns the index of the given message or -1 if not found.
void setTransferInProgress(bool value, bool force=false)
Set that the message shall not be deleted because it is still required.
void getLocation(unsigned long key, KMFolder **retFolder, int *retIndex) const
Returns the folder the message represented by the serial number key is in and the index in that folde...
static const KMMsgDict * instance()
Access the globally unique MessageDict.
Utility class that provides persisting of filters to/from TDEConfig.
KMail Filter Log Collector.