filter.cpp
00001
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 )
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
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
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 }
This file is part of the documentation for kaddressbook Library Version 3.2.2.