kmail Library API Documentation

kmfilter.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*- 00002 // kmfilter.cpp 00003 // Author: Stefan Taferner <taferner@kde.org> 00004 00005 #ifdef HAVE_CONFIG_H 00006 #include <config.h> 00007 #endif 00008 00009 #include "kmfilter.h" 00010 #include "kmfilteraction.h" 00011 #include "kmglobal.h" 00012 #include "filterlog.h" 00013 using KMail::FilterLog; 00014 00015 #include <klocale.h> 00016 #include <kmessagebox.h> 00017 #include <kdebug.h> 00018 #include <kconfig.h> 00019 00020 #include <assert.h> 00021 00022 00023 KMFilter::KMFilter( KConfig* aConfig, bool popFilter ) 00024 : bPopFilter(popFilter) 00025 { 00026 if (!bPopFilter) 00027 mActions.setAutoDelete( true ); 00028 00029 if ( aConfig ) 00030 readConfig( aConfig ); 00031 else if ( bPopFilter ) 00032 mAction = Down; 00033 else { 00034 bApplyOnInbound = true; 00035 bApplyOnOutbound = false; 00036 bApplyOnExplicit = true; 00037 bStopProcessingHere = true; 00038 bConfigureShortcut = false; 00039 } 00040 } 00041 00042 00043 KMFilter::KMFilter( const KMFilter & aFilter ) 00044 { 00045 bPopFilter = aFilter.isPopFilter(); 00046 00047 if ( !bPopFilter ) 00048 mActions.setAutoDelete( true ); 00049 00050 mPattern = aFilter.mPattern; 00051 00052 if ( bPopFilter ){ 00053 mAction = aFilter.mAction; 00054 } else { 00055 bApplyOnInbound = aFilter.applyOnInbound(); 00056 bApplyOnOutbound = aFilter.applyOnOutbound(); 00057 bApplyOnExplicit = aFilter.applyOnExplicit(); 00058 bStopProcessingHere = aFilter.stopProcessingHere(); 00059 bConfigureShortcut = aFilter.configureShortcut(); 00060 mIcon = aFilter.icon(); 00061 00062 QPtrListIterator<KMFilterAction> it( aFilter.mActions ); 00063 for ( it.toFirst() ; it.current() ; ++it ) { 00064 KMFilterActionDesc *desc = (*kmkernel->filterActionDict())[ (*it)->name() ]; 00065 if ( desc ) { 00066 KMFilterAction *f = desc->create(); 00067 if ( f ) { 00068 f->argsFromString( (*it)->argsAsString() ); 00069 mActions.append( f ); 00070 } 00071 } 00072 } 00073 } 00074 } 00075 00076 // only for !bPopFilter 00077 KMFilter::ReturnCode KMFilter::execActions( KMMessage* msg, bool& stopIt ) const 00078 { 00079 ReturnCode status = NoResult; 00080 00081 QPtrListIterator<KMFilterAction> it( mActions ); 00082 for ( it.toFirst() ; it.current() ; ++it ) { 00083 00084 if ( FilterLog::instance()->isLogging() ) { 00085 QString logText( i18n( "<b>Applying filter action:</b> " ) ); 00086 logText.append( (*it)->label() ); 00087 logText.append( " \"" ); 00088 logText.append( FilterLog::recode( (*it)->argsAsString() ) ); 00089 logText.append( "\"" ); 00090 FilterLog::instance()->add( logText, FilterLog::appliedAction ); 00091 } 00092 00093 KMFilterAction::ReturnCode result = (*it)->process( msg ); 00094 00095 switch ( result ) { 00096 case KMFilterAction::CriticalError: 00097 // in case it's a critical error: return immediately! 00098 return CriticalError; 00099 case KMFilterAction::ErrorButGoOn: 00100 default: 00101 break; 00102 } 00103 } 00104 00105 if ( status == NoResult ) // No filters matched, keep copy of message 00106 status = GoOn; 00107 00108 stopIt = stopProcessingHere(); 00109 00110 return status; 00111 } 00112 00113 bool KMFilter::requiresBody( KMMsgBase* msg ) 00114 { 00115 if (pattern() && pattern()->requiresBody()) 00116 return true; // no pattern means always matches? 00117 QPtrListIterator<KMFilterAction> it( *actions() ); 00118 for ( it.toFirst() ; it.current() ; ++it ) 00119 if ((*it)->requiresBody( msg )) 00120 return true; 00121 return false; 00122 } 00123 00125 // only for bPopFilter 00126 void KMFilter::setAction(const KMPopFilterAction aAction) 00127 { 00128 mAction = aAction; 00129 } 00130 00131 // only for bPopFilter 00132 KMPopFilterAction KMFilter::action() 00133 { 00134 return mAction; 00135 } 00136 00137 // only for !bPopFilter 00138 bool KMFilter::folderRemoved( KMFolder* aFolder, KMFolder* aNewFolder ) 00139 { 00140 bool rem = false; 00141 00142 QPtrListIterator<KMFilterAction> it( mActions ); 00143 for ( it.toFirst() ; it.current() ; ++it ) 00144 if ( (*it)->folderRemoved( aFolder, aNewFolder ) ) 00145 rem = true; 00146 00147 return rem; 00148 } 00149 00150 //----------------------------------------------------------------------------- 00151 void KMFilter::readConfig(KConfig* config) 00152 { 00153 // MKSearchPattern::readConfig ensures 00154 // that the pattern is purified. 00155 mPattern.readConfig(config); 00156 00157 if (bPopFilter) { 00158 // get the action description... 00159 QString action = config->readEntry( "action" ); 00160 if ( action == "down" ) 00161 mAction = Down; 00162 else if ( action == "later" ) 00163 mAction = Later; 00164 else if ( action == "delete" ) 00165 mAction = Delete; 00166 else 00167 mAction = NoAction; 00168 } 00169 else { 00170 QStringList sets = config->readListEntry("apply-on"); 00171 if ( sets.isEmpty() && !config->hasKey("apply-on") ) { 00172 bApplyOnOutbound = false; 00173 bApplyOnInbound = true; 00174 bApplyOnExplicit = true; 00175 } else { 00176 bApplyOnInbound = bool(sets.contains("check-mail")); 00177 bApplyOnOutbound = bool(sets.contains("send-mail")); 00178 bApplyOnExplicit = bool(sets.contains("manual-filtering")); 00179 } 00180 00181 bStopProcessingHere = config->readBoolEntry("StopProcessingHere", true); 00182 bConfigureShortcut = config->readBoolEntry("ConfigureShortcut", false); 00183 mIcon = config->readEntry( "Icon", "gear" ); 00184 00185 int i, numActions; 00186 QString actName, argsName; 00187 00188 mActions.clear(); 00189 00190 numActions = config->readNumEntry("actions",0); 00191 if (numActions > FILTER_MAX_ACTIONS) { 00192 numActions = FILTER_MAX_ACTIONS ; 00193 KMessageBox::information( 0, i18n("<qt>Too many filter actions in filter rule <b>%1</b>.</qt>").arg( mPattern.name() ) ); 00194 } 00195 00196 for ( i=0 ; i < numActions ; i++ ) { 00197 actName.sprintf("action-name-%d", i); 00198 argsName.sprintf("action-args-%d", i); 00199 // get the action description... 00200 KMFilterActionDesc *desc = (*kmkernel->filterActionDict())[ config->readEntry( actName ) ]; 00201 if ( desc ) { 00202 //...create an instance... 00203 KMFilterAction *fa = desc->create(); 00204 if ( fa ) { 00205 //...load it with it's parameter... 00206 fa->argsFromString( config->readEntry( argsName ) ); 00207 //...check if it's emoty and... 00208 if ( !fa->isEmpty() ) 00209 //...append it if it's not and... 00210 mActions.append( fa ); 00211 else 00212 //...delete is else. 00213 delete fa; 00214 } 00215 } else 00216 KMessageBox::information( 0 /* app-global modal dialog box */, 00217 i18n("<qt>Unknown filter action <b>%1</b><br>in filter rule <b>%2</b>.<br>Ignoring it.</qt>") 00218 .arg( config->readEntry( actName ) ).arg( mPattern.name() ) ); 00219 } 00220 } 00221 } 00222 00223 00224 void KMFilter::writeConfig(KConfig* config) const 00225 { 00226 mPattern.writeConfig(config); 00227 00228 if (bPopFilter) { 00229 switch ( mAction ) { 00230 case Down: 00231 config->writeEntry( "action", "down" ); 00232 break; 00233 case Later: 00234 config->writeEntry( "action", "later" ); 00235 break; 00236 case Delete: 00237 config->writeEntry( "action", "delete" ); 00238 break; 00239 default: 00240 config->writeEntry( "action", "" ); 00241 } 00242 } else { 00243 QStringList sets; 00244 if ( bApplyOnInbound ) 00245 sets.append( "check-mail" ); 00246 if ( bApplyOnOutbound ) 00247 sets.append( "send-mail" ); 00248 if ( bApplyOnExplicit ) 00249 sets.append( "manual-filtering" ); 00250 config->writeEntry( "apply-on", sets ); 00251 00252 config->writeEntry( "StopProcessingHere", bStopProcessingHere ); 00253 config->writeEntry( "ConfigureShortcut", bConfigureShortcut ); 00254 config->writeEntry( "Icon", mIcon ); 00255 00256 QString key; 00257 int i; 00258 00259 QPtrListIterator<KMFilterAction> it( mActions ); 00260 for ( i=0, it.toFirst() ; it.current() ; ++it, ++i ) { 00261 config->writeEntry( key.sprintf("action-name-%d", i), 00262 (*it)->name() ); 00263 config->writeEntry( key.sprintf("action-args-%d", i), 00264 (*it)->argsAsString() ); 00265 } 00266 config->writeEntry("actions", i ); 00267 } 00268 } 00269 00270 void KMFilter::purify() 00271 { 00272 mPattern.purify(); 00273 00274 if (!bPopFilter) { 00275 QPtrListIterator<KMFilterAction> it( mActions ); 00276 it.toLast(); 00277 while ( it.current() ) 00278 if ( (*it)->isEmpty() ) 00279 mActions.remove ( (*it) ); 00280 else 00281 --it; 00282 } 00283 } 00284 00285 bool KMFilter::isEmpty() const 00286 { 00287 if (bPopFilter) 00288 return mPattern.isEmpty(); 00289 else 00290 return mPattern.isEmpty() && mActions.isEmpty(); 00291 } 00292 00293 #ifndef NDEBUG 00294 const QString KMFilter::asString() const 00295 { 00296 QString result; 00297 00298 result += mPattern.asString(); 00299 00300 if (bPopFilter){ 00301 result += " action: "; 00302 result += mAction; 00303 result += "\n"; 00304 } 00305 else { 00306 QPtrListIterator<KMFilterAction> it( mActions ); 00307 for ( it.toFirst() ; it.current() ; ++it ) { 00308 result += " action: "; 00309 result += (*it)->label(); 00310 result += " "; 00311 result += (*it)->argsAsString(); 00312 result += "\n"; 00313 } 00314 result += "This filter belongs to the following sets:"; 00315 if ( bApplyOnInbound ) 00316 result += " Inbound"; 00317 if ( bApplyOnOutbound ) 00318 result += " Outbound"; 00319 if ( bApplyOnExplicit ) 00320 result += " Explicit"; 00321 result += "\n"; 00322 if ( bStopProcessingHere ) 00323 result += "If it matches, processing stops at this filter.\n"; 00324 } 00325 return result; 00326 } 00327 #endif
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:52:30 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003