File indexing completed on 2024-12-22 04:56:51

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "storeresultjob.h"
0008 
0009 #include "maildispatcher_debug.h"
0010 #include <Akonadi/DispatchModeAttribute>
0011 #include <Akonadi/ErrorAttribute>
0012 #include <Akonadi/ItemFetchJob>
0013 #include <Akonadi/ItemModifyJob>
0014 #include <Akonadi/MessageFlags>
0015 #include <KLocalizedString>
0016 
0017 using namespace Akonadi;
0018 
0019 StoreResultJob::StoreResultJob(const Item &item, bool success, const QString &message, QObject *parent)
0020     : TransactionSequence(parent)
0021     , mItem(item)
0022     , mSuccess(success)
0023     , mMessage(message)
0024 {
0025 }
0026 
0027 StoreResultJob::~StoreResultJob() = default;
0028 
0029 void StoreResultJob::doStart()
0030 {
0031     // Fetch item in case it was modified elsewhere.
0032     auto job = new ItemFetchJob(mItem, this);
0033     connect(job, &ItemFetchJob::result, this, &StoreResultJob::fetchDone);
0034 }
0035 
0036 bool StoreResultJob::success() const
0037 {
0038     return mSuccess;
0039 }
0040 
0041 QString StoreResultJob::message() const
0042 {
0043     return mMessage;
0044 }
0045 
0046 void StoreResultJob::fetchDone(KJob *job)
0047 {
0048     if (job->error()) {
0049         return;
0050     }
0051 
0052     qCDebug(MAILDISPATCHER_LOG) << " Fetch done";
0053 
0054     const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
0055     Q_ASSERT(fetchJob);
0056     if (fetchJob->items().count() != 1) {
0057         qCCritical(MAILDISPATCHER_LOG) << "Fetched" << fetchJob->items().count() << "items, expected 1.";
0058         setError(Unknown);
0059         setErrorText(i18n("Failed to fetch item."));
0060         commit();
0061         return;
0062     }
0063 
0064     // Store result in item.
0065     Item item = fetchJob->items().at(0);
0066     if (mSuccess) {
0067         item.clearFlag(Akonadi::MessageFlags::Queued);
0068         item.setFlag(Akonadi::MessageFlags::Sent);
0069         item.setFlag(Akonadi::MessageFlags::Seen);
0070         item.removeAttribute<ErrorAttribute>();
0071     } else {
0072         item.setFlag(Akonadi::MessageFlags::HasError);
0073         auto errorAttribute = new ErrorAttribute(mMessage);
0074         item.addAttribute(errorAttribute);
0075 
0076         // If dispatch failed, set the DispatchModeAttribute to Manual.
0077         // Otherwise, the user will *never* be able to try sending the mail again,
0078         // as Send Queued Messages will ignore it.
0079         if (item.hasAttribute<DispatchModeAttribute>()) {
0080             item.attribute<DispatchModeAttribute>()->setDispatchMode(Akonadi::DispatchModeAttribute::Manual);
0081         } else {
0082             item.addAttribute(new DispatchModeAttribute(Akonadi::DispatchModeAttribute::Manual));
0083         }
0084     }
0085 
0086     auto modifyJob = new ItemModifyJob(item, this);
0087     QObject::connect(modifyJob, &ItemModifyJob::result, this, &StoreResultJob::modifyDone);
0088 }
0089 
0090 void StoreResultJob::modifyDone(KJob *job)
0091 {
0092     if (job->error()) {
0093         return;
0094     }
0095 
0096     qCDebug(MAILDISPATCHER_LOG);
0097 
0098     commit();
0099 }
0100 
0101 #include "moc_storeresultjob.cpp"