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 
22 using namespace MessageComposer;
23 
24 class MessageComposer::SaveContactPreferenceJobPrivate
25 {
26 public:
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 *q_ptr;
35  Q_DECLARE_PUBLIC(SaveContactPreferenceJob)
36 
37  const QString mEmail;
38  const ContactPreference mPref;
39 };
40 
41 SaveContactPreferenceJob::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 
47 SaveContactPreferenceJob::~SaveContactPreferenceJob() = default;
48 
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 
60 void 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;
69  const QString fullName = QInputDialog::getText(nullptr,
70  i18n("Name Selection"),
71  i18n("Which name shall the contact '%1' have in your address book?", d->mEmail),
73  QString(),
74  &ok);
75  if (!ok) {
76  deleteLater();
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;
86  deleteLater();
87  return;
88  }
89 
90  const Akonadi::Collection targetCollection = dlg->selectedCollection();
91  delete dlg;
92 
93  KContacts::Addressee contact;
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 
118 void 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 & first()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
Q_SCRIPTABLE Q_NOREPLY void start()
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)
bool isEmpty() const const
void setNameFromString(const QString &s)
void addEmail(const Email &email)
void setPayload(const T &p)
virtual QString errorString() const
int error() const
T payload() const
QString fullName(const PartType &type)
Q_D(Todo)
static QString mimeType()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.