Line data Source code
1 : // SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief UsersDialog, the recipient picker for a folder's `.gpg-id`.
6 : */
7 : #ifndef SRC_USERSDIALOG_H_
8 : #define SRC_USERSDIALOG_H_
9 :
10 : #include "appsettings.h"
11 : #include "userinfo.h"
12 :
13 : #include <QDialog>
14 : #include <QList>
15 : #include <QRegularExpression>
16 : #include <QScopedPointer>
17 : #include <QSet>
18 :
19 : namespace Ui {
20 : class UsersDialog;
21 : }
22 :
23 : class Pass;
24 : class QKeyEvent;
25 : class QListWidgetItem;
26 :
27 : /**
28 : * @class UsersDialog
29 : * @ingroup keys
30 : * @brief Dialog for selecting GPG recipients for password encryption.
31 : *
32 : * UsersDialog allows users to select which GPG keys to encrypt passwords to.
33 : * It displays available keys, supports filtering, and shows key expiration
34 : * status.
35 : */
36 : class UsersDialog : public QDialog {
37 12363 : Q_OBJECT
38 :
39 : public:
40 : /** @brief Whether the recipients on disk are ticked when it opens. */
41 : enum class Preselect {
42 : FromList, ///< The folder's recipient list, as the backend reads it.
43 : Nothing, ///< None: for a store that had no list when it was chosen.
44 : };
45 :
46 : /**
47 : * @brief Construct users dialog.
48 : * @param pass Active Pass backend.
49 : * @param s Application settings snapshot.
50 : * @param dir Password store directory path.
51 : * @param parent Parent widget.
52 : */
53 : explicit UsersDialog(Pass *pass, const AppSettings &s, QString dir,
54 : QWidget *parent = nullptr);
55 : /**
56 : * @brief Construct users dialog, saying what to preselect.
57 : * @param pass Active Pass backend.
58 : * @param s Application settings snapshot.
59 : * @param dir Password store directory path.
60 : * @param preselect What is ticked when it opens.
61 : * @param parent Parent widget.
62 : */
63 : UsersDialog(Pass *pass, const AppSettings &s, QString dir,
64 : Preselect preselect, QWidget *parent = nullptr);
65 : /**
66 : * @brief Destroys the dialog and its generated form.
67 : */
68 : ~UsersDialog() override;
69 :
70 : /**
71 : * @brief Choose whether OK runs Pass::Init on the folder.
72 : *
73 : * On by default. Off, the dialog only collects the selection; the caller
74 : * reads selectedUsers() and initialises the folder itself (new profiles,
75 : * which must not go through the backend of the active store).
76 : * @param initOnAccept false to skip Pass::Init.
77 : */
78 3 : void setInitOnAccept(bool initOnAccept) { m_initOnAccept = initOnAccept; }
79 :
80 : /**
81 : * @brief The key list with the user's choice in each entry's enabled flag.
82 : * @return All listed users; the selected ones have enabled == true.
83 : */
84 : [[nodiscard]] auto selectedUsers() const -> QList<UserInfo> {
85 : return m_userList;
86 : }
87 :
88 : /**
89 : * @brief Whether at least one key is ticked.
90 : * @return true if Init would get a recipient.
91 : */
92 : [[nodiscard]] auto hasSelection() const -> bool;
93 :
94 : public slots:
95 : /**
96 : * @brief Handle dialog acceptance.
97 : */
98 : void accept() override;
99 :
100 : protected:
101 : /**
102 : * @brief Handle key press.
103 : * @param event Key event.
104 : */
105 : void keyPressEvent(QKeyEvent *event) override;
106 :
107 : private slots:
108 : /**
109 : * @brief Handle user selection change.
110 : * @param item Changed list item.
111 : */
112 : void itemChange(QListWidgetItem *item);
113 : /**
114 : * @brief Filter user list.
115 : * @param filter Filter text.
116 : */
117 : void on_lineEdit_textChanged(const QString &filter);
118 : /**
119 : * @brief Handle select all checkbox.
120 : */
121 : void on_checkBox_clicked();
122 : /**
123 : * @brief Import a GPG key and refresh the list.
124 : */
125 : void on_importKeyButton_clicked();
126 : /**
127 : * @brief Make the key gpg just imported the current row. gpg reports a
128 : * 16-character long ID (IMPORTED) or a 40-character fingerprint
129 : * (IMPORT_OK), so the two are compared both ways, by suffix.
130 : * @param importedKey What gpg reported.
131 : */
132 : void selectImportedKey(const QString &importedKey);
133 :
134 : private:
135 : /// The widgets from usersdialog.ui.
136 : QScopedPointer<Ui::UsersDialog> ui;
137 : Pass *m_pass; /**< Active Pass backend */
138 : QString m_passStore; /**< Password store root path */
139 : QString m_gpgExe; /**< GPG executable path */
140 : QList<UserInfo> m_userList; /**< List of available GPG users */
141 : bool m_initOnAccept{true}; /**< Whether accept() runs Pass::Init. */
142 : /// Enable OK only while at least one key is ticked (hasSelection()).
143 : void updateOkButton();
144 : QString m_dir; /**< Password store directory */
145 : Preselect m_preselect = Preselect::FromList; /**< What opens ticked */
146 : /// Whether loadRecipients() read the folder's list, so accept() has
147 : /// something to compare the list on disk with.
148 : bool m_listRead = false;
149 : /// How the list stood up when the dialog read it: a
150 : /// Pass::RecipientsForEditing::State, kept as its value since Pass is only
151 : /// declared here.
152 : int m_readState = 0;
153 : /// The recipients the list named when the dialog read it.
154 : QStringList m_readRecipients;
155 : QString m_lastFilter; /**< Last filter text for caching */
156 : /**
157 : * @brief `.gpg-id` lines selectRecipients() listed unticked without asking
158 : * gpg. They are shown without "Show unusable keys", so OK does not drop
159 : * them unseen.
160 : */
161 : QSet<QString> m_notLookedUp;
162 : QString m_cachedPatternString; /**< Cached pattern string */
163 : QRegularExpression m_cachedNameFilter; /**< Cached regex filter */
164 : mutable QDateTime m_cachedCurrentDateTime; /**< Cached current date/time for
165 : expiry checks */
166 : mutable bool m_cachedDateTimeValid =
167 : false; /**< Whether cached date/time is valid */
168 :
169 : /// Attach WindowStateStore, which restores the dialog's saved geometry
170 : /// now and saves it under "usersDialog" when the dialog closes.
171 : void restoreDialogState();
172 :
173 : /**
174 : * @brief Connect dialog signals.
175 : */
176 : void connectSignals();
177 :
178 : /**
179 : * @brief Load GPG keys and determine secret key status.
180 : * @return true if successful, false if keys could not be loaded.
181 : */
182 : auto loadGpgKeys() -> bool;
183 :
184 : /**
185 : * @brief Mark which keys have secret counterparts.
186 : * @param users List of users to mark.
187 : */
188 : void markSecretKeys(QList<UserInfo> &users);
189 :
190 : /**
191 : * @brief Load recipients and handle missing keys.
192 : */
193 : void loadRecipients();
194 :
195 : /**
196 : * @brief Whether the folder's recipient list reads differently now from
197 : * when loadRecipients() read it: a pull, a sync or another program
198 : * changed it while the dialog was open.
199 : * @return True when the state or the recipients differ.
200 : */
201 : [[nodiscard]] auto listChangedSinceRead() const -> bool;
202 : /**
203 : * @brief Show @p text above the key list in bold red, for a recipient list
204 : * that could not be verified.
205 : * @param text The warning, plain text.
206 : * @param name The banner's object name.
207 : */
208 : void showRecipientWarning(
209 : const QString &text,
210 : const QString &name = QStringLiteral("recipientWarning"));
211 : /**
212 : * @brief Name the folders below this one that have a recipient list of
213 : * their own: saving here does not change theirs, so someone removed here
214 : * can still read what is in them. Folders below that cannot be read are
215 : * named too, since whether they have one is unknown.
216 : */
217 : void showNestedLists();
218 : /**
219 : * @brief Tick the keys the folder is encrypted to, and list recipients no
220 : * key in the keyring answers to, so saving does not drop them silently.
221 : * A line that names a key by a subkey or a secondary user ID ticks that
222 : * key, so unticking it removes the line too.
223 : * @param listed The folder's recipients, as listed in its `.gpg-id`.
224 : */
225 : void selectRecipients(const QStringList &listed);
226 : /**
227 : * @brief List @p line as a ticked row of its own, named by @p why, so
228 : * OK writes it back as it was.
229 : * @param line The `.gpg-id` line.
230 : * @param why Why it is kept as written, shown after " ?? ".
231 : */
232 : void keepAsWritten(const QString &line, const QString &why);
233 : /**
234 : * @brief Tick the rows of the keys @p ids, which a probe of @p lines
235 : * reached. A key on more than one row (a shared subkey) is ticked only
236 : * by a line naming its own primary key; other lines are kept as written.
237 : * @param ids The key IDs the probe reached.
238 : * @param lines The lines that were probed.
239 : * @return false, ticking nothing, when one of @p ids has no row.
240 : */
241 : auto tickReached(const QStringList &ids, const QStringList &lines) -> bool;
242 : /**
243 : * @brief Keep as written, and warn about, the lines a key ID or
244 : * fingerprint names that two key blocks hold, or every line when the
245 : * keyring cannot be listed; those lines leave @p recipients.
246 : * @param recipients The lines still to settle; updated.
247 : * @return Whether any line is left to settle.
248 : */
249 : auto setAsideHeldTwice(QStringList *recipients) -> bool;
250 : /**
251 : * @brief After gpg refused the list as a whole, probe each line on its
252 : * own (address and name lines first, at most ten), tick what each
253 : * reaches, and keep the lines gpg cannot encrypt to as written.
254 : * @param recipients The lines still to settle; left holding the ones a
255 : * probe could not settle.
256 : * @return Whether any line is left for the keyring search.
257 : */
258 : auto probeEachLine(QStringList *recipients) -> bool;
259 : /**
260 : * @brief Tick the keys a keyring search resolves @p recipients to, and
261 : * list the lines no key answers to, or that were not looked up, as rows
262 : * of their own.
263 : * @param recipients The lines a probe could not settle.
264 : */
265 : void searchKeyring(const QStringList &recipients);
266 :
267 : /**
268 : * @brief Populate user list.
269 : * @param filter Optional filter text.
270 : */
271 : void populateList(const QString &filter = QString());
272 : /**
273 : * @brief Check if user passes filter.
274 : * @param user User to check.
275 : * @param filter Text filter.
276 : * @param nameFilter Regex filter.
277 : * @return true if user passes.
278 : */
279 : bool passesFilter(const UserInfo &user, const QString &filter,
280 : const QRegularExpression &nameFilter) const;
281 : /**
282 : * @brief Check if GPG key is expired.
283 : * @param user User to check.
284 : * @return true if key is expired.
285 : */
286 : auto isUserExpired(const UserInfo &user) const -> bool;
287 : /**
288 : * @brief Build display text for user: the user ID and key ID on a line
289 : * each, each cut to 256 characters (the row only shows them).
290 : * @param user User info.
291 : * @return Formatted text.
292 : */
293 : QString buildUserText(const UserInfo &user) const;
294 : /**
295 : * @brief Apply visual styling to user item.
296 : * @param item List item.
297 : * @param user User info.
298 : */
299 : void applyUserStyling(QListWidgetItem *item, const UserInfo &user) const;
300 : };
301 :
302 : #endif // SRC_USERSDIALOG_H_
|