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