File indexing completed on 2024-06-23 05:13:38
0001 /* -*- mode: c++; c-basic-offset:4 -*- 0002 commands/decryptverifyclipboardcommand.cpp 0003 0004 This file is part of Kleopatra, the KDE keymanager 0005 SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include <config-kleopatra.h> 0011 0012 #include "decryptverifyclipboardcommand.h" 0013 0014 #ifndef QT_NO_CLIPBOARD 0015 0016 #include "command_p.h" 0017 0018 #include <crypto/decryptverifyemailcontroller.h> 0019 0020 #include <utils/input.h> 0021 #include <utils/output.h> 0022 0023 #include <Libkleo/Classify> 0024 #include <Libkleo/Stl_Util> 0025 0026 #include "kleopatra_debug.h" 0027 #include <KLocalizedString> 0028 0029 #include <exception> 0030 0031 using namespace Kleo; 0032 using namespace Kleo::Commands; 0033 using namespace Kleo::Crypto; 0034 0035 class DecryptVerifyClipboardCommand::Private : public Command::Private 0036 { 0037 friend class ::Kleo::Commands::DecryptVerifyClipboardCommand; 0038 DecryptVerifyClipboardCommand *q_func() const 0039 { 0040 return static_cast<DecryptVerifyClipboardCommand *>(q); 0041 } 0042 0043 public: 0044 explicit Private(DecryptVerifyClipboardCommand *qq, KeyListController *c); 0045 ~Private() override; 0046 0047 void init(); 0048 0049 private: 0050 void slotControllerDone() 0051 { 0052 finished(); 0053 } 0054 void slotControllerError(int, const QString &) 0055 { 0056 finished(); 0057 } 0058 0059 private: 0060 std::shared_ptr<const ExecutionContext> shared_qq; 0061 std::shared_ptr<Input> input; 0062 DecryptVerifyEMailController controller; 0063 }; 0064 0065 DecryptVerifyClipboardCommand::Private *DecryptVerifyClipboardCommand::d_func() 0066 { 0067 return static_cast<Private *>(d.get()); 0068 } 0069 const DecryptVerifyClipboardCommand::Private *DecryptVerifyClipboardCommand::d_func() const 0070 { 0071 return static_cast<const Private *>(d.get()); 0072 } 0073 0074 #define d d_func() 0075 #define q q_func() 0076 0077 DecryptVerifyClipboardCommand::Private::Private(DecryptVerifyClipboardCommand *qq, KeyListController *c) 0078 : Command::Private(qq, c) 0079 , shared_qq(qq, [](DecryptVerifyClipboardCommand *) {}) 0080 , input() 0081 , controller() 0082 { 0083 } 0084 0085 DecryptVerifyClipboardCommand::Private::~Private() 0086 { 0087 qCDebug(KLEOPATRA_LOG); 0088 } 0089 0090 DecryptVerifyClipboardCommand::DecryptVerifyClipboardCommand(KeyListController *c) 0091 : Command(new Private(this, c)) 0092 { 0093 d->init(); 0094 } 0095 0096 DecryptVerifyClipboardCommand::DecryptVerifyClipboardCommand(QAbstractItemView *v, KeyListController *c) 0097 : Command(v, new Private(this, c)) 0098 { 0099 d->init(); 0100 } 0101 0102 void DecryptVerifyClipboardCommand::Private::init() 0103 { 0104 controller.setExecutionContext(shared_qq); 0105 connect(&controller, &Controller::done, q, [this]() { 0106 slotControllerDone(); 0107 }); 0108 connect(&controller, &Controller::error, q, [this](int err, const QString &details) { 0109 slotControllerError(err, details); 0110 }); 0111 } 0112 0113 DecryptVerifyClipboardCommand::~DecryptVerifyClipboardCommand() 0114 { 0115 qCDebug(KLEOPATRA_LOG); 0116 } 0117 0118 // static 0119 bool DecryptVerifyClipboardCommand::canDecryptVerifyCurrentClipboard() 0120 { 0121 try { 0122 return Input::createFromClipboard()->classification() & (Class::CipherText | Class::ClearsignedMessage | Class::OpaqueSignature); 0123 } catch (...) { 0124 } 0125 return false; 0126 } 0127 0128 void DecryptVerifyClipboardCommand::doStart() 0129 { 0130 try { 0131 const std::shared_ptr<Input> input = Input::createFromClipboard(); 0132 0133 const unsigned int classification = input->classification(); 0134 0135 if (classification & (Class::ClearsignedMessage | Class::OpaqueSignature)) { 0136 d->controller.setOperation(Verify); 0137 d->controller.setVerificationMode(Opaque); 0138 } else if (classification & Class::CipherText) { 0139 d->controller.setOperation(DecryptVerify); 0140 } else { 0141 d->information(i18n("The clipboard does not appear to " 0142 "contain a signature or encrypted text."), 0143 i18n("Decrypt/Verify Clipboard Error")); 0144 d->finished(); 0145 return; 0146 } 0147 0148 d->controller.setProtocol(findProtocol(classification)); 0149 d->controller.setInput(input); 0150 d->controller.setOutput(Output::createFromClipboard()); 0151 0152 d->controller.start(); 0153 0154 } catch (const std::exception &e) { 0155 d->information(i18n("An error occurred: %1", QString::fromLocal8Bit(e.what())), i18n("Decrypt/Verify Clipboard Error")); 0156 d->finished(); 0157 } 0158 } 0159 0160 void DecryptVerifyClipboardCommand::doCancel() 0161 { 0162 qCDebug(KLEOPATRA_LOG); 0163 d->controller.cancel(); 0164 } 0165 0166 #undef d 0167 #undef q 0168 0169 #include "moc_decryptverifyclipboardcommand.cpp" 0170 0171 #endif // QT_NO_CLIPBOARD