Mailcommon

filteractiondecrypt.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Daniel Vr├ítil <[email protected]>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  */
7 
8 #include "filteractiondecrypt.h"
9 #include "mailcommon_debug.h"
10 #include "util/cryptoutils.h"
11 
12 #include <KColorScheme>
13 #include <KLocalizedString>
14 
15 #include <KMime/Message>
16 
17 #include <gpgme++/decryptionresult.h>
18 
19 #include <Akonadi/MessageFlags>
20 
21 #include <QLabel>
22 #include <QVBoxLayout>
23 
24 using namespace MailCommon;
25 
26 FilterActionDecrypt::FilterActionDecrypt(QObject *parent)
27  : FilterActionWithCrypto(QStringLiteral("decrypt"), i18n("Decrypt"), parent)
28 {
29 }
30 
31 FilterActionDecrypt::~FilterActionDecrypt() = default;
32 
33 FilterAction *FilterActionDecrypt::newAction()
34 {
35  return new FilterActionDecrypt();
36 }
37 
38 QString FilterActionDecrypt::displayString() const
39 {
40  return i18n("Decrypt");
41 }
42 
43 QString FilterActionDecrypt::argsAsString() const
44 {
45  return {};
46 }
47 
48 void FilterActionDecrypt::argsFromString(const QString &)
49 {
50 }
51 
52 SearchRule::RequiredPart FilterActionDecrypt::requiredPart() const
53 {
55 }
56 
57 FilterAction::ReturnCode FilterActionDecrypt::process(ItemContext &context, bool) const
58 {
59  auto &item = context.item();
60  if (!item.hasPayload<KMime::Message::Ptr>()) {
61  return ErrorNeedComplete;
62  }
63 
64  auto msg = item.payload<KMime::Message::Ptr>();
65  if (!isEncrypted(msg.data())) {
66  qCDebug(MAILCOMMON_LOG) << "Message not encrypted";
67  return GoOn;
68  }
69 
70  bool wasEncrypted;
71  auto nec = CryptoUtils::decryptMessage(msg, wasEncrypted);
72  if (!nec) {
73  return wasEncrypted ? ErrorButGoOn : GoOn;
74  }
75 
76  context.item().setPayload(nec);
78  context.setNeedsPayloadStore();
79  context.setNeedsFlagStore();
80  return GoOn;
81 }
82 
83 QWidget *FilterActionDecrypt::createParamWidget(QWidget *parent) const
84 {
85  auto w = new QWidget(parent);
86  auto l = new QVBoxLayout(w);
87 
88  auto lbl = new QLabel(w);
89 
90  QPalette palette = lbl->palette();
91  palette.setColor(lbl->foregroundRole(), KColorScheme(QPalette::Normal).foreground(KColorScheme::NegativeText).color());
92  lbl->setPalette(palette);
93  lbl->setWordWrap(true);
94 
95  lbl->setText(i18n("<b>Warning:</b> Decrypted emails may be uploaded to a server!"));
96  lbl->setToolTip(
97  i18n("<p>If the email folder that you are filtering into is connected to a remote "
98  "account (like an IMAP-Server) the decrypted content will go there.</p>"));
99  l->addWidget(lbl);
100 
101  return w;
102 }
Abstract base class for mail filter actions.
Definition: filteraction.h:38
QString i18n(const char *text, const TYPE &arg...)
void setNeedsFlagStore()
Marks that the item's flags has been changed and needs to be written back.
Definition: itemcontext.cpp:43
RequiredPart
Possible required parts.
Definition: searchrule.h:68
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Definition: itemcontext.cpp:33
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
A helper class for the filtering process.
Definition: itemcontext.h:26
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
void clearFlag(const QByteArray &name)
const AKONADI_MIME_EXPORT char Encrypted[]
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
The filter dialog.
void setPayload(const T &p)
T payload() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.