22#include "contactgroupexpandjob.h"
24#include <akonadi/contact/contactgroupsearchjob.h>
25#include <akonadi/itemfetchjob.h>
26#include <akonadi/itemfetchscope.h>
27#include <akonadi/itemsearchjob.h>
29using namespace Akonadi;
31class ContactGroupExpandJob::Private
35 : mParent( parent ), mGroup( group ), mFetchCount( 0 )
40 : mParent( parent ), mName( name ), mFetchCount( 0 )
46 for (
unsigned int i = 0; i < mGroup.dataCount(); ++i ) {
47 const KABC::ContactGroup::Data data = mGroup.data( i );
49 KABC::Addressee contact;
50 contact.setNameFromString( data.name() );
51 contact.insertEmail( data.email(),
true );
53 mContacts.append( contact );
56 for (
unsigned int i = 0; i < mGroup.contactReferenceCount(); ++i ) {
57 const KABC::ContactGroup::ContactReference reference = mGroup.contactReference( i );
60 if ( !reference.gid().isEmpty() ) {
61 item.setGid( reference.gid() );
63 item.setId( reference.uid().toLongLong() );
65 ItemFetchJob *job =
new ItemFetchJob( item, mParent );
66 job->fetchScope().fetchFullPayload();
67 job->setProperty(
"preferredEmail", reference.preferredEmail() );
69 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(fetchResult(KJob*)) );
74 if ( mFetchCount == 0 ) {
75 mParent->emitResult();
79 void searchResult( KJob *job )
82 mParent->setError( job->error() );
83 mParent->setErrorText( job->errorText() );
84 mParent->emitResult();
90 if ( searchJob->contactGroups().isEmpty() ) {
91 mParent->emitResult();
95 mGroup = searchJob->contactGroups().first();
99 void fetchResult( KJob *job )
101 const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
103 const Item::List items = fetchJob->items();
104 if ( !items.isEmpty() ) {
105 const QString email = fetchJob->property(
"preferredEmail" ).toString();
107 const Item item = items.first();
108 if ( item.hasPayload<KABC::Addressee>() ) {
109 KABC::Addressee contact = item.payload<KABC::Addressee>();
110 if ( !email.isEmpty() ) {
111 contact.insertEmail( email,
true );
114 mContacts.append( contact );
116 kWarning() <<
"Contact for Akonadi item" << item.id() <<
"does not exist anymore!";
121 if ( mFetchCount == 0 ) {
122 mParent->emitResult();
127 KABC::ContactGroup mGroup;
129 KABC::Addressee::List mContacts;
135 : KJob( parent ), d( new Private( group, this ) )
140 : KJob( parent ), d( new Private( name, this ) )
151 if ( !d->mName.isEmpty() && !d->mName.contains( QLatin1Char(
'@' ) ) ) {
155 searchJob->setLimit( 1 );
156 connect( searchJob, SIGNAL(result(KJob*)),
this, SLOT(searchResult(KJob*)) );
167#include "moc_contactgroupexpandjob.cpp"