Branch data Line data Source code
1 : : // SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2 : : // SPDX-License-Identifier: GPL-3.0-or-later
3 : : /**
4 : : * @file
5 : : * @brief Settings dialog: every page of the configuration window and the
6 : : * profile table, with the validation that gates OK.
7 : : */
8 : : #ifndef SRC_CONFIGDIALOG_H_
9 : : #define SRC_CONFIGDIALOG_H_
10 : :
11 : : #include "appsettings.h"
12 : : #include "enums.h"
13 : : #include "passwordconfiguration.h"
14 : : #include "profile.h"
15 : :
16 : : #include <QDialog>
17 : : #include <QHash>
18 : : #include <QProcessEnvironment>
19 : : #include <QSet>
20 : : #include <optional>
21 : :
22 : : namespace Ui {
23 : : class ConfigDialog;
24 : : } // namespace Ui
25 : :
26 : : class QLabel;
27 : :
28 : : /**
29 : : * @class ConfigDialog
30 : : * @ingroup ui
31 : : * @brief The ConfigDialog handles the configuration interface.
32 : : *
33 : : * Presents controls for profiles, generation tools (pwgen/pass),
34 : : * GPG/git/OTP/qrencode integration, template handling, tray icon and
35 : : * autoclear options, and exposes methods to read and update those settings.
36 : : */
37 : : class ConfigDialog : public QDialog {
38 : 577 : Q_OBJECT
39 : :
40 : : public:
41 : : /**
42 : : * @brief Construct a ConfigDialog associated with the given main window.
43 : : * @param parent Parent widget, usually the main window.
44 : : */
45 : : explicit ConfigDialog(QWidget *parent);
46 : : ~ConfigDialog() override;
47 : :
48 : : /**
49 : : * @brief Whether a signing-key setting holds only full key fingerprints
50 : : * (40 or 64 hexadecimal characters each, space separated); empty passes.
51 : : * @param setting The profile's signing key field.
52 : : * @return true when every token is a fingerprint.
53 : : */
54 : : static auto isFingerprintList(const QString &setting) -> bool;
55 : :
56 : : /**
57 : : * @brief Enable or disable clipboard selection mode.
58 : : * @param useSelection true to enable selection mode.
59 : : */
60 : : void useSelection(bool useSelection);
61 : :
62 : : /**
63 : : * @brief Return all configured profiles.
64 : : * @return Hash of profile name to key-value settings map.
65 : : */
66 : : auto getProfiles() -> Profiles;
67 : :
68 : : /**
69 : : * @brief Show or hide the system tray icon.
70 : : * @param useSystray true to enable the tray icon.
71 : : */
72 : : void useTrayIcon(bool useSystray);
73 : :
74 : : /**
75 : : * @brief Enable or disable git integration.
76 : : * @param useGit true to enable git; ignored without a git path.
77 : : */
78 : : void useGit(bool useGit);
79 : :
80 : : /**
81 : : * @brief Enable or disable OTP support.
82 : : * @param useOtp true to enable OTP.
83 : : */
84 : : void useOtp(bool useOtp);
85 : :
86 : : /**
87 : : * @brief Enable or disable grep content search.
88 : : * @param useGrepSearch true to enable grep search.
89 : : */
90 : : void useGrepSearch(bool useGrepSearch);
91 : :
92 : : /**
93 : : * @brief Enable or disable QR code display.
94 : : * @param useQrencode true to enable qrencode.
95 : : */
96 : : void useQrencode(bool useQrencode);
97 : :
98 : : /**
99 : : * @brief Set the path to the pwgen executable.
100 : : * @param path Path to pwgen.
101 : : */
102 : : void setPwgenPath(const QString &path);
103 : :
104 : : /**
105 : : * @brief Enable or disable pwgen-based password generation.
106 : : * @param usePwgen true to use pwgen.
107 : : */
108 : : void usePwgen(bool usePwgen);
109 : :
110 : : /**
111 : : * @brief Apply a password configuration to the dialog controls.
112 : : * @param config Configuration to display.
113 : : */
114 : : void setPasswordConfiguration(const PasswordConfiguration &config);
115 : :
116 : : /**
117 : : * @brief Read the current password configuration from the dialog controls.
118 : : * @return Current PasswordConfiguration.
119 : : */
120 : : auto getPasswordConfiguration() -> PasswordConfiguration;
121 : :
122 : : protected:
123 : : /**
124 : : * @brief Forget the signing keys gpg did not know while the dialog is in
125 : : * the background, and check them again once it is back in front.
126 : : * @param event The change; only activation changes matter here.
127 : : */
128 : : void changeEvent(QEvent *event) override;
129 : :
130 : : private slots:
131 : : /**
132 : : * @brief On OK: warn about a dubious SSH_AUTH_SOCK override, save the
133 : : * settings and the profiles, let the active profile follow a rename, and
134 : : * create and initialise the stores of profiles added in this session.
135 : : */
136 : : void on_accepted();
137 : : /**
138 : : * @brief Fill the pass, gpg (gpg2 first), git and pwgen paths from PATH,
139 : : * keeping a field as it is when nothing is found, and choose the pass
140 : : * backend when pass was found.
141 : : */
142 : : void on_autodetectButton_clicked();
143 : : /// Native (gpg/git) backend chosen: enable its group box, disable pass's.
144 : : void on_radioButtonNative_clicked();
145 : : /// pass backend chosen: enable its group box, disable the native one.
146 : : void on_radioButtonPass_clicked();
147 : : /**
148 : : * @brief Pick the git executable in a file dialog; a choice enables "use
149 : : * Git", a cancelled dialog keeps the field and the checkbox.
150 : : */
151 : : void on_toolButtonGit_clicked();
152 : : /// Pick the gpg executable in a file dialog; cancel keeps the field.
153 : : void on_toolButtonGpg_clicked();
154 : : /// Open KeygenDialog for the gpg shown in the dialog.
155 : : void on_pushButtonGenerateKey_clicked();
156 : : /**
157 : : * @brief Pick the pwgen executable; a choice enables "use pwgen", a
158 : : * cancelled dialog keeps the field and the checkbox.
159 : : */
160 : : void on_toolButtonPwgen_clicked();
161 : : /// Pick the pass executable in a file dialog; cancel keeps the field.
162 : : void on_toolButtonPass_clicked();
163 : : /// Pick the password store folder for the pass backend's store path.
164 : : void on_toolButtonStore_clicked();
165 : : /**
166 : : * @brief Enable the controls that only mean something with a clipboard
167 : : * (selection, hiding, autoclear) unless "No clipboard" is chosen.
168 : : * @param index The combo box row; 0 is "No clipboard".
169 : : */
170 : : void on_comboBoxClipboard_activated(int index);
171 : : /**
172 : : * @brief Show the chosen character set's characters in the line edit,
173 : : * editable only for the custom set.
174 : : * @param index A PasswordConfiguration::CharacterSet value.
175 : : */
176 : : void on_passwordCharTemplateSelector_activated(int index);
177 : : /// Re-apply the clipboard gating for the current clipboard mode.
178 : : void on_checkBoxSelection_clicked();
179 : : /**
180 : : * @brief Append a "New profile" row pointing at the global store path,
181 : : * select it and put the cursor in the name field.
182 : : */
183 : : void on_addButton_clicked();
184 : : /**
185 : : * @brief Remove the selected profile row (a message when none is); the
186 : : * list's currentRowChanged re-points the form at a neighbour.
187 : : */
188 : : void on_deleteButton_clicked();
189 : : /// Pick the selected profile's store folder and store it in its entry.
190 : : void on_profilePathBrowse_clicked();
191 : : /// Hide-on-close and start-minimised are only offered with a tray icon.
192 : : void on_checkBoxUseTrayIcon_clicked();
193 : : /**
194 : : * @brief Enable the Git-dependent boxes (add .gpg-id, auto pull/push, and
195 : : * in native mode the parent-repository switch) with "use Git".
196 : : */
197 : : void on_checkBoxUseGit_clicked();
198 : : /**
199 : : * @brief Enable pwgen's flags or the character-set controls, whichever
200 : : * the "use pwgen" box selects; nothing in pass mode, where pass generates.
201 : : */
202 : : void on_checkBoxUsePwgen_clicked();
203 : : /**
204 : : * @brief The profile list's current row changed: show that profile.
205 : : * @param row The new row, -1 for none.
206 : : */
207 : : void onProfileSelected(int row);
208 : : /**
209 : : * @brief The name field was typed in: store it in the entry and the list
210 : : * item, then validate (duplicates, empty).
211 : : * @param name The text as typed.
212 : : */
213 : : void onProfileNameEdited(const QString &name);
214 : : /**
215 : : * @brief The path field was typed in: store it in the entry, validate and
216 : : * refresh the status line.
217 : : * @param path The text as typed.
218 : : */
219 : : void onProfilePathEdited(const QString &path);
220 : : /**
221 : : * @brief The signing-key field was typed in: store it in the entry and
222 : : * validate (fingerprints only, shared keys).
223 : : * @param key The text as typed.
224 : : */
225 : : void onProfileSigningKeyEdited(const QString &key);
226 : : /**
227 : : * @brief One of the profile's Git boxes was clicked: all three flags
228 : : * become the profile's own (no longer following the globals), and push
229 : : * and pull are enabled only with Git.
230 : : */
231 : : void onProfileGitToggled();
232 : :
233 : : private:
234 : : /// The secret-fields and every-field checkboxes only while passwords are
235 : : /// hidden; the secret words and "Default" only while secret fields are
236 : : /// hidden and every field is not.
237 : : void updateSecretFieldsEnabled();
238 : : /**
239 : : * @brief One row of the profile list while the dialog is open. A QList
240 : : * rather than the Profiles map so a profile can pass through an empty or
241 : : * duplicate name while it is being typed; getProfiles() builds the map.
242 : : */
243 : : struct ProfileEntry {
244 : : QString name; ///< The name as typed; may be empty or a duplicate.
245 : : Profile profile; ///< Path, signing key and Git flags as edited.
246 : : /// The name the profile had when the dialog opened (empty for one added
247 : : /// here), so a rename of the active profile can follow it.
248 : : QString originalName;
249 : : };
250 : : /// What is wrong with one profile's fields; empty strings where nothing is.
251 : : struct ProfileProblems {
252 : : QString name; ///< Empty or duplicate name; empty when fine.
253 : : QString path; ///< Missing store path; empty when fine.
254 : : QString key; ///< Signing key that is no fingerprint list; empty when fine.
255 : : /// Not a problem, a warning: other profiles sign with the same key.
256 : : /// It neither blocks OK nor marks the row.
257 : : QString sharedKey;
258 : : /**
259 : : * @brief Whether all three fields are fine.
260 : : * @return true when name, path and key have no complaint.
261 : : */
262 : : [[nodiscard]] auto none() const -> bool {
263 [ + + + + : 557 : return name.isEmpty() && path.isEmpty() && key.isEmpty();
+ + + + +
+ + + + +
+ + + + ]
264 : : }
265 : : /**
266 : : * @brief The first complaint, for a tooltip on the profile's row.
267 : : * @return The name, path or key complaint, in that order; empty if none.
268 : : */
269 : 15 : [[nodiscard]] auto first() const -> QString {
270 [ + + + + ]: 15 : return !name.isEmpty() ? name : !path.isEmpty() ? path : key;
271 : : }
272 : : };
273 : : /// Designer tooltip of the profile-name field, put back when validate()
274 : : /// has no complaint about it.
275 : : QString m_nameTip;
276 : : /// Designer tooltip of the store-path field, put back when validate() has
277 : : /// no complaint about it.
278 : : QString m_pathTip;
279 : : /// Designer tooltip of the signing-key field, put back when validate() has
280 : : /// no complaint about it.
281 : : QString m_keyTip;
282 : : /// Below the signing key: the profiles that share it, if any.
283 : : QLabel *m_sharedKeyWarning = nullptr;
284 : : /// In the pass box: the stores pass would commit outside of, if any.
285 : : QLabel *m_passGitWarning = nullptr;
286 : : /// "Use Git" was on when the git path field was emptied; the next path
287 : : /// turns it on again.
288 : : bool m_gitOnBeforeEmptyPath = false;
289 : : /// Primary fingerprint per gpg and signing-key token (empty: gpg does
290 : : /// not know it), so each key is looked up once, not on every keystroke.
291 : : mutable QHash<QString, QString> m_primaryOf;
292 : : /// An unknown key was forgotten in the background: validate() again on
293 : : /// return.
294 : : bool m_recheckSigners = false;
295 : : /// The settings naming the GPG home those lookups run in, read on first
296 : : /// use.
297 : : mutable std::optional<AppSettings> m_gpgSettings;
298 : : /// The profiles as shown; index == row in profileList.
299 : : QList<ProfileEntry> m_entries;
300 : : /// Row whose fields the form shows, -1 for none.
301 : : int m_currentEntry = -1;
302 : : /// True while loadProfileForm() sets the fields, so their edited-signals
303 : : /// do not write back.
304 : : bool m_loadingForm = false;
305 : :
306 : : /**
307 : : * @brief Show row @p row's profile in the form (or clear and disable it
308 : : * for -1), with m_loadingForm set so the fields' edited-signals do not
309 : : * write back. The Git boxes show the profile's own flags, else the
310 : : * global ones.
311 : : * @param row Row in m_entries, or -1 for none.
312 : : */
313 : : void loadProfileForm(int row);
314 : : /**
315 : : * @brief The entry the form is editing.
316 : : * @return A pointer into m_entries, or nullptr when no row is current.
317 : : */
318 : : auto currentEntry() -> ProfileEntry *;
319 : : /**
320 : : * @brief Refresh the status line under the form: the profile's name and
321 : : * cleaned path, marked "New" while the folder does not exist, or a
322 : : * reminder to fill in the required fields.
323 : : */
324 : : void updateProfileStatus();
325 : : /// The widgets from configdialog.ui.
326 : : QScopedPointer<Ui::ConfigDialog> ui;
327 : :
328 : : /**
329 : : * @brief Show @p path as the git executable; an empty path disables and
330 : : * turns off "use Git".
331 : : * @param path The git executable, or empty for none.
332 : : */
333 : : void setGitPath(const QString &path);
334 : : /**
335 : : * @brief Fill the profile list and m_entries from @p profiles, dropping
336 : : * the empty-named leftover older versions wrote, select
337 : : * @p currentProfile and validate.
338 : : * @param profiles The saved profiles.
339 : : * @param currentProfile The active profile's name, to select its row.
340 : : */
341 : : void setProfiles(Profiles profiles, const QString ¤tProfile);
342 : : /**
343 : : * @brief Tick the pass or the native radio button and enable the matching
344 : : * group box.
345 : : * @param usePass true for the pass backend.
346 : : */
347 : : void usePass(bool usePass);
348 : :
349 : : /**
350 : : * @brief Load the dialog's value widgets from an AppSettings struct.
351 : : *
352 : : * Mirrors the legacy per-getter constructor loads. Sets values only; the
353 : : * constructor still handles environment-dependent gating (tray/OTP/qrencode
354 : : * availability, clipboard combo population, primary-selection support) and
355 : : * the profile table. Password-generation and pwgen-path widgets are
356 : : * intentionally not loaded here, matching the previous behaviour.
357 : : * @param settings Values to display.
358 : : */
359 : : void applySettings(const AppSettings &settings);
360 : : /**
361 : : * @brief Build an AppSettings struct from the dialog's widgets.
362 : : *
363 : : * Starts from the currently persisted settings and overwrites only the
364 : : * fields this dialog owns, so unrelated keys (window geometry,
365 : : * profiles, etc.) are preserved on save.
366 : : * @return Settings reflecting the dialog state.
367 : : */
368 : : auto readSettings() -> AppSettings;
369 : :
370 : : /**
371 : : * @brief Enable the group box of the chosen backend and, in pass mode,
372 : : * disable the password-generation controls pass takes care of; in native
373 : : * mode hand them back to on_checkBoxUsePwgen_clicked().
374 : : */
375 : : void setGroupBoxState();
376 : : /**
377 : : * @brief Make "use pwgen" follow the pwgen path field: enabled with a path
378 : : * in native mode, unticked and disabled without one, then let
379 : : * on_checkBoxUsePwgen_clicked() set the generator controls.
380 : : */
381 : : void updatePwgenCheckbox();
382 : : /**
383 : : * @brief Make "use Git" follow the git path field: enabled with a path,
384 : : * turned off and disabled without one. A path that comes back after the
385 : : * field was emptied turns Git on again if it was on before.
386 : : */
387 : : void updateGitCheckbox();
388 : : /**
389 : : * @brief Ask for an existing file in a read-only file dialog.
390 : : * @return The chosen file, empty when cancelled.
391 : : */
392 : : auto selectExecutable() -> QString;
393 : : /**
394 : : * @brief Ask for a directory in a directories-only file dialog.
395 : : * @return The chosen folder, empty when cancelled.
396 : : */
397 : : auto selectFolder() -> QString;
398 : : /**
399 : : * @brief QMessageBox::critical with a single OK button. Once a workaround
400 : : * for crashes in Qt 5.4.1 before QApplication::exec(); no caller today.
401 : : * @param title The box's title.
402 : : * @param text The message.
403 : : */
404 : : void criticalMessage(const QString &title, const QString &text);
405 : :
406 : : /**
407 : : * @brief Whether qrencode can run: @p configuredPath when it is an
408 : : * executable file, else a `qrencode` on Util's PATH, which is then saved
409 : : * as the qrencode executable. Always false on Windows.
410 : : * @param configuredPath The saved qrencode executable, possibly empty.
411 : : * @return true when QR codes can be shown.
412 : : */
413 : : auto isQrencodeAvailable(const QString &configuredPath) -> bool;
414 : : /**
415 : : * @brief Check every profile (problemsOf(), sharingTheKey()), mark the
416 : : * rows and the form, enable OK only when no profile has a problem, and
417 : : * refresh the pass Git warning.
418 : : */
419 : : void validate();
420 : : /// Shows m_passGitWarning for the stores Util::repositoryPassClimbsTo()
421 : : /// names, while the pass backend is chosen.
422 : : void updatePassGitWarning();
423 : : /**
424 : : * @brief What is wrong with @p entry's name, store path and signing keys.
425 : : * @param entry The profile to check.
426 : : * @param names How many profiles carry each case-folded name, for
427 : : * duplicates.
428 : : * @return The complaints, empty where a field is fine.
429 : : */
430 : : auto problemsOf(const ProfileEntry &entry,
431 : : const QHash<QString, int> &names) const -> ProfileProblems;
432 : : /**
433 : : * @brief Mark row @p row of the profile list, and the form fields when it
434 : : * is the row being edited, with @p problems.
435 : : * @param row Row in the profile list.
436 : : * @param problems What problemsOf() found for it.
437 : : */
438 : : void showProblems(int row, const ProfileProblems &problems);
439 : : /// Per profile row: its store as the file system resolves it, and its
440 : : /// signing-key fingerprints (upper case).
441 : 252 : struct SigningStores {
442 : : QStringList stores; ///< Canonical (or cleaned) store path.
443 : : QList<QSet<QString>> keys; ///< Fingerprints of the signing key.
444 : : };
445 : : /**
446 : : * @brief Resolve every profile's store and fingerprints once.
447 : : * @return One entry per row of m_entries.
448 : : */
449 : : [[nodiscard]] auto signingStores() const -> SigningStores;
450 : : /**
451 : : * @brief One profile's signing keys as signers: each key as written, plus
452 : : * the primary fingerprint gpg gives for a complete key ID or (sub)key
453 : : * fingerprint.
454 : : * @param signingKey The profile's signing-key setting.
455 : : * @param env The environment gpg looks keys up in (its GPG home).
456 : : * @return Upper-case fingerprints and key IDs.
457 : : */
458 : : [[nodiscard]] auto signerKeys(const QString &signingKey,
459 : : const QProcessEnvironment &env) const
460 : : -> QSet<QString>;
461 : : /**
462 : : * @brief The other profiles, for other stores, whose signing keys share a
463 : : * fingerprint with row @p row's.
464 : : * @param row The profile's row.
465 : : * @param resolved What signingStores() gave for this validation.
466 : : * @return Their names, sorted; empty when none or no key is set.
467 : : */
468 : : [[nodiscard]] auto sharingTheKey(int row, const SigningStores &resolved) const
469 : : -> QStringList;
470 : :
471 : : /**
472 : : * @brief For every saved profile whose store was not a profile's store
473 : : * when the dialog opened: offer to create the folder and, when it has no
474 : : * `.gpg-id`, ask for recipients and initialise it.
475 : : * @param existingProfiles The profiles as they were when the dialog opened.
476 : : */
477 : : void initializeNewProfiles(const Profiles &existingProfiles);
478 : : /**
479 : : * @brief Offer to create a new profile's store folder when it is missing.
480 : : * @param path The folder, cleaned.
481 : : * @return Whether the folder exists now.
482 : : */
483 : : auto ensureProfileFolder(const QString &path) -> bool;
484 : : /**
485 : : * @brief Ask for a new profile's recipients and initialise its store with
486 : : * them, keeping the active backend's settings and queue on the active
487 : : * store (#1774).
488 : : * @param name The profile's name, for titles and messages.
489 : : * @param profile The profile.
490 : : * @param path Its store folder, cleaned.
491 : : */
492 : : void initialiseProfileStore(const QString &name, const Profile &profile,
493 : : const QString &path);
494 : : /// Warn, without refusing, when the SSH_AUTH_SOCK override does not look
495 : : /// like a socket; the value is saved as entered either way.
496 : : void warnAboutSshAuthSockOverride();
497 : : /**
498 : : * @brief Point the active profile at its new name after a rename (or at
499 : : * none after a deletion), and let its own Git flags replace the global
500 : : * ones, as switching to it does.
501 : : * @param profiles The profiles as saved.
502 : : */
503 : : void followActiveProfile(const Profiles &profiles);
504 : :
505 : : /// User-defined custom charset, retained while a builtin set is selected so
506 : : /// it is not lost when the line edit shows the builtin's characters instead.
507 : : QString m_customPasswordChars;
508 : : };
509 : :
510 : : #endif // SRC_CONFIGDIALOG_H_
|