QtPass  1.2.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 
7 #ifdef QT_DEBUG
8 #include "debughelper.h"
9 #endif
10 
16  : QDialog(parent), ui(new Ui::KeygenDialog) {
17  ui->setupUi(this);
18  dialog = parent;
19 }
20 
25 
31 void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
32  bool state = ui->passphrase1->text() == ui->passphrase2->text();
33  if (state) {
34  replace("Passphrase", arg1);
35  no_protection(arg1.isEmpty());
36  }
37 
38  ui->buttonBox->setEnabled(state);
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  ui->plainTextEdit->setReadOnly(!arg1);
56  ui->plainTextEdit->setEnabled(arg1);
57 }
58 
64 void KeygenDialog::on_email_textChanged(const QString &arg1) {
65  replace("Name-Email", arg1);
66 }
67 
73 void KeygenDialog::on_name_textChanged(const QString &arg1) {
74  replace("Name-Real", arg1);
75 }
76 
83 void KeygenDialog::replace(QString key, QString value) {
84  QStringList clear;
85  QString expert = ui->plainTextEdit->toPlainText();
86  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
87  foreach (QString line, lines) {
88  line.replace(QRegExp(key + ":.*"), key + ": " + value);
89  if (key == "Passphrase")
90  line.replace("%no-protection", "Passphrase: " + value);
91  clear.append(line);
92  }
93  ui->plainTextEdit->setPlainText(clear.join("\n"));
94 }
95 
101 void KeygenDialog::no_protection(bool enable) {
102  QStringList clear;
103  QString expert = ui->plainTextEdit->toPlainText();
104  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
105  foreach (QString line, lines) {
106  bool remove = false;
107  if (!enable) {
108  if (line.indexOf("%no-protection") == 0)
109  remove = true;
110  } else {
111  if (line.indexOf("Passphrase") == 0)
112  line = "%no-protection";
113  }
114  if (!remove)
115  clear.append(line);
116  }
117  ui->plainTextEdit->setPlainText(clear.join("\n"));
118 }
119 
125 void KeygenDialog::done(int r) {
126  if (QDialog::Accepted == r) { // ok was pressed
127  // check name
128  if (ui->name->text().length() < 5) {
129  QMessageBox::critical(this, tr("Invalid name"),
130  tr("Name must be at least 5 characters long."));
131  return;
132  }
133 
134  // check email
135  QRegExp mailre("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
136  mailre.setCaseSensitivity(Qt::CaseInsensitive);
137  mailre.setPatternSyntax(QRegExp::RegExp);
138  if (!mailre.exactMatch(ui->email->text())) {
139  QMessageBox::critical(
140  this, tr("Invalid email"),
141  tr("The email address you typed is not a valid email address."));
142  return;
143  }
144 
145  ui->widget->setEnabled(false);
146  ui->buttonBox->setEnabled(false);
147  ui->checkBox->setEnabled(false);
148  ui->plainTextEdit->setEnabled(false);
149 
151  pi->startAnimation();
152  pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
153 
154  ui->frame->hide();
155  ui->label->setText(
156  tr("This operation can take some minutes.<br />"
157  "We need to generate a lot of random bytes. It is a good idea to "
158  "perform some other action (type on the keyboard, move the mouse, "
159  "utilize the disks) during the prime generation; this gives the "
160  "random number generator a better chance to gain enough entropy."));
161 
162  this->layout()->addWidget(pi);
163 
164  this->show();
165  dialog->genKey(ui->plainTextEdit->toPlainText(), this);
166  } else { // cancel, close or exc was pressed
167  QDialog::done(r);
168  return;
169  }
170 }
171 
176 void KeygenDialog::closeEvent(QCloseEvent *event) {
177  // TODO(annejan) save window size or somethign
178  event->accept();
179 }
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.
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:24