Akonadi Contacts

openemailaddressjob.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 "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
19using namespace Akonadi;
20
21class Akonadi::OpenEmailAddressJobPrivate
22{
23public:
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 Akonadi::AddEmailAddressJob(mCompleteAddress, mParentWidget, q);
56 q->connect(createJob, &Akonadi::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 Akonadi::AddEmailAddressJob *createJob = qobject_cast<Akonadi::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
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"
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.
A dialog for creating or editing a contact in Akonadi.
@ EditMode
Edits an existing contact.
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.
void setLimit(int limit)
Sets a limit on how many results will be returned by this search job.
Item::List items() const
A job to open the contact editor for a contact with a given email address.
OpenEmailAddressJob(const QString &email, QWidget *parentWidget, QObject *parent=nullptr)
Creates a new open email address job.
void start() override
Starts the job.
~OpenEmailAddressJob() override
Destroys the open email address job.
static void parseEmailAddress(const QString &rawEmail, QString &fullName, QString &email)
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString errorText() const
A widget for editing the display name of a contact.
T & first()
bool isEmpty() const const
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 Mon Nov 4 2024 16:36:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.