File indexing completed on 2024-06-23 05:14:14

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     uiserver/signencryptfilescommand.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2007 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 <crypto/signencryptfilescontroller.h>
0015 
0016 #include <Libkleo/KleoException>
0017 
0018 #include <KLocalizedString>
0019 
0020 using namespace Kleo;
0021 using namespace Kleo::Crypto;
0022 
0023 class SignEncryptFilesCommand::Private : public QObject
0024 {
0025     Q_OBJECT
0026 private:
0027     friend class ::Kleo::SignEncryptFilesCommand;
0028     SignEncryptFilesCommand *const q;
0029 
0030 public:
0031     explicit Private(SignEncryptFilesCommand *qq)
0032         : q(qq)
0033         , controller()
0034     {
0035     }
0036 
0037 private:
0038     void checkForErrors() const;
0039 
0040 private Q_SLOTS:
0041     void slotDone();
0042     void slotError(int, const QString &);
0043 
0044 private:
0045     std::shared_ptr<SignEncryptFilesController> controller;
0046 };
0047 
0048 SignEncryptFilesCommand::SignEncryptFilesCommand()
0049     : AssuanCommandMixin<SignEncryptFilesCommand>()
0050     , d(new Private(this))
0051 {
0052 }
0053 
0054 SignEncryptFilesCommand::~SignEncryptFilesCommand()
0055 {
0056 }
0057 
0058 void SignEncryptFilesCommand::Private::checkForErrors() const
0059 {
0060     if (!q->numFiles())
0061         throw Exception(makeError(GPG_ERR_ASS_NO_INPUT), i18n("At least one FILE must be present"));
0062 
0063     if (!q->senders().empty())
0064         throw Exception(makeError(GPG_ERR_CONFLICT),
0065                         i18n("%1 is a filemanager mode command, "
0066                              "connection seems to be in email mode (%2 present)",
0067                              QString::fromLatin1(q->name()),
0068                              QStringLiteral("SENDER")));
0069     if (!q->recipients().empty())
0070         throw Exception(makeError(GPG_ERR_CONFLICT),
0071                         i18n("%1 is a filemanager mode command, "
0072                              "connection seems to be in email mode (%2 present)",
0073                              QString::fromLatin1(q->name()),
0074                              QStringLiteral("RECIPIENT")));
0075 
0076     if (!q->inputs().empty())
0077         throw Exception(makeError(GPG_ERR_CONFLICT),
0078                         i18n("%1 is a filemanager mode command, "
0079                              "connection seems to be in email mode (%2 present)",
0080                              QString::fromLatin1(q->name()),
0081                              QStringLiteral("INPUT")));
0082     if (!q->outputs().empty())
0083         throw Exception(makeError(GPG_ERR_CONFLICT),
0084                         i18n("%1 is a filemanager mode command, "
0085                              "connection seems to be in email mode (%2 present)",
0086                              QString::fromLatin1(q->name()),
0087                              QStringLiteral("OUTPUT")));
0088     if (!q->messages().empty())
0089         throw Exception(makeError(GPG_ERR_CONFLICT),
0090                         i18n("%1 is a filemanager mode command, "
0091                              "connection seems to be in email mode (%2 present)",
0092                              QString::fromLatin1(q->name()),
0093                              QStringLiteral("MESSAGE")));
0094 }
0095 
0096 int SignEncryptFilesCommand::doStart()
0097 {
0098     d->checkForErrors();
0099 
0100     d->controller.reset(new SignEncryptFilesController(shared_from_this()));
0101 
0102     d->controller->setProtocol(checkProtocol(FileManager));
0103 
0104     unsigned int op = operation();
0105     if (hasOption("archive")) {
0106         op |= SignEncryptFilesController::ArchiveForced;
0107     } else {
0108         op |= SignEncryptFilesController::ArchiveAllowed;
0109     }
0110     d->controller->setOperationMode(op);
0111     d->controller->setFiles(fileNames());
0112 
0113     QObject::connect(d->controller.get(), &Controller::done, d.get(), &Private::slotDone, Qt::QueuedConnection);
0114     QObject::connect(d->controller.get(), &Controller::error, d.get(), &Private::slotError, Qt::QueuedConnection);
0115 
0116     d->controller->start();
0117 
0118     return 0;
0119 }
0120 
0121 void SignEncryptFilesCommand::Private::slotDone()
0122 {
0123     q->done();
0124 }
0125 
0126 void SignEncryptFilesCommand::Private::slotError(int err, const QString &details)
0127 {
0128     q->done(err, details);
0129 }
0130 
0131 void SignEncryptFilesCommand::doCanceled()
0132 {
0133     if (d->controller) {
0134         d->controller->cancel();
0135     }
0136 }
0137 
0138 #include "signencryptfilescommand.moc"