21 #include "addressbook.h"
22 #include "distributionlist.h"
23 #include "errorhandler.h"
28 #include <kcomponentdata.h>
29 #include <klocalizedstring.h>
30 #include <kstandarddirs.h>
35 class AddressBook::Private
42 QList<Resource*> mPendingLoadResources;
43 QList<Resource*> mPendingSaveResources;
45 ConstIterator constEnd;
48 struct AddressBook::Iterator::IteratorData
50 Resource::Iterator mIt;
51 QList<Resource*> mResources;
55 struct AddressBook::ConstIterator::ConstIteratorData
58 QList<Resource*> mResources;
63 : d( new IteratorData )
68 : d( new IteratorData )
71 d->mResources = i.d->mResources;
72 d->mCurrRes = i.d->mCurrRes;
83 d->mResources = i.d->mResources;
84 d->mCurrRes = i.d->mCurrRes;
89 AddressBook::Iterator::~Iterator()
106 return &( *( d->mIt ) );
113 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
115 if ( d->mCurrRes == d->mResources.count() - 1 ) {
122 d->mIt = ( d->mResources[ d->mCurrRes ] )->
begin();
129 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
138 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
140 if ( d->mCurrRes == d->mResources.count() - 1 ) {
147 d->mIt = ( d->mResources[ d->mCurrRes ] )->
begin();
154 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
175 return d->mIt == it.d->mIt;
180 return d->mIt != it.d->mIt;
184 : d( new ConstIteratorData )
189 : d( new ConstIteratorData )
192 d->mResources = i.d->mResources;
193 d->mCurrRes = i.d->mCurrRes;
196 #ifndef QT_STRICT_ITERATORS
198 :d( new ConstIteratorData )
201 d->mResources = i.d->mResources;
202 d->mCurrRes = i.d->mCurrRes;
214 d->mResources = i.d->mResources;
215 d->mCurrRes = i.d->mCurrRes;
220 AddressBook::ConstIterator::~ConstIterator()
232 return &( *( d->mIt ) );
239 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
241 if ( d->mCurrRes == d->mResources.count() - 1 ) {
248 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
255 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
264 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
266 if ( d->mCurrRes == d->mResources.count() - 1 ) {
273 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
280 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
299 return d->mIt == it.d->mIt;
304 return d->mIt != it.d->mIt;
310 d->mErrorHandler = 0;
313 d->end.d->mResources = QList<Resource*>();
314 d->end.d->mCurrRes = -1;
315 d->constEnd.d->mResources = QList<Resource*>();
316 d->constEnd.d->mCurrRes = -1;
322 d->mErrorHandler = 0;
323 if ( config.isEmpty() ) {
326 d->mConfig =
new KConfig( config );
329 d->mManager->readConfig( d->mConfig );
330 d->end.d->mResources = QList<Resource*>();
331 d->end.d->mCurrRes = -1;
332 d->constEnd.d->mResources = QList<Resource*>();
333 d->constEnd.d->mCurrRes = -1;
342 delete d->mErrorHandler;
343 d->mErrorHandler = 0;
355 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
356 if ( !( *it )->load() ) {
357 error( i18n(
"Unable to load resource '%1'", ( *it )->resourceName() ) );
373 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
374 d->mPendingLoadResources.append( *it );
375 if ( !( *it )->asyncLoad() ) {
376 error( i18n(
"Unable to load resource '%1'", ( *it )->resourceName() ) );
404 d->mPendingSaveResources.append( ticket->
resource() );
417 QList<Resource*> list;
419 for ( resIt = d->mManager->activeBegin();
420 resIt != d->mManager->activeEnd(); ++resIt ) {
421 list.append( *resIt );
424 if ( list.count() == 0 ) {
429 it.d->mResources = list;
431 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->
begin();
433 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->end() ) {
434 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
440 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->
begin();
448 QList<Resource*> list;
450 for ( resIt = d->mManager->activeBegin();
451 resIt != d->mManager->activeEnd(); ++resIt ) {
452 list.append( *resIt );
455 if ( list.count() == 0 ) {
460 it.d->mResources = list;
462 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
464 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->constEnd() ) {
465 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
471 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
481 if ( resIt == d->mManager->activeBegin() || ! *( --resIt ) ) {
483 d->end.d->mIt = Resource::Iterator();
485 d->end.d->mIt = ( *resIt )->end();
495 if ( resIt == d->mManager->activeBegin() || ! *( --resIt ) ) {
499 d->constEnd.d->mIt = ( *resIt )->constEnd();
508 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
522 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
523 if ( ( *it ) == resource ) {
524 if ( ( *it )->readOnly() || !( *it )->isOpen() ) {
527 return ( *it )->requestSaveTicket();
549 if ( resource == 0 ) {
553 Resource::Iterator it;
584 if ( ( *it ).resource() ) {
585 ( *it ).resource()->removeAddressee( *it );
592 for ( it =
begin(); it !=
end(); ++it ) {
593 if ( a.
uid() == ( *it ).uid() ) {
604 for ( it =
begin(); it !=
end(); ++it ) {
605 if ( a.
uid() == ( *it ).uid() ) {
616 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
617 Addressee addr = ( *it )->findByUid( uid );
631 for ( it =
begin(); it !=
end(); ++it ) {
643 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
644 results += ( *it )->findByName( name );
655 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
656 results += ( *it )->findByEmail( email );
667 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
668 results += ( *it )->findByCategory( category );
676 if ( resource == 0 ) {
685 if ( !list || !list->resource() ) {
695 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
696 DistributionList *list = ( *it )->findDistributionListByIdentifier( identifier );
706 Qt::CaseSensitivity caseSensitivity )
709 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
710 DistributionList *list = ( *it )->findDistributionListByName( name, caseSensitivity );
721 QList<DistributionList*> results;
724 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
725 results += ( *it )->allDistributionLists();
736 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
737 results += ( *it )->allDistributionListNames();
745 kDebug() <<
"--- begin ---";
748 for ( it =
begin(); it !=
end(); ++it ) {
749 kDebug() << ( *it ).toString();
752 kDebug() <<
"--- end ---";
760 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
761 if ( !( *it )->identifier().isEmpty() ) {
762 identifier.append( ( *it )->identifier() );
766 return identifier.join( QLatin1String(
":" ) );
771 if ( d->mAllFields.isEmpty() ) {
776 return d->mAllFields;
780 Field::List::ConstIterator it;
781 for ( it = d->mAllFields.constBegin(); it != d->mAllFields.constEnd(); ++it ) {
782 if ( ( *it )->category() & category ) {
783 result.append( *it );
793 const QString &app )
const
795 if ( d->mAllFields.isEmpty() ) {
799 QString a = app.isNull() ? KGlobal::mainComponent().componentName() : app;
800 QString k = key.isNull() ? label : key;
808 d->mAllFields.append( field );
813 QDataStream &KABC::operator<<( QDataStream &s,
const AddressBook &ab )
822 QDataStream &KABC::operator>>( QDataStream &s,
AddressBook &ab )
833 if ( !resource->
open() ) {
834 kDebug() <<
"can't add resource";
838 d->mManager->add( resource );
846 connect( resource, SIGNAL(loadingError(
Resource*,QString)),
848 connect( resource, SIGNAL(savingError(
Resource*,QString)),
859 d->mManager->setStandardResource( 0 );
869 disconnect( resource, SIGNAL(loadingError(
Resource*,QString)),
871 disconnect( resource, SIGNAL(savingError(
Resource*,QString)),
874 d->mManager->remove( resource );
881 QList<Resource*> list;
884 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
885 if ( d->mManager->standardResource() == ( *it ) ) {
897 delete d->mErrorHandler;
898 d->mErrorHandler = handler;
903 if ( !d->mErrorHandler ) {
908 if ( d->mErrorHandler ) {
909 d->mErrorHandler->error( msg );
911 kError() <<
"no error handler defined";
917 d->mManager->setStandardResource( resource );
922 return d->mManager->standardResource();
932 return d->mPendingLoadResources.isEmpty();
937 d->mPendingLoadResources.removeAll( resource );
940 if ( d->mPendingLoadResources.count() == 0 ) {
947 d->mPendingSaveResources.removeAll( resource );
953 const QString &errMsg )
957 d->mPendingLoadResources.removeAll( resource );
958 if ( d->mPendingLoadResources.count() == 0 ) {
964 const QString &errMsg )
968 d->mPendingSaveResources.removeAll( resource );
Ticket * requestSaveTicket(Resource *resource=0)
Requests a ticket for saving the addressbook.
const Addressee * operator->() const
Arrow Dereference operator, provided for convenience.
void dump() const
Used for debug output.
ConstIterator end() const
Returns an iterator pointing to the last addressee of address book.
void addressBookChanged(AddressBook *addressBook)
Emitted when one of the resources discovered a change in its backend or the asynchronous loading of a...
void resourceLoadingError(Resource *resource, const QString &errMsg)
Handles loading errors.
Abstract class that provides displaying of error messages.
DistributionList * createDistributionList(const QString &name, Resource *resource=0)
Creates a distribution list with a given name storing it in a given resource.
bool removeResource(Resource *resource)
Removes a resource from the address book.
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
bool operator==(const ConstIterator &it) const
Equality operator.
QStringList allDistributionListNames() const
Returns a list of names of all distribution lists of all resources of this address book...
a QValueList of Addressee, with sorting functionality
ConstIterator & operator++()
Preincrement operator.
const Addressee & operator*() const
Constant Dereference operator.
void removeAddressee(const Addressee &addr)
Removes an addressee from the address book.
void setResource(Resource *resource)
Set resource where the addressee is from.
bool asyncSave(Ticket *ticket)
Saves all addressees of one resource asynchronously.
void error(const QString &msg)
Shows GUI independent error messages.
QList< DistributionList * > allDistributionLists()
Returns a list of all distribution lists of all resources of this address book.
void resourceSavingFinished(Resource *resource)
Handles saving success.
void releaseSaveTicket(Ticket *ticket)
Releases the ticket requested previously with requestSaveTicket().
const Addressee & operator*() const
Constant Dereference operator.
Addressee * operator->()
Arrow Dereference operator, provided for convenience.
AddressBook()
Constructs an address book object.
bool addResource(Resource *resource)
Adds a resource to the address book.
void loadingFinished(Resource *resource)
Emitted when the asynchronous loading of one resource has finished after calling asyncLoad().
Addressee::List findByCategory(const QString &category) const
Searches all addressees which belongs to the specified category.
Distribution list of email addresses.
virtual void insertAddressee(const Addressee &addr)
Insert an addressee into the resource.
void setChanged(bool value)
Mark addressee as changed.
Addressee findByUid(const QString &uid) const
Searches an addressee with the specified unique identifier.
Helper class for handling coordinated save of address books.
void insertAddressee(const Addressee &addr)
Insert an addressee into the address book.
void clear()
Removes all addressees from the address book.
KRES::Manager< Resource > * resourceManager()
Returns the addressbook's resource manager.
bool operator!=(const Iterator &it) const
Inequality operator.
Iterator & operator++()
Preincrement operator.
This class prints the error messages to stderr via kError().
bool addCustomField(const QString &label, int category=Field::All, const QString &key=QString(), const QString &app=QString()) const
Add custom field to address book.
virtual QString identifier() const
Returns a string identifying this addressbook.
virtual void removeDistributionList(DistributionList *list)
Removes a distribution list from this resource.
Address Book Const Iterator.
ConstIterator begin() const
Returns an iterator pointing to the first addressee of address book.
ConstIterator()
Default constructor.
static Field::List allFields()
Returns a list of all fields.
QList< Field * > List
This type is used for a list of fields.
void resourceLoadingFinished(Resource *resource)
Handles loading success.
Iterator find(const Addressee &addr)
Returns an iterator pointing to the specified addressee.
Resource * resource()
Returns the resource for which this ticket has been created.
virtual void removeAddressee(const Addressee &addr)
Removes an addressee from resource.
Represents a field in the Addressbook.
bool loadingHasFinished() const
Returns true when the loading of the addressbook has finished, otherwise false.
Addressee::List allAddressees() const
Returns a list of all addressees in the address book.
Iterator & operator--()
Predecrement operator.
DistributionList * findDistributionListByIdentifier(const QString &identifier)
Returns a distribution list for the given identifier or 0.
bool isEmpty() const
Return, if the address book entry is empty.
virtual ~AddressBook()
Destructor.
Field::List fields(int category=Field::All) const
Returns a list of all Fields known to the address book which are associated with the given field cate...
Iterator()
Default constructor.
Addressee::List findByName(const QString &name) const
Searches all addressees which match the specified name.
virtual Addressee findByUid(const QString &uid)
Searches an addressee with the specified unique identifier.
Resource * resource() const
Return pointer to resource.
void setRevision(const QDateTime &revision)
Set revision date.
DistributionList * findDistributionListByName(const QString &name, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Returns a distribution list with the given name or 0.
void setAddressBook(AddressBook *addr)
bool operator==(const Iterator &it) const
Equality operator.
QString uid() const
Return unique identifier.
void savingFinished(Resource *resource)
Emitted when the asynchronous saving of one resource has finished after calling asyncSave().
bool operator!=(const ConstIterator &it) const
Inequality operator.
virtual void releaseSaveTicket(Ticket *ticket)=0
Releases the ticket previousely requested with requestSaveTicket().
static Field * createCustomField(const QString &label, int category, const QString &key, const QString &app)
Creates a custom field.
void removeDistributionList(DistributionList *list)
Removes a distribution list from its associated resource.
void setStandardResource(Resource *resource)
Sets the resource manager's standard resource.
void setErrorHandler(ErrorHandler *errorHandler)
Sets the ErrorHandler, that is used by error() to provide GUI independent error messages.
Addressee::List findByEmail(const QString &email) const
Searches all addressees which match the specified email address.
ConstIterator & operator--()
Predecrement operator.
QList< Resource * > resources() const
Returns a list of all resources.
bool save(Ticket *ticket)
Saves all addressees of one resource synchronously.
bool asyncLoad()
Loads all addressees asynchronously.
Resource * standardResource()
Returns the addressbook resource manager's standard resource.
void resourceSavingError(Resource *resource, const QString &errMsg)
Handles saving errors.
bool load()
Loads all addressees synchronously.
virtual bool save(Ticket *ticket)=0
Saves all addressees synchronously.