QtPass  1.2.3
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  m_templating = false;
20  m_allFields = false;
21  m_isNew = false;
22 
23  ui->setupUi(this);
24  setLength(m_passConfig.length);
25  setPasswordCharTemplate(m_passConfig.selected);
26 }
27 
35 PasswordDialog::PasswordDialog(const QString &file, const bool &isNew,
36  QWidget *parent)
37  : QDialog(parent), ui(new Ui::PasswordDialog), m_file(file),
38  m_isNew(isNew) {
39 
40  if (!isNew)
41  QtPassSettings::getPass()->Show(m_file);
42 
43  ui->setupUi(this);
44 
45  setWindowTitle(this->windowTitle() + " " + m_file);
51 
52  setLength(m_passConfig.length);
53  setPasswordCharTemplate(m_passConfig.selected);
54 
56  &PasswordDialog::close);
57  connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
58  connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
59 }
60 
65 
70 void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
71  if (arg1)
72  ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
73  else
74  ui->lineEditPassword->setEchoMode(QLineEdit::Password);
75 }
76 
82 void PasswordDialog::on_createPasswordButton_clicked() {
83  ui->widget->setEnabled(false);
84  QString newPass = QtPassSettings::getPass()->Generate_b(
85  static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
86  m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
87  ui->passwordTemplateSwitch->currentIndex())]);
88  if (newPass.length() > 0)
89  ui->lineEditPassword->setText(newPass);
90  ui->widget->setEnabled(true);
91 }
92 
96 void PasswordDialog::on_accepted() {
97  QString newValue = getPassword();
98  if (newValue.isEmpty())
99  return;
100 
101  if (newValue.right(1) != "\n")
102  newValue += "\n";
103 
104  QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
105 }
106 
110 void PasswordDialog::on_rejected() { setPassword(QString()); }
111 
116 void PasswordDialog::setPassword(QString password) {
117  FileContent fileContent = FileContent::parse(
118  password, m_templating ? m_fields : QStringList(), m_allFields);
119  ui->lineEditPassword->setText(fileContent.getPassword());
120 
121  QWidget *previous = ui->checkBoxShow;
122  // first set templated values
123  NamedValues namedValues = fileContent.getNamedValues();
124  for (QLineEdit *line : templateLines) {
125  line->setText(namedValues.takeValue(line->objectName()));
126  previous = line;
127  }
128  // show remaining values (if there are)
129  otherLines.clear();
130  for (const NamedValue &nv : namedValues) {
131  QLineEdit *line = new QLineEdit();
132  line->setObjectName(nv.name);
133  line->setText(nv.value);
134  ui->formLayout->addRow(new QLabel(nv.name), line);
135  setTabOrder(previous, line);
136  otherLines.append(line);
137  previous = line;
138  }
139 
140  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
141 }
142 
149  QString passFile = ui->lineEditPassword->text() + "\n";
150  QList<QLineEdit *> allLines(templateLines);
151  allLines.append(otherLines);
152  for (QLineEdit *line : allLines) {
153  QString text = line->text();
154  if (text.isEmpty())
155  continue;
156  passFile += line->objectName() + ": " + text + "\n";
157  }
158  passFile += ui->plainTextEdit->toPlainText();
159  return passFile;
160 }
161 
166 void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
167  m_fields = rawFields.split('\n');
168  m_templating = useTemplate;
169  templateLines.clear();
170 
171  if (m_templating) {
172  QWidget *previous = ui->checkBoxShow;
173  foreach (QString field, m_fields) {
174  if (field.isEmpty())
175  continue;
176  QLineEdit *line = new QLineEdit();
177  line->setObjectName(field);
178  ui->formLayout->addRow(new QLabel(field), line);
179  setTabOrder(previous, line);
180  templateLines.append(line);
181  previous = line;
182  }
183  }
184 }
185 
191 void PasswordDialog::templateAll(bool templateAll) {
192  m_allFields = templateAll;
193 }
194 
200 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
201 
208  ui->passwordTemplateSwitch->setCurrentIndex(t);
209 }
210 
216 void PasswordDialog::usePwgen(bool usePwgen) {
217  ui->passwordTemplateSwitch->setDisabled(usePwgen);
218  ui->label_characterset->setDisabled(usePwgen);
219 }
220 
221 void PasswordDialog::setPass(const QString &output) {
222  setPassword(output);
223  // TODO(bezet): enable ui
224 }
The NamedValues class is mostly a list of but also has a method to take a specific NamedValue pair ou...
Definition: filecontent.h:17
void setTemplate(QString rawFields, bool useTemplate)
Sets content in the template for 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.
QString takeValue(const QString &name)
Definition: filecontent.cpp:45
~PasswordDialog()
PasswordDialog::~PasswordDialog basic destructor.
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
virtual void Show(QString file)=0
enum PasswordConfiguration::characterSet selected
PasswordDialog Handles the inserting and editing of passwords.
void setPassword(QString password)
Sets content in the password field in the interface.
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()
static bool isUsePwgen(const bool &defaultValue=QVariant().toBool())
void setPasswordCharTemplate(int t)
PasswordDialog::setPasswordCharTemplate PasswordDialog::setPasswordCharTemplate chose the template st...
static QString getPassTemplate(const QString &defaultValue=QVariant().toString())
QString getPassword()
Returns the password as set in the password field in the interface.
static bool isUseTemplate(const bool &defaultValue=QVariant().toBool())
static bool isTemplateAllFields(const bool &defaultValue=QVariant().toBool())
virtual void Insert(QString file, QString value, bool force)=0
QString Characters[CHARSETS_COUNT]
The different character sets.
static PasswordConfiguration getPasswordConfiguration()
void processErrorExit(int exitCode, const QString &err)
void setPass(const QString &output)
QString getPassword() const
Definition: filecontent.cpp:28