LCOV - code coverage report
Current view: top level - src - keygendialog.cpp (source / functions) Hit Total Coverage
Test: .lcov.total Lines: 0 91 0.0 %
Date: 2017-03-14 11:46:08 Functions: 0 16 0.0 %

          Line data    Source code
       1             : #include "keygendialog.h"
       2             : #include "debughelper.h"
       3             : #include "qprogressindicator.h"
       4             : #include "ui_keygendialog.h"
       5             : #include <QMessageBox>
       6             : 
       7             : /**
       8             :  * @brief KeygenDialog::KeygenDialog basic constructor.
       9             :  * @param parent
      10             :  */
      11           0 : KeygenDialog::KeygenDialog(ConfigDialog *parent)
      12           0 :     : QDialog(parent), ui(new Ui::KeygenDialog) {
      13           0 :   ui->setupUi(this);
      14           0 :   dialog = parent;
      15           0 : }
      16             : 
      17             : /**
      18             :  * @brief KeygenDialog::~KeygenDialog even more basic destructor.
      19             :  */
      20           0 : KeygenDialog::~KeygenDialog() { delete ui; }
      21             : 
      22             : /**
      23             :  * @brief KeygenDialog::on_passphrase1_textChanged see if we want to have
      24             :  * protection.
      25             :  * @param arg1
      26             :  */
      27           0 : void KeygenDialog::on_passphrase1_textChanged(const QString &arg1) {
      28           0 :   if (ui->passphrase1->text() == ui->passphrase2->text()) {
      29           0 :     ui->buttonBox->setEnabled(true);
      30           0 :     replace("Passphrase", arg1);
      31           0 :     if (arg1 == "")
      32           0 :       no_protection(true);
      33             :     else
      34           0 :       no_protection(false);
      35             :   } else {
      36           0 :     ui->buttonBox->setEnabled(false);
      37             :   }
      38           0 : }
      39             : 
      40             : /**
      41             :  * @brief KeygenDialog::on_passphrase2_textChanged wrapper for
      42             :  * KeygenDialog::on_passphrase1_textChanged
      43             :  * @param arg1
      44             :  */
      45           0 : void KeygenDialog::on_passphrase2_textChanged(const QString &arg1) {
      46           0 :   on_passphrase1_textChanged(arg1);
      47           0 : }
      48             : 
      49             : /**
      50             :  * @brief KeygenDialog::on_checkBox_stateChanged expert mode enabled / disabled.
      51             :  * @param arg1
      52             :  */
      53           0 : void KeygenDialog::on_checkBox_stateChanged(int arg1) {
      54           0 :   if (arg1) {
      55           0 :     ui->plainTextEdit->setReadOnly(false);
      56           0 :     ui->plainTextEdit->setEnabled(true);
      57           0 :   } else {
      58           0 :     ui->plainTextEdit->setReadOnly(true);
      59           0 :     ui->plainTextEdit->setEnabled(false);
      60             :   }
      61           0 : }
      62             : 
      63             : /**
      64             :  * @brief KeygenDialog::on_email_textChanged update the email in keypair
      65             :  * generation template.
      66             :  * @param arg1
      67             :  */
      68           0 : void KeygenDialog::on_email_textChanged(const QString &arg1) {
      69           0 :   replace("Name-Email", arg1);
      70           0 : }
      71             : 
      72             : /**
      73             :  * @brief KeygenDialog::on_name_textChanged update the name in keypair
      74             :  * generation template.
      75             :  * @param arg1
      76             :  */
      77           0 : void KeygenDialog::on_name_textChanged(const QString &arg1) {
      78           0 :   replace("Name-Real", arg1);
      79           0 : }
      80             : 
      81             : /**
      82             :  * @brief KeygenDialog::replace do some regex magic. fore replacing Passphrase
      83             :  * and protection in keypair generation template.
      84             :  * @param key
      85             :  * @param value
      86             :  */
      87           0 : void KeygenDialog::replace(QString key, QString value) {
      88           0 :   QStringList clear;
      89           0 :   QString expert = ui->plainTextEdit->toPlainText();
      90           0 :   QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
      91           0 :   foreach (QString line, lines) {
      92           0 :     line.replace(QRegExp(key + ":.*"), key + ": " + value);
      93           0 :     if (key == "Passphrase")
      94           0 :       line.replace("%no-protection", "Passphrase: " + value);
      95           0 :     clear.append(line);
      96             :   }
      97           0 :   ui->plainTextEdit->setPlainText(clear.join("\n"));
      98           0 : }
      99             : 
     100             : /**
     101             :  * @brief KeygenDialog::no_protection remove protection in keypair generation
     102             :  * template.
     103             :  * @param enable
     104             :  */
     105           0 : void KeygenDialog::no_protection(bool enable) {
     106           0 :   QStringList clear;
     107           0 :   QString expert = ui->plainTextEdit->toPlainText();
     108           0 :   QStringList lines = expert.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
     109           0 :   foreach (QString line, lines) {
     110             :     bool remove = false;
     111           0 :     if (!enable) {
     112           0 :       if (line.indexOf("%no-protection") == 0)
     113           0 :         remove = true;
     114             :     } else {
     115           0 :       if (line.indexOf("Passphrase") == 0)
     116           0 :         line = "%no-protection";
     117             :     }
     118           0 :     if (!remove)
     119           0 :       clear.append(line);
     120             :   }
     121           0 :   ui->plainTextEdit->setPlainText(clear.join("\n"));
     122           0 : }
     123             : 
     124             : /**
     125             :  * @brief KeygenDialog::done we are going to create a key pair and show the
     126             :  * QProgressIndicator and some text since the generation will take some time.
     127             :  * @param r
     128             :  */
     129           0 : void KeygenDialog::done(int r) {
     130           0 :   if (QDialog::Accepted == r) { // ok was pressed
     131             :     // check name
     132           0 :     if (ui->name->text().length() < 5) {
     133           0 :       QMessageBox::critical(this, tr("Invalid name"),
     134           0 :                             tr("Name must be at least 5 characters long."));
     135           0 :       return;
     136             :     }
     137             : 
     138             :     // check email
     139           0 :     QRegExp mailre("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b");
     140           0 :     mailre.setCaseSensitivity(Qt::CaseInsensitive);
     141           0 :     mailre.setPatternSyntax(QRegExp::RegExp);
     142           0 :     if (!mailre.exactMatch(ui->email->text())) {
     143           0 :       QMessageBox::critical(
     144           0 :           this, tr("Invalid email"),
     145           0 :           tr("The email address you typed is not a valid email address."));
     146           0 :       return;
     147             :     }
     148             : 
     149           0 :     ui->widget->setEnabled(false);
     150           0 :     ui->buttonBox->setEnabled(false);
     151           0 :     ui->checkBox->setEnabled(false);
     152           0 :     ui->plainTextEdit->setEnabled(false);
     153             : 
     154           0 :     QProgressIndicator *pi = new QProgressIndicator();
     155           0 :     pi->startAnimation();
     156           0 :     pi->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
     157             : 
     158           0 :     ui->frame->hide();
     159           0 :     ui->label->setText(
     160           0 :         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           0 :     this->layout()->addWidget(pi);
     167             : 
     168           0 :     this->show();
     169           0 :     dialog->genKey(ui->plainTextEdit->toPlainText(), this);
     170           0 :   } else { // cancel, close or exc was pressed
     171           0 :     QDialog::done(r);
     172           0 :     return;
     173             :   }
     174           0 : }
     175             : 
     176             : /**
     177             :  * @brief KeygenDialog::closeEvent we are done here.
     178             :  * @param event
     179             :  */
     180           0 : void KeygenDialog::closeEvent(QCloseEvent *event) {
     181             :   // TODO(annejan) save window size or somethign
     182           0 :   event->accept();
     183           0 : }

Generated by: LCOV version 1.13