File indexing completed on 2024-06-23 05:13:38

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/checksumcreatefilescommand.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 "checksumcreatefilescommand.h"
0013 
0014 #include "command_p.h"
0015 
0016 #include <crypto/createchecksumscontroller.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 ChecksumCreateFilesCommand::Private : public Command::Private
0032 {
0033     friend class ::Kleo::Commands::ChecksumCreateFilesCommand;
0034     ChecksumCreateFilesCommand *q_func() const
0035     {
0036         return static_cast<ChecksumCreateFilesCommand *>(q);
0037     }
0038 
0039 public:
0040     explicit Private(ChecksumCreateFilesCommand *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     CreateChecksumsController controller;
0061 };
0062 
0063 ChecksumCreateFilesCommand::Private *ChecksumCreateFilesCommand::d_func()
0064 {
0065     return static_cast<Private *>(d.get());
0066 }
0067 const ChecksumCreateFilesCommand::Private *ChecksumCreateFilesCommand::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 ChecksumCreateFilesCommand::Private::Private(ChecksumCreateFilesCommand *qq, KeyListController *c)
0076     : Command::Private(qq, c)
0077     , files()
0078     , shared_qq(qq, [](ChecksumCreateFilesCommand *) {})
0079     , controller()
0080 {
0081     controller.setAllowAddition(true);
0082 }
0083 
0084 ChecksumCreateFilesCommand::Private::~Private()
0085 {
0086     qCDebug(KLEOPATRA_LOG);
0087 }
0088 
0089 ChecksumCreateFilesCommand::ChecksumCreateFilesCommand(KeyListController *c)
0090     : Command(new Private(this, c))
0091 {
0092     d->init();
0093 }
0094 
0095 ChecksumCreateFilesCommand::ChecksumCreateFilesCommand(QAbstractItemView *v, KeyListController *c)
0096     : Command(v, new Private(this, c))
0097 {
0098     d->init();
0099 }
0100 
0101 ChecksumCreateFilesCommand::ChecksumCreateFilesCommand(const QStringList &files, KeyListController *c)
0102     : Command(new Private(this, c))
0103 {
0104     d->init();
0105     d->files = files;
0106 }
0107 
0108 ChecksumCreateFilesCommand::ChecksumCreateFilesCommand(const QStringList &files, QAbstractItemView *v, KeyListController *c)
0109     : Command(v, new Private(this, c))
0110 {
0111     d->init();
0112     d->files = files;
0113 }
0114 
0115 void ChecksumCreateFilesCommand::Private::init()
0116 {
0117     controller.setExecutionContext(shared_qq);
0118     connect(&controller, &Crypto::Controller::done, q, [this]() {
0119         slotControllerDone();
0120     });
0121     connect(&controller, &Crypto::Controller::error, q, [this](int err, const QString &details) {
0122         slotControllerError(err, details);
0123     });
0124 }
0125 
0126 ChecksumCreateFilesCommand::~ChecksumCreateFilesCommand()
0127 {
0128     qCDebug(KLEOPATRA_LOG);
0129 }
0130 
0131 void ChecksumCreateFilesCommand::setFiles(const QStringList &files)
0132 {
0133     d->files = files;
0134 }
0135 
0136 void ChecksumCreateFilesCommand::doStart()
0137 {
0138     try {
0139         if (d->files.empty()) {
0140             d->files = d->selectFiles();
0141         }
0142         if (d->files.empty()) {
0143             d->finished();
0144             return;
0145         }
0146 
0147         d->controller.setFiles(d->files);
0148         d->controller.start();
0149 
0150     } catch (const std::exception &e) {
0151         d->information(i18n("An error occurred: %1", QString::fromLocal8Bit(e.what())), i18n("Create Checksum Files Error"));
0152         d->finished();
0153     }
0154 }
0155 
0156 void ChecksumCreateFilesCommand::doCancel()
0157 {
0158     qCDebug(KLEOPATRA_LOG);
0159     d->controller.cancel();
0160 }
0161 
0162 QStringList ChecksumCreateFilesCommand::Private::selectFiles() const
0163 {
0164     return FileDialog::getOpenFileNames(parentWidgetOrView(), i18n("Select One or More Files to Create Checksums For"), QStringLiteral("chk"));
0165 }
0166 
0167 #undef d
0168 #undef q
0169 
0170 #include "moc_checksumcreatefilescommand.cpp"