-rw-r--r-- | kaddressbook/filter.cpp | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/kaddressbook/filter.cpp b/kaddressbook/filter.cpp new file mode 100644 index 0000000..b0d04ca --- a/dev/null +++ b/kaddressbook/filter.cpp | |||
@@ -0,0 +1,205 @@ | |||
1 | /* | ||
2 | This file is part of KAddressBook. | ||
3 | Copyright (c) 2002 Mike Pilone <mpilone@slac.com> | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | |||
19 | As a special exception, permission is given to link this program | ||
20 | with any edition of Qt, and distribute the resulting executable, | ||
21 | without including the source code for Qt in the source distribution. | ||
22 | */ | ||
23 | |||
24 | #include <kconfig.h> | ||
25 | #include <kconfigbase.h> | ||
26 | #include <kdebug.h> | ||
27 | |||
28 | #include "kabprefs.h" | ||
29 | |||
30 | #include "filter.h" | ||
31 | |||
32 | Filter::Filter() | ||
33 | : mName( QString::null ), mMatchRule( Matching ), mEnabled( true ), | ||
34 | mInternal( false ) | ||
35 | { | ||
36 | } | ||
37 | |||
38 | Filter::Filter( const QString &name ) | ||
39 | : mName( name ), mMatchRule( Matching ), mEnabled( true ), | ||
40 | mInternal( false ) | ||
41 | { | ||
42 | } | ||
43 | |||
44 | Filter::~Filter() | ||
45 | { | ||
46 | } | ||
47 | |||
48 | void Filter::setName( const QString &name ) | ||
49 | { | ||
50 | mName = name; | ||
51 | } | ||
52 | |||
53 | const QString &Filter::name() const | ||
54 | { | ||
55 | return mName; | ||
56 | } | ||
57 | |||
58 | bool Filter::isInternal() const | ||
59 | { | ||
60 | return mInternal; | ||
61 | } | ||
62 | |||
63 | void Filter::apply( KABC::Addressee::List &addresseeList ) | ||
64 | { | ||
65 | KABC::Addressee::List::Iterator iter; | ||
66 | for ( iter = addresseeList.begin(); iter != addresseeList.end(); ) { | ||
67 | if ( filterAddressee( *iter ) ) | ||
68 | ++iter; | ||
69 | else | ||
70 | { | ||
71 | #ifndef KAB_EMBEDDED | ||
72 | iter = addresseeList.erase( iter ); | ||
73 | #else //KAB_EMBEDDED | ||
74 | iter = addresseeList.remove( iter ); | ||
75 | #endif //KAB_EMBEDDED | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | |||
80 | bool Filter::filterAddressee( const KABC::Addressee &a ) | ||
81 | { | ||
82 | QStringList::Iterator iter; | ||
83 | iter = mCategoryList.begin(); | ||
84 | // empty filter always matches | ||
85 | |||
86 | if ( iter == mCategoryList.end() ) | ||
87 | return true; | ||
88 | |||
89 | for ( ; iter != mCategoryList.end(); ++iter ) { | ||
90 | if ( a.hasCategory( *iter ) ) | ||
91 | return ( mMatchRule == Matching ); | ||
92 | } | ||
93 | |||
94 | return !( mMatchRule == Matching ); | ||
95 | } | ||
96 | |||
97 | void Filter::setEnabled( bool on ) | ||
98 | { | ||
99 | mEnabled = on; | ||
100 | } | ||
101 | |||
102 | bool Filter::isEnabled() const | ||
103 | { | ||
104 | return mEnabled; | ||
105 | } | ||
106 | |||
107 | void Filter::setCategories( const QStringList &list ) | ||
108 | { | ||
109 | mCategoryList = list; | ||
110 | } | ||
111 | |||
112 | const QStringList &Filter::categories() const | ||
113 | { | ||
114 | return mCategoryList; | ||
115 | } | ||
116 | |||
117 | void Filter::save( KConfig *config ) | ||
118 | { | ||
119 | config->writeEntry( "Name", mName ); | ||
120 | config->writeEntry( "Enabled", mEnabled ); | ||
121 | config->writeEntry( "Categories", mCategoryList ); | ||
122 | config->writeEntry( "MatchRule", (int)mMatchRule ); | ||
123 | } | ||
124 | |||
125 | void Filter::restore( KConfig *config ) | ||
126 | { | ||
127 | mName = config->readEntry( "Name", "<internal error>" ); | ||
128 | mEnabled = config->readBoolEntry( "Enabled", true ); | ||
129 | mCategoryList = config->readListEntry( "Categories" ); | ||
130 | mMatchRule = (MatchRule)config->readNumEntry( "MatchRule", Matching ); | ||
131 | } | ||
132 | |||
133 | void Filter::save( KConfig *config, QString baseGroup, Filter::List &list ) | ||
134 | { | ||
135 | { | ||
136 | KConfigGroupSaver s( config, baseGroup ); | ||
137 | |||
138 | // remove the old filters | ||
139 | uint count = config->readNumEntry( "Count" ); | ||
140 | /* // memory access violation here | ||
141 | for ( uint i = 0; i < count; ++i ) | ||
142 | config->deleteGroup( QString( "%1_%2" ).arg( baseGroup ).arg( i ) ); | ||
143 | */ | ||
144 | } | ||
145 | |||
146 | int index = 0; | ||
147 | Filter::List::Iterator iter; | ||
148 | for ( iter = list.begin(); iter != list.end(); ++iter ) { | ||
149 | if ( !(*iter).mInternal ) { | ||
150 | KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup ).arg( index ) ); | ||
151 | |||
152 | (*iter).save( config ); | ||
153 | index++; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | KConfigGroupSaver s( config, baseGroup ); | ||
158 | |||
159 | config->writeEntry( "Count", index ); | ||
160 | |||
161 | } | ||
162 | |||
163 | Filter::List Filter::restore( KConfig *config, QString baseGroup ) | ||
164 | { | ||
165 | Filter::List list; | ||
166 | int count = 0; | ||
167 | Filter f; | ||
168 | |||
169 | { | ||
170 | KConfigGroupSaver s( config, baseGroup ); | ||
171 | count = config->readNumEntry( "Count", 0 ); | ||
172 | } | ||
173 | |||
174 | for ( int i = 0; i < count; i++ ) { | ||
175 | { | ||
176 | KConfigGroupSaver s( config, QString( "%1_%2" ).arg( baseGroup ).arg( i ) ); | ||
177 | f.restore( config ); | ||
178 | } | ||
179 | |||
180 | list.append( f ); | ||
181 | } | ||
182 | |||
183 | QStringList cats = KABPrefs::instance()->mCustomCategories; | ||
184 | for ( QStringList::Iterator it = cats.begin(); it != cats.end(); ++it ) { | ||
185 | Filter filter; | ||
186 | filter.mName = *it; | ||
187 | filter.mEnabled = true; | ||
188 | filter.mCategoryList = *it; | ||
189 | filter.mMatchRule = Matching; | ||
190 | filter.mInternal = true; | ||
191 | list.append( filter ); | ||
192 | } | ||
193 | |||
194 | return list; | ||
195 | } | ||
196 | |||
197 | void Filter::setMatchRule( MatchRule rule ) | ||
198 | { | ||
199 | mMatchRule = rule; | ||
200 | } | ||
201 | |||
202 | Filter::MatchRule Filter::matchRule() const | ||
203 | { | ||
204 | return mMatchRule; | ||
205 | } | ||