QtPass 1.4.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
keygendialog.cpp
Go to the documentation of this file.
1#include "keygendialog.h"
2#include "configdialog.h"
4#include "ui_keygendialog.h"
5#include <QMessageBox>
6#include <QRegularExpression>
7
8#ifdef QT_DEBUG
9#include "debughelper.h"
10#endif
11
17 : QDialog(parent), ui(new Ui::KeygenDialog) {
18 ui->setupUi(this);
19 dialog = parent;
20}
21
26
32void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
33 bool state = ui->passphrase1->text() == ui->passphrase2->text();
34 if (state) {
35 replace("Passphrase", arg1);
36 no_protection(arg1.isEmpty());
37 }
38
39 ui->buttonBox->setEnabled(state);
40}
41
47void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
48 on_passphrase1_textChanged(arg1);
49}
50
55void KeygenDialog::on_checkBox_stateChanged(int arg1) {
56 ui->plainTextEdit->setReadOnly(!arg1);
57 ui->plainTextEdit->setEnabled(arg1);
58}
59
65void KeygenDialog::on_email_textChanged(const QString &arg1) {
66 replace("Name-Email", arg1);
67}
68
74void KeygenDialog::on_name_textChanged(const QString &arg1) {
75 replace("Name-Real", arg1);
76}
77
84void KeygenDialog::replace(const QString &key, const QString &value) {
85 QStringList clear;
86 QString expert = ui->plainTextEdit->toPlainText();
87 static const QRegularExpression newLines{"[\r\n]"};
88#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
89 const QStringList lines = expert.split(newLines, Qt::SkipEmptyParts);
90#else
91 const QStringList lines = expert.split(newLines, QString::SkipEmptyParts);
92#endif
93 for (QString line : lines) {
94 line.replace(QRegularExpression(key + ":.*"), key + ": " + value);
95 if (key == "Passphrase")
96 line.replace("%no-protection", "Passphrase: " + value);
97 clear.append(line);
98 }
99 ui->plainTextEdit->setPlainText(clear.join("\n"));
100}
101
107void KeygenDialog::no_protection(bool enable) {
108 QStringList clear;
109 QString expert = ui->plainTextEdit->toPlainText();
110 static const QRegularExpression newLines{"[\r\n]"};
111#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
112 const QStringList lines = expert.split(newLines, Qt::SkipEmptyParts);
113#else
114 const QStringList lines = expert.split(newLines, QString::SkipEmptyParts);
115#endif
116 for (QString line : lines) {
117 bool remove = false;
118 if (!enable) {
119 if (line.indexOf("%no-protection") == 0)
120 remove = true;
121 } else {
122 if (line.indexOf("Passphrase") == 0)
123 line = "%no-protection";
124 }
125 if (!remove)
126 clear.append(line);
127 }
128 ui->plainTextEdit->setPlainText(clear.join("\n"));
129}
130
136void KeygenDialog::done(int r) {
137 if (QDialog::Accepted == r) { // ok was pressed
138 // check name
139 if (ui->name->text().length() < 5) {
140 QMessageBox::critical(this, tr("Invalid name"),
141 tr("Name must be at least 5 characters long."));
142 return;
143 }
144
145 // check email
146 static const QRegularExpression mailre(
147 QRegularExpression::anchoredPattern(
148 R"(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)"),
149 QRegularExpression::CaseInsensitiveOption);
150 if (!mailre.match(ui->email->text()).hasMatch()) {
151 QMessageBox::critical(
152 this, tr("Invalid email"),
153 tr("The email address you typed is not a valid email address."));
154 return;
155 }
156
157 ui->widget->setEnabled(false);
158 ui->buttonBox->setEnabled(false);
159 ui->checkBox->setEnabled(false);
160 ui->plainTextEdit->setEnabled(false);
161
162 auto *pi = new QProgressIndicator();
163 pi->startAnimation();
164 pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
165
166 ui->frame->hide();
167 ui->label->setText(
168 tr("This operation can take some minutes.<br />"
169 "We need to generate a lot of random bytes. It is a good idea to "
170 "perform some other action (type on the keyboard, move the mouse, "
171 "utilize the disks) during the prime generation; this gives the "
172 "random number generator a better chance to gain enough entropy."));
173
174 this->layout()->addWidget(pi);
175
176 this->show();
177 dialog->genKey(ui->plainTextEdit->toPlainText(), this);
178 } else { // cancel, close or exc was pressed
179 QDialog::done(r);
180 return;
181 }
182}
183
188void KeygenDialog::closeEvent(QCloseEvent *event) {
189 // TODO(annejan) save window size or somethign
190 event->accept();
191}
The ConfigDialog handles the configuration interface.
Definition: configdialog.h:24
void genKey(QString, QDialog *)
ConfigDialog::genKey tunnel function to make MainWindow generate a gpg key pair.
Handles GPG keypair generation.
Definition: keygendialog.h:16
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 ...
Definition: configdialog.h:9