Messagelib

followupremindercreatejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "followupremindercreatejob.h"
8 #include "followupreminderinterface.h"
9 #include "messagecomposer_debug.h"
10 
11 #include <Akonadi/ItemCreateJob>
12 #include <Akonadi/ServerManager>
13 #include <KCalendarCore/Todo>
14 #include <KLocalizedString>
15 
16 #include <QDBusConnection>
17 #include <QDBusPendingCallWatcher>
18 #include <QDBusPendingReply>
19 
20 using namespace MessageComposer;
21 class MessageComposer::FollowupReminderCreateJobPrivate
22 {
23 public:
24  Akonadi::Collection mCollection;
25  QDate mFollowupDate;
26  Akonadi::Item::Id mOriginalMessageItemId = -1;
27  Akonadi::Item::Id mTodoId = -1;
28  QString mMessageId;
29  QString mSubject;
30  QString mTo;
31 };
32 
33 FollowupReminderCreateJob::FollowupReminderCreateJob(QObject *parent)
34  : KJob(parent)
35  , d(new MessageComposer::FollowupReminderCreateJobPrivate)
36 {
37 }
38 
39 FollowupReminderCreateJob::~FollowupReminderCreateJob() = default;
40 
41 void FollowupReminderCreateJob::setFollowUpReminderDate(const QDate &date)
42 {
43  d->mFollowupDate = date;
44 }
45 
46 void FollowupReminderCreateJob::setOriginalMessageItemId(Akonadi::Item::Id value)
47 {
48  d->mOriginalMessageItemId = value;
49 }
50 
51 void FollowupReminderCreateJob::setMessageId(const QString &messageId)
52 {
53  d->mMessageId = messageId;
54 }
55 
56 void FollowupReminderCreateJob::setTo(const QString &to)
57 {
58  d->mTo = to;
59 }
60 
61 void FollowupReminderCreateJob::setSubject(const QString &subject)
62 {
63  d->mSubject = subject;
64 }
65 
66 void FollowupReminderCreateJob::setCollectionToDo(const Akonadi::Collection &collection)
67 {
68  d->mCollection = collection;
69 }
70 
72 {
73  if (!d->mMessageId.isEmpty() && d->mFollowupDate.isValid() && !d->mTo.isEmpty()) {
74  if (d->mCollection.isValid()) {
76  todo->setSummary(i18n("Wait answer from \"%1\" send to \"%2\"", d->mSubject, d->mTo));
77  todo->setDtDue(QDateTime(d->mFollowupDate, QTime(0, 0, 0)));
78  Akonadi::Item newTodoItem;
81 
82  auto createJob = new Akonadi::ItemCreateJob(newTodoItem, d->mCollection);
83  connect(createJob, &Akonadi::ItemCreateJob::result, this, &FollowupReminderCreateJob::slotCreateNewTodo);
84  } else {
85  writeFollowupReminderInfo();
86  }
87  } else {
88  qCWarning(MESSAGECOMPOSER_LOG) << "FollowupReminderCreateJob info not valid!";
89  emitResult();
90  return;
91  }
92 }
93 
94 void FollowupReminderCreateJob::slotCreateNewTodo(KJob *job)
95 {
96  if (job->error()) {
97  qCWarning(MESSAGECOMPOSER_LOG) << "Error during create new Todo " << job->errorString();
98  setError(job->error());
99  setErrorText(i18n("Failed to store a new reminder: an error occurred while trying to create a new to-do in your calendar: %1", job->errorString()));
100  emitResult();
101  return;
102  }
103 
104  auto createJob = qobject_cast<Akonadi::ItemCreateJob *>(job);
105  d->mTodoId = createJob->item().id();
106  writeFollowupReminderInfo();
107 }
108 
109 void FollowupReminderCreateJob::writeFollowupReminderInfo()
110 {
111  std::unique_ptr<org::freedesktop::Akonadi::FollowUpReminderAgent> iface{new org::freedesktop::Akonadi::FollowUpReminderAgent{
112  Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Agent, QStringLiteral("akonadi_followupreminder_agent")),
113  QStringLiteral("/FollowUpReminder"),
115 
116  if (!iface->isValid()) {
117  qCWarning(MESSAGECOMPOSER_LOG) << "The FollowUpReminder agent is not running!";
118  return;
119  }
120 
121  auto call = iface->addReminder(d->mMessageId, d->mOriginalMessageItemId, d->mTo, d->mSubject, d->mFollowupDate, d->mTodoId);
122  auto wait = new QDBusPendingCallWatcher{call, this};
123  connect(wait, &QDBusPendingCallWatcher::finished, this, [this, iface_ = std::move(iface)](QDBusPendingCallWatcher *watcher) mutable {
124  auto iface = std::move(iface_);
125  watcher->deleteLater();
126 
127  const QDBusPendingReply<void> reply = *watcher;
128  if (reply.isError()) {
129  qCWarning(MESSAGECOMPOSER_LOG) << "Failed to write the new reminder, agent replied" << reply.error().message();
130  setError(KJob::UserDefinedError);
131  setErrorText(i18n("Failed to store a new reminder: %1", reply.error().message()));
132  }
133 
134  emitResult();
135  });
136 }
void finished(QDBusPendingCallWatcher *self)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void result(KJob *job)
QString message() const const
static QLatin1String todoMimeType()
Q_SCRIPTABLE Q_NOREPLY void start()
void setMimeType(const QString &mimeType)
bool isError() const const
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
void deleteLater()
QString i18n(const char *text, const TYPE &arg...)
QDBusConnection sessionBus()
QDBusError error() const const
virtual QString errorString() const
int error() const
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
void setPayload(const T &p)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.