QtPass  1.2.1
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 "debughelper.h"
3 #include "filecontent.h"
5 #include "qtpasssettings.h"
6 #include "ui_passworddialog.h"
7 
8 #include <QLabel>
9 #include <QLineEdit>
10 
17  QWidget *parent)
18  : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
19  templating = false;
20  allFields = false;
21  ui->setupUi(this);
22  setLength(m_passConfig.length);
23  setPasswordCharTemplate(m_passConfig.selected);
24 }
25 
30 
35 void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
36  if (arg1)
37  ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
38  else
39  ui->lineEditPassword->setEchoMode(QLineEdit::Password);
40 }
41 
47 void PasswordDialog::on_createPasswordButton_clicked() {
48  ui->widget->setEnabled(false);
49  QString newPass = QtPassSettings::getPass()->Generate_b(
50  static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
51  m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
52  ui->passwordTemplateSwitch->currentIndex())]);
53  if (newPass.length() > 0)
54  ui->lineEditPassword->setText(newPass);
55  ui->widget->setEnabled(true);
56 }
57 
62 void PasswordDialog::setPassword(QString password) {
63  FileContent fileContent = FileContent::parse(
64  password, templating ? fields : QStringList(), allFields);
65  ui->lineEditPassword->setText(fileContent.getPassword());
66 
67  QWidget *previous = ui->checkBoxShow;
68  // first set templated values
69  NamedValues namedValues = fileContent.getNamedValues();
70  for (QLineEdit *line : templateLines) {
71  line->setText(namedValues.takeValue(line->objectName()));
72  previous = line;
73  }
74  // show remaining values (if there are)
75  otherLines.clear();
76  for (const NamedValue &nv : namedValues) {
77  QLineEdit *line = new QLineEdit();
78  line->setObjectName(nv.name);
79  line->setText(nv.value);
80  ui->formLayout->addRow(new QLabel(nv.name), line);
81  setTabOrder(previous, line);
82  otherLines.append(line);
83  previous = line;
84  }
85 
86  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
87 }
88 
95  QString passFile = ui->lineEditPassword->text() + "\n";
96  QList<QLineEdit *> allLines(templateLines);
97  allLines.append(otherLines);
98  for (QLineEdit *line : allLines) {
99  QString text = line->text();
100  if (text.isEmpty())
101  continue;
102  passFile += line->objectName() + ": " + text + "\n";
103  }
104  passFile += ui->plainTextEdit->toPlainText();
105  return passFile;
106 }
107 
112 void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
113  fields = rawFields.split('\n');
114  templating = useTemplate;
115  templateLines.clear();
116 
117  if (templating) {
118  QWidget *previous = ui->checkBoxShow;
119  foreach (QString field, fields) {
120  if (field.isEmpty())
121  continue;
122  QLineEdit *line = new QLineEdit();
123  line->setObjectName(field);
124  ui->formLayout->addRow(new QLabel(field), line);
125  setTabOrder(previous, line);
126  templateLines.append(line);
127  previous = line;
128  }
129  }
130 }
131 
136 void PasswordDialog::setFile(QString file) {
137  this->setWindowTitle(this->windowTitle() + " " + file);
138 }
139 
145 void PasswordDialog::templateAll(bool templateAll) { allFields = templateAll; }
146 
152 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
153 
160  ui->passwordTemplateSwitch->setCurrentIndex(t);
161 }
162 
168 void PasswordDialog::usePwgen(bool usePwgen) {
169  ui->passwordTemplateSwitch->setDisabled(usePwgen);
170  ui->label_characterset->setDisabled(usePwgen);
171 }
172 
173 void PasswordDialog::setPass(const QString &output) {
174  setPassword(output);
175  // TODO(bezet): enable ui
176 }
The NamedValues class is mostly a list of but also has a method to take a specific NamedValue pair ou...
Definition: filecontent.h:16
QPlainTextEdit * plainTextEdit
void setTemplate(QString rawFields, bool useTemplate)
Sets content in the template for the interface.
void setFile(QString)
Sets the file (name) in the interface.
int length
Length of the password.
Holds the Password configuration settings.
NamedValues getNamedValues() const
Definition: filecontent.cpp:30
Definition: configdialog.h:9
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don&#39;t use own password generator.
PasswordDialog(const PasswordConfiguration &passConfig, QWidget *parent=0)
PasswordDialog::PasswordDialog basic constructor.
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 setupUi(QDialog *PasswordDialog)
QString takeValue(const QString &name)
Definition: filecontent.cpp:45
~PasswordDialog()
PasswordDialog::~PasswordDialog basic destructor.
QCheckBox * checkBoxShow
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:3
QSpinBox * spinBox_pwdLength
enum PasswordConfiguration::characterSet selected
PasswordDialog Handles the inserting and editing of passwords.
QLineEdit * lineEditPassword
void setPassword(QString password)
Sets content in the password field in the interface.
QFormLayout * formLayout
QString getRemainingData() const
Definition: filecontent.cpp:32
virtual QString Generate_b(unsigned int length, const QString &charset)
Pass::Generate use either pwgen or internal password generator.
Definition: pass.cpp:63
static Pass * getPass()
void setPasswordCharTemplate(int t)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
QString getPassword()
Returns the password as set in the password field in the interface.
QString Characters[CHARSETS_COUNT]
The different character sets.
void setPass(const QString &output)
QString getPassword() const
Definition: filecontent.cpp:28
QComboBox * passwordTemplateSwitch