kaddressbook Library API Documentation

filter.cpp

00001 /*                                                                      
00002     This file is part of KAddressBook.                                  
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
00004                                                                         
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or   
00008     (at your option) any later version.                                 
00009                                                                         
00010     This program is distributed in the hope that it will be useful,     
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of      
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
00013     GNU General Public License for more details.                        
00014                                                                         
00015     You should have received a copy of the GNU General Public License   
00016     along with this program; if not, write to the Free Software         
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           
00018                                                                         
00019     As a special exception, permission is given to link this program    
00020     with any edition of Qt, and distribute the resulting executable,    
00021     without including the source code for Qt in the source distribution.
00022 */                                                                      
00023 
00024 #include <kconfig.h>
00025 #include <kdebug.h>
00026 
00027 #include "kabprefs.h"
00028 
00029 #include "filter.h"
00030 
00031 Filter::Filter()
00032   : mName( QString::null ), mMatchRule( Matching ), mEnabled( true ),
00033     mInternal( false )
00034 {
00035 }
00036 
00037 Filter::Filter( const QString &name )
00038   : mName( name ), mMatchRule( Matching ), mEnabled( true ),
00039     mInternal( false )
00040 {
00041 }
00042 
00043 Filter::~Filter()
00044 {
00045 }
00046 
00047 void Filter::setName( const QString &name )
00048 {
00049   mName = name;
00050 }
00051 
00052 const QString &Filter::name() const
00053 {
00054   return mName;
00055 }
00056 
00057 bool Filter::isInternal() const
00058 {
00059   return mInternal;
00060 }
00061 
00062 void Filter::apply( KABC::Addressee::List &addresseeList )
00063 {
00064   KABC::Addressee::List::Iterator iter;
00065   for ( iter = addresseeList.begin(); iter != addresseeList.end(); ) {
00066     if ( filterAddressee( *iter ) )
00067       ++iter;
00068     else
00069       iter = addresseeList.erase( iter );
00070   }
00071 }
00072 
00073 bool Filter::filterAddressee( const KABC::Addressee &a )
00074 {
00075   QStringList::Iterator iter;
00076   iter = mCategoryList.begin();
00077   // empty filter always matches
00078 
00079   if ( iter == mCategoryList.end() )
00080     return true;
00081 
00082   for ( ; iter != mCategoryList.end(); ++iter ) {
00083     if ( a.hasCategory( *iter ) )
00084       return ( mMatchRule == Matching );
00085   }
00086   
00087   return !( mMatchRule == Matching );
00088 }
00089 
00090 void Filter::setEnabled( bool on )
00091 {
00092   mEnabled = on;
00093 }
00094 
00095 bool Filter::isEnabled() const
00096 {
00097   return mEnabled;
00098 }
00099 
00100 void Filter::setCategories( const QStringList &list )
00101 {
00102   mCategoryList = list;
00103 }
00104     
00105 const QStringList &Filter::categories() const
00106 {
00107   return mCategoryList;
00108 }
00109 
00110 void Filter::save( KConfig *config )
00111 {
00112   config->writeEntry( "Name", mName );
00113   config->writeEntry( "Enabled", mEnabled );
00114   config->writeEntry( "Categories", mCategoryList );
00115   config->writeEntry( "MatchRule", (int)mMatchRule );
00116 }
00117 
00118 void Filter::restore( KConfig *config )
00119 {
00120   mName = config->readEntry( "Name", "<internal error>" );
00121   mEnabled = config->readBoolEntry( "Enabled", true );
00122   mCategoryList = config->readListEntry( "Categories" );
00123   mMatchRule = (MatchRule)config->readNumEntry( "MatchRule", Matching );
00124 }
00125 
00126 void Filter::save( KConfig *config, QString baseGroup, Filter::List &list )
00127 {
00128   {
00129     KConfigGroupSaver s( config, baseGroup );
00130 
00131     // remove the old filters
00132     uint count = config->readNumEntry( "Count" );
00133     for ( uint i = 0; i < count; ++i )
00134       config->deleteGroup( QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00135 
00136   }
00137 
00138   int index = 0;
00139   Filter::List::Iterator iter;
00140   for ( iter = list.begin(); iter != list.end(); ++iter ) {
00141     if ( !(*iter).mInternal ) {
00142       KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup )
00143                                                      .arg( index ) );
00144       (*iter).save( config );
00145       index++;
00146     }
00147   }
00148 
00149   KConfigGroupSaver s( config, baseGroup );
00150   config->writeEntry( "Count", index );
00151 }
00152                      
00153 Filter::List Filter::restore( KConfig *config, QString baseGroup )
00154 {
00155   Filter::List list;
00156   int count = 0;
00157   Filter f;
00158   
00159   {
00160     KConfigGroupSaver s( config, baseGroup );
00161     count = config->readNumEntry( "Count", 0 );
00162   }
00163   
00164   for ( int i = 0; i < count; i++ ) {
00165     {
00166       KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00167       f.restore( config );
00168     }
00169 
00170     list.append( f );
00171   }
00172 
00173   QStringList cats = KABPrefs::instance()->mCustomCategories;
00174   for ( QStringList::Iterator it = cats.begin(); it != cats.end(); ++it ) {
00175     Filter filter;
00176     filter.mName = *it;
00177     filter.mEnabled = true;
00178     filter.mCategoryList = *it;
00179     filter.mMatchRule = Matching;
00180     filter.mInternal = true;
00181     list.append( filter );
00182   }
00183 
00184   return list;
00185 }
00186 
00187 void Filter::setMatchRule( MatchRule rule )
00188 {
00189   mMatchRule = rule;
00190 }
00191     
00192 Filter::MatchRule Filter::matchRule() const
00193 {
00194   return mMatchRule;
00195 }
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:51 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003