QtPass  1.3.2
Multi-platform GUI for pass, the standard unix password manager.
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);
29  setPasswordCharTemplate(m_passConfig.selected);
30 
33 }
34 
41 PasswordDialog::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)
47  QtPassSettings::getPass()->Show(m_file);
48 
49  ui->setupUi(this);
50 
51  setWindowTitle(this->windowTitle() + " " + m_file);
57 
58  setLength(m_passConfig.length);
59  setPasswordCharTemplate(m_passConfig.selected);
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 
78 void 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 
90 void 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 
104 void 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 
118 void PasswordDialog::on_rejected() { setPassword(QString()); }
119 
124 void 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 : 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 : 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 
174 void 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 
199 void PasswordDialog::templateAll(bool templateAll) {
200  m_allFields = templateAll;
201 }
202 
208 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
209 
216  ui->passwordTemplateSwitch->setCurrentIndex(t);
217 }
218 
224 void PasswordDialog::usePwgen(bool usePwgen) {
225  ui->passwordTemplateSwitch->setDisabled(usePwgen);
226  ui->label_characterset->setDisabled(usePwgen);
227 }
228 
229 void PasswordDialog::setPass(const QString &output) {
230  setPassword(output);
231  // TODO(bezet): enable ui
232 }
Pass::processErrorExit
void processErrorExit(int exitCode, const QString &err)
PasswordConfiguration::characterSet
characterSet
The selected character set.
Definition: passwordconfiguration.h:14
PasswordDialog::setLength
void setLength(int l)
PasswordDialog::setLength PasswordDialog::setLength password length.
Definition: passworddialog.cpp:208
PasswordConfiguration::length
int length
Length of the password.
Definition: passwordconfiguration.h:24
QtPassSettings::getPassTemplate
static QString getPassTemplate(const QString &defaultValue=QVariant().toString())
Definition: qtpasssettings.cpp:519
PasswordDialog::setPass
void setPass(const QString &output)
Definition: passworddialog.cpp:229
QtPassSettings::getPasswordConfiguration
static PasswordConfiguration getPasswordConfiguration()
Definition: qtpasssettings.cpp:35
FileContent::getNamedValues
NamedValues getNamedValues() const
Definition: filecontent.cpp:38
QtPassSettings::isUsePwgen
static bool isUsePwgen(const bool &defaultValue=QVariant().toBool())
Definition: qtpasssettings.cpp:408
NamedValues
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition: filecontent.h:17
debughelper.h
Pass::Show
virtual void Show(QString file)=0
filecontent.h
QtPassSettings::isTemplateAllFields
static bool isTemplateAllFields(const bool &defaultValue=QVariant().toBool())
Definition: qtpasssettings.cpp:537
FileContent
Definition: filecontent.h:25
PasswordDialog::setPasswordCharTemplate
void setPasswordCharTemplate(int t)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
Definition: passworddialog.cpp:215
QtPassSettings::getPass
static Pass * getPass()
Definition: qtpasssettings.cpp:87
FileContent::getRemainingData
QString getRemainingData() const
Definition: filecontent.cpp:40
PasswordDialog::getPassword
QString getPassword()
Returns the password as set in the password field in the interface.
Definition: passworddialog.cpp:156
NamedValues::takeValue
QString takeValue(const QString &name)
Definition: filecontent.cpp:59
PasswordConfiguration::selected
enum PasswordConfiguration::characterSet selected
QtPassSettings::isUseTemplate
static bool isUseTemplate(const bool &defaultValue=QVariant().toBool())
Definition: qtpasssettings.cpp:528
PasswordDialog::usePwgen
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don't use own password generator.
Definition: passworddialog.cpp:224
PasswordDialog::templateAll
void templateAll(bool templateAll)
PasswordDialog::templateAll basic setter for use in PasswordDialog::setPassword templating all tokeni...
Definition: passworddialog.cpp:199
Pass::Insert
virtual void Insert(QString file, QString value, bool force)=0
Pass::Generate_b
virtual QString Generate_b(unsigned int length, const QString &charset)
Pass::Generate use either pwgen or internal password generator.
Definition: pass.cpp:69
Ui
Definition: configdialog.h:9
PasswordDialog::~PasswordDialog
~PasswordDialog()
Pass{}{}wordDialog::~PasswordDialog basic destructor.
Definition: passworddialog.cpp:72
PasswordDialog
PasswordDialog Handles the inserting and editing of passwords.
Definition: passworddialog.h:20
PasswordConfiguration
Holds the Password configuration settings.
Definition: passwordconfiguration.h:10
passworddialog.h
PasswordDialog::setTemplate
void setTemplate(QString rawFields, bool useTemplate)
Sets content in the template for the interface.
Definition: passworddialog.cpp:174
PasswordConfiguration::Characters
QString Characters[CHARSETS_COUNT]
The different character sets.
Definition: passwordconfiguration.h:28
NamedValue
Definition: filecontent.h:8
FileContent::getPassword
QString getPassword() const
Definition: filecontent.cpp:36
FileContent::parse
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
PasswordDialog::setPassword
void setPassword(QString password)
Sets content in the password field in the interface.
Definition: passworddialog.cpp:124
Pass::finishedShow
void finishedShow(const QString &)
qtpasssettings.h
passwordconfiguration.h
PasswordDialog::PasswordDialog
PasswordDialog(const PasswordConfiguration &passConfig, QWidget *parent=nullptr)
PasswordDialog::PasswordDialog basic constructor.
Definition: passworddialog.cpp:20
pass.h