Line data Source code
1 : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : #include "keygendialog.h"
4 : #include "configdialog.h"
5 : #include "qprogressindicator.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 :
15 : /**
16 : * @brief KeygenDialog::KeygenDialog basic constructor.
17 : * @param parent
18 : */
19 0 : KeygenDialog::KeygenDialog(ConfigDialog *parent)
20 0 : : QDialog(parent), ui(new Ui::KeygenDialog) {
21 0 : ui->setupUi(this);
22 0 : dialog = parent;
23 0 : }
24 :
25 : /**
26 : * @brief KeygenDialog::~KeygenDialog even more basic destructor.
27 : */
28 0 : KeygenDialog::~KeygenDialog() { delete ui; }
29 :
30 : /**
31 : * @brief KeygenDialog::on_passphrase1_textChanged see if we want to have
32 : * protection.
33 : * @param arg1
34 : */
35 0 : void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
36 0 : bool state = ui->passphrase1->text() == ui->passphrase2->text();
37 0 : if (state) {
38 0 : replace("Passphrase", arg1);
39 0 : no_protection(arg1.isEmpty());
40 : }
41 :
42 0 : ui->buttonBox->setEnabled(state);
43 0 : }
44 :
45 : /**
46 : * @brief KeygenDialog::on_passphrase2_textChanged wrapper for
47 : * KeygenDialog::on_passphrase1_textChanged
48 : * @param arg1
49 : */
50 0 : void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
51 0 : on_passphrase1_textChanged(arg1);
52 0 : }
53 :
54 : /**
55 : * @brief KeygenDialog::on_checkBox_stateChanged expert mode enabled / disabled.
56 : * @param arg1
57 : */
58 0 : void KeygenDialog::on_checkBox_stateChanged(int arg1) {
59 0 : ui->plainTextEdit->setReadOnly(!arg1);
60 0 : ui->plainTextEdit->setEnabled(arg1);
61 0 : }
62 :
63 : /**
64 : * @brief KeygenDialog::on_email_textChanged update the email in keypair
65 : * generation template.
66 : * @param arg1
67 : */
68 0 : void KeygenDialog::on_email_textChanged(const QString &arg1) {
69 0 : replace("Name-Email", arg1);
70 0 : }
71 :
72 : /**
73 : * @brief KeygenDialog::on_name_textChanged update the name in keypair
74 : * generation template.
75 : * @param arg1
76 : */
77 0 : void KeygenDialog::on_name_textChanged(const QString &arg1) {
78 0 : replace("Name-Real", arg1);
79 0 : }
80 :
81 : /**
82 : * @brief KeygenDialog::replace do some regex magic. fore replacing Passphrase
83 : * and protection in keypair generation template.
84 : * @param key
85 : * @param value
86 : */
87 0 : void KeygenDialog::replace(const QString &key, const QString &value) {
88 0 : QStringList clear;
89 0 : QString expert = ui->plainTextEdit->toPlainText();
90 : #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
91 : const QStringList lines =
92 0 : expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
93 : #else
94 : const QStringList lines =
95 : expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
96 : #endif
97 0 : for (QString line : lines) {
98 0 : line.replace(QRegularExpression(key + ":.*"), key + ": " + value);
99 0 : if (key == "Passphrase") {
100 0 : line.replace("%no-protection", "Passphrase: " + value);
101 : }
102 : clear.append(line);
103 : }
104 0 : ui->plainTextEdit->setPlainText(clear.join("\n"));
105 0 : }
106 :
107 : /**
108 : * @brief KeygenDialog::no_protection remove protection in keypair generation
109 : * template.
110 : * @param enable
111 : */
112 0 : void KeygenDialog::no_protection(bool enable) {
113 0 : QStringList clear;
114 0 : QString expert = ui->plainTextEdit->toPlainText();
115 : #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
116 : const QStringList lines =
117 0 : expert.split(Util::newLinesRegex(), Qt::SkipEmptyParts);
118 : #else
119 : const QStringList lines =
120 : expert.split(Util::newLinesRegex(), QString::SkipEmptyParts);
121 : #endif
122 0 : for (QString line : lines) {
123 : bool remove = false;
124 0 : if (!enable) {
125 0 : if (line.indexOf("%no-protection") == 0) {
126 : remove = true;
127 : }
128 : } else {
129 0 : if (line.indexOf("Passphrase") == 0) {
130 0 : line = "%no-protection";
131 : }
132 : }
133 : if (!remove) {
134 : clear.append(line);
135 : }
136 : }
137 0 : ui->plainTextEdit->setPlainText(clear.join("\n"));
138 0 : }
139 :
140 : /**
141 : * @brief KeygenDialog::done we are going to create a key pair and show the
142 : * QProgressIndicator and some text since the generation will take some time.
143 : * @param r
144 : */
145 0 : void KeygenDialog::done(int r) {
146 0 : if (QDialog::Accepted == r) { // ok was pressed
147 : // check name
148 0 : if (ui->name->text().length() < 5) {
149 0 : QMessageBox::critical(this, tr("Invalid name"),
150 0 : tr("Name must be at least 5 characters long."));
151 0 : return;
152 : }
153 :
154 : // check email
155 : static const QRegularExpression mailre(
156 0 : QRegularExpression::anchoredPattern(
157 0 : R"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b)"),
158 0 : QRegularExpression::CaseInsensitiveOption);
159 0 : if (!mailre.match(ui->email->text()).hasMatch()) {
160 0 : QMessageBox::critical(
161 0 : this, tr("Invalid email"),
162 0 : tr("The email address you typed is not a valid email address."));
163 0 : return;
164 : }
165 :
166 0 : ui->widget->setEnabled(false);
167 0 : ui->buttonBox->setEnabled(false);
168 0 : ui->checkBox->setEnabled(false);
169 0 : ui->plainTextEdit->setEnabled(false);
170 :
171 0 : auto *pi = new QProgressIndicator();
172 0 : pi->startAnimation();
173 0 : pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
174 :
175 0 : ui->frame->hide();
176 0 : ui->label->setText(
177 0 : 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 0 : this->layout()->addWidget(pi);
184 :
185 0 : this->show();
186 0 : dialog->genKey(ui->plainTextEdit->toPlainText(), this);
187 : } else { // cancel, close or exc was pressed
188 0 : QDialog::done(r);
189 0 : return;
190 : }
191 : }
192 :
193 : /**
194 : * @brief KeygenDialog::closeEvent we are done here.
195 : * @param event
196 : */
197 0 : void KeygenDialog::closeEvent(QCloseEvent *event) {
198 : // TODO(annejan): save window size or somethign
199 : event->accept();
200 0 : }
|