diff --git a/src/commands/command.cpp b/src/commands/command.cpp index 30d82676e..354469347 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -1,313 +1,301 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/command.cpp This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #include #include "command.h" #include "command_p.h" #include "signencryptfilescommand.h" #include "importcertificatefromfilecommand.h" #include "decryptverifyfilescommand.h" #include "detailscommand.h" #include "lookupcertificatescommand.h" #include "checksumverifyfilescommand.h" #include #include #include #include "kleopatra_debug.h" #include #include #include using namespace Kleo; using namespace Kleo::Commands; using namespace GpgME; Command::Private::Private(Command *qq) : q(qq), autoDelete(true), warnWhenRunningAtShutdown(true) { } Command::Private::Private(Command *qq, KeyListController *controller) : q(qq), autoDelete(true), warnWhenRunningAtShutdown(true), controller_(controller) { } Command::Private::Private(Command *qq, QWidget *parent) : q(qq), autoDelete(true), warnWhenRunningAtShutdown(true), parentWidget_(parent) { } Command::Private::~Private() { qCDebug(KLEOPATRA_LOG); } Command::Command(KeyListController *p) : QObject(p), d(new Private(this, p)) { if (p) { p->registerCommand(this); } } Command::Command(QAbstractItemView *v, KeyListController *p) : QObject(p), d(new Private(this, p)) { if (p) { p->registerCommand(this); } if (v) { setView(v); } } Command::Command(Private *pp) : QObject(pp->controller_), d(pp) { if (pp->controller_) { pp->controller_->registerCommand(this); } } Command::Command(QAbstractItemView *v, Private *pp) : QObject(pp->controller_), d(pp) { if (pp->controller_) { pp->controller_->registerCommand(this); } if (v) { setView(v); } } Command::Command(const GpgME::Key &key) : QObject(nullptr), d(new Private(this)) { d->keys_ = std::vector(1, key); } Command::Command(const std::vector &keys) : QObject(nullptr), d(new Private(this)) { d->keys_ = keys; } Command::Command(const Key &key, Private *pp) : QObject(nullptr), d(pp) { d->keys_ = std::vector(1, key); } Command::Command(const std::vector &keys, Private *pp) : QObject(nullptr), d(pp) { d->keys_ = keys; } Command::~Command() { qCDebug(KLEOPATRA_LOG); } void Command::setAutoDelete(bool on) { d->autoDelete = on; } bool Command::autoDelete() const { return d->autoDelete; } void Command::setWarnWhenRunningAtShutdown(bool on) { d->warnWhenRunningAtShutdown = on; } bool Command::warnWhenRunningAtShutdown() const { return d->warnWhenRunningAtShutdown; } void Command::setParentWidget(QWidget *widget) { d->parentWidget_ = widget; } void Command::setParentWId(WId wid) { d->parentWId_ = wid; } void Command::setView(QAbstractItemView *view) { if (view == d->view_) { return; } d->view_ = view; if (!view || !d->indexes_.empty()) { return; } const QItemSelectionModel *const sm = view->selectionModel(); if (!sm) { qCWarning(KLEOPATRA_LOG) << "view " << (void *)view << " has no selectionModel!"; return; } const QList selected = sm->selectedRows(); if (!selected.empty()) { std::copy(selected.begin(), selected.end(), std::back_inserter(d->indexes_)); return; } } -void Command::setIndex(const QModelIndex &idx) -{ - d->indexes_.clear(); - d->indexes_.push_back(idx); -} - -void Command::setIndexes(const QList &idx) -{ - d->indexes_.clear(); - std::copy(idx.begin(), idx.end(), std::back_inserter(d->indexes_)); -} - void Command::setKey(const Key &key) { d->keys_.clear(); if (!key.isNull()) { d->keys_.push_back(key); } } void Command::setKeys(const std::vector &keys) { d->keys_ = keys; } void Command::start() { doStart(); } void Command::cancel() { qCDebug(KLEOPATRA_LOG) << metaObject()->className(); doCancel(); Q_EMIT canceled(); } void Command::addTemporaryView(const QString &title, AbstractKeyListSortFilterProxyModel *proxy, const QString &tabToolTip) { if (TabWidget *const tw = d->controller_ ? d->controller_->tabWidget() : nullptr) if (QAbstractItemView *const v = tw->addTemporaryView(title, proxy, tabToolTip)) { setView(v); } } void Command::applyWindowID(QWidget *w) const { if (w) { if (d->parentWId()) { if (QWidget *pw = QWidget::find(d->parentWId())) { // remember the current focus widget; re-parenting resets it QWidget *focusWidget = w->focusWidget(); w->setParent(pw, w->windowFlags()); if (focusWidget) { focusWidget->setFocus(); } } else { w->setAttribute(Qt::WA_NativeWindow, true); KWindowSystem::setMainWindow(w->windowHandle(), d->parentWId()); } } else { // remember the current focus widget; re-parenting resets it QWidget *focusWidget = w->focusWidget(); w->setParent(d->parentWidgetOrView(), w->windowFlags()); if (focusWidget) { focusWidget->setFocus(); } } } } // static QVector Command::commandsForFiles(const QStringList &files) { QStringList importFiles, decryptFiles, encryptFiles, checksumFiles; QVector cmds; for (const QString &fileName : files) { const unsigned int classification = classify(fileName); if (classification & Class::AnyCertStoreType) { importFiles << fileName; } else if (classification & Class::AnyMessageType) { // For any message we decrypt / verify. This includes // the class CipherText decryptFiles << fileName; } else if (isChecksumFile(fileName)) { checksumFiles << fileName; } else { QFileInfo fi(fileName); if (fi.isReadable()) { encryptFiles << fileName; } } } if (!importFiles.isEmpty()) { cmds << new ImportCertificateFromFileCommand(importFiles, nullptr); } if (!decryptFiles.isEmpty()) { cmds << new DecryptVerifyFilesCommand(decryptFiles, nullptr); } if (!encryptFiles.isEmpty()) { cmds << new SignEncryptFilesCommand(encryptFiles, nullptr); } if (!checksumFiles.isEmpty()) { cmds << new ChecksumVerifyFilesCommand(checksumFiles, nullptr); } return cmds; } // static Command *Command::commandForQuery(const QString &query) { const auto cache = Kleo::KeyCache::instance(); GpgME::Key key = cache->findByKeyIDOrFingerprint(query.toLocal8Bit().data()); if (key.isNull() && query.size() > 16) { // Try to find by subkeyid std::vector id; id.push_back(query.right(16).toStdString()); auto keys = cache->findSubkeysByKeyID(id); if (keys.size()) { key = keys[0].parent(); } } if (key.isNull()) { return new LookupCertificatesCommand(query, nullptr); } else { return new DetailsCommand(key, nullptr); } } diff --git a/src/commands/command.h b/src/commands/command.h index 5949f5b12..c664ec394 100644 --- a/src/commands/command.h +++ b/src/commands/command.h @@ -1,137 +1,135 @@ /* -*- mode: c++; c-basic-offset:4 -*- commands/command.h This file is part of Kleopatra, the KDE keymanager SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include #include // for WId #include #include // for ExecutionContext #include class QModelIndex; template class QList; class QAbstractItemView; namespace GpgME { class Key; } namespace Kleo { class KeyListController; class AbstractKeyListSortFilterProxyModel; class Command : public QObject, public ExecutionContext { Q_OBJECT public: explicit Command(KeyListController *parent); explicit Command(QAbstractItemView *view, KeyListController *parent); explicit Command(const GpgME::Key &key); explicit Command(const std::vector &keys); ~Command() override; enum Restriction { NoRestriction = 0x0000, NeedSelection = 0x0001, OnlyOneKey = 0x0002, NeedSecretKey = 0x0004, //< command performs secret key operations NeedSecretKeyData = 0x0008, //< command needs access to the secret key data MustBeOpenPGP = 0x0010, MustBeCMS = 0x0020, // esoteric: MayOnlyBeSecretKeyIfOwnerTrustIsNotYetUltimate = 0x0040, // for set-owner-trust AnyCardHasNullPin = 0x0080, AnyCardCanLearnKeys = 0x0100, MustBeRoot = 0x0200, MustBeTrustedRoot = 0x0400 | MustBeRoot, MustBeUntrustedRoot = 0x0800 | MustBeRoot, _AllRestrictions_Helper, AllRestrictions = 2 * (_AllRestrictions_Helper - 1) - 1 }; Q_DECLARE_FLAGS(Restrictions, Restriction) static Restrictions restrictions() { return NoRestriction; } /** Classify the files and return the most appropriate commands. * * @param files: A list of files. * * @returns null QString on success. Error message otherwise. */ static QVectorcommandsForFiles(const QStringList &files); /** Get a command for a query. * * @param query: A keyid / fingerprint or any string to use in the search. */ static Command *commandForQuery(const QString &query); void setParentWidget(QWidget *widget); void setParentWId(WId wid); void setView(QAbstractItemView *view); - void setIndex(const QModelIndex &idx); - void setIndexes(const QList &idx); void setKey(const GpgME::Key &key); void setKeys(const std::vector &keys); void setAutoDelete(bool on); bool autoDelete() const; void setWarnWhenRunningAtShutdown(bool warn); bool warnWhenRunningAtShutdown() const; public Q_SLOTS: void start(); void cancel(); Q_SIGNALS: void info(const QString &message, int timeout = 0); void progress(const QString &message, int current, int total); void finished(); void canceled(); private: virtual void doStart() = 0; virtual void doCancel() = 0; private: void applyWindowID(QWidget *wid) const override; protected: void addTemporaryView(const QString &title, AbstractKeyListSortFilterProxyModel *proxy = nullptr, const QString &tabToolTip = QString()); protected: class Private; kdtools::pimpl_ptr d; protected: explicit Command(Private *pp); explicit Command(QAbstractItemView *view, Private *pp); explicit Command(const std::vector &keys, Private *pp); explicit Command(const GpgME::Key &key, Private *pp); }; } Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::Command::Restrictions)