-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
passworddialog.cpp
111 lines (101 loc) · 3.56 KB
/
passworddialog.cpp
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
#include "passworddialog.h"
#include "ui_passworddialog.h"
#include <QDebug>
#include <QLabel>
#include <QLineEdit>
PasswordDialog::PasswordDialog(MainWindow *parent)
: QDialog(parent), ui(new Ui::PasswordDialog) {
mainWindow = parent;
templating = false;
allFields = false;
ui->setupUi(this);
}
PasswordDialog::~PasswordDialog() { delete ui; }
void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
if (arg1)
ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
else
ui->lineEditPassword->setEchoMode(QLineEdit::Password);
}
void PasswordDialog::on_createPasswordButton_clicked() {
ui->widget->setEnabled(false);
ui->lineEditPassword->setText(mainWindow->generatePassword());
ui->widget->setEnabled(true);
}
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"));
}
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;
}
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;
}
}
void PasswordDialog::setFile(QString file) {
this->setWindowTitle(this->windowTitle() + " " + file);
}
void PasswordDialog::templateAll(bool templateAll) { allFields = templateAll; }
void PasswordDialog::useTemplate(bool useTemplate) { templating = useTemplate; }