Akonadi Contacts

addcontactjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "addcontactjob.h"
8 #include <Collection>
9 #include "akonadi/contact/selectaddressbookdialog.h"
10 
11 #include <Akonadi/Contact/ContactSearchJob>
12 #include <item.h>
13 #include <itemcreatejob.h>
14 #include <kcontacts/addressee.h>
15 #include <KLocalizedString>
16 #include <KMessageBox>
17 
18 #include <QPointer>
19 
20 using namespace Akonadi;
21 
22 class Q_DECL_HIDDEN AddContactJob::Private
23 {
24 public:
25  Private(AddContactJob *qq, const KContacts::Addressee &contact, QWidget *parentWidget)
26  : q(qq)
27  , mContact(contact)
28  , mParentWidget(parentWidget)
29  {
30  }
31 
32  Private(AddContactJob *qq, const KContacts::Addressee &contact, const Akonadi::Collection &collection)
33  : q(qq)
34  , mContact(contact)
35  , mCollection(collection)
36  {
37  }
38 
39  void slotSearchDone(KJob *job)
40  {
41  if (job->error()) {
42  q->setError(job->error());
43  q->setErrorText(job->errorText());
44  q->emitResult();
45  return;
46  }
47 
48  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
49 
50  const KContacts::Addressee::List contacts = searchJob->contacts();
51 
52  if (!contacts.isEmpty()) { // contact is already part of the address book...
53  if (mShowMessageBox) {
54  const QString text
55  = i18nc("@info",
56  "The vCard's primary email address is already in "
57  "your address book; however, you may save the vCard into "
58  "a file and import it into the address book manually.");
59  KMessageBox::information(mParentWidget, text);
60  }
61  q->setError(UserDefinedError);
62  q->emitResult();
63  return;
64  }
65 
66  if (!mCollection.isValid()) {
67  // ask user in which address book the new contact shall be stored
69 
70  bool gotIt = true;
71  if (!dlg->exec()) {
72  q->setError(UserDefinedError);
73  q->emitResult();
74  gotIt = false;
75  } else {
76  mCollection = dlg->selectedCollection();
77  }
78  delete dlg;
79  if (!gotIt) {
80  return;
81  }
82  }
83 
84  if (mCollection.isValid()) {
85  // create the new item
86  Akonadi::Item item;
87  item.setMimeType(KContacts::Addressee::mimeType());
88  item.setPayload<KContacts::Addressee>(mContact);
89 
90  // save the new item in akonadi storage
91  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob(item, mCollection);
92  q->connect(job, &Akonadi::ItemCreateJob::result, q, [this](KJob *job) {
93  slotAddContactDone(job);
94  });
95  } else {
96  q->setError(UserDefinedError);
97  q->emitResult();
98  }
99  }
100 
101  void slotAddContactDone(KJob *job)
102  {
103  if (job->error()) {
104  q->setError(job->error());
105  q->setErrorText(job->errorText());
106  q->emitResult();
107  return;
108  }
109 
110  if (mShowMessageBox) {
111  const QString text
112  = i18nc("@info",
113  "The vCard was added to your address book; "
114  "you can add more information to this "
115  "entry by opening the address book.");
117  mParentWidget,
118  text,
119  QString(),
120  QStringLiteral("addedtokabc"));
121  }
122  q->emitResult();
123  }
124 
125  AddContactJob *const q;
126  KContacts::Addressee mContact;
127  QWidget *mParentWidget = nullptr;
128  Akonadi::Collection mCollection;
129  bool mShowMessageBox = true;
130 };
131 
132 AddContactJob::AddContactJob(const KContacts::Addressee &contact, QWidget *parentWidget, QObject *parent)
133  : KJob(parent)
134  , d(new Private(this, contact, parentWidget))
135 {
136 }
137 
139  : KJob(parent)
140  , d(new Private(this, contact, collection))
141 {
142 }
143 
145 {
146  delete d;
147 }
148 
149 void AddContactJob::showMessageBox(bool b)
150 {
151  d->mShowMessageBox = b;
152 }
153 
155 {
156  // first check whether a contact with the same email exists already
158  searchJob->setLimit(1);
159  searchJob->setQuery(Akonadi::ContactSearchJob::Email, d->mContact.preferredEmail().toLower(),
161 
162  connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) {
163  d->slotSearchDone(job);
164  });
165 }
166 
167 #include "moc_addcontactjob.cpp"
static QString mimeType()
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
The SelectAddressBookDialog class This class allows to select addressbook where saving contacts...
~AddContactJob() override
Destroys the add email address job.
void start() override
Starts the job.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AddContactJob(const KContacts::Addressee &contact, QWidget *parentWidget, QObject *parent=nullptr)
Creates a new add contact job.
Job that searches for contacts in the Akonadi storage.
A job to add a new contact to Akonadi.
Definition: addcontactjob.h:26
The email address of the contact.
KContacts::Addressee::List contacts() const
Returns the contacts that matched the search criteria.
AddresseeList List
void setQuery(Criterion criterion, const QString &value, Match match=ExactMatch)
Sets the criterion and value for the search with match.
void result(KJob *job)
void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
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-2020 The KDE developers.
Generated on Sat Sep 19 2020 23:07:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.