kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 ), mIsEmpty( true )
00034 {
00035 }
00036 
00037 Filter::Filter( const QString &name )
00038   : mName( name ), mMatchRule( Matching ), mEnabled( true ),
00039     mInternal( false ), mIsEmpty( false )
00040 {
00041 }
00042 
00043 Filter::~Filter()
00044 {
00045 }
00046 
00047 void Filter::setName( const QString &name )
00048 {
00049   mName = name;
00050 
00051   mIsEmpty = false;
00052 }
00053 
00054 const QString &Filter::name() const
00055 {
00056   return mName;
00057 }
00058 
00059 bool Filter::isInternal() const
00060 {
00061   return mInternal;
00062 }
00063 
00064 void Filter::apply( KABC::Addressee::List &addresseeList )
00065 {
00066   KABC::Addressee::List::Iterator iter;
00067   for ( iter = addresseeList.begin(); iter != addresseeList.end(); ) {
00068     if ( filterAddressee( *iter ) )
00069       ++iter;
00070     else
00071       iter = addresseeList.erase( iter );
00072   }
00073 }
00074 
00075 bool Filter::filterAddressee( const KABC::Addressee &a ) const
00076 {
00077   QStringList::ConstIterator iter;
00078   iter = mCategoryList.begin();
00079   // empty filter always matches
00080 
00081   if ( iter == mCategoryList.end() ) {
00082     if ( mMatchRule == Matching )
00083       return true;
00084     else {
00085       if ( a.categories().empty() )
00086         return true;
00087       else
00088         return false;
00089     }
00090   }
00091 
00092   for ( ; iter != mCategoryList.end(); ++iter ) {
00093     if ( a.hasCategory( *iter ) )
00094       return ( mMatchRule == Matching );
00095   }
00096 
00097   return !( mMatchRule == Matching );
00098 }
00099 
00100 void Filter::setEnabled( bool on )
00101 {
00102   mEnabled = on;
00103 
00104   mIsEmpty = false;
00105 }
00106 
00107 bool Filter::isEnabled() const
00108 {
00109   return mEnabled;
00110 }
00111 
00112 void Filter::setCategories( const QStringList &list )
00113 {
00114   mCategoryList = list;
00115 
00116   mIsEmpty = false;
00117 }
00118 
00119 const QStringList &Filter::categories() const
00120 {
00121   return mCategoryList;
00122 }
00123 
00124 void Filter::save( KConfig *config )
00125 {
00126   config->writeEntry( "Name", mName );
00127   config->writeEntry( "Enabled", mEnabled );
00128   config->writeEntry( "Categories", mCategoryList );
00129   config->writeEntry( "MatchRule", (int)mMatchRule );
00130 }
00131 
00132 void Filter::restore( KConfig *config )
00133 {
00134   mName = config->readEntry( "Name", "<internal error>" );
00135   mEnabled = config->readBoolEntry( "Enabled", true );
00136   mCategoryList = config->readListEntry( "Categories" );
00137   mMatchRule = (MatchRule)config->readNumEntry( "MatchRule", Matching );
00138 
00139   mIsEmpty = false;
00140 }
00141 
00142 void Filter::save( KConfig *config, const QString &baseGroup, Filter::List &list )
00143 {
00144   {
00145     KConfigGroupSaver s( config, baseGroup );
00146 
00147     // remove the old filters
00148     uint count = config->readNumEntry( "Count" );
00149     for ( uint i = 0; i < count; ++i )
00150       config->deleteGroup( QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00151 
00152   }
00153 
00154   int index = 0;
00155   Filter::List::Iterator iter;
00156   for ( iter = list.begin(); iter != list.end(); ++iter ) {
00157     if ( !(*iter).mInternal ) {
00158       KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup )
00159                                                      .arg( index ) );
00160       (*iter).save( config );
00161       index++;
00162     }
00163   }
00164 
00165   KConfigGroupSaver s( config, baseGroup );
00166   config->writeEntry( "Count", index );
00167 }
00168 
00169 Filter::List Filter::restore( KConfig *config, const QString &baseGroup )
00170 {
00171   Filter::List list;
00172   int count = 0;
00173   Filter f;
00174 
00175   {
00176     KConfigGroupSaver s( config, baseGroup );
00177     count = config->readNumEntry( "Count", 0 );
00178   }
00179 
00180   for ( int i = 0; i < count; i++ ) {
00181     {
00182       KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00183       f.restore( config );
00184     }
00185 
00186     list.append( f );
00187   }
00188 
00189   const QStringList cats = KABPrefs::instance()->customCategories();
00190   for ( QStringList::ConstIterator it = cats.begin(); it != cats.end(); ++it ) {
00191     Filter filter;
00192     filter.mName = *it;
00193     filter.mEnabled = true;
00194     filter.mCategoryList = *it;
00195     filter.mMatchRule = Matching;
00196     filter.mInternal = true;
00197     filter.mIsEmpty = false;
00198     list.append( filter );
00199   }
00200 
00201   return list;
00202 }
00203 
00204 void Filter::setMatchRule( MatchRule rule )
00205 {
00206   mMatchRule = rule;
00207 
00208   mIsEmpty = false;
00209 }
00210 
00211 Filter::MatchRule Filter::matchRule() const
00212 {
00213   return mMatchRule;
00214 }
00215 
00216 bool Filter::isEmpty() const
00217 {
00218   return mIsEmpty;
00219 }
KDE Home | KDE Accessibility Home | Description of Access Keys