• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • job
addemailaddressjob.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2010 Tobias Koenig <tokoe@kde.org>
3  Copyright 2010 Nicolas Lécureuil <nicolas.lecureuil@free.fr>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "addemailaddressjob.h"
22 #include "misc/broadcaststatus.h"
23 #include "widgets/selectedcollectiondialog.h"
24 
25 
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>
38 
39 #include <KABC/Addressee>
40 #include <KABC/ContactGroup>
41 
42 #include <KLocale>
43 #include <KMessageBox>
44 
45 #include <QPointer>
46 #include <QTextDocument>
47 
48 using namespace KPIM;
49 
50 class AddEmailAddressJob::Private
51 {
52  public:
53  Private( AddEmailAddressJob *qq, const QString &emailString, QWidget *parentWidget )
54  : q( qq ), mCompleteAddress( emailString ), mParentWidget( parentWidget )
55  {
56  KABC::Addressee::parseEmailAddress( emailString, mName, mEmail );
57  }
58 
59  void slotResourceCreationDone( KJob* job )
60  {
61  if ( job->error() ) {
62  q->setError( job->error() );
63  q->setErrorText( job->errorText() );
64  q->emitResult();
65  return;
66  }
67  createContact();
68  }
69 
70 
71  void slotSearchDone( KJob *job )
72  {
73  if ( job->error() ) {
74  q->setError( job->error() );
75  q->setErrorText( job->errorText() );
76  q->emitResult();
77  return;
78  }
79 
80  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
81 
82  const KABC::Addressee::List contacts = searchJob->contacts();
83  if ( !contacts.isEmpty() ) {
84  const QString text =
85  i18nc( "@info",
86  "A contact with the email address <email>%1</email> "
87  "is already in your address book.", Qt::escape(mCompleteAddress) );
88 
89  KMessageBox::information(
90  mParentWidget,
91  text,
92  QString(),
93  QLatin1String( "alreadyInAddressBook" ) );
94  q->setError( UserDefinedError );
95  q->emitResult();
96  return;
97  }
98  createContact();
99  }
100 
101  void createContact()
102  {
103  const QStringList mimeTypes( KABC::Addressee::mimeType() );
104 
105  Akonadi::CollectionFetchJob * const addressBookJob =
106  new Akonadi::CollectionFetchJob( Akonadi::Collection::root(),
107  Akonadi::CollectionFetchJob::Recursive );
108 
109  addressBookJob->fetchScope().setContentMimeTypes( mimeTypes );
110  q->connect( addressBookJob, SIGNAL(result(KJob*)), SLOT(slotCollectionsFetched(KJob*)) );
111  }
112 
113  void slotCollectionsFetched( KJob *job )
114  {
115  if ( job->error() ) {
116  q->setError( job->error() );
117  q->setErrorText( job->errorText() );
118  q->emitResult();
119  return;
120  }
121 
122  const Akonadi::CollectionFetchJob *addressBookJob =
123  qobject_cast<Akonadi::CollectionFetchJob*>( job );
124 
125  Akonadi::Collection::List canCreateItemCollections ;
126 
127  foreach ( const Akonadi::Collection &collection, addressBookJob->collections() ) {
128  if ( Akonadi::Collection::CanCreateItem & collection.rights() ) {
129  canCreateItemCollections.append(collection);
130  }
131  }
132 
133  Akonadi::Collection addressBook;
134 
135  const int nbItemCollection( canCreateItemCollections.size() );
136  if ( nbItemCollection == 0 ) {
137  if(KMessageBox::questionYesNo(
138  mParentWidget,
139  i18nc( "@info",
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" ) );
147 
148  if ( dlg.exec() ) {
149  const Akonadi::AgentType agentType = dlg.agentType();
150 
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 );
155  job->start();
156  return;
157  } else { //if agent is not valid => return error and finish job
158  q->setError( UserDefinedError );
159  q->emitResult();
160  return;
161  }
162  } else { //Canceled create agent => return error and finish job
163  q->setError( UserDefinedError );
164  q->emitResult();
165  return;
166  }
167  } else {
168  q->setError( UserDefinedError );
169  q->emitResult();
170  return;
171  }
172  } else if ( nbItemCollection == 1 ) {
173  addressBook = canCreateItemCollections[0];
174  } else {
175  // ask user in which address book the new contact shall be stored
176  QPointer<SelectedCollectionDialog> dlg = new SelectedCollectionDialog( mParentWidget );
177 
178  bool gotIt = true;
179  if ( dlg->exec() ) {
180  addressBook = dlg->selectedCollection();
181  } else {
182  q->setError( UserDefinedError );
183  q->emitResult();
184  gotIt = false;
185  }
186  delete dlg;
187  if ( !gotIt ) {
188  return;
189  }
190  }
191 
192  if ( !addressBook.isValid() ) {
193  q->setError( UserDefinedError );
194  q->emitResult();
195  return;
196  }
197  KABC::Addressee contact;
198  contact.setNameFromString( mName );
199  contact.insertEmail( mEmail, true );
200 
201  // create the new item
202  Akonadi::Item item;
203  item.setMimeType( KABC::Addressee::mimeType() );
204  item.setPayload<KABC::Addressee>( contact );
205 
206  // save the new item in akonadi storage
207  Akonadi::ItemCreateJob *createJob = new Akonadi::ItemCreateJob( item, addressBook, q );
208  q->connect( createJob, SIGNAL(result(KJob*)), SLOT(slotAddContactDone(KJob*)) );
209  }
210 
211  void slotAddContactDone( KJob *job )
212  {
213  if ( job->error() ) {
214  q->setError( job->error() );
215  q->setErrorText( job->errorText() );
216  q->emitResult();
217  return;
218  }
219 
220  const Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob*>( job );
221  mItem = createJob->item();
222 
223  const QString text =
224  i18nc( "@info",
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) );
229 
230  if ( KMessageBox::questionYesNo(
231  mParentWidget,
232  text,
233  QString(),
234  KStandardGuiItem::yes(),
235  KStandardGuiItem::no(),
236  QLatin1String( "addedtokabc" ) ) == KMessageBox::Yes ) {
237  QPointer<Akonadi::ContactEditorDialog> dlg =
238  new Akonadi::ContactEditorDialog( Akonadi::ContactEditorDialog::EditMode,
239  mParentWidget );
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)) );
245  dlg->exec();
246  delete dlg;
247  }
248  q->emitResult();
249  }
250 
251  void slotContactEditorError(const QString &error)
252  {
253  KMessageBox::error(mParentWidget, i18n("Contact cannot be stored: %1", error), i18n("Failed to store contact"));
254  }
255 
256  void contactStored( const Akonadi::Item & )
257  {
258  KPIM::BroadcastStatus::instance()->setStatusMsg( i18n( "Contact created successfully" ) );
259  }
260 
261 
262  AddEmailAddressJob *q;
263  QString mCompleteAddress;
264  QString mEmail;
265  QString mName;
266  QWidget *mParentWidget;
267  Akonadi::Item mItem;
268 };
269 
270 AddEmailAddressJob::AddEmailAddressJob( const QString &email,
271  QWidget *parentWidget, QObject *parent )
272  : KJob( parent ), d( new Private( this, email, parentWidget ) )
273 {
274 }
275 
276 AddEmailAddressJob::~AddEmailAddressJob()
277 {
278  delete d;
279 }
280 
281 void AddEmailAddressJob::start()
282 {
283  // first check whether a contact with the same email exists already
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*)) );
289 }
290 
291 Akonadi::Item AddEmailAddressJob::contact() const
292 {
293  return d->mItem;
294 }
295 
296 #include "addemailaddressjob.moc"
KPIM::AddEmailAddressJob::contact
Akonadi::Item contact() const
Returns the item that represents the new contact.
Definition: addemailaddressjob.cpp:291
KPIM::BroadcastStatus::instance
static BroadcastStatus * instance()
Return the instance of the singleton object for this class.
Definition: broadcaststatus.cpp:44
KPIM::AddEmailAddressJob
A job to add a new contact with a given email address to Akonadi.
Definition: addemailaddressjob.h:40
selectedcollectiondialog.h
KPIM::AddEmailAddressJob::~AddEmailAddressJob
~AddEmailAddressJob()
Destroys the add email address job.
Definition: addemailaddressjob.cpp:276
QWidget
QObject
KPIM::BroadcastStatus::setStatusMsg
void setStatusMsg(const QString &message)
Emit an update status bar signal.
Definition: broadcaststatus.cpp:63
KPIM::AddEmailAddressJob::start
virtual void start()
Starts the job.
Definition: addemailaddressjob.cpp:281
KPIM::SelectedCollectionDialog
Definition: selectedcollectiondialog.h:36
addemailaddressjob.h
KPIM::AddEmailAddressJob::AddEmailAddressJob
AddEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent=0)
Creates a new add email address job.
Definition: addemailaddressjob.cpp:270
broadcaststatus.h
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal