14#include "ui_keygendialog.h"
18#include <QRegularExpression>
27 connect(
ui->checkBox, &QCheckBox::toggled,
this,
29 connect(
ui->noPassphrase, &QCheckBox::toggled,
this,
39 ui->plainTextEdit->setVisible(
ui->checkBox->isChecked());
53 const QString passphrase =
ui->passphrase1->text();
54 const bool agreed = passphrase ==
ui->passphrase2->text();
55 return agreed && (!passphrase.isEmpty() ||
ui->noPassphrase->isChecked());
64 ui->passphrase1->clear();
65 ui->passphrase2->clear();
67 ui->passphrase1->setEnabled(!checked);
68 ui->passphrase2->setEnabled(!checked);
77 ui->plainTextEdit->setReadOnly(!checked);
78 ui->plainTextEdit->setEnabled(checked);
79 ui->plainTextEdit->setVisible(checked);
100 batch.replace(QLatin1Char(
'\r'), QLatin1Char(
'\n'));
101 return batch.split(QLatin1Char(
'\n'), Qt::SkipEmptyParts);
112 const QStringList lines =
batchLines(
ui->plainTextEdit->toPlainText());
113 for (QString line : lines) {
114 line.replace(QRegularExpression(key +
":.*"), key +
": " + value);
117 ui->plainTextEdit->setPlainText(clear.join(
"\n"));
131 const QString trimmed = line.trimmed();
132 if (!trimmed.startsWith(control, Qt::CaseInsensitive))
134 return trimmed.size() == control.size() ||
135 trimmed.at(control.size()).isSpace();
142 const QString &passphrase) {
143 static const QString noProtection = QStringLiteral(
"%no-protection");
144 static const QString commit = QStringLiteral(
"%commit");
145 static const QString passphraseKey = QStringLiteral(
"Passphrase:");
146 const QString protection =
147 passphrase.isEmpty() ? noProtection : passphraseKey +
" " + passphrase;
150 bool spliced =
false;
152 for (QString line : lines) {
156 if (
const qsizetype nul = line.indexOf(QChar::Null); nul >= 0) {
160 line.trimmed().startsWith(passphraseKey, Qt::CaseInsensitive)) {
162 clear.append(protection);
170 if (!spliced && !passphrase.isEmpty()) {
172 const auto commitLine =
173 std::find_if(clear.cbegin(), clear.cend(), [](
const QString &line) {
174 return isControlStatement(line, commit);
176 clear.insert(commitLine - clear.cbegin(), protection);
178 return clear.join(
"\n");
182 return !
hasBatchBreak(value) && QStringView(value).isValidUtf16();
191 return std::any_of(value.cbegin(), value.cend(), [](QChar c) {
192 switch (c.unicode()) {
196 case QChar::LineSeparator:
197 case QChar::ParagraphSeparator:
211 const auto blank = [](QChar c) {
212 return c == QLatin1Char(
' ') ||
213 (c >= QLatin1Char(
'\t') && c <= QLatin1Char(
'\r'));
215 return passphrase.isEmpty() ||
216 (!blank(passphrase.front()) && !blank(passphrase.back()));
220 if (QDialog::Accepted == r) {
233 QMessageBox::critical(
this, tr(
"Invalid input"),
234 tr(
"The name and the passphrase cannot contain "
244 QMessageBox::critical(
this, tr(
"Invalid input"),
245 tr(
"The name or the passphrase holds half of a "
246 "character, such as a broken emoji. Type it "
253 QMessageBox::critical(
254 this, tr(
"Invalid input"),
255 tr(
"The passphrase cannot start or end with a space or a tab."));
258 if (
ui->name->text().length() < 5) {
259 QMessageBox::critical(
this, tr(
"Invalid name"),
260 tr(
"Name must be at least 5 characters long."));
264 static const QRegularExpression mailre(
265 QRegularExpression::anchoredPattern(
266 R
"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b)"),
267 QRegularExpression::CaseInsensitiveOption);
268 if (!mailre.match(
ui->email->text()).hasMatch()) {
269 QMessageBox::critical(
270 this, tr(
"Invalid email"),
271 tr(
"The email address you typed is not a valid email address."));
275 ui->widget->setEnabled(
false);
276 ui->buttonBox->setEnabled(
false);
277 ui->checkBox->setEnabled(
false);
278 ui->plainTextEdit->setEnabled(
false);
284 QSizePolicy::Expanding);
293 tr(
"This operation can take some minutes.<br />"
294 "We need to generate a lot of random bytes. It is a good idea to "
295 "perform some other action (type on the keyboard, move the mouse, "
296 "utilize the disks) during the prime generation; this gives the "
297 "random number generator a better chance to gain enough entropy."));
301 ui->passphrase1->text()));
318 [
this](
const QString &,
const QString &) {
321 QDialog::done(QDialog::Accepted);
327 [
this](
const QString &error) {
341 ui->widget->setEnabled(
true);
342 ui->checkBox->setEnabled(
true);
343 ui->plainTextEdit->setEnabled(
true);
348 tr(
"Key generation failed: %1").arg(error.toHtmlEscaped()));
static auto isBatchPassphrase(const QString &passphrase) -> bool
Whether gpg reads passphrase back as typed from a batch Passphrase: line.
static QString applyPassphrase(const QString &batch, const QString &passphrase)
Splice a passphrase into a GPG batch template.
void replace(const QString &key, const QString &value)
Rewrites every <key>: ... line of the batch template to carry value. Values that fail isBatchValue() ...
std::unique_ptr< QProgressIndicator > m_progressIndicator
Spinner shown while gpg works; created lazily on the first attempt, reused (hidden) across retries.
KeygenDialog(const QString &gpgExe, Pass *pass, QWidget *parent=nullptr)
Construct a KeygenDialog.
static auto hasBatchBreak(const QString &value) -> bool
Whether a field's text holds a line break or NUL, which would end its batch line early.
void setExpertMode(bool checked)
Enable or disable expert mode (the editable batch template).
void setNoPassphrase(bool checked)
Ticking "no passphrase" clears and disables the passphrase fields and makes OK available; unticking i...
void on_passphrase1_textChanged(const QString &arg1)
First passphrase field edited: re-evaluates whether OK may be enabled. The text itself stays out of t...
void generationFailed(const QString &error)
Stops and hides the progress indicator, re-enables the form under the usual OK rule and shows error i...
void done(int r) override
On Accepted, validates the form (passphrase chosen, no line breaks, name of 5+ characters,...
QMetaObject::Connection m_failed
Connection to Pass::generateGPGKeysFailed, live only during an attempt.
void startGeneration(const QString &batch)
Connects to the backend's finished/failed signals for this one attempt and calls Pass::GenerateGPGKey...
void on_email_textChanged(const QString &arg1)
Mirrors the e-mail field into the template's Name-Email: line.
QScopedPointer< Ui::KeygenDialog > ui
Generated form (keygendialog.ui); owned.
~KeygenDialog() override
Destroys the dialog and its generated UI.
void updateOkState()
OK is available when a passphrase is typed twice the same, or when no passphrase was asked for explic...
Pass * m_pass
Backend that runs gpg; not owned, may be nullptr.
void on_name_textChanged(const QString &arg1)
Mirrors the name field into the template's Name-Real: line.
QMetaObject::Connection m_finished
Connection to Pass::finishedGenerateGPGKeys, live only during an attempt.
auto passphraseChosen() const -> bool
Whether the passphrase question has an answer: typed twice the same, or waived with the checkbox....
void on_passphrase2_textChanged(const QString &arg1)
Confirmation field edited: same as on_passphrase1_textChanged().
static auto isBatchValue(const QString &value) -> bool
Whether value can go into a GPG batch line as one value.
Abstract base class for password store operations.
void GenerateGPGKeys(QString batch)
Generate GPG keys using batch script.
static QString getDefaultKeyTemplate(const QString &gpgExecutable={})
Get default key template for new GPG keys.
void finishedGenerateGPGKeys(const QString &out, const QString &err)
GenerateGPGKeys() succeeded; the new key pair is in the keyring.
void generateGPGKeysFailed(const QString &err)
Emitted when GPG key generation fails, alongside the generic processErrorExit(); lets the keygen dial...
Settings dialog: every page of the configuration window and the profile table, with the validation th...
static bool isControlStatement(const QString &line, const QString &control)
Whether a batch line is the control statement control.
static QStringList batchLines(QString batch)
Cut a batch template into its non-empty lines at CR and LF.
Dialog that collects name, e-mail and passphrase for a new GPG keypair and drives gpg --gen-key --bat...
void attach(QDialog &dialog, const QString &key)
restore() now, save() whenever the dialog finishes.
Pass, the abstract password-store backend that RealPass and ImitatePass implement,...
Spinner widget that shows the application is busy.
The "qtpass" logging category.
QtPassSettings, the QSettings singleton behind every saved setting, the profiles and the Pass backend...
WindowStateStore: saves and restores top-level window geometry through QtPassSettings,...