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