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>
29 using namespace Akonadi;
31 class ContactGroupExpandJob::Private
34 Private(
const KABC::ContactGroup &group, ContactGroupExpandJob *parent )
35 : mParent( parent ), mGroup( group ), mFetchCount( 0 )
39 Private(
const QString &name, ContactGroupExpandJob *parent )
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 );
59 ItemFetchJob *job =
new ItemFetchJob( Item( reference.uid().toLongLong() ), mParent );
60 job->fetchScope().fetchFullPayload();
61 job->setProperty(
"preferredEmail", reference.preferredEmail() );
63 mParent->connect( job, SIGNAL(result(KJob*)), mParent, SLOT(fetchResult(KJob*)) );
68 if ( mFetchCount == 0 )
69 mParent->emitResult();
72 void searchResult( KJob *job )
75 mParent->setError( job->error() );
76 mParent->setErrorText( job->errorText() );
77 mParent->emitResult();
81 ContactGroupSearchJob *searchJob = qobject_cast<ContactGroupSearchJob*>( job );
83 if ( searchJob->contactGroups().isEmpty() ) {
84 mParent->emitResult();
88 mGroup = searchJob->contactGroups().first();
92 void fetchResult( KJob *job )
94 const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
96 const Item::List items = fetchJob->items();
97 if ( !items.isEmpty() ) {
98 const QString email = fetchJob->property(
"preferredEmail" ).toString();
100 const Item item = items.first();
101 if ( item.hasPayload<KABC::Addressee>() ) {
102 KABC::Addressee contact = item.payload<KABC::Addressee>();
103 if ( !email.isEmpty() )
104 contact.insertEmail( email,
true );
106 mContacts.append( contact );
108 kWarning() <<
"Contact for Akonadi item" << item.id() <<
"does not exist anymore!";
113 if ( mFetchCount == 0 )
114 mParent->emitResult();
117 ContactGroupExpandJob *mParent;
118 KABC::ContactGroup mGroup;
120 KABC::Addressee::List mContacts;
125 ContactGroupExpandJob::ContactGroupExpandJob(
const KABC::ContactGroup &group, QObject * parent )
126 : KJob( parent ), d( new Private( group, this ) )
130 ContactGroupExpandJob::ContactGroupExpandJob(
const QString &name, QObject * parent )
131 : KJob( parent ), d( new Private( name, this ) )
135 ContactGroupExpandJob::~ContactGroupExpandJob()
140 void ContactGroupExpandJob::start()
142 if ( !d->mName.isEmpty() && !d->mName.contains( QLatin1Char(
'@' ) ) ) {
144 ContactGroupSearchJob *searchJob =
new ContactGroupSearchJob(
this );
145 searchJob->setQuery( ContactGroupSearchJob::Name, d->mName );
146 searchJob->setLimit( 1 );
147 connect( searchJob, SIGNAL(result(KJob*)),
this, SLOT(searchResult(KJob*)) );
153 KABC::Addressee::List ContactGroupExpandJob::contacts()
const
158 #include "contactgroupexpandjob.moc"