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