Akonadi Contacts

openemailaddressjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "openemailaddressjob.h"
8 
9 #include "job/addemailaddressjob.h"
10 
11 #include <Akonadi/Contact/ContactEditorDialog>
12 #include <Akonadi/Contact/ContactSearchJob>
13 #include <QPointer>
14 #include <collectiondialog.h>
15 #include <item.h>
16 #include <itemcreatejob.h>
17 #include <kcontacts/addressee.h>
18 
19 using namespace Akonadi;
20 
21 class Q_DECL_HIDDEN OpenEmailAddressJob::Private
22 {
23 public:
24  Private(OpenEmailAddressJob *qq, const QString &emailString, QWidget *parentWidget)
25  : q(qq)
26  , mCompleteAddress(emailString)
27  , mParentWidget(parentWidget)
28  {
29  KContacts::Addressee::parseEmailAddress(emailString, mName, mEmail);
30  }
31 
32  void slotSearchDone(KJob *job)
33  {
34  if (job->error()) {
35  q->setError(job->error());
36  q->setErrorText(job->errorText());
37  q->emitResult();
38  return;
39  }
40 
41  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
42 
43  const Akonadi::Item::List contacts = searchJob->items();
44  if (!contacts.isEmpty()) {
45  // open the editor with the matching item
47  dlg->setContact(contacts.first());
48  dlg->exec();
49  delete dlg;
50 
51  q->emitResult();
52  return;
53  }
54 
55  auto createJob = new AddEmailAddressJob(mCompleteAddress, mParentWidget, q);
56  q->connect(createJob, &AddEmailAddressJob::result, q, [this](KJob *job) {
57  slotAddContactDone(job);
58  });
59  createJob->start();
60  }
61 
62  void slotAddContactDone(KJob *job)
63  {
64  if (job->error()) {
65  q->setError(job->error());
66  q->setErrorText(job->errorText());
67  q->emitResult();
68  return;
69  }
70 
71  const AddEmailAddressJob *createJob = qobject_cast<AddEmailAddressJob *>(job);
72 
73  // open the editor with the matching item
75  dlg->setContact(createJob->contact());
76  dlg->exec();
77  delete dlg;
78 
79  q->emitResult();
80  }
81 
82  OpenEmailAddressJob *const q;
83  QString mCompleteAddress;
84  QString mEmail;
85  QString mName;
86  QWidget *const mParentWidget;
87 };
88 
89 OpenEmailAddressJob::OpenEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent)
90  : KJob(parent)
91  , d(new Private(this, email, parentWidget))
92 {
93 }
94 
96 {
97  delete d;
98 }
99 
101 {
102  // first check whether a contact with the same email exists already
103  auto searchJob = new Akonadi::ContactSearchJob(this);
104  searchJob->setLimit(1);
106  connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) {
107  d->slotSearchDone(job);
108  });
109 }
110 
111 #include "moc_openemailaddressjob.cpp"
A dialog for creating or editing a contact in Akonadi.
void start() override
Starts the job.
Item::List items() const
OpenEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent=nullptr)
Creates a new open email address job.
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
static void parseEmailAddress(const QString &rawEmail, QString &fullName, QString &email)
A job to open the contact editor for a contact with a given email address.
Job that searches for contacts in the Akonadi storage.
Akonadi::Item contact() const
Returns the item that represents the new contact.
A job to add a new contact with a given email address to Akonadi.
The email address of the contact.
~OpenEmailAddressJob() override
Destroys the open email address job.
void setQuery(Criterion criterion, const QString &value, Match match=ExactMatch)
Sets the criterion and value for the search with match.
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString errorText() const
int error() const
The result must match exactly the pattern (case sensitive).
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.