QtPass
2.0.0
Developer documentation
Toggle main menu visibility
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
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
ExportPublicKeyDialog::ExportPublicKeyDialog
(
const
QString &keyId,
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
36
ExportPublicKeyDialog::~ExportPublicKeyDialog
() =
default
;
37
38
auto
ExportPublicKeyDialog::sanitizeKeyIdForFilename
(
const
QString &keyId)
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
51
void
ExportPublicKeyDialog::on_copyButton_clicked
() {
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
59
void
ExportPublicKeyDialog::on_saveButton_clicked
() {
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
76
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
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
}
ExportPublicKeyDialog::on_saveButton_clicked
void on_saveButton_clicked()
Prompt for a destination and write the armored key to that file through saveArmoredKey()....
Definition
exportpublickeydialog.cpp:59
ExportPublicKeyDialog::ui
QScopedPointer< Ui::ExportPublicKeyDialog > ui
Generated form (exportpublickeydialog.ui); owned.
Definition
exportpublickeydialog.h:87
ExportPublicKeyDialog::saveArmoredKey
static auto saveArmoredKey(const QString &fileName, const QString &text, QString *error) -> bool
Write the armored key to fileName, refusing a symbolic link there.
Definition
exportpublickeydialog.cpp:76
ExportPublicKeyDialog::m_keyId
QString m_keyId
Key id as passed to the constructor; the basis of the suggested file name.
Definition
exportpublickeydialog.h:90
ExportPublicKeyDialog::ExportPublicKeyDialog
ExportPublicKeyDialog(const QString &keyId, const QString &armoredKey, QWidget *parent=nullptr)
Construct an ExportPublicKeyDialog.
Definition
exportpublickeydialog.cpp:24
ExportPublicKeyDialog::~ExportPublicKeyDialog
~ExportPublicKeyDialog() override
Destroys the dialog and its generated UI.
ExportPublicKeyDialog::on_copyButton_clicked
void on_copyButton_clicked()
Copy the armored key text to the system clipboard; the button reads "Copied!" for 1....
Definition
exportpublickeydialog.cpp:51
ExportPublicKeyDialog::sanitizeKeyIdForFilename
static auto sanitizeKeyIdForFilename(const QString &keyId) -> QString
Reduce a key identifier to a filesystem-safe token.
Definition
exportpublickeydialog.cpp:38
ExportPublicKeyDialog::m_copyButtonOriginalText
QString m_copyButtonOriginalText
The copy button's label from the form, restored after the "Copied!" flash.
Definition
exportpublickeydialog.h:93
exportpublickeydialog.h
Dialog that shows an ASCII-armored public key and lets the user copy it or save it as a ....
PlainMessageBox::warning
void warning(QWidget *parent, const QString &title, const QString &text)
Show a warning in plain text.
Definition
plainmessagebox.cpp:132
plainmessagebox.h
QMessageBox helpers that force Qt::PlainText so store-controlled names can never be read as rich-text...
src
exportpublickeydialog.cpp
Generated by
1.18.0