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