File indexing completed on 2025-01-05 04:55:49

0001 /*  -*- c++ -*-
0002     keyrequester.h
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006 
0007     Based on kpgpui.h
0008     SPDX-FileCopyrightText: 2001, 2002 the KPGP authors
0009     See file libkdenetwork/AUTHORS.kpgp for details
0010 
0011     This file is part of KPGP, the KDE PGP/GnuPG support library.
0012 
0013     SPDX-License-Identifier: GPL-2.0-or-later
0014  */
0015 
0016 #pragma once
0017 
0018 #include "kleo_export.h"
0019 
0020 #include <QGpgME/Protocol>
0021 
0022 #include <QLabel>
0023 #include <QStringList>
0024 #include <QWidget>
0025 
0026 #include <vector>
0027 
0028 namespace GpgME
0029 {
0030 class Key;
0031 class KeyListResult;
0032 }
0033 
0034 class QString;
0035 class QPushButton;
0036 
0037 namespace Kleo
0038 {
0039 
0040 /// Base class for SigningKeyRequester and EncryptionKeyRequester
0041 class KLEO_EXPORT KeyRequester : public QWidget
0042 {
0043     Q_OBJECT
0044 public:
0045     explicit KeyRequester(unsigned int allowedKeys, bool multipleKeys = false, QWidget *parent = nullptr);
0046     // Constructor for Qt Designer
0047     explicit KeyRequester(QWidget *parent = nullptr);
0048     ~KeyRequester() override;
0049 
0050     const GpgME::Key &key() const;
0051     /** Preferred method to set a key for
0052         non-multi-KeyRequesters. Doesn't start a backend
0053         KeyListJob.
0054     */
0055     void setKey(const GpgME::Key &key);
0056 
0057     const std::vector<GpgME::Key> &keys() const;
0058     /** Preferred method to set a key for multi-KeyRequesters. Doesn't
0059         start a backend KeyListJob.
0060     */
0061     void setKeys(const std::vector<GpgME::Key> &keys);
0062 
0063     QString fingerprint() const;
0064     /** Set the key by fingerprint. Starts a background KeyListJob to
0065         retrieve the complete GpgME::Key object
0066     */
0067     void setFingerprint(const QString &fingerprint);
0068 
0069     QStringList fingerprints() const;
0070     /** Set the keys by fingerprint. Starts a background KeyListJob to
0071         retrieve the complete GpgME::Key objects
0072     */
0073     void setFingerprints(const QStringList &fingerprints);
0074 
0075     QPushButton *eraseButton();
0076     QPushButton *dialogButton();
0077 
0078     void setDialogCaption(const QString &caption);
0079     void setDialogMessage(const QString &message);
0080 
0081     bool isMultipleKeysEnabled() const;
0082     void setMultipleKeysEnabled(bool enable);
0083 
0084     unsigned int allowedKeys() const;
0085     void setAllowedKeys(unsigned int allowed);
0086 
0087     void setInitialQuery(const QString &s)
0088     {
0089         mInitialQuery = s;
0090     }
0091     const QString &initialQuery() const
0092     {
0093         return mInitialQuery;
0094     }
0095 
0096 Q_SIGNALS:
0097     void changed();
0098 
0099 private:
0100     void init();
0101     void startKeyListJob(const QStringList &fingerprints);
0102     void updateKeys();
0103 
0104 private Q_SLOTS:
0105     void slotNextKey(const GpgME::Key &key);
0106     void slotKeyListResult(const GpgME::KeyListResult &result);
0107     void slotDialogButtonClicked();
0108     void slotEraseButtonClicked();
0109 
0110 private:
0111     const QGpgME::Protocol *mOpenPGPBackend = nullptr;
0112     const QGpgME::Protocol *mSMIMEBackend = nullptr;
0113     QLabel *mComplianceIcon = nullptr;
0114     QLabel *mLabel = nullptr;
0115     QPushButton *mEraseButton = nullptr;
0116     QPushButton *mDialogButton = nullptr;
0117     QString mDialogCaption, mDialogMessage, mInitialQuery;
0118     bool mMulti;
0119     unsigned int mKeyUsage;
0120     int mJobs;
0121     std::vector<GpgME::Key> mKeys;
0122     std::vector<GpgME::Key> mTmpKeys;
0123 
0124 private:
0125     class Private;
0126     Private *const d;
0127 
0128 protected:
0129     virtual void virtual_hook(int, void *);
0130 };
0131 
0132 class KLEO_EXPORT EncryptionKeyRequester : public KeyRequester
0133 {
0134     Q_OBJECT
0135 public:
0136     enum { OpenPGP = 1, SMIME = 2, AllProtocols = OpenPGP | SMIME };
0137 
0138     /**
0139      * Preferred constructor
0140      */
0141     explicit EncryptionKeyRequester(bool multipleKeys = false,
0142                                     unsigned int proto = AllProtocols,
0143                                     QWidget *parent = nullptr,
0144                                     bool onlyTrusted = true,
0145                                     bool onlyValid = true);
0146     /**
0147      * Constructor for Qt designer
0148      */
0149     explicit EncryptionKeyRequester(QWidget *parent);
0150     ~EncryptionKeyRequester() override;
0151 
0152     void setAllowedKeys(unsigned int proto, bool onlyTrusted = true, bool onlyValid = true);
0153 
0154 private:
0155     class Private;
0156     Private *const d;
0157 
0158 protected:
0159     void virtual_hook(int, void *) override;
0160 };
0161 
0162 class KLEO_EXPORT SigningKeyRequester : public KeyRequester
0163 {
0164     Q_OBJECT
0165 public:
0166     enum { OpenPGP = 1, SMIME = 2, AllProtocols = OpenPGP | SMIME };
0167 
0168     /**
0169      * Preferred constructor
0170      * @param multipleKeys whether multiple keys can be selected
0171      *
0172      * @param proto the allowed protocols, OpenPGP and/or SMIME
0173      * @param parent the parent widget
0174      * @param onlyTrusted only show trusted keys
0175      * @param onlyValid only show valid keys
0176      */
0177     explicit SigningKeyRequester(bool multipleKeys = false,
0178                                  unsigned int proto = AllProtocols,
0179                                  QWidget *parent = nullptr,
0180                                  bool onlyTrusted = true,
0181                                  bool onlyValid = true);
0182     /**
0183      * Constructor for Qt designer
0184      */
0185     explicit SigningKeyRequester(QWidget *parent);
0186     ~SigningKeyRequester() override;
0187 
0188     /*
0189      * Those parameters affect the parameters given to the key selection dialog.
0190      * @param proto the allowed protocols, OpenPGP and/or SMIME
0191      * @param onlyTrusted only show trusted keys
0192      * @param onlyValid only show valid keys
0193      */
0194     void setAllowedKeys(unsigned int proto, bool onlyTrusted = true, bool onlyValid = true);
0195 
0196 private:
0197     class Private;
0198     Private *const d;
0199 
0200 protected:
0201     void virtual_hook(int, void *) override;
0202 };
0203 
0204 }