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

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     commands/command.h
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 #pragma once
0011 
0012 #include <QObject>
0013 
0014 #include <qwindowdefs.h> // for WId
0015 
0016 #include <utils/pimpl_ptr.h>
0017 #include <utils/types.h> // for ExecutionContext
0018 
0019 #include <vector>
0020 
0021 class QModelIndex;
0022 template<typename T>
0023 class QList;
0024 class QAbstractItemView;
0025 
0026 namespace GpgME
0027 {
0028 class Key;
0029 }
0030 
0031 namespace Kleo
0032 {
0033 
0034 class KeyListController;
0035 class AbstractKeyListSortFilterProxyModel;
0036 
0037 class Command : public QObject, public ExecutionContext
0038 {
0039     Q_OBJECT
0040 public:
0041     explicit Command(KeyListController *parent);
0042     explicit Command(QAbstractItemView *view, KeyListController *parent);
0043     explicit Command(const GpgME::Key &key);
0044     explicit Command(const std::vector<GpgME::Key> &keys);
0045     ~Command() override;
0046 
0047     enum Restriction {
0048         // clang-format off
0049         NoRestriction      = 0x0000,
0050         NeedSelection      = 0x0001,
0051         OnlyOneKey         = 0x0002,
0052         NeedSecretKey      = 0x0004, //< command performs secret key operations
0053         NeedSecretKeyData  = 0x0008, //< command needs access to the secret key data
0054         MustBeOpenPGP      = 0x0010,
0055         MustBeCMS          = 0x0020,
0056 
0057         // esoteric:
0058         MayOnlyBeSecretKeyIfOwnerTrustIsNotYetUltimate = 0x0040, // for set-owner-trust
0059 
0060         AnyCardHasNullPin   = 0x0080,
0061         AnyCardCanLearnKeys = 0x0100,
0062 
0063         MustBeRoot          = 0x0200,
0064         MustBeTrustedRoot   = 0x0400 | MustBeRoot,
0065         MustBeUntrustedRoot = 0x0800 | MustBeRoot,
0066 
0067         MustBeValid         = 0x1000, //< key is neither revoked nor expired nor otherwise "bad"
0068 
0069         _AllRestrictions_Helper,
0070         AllRestrictions = 2 * (_AllRestrictions_Helper - 1) - 1,
0071         // clang-format on
0072     };
0073 
0074     Q_DECLARE_FLAGS(Restrictions, Restriction)
0075 
0076     static Restrictions restrictions()
0077     {
0078         return NoRestriction;
0079     }
0080 
0081     /** Classify the files and return the most appropriate commands.
0082      *
0083      * @param files: A list of files.
0084      *
0085      * @returns null QString on success. Error message otherwise.
0086      */
0087     static QList<Command *> commandsForFiles(const QStringList &files);
0088 
0089     /** Get a command for a query.
0090      *
0091      * @param query: A keyid / fingerprint or any string to use in the search.
0092      */
0093     static Command *commandForQuery(const QString &query);
0094 
0095     void setParentWidget(QWidget *widget);
0096     void setParentWId(WId wid);
0097     void setView(QAbstractItemView *view);
0098     void setKey(const GpgME::Key &key);
0099     void setKeys(const std::vector<GpgME::Key> &keys);
0100 
0101     void setAutoDelete(bool on);
0102     bool autoDelete() const;
0103 
0104     void setWarnWhenRunningAtShutdown(bool warn);
0105     bool warnWhenRunningAtShutdown() const;
0106 
0107 public Q_SLOTS:
0108     void start();
0109     void cancel();
0110 
0111 Q_SIGNALS:
0112     void info(const QString &message, int timeout = 0);
0113     void progress(int current, int total);
0114     void finished();
0115     void canceled();
0116 
0117 private:
0118     virtual void doStart() = 0;
0119     virtual void doCancel() = 0;
0120 
0121 private:
0122     void applyWindowID(QWidget *wid) const override;
0123 
0124 protected:
0125     void addTemporaryView(const QString &title, AbstractKeyListSortFilterProxyModel *proxy = nullptr, const QString &tabToolTip = QString());
0126 
0127 protected:
0128     class Private;
0129     kdtools::pimpl_ptr<Private> d;
0130 
0131 protected:
0132     explicit Command(Private *pp);
0133     explicit Command(QAbstractItemView *view, Private *pp);
0134     explicit Command(const std::vector<GpgME::Key> &keys, Private *pp);
0135     explicit Command(const GpgME::Key &key, Private *pp);
0136 };
0137 
0138 }
0139 
0140 Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::Command::Restrictions)