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., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "resource.h" |
22 | |
23 | #include <kdebug.h> |
24 | #include <klocalizedstring.h> |
25 | |
26 | using namespace KABC; |
27 | |
28 | class Ticket::Private |
29 | { |
30 | public: |
31 | Private( Resource *resource ) |
32 | : mResource( resource ) |
33 | { |
34 | } |
35 | |
36 | Resource *mResource; |
37 | }; |
38 | |
39 | Ticket::Ticket( Resource *resource ) |
40 | : d( new Private( resource ) ) |
41 | { |
42 | } |
43 | |
44 | Ticket::~Ticket() |
45 | { |
46 | delete d; |
47 | } |
48 | |
49 | Resource *Ticket::resource() |
50 | { |
51 | return d->mResource; |
52 | } |
53 | |
54 | class Resource::Iterator::Private |
55 | { |
56 | public: |
57 | Addressee::Map::Iterator mIt; |
58 | }; |
59 | |
60 | class Resource::ConstIterator::Private |
61 | { |
62 | public: |
63 | Addressee::Map::ConstIterator mIt; |
64 | }; |
65 | |
66 | Resource::Iterator::Iterator() |
67 | : d( new Private ) |
68 | { |
69 | } |
70 | |
71 | Resource::Iterator::Iterator( const Resource::Iterator &other ) |
72 | : d( new Private ) |
73 | { |
74 | d->mIt = other.d->mIt; |
75 | } |
76 | |
77 | Resource::Iterator &Resource::Iterator::operator=( const Resource::Iterator &other ) |
78 | { |
79 | if ( this != &other ) { |
80 | d->mIt = other.d->mIt; |
81 | } |
82 | |
83 | return *this; |
84 | } |
85 | |
86 | Resource::Iterator::~Iterator() |
87 | { |
88 | delete d; |
89 | } |
90 | |
91 | const Addressee &Resource::Iterator::operator*() const |
92 | { |
93 | return d->mIt.value(); |
94 | } |
95 | |
96 | Addressee &Resource::Iterator::operator*() |
97 | { |
98 | return d->mIt.value(); |
99 | } |
100 | |
101 | Resource::Iterator &Resource::Iterator::operator++() |
102 | { |
103 | ( d->mIt )++; |
104 | return *this; |
105 | } |
106 | |
107 | Resource::Iterator &Resource::Iterator::operator++( int ) |
108 | { |
109 | ( d->mIt )++; |
110 | return *this; |
111 | } |
112 | |
113 | Resource::Iterator &Resource::Iterator::operator--() |
114 | { |
115 | ( d->mIt )--; |
116 | return *this; |
117 | } |
118 | |
119 | Resource::Iterator &Resource::Iterator::operator--( int ) |
120 | { |
121 | ( d->mIt )--; |
122 | return *this; |
123 | } |
124 | |
125 | bool Resource::Iterator::operator==( const Iterator &it ) const |
126 | { |
127 | return d->mIt == it.d->mIt; |
128 | } |
129 | |
130 | bool Resource::Iterator::operator!=( const Iterator &it ) const |
131 | { |
132 | return d->mIt != it.d->mIt; |
133 | } |
134 | |
135 | Resource::ConstIterator::ConstIterator() |
136 | : d( new Private ) |
137 | { |
138 | } |
139 | |
140 | Resource::ConstIterator::ConstIterator( const Resource::ConstIterator &other ) |
141 | : d( new Private ) |
142 | { |
143 | d->mIt = other.d->mIt; |
144 | } |
145 | |
146 | #ifndef QT_STRICT_ITERATORS |
147 | Resource::ConstIterator::ConstIterator( const Resource::Iterator &other ) |
148 | : d( new Private ) |
149 | { |
150 | d->mIt = other.d->mIt; |
151 | } |
152 | #endif |
153 | |
154 | Resource::ConstIterator &Resource::ConstIterator::operator=( const Resource::ConstIterator &other ) |
155 | { |
156 | if ( this != &other ) { |
157 | d->mIt = other.d->mIt; |
158 | } |
159 | |
160 | return *this; |
161 | } |
162 | |
163 | Resource::ConstIterator::~ConstIterator() |
164 | { |
165 | delete d; |
166 | } |
167 | |
168 | const Addressee &Resource::ConstIterator::operator*() const |
169 | { |
170 | return *( d->mIt ); |
171 | } |
172 | |
173 | Resource::ConstIterator &Resource::ConstIterator::operator++() |
174 | { |
175 | ( d->mIt )++; |
176 | return *this; |
177 | } |
178 | |
179 | Resource::ConstIterator &Resource::ConstIterator::operator++( int ) |
180 | { |
181 | ( d->mIt )++; |
182 | return *this; |
183 | } |
184 | |
185 | Resource::ConstIterator &Resource::ConstIterator::operator--() |
186 | { |
187 | --( d->mIt ); |
188 | return *this; |
189 | } |
190 | |
191 | Resource::ConstIterator &Resource::ConstIterator::operator--( int ) |
192 | { |
193 | --( d->mIt ); |
194 | return *this; |
195 | } |
196 | |
197 | bool Resource::ConstIterator::operator==( const ConstIterator &it ) const |
198 | { |
199 | return d->mIt == it.d->mIt; |
200 | } |
201 | |
202 | bool Resource::ConstIterator::operator!=( const ConstIterator &it ) const |
203 | { |
204 | return d->mIt != it.d->mIt; |
205 | } |
206 | |
207 | class Resource::Private |
208 | { |
209 | public: |
210 | Private() |
211 | : mAddressBook( 0 ) |
212 | { |
213 | } |
214 | |
215 | AddressBook *mAddressBook; |
216 | }; |
217 | |
218 | Resource::Resource() |
219 | : KRES::Resource(), d( new Private ) |
220 | { |
221 | } |
222 | |
223 | Resource::Resource( const KConfigGroup &group ) |
224 | : KRES::Resource( group ), d( new Private ) |
225 | { |
226 | } |
227 | |
228 | Resource::~Resource() |
229 | { |
230 | clear(); |
231 | delete d; |
232 | } |
233 | |
234 | Resource::Iterator Resource::begin() |
235 | { |
236 | Iterator it; |
237 | it.d->mIt = mAddrMap.begin(); |
238 | |
239 | return it; |
240 | } |
241 | |
242 | Resource::ConstIterator Resource::begin() const |
243 | { |
244 | ConstIterator it; |
245 | it.d->mIt = mAddrMap.constBegin(); |
246 | return it; |
247 | } |
248 | |
249 | Resource::Iterator Resource::end() |
250 | { |
251 | Iterator it; |
252 | it.d->mIt = mAddrMap.end(); |
253 | |
254 | return it; |
255 | } |
256 | |
257 | Resource::ConstIterator Resource::end() const |
258 | { |
259 | ConstIterator it; |
260 | it.d->mIt = mAddrMap.constEnd(); |
261 | return it; |
262 | } |
263 | |
264 | void Resource::writeConfig( KConfigGroup &group ) |
265 | { |
266 | KRES::Resource::writeConfig( group ); |
267 | } |
268 | |
269 | void Resource::setAddressBook( AddressBook *ab ) |
270 | { |
271 | d->mAddressBook = ab; |
272 | } |
273 | |
274 | AddressBook *Resource::addressBook() |
275 | { |
276 | return d->mAddressBook; |
277 | } |
278 | |
279 | Ticket *Resource::createTicket( Resource *resource ) |
280 | { |
281 | return new Ticket( resource ); |
282 | } |
283 | |
284 | void Resource::insertAddressee( const Addressee &addr ) |
285 | { |
286 | mAddrMap.insert( addr.uid(), addr ); |
287 | } |
288 | |
289 | void Resource::removeAddressee( const Addressee &addr ) |
290 | { |
291 | mAddrMap.remove( addr.uid() ); |
292 | } |
293 | |
294 | Addressee Resource::findByUid( const QString &uid ) |
295 | { |
296 | Addressee::Map::ConstIterator it = mAddrMap.constFind( uid ); |
297 | |
298 | if ( it != mAddrMap.constEnd() ) { |
299 | return it.value(); |
300 | } |
301 | |
302 | return Addressee(); |
303 | } |
304 | |
305 | Addressee::List Resource::findByName( const QString &name ) // TODO: const |
306 | { |
307 | Addressee::List results; |
308 | |
309 | ConstIterator it; |
310 | ConstIterator end( constEnd() ); |
311 | for ( it = constBegin(); it != end; ++it ) { |
312 | if ( name == ( *it ).name() ) { |
313 | results.append( *it ); |
314 | } |
315 | } |
316 | |
317 | return results; |
318 | } |
319 | |
320 | Addressee::List Resource::findByEmail( const QString &email ) // TODO: const |
321 | { |
322 | Addressee::List results; |
323 | const QString lowerEmail = email.toLower(); |
324 | |
325 | ConstIterator it; |
326 | for ( it = constBegin(); it != constEnd(); ++it ) { |
327 | const QStringList mailList = ( *it ).emails(); |
328 | const QStringList::ConstIterator end( mailList.end() ); |
329 | for ( QStringList::ConstIterator ite = mailList.begin(); ite != end; ++ite ) { |
330 | if ( lowerEmail == ( *ite ).toLower() ) { |
331 | results.append( *it ); |
332 | } |
333 | } |
334 | } |
335 | |
336 | return results; |
337 | } |
338 | |
339 | Addressee::List Resource::findByCategory( const QString &category ) // TODO: const |
340 | { |
341 | Addressee::List results; |
342 | |
343 | ConstIterator it; |
344 | ConstIterator end( constEnd() ); |
345 | for ( it = constBegin(); it != end; ++it ) { |
346 | if ( ( *it ).hasCategory( category ) ) { |
347 | results.append( *it ); |
348 | } |
349 | } |
350 | |
351 | return results; |
352 | } |
353 | |
354 | void Resource::clear() |
355 | { |
356 | mAddrMap.clear(); |
357 | |
358 | // take a copy of mDistListMap, then clear it and finally qDeleteAll |
359 | // the copy to avoid problems with removeDistributionList() called by |
360 | // ~DistributionList(). |
361 | DistributionListMap tempDistListMap( mDistListMap ); |
362 | mDistListMap.clear(); |
363 | qDeleteAll( tempDistListMap ); |
364 | } |
365 | |
366 | void Resource::insertDistributionList( DistributionList *list ) |
367 | { |
368 | Q_ASSERT( list ); |
369 | |
370 | mDistListMap.insert( list->identifier(), list ); |
371 | } |
372 | |
373 | void Resource::removeDistributionList( DistributionList *list ) |
374 | { |
375 | Q_ASSERT( list ); |
376 | |
377 | DistributionListMap::iterator it = mDistListMap.find( list->identifier() ); |
378 | if ( it != mDistListMap.end() ) { |
379 | if ( it.value() == list ) { |
380 | mDistListMap.erase( it ); |
381 | } |
382 | } |
383 | } |
384 | |
385 | DistributionList *Resource::findDistributionListByIdentifier( const QString &identifier ) |
386 | { |
387 | return mDistListMap.value( identifier ); |
388 | } |
389 | |
390 | DistributionList *Resource::findDistributionListByName( const QString &name, |
391 | Qt::CaseSensitivity caseSensitivity ) |
392 | { |
393 | QString searchName = name; |
394 | if ( caseSensitivity == Qt::CaseInsensitive ) { |
395 | searchName = name.toLower(); |
396 | } |
397 | |
398 | DistributionListMap::const_iterator it = mDistListMap.constBegin(); |
399 | DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); |
400 | for ( ; it != endIt; ++it ) { |
401 | if ( caseSensitivity == Qt::CaseSensitive ) { |
402 | if ( searchName == it.value()->name() ) { |
403 | return it.value(); |
404 | } |
405 | } else { |
406 | if ( searchName == it.value()->name().toLower() ) { |
407 | return it.value(); |
408 | } |
409 | } |
410 | } |
411 | |
412 | return 0; |
413 | } |
414 | |
415 | QList<DistributionList*> Resource::allDistributionLists() |
416 | { |
417 | return mDistListMap.values(); |
418 | } |
419 | |
420 | QStringList Resource::allDistributionListNames() const |
421 | { |
422 | QStringList results; |
423 | |
424 | DistributionListMap::const_iterator it = mDistListMap.constBegin(); |
425 | DistributionListMap::const_iterator endIt = mDistListMap.constEnd(); |
426 | for ( ; it != endIt; ++it ) { |
427 | results += it.value()->name(); |
428 | } |
429 | |
430 | return results; |
431 | } |
432 | |
433 | bool Resource::asyncLoad() |
434 | { |
435 | bool ok = load(); |
436 | if ( !ok ) { |
437 | emit loadingError( this, i18n( "Loading resource '%1' failed." , resourceName() ) ); |
438 | } else { |
439 | emit loadingFinished( this ); |
440 | } |
441 | |
442 | return ok; |
443 | } |
444 | |
445 | bool Resource::asyncSave( Ticket *ticket ) |
446 | { |
447 | bool ok = save( ticket ); |
448 | if ( !ok ) { |
449 | emit savingError( this, i18n( "Saving resource '%1' failed." , resourceName() ) ); |
450 | } else { |
451 | emit savingFinished( this ); |
452 | } |
453 | |
454 | return ok; |
455 | } |
456 | |
457 | |