00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "contactgroupexpandjob.h"
00023
00024 #include <akonadi/contact/contactgroupsearchjob.h>
00025 #include <akonadi/itemfetchjob.h>
00026 #include <akonadi/itemfetchscope.h>
00027 #include <akonadi/itemsearchjob.h>
00028
00029 using namespace Akonadi;
00030
00031 class ContactGroupExpandJob::Private
00032 {
00033 public:
00034 Private( const KABC::ContactGroup &group, ContactGroupExpandJob *parent )
00035 : mParent( parent ), mGroup( group ), mFetchCount( 0 )
00036 {
00037 }
00038
00039 Private( const QString &name, ContactGroupExpandJob *parent )
00040 : mParent( parent ), mName( name ), mFetchCount( 0 )
00041 {
00042 }
00043
00044 void resolveGroup()
00045 {
00046 for ( unsigned int i = 0; i < mGroup.dataCount(); ++i ) {
00047 const KABC::ContactGroup::Data data = mGroup.data( i );
00048
00049 KABC::Addressee contact;
00050 contact.setNameFromString( data.name() );
00051 contact.insertEmail( data.email(), true );
00052
00053 mContacts.append( contact );
00054 }
00055
00056 for ( unsigned int i = 0; i < mGroup.contactReferenceCount(); ++i ) {
00057 const KABC::ContactGroup::ContactReference reference = mGroup.contactReference( i );
00058
00059 ItemFetchJob *job = new ItemFetchJob( Item( reference.uid().toLongLong() ), mParent );
00060 job->fetchScope().fetchFullPayload();
00061 job->setProperty( "preferredEmail", reference.preferredEmail() );
00062
00063 mParent->connect( job, SIGNAL( result( KJob* ) ), mParent, SLOT( fetchResult( KJob* ) ) );
00064
00065 mFetchCount++;
00066 }
00067
00068 if ( mFetchCount == 0 )
00069 mParent->emitResult();
00070 }
00071
00072 void searchResult( KJob *job )
00073 {
00074 if ( job->error() ) {
00075 mParent->setError( job->error() );
00076 mParent->setErrorText( job->errorText() );
00077 mParent->emitResult();
00078 return;
00079 }
00080
00081 ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
00082
00083 if ( searchJob->contactGroups().isEmpty() ) {
00084 mParent->emitResult();
00085 return;
00086 }
00087
00088 mGroup = searchJob->contactGroups().first();
00089 resolveGroup();
00090 }
00091
00092 void fetchResult( KJob *job )
00093 {
00094 const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
00095
00096 const Item::List items = fetchJob->items();
00097 if ( !items.isEmpty() ) {
00098 const QString email = fetchJob->property( "preferredEmail" ).toString();
00099
00100 const Item item = items.first();
00101 if ( item.hasPayload<KABC::Addressee>() ) {
00102 KABC::Addressee contact = item.payload<KABC::Addressee>();
00103 if ( !email.isEmpty() )
00104 contact.insertEmail( email, true );
00105
00106 mContacts.append( contact );
00107 } else
00108 kWarning() << "Contact for Akonadi item" << item.id() << "does not exist anymore!";
00109 }
00110
00111 mFetchCount--;
00112
00113 if ( mFetchCount == 0 )
00114 mParent->emitResult();
00115 }
00116
00117 ContactGroupExpandJob *mParent;
00118 KABC::ContactGroup mGroup;
00119 QString mName;
00120 KABC::Addressee::List mContacts;
00121
00122 int mFetchCount;
00123 };
00124
00125 ContactGroupExpandJob::ContactGroupExpandJob( const KABC::ContactGroup &group, QObject * parent )
00126 : KJob( parent ), d( new Private( group, this ) )
00127 {
00128 }
00129
00130 ContactGroupExpandJob::ContactGroupExpandJob( const QString &name, QObject * parent )
00131 : KJob( parent ), d( new Private( name, this ) )
00132 {
00133 }
00134
00135 ContactGroupExpandJob::~ContactGroupExpandJob()
00136 {
00137 delete d;
00138 }
00139
00140 void ContactGroupExpandJob::start()
00141 {
00142 if ( !d->mName.isEmpty() ) {
00143
00144 ContactGroupSearchJob *searchJob = new ContactGroupSearchJob( this );
00145 searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
00146 searchJob->setLimit( 1 );
00147 connect( searchJob, SIGNAL( result( KJob* ) ), this, SLOT( searchResult( KJob* ) ) );
00148 } else {
00149 d->resolveGroup();
00150 }
00151 }
00152
00153 KABC::Addressee::List ContactGroupExpandJob::contacts() const
00154 {
00155 return d->mContacts;
00156 }
00157
00158 #include "contactgroupexpandjob.moc"