Line data Source code
1 : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Dialog that shows an ASCII-armored public key and lets the user copy
6 : * it or save it as a `.asc` file.
7 : */
8 : #ifndef SRC_EXPORTPUBLICKEYDIALOG_H_
9 : #define SRC_EXPORTPUBLICKEYDIALOG_H_
10 :
11 : #include <QDialog>
12 : #include <QScopedPointer>
13 : #include <QString>
14 :
15 : namespace Ui {
16 : class ExportPublicKeyDialog;
17 : }
18 :
19 : /**
20 : * @class ExportPublicKeyDialog
21 : * @ingroup keys
22 : * @brief Dialog showing an ASCII-armored public GPG key for sharing.
23 : *
24 : * Displays the armored key text and offers Copy-to-Clipboard and
25 : * Save-to-File actions so the user can hand the key to teammates.
26 : */
27 : class ExportPublicKeyDialog : public QDialog {
28 13 : Q_OBJECT
29 :
30 : public:
31 : /**
32 : * @brief Construct an ExportPublicKeyDialog.
33 : * @param keyId GPG key identifier or fingerprint shown in the header
34 : * and used as the default file name when saving.
35 : * @param armoredKey ASCII-armored public key text to display.
36 : * @param parent Optional parent widget.
37 : */
38 : explicit ExportPublicKeyDialog(const QString &keyId,
39 : const QString &armoredKey,
40 : QWidget *parent = nullptr);
41 : /** @brief Destroys the dialog and its generated UI. */
42 : ~ExportPublicKeyDialog() override;
43 :
44 : /**
45 : * @brief Reduce a key identifier to a filesystem-safe token.
46 : * @param keyId Raw identifier, possibly containing whitespace-separated
47 : * IDs or characters not valid in a filename.
48 : * @return The first whitespace-separated token of keyId with anything
49 : * outside [A-Za-z0-9_-] stripped. Empty if no characters survive.
50 : */
51 : static auto sanitizeKeyIdForFilename(const QString &keyId) -> QString;
52 :
53 : private slots:
54 : /**
55 : * @brief Copy the armored key text to the system clipboard; the button
56 : * reads "Copied!" for 1.5 s as feedback. Public keys are not secrets, so
57 : * this bypasses ClipboardManager and its autoclear.
58 : */
59 : void on_copyButton_clicked();
60 :
61 : /**
62 : * @brief Prompt for a destination and write the armored key to that file
63 : * through a QSaveFile. The suggested name is
64 : * `<sanitizeKeyIdForFilename(keyId)>.asc`, or `public_key.asc` when nothing
65 : * of the key id survives. Failures are reported in a warning box.
66 : */
67 : void on_saveButton_clicked();
68 :
69 : private:
70 : /** @brief Generated form (`exportpublickeydialog.ui`); owned. */
71 : QScopedPointer<Ui::ExportPublicKeyDialog> ui;
72 : /** @brief Key id as passed to the constructor; the basis of the suggested
73 : * file name. */
74 : QString m_keyId;
75 : /** @brief The copy button's label from the form, restored after the
76 : * "Copied!" flash. */
77 : QString m_copyButtonOriginalText;
78 : };
79 :
80 : #endif // SRC_EXPORTPUBLICKEYDIALOG_H_
|