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/CollectionDialog>
12 #include <Akonadi/ContactEditorDialog>
13 #include <Akonadi/ContactSearchJob>
14 #include <Akonadi/Item>
15 #include <Akonadi/ItemCreateJob>
16 #include <KContacts/Addressee>
17 #include <QPointer>
18 
19 using namespace Akonadi;
20 
21 class Akonadi::OpenEmailAddressJobPrivate
22 {
23 public:
24  OpenEmailAddressJobPrivate(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  const 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 OpenEmailAddressJobPrivate(this, email, parentWidget))
92 {
93 }
94 
96 
98 {
99  // first check whether a contact with the same email exists already
100  auto searchJob = new Akonadi::ContactSearchJob(this);
101  searchJob->setLimit(1);
103  connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) {
104  d->slotSearchDone(job);
105  });
106 }
107 
108 #include "moc_openemailaddressjob.cpp"
bool isEmpty() const const
OpenEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent=nullptr)
Creates a new open email address job.
Item::List items() const
void result(KJob *job)
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
A job to add a new contact with a given email address to Akonadi.
Akonadi::Item contact() const
Returns the item that represents the new contact.
T & first()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setQuery(Criterion criterion, const QString &value, Match match=ExactMatch)
Sets the criterion and value for the search with match.
static void parseEmailAddress(const QString &rawEmail, QString &fullName, QString &email)
@ Email
The email address of the contact.
QString errorText() const
A job to open the contact editor for a contact with a given email address.
Job that searches for contacts in the Akonadi storage.
A dialog for creating or editing a contact in Akonadi.
@ EditMode
Edits an existing contact.
~OpenEmailAddressJob() override
Destroys the open email address job.
void start() override
Starts the job.
int error() const
@ ExactMatch
The result must match exactly the pattern (case sensitive).
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.