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 "ui_exportpublickeydialog.h"
11
12#include <QApplication>
13#include <QClipboard>
14#include <QFileDialog>
15#include <QFontDatabase>
16#include <QMessageBox>
17#include <QRegularExpression>
18#include <QSaveFile>
19#include <QTextStream>
20#include <QTimer>
21
23 const QString &armoredKey,
24 QWidget *parent)
25 : QDialog(parent), ui(new Ui::ExportPublicKeyDialog), m_keyId(keyId) {
26 ui->setupUi(this);
27 ui->keyIdLabel->setText(tr("Public key for %1").arg(keyId));
28 ui->plainTextEdit->setFont(
29 QFontDatabase::systemFont(QFontDatabase::FixedFont));
30 ui->plainTextEdit->setPlainText(armoredKey);
31 m_copyButtonOriginalText = ui->copyButton->text();
32}
33
35
37 -> QString {
38 static const QRegularExpression whitespace(QStringLiteral("\\s+"));
39 static const QRegularExpression unsafeChars(QStringLiteral("[^A-Za-z0-9_-]"));
40 const QStringList tokens = keyId.split(whitespace, Qt::SkipEmptyParts);
41 if (tokens.isEmpty()) {
42 return {};
43 }
44 QString token = tokens.first();
45 token.remove(unsafeChars);
46 return token;
47}
48
50 QApplication::clipboard()->setText(ui->plainTextEdit->toPlainText());
51 ui->copyButton->setText(tr("Copied!"));
52 QTimer::singleShot(1500, this, [this]() {
53 ui->copyButton->setText(m_copyButtonOriginalText);
54 });
55}
56
58 QString safeKeyId = sanitizeKeyIdForFilename(m_keyId);
59 QString defaultName = safeKeyId.isEmpty()
60 ? QStringLiteral("public_key.asc")
61 : QStringLiteral("%1.asc").arg(safeKeyId);
62 QString fileName = QFileDialog::getSaveFileName(
63 this, tr("Save public key"), defaultName,
64 tr("ASCII-armored key (*.asc);;All files (*)"));
65 if (fileName.isEmpty()) {
66 return;
67 }
68 QSaveFile file(fileName);
69 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
70 QMessageBox::warning(this, tr("Save public key"),
71 tr("Could not open %1 for writing: %2")
72 .arg(fileName, file.errorString()));
73 return;
74 }
75 QTextStream out(&file);
76 out << ui->plainTextEdit->toPlainText();
77 out.flush();
78 if (out.status() != QTextStream::Ok || !file.commit()) {
79 QMessageBox::warning(
80 this, tr("Save public key"),
81 tr("Could not write to %1: %2").arg(fileName, file.errorString()));
82 }
83}
void on_saveButton_clicked()
Prompt for a destination and write the armored key to that file through a QSaveFile....
QScopedPointer< Ui::ExportPublicKeyDialog > ui
Generated form (exportpublickeydialog.ui); owned.
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 ....