Line data Source code
1 : #include "passworddialog.h"
2 : #include "debughelper.h"
3 : #include "filecontent.h"
4 : #include "passwordconfiguration.h"
5 : #include "qtpasssettings.h"
6 : #include "ui_passworddialog.h"
7 :
8 : #include <QLabel>
9 : #include <QLineEdit>
10 :
11 : /**
12 : * @brief PasswordDialog::PasswordDialog basic constructor.
13 : * @param passConfig configuration constant
14 : * @param parent
15 : */
16 7 : PasswordDialog::PasswordDialog(const PasswordConfiguration &passConfig,
17 : QWidget *parent)
18 35 : : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
19 7 : m_templating = false;
20 7 : m_allFields = false;
21 7 : m_isNew = false;
22 :
23 7 : ui->setupUi(this);
24 7 : setLength(m_passConfig.length);
25 7 : setPasswordCharTemplate(m_passConfig.selected);
26 14 : }
27 :
28 : /**
29 : * @brief PasswordDialog::PasswordDialog complete constructor.
30 : * @param passConfig configuration constant
31 : * @param file
32 : * @param isNew
33 : * @param parent
34 : */
35 0 : PasswordDialog::PasswordDialog(const QString &file, const bool &isNew,
36 : QWidget *parent)
37 0 : : QDialog(parent), ui(new Ui::PasswordDialog), m_file(file),
38 0 : m_isNew(isNew) {
39 :
40 0 : if (!isNew)
41 0 : QtPassSettings::getPass()->Show(m_file);
42 :
43 0 : ui->setupUi(this);
44 :
45 0 : setWindowTitle(this->windowTitle() + " " + m_file);
46 0 : m_passConfig = QtPassSettings::getPasswordConfiguration();
47 0 : usePwgen(QtPassSettings::isUsePwgen());
48 0 : setTemplate(QtPassSettings::getPassTemplate(),
49 0 : QtPassSettings::isUseTemplate());
50 0 : templateAll(QtPassSettings::isTemplateAllFields());
51 :
52 0 : setLength(m_passConfig.length);
53 0 : setPasswordCharTemplate(m_passConfig.selected);
54 :
55 0 : connect(QtPassSettings::getPass(), &Pass::processErrorExit, this,
56 : &PasswordDialog::close);
57 0 : connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
58 0 : connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
59 0 : }
60 :
61 : /**
62 : * @brief PasswordDialog::~PasswordDialog basic destructor.
63 : */
64 56 : PasswordDialog::~PasswordDialog() { delete ui; }
65 :
66 : /**
67 : * @brief PasswordDialog::on_checkBoxShow_stateChanged hide or show passwords.
68 : * @param arg1
69 : */
70 0 : void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
71 0 : if (arg1)
72 0 : ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
73 : else
74 0 : ui->lineEditPassword->setEchoMode(QLineEdit::Password);
75 0 : }
76 :
77 : /**
78 : * @brief PasswordDialog::on_createPasswordButton_clicked generate a random
79 : * passwords.
80 : * @todo refactor when process is untangled from MainWindow class.
81 : */
82 0 : void PasswordDialog::on_createPasswordButton_clicked() {
83 0 : ui->widget->setEnabled(false);
84 0 : QString newPass = QtPassSettings::getPass()->Generate_b(
85 0 : static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
86 0 : m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
87 0 : ui->passwordTemplateSwitch->currentIndex())]);
88 0 : if (newPass.length() > 0)
89 0 : ui->lineEditPassword->setText(newPass);
90 0 : ui->widget->setEnabled(true);
91 0 : }
92 :
93 : /**
94 : * @brief PasswordDialog::on_accepted handle Ok click for QDialog
95 : */
96 0 : void PasswordDialog::on_accepted() {
97 0 : QString newValue = getPassword();
98 0 : if (newValue.isEmpty())
99 0 : return;
100 :
101 0 : if (newValue.right(1) != "\n")
102 0 : newValue += "\n";
103 :
104 0 : QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
105 0 : }
106 :
107 : /**
108 : * @brief PasswordDialog::on_rejected handle Cancel click for QDialog
109 : */
110 0 : void PasswordDialog::on_rejected() { setPassword(QString()); }
111 :
112 : /**
113 : * @brief PasswordDialog::setPassword populate the (templated) fields.
114 : * @param password
115 : */
116 0 : void PasswordDialog::setPassword(QString password) {
117 28 : FileContent fileContent = FileContent::parse(
118 21 : password, m_templating ? m_fields : QStringList(), m_allFields);
119 21 : ui->lineEditPassword->setText(fileContent.getPassword());
120 :
121 7 : QWidget *previous = ui->checkBoxShow;
122 : // first set templated values
123 7 : NamedValues namedValues = fileContent.getNamedValues();
124 43 : for (QLineEdit *line : templateLines) {
125 8 : line->setText(namedValues.takeValue(line->objectName()));
126 2 : previous = line;
127 0 : }
128 : // show remaining values (if there are)
129 7 : otherLines.clear();
130 41 : for (const NamedValue &nv : namedValues) {
131 6 : QLineEdit *line = new QLineEdit();
132 2 : line->setObjectName(nv.name);
133 2 : line->setText(nv.value);
134 6 : ui->formLayout->addRow(new QLabel(nv.name), line);
135 2 : setTabOrder(previous, line);
136 2 : otherLines.append(line);
137 2 : previous = line;
138 2 : }
139 :
140 21 : ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
141 7 : }
142 :
143 : /**
144 : * @brief PasswordDialog::getPassword join the (templated) fields to a QString
145 : * for writing back.
146 : * @return collappsed password.
147 : */
148 0 : QString PasswordDialog::getPassword() {
149 21 : QString passFile = ui->lineEditPassword->text() + "\n";
150 7 : QList<QLineEdit *> allLines(templateLines);
151 7 : allLines.append(otherLines);
152 51 : for (QLineEdit *line : allLines) {
153 4 : QString text = line->text();
154 4 : if (text.isEmpty())
155 0 : continue;
156 24 : passFile += line->objectName() + ": " + text + "\n";
157 12 : }
158 21 : passFile += ui->plainTextEdit->toPlainText();
159 : return passFile;
160 14 : }
161 :
162 : /**
163 : * @brief PasswordDialog::setTemplate set the template and create the fields.
164 : * @param rawFields
165 : */
166 0 : void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
167 6 : m_fields = rawFields.split('\n');
168 6 : m_templating = useTemplate;
169 6 : templateLines.clear();
170 :
171 6 : if (m_templating) {
172 3 : QWidget *previous = ui->checkBoxShow;
173 36 : foreach (QString field, m_fields) {
174 3 : if (field.isEmpty())
175 : continue;
176 6 : QLineEdit *line = new QLineEdit();
177 2 : line->setObjectName(field);
178 6 : ui->formLayout->addRow(new QLabel(field), line);
179 2 : setTabOrder(previous, line);
180 2 : templateLines.append(line);
181 2 : previous = line;
182 2 : }
183 3 : }
184 6 : }
185 :
186 : /**
187 : * @brief PasswordDialog::templateAll basic setter for use in
188 : * PasswordDialog::setPassword templating all tokenisable lines.
189 : * @param templateAll
190 : */
191 0 : void PasswordDialog::templateAll(bool templateAll) {
192 3 : m_allFields = templateAll;
193 3 : }
194 :
195 : /**
196 : * @brief PasswordDialog::setLength
197 : * PasswordDialog::setLength password length.
198 : * @param l
199 : */
200 14 : void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
201 :
202 : /**
203 : * @brief PasswordDialog::setPasswordCharTemplate
204 : * PasswordDialog::setPasswordCharTemplate chose the template style.
205 : * @param t
206 : */
207 0 : void PasswordDialog::setPasswordCharTemplate(int t) {
208 14 : ui->passwordTemplateSwitch->setCurrentIndex(t);
209 7 : }
210 :
211 : /**
212 : * @brief PasswordDialog::usePwgen
213 : * PasswordDialog::usePwgen don't use own password generator.
214 : * @param usePwgen
215 : */
216 0 : void PasswordDialog::usePwgen(bool usePwgen) {
217 0 : ui->passwordTemplateSwitch->setDisabled(usePwgen);
218 0 : ui->label_characterset->setDisabled(usePwgen);
219 0 : }
220 :
221 0 : void PasswordDialog::setPass(const QString &output) {
222 21 : setPassword(output);
223 : // TODO(bezet): enable ui
224 7 : }
|