File indexing completed on 2025-05-04 04:54:54

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 "followupreminderjob.h"
0008 
0009 #include <Akonadi/ItemFetchJob>
0010 #include <Akonadi/ItemFetchScope>
0011 #include <Akonadi/MessageParts>
0012 
0013 #include <KMime/Message>
0014 
0015 #include "followupreminderagent_debug.h"
0016 
0017 FollowUpReminderJob::FollowUpReminderJob(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 FollowUpReminderJob::~FollowUpReminderJob() = default;
0023 
0024 void FollowUpReminderJob::start()
0025 {
0026     if (!mItem.isValid()) {
0027         qCDebug(FOLLOWUPREMINDERAGENT_LOG) << " item is not valid";
0028         deleteLater();
0029         return;
0030     }
0031     auto job = new Akonadi::ItemFetchJob(mItem);
0032     job->fetchScope().fetchPayloadPart(Akonadi::MessagePart::Envelope, true);
0033     job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
0034 
0035     connect(job, &Akonadi::ItemFetchJob::result, this, &FollowUpReminderJob::slotItemFetchJobDone);
0036 }
0037 
0038 void FollowUpReminderJob::setItem(const Akonadi::Item &item)
0039 {
0040     mItem = item;
0041 }
0042 
0043 void FollowUpReminderJob::slotItemFetchJobDone(KJob *job)
0044 {
0045     if (job->error()) {
0046         qCCritical(FOLLOWUPREMINDERAGENT_LOG) << "Error while fetching item. " << job->error() << job->errorString();
0047         deleteLater();
0048         return;
0049     }
0050 
0051     const Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
0052 
0053     const Akonadi::Item::List items = fetchJob->items();
0054     if (items.isEmpty()) {
0055         qCCritical(FOLLOWUPREMINDERAGENT_LOG) << "Error while fetching item: item not found";
0056         deleteLater();
0057         return;
0058     }
0059     const Akonadi::Item item = items.at(0);
0060     if (!item.hasPayload<KMime::Message::Ptr>()) {
0061         qCCritical(FOLLOWUPREMINDERAGENT_LOG) << "Item has not payload";
0062         deleteLater();
0063         return;
0064     }
0065     const auto msg = item.payload<KMime::Message::Ptr>();
0066     if (msg) {
0067         KMime::Headers::InReplyTo *replyTo = msg->inReplyTo(false);
0068         if (replyTo) {
0069             const QString replyToIdStr = replyTo->asUnicodeString();
0070             Q_EMIT finished(replyToIdStr, item.id());
0071         }
0072     }
0073     deleteLater();
0074 }
0075 
0076 #include "moc_followupreminderjob.cpp"