Better wording
[kdepim.git] / libkdepim / addcontactjob.cpp
blobf121c58097c304b74362a758abb444c3f4c5c51d
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 "addcontactjob.h"
22 #include <akonadi/collectiondialog.h>
23 #include <akonadi/contact/contactsearchjob.h>
24 #include <akonadi/item.h>
25 #include <akonadi/itemcreatejob.h>
26 #include <kabc/addressee.h>
27 #include <klocale.h>
28 #include <kmessagebox.h>
30 #include <QtCore/QPointer>
32 using namespace KPIM;
34 class AddContactJob::Private
36 public:
37 Private( AddContactJob *qq, const KABC::Addressee &contact, QWidget *parentWidget )
38 : q( qq ), mContact( contact ), mParentWidget( parentWidget )
42 Private( AddContactJob *qq, const KABC::Addressee &contact, const Akonadi::Collection &collection )
43 : q( qq ), mContact( contact ), mParentWidget( 0 ), mCollection( collection )
47 void slotSearchDone( KJob *job )
49 if ( job->error() ) {
50 q->setError( job->error() );
51 q->setErrorText( job->errorText() );
52 q->emitResult();
53 return;
56 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
58 const KABC::Addressee::List contacts = searchJob->contacts();
60 if ( !contacts.isEmpty() ) { // contact is already part of the address book...
61 const QString text = i18n( "The VCard's primary email address is already in "
62 "your address book; however, you may save the VCard "
63 "into a file and import it into the address book manually." );
64 KMessageBox::information( mParentWidget, text );
65 q->setError( UserDefinedError );
66 q->emitResult();
67 return;
70 if ( !mCollection.isValid() ) {
71 // ask user in which address book the new contact shall be stored
72 const QStringList mimeTypes( KABC::Addressee::mimeType() );
73 QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog( mParentWidget );
74 dlg->setMimeTypeFilter( mimeTypes );
75 dlg->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
76 dlg->setCaption( i18n( "Select Address Book" ) );
77 dlg->setDescription( i18n( "Select the address book the new contact shall be saved in:" ) );
79 if ( dlg->exec() == QDialog::Accepted ) {
80 mCollection = dlg->selectedCollection();
81 } else {
82 q->setError( UserDefinedError );
83 q->emitResult();
84 delete dlg;
85 return;
88 delete dlg;
91 if ( mCollection.isValid() ) {
92 // create the new item
93 Akonadi::Item item;
94 item.setMimeType( KABC::Addressee::mimeType() );
95 item.setPayload<KABC::Addressee>( mContact );
97 // save the new item in akonadi storage
98 Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, mCollection );
99 q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAddContactDone(KJob*)) );
100 } else {
101 q->setError( UserDefinedError );
102 q->emitResult();
106 void slotAddContactDone( KJob *job )
108 if ( job->error() ) {
109 q->setError( job->error() );
110 q->setErrorText( job->errorText() );
111 q->emitResult();
112 return;
115 const QString text = i18n( "The VCard was added to your address book; "
116 "you can add more information to this "
117 "entry by opening the address book." );
118 KMessageBox::information( mParentWidget, text, QString(), QLatin1String("addedtokabc") );
120 q->emitResult();
123 AddContactJob *q;
124 KABC::Addressee mContact;
125 QWidget *mParentWidget;
126 Akonadi::Collection mCollection;
129 AddContactJob::AddContactJob( const KABC::Addressee &contact, QWidget *parentWidget, QObject *parent )
130 : KJob( parent ), d( new Private( this, contact, parentWidget ) )
134 AddContactJob::AddContactJob( const KABC::Addressee &contact, const Akonadi::Collection &collection, QObject *parent )
135 : KJob( parent ), d( new Private( this, contact, collection ) )
139 AddContactJob::~AddContactJob()
141 delete d;
144 void AddContactJob::start()
146 // first check whether a contact with the same email exists already
147 Akonadi::ContactSearchJob *searchJob = new Akonadi::ContactSearchJob( this );
148 searchJob->setLimit( 1 );
149 searchJob->setQuery( Akonadi::ContactSearchJob::Email, d->mContact.preferredEmail(),
150 Akonadi::ContactSearchJob::ExactMatch );
152 connect( searchJob, SIGNAL(result(KJob*)), SLOT(slotSearchDone(KJob*)) );
155 #include "addcontactjob.moc"