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

0001 /*  -*- c++ -*-
0002     keyselectiondialog.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     SPDX-License-Identifier: GPL-2.0-or-later
0012 */
0013 
0014 #pragma once
0015 
0016 #include "kleo_export.h"
0017 
0018 #include <QGpgME/Protocol>
0019 
0020 #include <QDialog>
0021 
0022 #include <gpgme++/key.h>
0023 
0024 #include <vector>
0025 
0026 class QCheckBox;
0027 class QLabel;
0028 class QPoint;
0029 class QTimer;
0030 class QVBoxLayout;
0031 
0032 namespace Kleo
0033 {
0034 class KeyListView;
0035 class KeyListViewItem;
0036 }
0037 
0038 namespace GpgME
0039 {
0040 class KeyListResult;
0041 }
0042 
0043 namespace Kleo
0044 {
0045 
0046 class KLEO_EXPORT KeySelectionDialog : public QDialog
0047 {
0048     Q_OBJECT
0049 public:
0050     enum Option {
0051         // clang-format off
0052         RereadKeys                 = 0x01,
0053         ExternalCertificateManager = 0x02,
0054         ExtendedSelection          = 0x04,
0055         RememberChoice             = 0x08,
0056         // clang-format on
0057     };
0058     Q_DECLARE_FLAGS(Options, Option)
0059 
0060     enum KeyUsage {
0061         // clang-format off
0062         PublicKeys                 = 1,
0063         SecretKeys                 = 2,
0064         EncryptionKeys             = 4,
0065         SigningKeys                = 8,
0066         ValidKeys                  = 16,
0067         TrustedKeys                = 32,
0068         CertificationKeys          = 64,
0069         AuthenticationKeys         = 128,
0070         OpenPGPKeys                = 256,
0071         SMIMEKeys                  = 512,
0072         AllKeys                    = PublicKeys | SecretKeys | OpenPGPKeys | SMIMEKeys,
0073         ValidEncryptionKeys        = AllKeys | EncryptionKeys | ValidKeys,
0074         ValidTrustedEncryptionKeys = AllKeys | EncryptionKeys | ValidKeys | TrustedKeys
0075         // clang-format on
0076     };
0077 
0078     explicit KeySelectionDialog(QWidget *parent = nullptr, Options options = Options());
0079 
0080     KeySelectionDialog(const QString &title,
0081                        const QString &text,
0082                        const std::vector<GpgME::Key> &selectedKeys = std::vector<GpgME::Key>(),
0083                        unsigned int keyUsage = AllKeys,
0084                        bool extendedSelection = false,
0085                        bool rememberChoice = false,
0086                        QWidget *parent = nullptr,
0087                        bool modal = true);
0088     KeySelectionDialog(const QString &title,
0089                        const QString &text,
0090                        const QString &initialPattern,
0091                        const std::vector<GpgME::Key> &selectedKeys,
0092                        unsigned int keyUsage = AllKeys,
0093                        bool extendedSelection = false,
0094                        bool rememberChoice = false,
0095                        QWidget *parent = nullptr,
0096                        bool modal = true);
0097     KeySelectionDialog(const QString &title,
0098                        const QString &text,
0099                        const QString &initialPattern,
0100                        unsigned int keyUsage = AllKeys,
0101                        bool extendedSelection = false,
0102                        bool rememberChoice = false,
0103                        QWidget *parent = nullptr,
0104                        bool modal = true);
0105     ~KeySelectionDialog() override;
0106 
0107     void setText(const QString &text);
0108 
0109     void setKeys(const std::vector<GpgME::Key> &keys);
0110 
0111     /** Returns the key ID of the selected key in single selection mode.
0112         Otherwise it returns a null key. */
0113     const GpgME::Key &selectedKey() const;
0114 
0115     QString fingerprint() const;
0116 
0117     /** Returns a list of selected key IDs. */
0118     const std::vector<GpgME::Key> &selectedKeys() const
0119     {
0120         return mSelectedKeys;
0121     }
0122 
0123     /// Return all the selected fingerprints
0124     QStringList fingerprints() const;
0125 
0126     /// Return the selected openpgp fingerprints
0127     QStringList pgpKeyFingerprints() const;
0128     /// Return the selected smime fingerprints
0129     QStringList smimeFingerprints() const;
0130 
0131     bool rememberSelection() const;
0132 
0133     // Could be used by derived classes to insert their own widget
0134     QVBoxLayout *topLayout() const
0135     {
0136         return mTopLayout;
0137     }
0138 
0139 private Q_SLOTS:
0140     void slotRereadKeys();
0141     void slotStartCertificateManager(const QString &query = QString());
0142     void slotStartSearchForExternalCertificates()
0143     {
0144         slotStartCertificateManager(mInitialQuery);
0145     }
0146     void slotKeyListResult(const GpgME::KeyListResult &);
0147     void slotSelectionChanged();
0148     void slotCheckSelection()
0149     {
0150         slotCheckSelection(nullptr);
0151     }
0152     void slotCheckSelection(Kleo::KeyListViewItem *);
0153     void slotRMB(Kleo::KeyListViewItem *, const QPoint &);
0154     void slotRecheckKey();
0155     void slotTryOk();
0156     void slotOk();
0157     void slotCancel();
0158     void slotSearch(const QString &text);
0159     void slotSearch();
0160     void slotFilter();
0161 
0162 private:
0163     void filterByKeyID(const QString &keyID);
0164     void filterByKeyIDOrUID(const QString &keyID);
0165     void filterByUID(const QString &uid);
0166     void showAllItems();
0167 
0168     void connectSignals();
0169     void disconnectSignals();
0170 
0171     void startKeyListJobForBackend(const QGpgME::Protocol *, const std::vector<GpgME::Key> &, bool);
0172     void startValidatingKeyListing();
0173 
0174     void setUpUI(Options options, const QString &);
0175     void init(bool, bool, const QString &, const QString &);
0176 
0177 private:
0178     QVBoxLayout *mTopLayout = nullptr;
0179     QLabel *mTextLabel = nullptr;
0180     Kleo::KeyListView *mKeyListView = nullptr;
0181     Kleo::KeyListViewItem *mCurrentContextMenuItem = nullptr;
0182     QCheckBox *mRememberCB = nullptr;
0183     QPushButton *mOkButton = nullptr;
0184 
0185     const QGpgME::Protocol *mOpenPGPBackend = nullptr;
0186     const QGpgME::Protocol *mSMIMEBackend = nullptr;
0187     std::vector<GpgME::Key> mSelectedKeys, mKeysToCheck;
0188     unsigned int mKeyUsage;
0189     QTimer *mCheckSelectionTimer = nullptr;
0190     QTimer *mStartSearchTimer = nullptr;
0191     // cross-eventloop temporaries:
0192     QString mSearchText;
0193     const QString mInitialQuery;
0194     int mTruncated = 0;
0195     int mListJobCount = 0;
0196     int mSavedOffsetY = 0;
0197 };
0198 
0199 }
0200 
0201 Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::KeySelectionDialog::Options)