Messagelib

followupremindercreatejob.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 "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
20using namespace MessageComposer;
21class MessageComposer::FollowupReminderCreateJobPrivate
22{
23public:
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
33FollowupReminderCreateJob::FollowupReminderCreateJob(QObject *parent)
34 : KJob(parent)
35 , d(new MessageComposer::FollowupReminderCreateJobPrivate)
36{
37}
38
39FollowupReminderCreateJob::~FollowupReminderCreateJob() = default;
40
41void FollowupReminderCreateJob::setFollowUpReminderDate(const QDate &date)
42{
43 d->mFollowupDate = date;
44}
45
46void FollowupReminderCreateJob::setOriginalMessageItemId(Akonadi::Item::Id value)
47{
48 d->mOriginalMessageItemId = value;
49}
50
51void FollowupReminderCreateJob::setMessageId(const QString &messageId)
52{
53 d->mMessageId = messageId;
54}
55
56void FollowupReminderCreateJob::setTo(const QString &to)
57{
58 d->mTo = to;
59}
60
61void FollowupReminderCreateJob::setSubject(const QString &subject)
62{
63 d->mSubject = subject;
64}
65
66void FollowupReminderCreateJob::setCollectionToDo(const Akonadi::Collection &collection)
67{
68 d->mCollection = collection;
69}
70
71void FollowupReminderCreateJob::start()
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)));
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
94void 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
109void 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}
137
138#include "moc_followupremindercreatejob.cpp"
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
static QLatin1String todoMimeType()
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void result(KJob *job)
void setError(int errorCode)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QDBusConnection sessionBus()
QString message() const const
void finished(QDBusPendingCallWatcher *self)
QDBusError error() const const
bool isError() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.