QtPass 1.5.1
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
passworddialog.cpp
Go to the documentation of this file.
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"
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
25 QWidget *parent)
26 : QDialog(parent), ui(new Ui::PasswordDialog),
27 m_passConfig(std::move(passConfig)) {
28 m_templating = false;
29 m_allFields = false;
30 m_isNew = false;
31
32 ui->setupUi(this);
33 setLength(m_passConfig.length);
35
38}
39
46PasswordDialog::PasswordDialog(QString file, const bool &isNew, QWidget *parent)
47 : QDialog(parent), ui(new Ui::PasswordDialog), m_file(std::move(file)),
48 m_isNew(isNew) {
49
50 if (!isNew) {
51 QtPassSettings::getPass()->Show(m_file);
52 }
53
54 ui->setupUi(this);
55
56 setWindowTitle(this->windowTitle() + " " + m_file);
62
63 setLength(m_passConfig.length);
65
69 &PasswordDialog::close);
70 connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
71 connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
72}
73
78
83void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
84 if (arg1) {
85 ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
86 } else {
87 ui->lineEditPassword->setEchoMode(QLineEdit::Password);
88 }
89}
90
96void PasswordDialog::on_createPasswordButton_clicked() {
97 ui->widget->setEnabled(false);
98 QString newPass = QtPassSettings::getPass()->Generate_b(
99 static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
100 m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
101 ui->passwordTemplateSwitch->currentIndex())]);
102 if (newPass.length() > 0) {
103 ui->lineEditPassword->setText(newPass);
104 }
105 ui->widget->setEnabled(true);
106}
107
111void PasswordDialog::on_accepted() {
112 QString newValue = getPassword();
113 if (newValue.isEmpty()) {
114 return;
115 }
116
117 if (newValue.right(1) != "\n") {
118 newValue += "\n";
119 }
120
121 QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
122}
123
127void PasswordDialog::on_rejected() { setPassword(QString()); }
128
133void PasswordDialog::setPassword(const QString &password) {
134 FileContent fileContent = FileContent::parse(
135 password, m_templating ? m_fields : QStringList(), m_allFields);
136 ui->lineEditPassword->setText(fileContent.getPassword());
137
138 QWidget *previous = ui->checkBoxShow;
139 // first set templated values
140 NamedValues namedValues = fileContent.getNamedValues();
141 for (QLineEdit *line : AS_CONST(templateLines)) {
142 line->setText(namedValues.takeValue(line->objectName()));
143 previous = line;
144 }
145 // show remaining values (if there are)
146 otherLines.clear();
147 for (const NamedValue &nv : AS_CONST(namedValues)) {
148 auto *line = new QLineEdit();
149 line->setObjectName(nv.name);
150 line->setText(nv.value);
151 ui->formLayout->addRow(new QLabel(nv.name), line);
152 setTabOrder(previous, line);
153 otherLines.append(line);
154 previous = line;
155 }
156
157 ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
158}
159
166 QString passFile = ui->lineEditPassword->text() + "\n";
167 QList<QLineEdit *> allLines(templateLines);
168 allLines.append(otherLines);
169 for (QLineEdit *line : allLines) {
170 QString text = line->text();
171 if (text.isEmpty()) {
172 continue;
173 }
174 passFile += line->objectName() + ": " + text + "\n";
175 }
176 passFile += ui->plainTextEdit->toPlainText();
177 return passFile;
178}
179
184void PasswordDialog::setTemplate(const QString &rawFields, bool useTemplate) {
185 m_fields = rawFields.split('\n');
186 m_templating = useTemplate;
187 templateLines.clear();
188
189 if (m_templating) {
190 QWidget *previous = ui->checkBoxShow;
191 foreach (QString field, m_fields) {
192 if (field.isEmpty()) {
193 continue;
194 }
195 auto *line = new QLineEdit();
196 line->setObjectName(field);
197 ui->formLayout->addRow(new QLabel(field), line);
198 setTabOrder(previous, line);
199 templateLines.append(line);
200 previous = line;
201 }
202 }
203}
204
210void PasswordDialog::templateAll(bool templateAll) {
211 m_allFields = templateAll;
212}
213
219void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
220
227 ui->passwordTemplateSwitch->setCurrentIndex(t);
228}
229
235void PasswordDialog::usePwgen(bool usePwgen) {
236 ui->passwordTemplateSwitch->setDisabled(usePwgen);
237 ui->label_characterset->setDisabled(usePwgen);
238}
239
240void PasswordDialog::setPass(const QString &output) {
241 setPassword(output);
242 // TODO(bezet): enable ui
243}
auto getRemainingData() const -> QString
auto getNamedValues() const -> NamedValues
auto getPassword() const -> QString
static auto parse(const QString &fileContent, const QStringList &templateFields, bool allFields) -> FileContent
parse parses the given fileContent in a FileContent object. The password is accessible through getPas...
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition filecontent.h:19
auto takeValue(const QString &name) -> QString
void finishedShow(const QString &)
void processErrorExit(int exitCode, const QString &err)
PasswordDialog Handles the inserting and editing of passwords.
void setPasswordCharTemplate(int t)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
void setPassword(const QString &password)
Sets content in the password field in the interface.
PasswordDialog(PasswordConfiguration passConfig, QWidget *parent=nullptr)
PasswordDialog::PasswordDialog basic constructor.
~PasswordDialog()
Pass{}{}wordDialog::~PasswordDialog basic destructor.
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don't use own password generator.
void setTemplate(const QString &rawFields, bool useTemplate)
Sets content in the template for the interface.
void templateAll(bool templateAll)
PasswordDialog::templateAll basic setter for use in PasswordDialog::setPassword templating all tokeni...
void setLength(int l)
PasswordDialog::setLength PasswordDialog::setLength password length.
void setPass(const QString &output)
auto getPassword() -> QString
Returns the password as set in the password field in the interface.
static auto getPass() -> Pass *
static auto getPasswordConfiguration() -> PasswordConfiguration
static auto isTemplateAllFields(const bool &defaultValue=QVariant().toBool()) -> bool
static auto isUseTemplate(const bool &defaultValue=QVariant().toBool()) -> bool
static auto getPassTemplate(const QString &defaultValue=QVariant().toString()) -> QString
static auto isUsePwgen(const bool &defaultValue=QVariant().toBool()) -> bool
#define AS_CONST(x)
Definition helpers.h:9
Holds the Password configuration settings.
int length
Length of the password.
enum PasswordConfiguration::characterSet selected
characterSet
The selected character set.
QString Characters[CHARSETS_COUNT]
The different character sets.