QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
exportpublickeydialog.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
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
25 const QString &armoredKey,
26 QWidget *parent)
27 : QDialog(parent), ui(new Ui::ExportPublicKeyDialog), m_keyId(keyId) {
28 ui->setupUi(this);
29 ui->keyIdLabel->setText(tr("Public key for %1").arg(keyId));
30 ui->plainTextEdit->setFont(
31 QFontDatabase::systemFont(QFontDatabase::FixedFont));
32 ui->plainTextEdit->setPlainText(armoredKey);
33 m_copyButtonOriginalText = ui->copyButton->text();
34}
35
37
39 -> QString {
40 static const QRegularExpression whitespace(QStringLiteral("\\s+"));
41 static const QRegularExpression unsafeChars(QStringLiteral("[^A-Za-z0-9_-]"));
42 const QStringList tokens = keyId.split(whitespace, Qt::SkipEmptyParts);
43 if (tokens.isEmpty()) {
44 return {};
45 }
46 QString token = tokens.first();
47 token.remove(unsafeChars);
48 return token;
49}
50
52 QApplication::clipboard()->setText(ui->plainTextEdit->toPlainText());
53 ui->copyButton->setText(tr("Copied!"));
54 QTimer::singleShot(1500, this, [this]() {
55 ui->copyButton->setText(m_copyButtonOriginalText);
56 });
57}
58
60 QString safeKeyId = sanitizeKeyIdForFilename(m_keyId);
61 QString defaultName = safeKeyId.isEmpty()
62 ? QStringLiteral("public_key.asc")
63 : QStringLiteral("%1.asc").arg(safeKeyId);
64 QString fileName = QFileDialog::getSaveFileName(
65 this, tr("Save public key"), defaultName,
66 tr("ASCII-armored key (*.asc);;All files (*)"));
67 if (fileName.isEmpty()) {
68 return;
69 }
70 QString error;
71 if (!saveArmoredKey(fileName, ui->plainTextEdit->toPlainText(), &error)) {
72 PlainMessageBox::warning(this, tr("Save public key"), error);
73 }
74}
75
76auto 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 if (QFileInfo(fileName).isSymLink()) {
81 *error = tr("%1 is a symbolic link, so the key was not saved there. "
82 "Choose another name.")
83 .arg(fileName);
84 return false;
85 }
86 QSaveFile file(fileName);
87 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
88 *error = tr("Could not open %1 for writing: %2")
89 .arg(fileName, file.errorString());
90 return false;
91 }
92 QTextStream out(&file);
93 out << text;
94 out.flush();
95 if (out.status() != QTextStream::Ok || !file.commit()) {
96 *error = tr("Could not write to %1: %2").arg(fileName, file.errorString());
97 return false;
98 }
99 return true;
100}
void on_saveButton_clicked()
Prompt for a destination and write the armored key to that file through saveArmoredKey()....
QScopedPointer< Ui::ExportPublicKeyDialog > ui
Generated form (exportpublickeydialog.ui); owned.
static auto saveArmoredKey(const QString &fileName, const QString &text, QString *error) -> bool
Write the armored key to fileName, refusing a symbolic link there.
QString m_keyId
Key id as passed to the constructor; the basis of the suggested file name.
ExportPublicKeyDialog(const QString &keyId, const QString &armoredKey, QWidget *parent=nullptr)
Construct an ExportPublicKeyDialog.
~ExportPublicKeyDialog() override
Destroys the dialog and its generated UI.
void on_copyButton_clicked()
Copy the armored key text to the system clipboard; the button reads "Copied!" for 1....
static auto sanitizeKeyIdForFilename(const QString &keyId) -> QString
Reduce a key identifier to a filesystem-safe token.
QString m_copyButtonOriginalText
The copy button's label from the form, restored after the "Copied!" flash.
Dialog that shows an ASCII-armored public key and lets the user copy it or save it as a ....
void warning(QWidget *parent, const QString &title, const QString &text)
Show a warning in plain text.
QMessageBox helpers that force Qt::PlainText so store-controlled names can never be read as rich-text...