-rw-r--r-- | kabc/distributionlist.cpp | 293 |
1 files changed, 293 insertions, 0 deletions
diff --git a/kabc/distributionlist.cpp b/kabc/distributionlist.cpp new file mode 100644 index 0000000..aa2725d --- a/dev/null +++ b/kabc/distributionlist.cpp | |||
@@ -0,0 +1,293 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library 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 GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | #include <ksimpleconfig.h> | ||
22 | #include <kstandarddirs.h> | ||
23 | #include <kdebug.h> | ||
24 | |||
25 | #include "distributionlist.h" | ||
26 | |||
27 | using namespace KABC; | ||
28 | |||
29 | DistributionList::DistributionList( DistributionListManager *manager, | ||
30 | const QString &name ) : | ||
31 | mManager( manager ), mName( name ) | ||
32 | { | ||
33 | mManager->insert( this ); | ||
34 | } | ||
35 | |||
36 | DistributionList::~DistributionList() | ||
37 | { | ||
38 | mManager->remove( this ); | ||
39 | } | ||
40 | |||
41 | void DistributionList::setName( const QString &name ) | ||
42 | { | ||
43 | mName = name; | ||
44 | } | ||
45 | |||
46 | QString DistributionList::name() const | ||
47 | { | ||
48 | return mName; | ||
49 | } | ||
50 | |||
51 | void DistributionList::insertEntry( const Addressee &a, const QString &email ) | ||
52 | { | ||
53 | Entry e( a, email ); | ||
54 | |||
55 | QValueList<Entry>::Iterator it; | ||
56 | for( it = mEntries.begin(); it != mEntries.end(); ++it ) { | ||
57 | if ( (*it).addressee.uid() == a.uid() ) { | ||
58 | /** | ||
59 | We have to check if both email addresses contains no data, | ||
60 | a simple 'email1 == email2' wont work here | ||
61 | */ | ||
62 | if ( ( (*it).email.isNull() && email.isEmpty() ) || | ||
63 | ( (*it).email.isEmpty() && email.isNull() ) || | ||
64 | ( (*it).email == email ) ) { | ||
65 | *it = e; | ||
66 | return; | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | mEntries.append( e ); | ||
71 | } | ||
72 | |||
73 | void DistributionList::removeEntry( const Addressee &a, const QString &email ) | ||
74 | { | ||
75 | QValueList<Entry>::Iterator it; | ||
76 | for( it = mEntries.begin(); it != mEntries.end(); ++it ) { | ||
77 | if ( (*it).addressee.uid() == a.uid() && (*it).email == email ) { | ||
78 | mEntries.remove( it ); | ||
79 | return; | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | QStringList DistributionList::emails() const | ||
85 | { | ||
86 | QStringList emails; | ||
87 | |||
88 | Entry::List::ConstIterator it; | ||
89 | for( it = mEntries.begin(); it != mEntries.end(); ++it ) { | ||
90 | Addressee a = (*it).addressee; | ||
91 | QString email = (*it).email.isEmpty() ? a.fullEmail() : | ||
92 | a.fullEmail( (*it).email ); | ||
93 | |||
94 | if ( !email.isEmpty() ) { | ||
95 | emails.append( email ); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | return emails; | ||
100 | } | ||
101 | |||
102 | DistributionList::Entry::List DistributionList::entries() const | ||
103 | { | ||
104 | return mEntries; | ||
105 | } | ||
106 | |||
107 | |||
108 | DistributionListManager::DistributionListManager( AddressBook *ab ) : | ||
109 | mAddressBook( ab ) | ||
110 | { | ||
111 | } | ||
112 | |||
113 | DistributionListManager::~DistributionListManager() | ||
114 | { | ||
115 | } | ||
116 | |||
117 | DistributionList *DistributionListManager::list( const QString &name ) | ||
118 | { | ||
119 | DistributionList *list; | ||
120 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
121 | if ( list->name() == name ) return list; | ||
122 | } | ||
123 | |||
124 | return 0; | ||
125 | } | ||
126 | |||
127 | void DistributionListManager::insert( DistributionList *l ) | ||
128 | { | ||
129 | DistributionList *list; | ||
130 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
131 | if ( list->name() == l->name() ) { | ||
132 | mLists.remove( list ); | ||
133 | break; | ||
134 | } | ||
135 | } | ||
136 | mLists.append( l ); | ||
137 | } | ||
138 | |||
139 | void DistributionListManager::remove( DistributionList *l ) | ||
140 | { | ||
141 | DistributionList *list; | ||
142 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
143 | if ( list->name() == l->name() ) { | ||
144 | mLists.remove( list ); | ||
145 | return; | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | |||
150 | QStringList DistributionListManager::listNames() | ||
151 | { | ||
152 | QStringList names; | ||
153 | |||
154 | DistributionList *list; | ||
155 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
156 | names.append( list->name() ); | ||
157 | } | ||
158 | |||
159 | return names; | ||
160 | } | ||
161 | |||
162 | bool DistributionListManager::load() | ||
163 | { | ||
164 | KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) ); | ||
165 | |||
166 | #ifndef KAB_EMBEDDED | ||
167 | |||
168 | QMap<QString,QString> entryMap = cfg.entryMap( mAddressBook->identifier() ); | ||
169 | if ( entryMap.isEmpty() ) { | ||
170 | kdDebug(5700) << "No distlists for '" << mAddressBook->identifier() << "'" << endl; | ||
171 | return false; | ||
172 | } | ||
173 | |||
174 | cfg.setGroup( mAddressBook->identifier() ); | ||
175 | |||
176 | QMap<QString,QString>::ConstIterator it; | ||
177 | for( it = entryMap.begin(); it != entryMap.end(); ++it ) { | ||
178 | QString name = it.key(); | ||
179 | |||
180 | #else //KAB_EMBEDDED | ||
181 | cfg.setGroup( mAddressBook->identifier() ); | ||
182 | //US we work in microkde with a list of distributionlists | ||
183 | QStringList distlists = cfg.readListEntry( "Lists" ); | ||
184 | if ( distlists.isEmpty() ) { | ||
185 | kdDebug(5700) << "No distlists for '" << mAddressBook->identifier() << "'" << endl; | ||
186 | return false; | ||
187 | } | ||
188 | |||
189 | QStringList::ConstIterator it; | ||
190 | for( it = distlists.begin(); it != distlists.end(); ++it ) { | ||
191 | QString name = *it; | ||
192 | |||
193 | #endif //KAB_EMBEDDED | ||
194 | |||
195 | QStringList value = cfg.readListEntry( name ); | ||
196 | |||
197 | kdDebug(5700) << "DLM::load(): " << name << ": " << value.join(",") << endl; | ||
198 | |||
199 | DistributionList *list = new DistributionList( this, name ); | ||
200 | |||
201 | QStringList::ConstIterator it2 = value.begin(); | ||
202 | while( it2 != value.end() ) { | ||
203 | QString id = *it2++; | ||
204 | QString email = *it2; | ||
205 | |||
206 | kdDebug(5700) << "----- Entry " << id << endl; | ||
207 | |||
208 | Addressee a = mAddressBook->findByUid( id ); | ||
209 | if ( !a.isEmpty() ) { | ||
210 | list->insertEntry( a, email ); | ||
211 | } | ||
212 | |||
213 | if ( it2 == value.end() ) break; | ||
214 | ++it2; | ||
215 | } | ||
216 | } | ||
217 | |||
218 | return true; | ||
219 | } | ||
220 | |||
221 | bool DistributionListManager::save() | ||
222 | { | ||
223 | kdDebug(5700) << "DistListManager::save()" << endl; | ||
224 | |||
225 | KSimpleConfig cfg( locateLocal( "data", "kabc/distlists" ) ); | ||
226 | |||
227 | cfg.deleteGroup( mAddressBook->identifier() ); | ||
228 | cfg.setGroup( mAddressBook->identifier() ); | ||
229 | |||
230 | DistributionList *list; | ||
231 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
232 | kdDebug(5700) << " Saving '" << list->name() << "'" << endl; | ||
233 | QStringList value; | ||
234 | DistributionList::Entry::List entries = list->entries(); | ||
235 | DistributionList::Entry::List::ConstIterator it; | ||
236 | for( it = entries.begin(); it != entries.end(); ++it ) { | ||
237 | value.append( (*it).addressee.uid() ); | ||
238 | value.append( (*it).email ); | ||
239 | } | ||
240 | cfg.writeEntry( list->name(), value ); | ||
241 | } | ||
242 | |||
243 | #ifdef KAB_EMBEDDED | ||
244 | //US for microKDE we have not yet sophisticated methods to load maps. | ||
245 | // Because of that we store also a list of all distributionlists. | ||
246 | QStringList namelist; | ||
247 | for( list = mLists.first(); list; list = mLists.next() ) { | ||
248 | namelist.append( list->name() ); | ||
249 | } | ||
250 | cfg.writeEntry( "Lists", namelist ); | ||
251 | |||
252 | #endif //KAB_EMBEDDED | ||
253 | |||
254 | cfg.sync(); | ||
255 | |||
256 | return true; | ||
257 | } | ||
258 | |||
259 | DistributionListWatcher* DistributionListWatcher::mSelf = 0; | ||
260 | |||
261 | DistributionListWatcher::DistributionListWatcher() | ||
262 | : QObject( 0, "DistributionListWatcher" ) | ||
263 | { | ||
264 | #ifndef KAB_EMBEDDED | ||
265 | mDirWatch = new KDirWatch; | ||
266 | mDirWatch->addFile( locateLocal( "data", "kabc/distlists" ) ); | ||
267 | |||
268 | connect( mDirWatch, SIGNAL( dirty( const QString& ) ), SIGNAL( changed() ) ); | ||
269 | mDirWatch->startScan(); | ||
270 | #endif //KAB_EMBEDDED | ||
271 | } | ||
272 | |||
273 | DistributionListWatcher::~DistributionListWatcher() | ||
274 | { | ||
275 | #ifndef KAB_EMBEDDED | ||
276 | delete mDirWatch; | ||
277 | mDirWatch = 0; | ||
278 | #endif //KAB_EMBEDDED | ||
279 | } | ||
280 | |||
281 | DistributionListWatcher *DistributionListWatcher::self() | ||
282 | { | ||
283 | if ( !mSelf ) | ||
284 | mSelf = new DistributionListWatcher(); | ||
285 | |||
286 | return mSelf; | ||
287 | } | ||
288 | |||
289 | |||
290 | #ifndef KAB_EMBEDDED | ||
291 | #include "distributionlist.moc" | ||
292 | #endif //KAB_EMBEDDED | ||
293 | |||