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 "akonadi/contact/selectaddressbookdialog.h"
9 #include <Collection>
10 
11 #include <Akonadi/Contact/ContactSearchJob>
12 #include <KLocalizedString>
13 #include <KMessageBox>
14 #include <item.h>
15 #include <itemcreatejob.h>
16 #include <kcontacts/addressee.h>
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 = i18nc("@info",
55  "The vCard's primary email address is already in "
56  "your address book; however, you may save the vCard into "
57  "a file and import it into the address book manually.");
58  KMessageBox::information(mParentWidget, text);
59  }
60  q->setError(UserDefinedError);
61  q->emitResult();
62  return;
63  }
64 
65  if (!mCollection.isValid()) {
66  // ask user in which address book the new contact shall be stored
68 
69  bool gotIt = true;
70  if (!dlg->exec()) {
71  q->setError(UserDefinedError);
72  q->emitResult();
73  gotIt = false;
74  } else {
75  mCollection = dlg->selectedCollection();
76  }
77  delete dlg;
78  if (!gotIt) {
79  return;
80  }
81  }
82 
83  if (mCollection.isValid()) {
84  // create the new item
85  Akonadi::Item item;
87  item.setPayload<KContacts::Addressee>(mContact);
88 
89  // save the new item in akonadi storage
90  auto job = new Akonadi::ItemCreateJob(item, mCollection);
91  q->connect(job, &Akonadi::ItemCreateJob::result, q, [this](KJob *job) {
92  slotAddContactDone(job);
93  });
94  } else {
95  q->setError(UserDefinedError);
96  q->emitResult();
97  }
98  }
99 
100  void slotAddContactDone(KJob *job)
101  {
102  if (job->error()) {
103  q->setError(job->error());
104  q->setErrorText(job->errorText());
105  q->emitResult();
106  return;
107  }
108 
109  if (mShowMessageBox) {
110  const QString text = i18nc("@info",
111  "The vCard was added to your address book; "
112  "you can add more information to this "
113  "entry by opening the address book.");
114  KMessageBox::information(mParentWidget, text, QString(), QStringLiteral("addedtokabc"));
115  }
116  q->emitResult();
117  }
118 
119  AddContactJob *const q;
120  const KContacts::Addressee mContact;
121  QWidget *mParentWidget = nullptr;
122  Akonadi::Collection mCollection;
123  bool mShowMessageBox = true;
124 };
125 
126 AddContactJob::AddContactJob(const KContacts::Addressee &contact, QWidget *parentWidget, QObject *parent)
127  : KJob(parent)
128  , d(new Private(this, contact, parentWidget))
129 {
130 }
131 
133  : KJob(parent)
134  , d(new Private(this, contact, collection))
135 {
136 }
137 
139 {
140  delete d;
141 }
142 
143 void AddContactJob::showMessageBox(bool b)
144 {
145  d->mShowMessageBox = b;
146 }
147 
149 {
150  // first check whether a contact with the same email exists already
151  auto searchJob = new Akonadi::ContactSearchJob(this);
152  searchJob->setLimit(1);
153  searchJob->setQuery(Akonadi::ContactSearchJob::Email, d->mContact.preferredEmail().toLower(), Akonadi::ContactSearchJob::ExactMatch);
154 
155  connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) {
156  d->slotSearchDone(job);
157  });
158 }
159 
160 #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.
void setPayload(const T &p)
void setMimeType(const QString &mimeType)
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:27
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-2021 The KDE developers.
Generated on Thu Jun 17 2021 23:09:01 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.