25 #include <tdeapplication.h>
27 #include <tdeglobal.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() )
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() )
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 &TDEABC::operator<<( TQDataStream &s,
const AddressBook &ab )
695 TQDataStream &TDEABC::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, TQ_SLOT( resourceLoadingFinished( Resource* ) ) );
717 this, TQ_SLOT( resourceSavingFinished( Resource* ) ) );
719 connect( resource, TQ_SIGNAL( loadingError( Resource*,
const TQString& ) ),
720 this, TQ_SLOT( resourceLoadingError( Resource*,
const TQString& ) ) );
721 connect( resource, TQ_SIGNAL( savingError( Resource*,
const TQString& ) ),
722 this, TQ_SLOT( resourceSavingError( Resource*,
const TQString& ) ) );
731 if ( resource == standardResource() )
732 d->mManager->setStandardResource( 0 );
734 resource->setAddressBook( 0 );
737 this, TQ_SLOT( resourceLoadingFinished( Resource* ) ) );
739 this, TQ_SLOT( resourceSavingFinished( Resource* ) ) );
741 disconnect( resource, TQ_SIGNAL( loadingError( Resource*,
const TQString& ) ),
742 this, TQ_SLOT( resourceLoadingError( Resource*,
const TQString& ) ) );
743 disconnect( resource, TQ_SIGNAL( savingError( Resource*,
const TQString& ) ),
744 this, TQ_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 );
Address Book Const Iterator.
void insertAddressee(const Addressee &addr)
Insert an addressee into the address book.
bool load()
Loads all addressees synchronously.
Iterator find(const Addressee &addr)
Returns an iterator pointing to the specified addressee.
Addressee::List findByEmail(const TQString &email)
Searches all addressees which match the specified email address.
void error(const TQString &msg)
Shows GUI independent error messages.
bool asyncSave(Ticket *ticket)
Saves all addressees of one resource asynchronously.
TQPtrList< Resource > resources()
Returns a list of all resources.
void loadingFinished(Resource *resource)
Emitted when the asynchronous loading of one resource has finished after calling asyncLoad().
void removeAddressee(const Addressee &addr)
Removes an addressee from the address book.
bool addResource(Resource *resource)
Adds a resource to the address book.
virtual ~AddressBook()
Destructor.
void dump() const
Used for debug output.
void releaseSaveTicket(Ticket *ticket)
Releases the ticket requested previously with requestSaveTicket().
void savingFinished(Resource *resource)
Emitted when the asynchronous saving of one resource has finished after calling asyncSave().
Ticket * requestSaveTicket(Resource *resource=0)
Requests a ticket for saving the addressbook.
bool loadingHasFinished() const
Returns true when the loading of the addressbook has finished, otherwise false.
bool asyncLoad()
Loads all addressees asynchronously.
void cleanUp() TDE_DEPRECATED
void setErrorHandler(ErrorHandler *errorHandler)
Sets the ErrorHandler, that is used by error() to provide GUI independent error messages.
void clear()
Removes all addressees from the address book.
Addressee::List allAddressees()
Returns a list of all addressees in the address book.
AddressBook()
Constructs an address book object.
bool addCustomField(const TQString &label, int category=Field::All, const TQString &key=TQString::null, const TQString &app=TQString::null)
Add custom field to address book.
Field::List fields(int category=Field::All)
Returns a list of all Fields known to the address book which are associated with the given field cate...
ConstIterator begin() const
Returns an iterator pointing to the first addressee of address book.
virtual TQString identifier()
Returns a string identifying this addressbook.
bool removeResource(Resource *resource)
Removes a resource from the address book.
bool save(Ticket *ticket)
Saves all addressees of one resource synchronously.
void addressBookChanged(AddressBook *addressBook)
Emitted when one of the resources discovered a change in its backend or the asynchronous loading of a...
Addressee::List findByName(const TQString &name)
Searches all addressees which match the specified name.
Addressee::List findByCategory(const TQString &category)
Searches all addressees which belongs to the specified category.
Addressee findByUid(const TQString &uid)
Searches an addressee with the specified unique identifier.
ConstIterator end() const
Returns an iterator pointing to the last addressee of address book.
bool isEmpty() const
Return, if the address book entry is empty.
void setChanged(bool value)
Mark addressee as changed.
void setResource(Resource *resource)
Set resource where the addressee is from.
Resource * resource() const
Return pointer to resource.
TQString uid() const
Return unique identifier.
This class prints the error messages to stderr via kdError().
Abstract class that provides displaying of error messages.
Helper class for handling coordinated save of address books.
static TDEInstance * instance()
TQCString instanceName() const
kndbgstream & endl(kndbgstream &s)
kdbgstream kdError(int area=0)
kdbgstream kdDebug(int area=0)
static data, shared by ALL addressee objects