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

          Line data    Source code
       1             : #include "passworddialog.h"
       2             : #include "debughelper.h"
       3             : #include "qtpasssettings.h"
       4             : #include "ui_passworddialog.h"
       5             : #include <QDebug>
       6             : #include <QLabel>
       7             : #include <QLineEdit>
       8             : 
       9             : /**
      10             :  * @brief PasswordDialog::PasswordDialog basic constructor.
      11             :  * @param passConfig configuration constant
      12             :  * @param parent
      13             :  */
      14           0 : PasswordDialog::PasswordDialog(const passwordConfiguration &passConfig,
      15             :                                QWidget *parent)
      16           0 :     : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
      17           0 :   templating = false;
      18           0 :   allFields = false;
      19           0 :   ui->setupUi(this);
      20           0 :   setLength(m_passConfig.length);
      21           0 :   setPasswordCharTemplate(m_passConfig.selected);
      22           0 : }
      23             : 
      24             : /**
      25             :  * @brief PasswordDialog::~PasswordDialog basic destructor.
      26             :  */
      27           0 : PasswordDialog::~PasswordDialog() { delete ui; }
      28             : 
      29             : /**
      30             :  * @brief PasswordDialog::on_checkBoxShow_stateChanged hide or show passwords.
      31             :  * @param arg1
      32             :  */
      33           0 : void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
      34           0 :   if (arg1)
      35           0 :     ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
      36             :   else
      37           0 :     ui->lineEditPassword->setEchoMode(QLineEdit::Password);
      38           0 : }
      39             : 
      40             : /**
      41             :  * @brief PasswordDialog::on_createPasswordButton_clicked generate a random
      42             :  * passwords.
      43             :  * @todo refactor when process is untangled from MainWindow class.
      44             :  */
      45           0 : void PasswordDialog::on_createPasswordButton_clicked() {
      46           0 :   ui->widget->setEnabled(false);
      47           0 :   QString newPass = QtPassSettings::getPass()->Generate_b(
      48           0 :       ui->spinBox_pwdLength->value(),
      49           0 :       m_passConfig.Characters[(passwordConfiguration::characterSet)
      50           0 :                                   ui->passwordTemplateSwitch->currentIndex()]);
      51           0 :   if (newPass.length() > 0)
      52           0 :     ui->lineEditPassword->setText(newPass);
      53           0 :   ui->widget->setEnabled(true);
      54           0 : }
      55             : 
      56             : /**
      57             :  * @brief PasswordDialog::setPassword populate the (templated) fields.
      58             :  * @param password
      59             :  */
      60           0 : void PasswordDialog::setPassword(QString password) {
      61           0 :   QStringList tokens = password.split("\n");
      62           0 :   ui->lineEditPassword->setText(tokens[0]);
      63           0 :   tokens.pop_front();
      64           0 :   if (templating) {
      65           0 :     QWidget *previous = ui->checkBoxShow;
      66           0 :     for (int i = 0; i < ui->formLayout->rowCount(); ++i) {
      67           0 :       QLayoutItem *item = ui->formLayout->itemAt(i, QFormLayout::FieldRole);
      68           0 :       if (item == NULL)
      69           0 :         continue;
      70           0 :       QWidget *widget = item->widget();
      71           0 :       for (int j = 0; j < tokens.length(); ++j) {
      72           0 :         QString token = tokens.at(j);
      73           0 :         if (token.startsWith(widget->objectName() + ':')) {
      74           0 :           tokens.removeAt(j);
      75           0 :           QString value = token.remove(0, widget->objectName().length() + 1);
      76           0 :           reinterpret_cast<QLineEdit *>(widget)->setText(value.trimmed());
      77           0 :         }
      78           0 :       }
      79             :       previous = widget;
      80           0 :     }
      81           0 :     if (allFields) {
      82           0 :       for (int j = 0; j < tokens.length(); ++j) {
      83           0 :         QString token = tokens.at(j);
      84           0 :         if (token.contains(':')) {
      85           0 :           int colon = token.indexOf(':');
      86           0 :           QString field = token.left(colon);
      87           0 :           QString value = token.right(token.length() - colon - 1);
      88           0 :           if (!passTemplate.contains(field) && value.startsWith("//"))
      89           0 :             continue; // colon is probably from a url
      90           0 :           QLineEdit *line = new QLineEdit();
      91           0 :           line->setObjectName(field.trimmed());
      92           0 :           line->setText(value.trimmed());
      93           0 :           ui->formLayout->addRow(new QLabel(field), line);
      94           0 :           setTabOrder(previous, line);
      95             :           previous = line;
      96           0 :           tokens.removeAt(j);
      97           0 :           --j; // tokens.length() also got shortened by the remove..
      98           0 :         }
      99           0 :       }
     100           0 :     }
     101           0 :   }
     102           0 :   ui->plainTextEdit->insertPlainText(tokens.join("\n"));
     103           0 : }
     104             : 
     105             : /**
     106             :  * @brief PasswordDialog::getPassword  join the (templated) fields to a QString
     107             :  * for writing back.
     108             :  * @return collappsed password.
     109             :  */
     110           0 : QString PasswordDialog::getPassword() {
     111           0 :   QString passFile = ui->lineEditPassword->text() + "\n";
     112           0 :   for (int i = 0; i < ui->formLayout->rowCount(); ++i) {
     113           0 :     QLayoutItem *item = ui->formLayout->itemAt(i, QFormLayout::FieldRole);
     114           0 :     if (item == NULL)
     115           0 :       continue;
     116           0 :     QWidget *widget = item->widget();
     117           0 :     QString text = reinterpret_cast<QLineEdit *>(widget)->text();
     118           0 :     if (text.isEmpty())
     119           0 :       continue;
     120           0 :     passFile += widget->objectName() + ": " + text + "\n";
     121           0 :   }
     122           0 :   passFile += ui->plainTextEdit->toPlainText();
     123             :   return passFile;
     124           0 : }
     125             : 
     126             : /**
     127             :  * @brief PasswordDialog::setTemplate set the template and create the fields.
     128             :  * @param rawFields
     129             :  */
     130           0 : void PasswordDialog::setTemplate(QString rawFields) {
     131           0 :   fields = rawFields.split('\n');
     132           0 :   QWidget *previous = ui->checkBoxShow;
     133           0 :   foreach (QString field, fields) {
     134           0 :     if (field.isEmpty())
     135             :       continue;
     136           0 :     QLineEdit *line = new QLineEdit();
     137           0 :     line->setObjectName(field);
     138           0 :     ui->formLayout->addRow(new QLabel(field), line);
     139           0 :     setTabOrder(previous, line);
     140             :     previous = line;
     141           0 :   }
     142           0 : }
     143             : 
     144             : /**
     145             :  * @brief PasswordDialog::setFile show which (password) file we are editing.
     146             :  * @param file
     147             :  */
     148           0 : void PasswordDialog::setFile(QString file) {
     149           0 :   this->setWindowTitle(this->windowTitle() + " " + file);
     150           0 : }
     151             : 
     152             : /**
     153             :  * @brief PasswordDialog::templateAll basic setter for use in
     154             :  * PasswordDialog::setPassword templating all tokenisable lines.
     155             :  * @param templateAll
     156             :  */
     157           0 : void PasswordDialog::templateAll(bool templateAll) { allFields = templateAll; }
     158             : 
     159             : /**
     160             :  * @brief PasswordDialog::useTemplate basic setter for use in
     161             :  * PasswordDialog::useTemplate templating.
     162             :  * @param useTemplate
     163             :  */
     164           0 : void PasswordDialog::useTemplate(bool useTemplate) { templating = useTemplate; }
     165             : 
     166             : /**
     167             :  * @brief PasswordDialog::setLength
     168             :  * PasswordDialog::setLength password length.
     169             :  * @param l
     170             :  */
     171           0 : void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
     172             : 
     173             : /**
     174             :  * @brief PasswordDialog::setPasswordCharTemplate
     175             :  * PasswordDialog::setPasswordCharTemplate chose the template style.
     176             :  * @param t
     177             :  */
     178           0 : void PasswordDialog::setPasswordCharTemplate(int t) {
     179           0 :   ui->passwordTemplateSwitch->setCurrentIndex(t);
     180           0 : }
     181             : 
     182             : /**
     183             :  * @brief PasswordDialog::usePwgen
     184             :  * PasswordDialog::usePwgen don't use own password generator.
     185             :  * @param usePwgen
     186             :  */
     187           0 : void PasswordDialog::usePwgen(bool usePwgen) {
     188           0 :   ui->passwordTemplateSwitch->setDisabled(usePwgen);
     189           0 :   ui->label_characterset->setDisabled(usePwgen);
     190           0 : }
     191             : 
     192           0 : void PasswordDialog::setPass(const QString &output) {
     193           0 :   setPassword(output);
     194             :   //    TODO(bezet): enable ui
     195           0 : }

Generated by: LCOV version 1.13