kaddressbook
filter.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00080
00081 if ( iter == mCategoryList.end() ) {
00082 if ( mMatchRule == Matching || mMatchRule == MatchingAll )
00083 return true;
00084 else {
00085 if ( a.categories().empty() )
00086 return true;
00087 else
00088 return false;
00089 }
00090 }
00091
00092 if ( mMatchRule == Matching ) {
00093 for ( ; iter != mCategoryList.end(); ++iter ) {
00094 if ( a.hasCategory( *iter ) )
00095 return true;
00096 }
00097 } else if ( mMatchRule == MatchingAll ) {
00098 for ( ; iter != mCategoryList.end(); ++iter ) {
00099 if ( !a.hasCategory( *iter ) )
00100 return false;
00101 }
00102 return true;
00103 }
00104
00105 return ( mMatchRule == NotMatching );
00106 }
00107
00108 void Filter::setEnabled( bool on )
00109 {
00110 mEnabled = on;
00111
00112 mIsEmpty = false;
00113 }
00114
00115 bool Filter::isEnabled() const
00116 {
00117 return mEnabled;
00118 }
00119
00120 void Filter::setCategories( const QStringList &list )
00121 {
00122 mCategoryList = list;
00123
00124 mIsEmpty = false;
00125 }
00126
00127 const QStringList &Filter::categories() const
00128 {
00129 return mCategoryList;
00130 }
00131
00132 void Filter::save( KConfig *config )
00133 {
00134 config->writeEntry( "Name", mName );
00135 config->writeEntry( "Enabled", mEnabled );
00136 config->writeEntry( "Categories", mCategoryList );
00137 config->writeEntry( "MatchRule", (int)mMatchRule );
00138 }
00139
00140 void Filter::restore( KConfig *config )
00141 {
00142 mName = config->readEntry( "Name", "<internal error>" );
00143 mEnabled = config->readBoolEntry( "Enabled", true );
00144 mCategoryList = config->readListEntry( "Categories" );
00145 mMatchRule = (MatchRule)config->readNumEntry( "MatchRule", Matching );
00146
00147 mIsEmpty = false;
00148 }
00149
00150 void Filter::save( KConfig *config, const QString &baseGroup, Filter::List &list )
00151 {
00152 {
00153 KConfigGroupSaver s( config, baseGroup );
00154
00155
00156 uint count = config->readNumEntry( "Count" );
00157 for ( uint i = 0; i < count; ++i )
00158 config->deleteGroup( QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00159
00160 }
00161
00162 int index = 0;
00163 Filter::List::Iterator iter;
00164 for ( iter = list.begin(); iter != list.end(); ++iter ) {
00165 if ( !(*iter).mInternal ) {
00166 KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup )
00167 .arg( index ) );
00168 (*iter).save( config );
00169 index++;
00170 }
00171 }
00172
00173 KConfigGroupSaver s( config, baseGroup );
00174 config->writeEntry( "Count", index );
00175 }
00176
00177 Filter::List Filter::restore( KConfig *config, const QString &baseGroup )
00178 {
00179 Filter::List list;
00180 int count = 0;
00181 Filter f;
00182
00183 {
00184 KConfigGroupSaver s( config, baseGroup );
00185 count = config->readNumEntry( "Count", 0 );
00186 }
00187
00188 for ( int i = 0; i < count; i++ ) {
00189 {
00190 KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup ).arg( i ) );
00191 f.restore( config );
00192 }
00193
00194 list.append( f );
00195 }
00196
00197 const QStringList cats = KABPrefs::instance()->customCategories();
00198 for ( QStringList::ConstIterator it = cats.begin(); it != cats.end(); ++it ) {
00199 Filter filter;
00200 filter.mName = *it;
00201 filter.mEnabled = true;
00202 filter.mCategoryList = *it;
00203 filter.mMatchRule = Matching;
00204 filter.mInternal = true;
00205 filter.mIsEmpty = false;
00206 list.append( filter );
00207 }
00208
00209 return list;
00210 }
00211
00212 void Filter::setMatchRule( MatchRule rule )
00213 {
00214 mMatchRule = rule;
00215
00216 mIsEmpty = false;
00217 }
00218
00219 Filter::MatchRule Filter::matchRule() const
00220 {
00221 return mMatchRule;
00222 }
00223
00224 bool Filter::isEmpty() const
00225 {
00226 return mIsEmpty;
00227 }
|