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