Akonadi Contacts

addcontactjob.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "addcontactjob.h"
8#include "widgets/selectaddressbookdialog.h"
9#include <Akonadi/Collection>
10
11#include <Akonadi/ContactSearchJob>
12#include <Akonadi/Item>
13#include <Akonadi/ItemCreateJob>
14#include <KContacts/Addressee>
15#include <KLocalizedString>
16#include <KMessageBox>
17
18#include <QPointer>
19
20using namespace Akonadi;
21
22class Akonadi::AddContactJobPrivate
23{
24public:
25 AddContactJobPrivate(AddContactJob *qq, const KContacts::Addressee &contact, QWidget *parentWidget)
26 : q(qq)
27 , mContact(contact)
28 , mParentWidget(parentWidget)
29 {
30 }
31
32 AddContactJobPrivate(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(KJob::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(KJob::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(KJob::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
127 : KJob(parent)
128 , d(new AddContactJobPrivate(this, contact, parentWidget))
129{
130}
131
133 : KJob(parent)
134 , d(new AddContactJobPrivate(this, contact, collection))
135{
136}
137
139
140void AddContactJob::showMessageBox(bool b)
141{
142 d->mShowMessageBox = b;
143}
144
146{
147 // first check whether a contact with the same email exists already
148 auto searchJob = new Akonadi::ContactSearchJob(this);
149 searchJob->setLimit(1);
150 searchJob->setQuery(Akonadi::ContactSearchJob::Email, d->mContact.preferredEmail().toLower(), Akonadi::ContactSearchJob::ExactMatch);
151
152 connect(searchJob, &Akonadi::ContactSearchJob::result, this, [this](KJob *job) {
153 d->slotSearchDone(job);
154 });
155}
156
157#include "moc_addcontactjob.cpp"
A job to add a new contact to Akonadi.
~AddContactJob() override
Destroys the add email address job.
void start() override
Starts the job.
AddContactJob(const KContacts::Addressee &contact, QWidget *parentWidget, QObject *parent=nullptr)
Creates a new add contact job.
bool isValid() const
Job that searches for contacts in the Akonadi storage.
void setQuery(Criterion criterion, const QString &value, Match match=ExactMatch)
Sets the criterion and value for the search with match.
@ ExactMatch
The result must match exactly the pattern (case sensitive).
@ Email
The email address of the contact.
KContacts::Addressee::List contacts() const
Returns the contacts that matched the search criteria.
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
void setMimeType(const QString &mimeType)
void setPayload(const T &p)
The SelectAddressBookDialog class This class allows to select addressbook where saving contacts.
AddresseeList List
static QString mimeType()
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString errorText() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.