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>
31 #include <kmessagebox.h>
35 class OpenEmailAddressJob::Private
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
)
47 q
->setError( job
->error() );
48 q
->setErrorText( job
->errorText() );
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() );
66 AddEmailAddressJob
*createJob
= new AddEmailAddressJob( mCompleteAddress
, mParentWidget
, q
);
67 q
->connect( createJob
, SIGNAL(result(KJob
*)), SLOT(slotAddContactDone(KJob
*)) );
71 void slotAddContactDone( KJob
*job
)
74 q
->setError( job
->error() );
75 q
->setErrorText( job
->errorText() );
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() );
90 OpenEmailAddressJob
*q
;
91 QString mCompleteAddress
;
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()
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"