Messagelib

savecontactpreferencejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <[email protected]>
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/Contact/ContactSearchJob>
16 #include <Akonadi/ItemCreateJob>
17 #include <Akonadi/ItemModifyJob>
18 
19 #include "messagecomposer_debug.h"
20 #include <QPointer>
21 #include <kcontacts_version.h>
22 
23 using namespace MessageComposer;
24 
25 SaveContactPreferenceJob::SaveContactPreferenceJob(const QString &email, const Kleo::KeyResolver::ContactPreferences &pref, QObject *parent)
26  : QObject(parent)
27  , mEmail(email)
28  , mPref(pref)
29 {
30 }
31 
32 SaveContactPreferenceJob::~SaveContactPreferenceJob() = default;
33 
34 void SaveContactPreferenceJob::start()
35 {
36  auto job = new Akonadi::ContactSearchJob(this);
37  connect(job, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotSearchContact);
38  job->setLimit(1);
39  job->setQuery(Akonadi::ContactSearchJob::Email, mEmail);
40  job->start();
41 }
42 
43 void SaveContactPreferenceJob::slotSearchContact(KJob *job)
44 {
45  auto contactSearchJob = qobject_cast<Akonadi::ContactSearchJob *>(job);
46 
47  const Akonadi::Item::List items = contactSearchJob->items();
48 
49  if (items.isEmpty()) {
50  bool ok = true;
51  const QString fullName = QInputDialog::getText(nullptr,
52  i18n("Name Selection"),
53  i18n("Which name shall the contact '%1' have in your address book?", mEmail),
55  QString(),
56  &ok);
57  if (!ok) {
58  deleteLater();
59  return;
60  }
61 
62  QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog(Akonadi::CollectionDialog::KeepTreeExpanded);
63  dlg->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
64  dlg->setAccessRightsFilter(Akonadi::Collection::CanCreateItem);
65  dlg->setDescription(i18n("Select the address book folder to store the new contact in:"));
66  if (!dlg->exec()) {
67  delete dlg;
68  deleteLater();
69  return;
70  }
71 
72  const Akonadi::Collection targetCollection = dlg->selectedCollection();
73  delete dlg;
74 
75  KContacts::Addressee contact;
76  contact.setNameFromString(fullName);
77 #if KContacts_VERSION < QT_VERSION_CHECK(5, 88, 0)
78  contact.insertEmail(mEmail, true);
79 #else
80  KContacts::Email email(mEmail);
81  email.setPreferred(true);
82  contact.addEmail(email);
83 #endif
84  writeCustomContactProperties(contact, mPref);
85 
87  item.setPayload<KContacts::Addressee>(contact);
88 
89  auto createJob = new Akonadi::ItemCreateJob(item, targetCollection);
90  connect(createJob, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotModifyCreateItem);
91  } else {
92  Akonadi::Item item = items.first();
93 
94  auto contact = item.payload<KContacts::Addressee>();
95  writeCustomContactProperties(contact, mPref);
96 
97  item.setPayload<KContacts::Addressee>(contact);
98 
99  auto itemModifyJob = new Akonadi::ItemModifyJob(item);
100  connect(itemModifyJob, &Akonadi::ContactSearchJob::result, this, &SaveContactPreferenceJob::slotModifyCreateItem);
101  }
102 }
103 
104 void SaveContactPreferenceJob::writeCustomContactProperties(KContacts::Addressee &contact, const Kleo::KeyResolver::ContactPreferences &pref) const
105 {
106  contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
107  QStringLiteral("CRYPTOENCRYPTPREF"),
108  QLatin1String(Kleo::encryptionPreferenceToString(pref.encryptionPreference)));
109  contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
110  QStringLiteral("CRYPTOSIGNPREF"),
111  QLatin1String(Kleo::signingPreferenceToString(pref.signingPreference)));
112  contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
113  QStringLiteral("CRYPTOPROTOPREF"),
114  QLatin1String(cryptoMessageFormatToString(pref.cryptoMessageFormat)));
115  contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("OPENPGPFP"), pref.pgpKeyFingerprints.join(QLatin1Char(',')));
116  contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("SMIMEFP"), pref.smimeCertFingerprints.join(QLatin1Char(',')));
117 }
118 
119 void SaveContactPreferenceJob::slotModifyCreateItem(KJob *job)
120 {
121  if (job->error()) {
122  qCDebug(MESSAGECOMPOSER_LOG) << "modify item failed " << job->errorString();
123  }
124  deleteLater();
125 }
static QString mimeType()
virtual QString errorString() const
void setPayload(const T &p)
T payload() const
void insertEmail(const QString &email, bool preferred=false, const QMap< QString, QStringList > &param=QMap< QString, QStringList >())
QString fullName(const PartType &type)
void addEmail(const Email &email)
void setNameFromString(const QString &s)
void deleteLater()
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
QString i18n(const char *text, const TYPE &arg...)
void insertCustom(const QString &app, const QString &name, const QString &value)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.