25 #include <Akonadi/CollectionDialog>
26 #include <Akonadi/Contact/ContactSearchJob>
27 #include <Akonadi/Item>
28 #include <Akonadi/ItemCreateJob>
29 #include <Akonadi/ItemModifyJob>
30 #include <Akonadi/CollectionFetchJob>
31 #include <Akonadi/CollectionFetchScope>
32 #include <Akonadi/Collection>
33 #include <Akonadi/Contact/ContactEditorDialog>
34 #include <Akonadi/AgentTypeDialog>
35 #include <Akonadi/AgentType>
36 #include <Akonadi/AgentFilterProxyModel>
37 #include <Akonadi/AgentInstanceCreateJob>
39 #include <KABC/Addressee>
40 #include <KABC/ContactGroup>
43 #include <KMessageBox>
46 #include <QTextDocument>
50 class AddEmailDiplayJob::Private
56 mRemoteContent(false),
57 mCompleteAddress( emailString ),
58 mParentWidget( parentWidget )
60 KABC::Addressee::parseEmailAddress( emailString, mName, mEmail );
63 void slotResourceCreationDone(
KJob* job )
66 q->setError( job->error() );
67 q->setErrorText( job->errorText() );
77 Akonadi::ContactSearchJob *searchJob =
new Akonadi::ContactSearchJob( q );
78 searchJob->setLimit( 1 );
79 searchJob->setQuery( Akonadi::ContactSearchJob::Email, mEmail.toLower(),
80 Akonadi::ContactSearchJob::ExactMatch );
81 q->connect( searchJob, SIGNAL(result(
KJob*)), q, SLOT(slotSearchDone(
KJob*)) );
87 Akonadi::Item item = contact;
88 if (item.hasPayload<KABC::Addressee>()) {
89 KABC::Addressee address = item.payload<KABC::Addressee>();
90 address.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailPreferedFormatting" ), mShowAsHTML ? QLatin1String(
"HTML") : QLatin1String(
"TEXT") );
91 address.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailAllowToRemoteContent" ), mRemoteContent ? QLatin1String(
"TRUE" ) : QLatin1String(
"FALSE" ) );
92 item.setPayload<KABC::Addressee>( address );
93 Akonadi::ItemModifyJob *job =
new Akonadi::ItemModifyJob( item );
94 q->connect( job, SIGNAL(result(
KJob*)), SLOT(slotAddModifyContactDone(
KJob*)) );
100 void slotSearchDone(
KJob *job )
102 if ( job->error() ) {
103 q->setError( job->error() );
104 q->setErrorText( job->errorText() );
109 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
111 const Akonadi::Item::List items = searchJob->items();
112 if ( items.isEmpty() ) {
115 Akonadi::Item item = items.at(0);
116 KABC::Addressee contact = searchJob->contacts()[0];
117 contact.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailPreferedFormatting" ), mShowAsHTML ? QLatin1String(
"HTML") : QLatin1String(
"TEXT") );
118 contact.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailAllowToRemoteContent" ), mRemoteContent ? QLatin1String(
"TRUE" ) : QLatin1String(
"FALSE" ) );
119 item.setPayload<KABC::Addressee>( contact );
120 Akonadi::ItemModifyJob *job =
new Akonadi::ItemModifyJob( item );
121 q->connect( job, SIGNAL(result(
KJob*)), SLOT(slotAddModifyContactDone(
KJob*)) );
127 const QStringList mimeTypes( KABC::Addressee::mimeType() );
129 Akonadi::CollectionFetchJob *
const addressBookJob =
130 new Akonadi::CollectionFetchJob( Akonadi::Collection::root(),
131 Akonadi::CollectionFetchJob::Recursive );
133 addressBookJob->fetchScope().setContentMimeTypes( mimeTypes );
134 q->connect( addressBookJob, SIGNAL(result(
KJob*)), SLOT(slotCollectionsFetched(
KJob*)) );
137 void slotCollectionsFetched(
KJob *job )
139 if ( job->error() ) {
140 q->setError( job->error() );
141 q->setErrorText( job->errorText() );
146 const Akonadi::CollectionFetchJob *addressBookJob =
147 qobject_cast<Akonadi::CollectionFetchJob*>( job );
149 Akonadi::Collection::List canCreateItemCollections ;
151 foreach (
const Akonadi::Collection &collection, addressBookJob->collections() ) {
152 if ( Akonadi::Collection::CanCreateItem & collection.rights() ) {
153 canCreateItemCollections.append(collection);
157 Akonadi::Collection addressBook;
159 const int nbItemCollection( canCreateItemCollections.size() );
160 if ( nbItemCollection == 0 ) {
161 if(KMessageBox::questionYesNo(
164 "You must create an address book before adding a contact. Do you want to create an address book?" ),
165 i18nc(
"@title:window",
"No Address Book Available" ) ) == KMessageBox::Yes) {
166 Akonadi::AgentTypeDialog dlg( mParentWidget );
167 dlg.setCaption( i18n(
"Add Address Book") );
168 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::Addressee::mimeType());
169 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::ContactGroup::mimeType());
170 dlg.agentFilterProxyModel()->addCapabilityFilter( QLatin1String(
"Resource" ) );
173 const Akonadi::AgentType agentType = dlg.agentType();
175 if ( agentType.isValid() ) {
176 Akonadi::AgentInstanceCreateJob *job =
new Akonadi::AgentInstanceCreateJob( agentType, q );
177 q->connect( job, SIGNAL(result(
KJob*)), SLOT(slotResourceCreationDone(
KJob*)) );
178 job->configure( mParentWidget );
182 q->setError( UserDefinedError );
187 q->setError( UserDefinedError );
192 q->setError( UserDefinedError );
196 }
else if ( nbItemCollection == 1 ) {
197 addressBook = canCreateItemCollections[0];
204 addressBook = dlg->selectedCollection();
206 q->setError( UserDefinedError );
216 if ( !addressBook.isValid() ) {
217 q->setError( UserDefinedError );
221 KABC::Addressee contact;
222 contact.setNameFromString( mName );
223 contact.insertEmail( mEmail,
true );
224 contact.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailPreferedFormatting" ), mShowAsHTML ? QLatin1String(
"HTML") : QLatin1String(
"TEXT") );
225 contact.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"MailAllowToRemoteContent" ), mRemoteContent ? QLatin1String(
"TRUE" ) : QLatin1String(
"FALSE" ) );
229 item.setMimeType( KABC::Addressee::mimeType() );
230 item.setPayload<KABC::Addressee>( contact );
233 Akonadi::ItemCreateJob *createJob =
new Akonadi::ItemCreateJob( item, addressBook, q );
234 q->connect( createJob, SIGNAL(result(
KJob*)), SLOT(slotAddModifyContactDone(
KJob*)) );
237 void slotAddModifyContactDone(
KJob *job )
239 if ( job->error() ) {
240 q->setError( job->error() );
241 q->setErrorText( job->errorText() );
247 Akonadi::Item contact;
250 QString mCompleteAddress;
257 :
KJob( parent ), d( new Private( this, email, parentWidget ) )
268 d->mShowAsHTML = html;
273 d->mRemoteContent = b;
278 d->contact = contact;
283 if (d->contact.isValid()) {
290 #include "addemaildisplayjob.moc"
AddEmailDiplayJob(const QString &email, QWidget *parentWidget, QObject *parent=0)
void setContact(const Akonadi::Item &contact)
void setRemoteContent(bool b)
void setShowAsHTML(bool html)