QtPass 1.5.1
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
keygendialog.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
3#include "keygendialog.h"
4#include "configdialog.h"
6#include "ui_keygendialog.h"
7#include "util.h"
8#include <QMessageBox>
9#include <QRegularExpression>
10
11#ifdef QT_DEBUG
12#include "debughelper.h"
13#endif
14
20 : QDialog(parent), ui(new Ui::KeygenDialog) {
21 ui->setupUi(this);
22 dialog = parent;
23}
24
29
35void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
36 bool state = ui->passphrase1->text() == ui->passphrase2->text();
37 if (state) {
38 replace("Passphrase", arg1);
39 no_protection(arg1.isEmpty());
40 }
41
42 ui->buttonBox->setEnabled(state);
43}
44
50void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
51 on_passphrase1_textChanged(arg1);
52}
53
58void KeygenDialog::on_checkBox_stateChanged(int arg1) {
59 ui->plainTextEdit->setReadOnly(!arg1);
60 ui->plainTextEdit->setEnabled(arg1);
61}
62
68void KeygenDialog::on_email_textChanged(const QString &arg1) {
69 replace("Name-Email", arg1);
70}
71
77void KeygenDialog::on_name_textChanged(const QString &arg1) {
78 replace("Name-Real", arg1);
79}
80
87void KeygenDialog::replace(const QString &key, const QString &value) {
88 QStringList clear;
89 QString expert = ui->plainTextEdit->toPlainText();
90#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
91 const QStringList lines =
92 expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
93#else
94 const QStringList lines =
95 expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
96#endif
97 for (QString line : lines) {
98 line.replace(QRegularExpression(key + ":.*"), key + ": " + value);
99 if (key == "Passphrase") {
100 line.replace("%no-protection", "Passphrase: " + value);
101 }
102 clear.append(line);
103 }
104 ui->plainTextEdit->setPlainText(clear.join("\n"));
105}
106
112void KeygenDialog::no_protection(bool enable) {
113 QStringList clear;
114 QString expert = ui->plainTextEdit->toPlainText();
115#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
116 const QStringList lines =
117 expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
118#else
119 const QStringList lines =
120 expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
121#endif
122 for (QString line : lines) {
123 bool remove = false;
124 if (!enable) {
125 if (line.indexOf("%no-protection") == 0) {
126 remove = true;
127 }
128 } else {
129 if (line.indexOf("Passphrase") == 0) {
130 line = "%no-protection";
131 }
132 }
133 if (!remove) {
134 clear.append(line);
135 }
136 }
137 ui->plainTextEdit->setPlainText(clear.join("\n"));
138}
139
145void KeygenDialog::done(int r) {
146 if (QDialog::Accepted == r) { // ok was pressed
147 // check name
148 if (ui->name->text().length() < 5) {
149 QMessageBox::critical(this, tr("Invalid name"),
150 tr("Name must be at least 5 characters long."));
151 return;
152 }
153
154 // check email
155 static const QRegularExpression mailre(
156 QRegularExpression::anchoredPattern(
157 R"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b)"),
158 QRegularExpression::CaseInsensitiveOption);
159 if (!mailre.match(ui->email->text()).hasMatch()) {
160 QMessageBox::critical(
161 this, tr("Invalid email"),
162 tr("The email address you typed is not a valid email address."));
163 return;
164 }
165
166 ui->widget->setEnabled(false);
167 ui->buttonBox->setEnabled(false);
168 ui->checkBox->setEnabled(false);
169 ui->plainTextEdit->setEnabled(false);
170
171 auto *pi = new QProgressIndicator();
172 pi->startAnimation();
173 pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
174
175 ui->frame->hide();
176 ui->label->setText(
177 tr("This operation can take some minutes.<br />"
178 "We need to generate a lot of random bytes. It is a good idea to "
179 "perform some other action (type on the keyboard, move the mouse, "
180 "utilize the disks) during the prime generation; this gives the "
181 "random number generator a better chance to gain enough entropy."));
182
183 this->layout()->addWidget(pi);
184
185 this->show();
186 dialog->genKey(ui->plainTextEdit->toPlainText(), this);
187 } else { // cancel, close or exc was pressed
188 QDialog::done(r);
189 return;
190 }
191}
192
197void KeygenDialog::closeEvent(QCloseEvent *event) {
198 // TODO(annejan): save window size or somethign
199 event->accept();
200}
The ConfigDialog handles the configuration interface.
void genKey(const QString &, QDialog *)
ConfigDialog::genKey tunnel function to make MainWindow generate a gpg key pair.
Handles GPG keypair generation.
KeygenDialog(ConfigDialog *parent=0)
KeygenDialog::KeygenDialog basic constructor.
void closeEvent(QCloseEvent *event)
KeygenDialog::closeEvent we are done here.
~KeygenDialog()
KeygenDialog::~KeygenDialog even more basic destructor.
The QProgressIndicator class lets an application display a progress indicator to show that a lengthy ...
static auto newLinesRegex() -> const QRegularExpression &
Definition util.cpp:214