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 15 : 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 : /**
54 : * @brief Write the armored key to @p fileName, refusing a symbolic link
55 : * there.
56 : *
57 : * QSaveFile writes to the target of a link at the chosen name. A folder in
58 : * a shared store can hold such a link, pushed by a co-writer under the name
59 : * the dialog proposes (the key's fingerprint), so the key would land
60 : * outside the store. A dangling link counts too.
61 : * @param fileName The chosen file.
62 : * @param text The armored key.
63 : * @param error Set to a message for the user when the save fails.
64 : * @return Whether the key was written.
65 : */
66 : static auto saveArmoredKey(const QString &fileName, const QString &text,
67 : QString *error) -> bool;
68 :
69 : private slots:
70 : /**
71 : * @brief Copy the armored key text to the system clipboard; the button
72 : * reads "Copied!" for 1.5 s as feedback. Public keys are not secrets, so
73 : * this bypasses ClipboardManager and its autoclear.
74 : */
75 : void on_copyButton_clicked();
76 :
77 : /**
78 : * @brief Prompt for a destination and write the armored key to that file
79 : * through saveArmoredKey(). The suggested name is
80 : * `<sanitizeKeyIdForFilename(keyId)>.asc`, or `public_key.asc` when nothing
81 : * of the key id survives. Failures are reported in a warning box.
82 : */
83 : void on_saveButton_clicked();
84 :
85 : private:
86 : /** @brief Generated form (`exportpublickeydialog.ui`); owned. */
87 : QScopedPointer<Ui::ExportPublicKeyDialog> ui;
88 : /** @brief Key id as passed to the constructor; the basis of the suggested
89 : * file name. */
90 : QString m_keyId;
91 : /** @brief The copy button's label from the form, restored after the
92 : * "Copied!" flash. */
93 : QString m_copyButtonOriginalText;
94 : };
95 :
96 : #endif // SRC_EXPORTPUBLICKEYDIALOG_H_
|