26 #include <Akonadi/CollectionDialog>
27 #include <Akonadi/Contact/ContactSearchJob>
28 #include <Akonadi/Item>
29 #include <Akonadi/ItemCreateJob>
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 AddEmailAddressJob::Private
54 : q( qq ), mCompleteAddress( emailString ), mParentWidget( parentWidget )
56 KABC::Addressee::parseEmailAddress( emailString, mName, mEmail );
59 void slotResourceCreationDone(
KJob* job )
62 q->setError( job->error() );
63 q->setErrorText( job->errorText() );
71 void slotSearchDone(
KJob *job )
74 q->setError( job->error() );
75 q->setErrorText( job->errorText() );
80 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
82 const KABC::Addressee::List contacts = searchJob->contacts();
83 if ( !contacts.isEmpty() ) {
86 "A contact with the email address <email>%1</email> "
87 "is already in your address book.", Qt::escape(mCompleteAddress) );
89 KMessageBox::information(
93 QLatin1String(
"alreadyInAddressBook" ) );
94 q->setError( UserDefinedError );
103 const QStringList mimeTypes( KABC::Addressee::mimeType() );
105 Akonadi::CollectionFetchJob *
const addressBookJob =
106 new Akonadi::CollectionFetchJob( Akonadi::Collection::root(),
107 Akonadi::CollectionFetchJob::Recursive );
109 addressBookJob->fetchScope().setContentMimeTypes( mimeTypes );
110 q->connect( addressBookJob, SIGNAL(result(
KJob*)), SLOT(slotCollectionsFetched(
KJob*)) );
113 void slotCollectionsFetched(
KJob *job )
115 if ( job->error() ) {
116 q->setError( job->error() );
117 q->setErrorText( job->errorText() );
122 const Akonadi::CollectionFetchJob *addressBookJob =
123 qobject_cast<Akonadi::CollectionFetchJob*>( job );
125 Akonadi::Collection::List canCreateItemCollections ;
127 foreach (
const Akonadi::Collection &collection, addressBookJob->collections() ) {
128 if ( Akonadi::Collection::CanCreateItem & collection.rights() ) {
129 canCreateItemCollections.append(collection);
133 Akonadi::Collection addressBook;
135 const int nbItemCollection( canCreateItemCollections.size() );
136 if ( nbItemCollection == 0 ) {
137 if(KMessageBox::questionYesNo(
140 "You must create an address book before adding a contact. Do you want to create an address book?" ),
141 i18nc(
"@title:window",
"No Address Book Available" ) ) == KMessageBox::Yes) {
142 Akonadi::AgentTypeDialog dlg( mParentWidget );
143 dlg.setCaption( i18n(
"Add Address Book") );
144 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::Addressee::mimeType());
145 dlg.agentFilterProxyModel()->addMimeTypeFilter(KABC::ContactGroup::mimeType());
146 dlg.agentFilterProxyModel()->addCapabilityFilter( QLatin1String(
"Resource" ) );
149 const Akonadi::AgentType agentType = dlg.agentType();
151 if ( agentType.isValid() ) {
152 Akonadi::AgentInstanceCreateJob *job =
new Akonadi::AgentInstanceCreateJob( agentType, q );
153 q->connect( job, SIGNAL(result(
KJob*)), SLOT(slotResourceCreationDone(
KJob*)) );
154 job->configure( mParentWidget );
158 q->setError( UserDefinedError );
163 q->setError( UserDefinedError );
168 q->setError( UserDefinedError );
172 }
else if ( nbItemCollection == 1 ) {
173 addressBook = canCreateItemCollections[0];
180 addressBook = dlg->selectedCollection();
182 q->setError( UserDefinedError );
192 if ( !addressBook.isValid() ) {
193 q->setError( UserDefinedError );
198 contact.setNameFromString( mName );
199 contact.insertEmail( mEmail,
true );
203 item.setMimeType( KABC::Addressee::mimeType() );
204 item.setPayload<KABC::Addressee>(
contact );
207 Akonadi::ItemCreateJob *createJob =
new Akonadi::ItemCreateJob( item, addressBook, q );
208 q->connect( createJob, SIGNAL(result(
KJob*)), SLOT(slotAddContactDone(
KJob*)) );
211 void slotAddContactDone(
KJob *job )
213 if ( job->error() ) {
214 q->setError( job->error() );
215 q->setErrorText( job->errorText() );
220 const Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob*>( job );
221 mItem = createJob->item();
225 "<para>A contact for <email>%1</email> was successfully added "
226 "to your address book.</para>"
227 "<para>Do you want to edit this new contact now?</para>",
228 Qt::escape(mCompleteAddress) );
230 if ( KMessageBox::questionYesNo(
234 KStandardGuiItem::yes(),
235 KStandardGuiItem::no(),
236 QLatin1String(
"addedtokabc" ) ) == KMessageBox::Yes ) {
237 QPointer<Akonadi::ContactEditorDialog> dlg =
238 new Akonadi::ContactEditorDialog( Akonadi::ContactEditorDialog::EditMode,
240 dlg->setContact( mItem );
241 connect( dlg, SIGNAL(contactStored(Akonadi::Item)),
242 q, SLOT(contactStored(Akonadi::Item)) );
243 connect( dlg, SIGNAL(error(QString)),
244 q, SLOT(slotContactEditorError(QString)) );
251 void slotContactEditorError(
const QString &error)
253 KMessageBox::error(mParentWidget, i18n(
"Contact cannot be stored: %1", error), i18n(
"Failed to store contact"));
256 void contactStored(
const Akonadi::Item & )
263 QString mCompleteAddress;
272 :
KJob( parent ), d( new Private( this, email, parentWidget ) )
284 Akonadi::ContactSearchJob *searchJob =
new Akonadi::ContactSearchJob(
this );
285 searchJob->setLimit( 1 );
286 searchJob->setQuery( Akonadi::ContactSearchJob::Email, d->mEmail.toLower(),
287 Akonadi::ContactSearchJob::ExactMatch );
288 connect( searchJob, SIGNAL(result(
KJob*)), SLOT(slotSearchDone(
KJob*)) );
296 #include "addemailaddressjob.moc"
Akonadi::Item contact() const
Returns the item that represents the new contact.
static BroadcastStatus * instance()
Return the instance of the singleton object for this class.
A job to add a new contact with a given email address to Akonadi.
~AddEmailAddressJob()
Destroys the add email address job.
void setStatusMsg(const QString &message)
Emit an update status bar signal.
virtual void start()
Starts the job.
AddEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent=0)
Creates a new add email address job.