Mailcommon

mdnwarningjob.cpp
1/*
2 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "mdnwarningjob.h"
8#include "kernel/mailkernel.h"
9#include "mailcommon_debug.h"
10#include "util/mailutil.h"
11#include <Akonadi/ItemModifyJob>
12#include <Akonadi/MDNStateAttribute>
13#include <MessageComposer/MessageSender>
14#include <MessageComposer/Util>
15#include <MessageViewer/MessageViewerSettings>
16using namespace MailCommon;
17MDNWarningJob::MDNWarningJob(IKernel *kernel, QObject *parent)
18 : QObject{parent}
19 , mKernel(kernel)
20{
21}
22
23MDNWarningJob::~MDNWarningJob()
24{
25}
26
27void MDNWarningJob::start()
28{
29 if (!canStart()) {
30 qCWarning(MAILCOMMON_LOG) << " Impossible to start MDNWarningJob";
32 return;
33 }
34 const Akonadi::Collection collection = mItem.parentCollection();
35 if (collection.isValid()
36 && (CommonKernel->folderIsSentMailFolder(collection) || CommonKernel->folderIsTrash(collection) || CommonKernel->folderIsDraftOrOutbox(collection)
37 || CommonKernel->folderIsTemplates(collection))) {
38 qCWarning(MAILCOMMON_LOG) << " It's not a valid collection";
40 return;
41 }
42
43 const KMime::Message::Ptr message = MessageComposer::Util::message(mItem);
44 if (!message) {
45 qCWarning(MAILCOMMON_LOG) << " It's not a valid message";
47 return;
48 }
49
50 const QPair<bool, KMime::MDN::SendingMode> mdnSend = modifyItem(message);
51 qCDebug(MAILCOMMON_LOG) << " Send " << mdnSend.first << " mdnSend.sendmode " << mdnSend.second;
52
53 if (mdnSend.first) {
54 const int quote = MessageViewer::MessageViewerSettings::self()->quoteMessage();
55
56 MessageComposer::MessageFactoryNG factory(message, Akonadi::Item().id());
57 factory.setIdentityManager(mKernel->identityManager());
58 factory.setFolderIdentity(MailCommon::Util::folderIdentity(mItem));
59
60 const KMime::Message::Ptr mdn = factory.createMDN(KMime::MDN::ManualAction, KMime::MDN::Displayed, mdnSend.second, quote);
61 if (mdn) {
62 if (!mKernel->msgSender()->send(mdn)) {
63 qCDebug(MAILCOMMON_LOG) << "Sending failed.";
64 }
65 }
66 }
67 Q_EMIT finished();
69}
70
71const Akonadi::Item &MDNWarningJob::item() const
72{
73 return mItem;
74}
75
76void MDNWarningJob::setItem(const Akonadi::Item &newItem)
77{
78 mItem = newItem;
79}
80
81bool MDNWarningJob::canStart() const
82{
83 return mItem.isValid() && (mResponse != Unknown);
84}
85
86QPair<bool, KMime::MDN::SendingMode> MDNWarningJob::modifyItem(const KMime::Message::Ptr &msg)
87{
88 QPair<bool, KMime::MDN::SendingMode> result;
90 // create a minimal version of item with just the attribute we want to change
91 bool doSend = false;
92 // RFC 2298: An MDN MUST NOT be generated in response to an MDN.
93 if (MessageComposer::Util::findTypeInMessage(msg.data(), "message", "disposition-notification")) {
94 mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNIgnore);
95 } else if (mResponse == MDNIgnore) { // ignore
96 doSend = false;
97 mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNIgnore);
98 } else if (mResponse == Denied) { // denied
99 doSend = true;
100 mdnStateAttr->setMDNState(Akonadi::MDNStateAttribute::MDNDenied);
101 } else if (mResponse == Send) { // the user wants to send. let's make sure we can, according to the RFC.
102 doSend = true;
103 mdnStateAttr->setMDNState(MessageComposer::MDNAdviceHelper::dispositionToSentState(KMime::MDN::Displayed));
104 }
105 result.first = doSend;
106 result.second = mSendingMode;
107 Akonadi::Item i(mItem.id());
108 i.setRevision(mItem.revision());
109 i.setMimeType(mItem.mimeType());
110 i.addAttribute(mdnStateAttr);
111 auto modify = new Akonadi::ItemModifyJob(i);
112 modify->setIgnorePayload(true);
113 modify->disableRevisionCheck();
114 return result;
115}
116
117KMime::MDN::SendingMode MDNWarningJob::sendingMode() const
118{
119 return mSendingMode;
120}
121
122void MDNWarningJob::setSendingMode(KMime::MDN::SendingMode newSendingMode)
123{
124 mSendingMode = newSendingMode;
125}
126
127MDNWarningJob::ResponseMDN MDNWarningJob::response() const
128{
129 return mResponse;
130}
131
132void MDNWarningJob::setResponse(ResponseMDN newResponse)
133{
134 mResponse = newResponse;
135}
136
137#include "moc_mdnwarningjob.cpp"
bool isValid() const
QString mimeType() const
Collection & parentCollection()
Id id() const
int revision() const
bool isValid() const
Generic interface for mail kernels.
virtual KIdentityManagementCore::IdentityManager * identityManager()=0
Return the pointer to the identity manager.
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition mailutil.cpp:176
The filter dialog.
Q_EMITQ_EMIT
void deleteLater()
T * data() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 21 2024 12:03:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.