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