00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "addressbook.h"
00022 #include "distributionlist.h"
00023 #include "errorhandler.h"
00024 #include "resource.h"
00025
00026 #include <kdebug.h>
00027 #include <kglobal.h>
00028 #include <kcomponentdata.h>
00029 #include <klocale.h>
00030 #include <kstandarddirs.h>
00031
00032 #include "addressbook.moc"
00033
00034 using namespace KABC;
00035
00036 class AddressBook::Private
00037 {
00038 public:
00039 Field::List mAllFields;
00040 ErrorHandler *mErrorHandler;
00041 KConfig *mConfig;
00042 KRES::Manager<Resource> *mManager;
00043 QList<Resource*> mPendingLoadResources;
00044 QList<Resource*> mPendingSaveResources;
00045 Iterator end;
00046 ConstIterator constEnd;
00047 };
00048
00049 struct AddressBook::Iterator::IteratorData
00050 {
00051 Resource::Iterator mIt;
00052 QList<Resource*> mResources;
00053 int mCurrRes;
00054 };
00055
00056 struct AddressBook::ConstIterator::ConstIteratorData
00057 {
00058 Resource::ConstIterator mIt;
00059 QList<Resource*> mResources;
00060 int mCurrRes;
00061 };
00062
00063 AddressBook::Iterator::Iterator()
00064 : d( new IteratorData )
00065 {
00066 }
00067
00068 AddressBook::Iterator::Iterator( const AddressBook::Iterator &i )
00069 : d( new IteratorData )
00070 {
00071 d->mIt = i.d->mIt;
00072 d->mResources = i.d->mResources;
00073 d->mCurrRes = i.d->mCurrRes;
00074 }
00075
00076 AddressBook::Iterator &AddressBook::Iterator::operator=
00077 ( const AddressBook::Iterator &i )
00078 {
00079 if ( this == &i ) {
00080 return *this;
00081 }
00082
00083 d->mIt = i.d->mIt;
00084 d->mResources = i.d->mResources;
00085 d->mCurrRes = i.d->mCurrRes;
00086
00087 return *this;
00088 }
00089
00090 AddressBook::Iterator::~Iterator()
00091 {
00092 delete d;
00093 }
00094
00095 const Addressee &AddressBook::Iterator::operator*() const
00096 {
00097 return *(d->mIt);
00098 }
00099
00100 Addressee &AddressBook::Iterator::operator*()
00101 {
00102 return *(d->mIt);
00103 }
00104
00105 Addressee *AddressBook::Iterator::operator->()
00106 {
00107 return &(*(d->mIt));
00108 }
00109
00110 AddressBook::Iterator &AddressBook::Iterator::operator++()
00111 {
00112 do {
00113 bool jumped = false;
00114 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
00115
00116 if ( d->mCurrRes == d->mResources.count() - 1 ) {
00117 return *this;
00118 }
00119
00120 d->mCurrRes++;
00121
00122 jumped = true;
00123 d->mIt = ( d->mResources[ d->mCurrRes ] )->begin();
00124 }
00125
00126 if ( !jumped ) {
00127 (d->mIt)++;
00128 }
00129
00130 } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
00131
00132 return *this;
00133 }
00134
00135 AddressBook::Iterator &AddressBook::Iterator::operator++( int )
00136 {
00137 do {
00138 bool jumped = false;
00139 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
00140
00141 if ( d->mCurrRes == d->mResources.count() - 1 ) {
00142 return *this;
00143 }
00144
00145 d->mCurrRes++;
00146
00147 jumped = true;
00148 d->mIt = ( d->mResources[ d->mCurrRes ] )->begin();
00149 }
00150
00151 if ( !jumped ) {
00152 (d->mIt)++;
00153 }
00154
00155 } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
00156
00157 return *this;
00158 }
00159
00160 AddressBook::Iterator &AddressBook::Iterator::operator--()
00161 {
00162 (d->mIt)--;
00163
00164 return *this;
00165 }
00166
00167 AddressBook::Iterator &AddressBook::Iterator::operator--( int )
00168 {
00169 (d->mIt)--;
00170
00171 return *this;
00172 }
00173
00174 bool AddressBook::Iterator::operator==( const Iterator &it ) const
00175 {
00176 return d->mIt == it.d->mIt;
00177 }
00178
00179 bool AddressBook::Iterator::operator!=( const Iterator &it ) const
00180 {
00181 return d->mIt != it.d->mIt;
00182 }
00183
00184 AddressBook::ConstIterator::ConstIterator()
00185 : d( new ConstIteratorData )
00186 {
00187 }
00188
00189 AddressBook::ConstIterator::ConstIterator( const AddressBook::ConstIterator &i )
00190 : d( new ConstIteratorData )
00191 {
00192 d->mIt = i.d->mIt;
00193 d->mResources = i.d->mResources;
00194 d->mCurrRes = i.d->mCurrRes;
00195 }
00196
00197 #ifndef QT_STRICT_ITERATORS
00198 AddressBook::ConstIterator::ConstIterator( const AddressBook::Iterator &i )
00199 :d( new ConstIteratorData )
00200 {
00201 d->mIt = i.d->mIt;
00202 d->mResources = i.d->mResources;
00203 d->mCurrRes = i.d->mCurrRes;
00204 }
00205 #endif
00206
00207 AddressBook::ConstIterator &AddressBook::ConstIterator::operator=
00208 ( const AddressBook::ConstIterator &i )
00209 {
00210 if ( this == &i ) {
00211 return *this;
00212 }
00213
00214 d->mIt = i.d->mIt;
00215 d->mResources = i.d->mResources;
00216 d->mCurrRes = i.d->mCurrRes;
00217
00218 return *this;
00219 }
00220
00221 AddressBook::ConstIterator::~ConstIterator()
00222 {
00223 delete d;
00224 }
00225
00226 const Addressee &AddressBook::ConstIterator::operator*() const
00227 {
00228 return *(d->mIt);
00229 }
00230
00231 const Addressee *AddressBook::ConstIterator::operator->() const
00232 {
00233 return &(*(d->mIt));
00234 }
00235
00236 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++()
00237 {
00238 do {
00239 bool jumped = false;
00240 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
00241
00242 if ( d->mCurrRes == d->mResources.count() - 1 ) {
00243 return *this;
00244 }
00245
00246 d->mCurrRes++;
00247
00248 jumped = true;
00249 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
00250 }
00251
00252 if ( !jumped ) {
00253 (d->mIt)++;
00254 }
00255
00256 } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
00257
00258 return *this;
00259 }
00260
00261 AddressBook::ConstIterator &AddressBook::ConstIterator::operator++(int)
00262 {
00263 do {
00264 bool jumped = false;
00265 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
00266
00267 if ( d->mCurrRes == d->mResources.count() - 1 ) {
00268 return *this;
00269 }
00270
00271 d->mCurrRes++;
00272
00273 jumped = true;
00274 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
00275 }
00276
00277 if ( !jumped ) {
00278 (d->mIt)++;
00279 }
00280
00281 } while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
00282
00283 return *this;
00284 }
00285
00286 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--()
00287 {
00288 (d->mIt)--;
00289 return *this;
00290 }
00291
00292 AddressBook::ConstIterator &AddressBook::ConstIterator::operator--(int)
00293 {
00294 (d->mIt)--;
00295 return *this;
00296 }
00297
00298 bool AddressBook::ConstIterator::operator==( const ConstIterator &it ) const
00299 {
00300 return d->mIt == it.d->mIt;
00301 }
00302
00303 bool AddressBook::ConstIterator::operator!=( const ConstIterator &it ) const
00304 {
00305 return d->mIt != it.d->mIt;
00306 }
00307
00308 AddressBook::AddressBook()
00309 : d( new Private )
00310 {
00311 d->mErrorHandler = 0;
00312 d->mConfig = 0;
00313 d->mManager = new KRES::Manager<Resource>( QLatin1String( "contact" ) );
00314 d->end.d->mResources = QList<Resource*>();
00315 d->end.d->mCurrRes = -1;
00316 d->constEnd.d->mResources = QList<Resource*>();
00317 d->constEnd.d->mCurrRes = -1;
00318 }
00319
00320 AddressBook::AddressBook( const QString &config )
00321 : d( new Private )
00322 {
00323 d->mErrorHandler = 0;
00324 if ( config.isEmpty() ) {
00325 d->mConfig = 0;
00326 } else {
00327 d->mConfig = new KConfig( config );
00328 }
00329 d->mManager = new KRES::Manager<Resource>( QLatin1String( "contact" ) );
00330 d->mManager->readConfig( d->mConfig );
00331 d->end.d->mResources = QList<Resource*>();
00332 d->end.d->mCurrRes = -1;
00333 d->constEnd.d->mResources = QList<Resource*>();
00334 d->constEnd.d->mCurrRes = -1;
00335 }
00336
00337 AddressBook::~AddressBook()
00338 {
00339 delete d->mManager;
00340 d->mManager = 0;
00341 delete d->mConfig;
00342 d->mConfig = 0;
00343 delete d->mErrorHandler;
00344 d->mErrorHandler = 0;
00345 delete d;
00346 }
00347
00348 bool AddressBook::load()
00349 {
00350 kDebug();
00351
00352 clear();
00353
00354 KRES::Manager<Resource>::ActiveIterator it;
00355 bool ok = true;
00356 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00357 if ( !(*it)->load() ) {
00358 error( i18n( "Unable to load resource '%1'", (*it)->resourceName() ) );
00359 ok = false;
00360 }
00361 }
00362
00363 return ok;
00364 }
00365
00366 bool AddressBook::asyncLoad()
00367 {
00368 kDebug();
00369
00370 clear();
00371
00372 KRES::Manager<Resource>::ActiveIterator it;
00373 bool ok = true;
00374 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00375 d->mPendingLoadResources.append( *it );
00376 if ( !(*it)->asyncLoad() ) {
00377 error( i18n( "Unable to load resource '%1'", (*it)->resourceName() ) );
00378 ok = false;
00379 }
00380 }
00381
00382 return ok;
00383 }
00384
00385 bool AddressBook::save( Ticket *ticket )
00386 {
00387 kDebug();
00388
00389 if ( ticket->resource() ) {
00390 bool ok = ticket->resource()->save( ticket );
00391 if ( ok ) {
00392 ticket->resource()->releaseSaveTicket( ticket );
00393 }
00394 return ok;
00395 }
00396
00397 return false;
00398 }
00399
00400 bool AddressBook::asyncSave( Ticket *ticket )
00401 {
00402 kDebug();
00403
00404 if ( ticket->resource() ) {
00405 d->mPendingSaveResources.append( ticket->resource() );
00406 bool ok = ticket->resource()->asyncSave( ticket );
00407 if ( ok ) {
00408 ticket->resource()->releaseSaveTicket( ticket );
00409 }
00410 return ok;
00411 }
00412
00413 return false;
00414 }
00415
00416 AddressBook::Iterator AddressBook::begin()
00417 {
00418 QList<Resource*> list;
00419 KRES::Manager<Resource>::ActiveIterator resIt;
00420 for ( resIt = d->mManager->activeBegin();
00421 resIt != d->mManager->activeEnd(); ++resIt ) {
00422 list.append( *resIt );
00423 }
00424
00425 if ( list.count() == 0 ) {
00426 return end();
00427 }
00428
00429 Iterator it = Iterator();
00430 it.d->mResources = list;
00431 it.d->mCurrRes = 0;
00432 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->begin();
00433
00434 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->end() ) {
00435 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
00436 return end();
00437 }
00438
00439 it.d->mCurrRes++;
00440
00441 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->begin();
00442 }
00443
00444 return it;
00445 }
00446
00447 AddressBook::ConstIterator AddressBook::begin() const
00448 {
00449 QList<Resource*> list;
00450 KRES::Manager<Resource>::ActiveIterator resIt;
00451 for ( resIt = d->mManager->activeBegin();
00452 resIt != d->mManager->activeEnd(); ++resIt ) {
00453 list.append( *resIt );
00454 }
00455
00456 if ( list.count() == 0 ) {
00457 return end();
00458 }
00459
00460 ConstIterator it = ConstIterator();
00461 it.d->mResources = list;
00462 it.d->mCurrRes = 0;
00463 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
00464
00465 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->constEnd() ) {
00466 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
00467 return end();
00468 }
00469
00470 it.d->mCurrRes++;
00471
00472 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
00473 }
00474
00475 return it;
00476 }
00477
00478 AddressBook::Iterator AddressBook::end()
00479 {
00480 KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
00481
00482 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
00483
00484 d->end.d->mIt = Resource::Iterator();
00485 } else {
00486 d->end.d->mIt = (*resIt)->end();
00487 }
00488
00489 return d->end;
00490 }
00491
00492 AddressBook::ConstIterator AddressBook::end() const
00493 {
00494 KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
00495
00496 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
00497
00498 d->constEnd.d->mIt = Resource::ConstIterator();
00499 } else {
00500 d->constEnd.d->mIt = (*resIt)->constEnd();
00501 }
00502
00503 return d->constEnd;
00504 }
00505
00506 void AddressBook::clear()
00507 {
00508 KRES::Manager<Resource>::ActiveIterator it;
00509 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00510 (*it)->clear();
00511 }
00512 }
00513
00514 Ticket *AddressBook::requestSaveTicket( Resource *resource )
00515 {
00516 kDebug();
00517
00518 if ( !resource ) {
00519 resource = standardResource();
00520 }
00521
00522 KRES::Manager<Resource>::ActiveIterator it;
00523 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00524 if ( (*it) == resource ) {
00525 if ( (*it)->readOnly() || !(*it)->isOpen() ) {
00526 return 0;
00527 } else {
00528 return (*it)->requestSaveTicket();
00529 }
00530 }
00531 }
00532
00533 return 0;
00534 }
00535
00536 void AddressBook::releaseSaveTicket( Ticket *ticket )
00537 {
00538 if ( !ticket ) {
00539 return;
00540 }
00541
00542 if ( ticket->resource() ) {
00543 ticket->resource()->releaseSaveTicket( ticket );
00544 }
00545 }
00546
00547 void AddressBook::insertAddressee( const Addressee &a )
00548 {
00549 Resource *resource = a.resource();
00550 if ( resource == 0 ) {
00551 resource = standardResource();
00552 }
00553
00554 Resource::Iterator it;
00555 Addressee fAddr = resource->findByUid( a.uid() );
00556
00557 Addressee addr( a );
00558 if ( !fAddr.isEmpty() ) {
00559 if ( fAddr != a ) {
00560 addr.setRevision( QDateTime::currentDateTime() );
00561 } else {
00562 if ( fAddr.resource() == 0 ) {
00563 fAddr.setResource( resource );
00564
00565 resource->insertAddressee( fAddr );
00566 }
00567 return;
00568 }
00569 }
00570
00571 addr.setResource( resource );
00572 addr.setChanged( true );
00573 resource->insertAddressee( addr );
00574 }
00575
00576 void AddressBook::removeAddressee( const Addressee &a )
00577 {
00578 if ( a.resource() ) {
00579 a.resource()->removeAddressee( a );
00580 }
00581 }
00582
00583 void AddressBook::removeAddressee( const Iterator &it )
00584 {
00585 if ( (*it).resource() ) {
00586 (*it).resource()->removeAddressee( *it );
00587 }
00588 }
00589
00590 AddressBook::Iterator AddressBook::find( const Addressee &a )
00591 {
00592 Iterator it;
00593 for ( it = begin(); it != end(); ++it ) {
00594 if ( a.uid() == (*it).uid() ) {
00595 return it;
00596 }
00597 }
00598
00599 return end();
00600 }
00601
00602 AddressBook::ConstIterator AddressBook::find( const Addressee &a ) const
00603 {
00604 ConstIterator it;
00605 for ( it = begin(); it != end(); ++it ) {
00606 if ( a.uid() == (*it).uid() ) {
00607 return it;
00608 }
00609 }
00610
00611 return end();
00612 }
00613
00614 Addressee AddressBook::findByUid( const QString &uid ) const
00615 {
00616 KRES::Manager<Resource>::ActiveIterator it;
00617 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00618 Addressee addr = (*it)->findByUid( uid );
00619 if ( !addr.isEmpty() ) {
00620 return addr;
00621 }
00622 }
00623
00624 return Addressee();
00625 }
00626
00627 Addressee::List AddressBook::allAddressees() const
00628 {
00629 Addressee::List list;
00630
00631 ConstIterator it;
00632 for ( it = begin(); it != end(); ++it ) {
00633 list.append( *it );
00634 }
00635
00636 return list;
00637 }
00638
00639 Addressee::List AddressBook::findByName( const QString &name ) const
00640 {
00641 Addressee::List results;
00642
00643 KRES::Manager<Resource>::ActiveIterator it;
00644 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00645 results += (*it)->findByName( name );
00646 }
00647
00648 return results;
00649 }
00650
00651 Addressee::List AddressBook::findByEmail( const QString &email ) const
00652 {
00653 Addressee::List results;
00654
00655 KRES::Manager<Resource>::ActiveIterator it;
00656 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00657 results += (*it)->findByEmail( email );
00658 }
00659
00660 return results;
00661 }
00662
00663 Addressee::List AddressBook::findByCategory( const QString &category ) const
00664 {
00665 Addressee::List results;
00666
00667 KRES::Manager<Resource>::ActiveIterator it;
00668 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00669 results += (*it)->findByCategory( category );
00670 }
00671
00672 return results;
00673 }
00674
00675 DistributionList *AddressBook::createDistributionList( const QString &name, Resource *resource )
00676 {
00677 if ( resource == 0 ) {
00678 resource = standardResource();
00679 }
00680
00681 return new DistributionList( resource, name );
00682 }
00683
00684 void AddressBook::removeDistributionList( DistributionList *list )
00685 {
00686 if ( !list || !list->resource() ) {
00687 return;
00688 }
00689
00690 list->resource()->removeDistributionList( list );
00691 }
00692
00693 DistributionList *AddressBook::findDistributionListByIdentifier( const QString &identifier )
00694 {
00695 KRES::Manager<Resource>::ActiveIterator it;
00696 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00697 DistributionList *list = (*it)->findDistributionListByIdentifier( identifier );
00698 if ( list ) {
00699 return list;
00700 }
00701 }
00702
00703 return 0;
00704 }
00705
00706 DistributionList *AddressBook::findDistributionListByName( const QString &name,
00707 Qt::CaseSensitivity caseSensitivity )
00708 {
00709 KRES::Manager<Resource>::ActiveIterator it;
00710 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00711 DistributionList *list = (*it)->findDistributionListByName( name, caseSensitivity );
00712 if ( list ) {
00713 return list;
00714 }
00715 }
00716
00717 return 0;
00718 }
00719
00720 QList<DistributionList*> AddressBook::allDistributionLists()
00721 {
00722 QList<DistributionList*> results;
00723
00724 KRES::Manager<Resource>::ActiveIterator it;
00725 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00726 results += (*it)->allDistributionLists();
00727 }
00728
00729 return results;
00730 }
00731
00732 QStringList AddressBook::allDistributionListNames() const
00733 {
00734 QStringList results;
00735
00736 KRES::Manager<Resource>::ActiveIterator it;
00737 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00738 results += (*it)->allDistributionListNames();
00739 }
00740
00741 return results;
00742 }
00743
00744 void AddressBook::dump() const
00745 {
00746 kDebug() << "--- begin ---";
00747
00748 ConstIterator it;
00749 for ( it = begin(); it != end(); ++it ) {
00750 kDebug() << (*it).toString();
00751 }
00752
00753 kDebug() << "--- end ---";
00754 }
00755
00756 QString AddressBook::identifier() const
00757 {
00758 QStringList identifier;
00759
00760 KRES::Manager<Resource>::ActiveIterator it;
00761 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00762 if ( !(*it)->identifier().isEmpty() ) {
00763 identifier.append( (*it)->identifier() );
00764 }
00765 }
00766
00767 return identifier.join( QLatin1String( ":" ) );
00768 }
00769
00770 Field::List AddressBook::fields( int category ) const
00771 {
00772 if ( d->mAllFields.isEmpty() ) {
00773 d->mAllFields = Field::allFields();
00774 }
00775
00776 if ( category == Field::All ) {
00777 return d->mAllFields;
00778 }
00779
00780 Field::List result;
00781 Field::List::ConstIterator it;
00782 for ( it = d->mAllFields.constBegin(); it != d->mAllFields.constEnd(); ++it ) {
00783 if ( (*it)->category() & category ) {
00784 result.append( *it );
00785 }
00786 }
00787
00788 return result;
00789 }
00790
00791 bool AddressBook::addCustomField( const QString &label,
00792 int category,
00793 const QString &key,
00794 const QString &app ) const
00795 {
00796 if ( d->mAllFields.isEmpty() ) {
00797 d->mAllFields = Field::allFields();
00798 }
00799
00800 QString a = app.isNull() ? KGlobal::mainComponent().componentName() : app;
00801 QString k = key.isNull() ? label : key;
00802
00803 Field *field = Field::createCustomField( label, category, k, a );
00804
00805 if ( !field ) {
00806 return false;
00807 }
00808
00809 d->mAllFields.append( field );
00810
00811 return true;
00812 }
00813
00814 QDataStream &KABC::operator<<( QDataStream &s, const AddressBook &ab )
00815 {
00816 if ( !ab.d ) {
00817 return s;
00818 }
00819
00820 return s;
00821 }
00822
00823 QDataStream &KABC::operator>>( QDataStream &s, AddressBook &ab )
00824 {
00825 if ( !ab.d ) {
00826 return s;
00827 }
00828
00829 return s;
00830 }
00831
00832 bool AddressBook::addResource( Resource *resource )
00833 {
00834 if ( !resource->open() ) {
00835 kDebug() << "can't add resource";
00836 return false;
00837 }
00838
00839 d->mManager->add( resource );
00840 resource->setAddressBook( this );
00841
00842 connect( resource, SIGNAL( loadingFinished( Resource* ) ),
00843 this, SLOT( resourceLoadingFinished( Resource* ) ) );
00844 connect( resource, SIGNAL( savingFinished( Resource* ) ),
00845 this, SLOT( resourceSavingFinished( Resource* ) ) );
00846
00847 connect( resource, SIGNAL( loadingError( Resource*, const QString& ) ),
00848 this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00849 connect( resource, SIGNAL( savingError( Resource*, const QString& ) ),
00850 this, SLOT( resourceSavingError( Resource*, const QString& ) ) );
00851
00852 return true;
00853 }
00854
00855 bool AddressBook::removeResource( Resource *resource )
00856 {
00857 resource->close();
00858
00859 if ( resource == standardResource() ) {
00860 d->mManager->setStandardResource( 0 );
00861 }
00862
00863 resource->setAddressBook( 0 );
00864
00865 disconnect( resource, SIGNAL( loadingFinished( Resource* ) ),
00866 this, SLOT( resourceLoadingFinished( Resource* ) ) );
00867 disconnect( resource, SIGNAL( savingFinished( Resource* ) ),
00868 this, SLOT( resourceSavingFinished( Resource* ) ) );
00869
00870 disconnect( resource, SIGNAL( loadingError( Resource*, const QString& ) ),
00871 this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00872 disconnect( resource, SIGNAL( savingError( Resource*, const QString& ) ),
00873 this, SLOT( resourceLoadingError( Resource*, const QString& ) ) );
00874
00875 d->mManager->remove( resource );
00876
00877 return true;
00878 }
00879
00880 QList<Resource*> AddressBook::resources() const
00881 {
00882 QList<Resource*> list;
00883
00884 KRES::Manager<Resource>::ActiveIterator it;
00885 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
00886 if ( d->mManager->standardResource() == (*it) ) {
00887 list.prepend( *it );
00888 } else {
00889 list.append( *it );
00890 }
00891 }
00892
00893 return list;
00894 }
00895
00896 void AddressBook::setErrorHandler( ErrorHandler *handler )
00897 {
00898 delete d->mErrorHandler;
00899 d->mErrorHandler = handler;
00900 }
00901
00902 void AddressBook::error( const QString &msg )
00903 {
00904 if ( !d->mErrorHandler ) {
00905
00906 d->mErrorHandler = new ConsoleErrorHandler();
00907 }
00908
00909 if ( d->mErrorHandler ) {
00910 d->mErrorHandler->error( msg );
00911 } else {
00912 kError() << "no error handler defined";
00913 }
00914 }
00915
00916 void AddressBook::setStandardResource( Resource *resource )
00917 {
00918 d->mManager->setStandardResource( resource );
00919 }
00920
00921 Resource *AddressBook::standardResource()
00922 {
00923 return d->mManager->standardResource();
00924 }
00925
00926 KRES::Manager<Resource> *AddressBook::resourceManager()
00927 {
00928 return d->mManager;
00929 }
00930
00931 bool AddressBook::loadingHasFinished() const
00932 {
00933 return d->mPendingLoadResources.isEmpty();
00934 }
00935
00936 void AddressBook::resourceLoadingFinished( Resource *resource )
00937 {
00938 d->mPendingLoadResources.removeAll( resource );
00939 emit loadingFinished( resource );
00940
00941 if ( d->mPendingLoadResources.count() == 0 ) {
00942 emit addressBookChanged( this );
00943 }
00944 }
00945
00946 void AddressBook::resourceSavingFinished( Resource *resource )
00947 {
00948 d->mPendingSaveResources.removeAll( resource );
00949
00950 emit savingFinished( resource );
00951 }
00952
00953 void AddressBook::resourceLoadingError( Resource *resource,
00954 const QString &errMsg )
00955 {
00956 error( errMsg );
00957
00958 d->mPendingLoadResources.removeAll( resource );
00959 if ( d->mPendingLoadResources.count() == 0 ) {
00960 emit addressBookChanged( this );
00961 }
00962 }
00963
00964 void AddressBook::resourceSavingError( Resource *resource,
00965 const QString &errMsg )
00966 {
00967 error( errMsg );
00968
00969 d->mPendingSaveResources.removeAll( resource );
00970 }