File indexing completed on 2024-06-23 05:18:29

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "followupremindercreatejob.h"
0008 #include "followupreminderinterface.h"
0009 #include "messagecomposer_debug.h"
0010 
0011 #include <Akonadi/ItemCreateJob>
0012 #include <Akonadi/ServerManager>
0013 #include <KCalendarCore/Todo>
0014 #include <KLocalizedString>
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusPendingCallWatcher>
0018 #include <QDBusPendingReply>
0019 
0020 using namespace MessageComposer;
0021 class MessageComposer::FollowupReminderCreateJobPrivate
0022 {
0023 public:
0024     Akonadi::Collection mCollection;
0025     QDate mFollowupDate;
0026     Akonadi::Item::Id mOriginalMessageItemId = -1;
0027     Akonadi::Item::Id mTodoId = -1;
0028     QString mMessageId;
0029     QString mSubject;
0030     QString mTo;
0031 };
0032 
0033 FollowupReminderCreateJob::FollowupReminderCreateJob(QObject *parent)
0034     : KJob(parent)
0035     , d(new MessageComposer::FollowupReminderCreateJobPrivate)
0036 {
0037 }
0038 
0039 FollowupReminderCreateJob::~FollowupReminderCreateJob() = default;
0040 
0041 void FollowupReminderCreateJob::setFollowUpReminderDate(const QDate &date)
0042 {
0043     d->mFollowupDate = date;
0044 }
0045 
0046 void FollowupReminderCreateJob::setOriginalMessageItemId(Akonadi::Item::Id value)
0047 {
0048     d->mOriginalMessageItemId = value;
0049 }
0050 
0051 void FollowupReminderCreateJob::setMessageId(const QString &messageId)
0052 {
0053     d->mMessageId = messageId;
0054 }
0055 
0056 void FollowupReminderCreateJob::setTo(const QString &to)
0057 {
0058     d->mTo = to;
0059 }
0060 
0061 void FollowupReminderCreateJob::setSubject(const QString &subject)
0062 {
0063     d->mSubject = subject;
0064 }
0065 
0066 void FollowupReminderCreateJob::setCollectionToDo(const Akonadi::Collection &collection)
0067 {
0068     d->mCollection = collection;
0069 }
0070 
0071 void FollowupReminderCreateJob::start()
0072 {
0073     if (!d->mMessageId.isEmpty() && d->mFollowupDate.isValid() && !d->mTo.isEmpty()) {
0074         if (d->mCollection.isValid()) {
0075             KCalendarCore::Todo::Ptr todo(new KCalendarCore::Todo);
0076             todo->setSummary(i18n("Wait answer from \"%1\" send to \"%2\"", d->mSubject, d->mTo));
0077             todo->setDtDue(QDateTime(d->mFollowupDate, QTime(0, 0, 0)));
0078             Akonadi::Item newTodoItem;
0079             newTodoItem.setMimeType(KCalendarCore::Todo::todoMimeType());
0080             newTodoItem.setPayload<KCalendarCore::Todo::Ptr>(todo);
0081 
0082             auto createJob = new Akonadi::ItemCreateJob(newTodoItem, d->mCollection);
0083             connect(createJob, &Akonadi::ItemCreateJob::result, this, &FollowupReminderCreateJob::slotCreateNewTodo);
0084         } else {
0085             writeFollowupReminderInfo();
0086         }
0087     } else {
0088         qCWarning(MESSAGECOMPOSER_LOG) << "FollowupReminderCreateJob info not valid!";
0089         emitResult();
0090         return;
0091     }
0092 }
0093 
0094 void FollowupReminderCreateJob::slotCreateNewTodo(KJob *job)
0095 {
0096     if (job->error()) {
0097         qCWarning(MESSAGECOMPOSER_LOG) << "Error during create new Todo " << job->errorString();
0098         setError(job->error());
0099         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()));
0100         emitResult();
0101         return;
0102     }
0103 
0104     auto createJob = qobject_cast<Akonadi::ItemCreateJob *>(job);
0105     d->mTodoId = createJob->item().id();
0106     writeFollowupReminderInfo();
0107 }
0108 
0109 void FollowupReminderCreateJob::writeFollowupReminderInfo()
0110 {
0111     std::unique_ptr<org::freedesktop::Akonadi::FollowUpReminderAgent> iface{new org::freedesktop::Akonadi::FollowUpReminderAgent{
0112         Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Agent, QStringLiteral("akonadi_followupreminder_agent")),
0113         QStringLiteral("/FollowUpReminder"),
0114         QDBusConnection::sessionBus()}};
0115 
0116     if (!iface->isValid()) {
0117         qCWarning(MESSAGECOMPOSER_LOG) << "The FollowUpReminder agent is not running!";
0118         return;
0119     }
0120 
0121     auto call = iface->addReminder(d->mMessageId, d->mOriginalMessageItemId, d->mTo, d->mSubject, d->mFollowupDate, d->mTodoId);
0122     auto wait = new QDBusPendingCallWatcher{call, this};
0123     connect(wait, &QDBusPendingCallWatcher::finished, this, [this, iface_ = std::move(iface)](QDBusPendingCallWatcher *watcher) mutable {
0124         auto iface = std::move(iface_);
0125         watcher->deleteLater();
0126 
0127         const QDBusPendingReply<void> reply = *watcher;
0128         if (reply.isError()) {
0129             qCWarning(MESSAGECOMPOSER_LOG) << "Failed to write the new reminder, agent replied" << reply.error().message();
0130             setError(KJob::UserDefinedError);
0131             setErrorText(i18n("Failed to store a new reminder: %1", reply.error().message()));
0132         }
0133 
0134         emitResult();
0135     });
0136 }
0137 
0138 #include "moc_followupremindercreatejob.cpp"