Messagelib

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

KDE's Doxygen guidelines are available online.