• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 API Reference
  • KDE Home
  • Contact Us
 

kabc

  • kabc
addressbook.cpp
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 "addressbook.h"
22 #include "distributionlist.h"
23 #include "errorhandler.h"
24 #include "resource.h"
25 
26 #include <kdebug.h>
27 #include <kglobal.h>
28 #include <kcomponentdata.h>
29 #include <klocale.h>
30 #include <kstandarddirs.h>
31 
32 #include "addressbook.moc"
33 
34 using namespace KABC;
35 
36 class AddressBook::Private
37 {
38  public:
39  Field::List mAllFields;
40  ErrorHandler *mErrorHandler;
41  KConfig *mConfig;
42  KRES::Manager<Resource> *mManager;
43  QList<Resource*> mPendingLoadResources;
44  QList<Resource*> mPendingSaveResources;
45  Iterator end;
46  ConstIterator constEnd;
47 };
48 
49 struct AddressBook::Iterator::IteratorData
50 {
51  Resource::Iterator mIt;
52  QList<Resource*> mResources;
53  int mCurrRes;
54 };
55 
56 struct AddressBook::ConstIterator::ConstIteratorData
57 {
58  Resource::ConstIterator mIt;
59  QList<Resource*> mResources;
60  int mCurrRes;
61 };
62 
63 AddressBook::Iterator::Iterator()
64  : d( new IteratorData )
65 {
66 }
67 
68 AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
69  : d( new IteratorData )
70 {
71  d->mIt = i.d->mIt;
72  d->mResources = i.d->mResources;
73  d->mCurrRes = i.d->mCurrRes;
74 }
75 
76 AddressBook::Iterator &AddressBook::Iterator::operator=
77  ( const AddressBook::Iterator &i )
78 {
79  if ( this == &i ) {
80  return *this; // guard against self assignment
81  }
82 
83  d->mIt = i.d->mIt;
84  d->mResources = i.d->mResources;
85  d->mCurrRes = i.d->mCurrRes;
86 
87  return *this;
88 }
89 
90 AddressBook::Iterator::~Iterator()
91 {
92  delete d;
93 }
94 
95 const Addressee &AddressBook::Iterator::operator*() const
96 {
97  return *(d->mIt);
98 }
99 
100 Addressee &AddressBook::Iterator::operator*()
101 {
102  return *(d->mIt);
103 }
104 
105 Addressee *AddressBook::Iterator::operator->()
106 {
107  return &(*(d->mIt));
108 }
109 
110 AddressBook::Iterator &AddressBook::Iterator::operator++()
111 {
112  do {
113  bool jumped = false;
114  while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
115  // at end of addressee list of resource
116  if ( d->mCurrRes == d->mResources.count() - 1 ) {
117  return *this;
118  }
119 
120  d->mCurrRes++; // jump to next resource
121 
122  jumped = true;
123  d->mIt = ( d->mResources[ d->mCurrRes ] )->begin();
124  }
125 
126  if ( !jumped ) {
127  (d->mIt)++;
128  }
129 
130  } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
131 
132  return *this;
133 }
134 
135 AddressBook::Iterator &AddressBook::Iterator::operator++( int )
136 {
137  do {
138  bool jumped = false;
139  while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
140  // at end of addressee list of resource
141  if ( d->mCurrRes == d->mResources.count() - 1 ) {
142  return *this;
143  }
144 
145  d->mCurrRes++; // jump to next resource
146 
147  jumped = true;
148  d->mIt = ( d->mResources[ d->mCurrRes ] )->begin();
149  }
150 
151  if ( !jumped ) {
152  (d->mIt)++;
153  }
154 
155  } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
156 
157  return *this;
158 }
159 
160 AddressBook::Iterator &AddressBook::Iterator::operator--()
161 {
162  (d->mIt)--;
163 
164  return *this;
165 }
166 
167 AddressBook::Iterator &AddressBook::Iterator::operator--( int )
168 {
169  (d->mIt)--;
170 
171  return *this;
172 }
173 
174 bool AddressBook::Iterator::operator==( const Iterator &it ) const
175 {
176  return d->mIt == it.d->mIt;
177 }
178 
179 bool AddressBook::Iterator::operator!=( const Iterator &it ) const
180 {
181  return d->mIt != it.d->mIt;
182 }
183 
184 AddressBook::ConstIterator::ConstIterator()
185  : d( new ConstIteratorData )
186 {
187 }
188 
189 AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
190  : d( new ConstIteratorData )
191 {
192  d->mIt = i.d->mIt;
193  d->mResources = i.d->mResources;
194  d->mCurrRes = i.d->mCurrRes;
195 }
196 
197 #ifndef QT_STRICT_ITERATORS
198 AddressBook::ConstIterator::ConstIterator( const AddressBook::Iterator &i )
199  :d( new ConstIteratorData )
200 {
201  d->mIt = i.d->mIt;
202  d->mResources = i.d->mResources;
203  d->mCurrRes = i.d->mCurrRes;
204 }
205 #endif
206 
207 AddressBook::ConstIterator &AddressBook::ConstIterator::operator=
208  ( const AddressBook::ConstIterator &i )
209 {
210  if ( this == &i ) {
211  return *this; // guard for self assignment
212  }
213 
214  d->mIt = i.d->mIt;
215  d->mResources = i.d->mResources;
216  d->mCurrRes = i.d->mCurrRes;
217 
218  return *this;
219 }
220 
221 AddressBook::ConstIterator::~ConstIterator()
222 {
223  delete d;
224 }
225 
226 const Addressee &AddressBook::ConstIterator::operator*() const
227 {
228  return *(d->mIt);
229 }
230 
231 const Addressee *AddressBook::ConstIterator::operator->() const
232 {
233  return &(*(d->mIt));
234 }
235 
236 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
237 {
238  do {
239  bool jumped = false;
240  while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
241  // at end of addressee list of resource
242  if ( d->mCurrRes == d->mResources.count() - 1 ) {
243  return *this;
244  }
245 
246  d->mCurrRes++; // jump to next resource
247 
248  jumped = true;
249  d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
250  }
251 
252  if ( !jumped ) {
253  (d->mIt)++;
254  }
255 
256  } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
257 
258  return *this;
259 }
260 
261 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
262 {
263  do {
264  bool jumped = false;
265  while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
266  // at end of addressee list of resource
267  if ( d->mCurrRes == d->mResources.count() - 1 ) {
268  return *this;
269  }
270 
271  d->mCurrRes++; // jump to next resource
272 
273  jumped = true;
274  d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
275  }
276 
277  if ( !jumped ) {
278  (d->mIt)++;
279  }
280 
281  } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
282 
283  return *this;
284 }
285 
286 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
287 {
288  (d->mIt)--;
289  return *this;
290 }
291 
292 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
293 {
294  (d->mIt)--;
295  return *this;
296 }
297 
298 bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) const
299 {
300  return d->mIt == it.d->mIt;
301 }
302 
303 bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) const
304 {
305  return d->mIt != it.d->mIt;
306 }
307 
308 AddressBook::AddressBook()
309  : d( new Private )
310 {
311  d->mErrorHandler = 0;
312  d->mConfig = 0;
313  d->mManager = new KRES::Manager<Resource>( QLatin1String( "contact" ) );
314  d->end.d->mResources = QList<Resource*>();
315  d->end.d->mCurrRes = -1;
316  d->constEnd.d->mResources = QList<Resource*>();
317  d->constEnd.d->mCurrRes = -1;
318 }
319 
320 AddressBook::AddressBook( const QString &config )
321  : d( new Private )
322 {
323  d->mErrorHandler = 0;
324  if ( config.isEmpty() ) {
325  d->mConfig = 0;
326  } else {
327  d->mConfig = new KConfig( config );
328  }
329  d->mManager = new KRES::Manager<Resource>( QLatin1String( "contact" ) );
330  d->mManager->readConfig( d->mConfig );
331  d->end.d->mResources = QList<Resource*>();
332  d->end.d->mCurrRes = -1;
333  d->constEnd.d->mResources = QList<Resource*>();
334  d->constEnd.d->mCurrRes = -1;
335 }
336 
337 AddressBook::~AddressBook()
338 {
339  delete d->mManager;
340  d->mManager = 0;
341  delete d->mConfig;
342  d->mConfig = 0;
343  delete d->mErrorHandler;
344  d->mErrorHandler = 0;
345  delete d;
346 }
347 
348 bool AddressBook::load()
349 {
350  kDebug();
351 
352  clear();
353 
354  KRES::Manager<Resource>::ActiveIterator it;
355  bool ok = true;
356  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
357  if ( !(*it)->load() ) {
358  error( i18n( "Unable to load resource '%1'", (*it)->resourceName() ) );
359  ok = false;
360  }
361  }
362 
363  return ok;
364 }
365 
366 bool AddressBook::asyncLoad()
367 {
368  kDebug();
369 
370  clear();
371 
372  KRES::Manager<Resource>::ActiveIterator it;
373  bool ok = true;
374  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
375  d->mPendingLoadResources.append( *it );
376  if ( !(*it)->asyncLoad() ) {
377  error( i18n( "Unable to load resource '%1'", (*it)->resourceName() ) );
378  ok = false;
379  }
380  }
381 
382  return ok;
383 }
384 
385 bool AddressBook::save( Ticket *ticket )
386 {
387  kDebug();
388 
389  if ( ticket->resource() ) {
390  bool ok = ticket->resource()->save( ticket );
391  if ( ok ) {
392  ticket->resource()->releaseSaveTicket( ticket );
393  }
394  return ok;
395  }
396 
397  return false;
398 }
399 
400 bool AddressBook::asyncSave( Ticket *ticket )
401 {
402  kDebug();
403 
404  if ( ticket->resource() ) {
405  d->mPendingSaveResources.append( ticket->resource() );
406  bool ok = ticket->resource()->asyncSave( ticket );
407  if ( ok ) {
408  ticket->resource()->releaseSaveTicket( ticket );
409  }
410  return ok;
411  }
412 
413  return false;
414 }
415 
416 AddressBook::Iterator AddressBook::begin()
417 {
418  QList<Resource*> list;
419  KRES::Manager<Resource>::ActiveIterator resIt;
420  for ( resIt = d->mManager->activeBegin();
421  resIt != d->mManager->activeEnd(); ++resIt ) {
422  list.append( *resIt );
423  }
424 
425  if ( list.count() == 0 ) {
426  return end();
427  }
428 
429  Iterator it = Iterator();
430  it.d->mResources = list;
431  it.d->mCurrRes = 0;
432  it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->begin();
433 
434  while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->end() ) {
435  if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
436  return end();
437  }
438 
439  it.d->mCurrRes++;
440 
441  it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->begin();
442  }
443 
444  return it;
445 }
446 
447 AddressBook::ConstIterator AddressBook::begin() const
448 {
449  QList<Resource*> list;
450  KRES::Manager<Resource>::ActiveIterator resIt;
451  for ( resIt = d->mManager->activeBegin();
452  resIt != d->mManager->activeEnd(); ++resIt ) {
453  list.append( *resIt );
454  }
455 
456  if ( list.count() == 0 ) {
457  return end();
458  }
459 
460  ConstIterator it = ConstIterator();
461  it.d->mResources = list;
462  it.d->mCurrRes = 0;
463  it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
464 
465  while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->constEnd() ) {
466  if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
467  return end();
468  }
469 
470  it.d->mCurrRes++;
471 
472  it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
473  }
474 
475  return it;
476 }
477 
478 AddressBook::Iterator AddressBook::end()
479 {
480  KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
481 
482  if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
483  // no resource available
484  d->end.d->mIt = Resource::Iterator();
485  } else {
486  d->end.d->mIt = (*resIt)->end();
487  }
488 
489  return d->end;
490 }
491 
492 AddressBook::ConstIterator AddressBook::end() const
493 {
494  KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
495 
496  if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
497  // no resource available
498  d->constEnd.d->mIt = Resource::ConstIterator();
499  } else {
500  d->constEnd.d->mIt = (*resIt)->constEnd();
501  }
502 
503  return d->constEnd;
504 }
505 
506 void AddressBook::clear()
507 {
508  KRES::Manager<Resource>::ActiveIterator it;
509  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
510  (*it)->clear();
511  }
512 }
513 
514 Ticket *AddressBook::requestSaveTicket( Resource *resource )
515 {
516  kDebug();
517 
518  if ( !resource ) {
519  resource = standardResource();
520  }
521 
522  KRES::Manager<Resource>::ActiveIterator it;
523  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
524  if ( (*it) == resource ) {
525  if ( (*it)->readOnly() || !(*it)->isOpen() ) {
526  return 0;
527  } else {
528  return (*it)->requestSaveTicket();
529  }
530  }
531  }
532 
533  return 0;
534 }
535 
536 void AddressBook::releaseSaveTicket( Ticket *ticket )
537 {
538  if ( !ticket ) {
539  return;
540  }
541 
542  if ( ticket->resource() ) {
543  ticket->resource()->releaseSaveTicket( ticket );
544  }
545 }
546 
547 void AddressBook::insertAddressee( const Addressee &a )
548 {
549  Resource *resource = a.resource();
550  if ( resource == 0 ) {
551  resource = standardResource();
552  }
553 
554  Resource::Iterator it;
555  Addressee fAddr = resource->findByUid( a.uid() );
556 
557  Addressee addr( a );
558  if ( !fAddr.isEmpty() ) {
559  if ( fAddr != a ) {
560  addr.setRevision( QDateTime::currentDateTime() );
561  } else {
562  if ( fAddr.resource() == 0 ) {
563  fAddr.setResource( resource );
564  //NOTE: Should we have setChanged( true ) here?
565  resource->insertAddressee( fAddr );
566  }
567  return;
568  }
569  }
570 
571  addr.setResource( resource );
572  addr.setChanged( true );
573  resource->insertAddressee( addr );
574 }
575 
576 void AddressBook::removeAddressee( const Addressee &a )
577 {
578  if ( a.resource() ) {
579  a.resource()->removeAddressee( a );
580  }
581 }
582 
583 void AddressBook::removeAddressee( const Iterator &it )
584 {
585  if ( (*it).resource() ) {
586  (*it).resource()->removeAddressee( *it );
587  }
588 }
589 
590 AddressBook::Iterator AddressBook::find( const Addressee &a )
591 {
592  Iterator it;
593  for ( it = begin(); it != end(); ++it ) {
594  if ( a.uid() == (*it).uid() ) {
595  return it;
596  }
597  }
598 
599  return end();
600 }
601 
602 AddressBook::ConstIterator AddressBook::find( const Addressee &a ) const
603 {
604  ConstIterator it;
605  for ( it = begin(); it != end(); ++it ) {
606  if ( a.uid() == (*it).uid() ) {
607  return it;
608  }
609  }
610 
611  return end();
612 }
613 
614 Addressee AddressBook::findByUid( const QString &uid ) const
615 {
616  KRES::Manager<Resource>::ActiveIterator it;
617  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
618  Addressee addr = (*it)->findByUid( uid );
619  if ( !addr.isEmpty() ) {
620  return addr;
621  }
622  }
623 
624  return Addressee();
625 }
626 
627 Addressee::List AddressBook::allAddressees() const
628 {
629  Addressee::List list;
630 
631  ConstIterator it;
632  for ( it = begin(); it != end(); ++it ) {
633  list.append( *it );
634  }
635 
636  return list;
637 }
638 
639 Addressee::List AddressBook::findByName( const QString &name ) const
640 {
641  Addressee::List results;
642 
643  KRES::Manager<Resource>::ActiveIterator it;
644  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
645  results += (*it)->findByName( name );
646  }
647 
648  return results;
649 }
650 
651 Addressee::List AddressBook::findByEmail( const QString &email ) const
652 {
653  Addressee::List results;
654 
655  KRES::Manager<Resource>::ActiveIterator it;
656  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
657  results += (*it)->findByEmail( email );
658  }
659 
660  return results;
661 }
662 
663 Addressee::List AddressBook::findByCategory( const QString &category ) const
664 {
665  Addressee::List results;
666 
667  KRES::Manager<Resource>::ActiveIterator it;
668  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
669  results += (*it)->findByCategory( category );
670  }
671 
672  return results;
673 }
674 
675 DistributionList *AddressBook::createDistributionList( const QString &name, Resource *resource )
676 {
677  if ( resource == 0 ) {
678  resource = standardResource();
679  }
680 
681  return new DistributionList( resource, name );
682 }
683 
684 void AddressBook::removeDistributionList( DistributionList *list )
685 {
686  if ( !list || !list->resource() ) {
687  return;
688  }
689 
690  list->resource()->removeDistributionList( list );
691 }
692 
693 DistributionList *AddressBook::findDistributionListByIdentifier( const QString &identifier )
694 {
695  KRES::Manager<Resource>::ActiveIterator it;
696  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
697  DistributionList *list = (*it)->findDistributionListByIdentifier( identifier );
698  if ( list ) {
699  return list;
700  }
701  }
702 
703  return 0;
704 }
705 
706 DistributionList *AddressBook::findDistributionListByName( const QString &name,
707  Qt::CaseSensitivity caseSensitivity )
708 {
709  KRES::Manager<Resource>::ActiveIterator it;
710  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
711  DistributionList *list = (*it)->findDistributionListByName( name, caseSensitivity );
712  if ( list ) {
713  return list;
714  }
715  }
716 
717  return 0;
718 }
719 
720 QList<DistributionList*> AddressBook::allDistributionLists()
721 {
722  QList<DistributionList*> results;
723 
724  KRES::Manager<Resource>::ActiveIterator it;
725  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
726  results += (*it)->allDistributionLists();
727  }
728 
729  return results;
730 }
731 
732 QStringList AddressBook::allDistributionListNames() const
733 {
734  QStringList results;
735 
736  KRES::Manager<Resource>::ActiveIterator it;
737  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
738  results += (*it)->allDistributionListNames();
739  }
740 
741  return results;
742 }
743 
744 void AddressBook::dump() const
745 {
746  kDebug() << "--- begin ---";
747 
748  ConstIterator it;
749  for ( it = begin(); it != end(); ++it ) {
750  kDebug() << (*it).toString();
751  }
752 
753  kDebug() << "--- end ---";
754 }
755 
756 QString AddressBook::identifier() const
757 {
758  QStringList identifier;
759 
760  KRES::Manager<Resource>::ActiveIterator it;
761  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
762  if ( !(*it)->identifier().isEmpty() ) {
763  identifier.append( (*it)->identifier() );
764  }
765  }
766 
767  return identifier.join( QLatin1String( ":" ) );
768 }
769 
770 Field::List AddressBook::fields( int category ) const
771 {
772  if ( d->mAllFields.isEmpty() ) {
773  d->mAllFields = Field::allFields();
774  }
775 
776  if ( category == Field::All ) {
777  return d->mAllFields;
778  }
779 
780  Field::List result;
781  Field::List::ConstIterator it;
782  for ( it = d->mAllFields.constBegin(); it != d->mAllFields.constEnd(); ++it ) {
783  if ( (*it)->category() & category ) {
784  result.append( *it );
785  }
786  }
787 
788  return result;
789 }
790 
791 bool AddressBook::addCustomField( const QString &label,
792  int category,
793  const QString &key,
794  const QString &app ) const
795 {
796  if ( d->mAllFields.isEmpty() ) {
797  d->mAllFields = Field::allFields();
798  }
799 
800  QString a = app.isNull() ? KGlobal::mainComponent().componentName() : app;
801  QString k = key.isNull() ? label : key;
802 
803  Field *field = Field::createCustomField( label, category, k, a );
804 
805  if ( !field ) {
806  return false;
807  }
808 
809  d->mAllFields.append( field );
810 
811  return true;
812 }
813 
814 QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
815 {
816  if ( !ab.d ) {
817  return s;
818  }
819 
820  return s;// << ab.d->mAddressees;
821 }
822 
823 QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
824 {
825  if ( !ab.d ) {
826  return s;
827  }
828 
829  return s;// s >> ab.d->mAddressees;
830 }
831 
832 bool AddressBook::addResource( Resource *resource )
833 {
834  if ( !resource->open() ) {
835  kDebug() << "can't add resource";
836  return false;
837  }
838 
839  d->mManager->add( resource );
840  resource->setAddressBook( this );
841 
842  connect( resource, SIGNAL(loadingFinished(Resource*)),
843  this, SLOT(resourceLoadingFinished(Resource*)) );
844  connect( resource, SIGNAL(savingFinished(Resource*)),
845  this, SLOT(resourceSavingFinished(Resource*)) );
846 
847  connect( resource, SIGNAL(loadingError(Resource*,QString)),
848  this, SLOT(resourceLoadingError(Resource*,QString)) );
849  connect( resource, SIGNAL(savingError(Resource*,QString)),
850  this, SLOT(resourceSavingError(Resource*,QString)) );
851 
852  return true;
853 }
854 
855 bool AddressBook::removeResource( Resource *resource )
856 {
857  resource->close();
858 
859  if ( resource == standardResource() ) {
860  d->mManager->setStandardResource( 0 );
861  }
862 
863  resource->setAddressBook( 0 );
864 
865  disconnect( resource, SIGNAL(loadingFinished(Resource*)),
866  this, SLOT(resourceLoadingFinished(Resource*)) );
867  disconnect( resource, SIGNAL(savingFinished(Resource*)),
868  this, SLOT(resourceSavingFinished(Resource*)) );
869 
870  disconnect( resource, SIGNAL(loadingError(Resource*,QString)),
871  this, SLOT(resourceLoadingError(Resource*,QString)) );
872  disconnect( resource, SIGNAL(savingError(Resource*,QString)),
873  this, SLOT(resourceLoadingError(Resource*,QString)) );
874 
875  d->mManager->remove( resource );
876 
877  return true;
878 }
879 
880 QList<Resource*> AddressBook::resources() const
881 {
882  QList<Resource*> list;
883 
884  KRES::Manager<Resource>::ActiveIterator it;
885  for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
886  if ( d->mManager->standardResource() == (*it) ) {
887  list.prepend( *it );
888  } else {
889  list.append( *it );
890  }
891  }
892 
893  return list;
894 }
895 
896 void AddressBook::setErrorHandler( ErrorHandler *handler )
897 {
898  delete d->mErrorHandler;
899  d->mErrorHandler = handler;
900 }
901 
902 void AddressBook::error( const QString &msg )
903 {
904  if ( !d->mErrorHandler ) {
905  // create default error handler
906  d->mErrorHandler = new ConsoleErrorHandler();
907  }
908 
909  if ( d->mErrorHandler ) {
910  d->mErrorHandler->error( msg );
911  } else {
912  kError() << "no error handler defined";
913  }
914 }
915 
916 void AddressBook::setStandardResource( Resource *resource )
917 {
918  d->mManager->setStandardResource( resource );
919 }
920 
921 Resource *AddressBook::standardResource()
922 {
923  return d->mManager->standardResource();
924 }
925 
926 KRES::Manager<Resource> *AddressBook::resourceManager()
927 {
928  return d->mManager;
929 }
930 
931 bool AddressBook::loadingHasFinished() const
932 {
933  return d->mPendingLoadResources.isEmpty();
934 }
935 
936 void AddressBook::resourceLoadingFinished( Resource *resource )
937 {
938  d->mPendingLoadResources.removeAll( resource );
939  emit loadingFinished( resource );
940 
941  if ( d->mPendingLoadResources.count() == 0 ) {
942  emit addressBookChanged( this );
943  }
944 }
945 
946 void AddressBook::resourceSavingFinished( Resource *resource )
947 {
948  d->mPendingSaveResources.removeAll( resource );
949 
950  emit savingFinished( resource );
951 }
952 
953 void AddressBook::resourceLoadingError( Resource *resource,
954  const QString &errMsg )
955 {
956  error( errMsg );
957 
958  d->mPendingLoadResources.removeAll( resource );
959  if ( d->mPendingLoadResources.count() == 0 ) {
960  emit addressBookChanged( this );
961  }
962 }
963 
964 void AddressBook::resourceSavingError( Resource *resource,
965  const QString &errMsg )
966 {
967  error( errMsg );
968 
969  d->mPendingSaveResources.removeAll( resource );
970 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:59:32 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal