Warning, file /pim/mailcommon/src/mdn/mdnwarningjob.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mdnwarningjob.h"
0008 #include "kernel/mailkernel.h"
0009 #include "mailcommon_debug.h"
0010 #include "util/mailutil.h"
0011 #include <Akonadi/ItemModifyJob>
0012 #include <Akonadi/MDNStateAttribute>
0013 #include <MessageComposer/MessageSender>
0014 #include <MessageComposer/Util>
0015 #include <MessageViewer/MessageViewerSettings>
0016 using namespace MailCommon;
0017 MDNWarningJob::MDNWarningJob(IKernel *kernel, QObject *parent)
0018     : QObject{parent}
0019     , mKernel(kernel)
0020 {
0021 }
0022 
0023 MDNWarningJob::~MDNWarningJob()
0024 {
0025 }
0026 
0027 void MDNWarningJob::start()
0028 {
0029     if (!canStart()) {
0030         qCWarning(MAILCOMMON_LOG) << " Impossible to start MDNWarningJob";
0031         deleteLater();
0032         return;
0033     }
0034     const Akonadi::Collection collection = mItem.parentCollection();
0035     if (collection.isValid()
0036         && (CommonKernel->folderIsSentMailFolder(collection) || CommonKernel->folderIsTrash(collection) || CommonKernel->folderIsDraftOrOutbox(collection)
0037             || CommonKernel->folderIsTemplates(collection))) {
0038         qCWarning(MAILCOMMON_LOG) << " It's not a valid collection";
0039         deleteLater();
0040         return;
0041     }
0042 
0043     const KMime::Message::Ptr message = MessageComposer::Util::message(mItem);
0044     if (!message) {
0045         qCWarning(MAILCOMMON_LOG) << " It's not a valid message";
0046         deleteLater();
0047         return;
0048     }
0049 
0050     const QPair<bool, KMime::MDN::SendingMode> mdnSend = modifyItem(message);
0051     qCDebug(MAILCOMMON_LOG) << " Send " << mdnSend.first << " mdnSend.sendmode " << mdnSend.second;
0052 
0053     if (mdnSend.first) {
0054         const int quote = MessageViewer::MessageViewerSettings::self()->quoteMessage();
0055 
0056         MessageComposer::MessageFactoryNG factory(message, Akonadi::Item().id());
0057         factory.setIdentityManager(mKernel->identityManager());
0058         factory.setFolderIdentity(MailCommon::Util::folderIdentity(mItem));
0059 
0060         const KMime::Message::Ptr mdn = factory.createMDN(KMime::MDN::ManualAction, KMime::MDN::Displayed, mdnSend.second, quote);
0061         if (mdn) {
0062             if (!mKernel->msgSender()->send(mdn)) {
0063                 qCDebug(MAILCOMMON_LOG) << "Sending failed.";
0064             }
0065         }
0066     }
0067     Q_EMIT finished();
0068     deleteLater();
0069 }
0070 
0071 const Akonadi::Item &MDNWarningJob::item() const
0072 {
0073     return mItem;
0074 }
0075 
0076 void MDNWarningJob::setItem(const Akonadi::Item &newItem)
0077 {
0078     mItem = newItem;
0079 }
0080 
0081 bool MDNWarningJob::canStart() const
0082 {
0083     return mItem.isValid() && (mResponse != Unknown);
0084 }
0085 
0086 QPair<bool, KMime::MDN::SendingMode> MDNWarningJob::modifyItem(const KMime::Message::Ptr &msg)
0087 {
0088     QPair<bool, KMime::MDN::SendingMode> result;
0089     auto mdnStateAttr = new Akonadi::MDNStateAttribute(Akonadi::MDNStateAttribute::MDNStateUnknown);
0090     // create a minimal version of item with just the attribute we want to change
0091     bool doSend = false;
0092     // RFC 2298: An MDN MUST NOT be generated in response to an MDN.
0093     if (MessageComposer::Util::findTypeInMessage(msg.data(), "message", "disposition-notification")) {
0094         mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNIgnore);
0095     } else if (mResponse == MDNIgnore) { // ignore
0096         doSend = false;
0097         mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNIgnore);
0098     } else if (mResponse == Denied) { // denied
0099         doSend = true;
0100         mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNDenied);
0101     } else if (mResponse == Send) { // the user wants to send. let's make sure we can, according to the RFC.
0102         doSend = true;
0103         mdnStateAttr->setMDNState(MessageComposer::MDNAdviceHelper::dispositionToSentState(KMime::MDN::Displayed));
0104     }
0105     result.first = doSend;
0106     result.second = mSendingMode;
0107     Akonadi::Item i(mItem.id());
0108     i.setRevision(mItem.revision());
0109     i.setMimeType(mItem.mimeType());
0110     i.addAttribute(mdnStateAttr);
0111     auto modify = new Akonadi::ItemModifyJob(i);
0112     modify->setIgnorePayload(true);
0113     modify->disableRevisionCheck();
0114     return result;
0115 }
0116 
0117 KMime::MDN::SendingMode MDNWarningJob::sendingMode() const
0118 {
0119     return mSendingMode;
0120 }
0121 
0122 void MDNWarningJob::setSendingMode(KMime::MDN::SendingMode newSendingMode)
0123 {
0124     mSendingMode = newSendingMode;
0125 }
0126 
0127 MDNWarningJob::ResponseMDN MDNWarningJob::response() const
0128 {
0129     return mResponse;
0130 }
0131 
0132 void MDNWarningJob::setResponse(ResponseMDN newResponse)
0133 {
0134     mResponse = newResponse;
0135 }
0136 
0137 #include "moc_mdnwarningjob.cpp"