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 "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  bool state = ui->passphrase1->text() == ui->passphrase2->text();
30  if (state) {
31  replace("Passphrase", arg1);
32  no_protection(arg1.isEmpty());
33  }
34 
35  ui->buttonBox->setEnabled(state);
36 }
37 
43 void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
44  on_passphrase1_textChanged(arg1);
45 }
46 
51 void KeygenDialog::on_checkBox_stateChanged(int arg1) {
52  ui->plainTextEdit->setReadOnly(!arg1);
53  ui->plainTextEdit->setEnabled(arg1);
54 }
55 
61 void KeygenDialog::on_email_textChanged(const QString &arg1) {
62  replace("Name-Email", arg1);
63 }
64 
70 void KeygenDialog::on_name_textChanged(const QString &arg1) {
71  replace("Name-Real", arg1);
72 }
73 
80 void KeygenDialog::replace(QString key, QString value) {
81  QStringList clear;
82  QString expert = ui->plainTextEdit->toPlainText();
83  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
84  foreach (QString line, lines) {
85  line.replace(QRegExp(key + ":.*"), key + ": " + value);
86  if (key == "Passphrase")
87  line.replace("%no-protection", "Passphrase: " + value);
88  clear.append(line);
89  }
90  ui->plainTextEdit->setPlainText(clear.join("\n"));
91 }
92 
98 void KeygenDialog::no_protection(bool enable) {
99  QStringList clear;
100  QString expert = ui->plainTextEdit->toPlainText();
101  QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
102  foreach (QString line, lines) {
103  bool remove = false;
104  if (!enable) {
105  if (line.indexOf("%no-protection") == 0)
106  remove = true;
107  } else {
108  if (line.indexOf("Passphrase") == 0)
109  line = "%no-protection";
110  }
111  if (!remove)
112  clear.append(line);
113  }
114  ui->plainTextEdit->setPlainText(clear.join("\n"));
115 }
116 
122 void KeygenDialog::done(int r) {
123  if (QDialog::Accepted == r) { // ok was pressed
124  // check name
125  if (ui->name->text().length() < 5) {
126  QMessageBox::critical(this, tr("Invalid name"),
127  tr("Name must be at least 5 characters long."));
128  return;
129  }
130 
131  // check email
132  QRegExp mailre("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
133  mailre.setCaseSensitivity(Qt::CaseInsensitive);
134  mailre.setPatternSyntax(QRegExp::RegExp);
135  if (!mailre.exactMatch(ui->email->text())) {
136  QMessageBox::critical(
137  this, tr("Invalid email"),
138  tr("The email address you typed is not a valid email address."));
139  return;
140  }
141 
142  ui->widget->setEnabled(false);
143  ui->buttonBox->setEnabled(false);
144  ui->checkBox->setEnabled(false);
145  ui->plainTextEdit->setEnabled(false);
146 
148  pi->startAnimation();
149  pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
150 
151  ui->frame->hide();
152  ui->label->setText(
153  tr("This operation can take some minutes.<br />"
154  "We need to generate a lot of random bytes. It is a good idea to "
155  "perform some other action (type on the keyboard, move the mouse, "
156  "utilize the disks) during the prime generation; this gives the "
157  "random number generator a better chance to gain enough entropy."));
158 
159  this->layout()->addWidget(pi);
160 
161  this->show();
162  dialog->genKey(ui->plainTextEdit->toPlainText(), this);
163  } else { // cancel, close or exc was pressed
164  QDialog::done(r);
165  return;
166  }
167 }
168 
173 void KeygenDialog::closeEvent(QCloseEvent *event) {
174  // TODO(annejan) save window size or somethign
175  event->accept();
176 }
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:23