21 #include "addressbook.h"
22 #include "distributionlist.h"
23 #include "errorhandler.h"
28 #include <kcomponentdata.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;
554 Addressee fAddr = resource->
findByUid( a.uid() );
557 if ( !fAddr.isEmpty() ) {
559 addr.setRevision( QDateTime::currentDateTime() );
561 if ( fAddr.resource() == 0 ) {
562 fAddr.setResource( resource );
570 addr.setResource( resource );
571 addr.setChanged(
true );
577 if ( a.resource() ) {
578 a.resource()->removeAddressee( a );
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 );
618 if ( !addr.isEmpty() ) {
628 Addressee::List list;
631 for ( it =
begin(); it !=
end(); ++it ) {
640 Addressee::List results;
643 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
644 results += ( *it )->findByName( name );
652 Addressee::List results;
655 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
656 results += ( *it )->findByEmail( email );
664 Addressee::List results;
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 );