File indexing completed on 2024-09-22 04:49:59

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "filteractiondecrypt.h"
0009 #include "mailcommon_debug.h"
0010 #include "util/cryptoutils.h"
0011 
0012 #include <KColorScheme>
0013 #include <KLocalizedString>
0014 
0015 #include <KMime/Message>
0016 
0017 #include <gpgme++/decryptionresult.h>
0018 
0019 #include <Akonadi/MessageFlags>
0020 
0021 #include <QLabel>
0022 #include <QVBoxLayout>
0023 
0024 using namespace MailCommon;
0025 
0026 FilterActionDecrypt::FilterActionDecrypt(QObject *parent)
0027     : FilterActionWithCrypto(QStringLiteral("decrypt"), i18n("Decrypt"), parent)
0028 {
0029 }
0030 
0031 FilterActionDecrypt::~FilterActionDecrypt() = default;
0032 
0033 FilterAction *FilterActionDecrypt::newAction()
0034 {
0035     return new FilterActionDecrypt();
0036 }
0037 
0038 QString FilterActionDecrypt::displayString() const
0039 {
0040     return i18n("Decrypt");
0041 }
0042 
0043 QString FilterActionDecrypt::argsAsString() const
0044 {
0045     return {};
0046 }
0047 
0048 void FilterActionDecrypt::argsFromString(const QString &)
0049 {
0050 }
0051 
0052 SearchRule::RequiredPart FilterActionDecrypt::requiredPart() const
0053 {
0054     return SearchRule::CompleteMessage;
0055 }
0056 
0057 FilterAction::ReturnCode FilterActionDecrypt::process(ItemContext &context, bool) const
0058 {
0059     auto &item = context.item();
0060     if (!item.hasPayload<KMime::Message::Ptr>()) {
0061         return ErrorNeedComplete;
0062     }
0063 
0064     auto msg = item.payload<KMime::Message::Ptr>();
0065     if (!isEncrypted(msg.data())) {
0066         qCDebug(MAILCOMMON_LOG) << "Message not encrypted";
0067         return GoOn;
0068     }
0069 
0070     bool wasEncrypted;
0071     auto nec = CryptoUtils::decryptMessage(msg, wasEncrypted);
0072     if (!nec) {
0073         return wasEncrypted ? ErrorButGoOn : GoOn;
0074     }
0075 
0076     context.item().setPayload(nec);
0077     context.item().clearFlag(Akonadi::MessageFlags::Encrypted);
0078     context.setNeedsPayloadStore();
0079     context.setNeedsFlagStore();
0080     return GoOn;
0081 }
0082 
0083 QWidget *FilterActionDecrypt::createParamWidget(QWidget *parent) const
0084 {
0085     auto w = new QWidget(parent);
0086     auto l = new QVBoxLayout(w);
0087 
0088     auto lbl = new QLabel(w);
0089 
0090     QPalette palette = lbl->palette();
0091     palette.setColor(lbl->foregroundRole(), KColorScheme(QPalette::Normal).foreground(KColorScheme::NegativeText).color());
0092     lbl->setPalette(palette);
0093     lbl->setWordWrap(true);
0094 
0095     lbl->setText(i18n("<b>Warning:</b> Decrypted emails may be uploaded to a server!"));
0096     lbl->setToolTip(
0097         i18n("<p>If the email folder that you are filtering into is connected to a remote "
0098              "account (like an IMAP-Server) the decrypted content will go there.</p>"));
0099     l->addWidget(lbl);
0100 
0101     return w;
0102 }
0103 
0104 #include "moc_filteractiondecrypt.cpp"