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 "filter.h"
00028
00029 Filter::Filter()
00030 {
00031 mName = QString::null;
00032 mEnabled = true;
00033 mMatchRule = Matching;
00034 }
00035
00036 Filter::Filter(const QString &name)
00037 {
00038 mName = name;
00039 mEnabled = true;
00040 mMatchRule = Matching;
00041 }
00042
00043 Filter::Filter(const Filter ©From)
00044 {
00045 (*this) = copyFrom;
00046 }
00047
00048 Filter::~Filter()
00049 {
00050 }
00051
00052 Filter &Filter::operator=(const Filter ©From)
00053 {
00054 if (this == ©From)
00055 return *this;
00056
00057 mName = copyFrom.mName;
00058 mEnabled = copyFrom.mEnabled;
00059 mMatchRule = copyFrom.mMatchRule;
00060 mCategoryList = copyFrom.mCategoryList;
00061
00062 return *this;
00063 }
00064
00065 void Filter::apply(KABC::Addressee::List &addresseeList)
00066 {
00067 KABC::Addressee::List::Iterator iter;
00068 for (iter = addresseeList.begin(); iter != addresseeList.end(); )
00069 {
00070 if (filterAddressee(*iter))
00071 ++iter;
00072 else
00073 iter = addresseeList.erase(iter);
00074 }
00075 }
00076
00077 bool Filter::filterAddressee(const KABC::Addressee &a)
00078 {
00079 QStringList::Iterator iter;
00080 iter = mCategoryList.begin();
00081
00082
00083 if ( iter == mCategoryList.end() )
00084 return true;
00085
00086 for (; iter != mCategoryList.end(); ++iter ) {
00087 if ( a.hasCategory( *iter ) )
00088 return ( mMatchRule == Matching );
00089 }
00090
00091 return !( mMatchRule == Matching );
00092 }
00093
00094 void Filter::save(KConfig *config)
00095 {
00096 config->writeEntry("Name", mName);
00097 config->writeEntry("Enabled", mEnabled);
00098 config->writeEntry("Categories", mCategoryList);
00099 config->writeEntry("MatchRule", (int)mMatchRule);
00100 }
00101
00102 void Filter::restore(KConfig *config)
00103 {
00104 mName = config->readEntry("Name", "<internal error>");
00105 mEnabled = config->readBoolEntry("Enabled", true);
00106 mCategoryList = config->readListEntry("Categories");
00107 mMatchRule = (MatchRule)config->readNumEntry("MatchRule", Matching);
00108 }
00109
00110 void Filter::save(KConfig *config, QString baseGroup,
00111 Filter::List &list)
00112 {
00113 {
00114 KConfigGroupSaver s(config, baseGroup);
00115
00116
00117 uint count = config->readNumEntry( "Count" );
00118 for ( uint i = 0; i < count; ++i )
00119 config->deleteGroup( QString("%1_%2").arg(baseGroup).arg(i) );
00120
00121 config->writeEntry("Count", list.count());
00122 }
00123
00124 int index = 0;
00125 Filter::List::Iterator iter;
00126 for (iter = list.begin(); iter != list.end(); ++iter)
00127 {
00128 {
00129 KConfigGroupSaver s(config, QString("%1_%2").arg(baseGroup).arg(index));
00130 (*iter).save(config);
00131 }
00132 index++;
00133 }
00134 }
00135
00136 Filter::List Filter::restore(KConfig *config, QString baseGroup)
00137 {
00138 Filter::List list;
00139 int count = 0;
00140 Filter f;
00141
00142 {
00143 KConfigGroupSaver s(config, baseGroup);
00144 count = config->readNumEntry("Count", 0);
00145 }
00146
00147 for (int i = 0; i < count; i++)
00148 {
00149 {
00150 KConfigGroupSaver s(config, QString("%1_%2").arg(baseGroup).arg(i));
00151 f.restore(config);
00152 }
00153 list.append(f);
00154 }
00155
00156 return list;
00157 }
This file is part of the documentation for kdelibs Version 3.1.5.