26 #include "accountmanager.h"
28 #include "kmacctimap.h"
29 #include "kmfilteraction.h"
31 #include "filterlog.h"
34 #include <tdelocale.h>
35 #include <tdemessagebox.h>
37 #include <tdeconfig.h>
42 KMFilter::KMFilter( TDEConfig* aConfig,
bool popFilter )
43 : bPopFilter(popFilter)
46 mActions.setAutoDelete(
true );
49 readConfig( aConfig );
50 else if ( bPopFilter )
53 bApplyOnInbound =
true;
54 bApplyOnOutbound =
false;
55 bApplyOnExplicit =
true;
56 bStopProcessingHere =
true;
57 bConfigureShortcut =
false;
58 bConfigureToolbar =
false;
65 KMFilter::KMFilter(
const KMFilter & aFilter )
67 bPopFilter = aFilter.isPopFilter();
70 mActions.setAutoDelete(
true );
72 mPattern = aFilter.mPattern;
75 mAction = aFilter.mAction;
77 bApplyOnInbound = aFilter.applyOnInbound();
78 bApplyOnOutbound = aFilter.applyOnOutbound();
79 bApplyOnExplicit = aFilter.applyOnExplicit();
80 bStopProcessingHere = aFilter.stopProcessingHere();
81 bConfigureShortcut = aFilter.configureShortcut();
82 bConfigureToolbar = aFilter.configureToolbar();
83 mApplicability = aFilter.applicability();
84 mIcon = aFilter.icon();
85 mShortcut = aFilter.shortcut();
87 TQPtrListIterator<KMFilterAction> it( aFilter.mActions );
88 for ( it.toFirst() ; it.current() ; ++it ) {
100 TQValueListConstIterator<int> it2;
101 for ( it2 = aFilter.mAccounts.begin() ; it2 != aFilter.mAccounts.end() ; ++it2 )
102 mAccounts.append( *it2 );
107 KMFilter::ReturnCode KMFilter::execActions(
KMMessage* msg,
bool& stopIt )
const
109 ReturnCode status = NoResult;
111 TQPtrListIterator<KMFilterAction> it( mActions );
112 for ( it.toFirst() ; it.current() ; ++it ) {
114 if ( FilterLog::instance()->isLogging() ) {
115 TQString logText( i18n(
"<b>Applying filter action:</b> %1" )
116 .arg( (*it)->displayString() ) );
117 FilterLog::instance()->add( logText, FilterLog::appliedAction );
123 case KMFilterAction::CriticalError:
124 if ( FilterLog::instance()->isLogging() ) {
125 TQString logText = TQString(
"<font color=#FF0000>%1</font>" )
126 .arg( i18n(
"A critical error occurred. Processing stops here." ) );
127 FilterLog::instance()->add( logText, FilterLog::appliedAction );
130 return CriticalError;
131 case KMFilterAction::ErrorButGoOn:
132 if ( FilterLog::instance()->isLogging() ) {
133 TQString logText = TQString(
"<font color=#FF0000>%1</font>" )
134 .arg( i18n(
"A problem was found while applying this action." ) );
135 FilterLog::instance()->add( logText, FilterLog::appliedAction );
142 if ( status == NoResult )
145 stopIt = stopProcessingHere();
150 bool KMFilter::requiresBody( KMMsgBase* msg )
152 if (pattern() && pattern()->requiresBody())
154 TQPtrListIterator<KMFilterAction> it( *actions() );
155 for ( it.toFirst() ; it.current() ; ++it )
156 if ((*it)->requiresBody( msg ))
163 void KMFilter::setAction(
const KMPopFilterAction aAction)
169 KMPopFilterAction KMFilter::action()
179 TQPtrListIterator<KMFilterAction> it( mActions );
180 for ( it.toFirst() ; it.current() ; ++it )
181 if ( (*it)->folderRemoved( aFolder, aNewFolder ) )
187 void KMFilter::setApplyOnAccount( uint
id,
bool aApply )
189 if (aApply && !mAccounts.contains(
id )) {
190 mAccounts.append(
id );
191 }
else if (!aApply && mAccounts.contains(
id )) {
192 mAccounts.remove(
id );
196 bool KMFilter::applyOnAccount( uint
id )
const
198 if ( applicability() == All )
200 if ( applicability() == ButImap ) {
201 KMAccount *account = kmkernel->acctMgr()->find(
id );
202 bool result = account && !
dynamic_cast<KMAcctImap*
>(account);
205 if ( applicability() == Checked )
206 return mAccounts.contains(
id );
213 void KMFilter::readConfig(TDEConfig* config)
217 mPattern.readConfig(config);
221 TQString action = config->readEntry(
"action" );
222 if ( action ==
"down" )
224 else if ( action ==
"later" )
226 else if ( action ==
"delete" )
232 TQStringList sets = config->readListEntry(
"apply-on");
233 if ( sets.isEmpty() && !config->hasKey(
"apply-on") ) {
234 bApplyOnOutbound =
false;
235 bApplyOnInbound =
true;
236 bApplyOnExplicit =
true;
237 mApplicability = ButImap;
239 bApplyOnInbound = bool(sets.contains(
"check-mail"));
240 bApplyOnOutbound = bool(sets.contains(
"send-mail"));
241 bApplyOnExplicit = bool(sets.contains(
"manual-filtering"));
242 mApplicability = (AccountType)config->readNumEntry(
"Applicability", ButImap );
245 bStopProcessingHere = config->readBoolEntry(
"StopProcessingHere",
true);
246 bConfigureShortcut = config->readBoolEntry(
"ConfigureShortcut",
false);
247 TQString shortcut( config->readEntry(
"Shortcut" ) );
248 if ( !shortcut.isEmpty() ) {
249 TDEShortcut sc( shortcut );
252 bConfigureToolbar = config->readBoolEntry(
"ConfigureToolbar",
false);
253 bConfigureToolbar = bConfigureToolbar && bConfigureShortcut;
254 mIcon = config->readEntry(
"Icon",
"gear" );
255 bAutoNaming = config->readBoolEntry(
"AutomaticName",
false);
258 TQString actName, argsName;
262 numActions = config->readNumEntry(
"actions",0);
263 if (numActions > FILTER_MAX_ACTIONS) {
264 numActions = FILTER_MAX_ACTIONS ;
265 KMessageBox::information( 0, i18n(
"<qt>Too many filter actions in filter rule <b>%1</b>.</qt>").arg( mPattern.name() ) );
268 for ( i=0 ; i < numActions ; i++ ) {
269 actName.sprintf(
"action-name-%d", i);
270 argsName.sprintf(
"action-args-%d", i);
272 KMFilterActionDesc *desc = (*kmkernel->filterActionDict())[ config->readEntry( actName ) ];
282 mActions.append( fa );
288 KMessageBox::information( 0 ,
289 i18n(
"<qt>Unknown filter action <b>%1</b><br>in filter rule <b>%2</b>.<br>Ignoring it.</qt>")
290 .arg( config->readEntry( actName ) ).arg( mPattern.name() ) );
293 mAccounts = config->readIntListEntry(
"accounts-set" );
298 void KMFilter::writeConfig(TDEConfig* config)
const
300 mPattern.writeConfig(config);
305 config->writeEntry(
"action",
"down" );
308 config->writeEntry(
"action",
"later" );
311 config->writeEntry(
"action",
"delete" );
314 config->writeEntry(
"action",
"" );
318 if ( bApplyOnInbound )
319 sets.append(
"check-mail" );
320 if ( bApplyOnOutbound )
321 sets.append(
"send-mail" );
322 if ( bApplyOnExplicit )
323 sets.append(
"manual-filtering" );
324 config->writeEntry(
"apply-on", sets );
326 config->writeEntry(
"StopProcessingHere", bStopProcessingHere );
327 config->writeEntry(
"ConfigureShortcut", bConfigureShortcut );
328 if ( !mShortcut.isNull() )
329 config->writeEntry(
"Shortcut", mShortcut.toString() );
330 config->writeEntry(
"ConfigureToolbar", bConfigureToolbar );
331 config->writeEntry(
"Icon", mIcon );
332 config->writeEntry(
"AutomaticName", bAutoNaming );
333 config->writeEntry(
"Applicability", mApplicability );
338 TQPtrListIterator<KMFilterAction> it( mActions );
339 for ( i=0, it.toFirst() ; it.current() ; ++it, ++i ) {
340 config->writeEntry( key.sprintf(
"action-name-%d", i),
342 config->writeEntry( key.sprintf(
"action-args-%d", i),
343 (*it)->argsAsString() );
345 config->writeEntry(
"actions", i );
346 config->writeEntry(
"accounts-set", mAccounts );
350 void KMFilter::purify()
355 TQPtrListIterator<KMFilterAction> it( mActions );
357 while ( it.current() )
358 if ( (*it)->isEmpty() )
359 mActions.remove ( (*it) );
364 TQValueListIterator<int> it2 = mAccounts.begin();
365 while ( it2 != mAccounts.end() ) {
366 if ( !kmkernel->acctMgr()->find( *it2 ) )
367 it2 = mAccounts.remove( it2 );
374 bool KMFilter::isEmpty()
const
377 return mPattern.isEmpty();
379 return mPattern.isEmpty() && mActions.isEmpty() && mAccounts.isEmpty();
383 const TQString KMFilter::asString()
const
387 result += mPattern.asString();
390 result +=
" action: ";
395 TQPtrListIterator<KMFilterAction> it( mActions );
396 for ( it.toFirst() ; it.current() ; ++it ) {
397 result +=
" action: ";
398 result += (*it)->label();
400 result += (*it)->argsAsString();
403 result +=
"This filter belongs to the following sets:";
404 if ( bApplyOnInbound )
405 result +=
" Inbound";
406 if ( bApplyOnOutbound )
407 result +=
" Outbound";
408 if ( bApplyOnExplicit )
409 result +=
" Explicit";
411 if ( bApplyOnInbound && mApplicability == All ) {
412 result +=
"This filter applies to all accounts.\n";
413 }
else if ( bApplyOnInbound && mApplicability == ButImap ) {
414 result +=
"This filter applies to all but online IMAP accounts.\n";
415 }
else if ( bApplyOnInbound ) {
416 TQValueListConstIterator<int> it2;
417 result +=
"This filter applies to the following accounts:";
418 if ( mAccounts.isEmpty() )
420 else for ( it2 = mAccounts.begin() ; it2 != mAccounts.end() ; ++it2 )
421 if ( kmkernel->acctMgr()->find( *it2 ) )
422 result +=
" " + kmkernel->acctMgr()->find( *it2 )->name();
425 if ( bStopProcessingHere )
426 result +=
"If it matches, processing stops at this filter.\n";
Abstract base class for KMail's filter actions.
ReturnCode
Possible return codes of process:
virtual bool isEmpty() const
Determines whether this action is valid.
virtual void argsFromString(const TQString argsStr)=0
Read extra arguments from given string.
The account manager is responsible for creating accounts of various types via the factory method crea...
KMail Filter Log Collector.
Auxiliary struct to KMFilterActionDict.