File indexing completed on 2024-06-23 05:13:46
0001 /* -*- mode: c++; c-basic-offset:4 -*- 0002 commands/signencryptfilescommand.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 "signencryptfilescommand.h" 0013 0014 #include "command_p.h" 0015 0016 #include <crypto/signencryptfilescontroller.h> 0017 0018 #include <utils/filedialog.h> 0019 0020 #include <Libkleo/Stl_Util> 0021 0022 #include "kleopatra_debug.h" 0023 #include <KLocalizedString> 0024 0025 #include <exception> 0026 0027 using namespace Kleo; 0028 using namespace Kleo::Commands; 0029 using namespace Kleo::Crypto; 0030 0031 class SignEncryptFilesCommand::Private : public Command::Private 0032 { 0033 friend class ::Kleo::Commands::SignEncryptFilesCommand; 0034 SignEncryptFilesCommand *q_func() const 0035 { 0036 return static_cast<SignEncryptFilesCommand *>(q); 0037 } 0038 0039 public: 0040 explicit Private(SignEncryptFilesCommand *qq, KeyListController *c); 0041 ~Private() override; 0042 0043 QStringList selectFiles() const; 0044 0045 void init(); 0046 0047 private: 0048 void slotControllerDone() 0049 { 0050 finished(); 0051 } 0052 void slotControllerError(int, const QString &) 0053 { 0054 finished(); 0055 } 0056 0057 private: 0058 QStringList files; 0059 std::shared_ptr<const ExecutionContext> shared_qq; 0060 SignEncryptFilesController controller; 0061 }; 0062 0063 SignEncryptFilesCommand::Private *SignEncryptFilesCommand::d_func() 0064 { 0065 return static_cast<Private *>(d.get()); 0066 } 0067 const SignEncryptFilesCommand::Private *SignEncryptFilesCommand::d_func() const 0068 { 0069 return static_cast<const Private *>(d.get()); 0070 } 0071 0072 #define d d_func() 0073 #define q q_func() 0074 0075 SignEncryptFilesCommand::Private::Private(SignEncryptFilesCommand *qq, KeyListController *c) 0076 : Command::Private(qq, c) 0077 , files() 0078 , shared_qq(qq, [](SignEncryptFilesCommand *) {}) 0079 , controller() 0080 { 0081 controller.setOperationMode(SignEncryptFilesController::SignSelected // 0082 | SignEncryptFilesController::EncryptSelected // 0083 | SignEncryptFilesController::ArchiveAllowed); 0084 } 0085 0086 SignEncryptFilesCommand::Private::~Private() 0087 { 0088 qCDebug(KLEOPATRA_LOG) << q << __func__; 0089 } 0090 0091 SignEncryptFilesCommand::SignEncryptFilesCommand(KeyListController *c) 0092 : Command(new Private(this, c)) 0093 { 0094 d->init(); 0095 } 0096 0097 SignEncryptFilesCommand::SignEncryptFilesCommand(QAbstractItemView *v, KeyListController *c) 0098 : Command(v, new Private(this, c)) 0099 { 0100 d->init(); 0101 } 0102 0103 SignEncryptFilesCommand::SignEncryptFilesCommand(const QStringList &files, KeyListController *c) 0104 : Command(new Private(this, c)) 0105 { 0106 d->init(); 0107 d->files = files; 0108 } 0109 0110 SignEncryptFilesCommand::SignEncryptFilesCommand(const QStringList &files, QAbstractItemView *v, KeyListController *c) 0111 : Command(v, new Private(this, c)) 0112 { 0113 d->init(); 0114 d->files = files; 0115 } 0116 0117 void SignEncryptFilesCommand::Private::init() 0118 { 0119 controller.setExecutionContext(shared_qq); 0120 connect(&controller, &Controller::done, q, [this]() { 0121 slotControllerDone(); 0122 }); 0123 connect(&controller, &Controller::error, q, [this](int err, const QString &details) { 0124 slotControllerError(err, details); 0125 }); 0126 } 0127 0128 SignEncryptFilesCommand::~SignEncryptFilesCommand() 0129 { 0130 qCDebug(KLEOPATRA_LOG) << this << __func__; 0131 } 0132 0133 void SignEncryptFilesCommand::setFiles(const QStringList &files) 0134 { 0135 d->files = files; 0136 } 0137 0138 void SignEncryptFilesCommand::setSigningPolicy(Policy policy) 0139 { 0140 unsigned int mode = d->controller.operationMode(); 0141 mode &= ~SignEncryptFilesController::SignMask; 0142 switch (policy) { 0143 case NoPolicy: 0144 case Allow: 0145 mode |= SignEncryptFilesController::SignAllowed; 0146 break; 0147 case Deny: 0148 mode |= SignEncryptFilesController::SignDisallowed; 0149 break; 0150 case Force: 0151 mode |= SignEncryptFilesController::SignSelected; 0152 break; 0153 } 0154 try { 0155 d->controller.setOperationMode(mode); 0156 } catch (...) { 0157 } 0158 } 0159 0160 Policy SignEncryptFilesCommand::signingPolicy() const 0161 { 0162 const unsigned int mode = d->controller.operationMode(); 0163 switch (mode & SignEncryptFilesController::SignMask) { 0164 default: 0165 Q_ASSERT(!"This should not happen!"); 0166 return NoPolicy; 0167 case SignEncryptFilesController::SignAllowed: 0168 return Allow; 0169 case SignEncryptFilesController::SignSelected: 0170 return Force; 0171 case SignEncryptFilesController::SignDisallowed: 0172 return Deny; 0173 } 0174 } 0175 0176 void SignEncryptFilesCommand::setEncryptionPolicy(Policy policy) 0177 { 0178 unsigned int mode = d->controller.operationMode(); 0179 mode &= ~SignEncryptFilesController::EncryptMask; 0180 switch (policy) { 0181 case NoPolicy: 0182 case Allow: 0183 mode |= SignEncryptFilesController::EncryptAllowed; 0184 break; 0185 case Deny: 0186 mode |= SignEncryptFilesController::EncryptDisallowed; 0187 break; 0188 case Force: 0189 mode |= SignEncryptFilesController::EncryptSelected; 0190 break; 0191 } 0192 try { 0193 d->controller.setOperationMode(mode); 0194 } catch (...) { 0195 } 0196 } 0197 0198 Policy SignEncryptFilesCommand::encryptionPolicy() const 0199 { 0200 const unsigned int mode = d->controller.operationMode(); 0201 switch (mode & SignEncryptFilesController::EncryptMask) { 0202 default: 0203 Q_ASSERT(!"This should not happen!"); 0204 return NoPolicy; 0205 case SignEncryptFilesController::EncryptAllowed: 0206 return Allow; 0207 case SignEncryptFilesController::EncryptSelected: 0208 return Force; 0209 case SignEncryptFilesController::EncryptDisallowed: 0210 return Deny; 0211 } 0212 } 0213 0214 void SignEncryptFilesCommand::setArchivePolicy(Policy policy) 0215 { 0216 unsigned int mode = d->controller.operationMode(); 0217 mode &= ~SignEncryptFilesController::ArchiveMask; 0218 switch (policy) { 0219 case NoPolicy: 0220 case Allow: 0221 mode |= SignEncryptFilesController::ArchiveAllowed; 0222 break; 0223 case Deny: 0224 mode |= SignEncryptFilesController::ArchiveDisallowed; 0225 break; 0226 case Force: 0227 mode |= SignEncryptFilesController::ArchiveForced; 0228 break; 0229 } 0230 d->controller.setOperationMode(mode); 0231 } 0232 0233 Policy SignEncryptFilesCommand::archivePolicy() const 0234 { 0235 const unsigned int mode = d->controller.operationMode(); 0236 switch (mode & SignEncryptFilesController::ArchiveMask) { 0237 case SignEncryptFilesController::ArchiveAllowed: 0238 return Allow; 0239 case SignEncryptFilesController::ArchiveForced: 0240 return Force; 0241 case SignEncryptFilesController::ArchiveDisallowed: 0242 return Deny; 0243 default: 0244 Q_ASSERT(!"This should not happen!"); 0245 return NoPolicy; 0246 } 0247 } 0248 0249 void SignEncryptFilesCommand::setProtocol(GpgME::Protocol proto) 0250 { 0251 d->controller.setProtocol(proto); 0252 } 0253 0254 GpgME::Protocol SignEncryptFilesCommand::protocol() const 0255 { 0256 return d->controller.protocol(); 0257 } 0258 0259 void SignEncryptFilesCommand::doStart() 0260 { 0261 try { 0262 if (d->files.empty()) { 0263 d->files = selectFiles(); 0264 } 0265 if (d->files.empty()) { 0266 d->finished(); 0267 return; 0268 } 0269 0270 d->controller.setFiles(d->files); 0271 d->controller.start(); 0272 0273 } catch (const std::exception &e) { 0274 d->information(i18n("An error occurred: %1", QString::fromLocal8Bit(e.what())), i18n("Sign/Encrypt Files Error")); 0275 d->finished(); 0276 } 0277 } 0278 0279 void SignEncryptFilesCommand::doCancel() 0280 { 0281 qCDebug(KLEOPATRA_LOG) << this << __func__; 0282 d->controller.cancel(); 0283 } 0284 0285 QStringList SignEncryptFilesCommand::selectFiles() const 0286 { 0287 return FileDialog::getOpenFileNames(d->parentWidgetOrView(), i18n("Select One or More Files to Sign and/or Encrypt"), QStringLiteral("enc")); 0288 } 0289 0290 #undef d 0291 #undef q 0292 0293 #include "moc_signencryptfilescommand.cpp"