21#include "addressbook.h"
22#include "distributionlist.h"
23#include "errorhandler.h"
28#include <kcomponentdata.h>
29#include <klocalizedstring.h>
30#include <kstandarddirs.h>
35class AddressBook::Private
42 QList<Resource*> mPendingLoadResources;
43 QList<Resource*> mPendingSaveResources;
45 ConstIterator constEnd;
48struct AddressBook::Iterator::IteratorData
50 Resource::Iterator mIt;
51 QList<Resource*> mResources;
55struct 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;
89AddressBook::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;
220AddressBook::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 ) {
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() ) {
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 );
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 );
Address Book Const Iterator.
ConstIterator & operator++()
Preincrement operator.
bool operator==(const ConstIterator &it) const
Equality operator.
const Addressee & operator*() const
Constant Dereference operator.
ConstIterator()
Default constructor.
const Addressee * operator->() const
Arrow Dereference operator, provided for convenience.
ConstIterator & operator--()
Predecrement operator.
bool operator!=(const ConstIterator &it) const
Inequality operator.
bool operator==(const Iterator &it) const
Equality operator.
bool operator!=(const Iterator &it) const
Inequality operator.
Iterator & operator++()
Preincrement operator.
Iterator & operator--()
Predecrement operator.
const Addressee & operator*() const
Constant Dereference operator.
Addressee * operator->()
Arrow Dereference operator, provided for convenience.
Iterator()
Default constructor.
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.
void savingFinished(Resource *resource)
Emitted when the asynchronous saving of one resource has finished after calling asyncSave().
QList< Resource * > resources() const
Returns a list of all resources.
bool asyncSave(Ticket *ticket)
Saves all addressees of one resource asynchronously.
void removeAddressee(const Addressee &addr)
Removes an addressee from the address book.
virtual QString identifier() const
Returns a string identifying this addressbook.
Addressee::List findByName(const QString &name) const
Searches all addressees which match the specified name.
bool addResource(Resource *resource)
Adds a resource to the address book.
virtual ~AddressBook()
Destructor.
void dump() const
Used for debug output.
Addressee::List allAddressees() const
Returns a list of all addressees in the address book.
void setStandardResource(Resource *resource)
Sets the resource manager's standard resource.
void releaseSaveTicket(Ticket *ticket)
Releases the ticket requested previously with requestSaveTicket().
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...
DistributionList * findDistributionListByName(const QString &name, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
Returns a distribution list with the given name or 0.
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 addCustomField(const QString &label, int category=Field::All, const QString &key=QString(), const QString &app=QString()) const
Add custom field to address book.
bool asyncLoad()
Loads all addressees asynchronously.
DistributionList * findDistributionListByIdentifier(const QString &identifier)
Returns a distribution list for the given identifier or 0.
void resourceLoadingFinished(Resource *resource)
Handles loading success.
Addressee::List findByCategory(const QString &category) const
Searches all addressees which belongs to the specified category.
QStringList allDistributionListNames() const
Returns a list of names of all distribution lists of all resources of this address book.
void error(const QString &msg)
Shows GUI independent error messages.
Addressee::List findByEmail(const QString &email) const
Searches all addressees which match the specified email address.
Addressee findByUid(const QString &uid) const
Searches an addressee with the specified unique identifier.
void addressBookChanged(AddressBook *addressBook)
Emitted when one of the resources discovered a change in its backend or the asynchronous loading of a...
void setErrorHandler(ErrorHandler *errorHandler)
Sets the ErrorHandler, that is used by error() to provide 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 clear()
Removes all addressees from the address book.
Resource * standardResource()
Returns the addressbook resource manager's standard resource.
void loadingFinished(Resource *resource)
Emitted when the asynchronous loading of one resource has finished after calling asyncLoad().
AddressBook()
Constructs an address book object.
void resourceLoadingError(Resource *resource, const QString &errMsg)
Handles loading errors.
ConstIterator begin() const
Returns an iterator pointing to the first addressee of address book.
bool removeResource(Resource *resource)
Removes a resource from the address book.
bool save(Ticket *ticket)
Saves all addressees of one resource synchronously.
void removeDistributionList(DistributionList *list)
Removes a distribution list from its associated resource.
void resourceSavingError(Resource *resource, const QString &errMsg)
Handles saving errors.
DistributionList * createDistributionList(const QString &name, Resource *resource=0)
Creates a distribution list with a given name storing it in a given resource.
ConstIterator end() const
Returns an iterator pointing to the last addressee of address book.
KRES::Manager< Resource > * resourceManager()
Returns the addressbook's resource manager.
a QValueList of Addressee, with sorting functionality
void setRevision(const QDateTime &revision)
Set revision date.
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.
QString uid() const
Return unique identifier.
This class prints the error messages to stderr via kError().
Distribution list of email addresses.
Abstract class that provides displaying of error messages.
Represents a field in the Addressbook.
static Field * createCustomField(const QString &label, int category, const QString &key, const QString &app)
Creates a custom field.
QList< Field * > List
This type is used for a list of fields.
static Field::List allFields()
Returns a list of all fields.
virtual void releaseSaveTicket(Ticket *ticket)=0
Releases the ticket previousely requested with requestSaveTicket().
virtual bool save(Ticket *ticket)=0
Saves all addressees synchronously.
virtual void removeAddressee(const Addressee &addr)
Removes an addressee from resource.
virtual void insertAddressee(const Addressee &addr)
Insert an addressee into the resource.
virtual Addressee findByUid(const QString &uid)
Searches an addressee with the specified unique identifier.
virtual bool asyncSave(Ticket *ticket)
Saves all addressees asynchronously.
void setAddressBook(AddressBook *addr)
virtual void removeDistributionList(DistributionList *list)
Removes a distribution list from this resource.
Helper class for handling coordinated save of address books.
Resource * resource()
Returns the resource for which this ticket has been created.
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
QDataStream & operator<<(QDataStream &stream, const Address &address)
Serializes the address object into the stream.
QDataStream & operator>>(QDataStream &stream, Address &address)
Initializes the address object from the stream.