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 "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 
62  &PasswordDialog::close);
63  connect(this, &PasswordDialog::accepted, this, &PasswordDialog::on_accepted);
64  connect(this, &PasswordDialog::rejected, this, &PasswordDialog::on_rejected);
65 }
66 
71 
76 void PasswordDialog::on_checkBoxShow_stateChanged(int arg1) {
77  if (arg1)
78  ui->lineEditPassword->setEchoMode(QLineEdit::Normal);
79  else
80  ui->lineEditPassword->setEchoMode(QLineEdit::Password);
81 }
82 
88 void PasswordDialog::on_createPasswordButton_clicked() {
89  ui->widget->setEnabled(false);
90  QString newPass = QtPassSettings::getPass()->Generate_b(
91  static_cast<unsigned int>(ui->spinBox_pwdLength->value()),
92  m_passConfig.Characters[static_cast<PasswordConfiguration::characterSet>(
93  ui->passwordTemplateSwitch->currentIndex())]);
94  if (newPass.length() > 0)
95  ui->lineEditPassword->setText(newPass);
96  ui->widget->setEnabled(true);
97 }
98 
102 void PasswordDialog::on_accepted() {
103  QString newValue = getPassword();
104  if (newValue.isEmpty())
105  return;
106 
107  if (newValue.right(1) != "\n")
108  newValue += "\n";
109 
110  QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
111 }
112 
116 void PasswordDialog::on_rejected() { setPassword(QString()); }
117 
124  password, m_templating ? m_fields : QStringList(), m_allFields);
125  ui->lineEditPassword->setText(fileContent.getPassword());
126 
127  QWidget *previous = ui->checkBoxShow;
128  // first set templated values
130  for (QLineEdit *line : templateLines) {
131  line->setText(namedValues.takeValue(line->objectName()));
132  previous = line;
133  }
134  // show remaining values (if there are)
135  otherLines.clear();
136  for (const NamedValue &nv : namedValues) {
137  QLineEdit *line = new QLineEdit();
138  line->setObjectName(nv.name);
139  line->setText(nv.value);
140  ui->formLayout->addRow(new QLabel(nv.name), line);
141  setTabOrder(previous, line);
142  otherLines.append(line);
143  previous = line;
144  }
145 
146  ui->plainTextEdit->insertPlainText(fileContent.getRemainingData());
147 }
148 
155  QString passFile = ui->lineEditPassword->text() + "\n";
156  QList<QLineEdit *> allLines(templateLines);
157  allLines.append(otherLines);
158  for (QLineEdit *line : allLines) {
159  QString text = line->text();
160  if (text.isEmpty())
161  continue;
162  passFile += line->objectName() + ": " + text + "\n";
163  }
164  passFile += ui->plainTextEdit->toPlainText();
165  return passFile;
166 }
167 
172 void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
173  m_fields = rawFields.split('\n');
174  m_templating = useTemplate;
175  templateLines.clear();
176 
177  if (m_templating) {
178  QWidget *previous = ui->checkBoxShow;
179  foreach (QString field, m_fields) {
180  if (field.isEmpty())
181  continue;
182  QLineEdit *line = new QLineEdit();
183  line->setObjectName(field);
184  ui->formLayout->addRow(new QLabel(field), line);
185  setTabOrder(previous, line);
186  templateLines.append(line);
187  previous = line;
188  }
189  }
190 }
191 
197 void PasswordDialog::templateAll(bool templateAll) {
198  m_allFields = templateAll;
199 }
200 
206 void PasswordDialog::setLength(int l) { ui->spinBox_pwdLength->setValue(l); }
207 
214  ui->passwordTemplateSwitch->setCurrentIndex(t);
215 }
216 
222 void PasswordDialog::usePwgen(bool usePwgen) {
223  ui->passwordTemplateSwitch->setDisabled(usePwgen);
224  ui->label_characterset->setDisabled(usePwgen);
225 }
226 
227 void PasswordDialog::setPass(const QString &output) {
228  setPassword(output);
229  // TODO(bezet): enable ui
230 }
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.
FileContent FNamedValues onst QString & fileContent
Definition: filecontent.cpp:3
PasswordDialog(const PasswordConfiguration &passConfig, QWidget *parent=0)
PasswordDialog::PasswordDialog basic constructor.
void MainWindow::foconst cusInpu & t()
MainWindow::focusInput selects any text (if applicable) in the search box and sets focus to it...
Definition: mainwindow.cpp:133
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...
QString password
Definition: filecontent.cpp:7
NamedValues namedValues
Definition: filecontent.cpp:9
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.
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::const Generat&e use eithconst er pwge&n or internal password generator.
Definition: pass.cpp:70
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.
void finishedShow(const QString &)
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