Mailcommon

mdnwarningjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2022-2023 Laurent Montel <[email protected]>
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>
16 using namespace MailCommon;
17 MDNWarningJob::MDNWarningJob(IKernel *kernel, QObject *parent)
18  : QObject{parent}
19  , mKernel(kernel)
20 {
21 }
22 
23 MDNWarningJob::~MDNWarningJob()
24 {
25 }
26 
27 void MDNWarningJob::start()
28 {
29  if (!canStart()) {
30  qCWarning(MAILCOMMON_LOG) << " Impossible to start MDNWarningJob";
31  deleteLater();
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";
39  deleteLater();
40  return;
41  }
42 
44  if (!message) {
45  qCWarning(MAILCOMMON_LOG) << " It's not a valid message";
46  deleteLater();
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 
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();
68  deleteLater();
69 }
70 
71 const Akonadi::Item &MDNWarningJob::item() const
72 {
73  return mItem;
74 }
75 
76 void MDNWarningJob::setItem(const Akonadi::Item &newItem)
77 {
78  mItem = newItem;
79 }
80 
81 bool MDNWarningJob::canStart() const
82 {
83  return mItem.isValid() && (mResponse != Unknown);
84 }
85 
86 QPair<bool, KMime::MDN::SendingMode> MDNWarningJob::modifyItem(const KMime::Message::Ptr &msg)
87 {
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 
117 KMime::MDN::SendingMode MDNWarningJob::sendingMode() const
118 {
119  return mSendingMode;
120 }
121 
122 void MDNWarningJob::setSendingMode(KMime::MDN::SendingMode newSendingMode)
123 {
124  mSendingMode = newSendingMode;
125 }
126 
127 MDNWarningJob::ResponseMDN MDNWarningJob::response() const
128 {
129  return mResponse;
130 }
131 
132 void MDNWarningJob::setResponse(ResponseMDN newResponse)
133 {
134  mResponse = newResponse;
135 }
136 
137 #include "moc_mdnwarningjob.cpp"
bool isValid() const
T * data() const const
virtual KIdentityManagementCore::IdentityManager * identityManager()=0
Return the pointer to the identity manager.
Q_EMITQ_EMIT
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition: mailutil.cpp:177
int revision() const
QString mimeType() const
void deleteLater()
Collection & parentCollection()
Id id() const
Generic interface for mail kernels.
bool isValid() const
QString message
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:03:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.