QtPass  1.2.1
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 "debughelper.h"
4 #include "qprogressindicator.h"
5 #include "ui_keygendialog.h"
6 #include <QMessageBox>
7 
13  : QDialog(parent), ui(new Ui::KeygenDialog) {
14  ui->setupUi(this);
15  dialog = parent;
16 }
17 
22 
28 void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
29  if (ui->passphrase1->text() == ui->passphrase2->text()) {
30  ui->buttonBox->setEnabled(true);
31  replace("Passphrase", arg1);
32  if (arg1 == "")
33  no_protection(true);
34  else
35  no_protection(false);
36  } else {
37  ui->buttonBox->setEnabled(false);
38  }
39 }
40 
46 void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
47  on_passphrase1_textChanged(arg1);
48 }
49 
54 void KeygenDialog::on_checkBox_stateChanged(int arg1) {
55  if (arg1) {
56  ui->plainTextEdit->setReadOnly(false);
57  ui->plainTextEdit->setEnabled(true);
58  } else {
59  ui->plainTextEdit->setReadOnly(true);
60  ui->plainTextEdit->setEnabled(false);
61  }
62 }
63 
69 void KeygenDialog::on_email_textChanged(const QString &arg1) {
70  replace("Name-Email", arg1);
71 }
72 
78 void KeygenDialog::on_name_textChanged(const QString &arg1) {
79  replace("Name-Real", arg1);
80 }
81 
88 void KeygenDialog::replace(QString key, QString value) {
89  QStringList clear;
90  QString expert = ui->plainTextEdit->toPlainText();
91  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
92  foreach (QString line, lines) {
93  line.replace(QRegExp(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  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
110  foreach (QString line, lines) {
111  bool remove = false;
112  if (!enable) {
113  if (line.indexOf("%no-protection") == 0)
114  remove = true;
115  } else {
116  if (line.indexOf("Passphrase") == 0)
117  line = "%no-protection";
118  }
119  if (!remove)
120  clear.append(line);
121  }
122  ui->plainTextEdit->setPlainText(clear.join("\n"));
123 }
124 
130 void KeygenDialog::done(int r) {
131  if (QDialog::Accepted == r) { // ok was pressed
132  // check name
133  if (ui->name->text().length() < 5) {
134  QMessageBox::critical(this, tr("Invalid name"),
135  tr("Name must be at least 5 characters long."));
136  return;
137  }
138 
139  // check email
140  QRegExp mailre("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
141  mailre.setCaseSensitivity(Qt::CaseInsensitive);
142  mailre.setPatternSyntax(QRegExp::RegExp);
143  if (!mailre.exactMatch(ui->email->text())) {
144  QMessageBox::critical(
145  this, tr("Invalid email"),
146  tr("The email address you typed is not a valid email address."));
147  return;
148  }
149 
150  ui->widget->setEnabled(false);
151  ui->buttonBox->setEnabled(false);
152  ui->checkBox->setEnabled(false);
153  ui->plainTextEdit->setEnabled(false);
154 
156  pi->startAnimation();
157  pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
158 
159  ui->frame->hide();
160  ui->label->setText(
161  tr("This operation can take some minutes.<br />"
162  "We need to generate a lot of random bytes. It is a good idea to "
163  "perform some other action (type on the keyboard, move the mouse, "
164  "utilize the disks) during the prime generation; this gives the "
165  "random number generator a better chance to gain enough entropy."));
166 
167  this->layout()->addWidget(pi);
168 
169  this->show();
170  dialog->genKey(ui->plainTextEdit->toPlainText(), this);
171  } else { // cancel, close or exc was pressed
172  QDialog::done(r);
173  return;
174  }
175 }
176 
181 void KeygenDialog::closeEvent(QCloseEvent *event) {
182  // TODO(annejan) save window size or somethign
183  event->accept();
184 }
void startAnimation()
Starts the spin animation.
void closeEvent(QCloseEvent *event)
KeygenDialog::closeEvent we are done here.
Definition: configdialog.h:9
The QProgressIndicator class lets an application display a progress indicator to show that a lengthy ...
Handles GPG keypair generation.
Definition: keygendialog.h:16
~KeygenDialog()
KeygenDialog::~KeygenDialog even more basic destructor.
KeygenDialog(ConfigDialog *parent=0)
KeygenDialog::KeygenDialog basic constructor.
QPlainTextEdit * plainTextEdit
void genKey(QString, QDialog *)
ConfigDialog::genKey tunnel function to make MainWindow generate a gpg key pair.
QLineEdit * passphrase1
void setupUi(QDialog *KeygenDialog)
The ConfigDialog handles the configuration interface.
Definition: configdialog.h:23
QLineEdit * name
QCheckBox * checkBox
QLineEdit * email
QDialogButtonBox * buttonBox
QLineEdit * passphrase2