File indexing completed on 2024-10-27 04:51:06

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 "createfollowupreminderonexistingmessagejob.h"
0008 #include "kmail_debug.h"
0009 #include <Akonadi/ItemFetchJob>
0010 #include <Akonadi/ItemFetchScope>
0011 #include <KMime/Message>
0012 #include <MessageComposer/FollowupReminderCreateJob>
0013 
0014 CreateFollowupReminderOnExistingMessageJob::CreateFollowupReminderOnExistingMessageJob(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 CreateFollowupReminderOnExistingMessageJob::~CreateFollowupReminderOnExistingMessageJob() = default;
0020 
0021 void CreateFollowupReminderOnExistingMessageJob::start()
0022 {
0023     if (canStart()) {
0024         doStart();
0025     } else {
0026         qCDebug(KMAIL_LOG) << " job can not started";
0027         deleteLater();
0028     }
0029 }
0030 
0031 void CreateFollowupReminderOnExistingMessageJob::doStart()
0032 {
0033     auto job = new Akonadi::ItemFetchJob(mMessageItem, this);
0034     job->fetchScope().fetchFullPayload(true);
0035     connect(job, &KJob::result, this, &CreateFollowupReminderOnExistingMessageJob::itemFetchJobDone);
0036 }
0037 
0038 void CreateFollowupReminderOnExistingMessageJob::itemFetchJobDone(KJob *job)
0039 {
0040     auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
0041     if (fetchJob->items().count() == 1) {
0042         mMessageItem = fetchJob->items().constFirst();
0043     } else {
0044         qCDebug(KMAIL_LOG) << " CreateFollowupReminderOnExistingMessageJob Error during fetch: " << job->errorString();
0045         deleteLater();
0046         return;
0047     }
0048     if (!mMessageItem.hasPayload<KMime::Message::Ptr>()) {
0049         qCDebug(KMAIL_LOG) << " item has not payload";
0050         deleteLater();
0051         return;
0052     }
0053     auto msg = mMessageItem.payload<KMime::Message::Ptr>();
0054     if (msg) {
0055         auto reminderJob = new MessageComposer::FollowupReminderCreateJob(this);
0056         KMime::Headers::MessageID *messageID = msg->messageID(false);
0057         if (messageID) {
0058             const QString messageIdStr = messageID->asUnicodeString();
0059             reminderJob->setMessageId(messageIdStr);
0060         } else {
0061             qCDebug(KMAIL_LOG) << " missing messageId";
0062             delete reminderJob;
0063             deleteLater();
0064             return;
0065         }
0066 
0067         reminderJob->setFollowUpReminderDate(mDate);
0068         reminderJob->setCollectionToDo(mCollection);
0069         reminderJob->setOriginalMessageItemId(mMessageItem.id());
0070         KMime::Headers::To *to = msg->to(false);
0071         if (to) {
0072             reminderJob->setTo(to->asUnicodeString());
0073         }
0074         KMime::Headers::Subject *subject = msg->subject(false);
0075         if (subject) {
0076             reminderJob->setSubject(subject->asUnicodeString());
0077         }
0078 
0079         connect(reminderJob, &KJob::result, this, &CreateFollowupReminderOnExistingMessageJob::slotReminderDone);
0080         reminderJob->start();
0081     } else {
0082         qCDebug(KMAIL_LOG) << " no message found";
0083         deleteLater();
0084     }
0085 }
0086 
0087 void CreateFollowupReminderOnExistingMessageJob::slotReminderDone(KJob *job)
0088 {
0089     if (job->error()) {
0090         qCDebug(KMAIL_LOG) << "CreateFollowupReminderOnExistingMessageJob::slotReminderDone  :" << job->errorString();
0091     } else {
0092         // TODO update dialog if opened
0093     }
0094     deleteLater();
0095 }
0096 
0097 Akonadi::Collection CreateFollowupReminderOnExistingMessageJob::collection() const
0098 {
0099     return mCollection;
0100 }
0101 
0102 void CreateFollowupReminderOnExistingMessageJob::setCollection(const Akonadi::Collection &collection)
0103 {
0104     mCollection = collection;
0105 }
0106 
0107 QDate CreateFollowupReminderOnExistingMessageJob::date() const
0108 {
0109     return mDate;
0110 }
0111 
0112 void CreateFollowupReminderOnExistingMessageJob::setDate(QDate date)
0113 {
0114     mDate = date;
0115 }
0116 
0117 Akonadi::Item CreateFollowupReminderOnExistingMessageJob::messageItem() const
0118 {
0119     return mMessageItem;
0120 }
0121 
0122 void CreateFollowupReminderOnExistingMessageJob::setMessageItem(const Akonadi::Item &messageItem)
0123 {
0124     mMessageItem = messageItem;
0125 }
0126 
0127 bool CreateFollowupReminderOnExistingMessageJob::canStart() const
0128 {
0129     return mMessageItem.isValid() && mCollection.isValid() && mDate.isValid();
0130 }
0131 
0132 #include "moc_createfollowupreminderonexistingmessagejob.cpp"