Messagelib

savecontactpreferencejob.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "savecontactpreferencejob.h"
8
9#include <KJob>
10#include <KLocalizedString>
11
12#include <QInputDialog>
13
14#include <Akonadi/CollectionDialog>
15#include <Akonadi/ContactSearchJob>
16#include <Akonadi/ItemCreateJob>
17#include <Akonadi/ItemModifyJob>
18
19#include "messagecomposer_debug.h"
20#include <QPointer>
21
22using namespace MessageComposer;
23
24class MessageComposer::SaveContactPreferenceJobPrivate
25{
26public:
27 explicit SaveContactPreferenceJobPrivate(SaveContactPreferenceJob *qq, const QString &email, const ContactPreference &pref)
28 : q_ptr(qq)
29 , mEmail(email)
30 , mPref(pref)
31 {
32 }
33
34 SaveContactPreferenceJob *const q_ptr;
35 Q_DECLARE_PUBLIC(SaveContactPreferenceJob)
36
37 const QString mEmail;
38 const ContactPreference mPref;
39};
40
41SaveContactPreferenceJob::SaveContactPreferenceJob(const QString &email, const ContactPreference &pref, QObject *parent)
42 : QObject(parent)
43 , d_ptr(std::unique_ptr<SaveContactPreferenceJobPrivate>(new SaveContactPreferenceJobPrivate(this, email, pref)))
44{
45}
46
47SaveContactPreferenceJob::~SaveContactPreferenceJob() = default;
48
49void SaveContactPreferenceJob::start()
50{
51 Q_D(SaveContactPreferenceJob);
52
53 auto job = new Akonadi::ContactSearchJob(this);
54 connect(job, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotSearchContact);
55 job->setLimit(1);
56 job->setQuery(Akonadi::ContactSearchJob::Email, d->mEmail);
57 job->start();
58}
59
60void SaveContactPreferenceJob::slotSearchContact(KJob *job)
61{
62 Q_D(SaveContactPreferenceJob);
63 auto contactSearchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
64
65 const Akonadi::Item::List items = contactSearchJob->items();
66
67 if (items.isEmpty()) {
68 bool ok = true;
70 i18nc("@title:window", "Name Selection"),
71 i18n("Which name shall the contact '%1' have in your address book?", d->mEmail),
73 QString(),
74 &ok);
75 if (!ok) {
77 return;
78 }
79
80 QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog(Akonadi::CollectionDialog::KeepTreeExpanded);
81 dlg->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
82 dlg->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
83 dlg->setDescription(i18n("Select the address book folder to store the new contact in:"));
84 if (!dlg->exec()) {
85 delete dlg;
87 return;
88 }
89
90 const Akonadi::Collection targetCollection = dlg->selectedCollection();
91 delete dlg;
92
94 contact.setNameFromString(fullName);
95 KContacts::Email email(d->mEmail);
96 email.setPreferred(true);
97 contact.addEmail(email);
98 d->mPref.fillAddressee(contact);
99
101 item.setPayload<KContacts::Addressee>(contact);
102
103 auto createJob = new Akonadi::ItemCreateJob(item, targetCollection);
104 connect(createJob, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotModifyCreateItem);
105 } else {
106 Akonadi::Item item = items.first();
107
108 auto contact = item.payload<KContacts::Addressee>();
109 d->mPref.fillAddressee(contact);
110
111 item.setPayload<KContacts::Addressee>(contact);
112
113 auto itemModifyJob = new Akonadi::ItemModifyJob(item);
114 connect(itemModifyJob, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotModifyCreateItem);
115 }
116}
117
118void SaveContactPreferenceJob::slotModifyCreateItem(KJob *job)
119{
120 if (job->error()) {
121 qCDebug(MESSAGECOMPOSER_LOG) << "modify item failed " << job->errorString();
122 }
123 deleteLater();
124}
125
126#include "moc_savecontactpreferencejob.cpp"
T payload() const
void setPayload(const T &p)
void setNameFromString(const QString &s)
void addEmail(const Email &email)
static QString mimeType()
virtual QString errorString() const
int error() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString fullName(const PartType &type)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
T & first()
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
T qobject_cast(QObject *object)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 1 2024 18:49:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.