summaryrefslogtreecommitdiffabout
path: root/kabc/addressbook.cpp
Unidiff
Diffstat (limited to 'kabc/addressbook.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addressbook.cpp655
1 files changed, 655 insertions, 0 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp
new file mode 100644
index 0000000..203efc2
--- a/dev/null
+++ b/kabc/addressbook.cpp
@@ -0,0 +1,655 @@
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/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28#ifndef KAB_EMBEDDED
29
30#include <qfile.h>
31#include <qregexp.h>
32#include <qtimer.h>
33
34#include <kapplication.h>
35#include <kinstance.h>
36#include <kstandarddirs.h>
37
38#include "errorhandler.h"
39
40#else //KAB_EMBEDDED
41#include <qptrlist.h>
42#endif //KAB_EMBEDDED
43
44#include <kglobal.h>
45#include <klocale.h>
46#include <kdebug.h>
47#include "addressbook.h"
48#include "resource.h"
49
50#ifndef KAB_EMBEDDED
51#include "addressbook.moc"
52#endif //KAB_EMBEDDED
53
54using namespace KABC;
55
56struct AddressBook::AddressBookData
57{
58 Addressee::List mAddressees;
59 Addressee::List mRemovedAddressees;
60 Field::List mAllFields;
61 KConfig *mConfig;
62 KRES::Manager<Resource> *mManager;
63#ifndef KAB_EMBEDDED
64 ErrorHandler *mErrorHandler;
65#endif //KAB_EMBEDDED
66};
67
68struct AddressBook::Iterator::IteratorData
69{
70 Addressee::List::Iterator mIt;
71};
72
73struct AddressBook::ConstIterator::ConstIteratorData
74{
75 Addressee::List::ConstIterator mIt;
76};
77
78AddressBook::Iterator::Iterator()
79{
80 d = new IteratorData;
81}
82
83AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
84{
85 d = new IteratorData;
86 d->mIt = i.d->mIt;
87}
88
89AddressBook::Iterator &AddressBook::Iterator::operator=( const AddressBook::Iterator &i )
90{
91 if( this == &i ) return *this; // guard against self assignment
92 delete d; // delete the old data the Iterator was completely constructed before
93 d = new IteratorData;
94 d->mIt = i.d->mIt;
95 return *this;
96}
97
98AddressBook::Iterator::~Iterator()
99{
100 delete d;
101}
102
103const Addressee &AddressBook::Iterator::operator*() const
104{
105 return *(d->mIt);
106}
107
108Addressee &AddressBook::Iterator::operator*()
109{
110 return *(d->mIt);
111}
112
113Addressee *AddressBook::Iterator::operator->()
114{
115 return &(*(d->mIt));
116}
117
118AddressBook::Iterator &AddressBook::Iterator::operator++()
119{
120 (d->mIt)++;
121 return *this;
122}
123
124AddressBook::Iterator &AddressBook::Iterator::operator++(int)
125{
126 (d->mIt)++;
127 return *this;
128}
129
130AddressBook::Iterator &AddressBook::Iterator::operator--()
131{
132 (d->mIt)--;
133 return *this;
134}
135
136AddressBook::Iterator &AddressBook::Iterator::operator--(int)
137{
138 (d->mIt)--;
139 return *this;
140}
141
142bool AddressBook::Iterator::operator==( const Iterator &it )
143{
144 return ( d->mIt == it.d->mIt );
145}
146
147bool AddressBook::Iterator::operator!=( const Iterator &it )
148{
149 return ( d->mIt != it.d->mIt );
150}
151
152
153AddressBook::ConstIterator::ConstIterator()
154{
155 d = new ConstIteratorData;
156}
157
158AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
159{
160 d = new ConstIteratorData;
161 d->mIt = i.d->mIt;
162}
163
164AddressBook::ConstIterator &AddressBook::ConstIterator::operator=( const AddressBook::ConstIterator &i )
165{
166 if( this == &i ) return *this; // guard for self assignment
167 delete d; // delete the old data because the Iterator was really constructed before
168 d = new ConstIteratorData;
169 d->mIt = i.d->mIt;
170 return *this;
171}
172
173AddressBook::ConstIterator::~ConstIterator()
174{
175 delete d;
176}
177
178const Addressee &AddressBook::ConstIterator::operator*() const
179{
180 return *(d->mIt);
181}
182
183const Addressee* AddressBook::ConstIterator::operator->() const
184{
185 return &(*(d->mIt));
186}
187
188AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
189{
190 (d->mIt)++;
191 return *this;
192}
193
194AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
195{
196 (d->mIt)++;
197 return *this;
198}
199
200AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
201{
202 (d->mIt)--;
203 return *this;
204}
205
206AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
207{
208 (d->mIt)--;
209 return *this;
210}
211
212bool AddressBook::ConstIterator::operator==( const ConstIterator &it )
213{
214 return ( d->mIt == it.d->mIt );
215}
216
217bool AddressBook::ConstIterator::operator!=( const ConstIterator &it )
218{
219 return ( d->mIt != it.d->mIt );
220}
221
222
223AddressBook::AddressBook()
224{
225 init(0);
226}
227
228AddressBook::AddressBook( const QString &config )
229{
230 init(config);
231}
232
233void AddressBook::init(const QString &config)
234{
235 d = new AddressBookData;
236 if (config != 0) {
237 d->mConfig = new KConfig( config );
238// qDebug("AddressBook::init 1 config=%s",config.latin1() );
239 }
240 else {
241 d->mConfig = 0;
242// qDebug("AddressBook::init 1 config=0");
243 }
244
245#ifndef KAB_EMBEDDED
246 d->mErrorHandler = 0;
247#endif //KAB_EMBEDDED
248 d->mManager = new KRES::Manager<Resource>( "contact" );
249 d->mManager->readConfig( d->mConfig );
250}
251
252AddressBook::~AddressBook()
253{
254 delete d->mConfig; d->mConfig = 0;
255 delete d->mManager; d->mManager = 0;
256#ifndef KAB_EMBEDDED
257 delete d->mErrorHandler; d->mErrorHandler = 0;
258#endif //KAB_EMBEDDED
259 delete d; d = 0;
260}
261
262bool AddressBook::load()
263{
264 kdDebug(5700) << "AddressBook::load()" << endl;
265
266 clear();
267
268 KRES::Manager<Resource>::ActiveIterator it;
269 bool ok = true;
270 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
271 if ( !(*it)->load() ) {
272 error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) );
273 ok = false;
274 }
275
276 // mark all addressees as unchanged
277 Addressee::List::Iterator addrIt;
278 for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt )
279 (*addrIt).setChanged( false );
280
281 return ok;
282}
283
284bool AddressBook::save( Ticket *ticket )
285{
286 kdDebug(5700) << "AddressBook::save()"<< endl;
287
288 if ( ticket->resource() ) {
289 deleteRemovedAddressees();
290
291 return ticket->resource()->save( ticket );
292 }
293
294 return false;
295}
296
297AddressBook::Iterator AddressBook::begin()
298{
299 Iterator it = Iterator();
300 it.d->mIt = d->mAddressees.begin();
301 return it;
302}
303
304AddressBook::ConstIterator AddressBook::begin() const
305{
306 ConstIterator it = ConstIterator();
307 it.d->mIt = d->mAddressees.begin();
308 return it;
309}
310
311AddressBook::Iterator AddressBook::end()
312{
313 Iterator it = Iterator();
314 it.d->mIt = d->mAddressees.end();
315 return it;
316}
317
318AddressBook::ConstIterator AddressBook::end() const
319{
320 ConstIterator it = ConstIterator();
321 it.d->mIt = d->mAddressees.end();
322 return it;
323}
324
325void AddressBook::clear()
326{
327 d->mAddressees.clear();
328}
329
330Ticket *AddressBook::requestSaveTicket( Resource *resource )
331{
332 kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl;
333
334 if ( !resource )
335 {
336 qDebug("AddressBook::requestSaveTicket no resource" );
337 resource = standardResource();
338 }
339
340 KRES::Manager<Resource>::ActiveIterator it;
341 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
342 if ( (*it) == resource ) {
343 if ( (*it)->readOnly() || !(*it)->isOpen() )
344 return 0;
345 else
346 return (*it)->requestSaveTicket();
347 }
348 }
349
350 return 0;
351}
352
353void AddressBook::insertAddressee( const Addressee &a )
354{
355 Addressee::List::Iterator it;
356 for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) {
357 if ( a.uid() == (*it).uid() ) {
358 bool changed = false;
359 Addressee addr = a;
360 if ( addr != (*it) )
361 changed = true;
362
363 (*it) = a;
364 if ( (*it).resource() == 0 )
365 (*it).setResource( standardResource() );
366
367 if ( changed ) {
368 (*it).setRevision( QDateTime::currentDateTime() );
369 (*it).setChanged( true );
370 }
371
372 return;
373 }
374 }
375 d->mAddressees.append( a );
376 Addressee& addr = d->mAddressees.last();
377 if ( addr.resource() == 0 )
378 addr.setResource( standardResource() );
379
380 addr.setChanged( true );
381}
382
383void AddressBook::removeAddressee( const Addressee &a )
384{
385 Iterator it;
386 for ( it = begin(); it != end(); ++it ) {
387 if ( a.uid() == (*it).uid() ) {
388 removeAddressee( it );
389 return;
390 }
391 }
392}
393
394void AddressBook::removeAddressee( const Iterator &it )
395{
396 d->mRemovedAddressees.append( (*it) );
397 d->mAddressees.remove( it.d->mIt );
398}
399
400AddressBook::Iterator AddressBook::find( const Addressee &a )
401{
402 Iterator it;
403 for ( it = begin(); it != end(); ++it ) {
404 if ( a.uid() == (*it).uid() ) {
405 return it;
406 }
407 }
408 return end();
409}
410
411Addressee AddressBook::findByUid( const QString &uid )
412{
413 Iterator it;
414 for ( it = begin(); it != end(); ++it ) {
415 if ( uid == (*it).uid() ) {
416 return *it;
417 }
418 }
419 return Addressee();
420}
421
422Addressee::List AddressBook::allAddressees()
423{
424 return d->mAddressees;
425}
426
427Addressee::List AddressBook::findByName( const QString &name )
428{
429 Addressee::List results;
430
431 Iterator it;
432 for ( it = begin(); it != end(); ++it ) {
433 if ( name == (*it).name() ) {
434 results.append( *it );
435 }
436 }
437
438 return results;
439}
440
441Addressee::List AddressBook::findByEmail( const QString &email )
442{
443 Addressee::List results;
444 QStringList mailList;
445
446 Iterator it;
447 for ( it = begin(); it != end(); ++it ) {
448 mailList = (*it).emails();
449 for ( QStringList::Iterator ite = mailList.begin(); ite != mailList.end(); ++ite ) {
450 if ( email == (*ite) ) {
451 results.append( *it );
452 }
453 }
454 }
455
456 return results;
457}
458
459Addressee::List AddressBook::findByCategory( const QString &category )
460{
461 Addressee::List results;
462
463 Iterator it;
464 for ( it = begin(); it != end(); ++it ) {
465 if ( (*it).hasCategory( category) ) {
466 results.append( *it );
467 }
468 }
469
470 return results;
471}
472
473void AddressBook::dump() const
474{
475 kdDebug(5700) << "AddressBook::dump() --- begin ---" << endl;
476
477 ConstIterator it;
478 for( it = begin(); it != end(); ++it ) {
479 (*it).dump();
480 }
481
482 kdDebug(5700) << "AddressBook::dump() --- end ---" << endl;
483}
484
485QString AddressBook::identifier()
486{
487 QStringList identifier;
488
489
490 KRES::Manager<Resource>::ActiveIterator it;
491 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
492 if ( !(*it)->identifier().isEmpty() )
493 identifier.append( (*it)->identifier() );
494 }
495
496 return identifier.join( ":" );
497}
498
499Field::List AddressBook::fields( int category )
500{
501 if ( d->mAllFields.isEmpty() ) {
502 d->mAllFields = Field::allFields();
503 }
504
505 if ( category == Field::All ) return d->mAllFields;
506
507 Field::List result;
508 Field::List::ConstIterator it;
509 for( it = d->mAllFields.begin(); it != d->mAllFields.end(); ++it ) {
510 if ( (*it)->category() & category ) result.append( *it );
511 }
512
513 return result;
514}
515
516bool AddressBook::addCustomField( const QString &label, int category,
517 const QString &key, const QString &app )
518{
519 if ( d->mAllFields.isEmpty() ) {
520 d->mAllFields = Field::allFields();
521 }
522#ifndef KAB_EMBEDDED
523 QString a = app.isNull() ? KGlobal::instance()->instanceName() : app;
524#else //KAB_EMBEDDED
525 QString a = app.isNull() ? KGlobal::getAppName() : app;
526#endif //KAB_EMBEDDED
527
528 QString k = key.isNull() ? label : key;
529
530 Field *field = Field::createCustomField( label, category, k, a );
531
532 if ( !field ) return false;
533
534 d->mAllFields.append( field );
535
536 return true;
537}
538
539QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
540{
541 if (!ab.d) return s;
542
543 return s << ab.d->mAddressees;
544}
545
546QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
547{
548 if (!ab.d) return s;
549
550 s >> ab.d->mAddressees;
551
552 return s;
553}
554
555bool AddressBook::addResource( Resource *resource )
556{
557 qDebug("AddressBook::addResource 1");
558
559 if ( !resource->open() ) {
560 kdDebug(5700) << "AddressBook::addResource(): can't add resource" << endl;
561 return false;
562 }
563
564 resource->setAddressBook( this );
565
566 d->mManager->add( resource );
567 return true;
568}
569
570bool AddressBook::removeResource( Resource *resource )
571{
572 resource->close();
573
574 if ( resource == standardResource() )
575 d->mManager->setStandardResource( 0 );
576
577 resource->setAddressBook( 0 );
578
579 d->mManager->remove( resource );
580 return true;
581}
582
583QPtrList<Resource> AddressBook::resources()
584{
585 QPtrList<Resource> list;
586
587// qDebug("AddressBook::resources() 1");
588
589 KRES::Manager<Resource>::ActiveIterator it;
590 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
591 list.append( *it );
592
593 return list;
594}
595
596#ifndef KAB_EMBEDDED
597void AddressBook::setErrorHandler( ErrorHandler *handler )
598{
599 delete d->mErrorHandler;
600 d->mErrorHandler = handler;
601}
602#endif //KAB_EMBEDDED
603
604void AddressBook::error( const QString& msg )
605{
606#ifndef KAB_EMBEDDED
607 if ( !d->mErrorHandler ) // create default error handler
608 d->mErrorHandler = new ConsoleErrorHandler;
609
610 if ( d->mErrorHandler )
611 d->mErrorHandler->error( msg );
612 else
613 kdError(5700) << "no error handler defined" << endl;
614#else //KAB_EMBEDDED
615 kdDebug(5700) << "msg" << endl;
616 qDebug(msg);
617#endif //KAB_EMBEDDED
618}
619
620void AddressBook::deleteRemovedAddressees()
621{
622 Addressee::List::Iterator it;
623 for ( it = d->mRemovedAddressees.begin(); it != d->mRemovedAddressees.end(); ++it ) {
624 Resource *resource = (*it).resource();
625 if ( resource && !resource->readOnly() && resource->isOpen() )
626 resource->removeAddressee( *it );
627 }
628
629 d->mRemovedAddressees.clear();
630}
631
632void AddressBook::setStandardResource( Resource *resource )
633{
634// qDebug("AddressBook::setStandardResource 1");
635 d->mManager->setStandardResource( resource );
636}
637
638Resource *AddressBook::standardResource()
639{
640 return d->mManager->standardResource();
641}
642
643KRES::Manager<Resource> *AddressBook::resourceManager()
644{
645 return d->mManager;
646}
647
648void AddressBook::cleanUp()
649{
650 KRES::Manager<Resource>::ActiveIterator it;
651 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
652 if ( !(*it)->readOnly() && (*it)->isOpen() )
653 (*it)->cleanUp();
654 }
655}