QtPass  1.2.0-pre
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 "qtpasssettings.h"
4 #include "ui_passworddialog.h"
5 #include <QDebug>
6 #include <QLabel>
7 #include <QLineEdit>
8 
15  QWidget *parent)
16  : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
17  templating = false;
18  allFields = false;
19  ui->setupUi(this);
20  setLength(m_passConfig.length);
21  setPasswordCharTemplate(m_passConfig.selected);
22 }
23 
28 
33 void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
34  if (arg1)
35  ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
36  else
37  ui->lineEditPassword->setEchoMode(QLineEdit::Password);
38 }
39 
45 void PasswordDialog::on_createPasswordButton_clicked() {
46  ui->widget->setEnabled(false);
47  QString newPass = QtPassSettings::getPass()->Generate_b(
48  ui->spinBox_pwdLength->value(),
50  ui->passwordTemplateSwitch->currentIndex()]);
51  if (newPass.length() > 0)
52  ui->lineEditPassword->setText(newPass);
53  ui->widget->setEnabled(true);
54 }
55 
60 void PasswordDialog::setPassword(QString password) {
61  QStringList tokens = password.split("\n");
62  ui->lineEditPassword->setText(tokens[0]);
63  tokens.pop_front();
64  if (templating) {
65  QWidget *previous = ui->checkBoxShow;
66  for (int i = 0; i < ui->formLayout->rowCount(); ++i) {
67  QLayoutItem *item = ui->formLayout->itemAt(i, QFormLayout::FieldRole);
68  if (item == NULL)
69  continue;
70  QWidget *widget = item->widget();
71  for (int j = 0; j < tokens.length(); ++j) {
72  QString token = tokens.at(j);
73  if (token.startsWith(widget->objectName() + ':')) {
74  tokens.removeAt(j);
75  QString value = token.remove(0, widget->objectName().length() + 1);
76  reinterpret_cast<QLineEdit *>(widget)->setText(value.trimmed());
77  }
78  }
79  previous = widget;
80  }
81  if (allFields) {
82  for (int j = 0; j < tokens.length(); ++j) {
83  QString token = tokens.at(j);
84  if (token.contains(':')) {
85  int colon = token.indexOf(':');
86  QString field = token.left(colon);
87  QString value = token.right(token.length() - colon - 1);
88  if (!passTemplate.contains(field) && value.startsWith("//"))
89  continue; // colon is probably from a url
90  QLineEdit *line = new QLineEdit();
91  line->setObjectName(field.trimmed());
92  line->setText(value.trimmed());
93  ui->formLayout->addRow(new QLabel(field), line);
94  setTabOrder(previous, line);
95  previous = line;
96  tokens.removeAt(j);
97  --j; // tokens.length() also got shortened by the remove..
98  }
99  }
100  }
101  }
102  ui->plainTextEdit->insertPlainText(tokens.join("\n"));
103 }
104 
111  QString passFile = ui->lineEditPassword->text() + "\n";
112  for (int i = 0; i < ui->formLayout->rowCount(); ++i) {
113  QLayoutItem *item = ui->formLayout->itemAt(i, QFormLayout::FieldRole);
114  if (item == NULL)
115  continue;
116  QWidget *widget = item->widget();
117  QString text = reinterpret_cast<QLineEdit *>(widget)->text();
118  if (text.isEmpty())
119  continue;
120  passFile += widget->objectName() + ": " + text + "\n";
121  }
122  passFile += ui->plainTextEdit->toPlainText();
123  return passFile;
124 }
125 
130 void PasswordDialog::setTemplate(QString rawFields) {
131  fields = rawFields.split('\n');
132  QWidget *previous = ui->checkBoxShow;
133  foreach (QString field, fields) {
134  if (field.isEmpty())
135  continue;
136  QLineEdit *line = new QLineEdit();
137  line->setObjectName(field);
138  ui->formLayout->addRow(new QLabel(field), line);
139  setTabOrder(previous, line);
140  previous = line;
141  }
142 }
143 
148 void PasswordDialog::setFile(QString file) {
149  this->setWindowTitle(this->windowTitle() + " " + file);
150 }
151 
158 
165 
171 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
172 
179  ui->passwordTemplateSwitch->setCurrentIndex(t);
180 }
181 
188  ui->passwordTemplateSwitch->setDisabled(usePwgen);
189  ui->label_characterset->setDisabled(usePwgen);
190 }
191 
192 void PasswordDialog::setPass(const QString &output) {
193  setPassword(output);
194  // TODO(bezet): enable ui
195 }
void setFile(QString)
Sets the file (name) in the interface.
int length
passwordConfiguration::length of password.
Definition: datahelpers.h:25
PasswordDialog(const passwordConfiguration &passConfig, QWidget *parent=0)
PasswordDialog::PasswordDialog basic constructor.
void usePwgen(bool usePwgen)
PasswordDialog::usePwgen PasswordDialog::usePwgen don&#39;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 Characters[CHARSETS_COUNT]
passwordConfiguration::Characters the different character sets.
Definition: datahelpers.h:29
~PasswordDialog()
PasswordDialog::~PasswordDialog basic destructor.
void setTemplate(QString)
Sets content in the template for the interface.
enum passwordConfiguration::characterSet selected
PasswordDialog Handles the inserting and editing of passwords.
virtual QString Generate_b(int length, const QString &charset)
Pass::Generate use either pwgen or internal password generator.
Definition: pass.cpp:65
void setPassword(QString password)
Sets content in the password field in the interface.
static Pass * getPass()
void setPasswordCharTemplate(int t)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
void useTemplate(bool useTemplate)
PasswordDialog::useTemplate basic setter for use in PasswordDialog::useTemplate templating.
QString getPassword()
Returns the password as set in the password field in the interface.
holds the Password configuration settings
Definition: datahelpers.h:11
characterSet
passwordConfiguration::selected character set.
Definition: datahelpers.h:15
void setPass(const QString &output)