Branch data 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 Implements ExportPublicKeyDialog: key-id sanitising for the suggested
6 : : * file name, the clipboard copy with its "Copied!" flash and the QSaveFile
7 : : * write.
8 : : */
9 : : #include "exportpublickeydialog.h"
10 : : #include "plainmessagebox.h"
11 : : #include "ui_exportpublickeydialog.h"
12 : :
13 : : #include <QApplication>
14 : : #include <QClipboard>
15 : : #include <QFileDialog>
16 : : #include <QFileInfo>
17 : : #include <QFontDatabase>
18 : : #include <QMessageBox>
19 : : #include <QRegularExpression>
20 : : #include <QSaveFile>
21 : : #include <QTextStream>
22 : : #include <QTimer>
23 : :
24 : 7 : ExportPublicKeyDialog::ExportPublicKeyDialog(const QString &keyId,
25 : : const QString &armoredKey,
26 : 7 : QWidget *parent)
27 : 7 : : QDialog(parent), ui(new Ui::ExportPublicKeyDialog), m_keyId(keyId) {
28 : 7 : ui->setupUi(this);
29 : 14 : ui->keyIdLabel->setText(tr("Public key for %1").arg(keyId));
30 : 7 : ui->plainTextEdit->setFont(
31 [ + + ]: 14 : QFontDatabase::systemFont(QFontDatabase::FixedFont));
32 : 7 : ui->plainTextEdit->setPlainText(armoredKey);
33 : 7 : m_copyButtonOriginalText = ui->copyButton->text();
34 : 7 : }
35 : :
36 : 14 : ExportPublicKeyDialog::~ExportPublicKeyDialog() = default;
37 : :
38 : 13 : auto ExportPublicKeyDialog::sanitizeKeyIdForFilename(const QString &keyId)
39 : : -> QString {
40 : 15 : static const QRegularExpression whitespace(QStringLiteral("\\s+"));
41 : 15 : static const QRegularExpression unsafeChars(QStringLiteral("[^A-Za-z0-9_-]"));
42 : 13 : const QStringList tokens = keyId.split(whitespace, Qt::SkipEmptyParts);
43 [ + + ]: 13 : if (tokens.isEmpty()) {
44 : 2 : return {};
45 : : }
46 : : QString token = tokens.first();
47 : 11 : token.remove(unsafeChars);
48 : : return token;
49 : : }
50 : :
51 : 1 : void ExportPublicKeyDialog::on_copyButton_clicked() {
52 : 2 : QApplication::clipboard()->setText(ui->plainTextEdit->toPlainText());
53 : 1 : ui->copyButton->setText(tr("Copied!"));
54 : 1 : QTimer::singleShot(1500, this, [this]() {
55 : 1 : ui->copyButton->setText(m_copyButtonOriginalText);
56 : 1 : });
57 : 1 : }
58 : :
59 : 3 : void ExportPublicKeyDialog::on_saveButton_clicked() {
60 : 3 : QString safeKeyId = sanitizeKeyIdForFilename(m_keyId);
61 : : QString defaultName = safeKeyId.isEmpty()
62 [ + + ]: 4 : ? QStringLiteral("public_key.asc")
63 : 5 : : QStringLiteral("%1.asc").arg(safeKeyId);
64 : : QString fileName = QFileDialog::getSaveFileName(
65 : 3 : this, tr("Save public key"), defaultName,
66 [ + + + + ]: 9 : tr("ASCII-armored key (*.asc);;All files (*)"));
67 [ + + ]: 3 : if (fileName.isEmpty()) {
68 : : return;
69 : : }
70 : 2 : QString error;
71 [ + + + + ]: 4 : if (!saveArmoredKey(fileName, ui->plainTextEdit->toPlainText(), &error)) {
72 : 2 : PlainMessageBox::warning(this, tr("Save public key"), error);
73 : : }
74 : : }
75 : :
76 : 5 : auto ExportPublicKeyDialog::saveArmoredKey(const QString &fileName,
77 : : const QString &text, QString *error)
78 : : -> bool {
79 : : // QSaveFile writes to a link's target; a dangling link would create it.
80 [ + + + ]: 5 : if (QFileInfo(fileName).isSymLink()) {
81 : 2 : *error = tr("%1 is a symbolic link, so the key was not saved there. "
82 : : "Choose another name.")
83 : 4 : .arg(fileName);
84 : 2 : return false;
85 : : }
86 : 3 : QSaveFile file(fileName);
87 [ + + + ]: 3 : if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
88 : 1 : *error = tr("Could not open %1 for writing: %2")
89 : 2 : .arg(fileName, file.errorString());
90 : 1 : return false;
91 : : }
92 : 2 : QTextStream out(&file);
93 : 2 : out << text;
94 : 2 : out.flush();
95 [ + + - + : 2 : if (out.status() != QTextStream::Ok || !file.commit()) {
- + ]
96 : 0 : *error = tr("Could not write to %1: %2").arg(fileName, file.errorString());
97 : 0 : return false;
98 : : }
99 : : return true;
100 : 3 : }
|