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>
28 #include <kmessagebox.h>
30 #include <QtCore/QPointer>
34 class AddContactJob::Private
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
)
50 q
->setError( job
->error() );
51 q
->setErrorText( job
->errorText() );
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
);
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();
82 q
->setError( UserDefinedError
);
91 if ( mCollection
.isValid() ) {
92 // create the new 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
*)) );
101 q
->setError( UserDefinedError
);
106 void slotAddContactDone( KJob
*job
)
108 if ( job
->error() ) {
109 q
->setError( job
->error() );
110 q
->setErrorText( job
->errorText() );
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") );
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()
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"