kaddressbook

filter.cpp
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeconfig.h>
25#include <kdebug.h>
26
27#include "kabprefs.h"
28
29#include "filter.h"
30
31Filter::Filter()
32 : mName( TQString() ), mMatchRule( Matching ), mEnabled( true ),
33 mInternal( false ), mIsEmpty( true )
34{
35}
36
37Filter::Filter( const TQString &name )
38 : mName( name ), mMatchRule( Matching ), mEnabled( true ),
39 mInternal( false ), mIsEmpty( false )
40{
41}
42
43Filter::~Filter()
44{
45}
46
47void Filter::setName( const TQString &name )
48{
49 mName = name;
50
51 mIsEmpty = false;
52}
53
54const TQString &Filter::name() const
55{
56 return mName;
57}
58
60{
61 return mInternal;
62}
63
64void Filter::apply( TDEABC::Addressee::List &addresseeList )
65{
66 TDEABC::Addressee::List::Iterator iter;
67 for ( iter = addresseeList.begin(); iter != addresseeList.end(); ) {
68 if ( filterAddressee( *iter ) )
69 ++iter;
70 else
71 iter = addresseeList.erase( iter );
72 }
73}
74
75bool Filter::filterAddressee( const TDEABC::Addressee &a ) const
76{
77 TQStringList::ConstIterator iter;
78 iter = mCategoryList.begin();
79 // empty filter always matches
80
81 if ( iter == mCategoryList.end() ) {
82 if ( mMatchRule == Matching )
83 return true;
84 else {
85 if ( a.categories().empty() )
86 return true;
87 else
88 return false;
89 }
90 }
91
92 for ( ; iter != mCategoryList.end(); ++iter ) {
93 if ( a.hasCategory( *iter ) )
94 return ( mMatchRule == Matching );
95 }
96
97 return !( mMatchRule == Matching );
98}
99
100void Filter::setEnabled( bool on )
101{
102 mEnabled = on;
103
104 mIsEmpty = false;
105}
106
108{
109 return mEnabled;
110}
111
112void Filter::setCategories( const TQStringList &list )
113{
114 mCategoryList = list;
115
116 mIsEmpty = false;
117}
118
119const TQStringList &Filter::categories() const
120{
121 return mCategoryList;
122}
123
124void Filter::save( TDEConfig *config )
125{
126 config->writeEntry( "Name", mName );
127 config->writeEntry( "Enabled", mEnabled );
128 config->writeEntry( "Categories", mCategoryList );
129 config->writeEntry( "MatchRule", (int)mMatchRule );
130}
131
132void Filter::restore( TDEConfig *config )
133{
134 mName = config->readEntry( "Name", "<internal error>" );
135 mEnabled = config->readBoolEntry( "Enabled", true );
136 mCategoryList = config->readListEntry( "Categories" );
137 mMatchRule = (MatchRule)config->readNumEntry( "MatchRule", Matching );
138
139 mIsEmpty = false;
140}
141
142void Filter::save( TDEConfig *config, const TQString &baseGroup, Filter::List &list )
143{
144 {
145 TDEConfigGroupSaver s( config, baseGroup );
146
147 // remove the old filters
148 uint count = config->readNumEntry( "Count" );
149 for ( uint i = 0; i < count; ++i )
150 config->deleteGroup( TQString( "%1_%2" ).arg( baseGroup ).arg( i ) );
151
152 }
153
154 int index = 0;
155 Filter::List::Iterator iter;
156 for ( iter = list.begin(); iter != list.end(); ++iter ) {
157 if ( !(*iter).mInternal ) {
158 TDEConfigGroupSaver s( config, TQString( "%1_%2" ).arg( baseGroup )
159 .arg( index ) );
160 (*iter).save( config );
161 index++;
162 }
163 }
164
165 TDEConfigGroupSaver s( config, baseGroup );
166 config->writeEntry( "Count", index );
167}
168
169Filter::List Filter::restore( TDEConfig *config, const TQString &baseGroup )
170{
171 Filter::List list;
172 int count = 0;
173 Filter f;
174
175 {
176 TDEConfigGroupSaver s( config, baseGroup );
177 count = config->readNumEntry( "Count", 0 );
178 }
179
180 for ( int i = 0; i < count; i++ ) {
181 {
182 TDEConfigGroupSaver s( config, TQString( "%1_%2" ).arg( baseGroup ).arg( i ) );
183 f.restore( config );
184 }
185
186 list.append( f );
187 }
188
189 const TQStringList cats = KABPrefs::instance()->customCategories();
190 for ( TQStringList::ConstIterator it = cats.begin(); it != cats.end(); ++it ) {
191 Filter filter;
192 filter.mName = *it;
193 filter.mEnabled = true;
194 filter.mCategoryList = *it;
195 filter.mMatchRule = Matching;
196 filter.mInternal = true;
197 filter.mIsEmpty = false;
198 list.append( filter );
199 }
200
201 return list;
202}
203
204void Filter::setMatchRule( MatchRule rule )
205{
206 mMatchRule = rule;
207
208 mIsEmpty = false;
209}
210
211Filter::MatchRule Filter::matchRule() const
212{
213 return mMatchRule;
214}
215
216bool Filter::isEmpty() const
217{
218 return mIsEmpty;
219}
Filter for AddressBook related objects (Addressees)
Definition: filter.h:40
MatchRule matchRule() const
Definition: filter.cpp:211
bool isEnabled() const
Definition: filter.cpp:107
bool isEmpty() const
Definition: filter.cpp:216
void setName(const TQString &name)
Set the name of the filter.
Definition: filter.cpp:47
void restore(TDEConfig *config)
Loads the filter from the config file.
Definition: filter.cpp:132
void setEnabled(bool on)
Enable or disable the filter.
Definition: filter.cpp:100
const TQStringList & categories() const
Definition: filter.cpp:119
void setCategories(const TQStringList &list)
Set the list of categories.
Definition: filter.cpp:112
bool filterAddressee(const TDEABC::Addressee &a) const
Apply the filter to the addressee.
Definition: filter.cpp:75
void save(TDEConfig *config)
Saves the filter to the config file.
Definition: filter.cpp:124
const TQString & name() const
Definition: filter.cpp:54
void apply(TDEABC::Addressee::List &addresseeList)
Apply the filter to the addressee list.
Definition: filter.cpp:64
bool isInternal() const
Definition: filter.cpp:59
void setMatchRule(MatchRule rule)
Sets the filter rule.
Definition: filter.cpp:204