compile
[kdepim.git] / libkdepim / openemailaddressjob.cpp
blob0d0d5c8f0dbff5de5ed5300bfd5ece99b9423298
1 /*
2 Copyright 2010 Tobias Koenig <tokoe@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "openemailaddressjob.h"
22 #include "addemailaddressjob.h"
24 #include <akonadi/collectiondialog.h>
25 #include <akonadi/contact/contacteditordialog.h>
26 #include <akonadi/contact/contactsearchjob.h>
27 #include <akonadi/item.h>
28 #include <akonadi/itemcreatejob.h>
29 #include <kabc/addressee.h>
30 #include <klocale.h>
31 #include <kmessagebox.h>
33 using namespace KPIM;
35 class OpenEmailAddressJob::Private
37 public:
38 Private( OpenEmailAddressJob *qq, const QString &emailString, QWidget *parentWidget )
39 : q( qq ), mCompleteAddress( emailString ), mParentWidget( parentWidget )
41 KABC::Addressee::parseEmailAddress( emailString, mName, mEmail );
44 void slotSearchDone( KJob *job )
46 if ( job->error() ) {
47 q->setError( job->error() );
48 q->setErrorText( job->errorText() );
49 q->emitResult();
50 return;
53 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
55 const Akonadi::Item::List contacts = searchJob->items();
56 if ( !contacts.isEmpty() ) {
57 // open the editor with the matching item
58 Akonadi::ContactEditorDialog dlg( Akonadi::ContactEditorDialog::EditMode, mParentWidget );
59 dlg.setContact( contacts.first() );
60 dlg.exec();
62 q->emitResult();
63 return;
66 AddEmailAddressJob *createJob = new AddEmailAddressJob( mCompleteAddress, mParentWidget, q );
67 q->connect( createJob, SIGNAL(result(KJob*)), SLOT(slotAddContactDone(KJob*)) );
68 createJob->start();
71 void slotAddContactDone( KJob *job )
73 if ( job->error() ) {
74 q->setError( job->error() );
75 q->setErrorText( job->errorText() );
76 q->emitResult();
77 return;
80 const AddEmailAddressJob *createJob = qobject_cast<AddEmailAddressJob*>( job );
82 // open the editor with the matching item
83 Akonadi::ContactEditorDialog dlg( Akonadi::ContactEditorDialog::EditMode, mParentWidget );
84 dlg.setContact( createJob->contact() );
85 dlg.exec();
87 q->emitResult();
90 OpenEmailAddressJob *q;
91 QString mCompleteAddress;
92 QString mEmail;
93 QString mName;
94 QWidget *mParentWidget;
97 OpenEmailAddressJob::OpenEmailAddressJob( const QString &email, QWidget *parentWidget, QObject *parent )
98 : KJob( parent ), d( new Private( this, email, parentWidget ) )
102 OpenEmailAddressJob::~OpenEmailAddressJob()
104 delete d;
107 void OpenEmailAddressJob::start()
109 // first check whether a contact with the same email exists already
110 Akonadi::ContactSearchJob *searchJob = new Akonadi::ContactSearchJob( this );
111 searchJob->setLimit( 1 );
112 searchJob->setQuery( Akonadi::ContactSearchJob::Email, d->mEmail,
113 Akonadi::ContactSearchJob::ExactMatch );
114 connect( searchJob, SIGNAL(result(KJob*)), SLOT(slotSearchDone(KJob*)) );
117 #include "openemailaddressjob.moc"