21 #include "addressbook.h"
22 #include "distributionlist.h"
23 #include "errorhandler.h"
28 #include <kcomponentdata.h>
30 #include <kstandarddirs.h>
32 #include "addressbook.moc"
36 class AddressBook::Private
43 QList<Resource*> mPendingLoadResources;
44 QList<Resource*> mPendingSaveResources;
46 ConstIterator constEnd;
49 struct AddressBook::Iterator::IteratorData
51 Resource::Iterator mIt;
52 QList<Resource*> mResources;
56 struct AddressBook::ConstIterator::ConstIteratorData
59 QList<Resource*> mResources;
64 : d( new IteratorData )
69 : d( new IteratorData )
72 d->mResources = i.d->mResources;
73 d->mCurrRes = i.d->mCurrRes;
84 d->mResources = i.d->mResources;
85 d->mCurrRes = i.d->mCurrRes;
90 AddressBook::Iterator::~Iterator()
114 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
116 if ( d->mCurrRes == d->mResources.count() - 1 ) {
123 d->mIt = ( d->mResources[ d->mCurrRes ] )->
begin();
130 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
139 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() ) {
141 if ( d->mCurrRes == d->mResources.count() - 1 ) {
148 d->mIt = ( d->mResources[ d->mCurrRes ] )->
begin();
155 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->end() );
176 return d->mIt == it.d->mIt;
181 return d->mIt != it.d->mIt;
185 : d( new ConstIteratorData )
190 : d( new ConstIteratorData )
193 d->mResources = i.d->mResources;
194 d->mCurrRes = i.d->mCurrRes;
197 #ifndef QT_STRICT_ITERATORS
199 :d( new ConstIteratorData )
202 d->mResources = i.d->mResources;
203 d->mCurrRes = i.d->mCurrRes;
215 d->mResources = i.d->mResources;
216 d->mCurrRes = i.d->mCurrRes;
221 AddressBook::ConstIterator::~ConstIterator()
240 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
242 if ( d->mCurrRes == d->mResources.count() - 1 ) {
249 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
256 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
265 while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() ) {
267 if ( d->mCurrRes == d->mResources.count() - 1 ) {
274 d->mIt = ( d->mResources[ d->mCurrRes ] )->constBegin();
281 }
while ( d->mIt == ( d->mResources[ d->mCurrRes ] )->constEnd() );
300 return d->mIt == it.d->mIt;
305 return d->mIt != it.d->mIt;
311 d->mErrorHandler = 0;
314 d->end.d->mResources = QList<Resource*>();
315 d->end.d->mCurrRes = -1;
316 d->constEnd.d->mResources = QList<Resource*>();
317 d->constEnd.d->mCurrRes = -1;
323 d->mErrorHandler = 0;
324 if ( config.isEmpty() ) {
327 d->mConfig =
new KConfig( config );
330 d->mManager->readConfig( d->mConfig );
331 d->end.d->mResources = QList<Resource*>();
332 d->end.d->mCurrRes = -1;
333 d->constEnd.d->mResources = QList<Resource*>();
334 d->constEnd.d->mCurrRes = -1;
343 delete d->mErrorHandler;
344 d->mErrorHandler = 0;
356 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
357 if ( !(*it)->load() ) {
358 error( i18n(
"Unable to load resource '%1'", (*it)->resourceName() ) );
374 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
375 d->mPendingLoadResources.append( *it );
376 if ( !(*it)->asyncLoad() ) {
377 error( i18n(
"Unable to load resource '%1'", (*it)->resourceName() ) );
405 d->mPendingSaveResources.append( ticket->
resource() );
418 QList<Resource*> list;
420 for ( resIt = d->mManager->activeBegin();
421 resIt != d->mManager->activeEnd(); ++resIt ) {
422 list.append( *resIt );
425 if ( list.count() == 0 ) {
430 it.d->mResources = list;
432 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->
begin();
434 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->end() ) {
435 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
441 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->
begin();
449 QList<Resource*> list;
451 for ( resIt = d->mManager->activeBegin();
452 resIt != d->mManager->activeEnd(); ++resIt ) {
453 list.append( *resIt );
456 if ( list.count() == 0 ) {
461 it.d->mResources = list;
463 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
465 while ( it.d->mIt == ( it.d->mResources[ it.d->mCurrRes ] )->constEnd() ) {
466 if ( it.d->mCurrRes == it.d->mResources.count() - 1 ) {
472 it.d->mIt = ( it.d->mResources[ it.d->mCurrRes ] )->constBegin();
482 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
484 d->end.d->mIt = Resource::Iterator();
486 d->end.d->mIt = (*resIt)->end();
496 if ( resIt == d->mManager->activeBegin() || ! *(--resIt) ) {
500 d->constEnd.d->mIt = (*resIt)->constEnd();
509 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
523 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
524 if ( (*it) == resource ) {
525 if ( (*it)->readOnly() || !(*it)->isOpen() ) {
528 return (*it)->requestSaveTicket();
550 if ( resource == 0 ) {
554 Resource::Iterator it;
555 Addressee fAddr = resource->
findByUid( a.uid() );
558 if ( !fAddr.isEmpty() ) {
560 addr.setRevision( QDateTime::currentDateTime() );
562 if ( fAddr.resource() == 0 ) {
563 fAddr.setResource( resource );
571 addr.setResource( resource );
572 addr.setChanged(
true );
578 if ( a.resource() ) {
579 a.resource()->removeAddressee( a );
585 if ( (*it).resource() ) {
586 (*it).resource()->removeAddressee( *it );
593 for ( it =
begin(); it !=
end(); ++it ) {
594 if ( a.uid() == (*it).uid() ) {
605 for ( it =
begin(); it !=
end(); ++it ) {
606 if ( a.uid() == (*it).uid() ) {
617 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
618 Addressee addr = (*it)->findByUid( uid );
619 if ( !addr.isEmpty() ) {
629 Addressee::List list;
632 for ( it =
begin(); it !=
end(); ++it ) {
641 Addressee::List results;
644 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
645 results += (*it)->findByName( name );
653 Addressee::List results;
656 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
657 results += (*it)->findByEmail( email );
665 Addressee::List results;
668 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
669 results += (*it)->findByCategory( category );
677 if ( resource == 0 ) {
686 if ( !list || !list->resource() ) {
696 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
697 DistributionList *list = (*it)->findDistributionListByIdentifier( identifier );
707 Qt::CaseSensitivity caseSensitivity )
710 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
711 DistributionList *list = (*it)->findDistributionListByName( name, caseSensitivity );
722 QList<DistributionList*> results;
725 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
726 results += (*it)->allDistributionLists();
737 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
738 results += (*it)->allDistributionListNames();
746 kDebug() <<
"--- begin ---";
749 for ( it =
begin(); it !=
end(); ++it ) {
750 kDebug() << (*it).toString();
753 kDebug() <<
"--- end ---";
761 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
762 if ( !(*it)->identifier().isEmpty() ) {
763 identifier.append( (*it)->identifier() );
767 return identifier.join( QLatin1String(
":" ) );
772 if ( d->mAllFields.isEmpty() ) {
777 return d->mAllFields;
781 Field::List::ConstIterator it;
782 for ( it = d->mAllFields.constBegin(); it != d->mAllFields.constEnd(); ++it ) {
783 if ( (*it)->category() & category ) {
784 result.append( *it );
794 const QString &app )
const
796 if ( d->mAllFields.isEmpty() ) {
800 QString a = app.isNull() ? KGlobal::mainComponent().componentName() : app;
801 QString k = key.isNull() ? label : key;
809 d->mAllFields.append( field );
814 QDataStream &KABC::operator<<( QDataStream &s,
const AddressBook &ab )
823 QDataStream &KABC::operator>>( QDataStream &s,
AddressBook &ab )
834 if ( !resource->
open() ) {
835 kDebug() <<
"can't add resource";
839 d->mManager->add( resource );
847 connect( resource, SIGNAL(loadingError(
Resource*,QString)),
849 connect( resource, SIGNAL(savingError(
Resource*,QString)),
860 d->mManager->setStandardResource( 0 );
870 disconnect( resource, SIGNAL(loadingError(
Resource*,QString)),
872 disconnect( resource, SIGNAL(savingError(
Resource*,QString)),
875 d->mManager->remove( resource );
882 QList<Resource*> list;
885 for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) {
886 if ( d->mManager->standardResource() == (*it) ) {
898 delete d->mErrorHandler;
899 d->mErrorHandler = handler;
904 if ( !d->mErrorHandler ) {
909 if ( d->mErrorHandler ) {
910 d->mErrorHandler->error( msg );
912 kError() <<
"no error handler defined";
918 d->mManager->setStandardResource( resource );
923 return d->mManager->standardResource();
933 return d->mPendingLoadResources.isEmpty();
938 d->mPendingLoadResources.removeAll( resource );
941 if ( d->mPendingLoadResources.count() == 0 ) {
948 d->mPendingSaveResources.removeAll( resource );
954 const QString &errMsg )
958 d->mPendingLoadResources.removeAll( resource );
959 if ( d->mPendingLoadResources.count() == 0 ) {
965 const QString &errMsg )
969 d->mPendingSaveResources.removeAll( resource );