kmail Library API Documentation

kmfiltermgr.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*- 00002 // kmfiltermgr.cpp 00003 00004 // my header 00005 #ifdef HAVE_CONFIG_H 00006 #include <config.h> 00007 #endif 00008 00009 #include "kmfiltermgr.h" 00010 00011 // other kmail headers 00012 #include "kmfilterdlg.h" 00013 #include "kmfolderindex.h" 00014 #include "messageproperty.h" 00015 using KMail::MessageProperty; 00016 00017 // other KDE headers 00018 #include <kdebug.h> 00019 #include <klocale.h> 00020 #include <kconfig.h> 00021 00022 // other Qt headers 00023 #include <qregexp.h> 00024 00025 // other headers 00026 #include <assert.h> 00027 00028 00029 //----------------------------------------------------------------------------- 00030 KMFilterMgr::KMFilterMgr( bool popFilter ) 00031 : QPtrList<KMFilter>(), 00032 mEditDialog( 0 ), 00033 bPopFilter( popFilter ), 00034 mShowLater( false ), 00035 mRefCount( 0 ) 00036 { 00037 if (bPopFilter) 00038 kdDebug(5006) << "pPopFilter set" << endl; 00039 setAutoDelete(TRUE); 00040 } 00041 00042 00043 //----------------------------------------------------------------------------- 00044 KMFilterMgr::~KMFilterMgr() 00045 { 00046 deref(true); 00047 writeConfig(FALSE); 00048 } 00049 00050 00051 //----------------------------------------------------------------------------- 00052 void KMFilterMgr::readConfig(void) 00053 { 00054 KConfig* config = KMKernel::config(); 00055 int numFilters; 00056 QString grpName; 00057 00058 clear(); 00059 00060 KConfigGroupSaver saver(config, "General"); 00061 00062 if (bPopFilter) { 00063 numFilters = config->readNumEntry("popfilters",0); 00064 mShowLater = config->readNumEntry("popshowDLmsgs",0); 00065 } else { 00066 numFilters = config->readNumEntry("filters",0); 00067 } 00068 00069 for ( int i=0 ; i < numFilters ; ++i ) { 00070 grpName.sprintf("%s #%d", (bPopFilter ? "PopFilter" : "Filter") , i); 00071 KConfigGroupSaver saver(config, grpName); 00072 KMFilter * filter = new KMFilter(config, bPopFilter); 00073 filter->purify(); 00074 if ( filter->isEmpty() ) { 00075 #ifndef NDEBUG 00076 kdDebug(5006) << "KMFilter::readConfig: filter\n" << filter->asString() 00077 << "is empty!" << endl; 00078 #endif 00079 delete filter; 00080 } else 00081 append(filter); 00082 } 00083 } 00084 00085 00086 //----------------------------------------------------------------------------- 00087 void KMFilterMgr::writeConfig(bool withSync) 00088 { 00089 KConfig* config = KMKernel::config(); 00090 00091 // first, delete all groups: 00092 QStringList filterGroups = 00093 config->groupList().grep( QRegExp( bPopFilter ? "PopFilter #\\d+" : "Filter #\\d+" ) ); 00094 for ( QStringList::Iterator it = filterGroups.begin() ; 00095 it != filterGroups.end() ; ++it ) 00096 config->deleteGroup( *it ); 00097 00098 // Now, write out the new stuff: 00099 int i = 0; 00100 QString grpName; 00101 for ( QPtrListIterator<KMFilter> it(*this) ; it.current() ; ++it ) 00102 if ( !(*it)->isEmpty() ) { 00103 if ( bPopFilter ) 00104 grpName.sprintf("PopFilter #%d", i); 00105 else 00106 grpName.sprintf("Filter #%d", i); 00107 KConfigGroupSaver saver(config, grpName); 00108 (*it)->writeConfig(config); 00109 ++i; 00110 } 00111 00112 KConfigGroupSaver saver(config, "General"); 00113 if (bPopFilter) { 00114 config->writeEntry("popfilters", i); 00115 config->writeEntry("popshowDLmsgs", mShowLater); 00116 } else 00117 config->writeEntry("filters", i); 00118 00119 if (withSync) config->sync(); 00120 } 00121 00122 00123 int KMFilterMgr::processPop( KMMessage * msg ) const { 00124 for ( QPtrListIterator<KMFilter> it( *this ) ; it.current() ; ++it ) 00125 if ( (*it)->pattern()->matches( msg ) ) 00126 return (*it)->action(); 00127 return NoAction; 00128 } 00129 00130 bool KMFilterMgr::beginFiltering(KMMsgBase *msgBase) const 00131 { 00132 if (MessageProperty::filtering( msgBase )) 00133 return false; 00134 MessageProperty::setFiltering( msgBase, true ); 00135 MessageProperty::setFilterFolder( msgBase, 0 ); 00136 return true; 00137 } 00138 00139 int KMFilterMgr::moveMessage(KMMessage *msg) const 00140 { 00141 if (MessageProperty::filterFolder(msg)->moveMsg( msg ) == 0) { 00142 if ( kmkernel->folderIsTrash( MessageProperty::filterFolder( msg ))) 00143 KMFilterAction::sendMDN( msg, KMime::MDN::Deleted ); 00144 } else { 00145 kdDebug(5006) << "KMfilterAction - couldn't move msg" << endl; 00146 return 2; 00147 } 00148 return 0; 00149 } 00150 00151 void KMFilterMgr::endFiltering(KMMsgBase *msgBase) const 00152 { 00153 KMFolder *parent = msgBase->parent(); 00154 if ( parent ) { 00155 if ( parent == MessageProperty::filterFolder( msgBase ) ) { 00156 parent->take( parent->find( msgBase ) ); 00157 } 00158 else if ( ! MessageProperty::filterFolder( msgBase ) ) { 00159 int index = parent->find( msgBase ); 00160 KMMessage *msg = parent->getMsg( index ); 00161 parent->take( index ); 00162 parent->addMsgKeepUID( msg ); 00163 } 00164 } 00165 MessageProperty::setFiltering( msgBase, false ); 00166 } 00167 00168 int KMFilterMgr::process( KMMessage * msg, const KMFilter * filter ) { 00169 if ( !msg || !filter || !beginFiltering( msg )) 00170 return 1; 00171 bool stopIt = false; 00172 int result = 1; 00173 if (filter->execActions( msg, stopIt ) == KMFilter::CriticalError) 00174 return 2; 00175 00176 KMFolder *folder = MessageProperty::filterFolder( msg ); 00177 00178 endFiltering( msg ); 00179 if (folder) { 00180 tempOpenFolder( folder ); 00181 result = folder->moveMsg( msg ); 00182 } 00183 return result; 00184 } 00185 00186 int KMFilterMgr::process( KMMessage * msg, FilterSet set ) { 00187 if ( bPopFilter ) 00188 return processPop( msg ); 00189 00190 if ( set == NoSet ) { 00191 kdDebug(5006) << "KMFilterMgr: process() called with not filter set selected" 00192 << endl; 00193 return 1; 00194 } 00195 00196 bool stopIt = false; 00197 00198 if (!beginFiltering( msg )) 00199 return 1; 00200 for ( QPtrListIterator<KMFilter> it(*this) ; !stopIt && it.current() ; ++it ) { 00201 00202 if ( ( (set&Outbound) && (*it)->applyOnOutbound() ) || 00203 ( (set&Inbound) && (*it)->applyOnInbound() ) || 00204 ( (set&Explicit) && (*it)->applyOnExplicit() ) ) { 00205 // filter is applicable 00206 00207 if ( (*it)->pattern()->matches( msg ) ) { 00208 // filter matches 00209 // execute actions: 00210 if ( (*it)->execActions(msg, stopIt) == KMFilter::CriticalError ) 00211 return 2; 00212 } 00213 } 00214 } 00215 00216 KMFolder *folder = MessageProperty::filterFolder( msg ); 00217 endFiltering( msg ); 00218 if (folder) { 00219 tempOpenFolder( folder ); 00220 folder->moveMsg(msg); 00221 return 0; 00222 } 00223 return 1; 00224 } 00225 00226 00227 //----------------------------------------------------------------------------- 00228 void KMFilterMgr::ref(void) 00229 { 00230 mRefCount++; 00231 } 00232 00233 //----------------------------------------------------------------------------- 00234 void KMFilterMgr::deref(bool force) 00235 { 00236 if (!force) 00237 mRefCount--; 00238 if (mRefCount < 0) 00239 mRefCount = 0; 00240 if (mRefCount && !force) 00241 return; 00242 QPtrListIterator<KMFolder> it(mOpenFolders); 00243 for ( it.toFirst() ; it.current() ; ++it ) 00244 (*it)->close(); 00245 mOpenFolders.clear(); 00246 } 00247 00248 00249 //----------------------------------------------------------------------------- 00250 int KMFilterMgr::tempOpenFolder(KMFolder* aFolder) 00251 { 00252 assert( aFolder ); 00253 00254 int rc = aFolder->open(); 00255 if (rc) return rc; 00256 00257 mOpenFolders.append( aFolder ); 00258 return 0; 00259 } 00260 00261 00262 //----------------------------------------------------------------------------- 00263 void KMFilterMgr::openDialog( QWidget * ) 00264 { 00265 if( !mEditDialog ) 00266 { 00267 // 00268 // We can't use the parent as long as the dialog is modeless 00269 // and there is one shared dialog for all top level windows. 00270 // 00271 mEditDialog = new KMFilterDlg( 0, "filterdialog", bPopFilter ); 00272 } 00273 mEditDialog->show(); 00274 } 00275 00276 00277 //----------------------------------------------------------------------------- 00278 void KMFilterMgr::createFilter( const QCString & field, const QString & value ) 00279 { 00280 openDialog( 0 ); 00281 mEditDialog->createFilter( field, value ); 00282 } 00283 00284 00285 //----------------------------------------------------------------------------- 00286 bool KMFilterMgr::folderRemoved(KMFolder* aFolder, KMFolder* aNewFolder) 00287 { 00288 bool rem = FALSE; 00289 00290 QPtrListIterator<KMFilter> it(*this); 00291 for ( it.toFirst() ; it.current() ; ++it ) 00292 if ( (*it)->folderRemoved(aFolder, aNewFolder) ) rem=TRUE; 00293 00294 return rem; 00295 } 00296 00297 00298 //----------------------------------------------------------------------------- 00299 #ifndef NDEBUG 00300 void KMFilterMgr::dump(void) 00301 { 00302 QPtrListIterator<KMFilter> it(*this); 00303 for ( it.toFirst() ; it.current() ; ++it ) 00304 { 00305 kdDebug(5006) << (*it)->asString() << endl; 00306 } 00307 } 00308 #endif 00309 00310 //----------------------------------------------------------------------------- 00311 void KMFilterMgr::endUpdate(void) 00312 { 00313 emit filterListUpdated(); 00314 } 00315 00316 #include "kmfiltermgr.moc"
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 Wed Jul 28 23:58:00 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003