QtPass 1.4.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#include "passworddialog.h"
2#include "filecontent.h"
3#include "pass.h"
5#include "qtpasssettings.h"
6#include "ui_passworddialog.h"
7
8#include <QLabel>
9#include <QLineEdit>
10
11#ifdef QT_DEBUG
12#include "debughelper.h"
13#endif
14
21 QWidget *parent)
22 : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
23 m_templating = false;
24 m_allFields = false;
25 m_isNew = false;
26
27 ui->setupUi(this);
28 setLength(m_passConfig.length);
30
33}
34
41PasswordDialog::PasswordDialog(const QString &file, const bool &isNew,
42 QWidget *parent)
43 : QDialog(parent), ui(new Ui::PasswordDialog), m_file(file),
44 m_isNew(isNew) {
45
46 if (!isNew)
48
49 ui->setupUi(this);
50
51 setWindowTitle(this->windowTitle() + " " + m_file);
57
58 setLength(m_passConfig.length);
60
64 &PasswordDialog::close);
65 connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
66 connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
67}
68
73
78void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
79 if (arg1)
80 ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
81 else
82 ui->lineEditPassword->setEchoMode(QLineEdit::Password);
83}
84
90void PasswordDialog::on_createPasswordButton_clicked() {
91 ui->widget->setEnabled(false);
92 QString newPass = QtPassSettings::getPass()->Generate_b(
93 static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
94 m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
95 ui->passwordTemplateSwitch->currentIndex())]);
96 if (newPass.length() > 0)
97 ui->lineEditPassword->setText(newPass);
98 ui->widget->setEnabled(true);
99}
100
104void PasswordDialog::on_accepted() {
105 QString newValue = getPassword();
106 if (newValue.isEmpty())
107 return;
108
109 if (newValue.right(1) != "\n")
110 newValue += "\n";
111
112 QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
113}
114
118void PasswordDialog::on_rejected() { setPassword(QString()); }
119
124void PasswordDialog::setPassword(QString password) {
125 FileContent fileContent = FileContent::parse(
126 password, m_templating ? m_fields : QStringList(), m_allFields);
127 ui->lineEditPassword->setText(fileContent.getPassword());
128
129 QWidget *previous = ui->checkBoxShow;
130 // first set templated values
131 NamedValues namedValues = fileContent.getNamedValues();
132 for (QLineEdit *line : qAsConst(templateLines)) {
133 line->setText(namedValues.takeValue(line->objectName()));
134 previous = line;
135 }
136 // show remaining values (if there are)
137 otherLines.clear();
138 for (const NamedValue &nv : qAsConst(namedValues)) {
139 auto *line = new QLineEdit();
140 line->setObjectName(nv.name);
141 line->setText(nv.value);
142 ui->formLayout->addRow(new QLabel(nv.name), line);
143 setTabOrder(previous, line);
144 otherLines.append(line);
145 previous = line;
146 }
147
148 ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
149}
150
157 QString passFile = ui->lineEditPassword->text() + "\n";
158 QList<QLineEdit *> allLines(templateLines);
159 allLines.append(otherLines);
160 for (QLineEdit *line : allLines) {
161 QString text = line->text();
162 if (text.isEmpty())
163 continue;
164 passFile += line->objectName() + ": " + text + "\n";
165 }
166 passFile += ui->plainTextEdit->toPlainText();
167 return passFile;
168}
169
174void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
175 m_fields = rawFields.split('\n');
176 m_templating = useTemplate;
177 templateLines.clear();
178
179 if (m_templating) {
180 QWidget *previous = ui->checkBoxShow;
181 foreach (QString field, m_fields) {
182 if (field.isEmpty())
183 continue;
184 auto *line = new QLineEdit();
185 line->setObjectName(field);
186 ui->formLayout->addRow(new QLabel(field), line);
187 setTabOrder(previous, line);
188 templateLines.append(line);
189 previous = line;
190 }
191 }
192}
193
199void PasswordDialog::templateAll(bool templateAll) {
200 m_allFields = templateAll;
201}
202
208void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
209
216 ui->passwordTemplateSwitch->setCurrentIndex(t);
217}
218
224void PasswordDialog::usePwgen(bool usePwgen) {
225 ui->passwordTemplateSwitch->setDisabled(usePwgen);
226 ui->label_characterset->setDisabled(usePwgen);
227}
228
229void PasswordDialog::setPass(const QString &output) {
230 setPassword(output);
231 // TODO(bezet): enable ui
232}
QString getRemainingData() const
Definition: filecontent.cpp:40
QString getPassword() const
Definition: filecontent.cpp:36
NamedValues getNamedValues() const
Definition: filecontent.cpp:38
static FileContent parse(const QString &fileContent, const QStringList &templateFields, bool allFields)
parse parses the given fileContent in a FileContent object. The password is accessible through getPas...
Definition: filecontent.cpp:7
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition: filecontent.h:17
QString takeValue(const QString &name)
Definition: filecontent.cpp:59
void finishedShow(const QString &)
virtual void Show(QString file)=0
virtual void Insert(QString file, QString value, bool force)=0
virtual QString Generate_b(unsigned int length, const QString &charset)
Pass::Generate use either pwgen or internal password generator.
Definition: pass.cpp:71
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 setTemplate(QString rawFields, bool useTemplate)
Sets content in the template for the interface.
PasswordDialog(const PasswordConfiguration &passConfig, QWidget *parent=nullptr)
PasswordDialog::PasswordDialog basic constructor.
void setPassword(QString password)
Sets content in the password field in the interface.
~PasswordDialog()
Pass{}{}wordDialog::~PasswordDialog basic destructor.
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don't use own password generator.
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.
QString getPassword()
Returns the password as set in the password field in the interface.
void setPass(const QString &output)
static bool isUseTemplate(const bool &defaultValue=QVariant().toBool())
static bool isTemplateAllFields(const bool &defaultValue=QVariant().toBool())
static QString getPassTemplate(const QString &defaultValue=QVariant().toString())
static bool isUsePwgen(const bool &defaultValue=QVariant().toBool())
static Pass * getPass()
static PasswordConfiguration getPasswordConfiguration()
Definition: configdialog.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.