QtPass 1.6.0
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: 2015 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);
34 setPasswordCharTemplate(m_passConfig.selected);
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);
64 setPasswordCharTemplate(m_passConfig.selected);
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
95void PasswordDialog::on_createPasswordButton_clicked() {
96 ui->widget->setEnabled(false);
97 const int currentIndex = ui->passwordTemplateSwitch->currentIndex();
98 if (currentIndex < 0 ||
99 currentIndex >= static_cast<int>(PasswordConfiguration::CHARSETS_COUNT)) {
100 ui->widget->setEnabled(true);
101 return;
102 }
103
104 QString newPass = QtPassSettings::getPass()->generatePassword(
105 static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
106 m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
107 currentIndex)]);
108 if (!newPass.isEmpty()) {
109 ui->lineEditPassword->setText(newPass);
110 }
111 ui->widget->setEnabled(true);
112}
113
117void PasswordDialog::on_accepted() {
118 QString newValue = getPassword();
119 if (newValue.isEmpty()) {
120 return;
121 }
122
123 if (newValue.right(1) != "\n") {
124 newValue += "\n";
125 }
126
127 QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
128}
129
133void PasswordDialog::on_rejected() { setPassword(QString()); }
134
139void PasswordDialog::setPassword(const QString &password) {
140 FileContent fileContent = FileContent::parse(
141 password, m_templating ? m_fields : QStringList(), m_allFields);
142 ui->lineEditPassword->setText(fileContent.getPassword());
143
144 QWidget *previous = ui->checkBoxShow;
145 // first set templated values
146 NamedValues namedValues = fileContent.getNamedValues();
147 for (QLineEdit *line : AS_CONST(templateLines)) {
148 line->setText(namedValues.takeValue(line->objectName()));
149 previous = line;
150 }
151 // show remaining values (if there are)
152 otherLines.clear();
153 for (const NamedValue &nv : AS_CONST(namedValues)) {
154 auto *line = new QLineEdit();
155 line->setObjectName(nv.name);
156 line->setText(nv.value);
157 ui->formLayout->addRow(new QLabel(nv.name), line);
158 setTabOrder(previous, line);
159 otherLines.append(line);
160 previous = line;
161 }
162
163 ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
164}
165
172 QString passFile = ui->lineEditPassword->text() + "\n";
173 QList<QLineEdit *> allLines(templateLines);
174 allLines.append(otherLines);
175 for (QLineEdit *line : allLines) {
176 QString text = line->text();
177 if (text.isEmpty()) {
178 continue;
179 }
180 passFile += line->objectName() + ": " + text + "\n";
181 }
182 passFile += ui->plainTextEdit->toPlainText();
183 return passFile;
184}
185
190void PasswordDialog::setTemplate(const QString &rawFields, bool useTemplate) {
191 m_fields = rawFields.split('\n');
192 m_templating = useTemplate;
193 templateLines.clear();
194
195 if (m_templating) {
196 QWidget *previous = ui->checkBoxShow;
197 for (const QString &field : std::as_const(m_fields)) {
198 if (field.isEmpty()) {
199 continue;
200 }
201 auto *line = new QLineEdit();
202 line->setObjectName(field);
203 ui->formLayout->addRow(new QLabel(field), line);
204 setTabOrder(previous, line);
205 templateLines.append(line);
206 previous = line;
207 }
208 }
209}
210
217 m_allFields = templateAll;
218}
219
226 ui->spinBox_pwdLength->setValue(length);
227}
228
235 ui->passwordTemplateSwitch->setCurrentIndex(templateIndex);
236}
237
244 ui->passwordTemplateSwitch->setDisabled(usePwgen);
245 ui->label_characterset->setDisabled(usePwgen);
246}
247
252void PasswordDialog::setPass(const QString &output) {
253 setPassword(output);
254 // UI is enabled by default when password is set - no additional action needed
255}
auto getRemainingData() const -> QString
Gets remaining data not in named values.
auto getNamedValues() const -> NamedValues
Gets named value pairs from the parsed file.
auto getPassword() const -> QString
Gets the password from the parsed file.
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:23
auto takeValue(const QString &name) -> QString
Finds and removes a named value by name.
void finishedShow(const QString &)
Emitted when show finishes.
void processErrorExit(int exitCode, const QString &err)
Emitted on process error exit.
void setPassword(const QString &password)
Sets content in the password field in the interface.
PasswordDialog(PasswordConfiguration passConfig, QWidget *parent=nullptr)
PasswordDialog::PasswordDialog basic constructor.
void setLength(int length)
PasswordDialog::setLength PasswordDialog::setLength password length.
~PasswordDialog() override
PasswordDialog::~PasswordDialog basic destructor.
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don't use own password generator.
void setPasswordCharTemplate(int templateIndex)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
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 setPass(const QString &output)
Sets the password from pass show output.
auto getPassword() -> QString
Returns the password as set in the password field in the interface.
static auto getPass() -> Pass *
Get currently active pass backend instance.
static auto getPasswordConfiguration() -> PasswordConfiguration
Get complete password generation configuration.
static auto isTemplateAllFields(const bool &defaultValue=QVariant().toBool()) -> bool
Check whether template applies to all fields.
static auto isUseTemplate(const bool &defaultValue=QVariant().toBool()) -> bool
Check whether template usage is enabled.
static auto getPassTemplate(const QString &defaultValue=QVariant().toString()) -> QString
Get pass entry template.
static auto isUsePwgen(const bool &defaultValue=QVariant().toBool()) -> bool
Check whether pwgen support is enabled.
Debug utilities for QtPass.
Utility macros for QtPass.
#define AS_CONST(x)
Cross-platform const_cast for range-based for loops.
Definition helpers.h:20
Holds the Password configuration settings.
characterSet
The selected character set.