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 
34 PasswordDialog::PasswordDialog(const QString &file, const bool &isNew,
35  QWidget *parent)
36  : QDialog(parent), ui(new Ui::PasswordDialog), m_file(file),
37  m_isNew(isNew) {
38 
39  if (!isNew)
40  QtPassSettings::getPass()->Show(m_file);
41 
42  ui->setupUi(this);
43 
44  setWindowTitle(this->windowTitle() + " " + m_file);
50 
51  setLength(m_passConfig.length);
52  setPasswordCharTemplate(m_passConfig.selected);
53 
55  &PasswordDialog::close);
56  connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
57  connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
58 }
59 
64 
69 void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
70  if (arg1)
71  ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
72  else
73  ui->lineEditPassword->setEchoMode(QLineEdit::Password);
74 }
75 
81 void PasswordDialog::on_createPasswordButton_clicked() {
82  ui->widget->setEnabled(false);
83  QString newPass = QtPassSettings::getPass()->Generate_b(
84  static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
85  m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
86  ui->passwordTemplateSwitch->currentIndex())]);
87  if (newPass.length() > 0)
88  ui->lineEditPassword->setText(newPass);
89  ui->widget->setEnabled(true);
90 }
91 
95 void PasswordDialog::on_accepted() {
96  QString newValue = getPassword();
97  if (newValue.isEmpty())
98  return;
99 
100  if (newValue.right(1) != "\n")
101  newValue += "\n";
102 
103  QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
104 }
105 
109 void PasswordDialog::on_rejected() { setPassword(QString()); }
110 
115 void PasswordDialog::setPassword(QString password) {
116  FileContent fileContent = FileContent::parse(
117  password, m_templating ? m_fields : QStringList(), m_allFields);
118  ui->lineEditPassword->setText(fileContent.getPassword());
119 
120  QWidget *previous = ui->checkBoxShow;
121  // first set templated values
122  NamedValues namedValues = fileContent.getNamedValues();
123  for (QLineEdit *line : templateLines) {
124  line->setText(namedValues.takeValue(line->objectName()));
125  previous = line;
126  }
127  // show remaining values (if there are)
128  otherLines.clear();
129  for (const NamedValue &nv : namedValues) {
130  QLineEdit *line = new QLineEdit();
131  line->setObjectName(nv.name);
132  line->setText(nv.value);
133  ui->formLayout->addRow(new QLabel(nv.name), line);
134  setTabOrder(previous, line);
135  otherLines.append(line);
136  previous = line;
137  }
138 
139  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
140 }
141 
148  QString passFile = ui->lineEditPassword->text() + "\n";
149  QList<QLineEdit *> allLines(templateLines);
150  allLines.append(otherLines);
151  for (QLineEdit *line : allLines) {
152  QString text = line->text();
153  if (text.isEmpty())
154  continue;
155  passFile += line->objectName() + ": " + text + "\n";
156  }
157  passFile += ui->plainTextEdit->toPlainText();
158  return passFile;
159 }
160 
165 void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
166  m_fields = rawFields.split('\n');
167  m_templating = useTemplate;
168  templateLines.clear();
169 
170  if (m_templating) {
171  QWidget *previous = ui->checkBoxShow;
172  foreach (QString field, m_fields) {
173  if (field.isEmpty())
174  continue;
175  QLineEdit *line = new QLineEdit();
176  line->setObjectName(field);
177  ui->formLayout->addRow(new QLabel(field), line);
178  setTabOrder(previous, line);
179  templateLines.append(line);
180  previous = line;
181  }
182  }
183 }
184 
190 void PasswordDialog::templateAll(bool templateAll) {
191  m_allFields = templateAll;
192 }
193 
199 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
200 
207  ui->passwordTemplateSwitch->setCurrentIndex(t);
208 }
209 
215 void PasswordDialog::usePwgen(bool usePwgen) {
216  ui->passwordTemplateSwitch->setDisabled(usePwgen);
217  ui->label_characterset->setDisabled(usePwgen);
218 }
219 
220 void PasswordDialog::setPass(const QString &output) {
221  setPassword(output);
222  // TODO(bezet): enable ui
223 }
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
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