25 #include <kapplication.h>
28 #include <kinstance.h>
30 #include <kstandarddirs.h>
32 #include "errorhandler.h"
35 #include "addressbook.h"
36 #include "addressbook.moc"
40 struct AddressBook::AddressBookData
42 Field::List mAllFields;
45 KRES::Manager<Resource> *mManager;
46 TQPtrList<Resource> mPendingLoadResources;
47 TQPtrList<Resource> mPendingSaveResources;
51 struct AddressBook::Iterator::IteratorData
54 TQValueList<Resource*> mResources;
58 struct AddressBook::ConstIterator::ConstIteratorData
61 TQValueList<Resource*> mResources;
65 AddressBook::Iterator::Iterator()
66 : d( new IteratorData )
71 : d( new IteratorData )
74 d->mResources = i.d->mResources;
75 d->mCurrRes = i.d->mCurrRes;
86 d->mResources = i.d->mResources;
87 d->mCurrRes = i.d->mCurrRes;
92 AddressBook::Iterator::~Iterator()
98 const Addressee &AddressBook::Iterator::operator*()
const
103 Addressee &AddressBook::Iterator::operator*()
108 Addressee *AddressBook::Iterator::operator->()
117 while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() ) {
118 if ( (uint)d->mCurrRes == d->mResources.count() - 1 ) {
125 d->mIt = (d->mResources[ d->mCurrRes ])->
begin();
131 }
while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() );
140 while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() ) {
141 if ( (uint)d->mCurrRes == d->mResources.count() - 1 ) {
148 d->mIt = (d->mResources[ d->mCurrRes ])->
begin();
154 }
while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() );
173 bool AddressBook::Iterator::operator==(
const Iterator &it )
175 return ( d->mIt == it.d->mIt );
178 bool AddressBook::Iterator::operator!=(
const Iterator &it )
180 return ( d->mIt != it.d->mIt );
184 AddressBook::ConstIterator::ConstIterator()
185 : d( new ConstIteratorData )
190 : d( new ConstIteratorData )
193 d->mResources = i.d->mResources;
194 d->mCurrRes = i.d->mCurrRes;
199 d =
new ConstIteratorData;
201 d->mResources = i.d->mResources;
202 d->mCurrRes = i.d->mCurrRes;
211 d =
new ConstIteratorData;
213 d->mResources = i.d->mResources;
214 d->mCurrRes = i.d->mCurrRes;
219 AddressBook::ConstIterator::~ConstIterator()
225 const Addressee &AddressBook::ConstIterator::operator*()
const
230 const Addressee* AddressBook::ConstIterator::operator->()
const
239 while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() ) {
240 if ( (uint)d->mCurrRes == d->mResources.count() - 1 ) {
247 d->mIt = (d->mResources[ d->mCurrRes ])->
begin();
253 }
while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() );
262 while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() ) {
263 if ( (uint)d->mCurrRes == d->mResources.count() - 1 ) {
270 d->mIt = (d->mResources[ d->mCurrRes ])->
begin();
276 }
while ( d->mIt == (d->mResources[ d->mCurrRes ])->end() );
293 bool AddressBook::ConstIterator::operator==(
const ConstIterator &it )
295 return ( d->mIt == it.d->mIt );
298 bool AddressBook::ConstIterator::operator!=(
const ConstIterator &it )
300 return ( d->mIt != it.d->mIt );
305 : d( new AddressBookData )
307 d->mErrorHandler = 0;
309 d->mManager =
new KRES::Manager<Resource>(
"contact" );
310 d->end.d->mResources = TQValueList<Resource*>();
311 d->end.d->mCurrRes = -1;
315 : d( new AddressBookData )
317 d->mErrorHandler = 0;
318 if ( config.isEmpty() )
321 d->mConfig =
new KConfig( config );
322 d->mManager =
new KRES::Manager<Resource>(
"contact" );
323 d->mManager->readConfig( d->mConfig );
324 d->end.d->mResources = TQValueList<Resource*>();
325 d->end.d->mCurrRes = -1;
330 delete d->mManager; d->mManager = 0;
331 delete d->mConfig; d->mConfig = 0;
332 delete d->mErrorHandler; d->mErrorHandler = 0;
342 KRES::Manager<Resource>::ActiveIterator it;
344 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
345 if ( !(*it)->load() ) {
346 error( i18n(
"Unable to load resource '%1'").arg( (*it)->resourceName() ) );
356 kdDebug(5700) <<
"AddressBook::asyncLoad()" <<
endl;
360 KRES::Manager<Resource>::ActiveIterator it;
362 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
363 d->mPendingLoadResources.append( *it );
364 if ( !(*it)->asyncLoad() ) {
365 error( i18n(
"Unable to load resource '%1'").arg( (*it)->resourceName() ) );
377 if ( ticket->resource() ) {
378 deleteRemovedAddressees();
379 bool ok = ticket->resource()->save( ticket );
380 if ( ok ) ticket->resource()->releaseSaveTicket( ticket );
389 kdDebug(5700) <<
"AddressBook::asyncSave()"<<
endl;
391 if ( ticket->resource() ) {
392 d->mPendingSaveResources.append( ticket->resource() );
393 bool ok = ticket->resource()->asyncSave( ticket );
394 if ( ok ) ticket->resource()->releaseSaveTicket( ticket );
403 TQValueList<Resource*> list;
404 KRES::Manager<Resource>::ActiveIterator resIt;
405 for ( resIt = d->mManager->activeBegin(); resIt != d->mManager->activeEnd(); ++resIt )
406 list.append( *resIt );
408 if ( list.count() == 0 )
412 it.d->mResources = list;
414 it.d->mIt = (it.d->mResources[ it.d->mCurrRes ])->
begin();
416 while ( it.d->mIt == (it.d->mResources[ it.d->mCurrRes ])->end() ) {
417 if ( (uint)it.d->mCurrRes == it.d->mResources.count() - 1 )
422 it.d->mIt = (it.d->mResources[ it.d->mCurrRes ])->
begin();
430 TQValueList<Resource*> list;
431 KRES::Manager<Resource>::ActiveIterator resIt;
432 for ( resIt = d->mManager->activeBegin(); resIt != d->mManager->activeEnd(); ++resIt )
433 list.append( *resIt );
435 if ( list.count() == 0 )
439 it.d->mResources = list;
441 it.d->mIt = (it.d->mResources[ it.d->mCurrRes ])->
begin();
443 while ( it.d->mIt == (it.d->mResources[ it.d->mCurrRes ])->end() ) {
444 if ( (uint)it.d->mCurrRes == it.d->mResources.count() - 1 )
449 it.d->mIt = (it.d->mResources[ it.d->mCurrRes ])->
begin();
457 KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
459 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
462 d->end.d->mIt = (*resIt)->end();
470 KRES::Manager<Resource>::ActiveIterator resIt = d->mManager->activeEnd();
472 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
475 d->end.d->mIt = (*resIt)->end();
483 KRES::Manager<Resource>::ActiveIterator it;
484 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
490 kdDebug(5700) <<
"AddressBook::requestSaveTicket()" <<
endl;
493 resource = standardResource();
495 KRES::Manager<Resource>::ActiveIterator it;
496 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
497 if ( (*it) == resource ) {
498 if ( (*it)->readOnly() || !(*it)->isOpen() )
501 return (*it)->requestSaveTicket();
513 if ( ticket->resource() ) {
514 ticket->resource()->releaseSaveTicket( ticket );
522 resource = standardResource();
530 addr.setRevision( TQDateTime::currentDateTime() );
535 resource->insertAddressee( fAddr );
543 resource->insertAddressee( addr );
554 if ( (*it).resource() )
555 (*it).resource()->removeAddressee( *it );
561 for ( it =
begin(); it !=
end(); ++it ) {
562 if ( a.
uid() == (*it).uid() )
571 KRES::Manager<Resource>::ActiveIterator it;
572 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
573 Addressee addr = (*it)->findByUid( uid );
583 Addressee::List list;
586 for ( it =
begin(); it !=
end(); ++it )
594 Addressee::List results;
596 KRES::Manager<Resource>::ActiveIterator it;
597 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
598 results += (*it)->findByName( name );
605 Addressee::List results;
607 KRES::Manager<Resource>::ActiveIterator it;
608 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
609 results += (*it)->findByEmail( email );
616 Addressee::List results;
618 KRES::Manager<Resource>::ActiveIterator it;
619 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it )
620 results += (*it)->findByCategory( category );
627 kdDebug(5700) <<
"AddressBook::dump() --- begin ---" <<
endl;
630 for( it =
begin(); it !=
end(); ++it ) {
634 kdDebug(5700) <<
"AddressBook::dump() --- end ---" <<
endl;
642 KRES::Manager<Resource>::ActiveIterator it;
643 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
644 if ( !(*it)->identifier().isEmpty() )
645 identifier.append( (*it)->identifier() );
648 return identifier.join(
":" );
653 if ( d->mAllFields.isEmpty() ) {
654 d->mAllFields = Field::allFields();
657 if ( category == Field::All )
return d->mAllFields;
660 Field::List::ConstIterator it;
661 for ( it = d->mAllFields.constBegin(); it != d->mAllFields.constEnd(); ++it ) {
662 if ( (*it)->category() & category )
663 result.append( *it );
670 const TQString &key,
const TQString &app )
672 if ( d->mAllFields.isEmpty() ) {
673 d->mAllFields = Field::allFields();
677 TQString k = key.isNull() ? label : key;
679 Field *field = Field::createCustomField( label, category, k, a );
681 if ( !field )
return false;
683 d->mAllFields.append( field );
688 TQDataStream &KABC::operator<<( TQDataStream &s,
const AddressBook &ab )
695 TQDataStream &KABC::operator>>( TQDataStream &s,
AddressBook &ab )
706 if ( !resource->open() ) {
707 kdDebug(5700) <<
"AddressBook::addResource(): can't add resource" <<
endl;
711 d->mManager->add( resource );
712 resource->setAddressBook(
this );
715 this, TQT_SLOT( resourceLoadingFinished( Resource* ) ) );
717 this, TQT_SLOT( resourceSavingFinished( Resource* ) ) );
719 connect( resource, TQT_SIGNAL( loadingError( Resource*,
const TQString& ) ),
720 this, TQT_SLOT( resourceLoadingError( Resource*,
const TQString& ) ) );
721 connect( resource, TQT_SIGNAL( savingError( Resource*,
const TQString& ) ),
722 this, TQT_SLOT( resourceSavingError( Resource*,
const TQString& ) ) );
731 if ( resource == standardResource() )
732 d->mManager->setStandardResource( 0 );
734 resource->setAddressBook( 0 );
737 this, TQT_SLOT( resourceLoadingFinished( Resource* ) ) );
739 this, TQT_SLOT( resourceSavingFinished( Resource* ) ) );
741 disconnect( resource, TQT_SIGNAL( loadingError( Resource*,
const TQString& ) ),
742 this, TQT_SLOT( resourceLoadingError( Resource*,
const TQString& ) ) );
743 disconnect( resource, TQT_SIGNAL( savingError( Resource*,
const TQString& ) ),
744 this, TQT_SLOT( resourceLoadingError( Resource*,
const TQString& ) ) );
746 d->mManager->remove( resource );
753 TQPtrList<Resource> list;
755 KRES::Manager<Resource>::ActiveIterator it;
756 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
757 if ( d->mManager->standardResource() == (*it) )
768 delete d->mErrorHandler;
769 d->mErrorHandler = handler;
774 if ( !d->mErrorHandler )
777 if ( d->mErrorHandler )
778 d->mErrorHandler->error( msg );
780 kdError(5700) <<
"no error handler defined" <<
endl;
783 void AddressBook::deleteRemovedAddressees()
788 void AddressBook::setStandardResource( Resource *resource )
790 d->mManager->setStandardResource( resource );
793 Resource *AddressBook::standardResource()
795 return d->mManager->standardResource();
798 KRES::Manager<Resource> *AddressBook::resourceManager()
809 return d->mPendingLoadResources.isEmpty();
812 void AddressBook::resourceLoadingFinished( Resource *res )
814 d->mPendingLoadResources.remove( res );
817 if ( d->mPendingLoadResources.count() == 0 )
821 void AddressBook::resourceSavingFinished( Resource *res )
823 d->mPendingSaveResources.remove( res );
828 void AddressBook::resourceLoadingError( Resource *res,
const TQString &errMsg )
832 d->mPendingLoadResources.remove( res );
833 if ( d->mPendingLoadResources.count() == 0 )
837 void AddressBook::resourceSavingError( Resource *res,
const TQString &errMsg )
841 d->mPendingSaveResources.remove( res );