1 /*  -*- c++ -*-
2     keyselectiondialog.h
3 
4     This file is part of libkleopatra, the KDE keymanagement library
5     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
6 
7     Based on kpgpui.h
8     SPDX-FileCopyrightText: 2001, 2002 the KPGP authors
9     See file libkdenetwork/AUTHORS.kpgp for details
10 
11     SPDX-License-Identifier: GPL-2.0-or-later
12 */
13 
14 #pragma once
15 
16 #include "kleo_export.h"
17 
18 #include <gpgme++/key.h>
19 #include <qgpgme/protocol.h>
20 
21 #include <QDialog>
22 #include <QPixmap>
23 
24 #include <vector>
25 
26 class QCheckBox;
27 class QLabel;
28 class QPoint;
29 class QRegExp;
30 class QTimer;
31 class QVBoxLayout;
32 
33 namespace Kleo
34 {
35 class KeyListView;
36 class KeyListViewItem;
37 }
38 
39 namespace GpgME
40 {
41 class KeyListResult;
42 }
43 
44 namespace Kleo
45 {
46 
47 class KLEO_EXPORT KeySelectionDialog : public QDialog
48 {
49     Q_OBJECT
50 public:
51     enum Option {
52         RereadKeys = 0x01,
53         ExternalCertificateManager = 0x02,
54         ExtendedSelection = 0x04,
55         RememberChoice = 0x08
56     };
57     Q_DECLARE_FLAGS(Options, Option)
58 
59     enum KeyUsage {
60         PublicKeys = 1,
61         SecretKeys = 2,
62         EncryptionKeys = 4,
63         SigningKeys = 8,
64         ValidKeys = 16,
65         TrustedKeys = 32,
66         CertificationKeys = 64,
67         AuthenticationKeys = 128,
68         OpenPGPKeys = 256,
69         SMIMEKeys = 512,
70         AllKeys = PublicKeys | SecretKeys | OpenPGPKeys | SMIMEKeys,
71         ValidEncryptionKeys = AllKeys | EncryptionKeys | ValidKeys,
72         ValidTrustedEncryptionKeys = AllKeys | EncryptionKeys | ValidKeys | TrustedKeys
73     };
74 
75     explicit KeySelectionDialog(QWidget *parent = nullptr, Options options = Options());
76 
77     KeySelectionDialog(const QString &title,
78                        const QString &text,
79                        const std::vector<GpgME::Key> &selectedKeys = std::vector<GpgME::Key>(),
80                        unsigned int keyUsage = AllKeys,
81                        bool extendedSelection = false,
82                        bool rememberChoice = false,
83                        QWidget *parent = nullptr,
84                        bool modal = true);
85     KeySelectionDialog(const QString &title,
86                        const QString &text,
87                        const QString &initialPattern,
88                        const std::vector<GpgME::Key> &selectedKeys,
89                        unsigned int keyUsage = AllKeys,
90                        bool extendedSelection = false,
91                        bool rememberChoice = false,
92                        QWidget *parent = nullptr,
93                        bool modal = true);
94     KeySelectionDialog(const QString &title,
95                        const QString &text,
96                        const QString &initialPattern,
97                        unsigned int keyUsage = AllKeys,
98                        bool extendedSelection = false,
99                        bool rememberChoice = false,
100                        QWidget *parent = nullptr,
101                        bool modal = true);
102     ~KeySelectionDialog() override;
103 
104     void setText(const QString &text);
105 
106     void setKeys(const std::vector<GpgME::Key> &keys);
107 
108     /** Returns the key ID of the selected key in single selection mode.
109         Otherwise it returns a null key. */
110     const GpgME::Key &selectedKey() const;
111 
112     QString fingerprint() const;
113 
114     /** Returns a list of selected key IDs. */
selectedKeys()115     const std::vector<GpgME::Key> &selectedKeys() const
116     {
117         return mSelectedKeys;
118     }
119 
120     /// Return all the selected fingerprints
121     QStringList fingerprints() const;
122 
123     /// Return the selected openpgp fingerprints
124     QStringList pgpKeyFingerprints() const;
125     /// Return the selected smime fingerprints
126     QStringList smimeFingerprints() const;
127 
128     bool rememberSelection() const;
129 
130     // Could be used by derived classes to insert their own widget
topLayout()131     QVBoxLayout *topLayout() const
132     {
133         return mTopLayout;
134     }
135 
136 private Q_SLOTS:
137     void slotRereadKeys();
138     void slotStartCertificateManager(const QString &query = QString());
slotStartSearchForExternalCertificates()139     void slotStartSearchForExternalCertificates()
140     {
141         slotStartCertificateManager(mInitialQuery);
142     }
143     void slotKeyListResult(const GpgME::KeyListResult &);
144     void slotSelectionChanged();
slotCheckSelection()145     void slotCheckSelection()
146     {
147         slotCheckSelection(nullptr);
148     }
149     void slotCheckSelection(Kleo::KeyListViewItem *);
150     void slotRMB(Kleo::KeyListViewItem *, const QPoint &);
151     void slotRecheckKey();
152     void slotTryOk();
153     void slotOk();
154     void slotCancel();
155     void slotSearch(const QString &text);
156     void slotSearch();
157     void slotFilter();
158 
159 private:
160     void filterByKeyID(const QString &keyID);
161     void filterByKeyIDOrUID(const QString &keyID);
162     void filterByUID(const QString &uid);
163     void showAllItems();
164 
165     void connectSignals();
166     void disconnectSignals();
167 
168     void startKeyListJobForBackend(const QGpgME::Protocol *, const std::vector<GpgME::Key> &, bool);
169     void startValidatingKeyListing();
170 
171     void setUpUI(Options options, const QString &);
172     void init(bool, bool, const QString &, const QString &);
173 
174 private:
175     QVBoxLayout *mTopLayout = nullptr;
176     QLabel *mTextLabel = nullptr;
177     Kleo::KeyListView *mKeyListView = nullptr;
178     Kleo::KeyListViewItem *mCurrentContextMenuItem = nullptr;
179     QCheckBox *mRememberCB = nullptr;
180     QPushButton *mOkButton = nullptr;
181 
182     const QGpgME::Protocol *mOpenPGPBackend = nullptr;
183     const QGpgME::Protocol *mSMIMEBackend = nullptr;
184     std::vector<GpgME::Key> mSelectedKeys, mKeysToCheck;
185     unsigned int mKeyUsage;
186     QTimer *mCheckSelectionTimer = nullptr;
187     QTimer *mStartSearchTimer = nullptr;
188     // cross-eventloop temporaries:
189     QString mSearchText;
190     const QString mInitialQuery;
191     int mTruncated = 0,
192         mListJobCount = 0,
193         mSavedOffsetY = 0;
194 };
195 
196 }
197 
198 Q_DECLARE_OPERATORS_FOR_FLAGS(Kleo::KeySelectionDialog::Options)
199 
200