summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClaudio Maradonna <penguyman@stronzi.org>2018-04-24 14:47:11 +0200
committerClaudio Maradonna <penguyman@stronzi.org>2018-04-24 14:47:11 +0200
commitc13ed0828f2705c782fa745d7633bf496ba3dc75 (patch)
treeaef37df07a73dca9942bf5e5e31671c82207c2d4 /src
parent189d075ff7c29660801792df9abd7be7dfdae993 (diff)
Refactor PasswordDialog
Diffstat (limited to 'src')
-rw-r--r--src/mainwindow.cpp43
-rw-r--r--src/passworddialog.cpp81
-rw-r--r--src/passworddialog.h19
-rw-r--r--src/qtpasssettings.h2
4 files changed, 84 insertions, 61 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index a562acdf..ffbcf6a5 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -304,14 +304,14 @@ bool MainWindow::checkConfig() {
model.setNameFilterDisables(false);
proxyModel.setSourceModel(&model);
- proxyModel.setModelAndStore(&model, QtPassSettings::getPassStore());
+ proxyModel.setModelAndStore(&model, passStore);
selectionModel.reset(new QItemSelectionModel(&proxyModel));
- model.fetchMore(model.setRootPath(QtPassSettings::getPassStore()));
+ model.fetchMore(model.setRootPath(passStore));
model.sort(0, Qt::AscendingOrder);
ui->treeView->setModel(&proxyModel);
ui->treeView->setRootIndex(proxyModel.mapFromSource(
- model.setRootPath(QtPassSettings::getPassStore())));
+ model.setRootPath(passStore)));
ui->treeView->setColumnHidden(1, true);
ui->treeView->setColumnHidden(2, true);
ui->treeView->setColumnHidden(3, true);
@@ -883,35 +883,16 @@ QModelIndex MainWindow::firstFile(QModelIndex parentIndex) {
}
/**
- * @brief MainWindow::setPassword open passworddialog and save file (if not
- * canceled)
+ * @brief MainWindow::setPassword open passworddialog
* @param file which pgp file
- * @param overwrite update file (not insert)
* @param isNew insert (not update)
*/
void MainWindow::setPassword(QString file, bool isNew) {
- PasswordDialog d(QtPassSettings::getPasswordConfiguration(), this);
+ PasswordDialog d(QtPassSettings::getPasswordConfiguration(), file, isNew, this);
connect(QtPassSettings::getPass(), &Pass::finishedShow, &d,
&PasswordDialog::setPass);
- // TODO(bezet): add error handling
- QtPassSettings::getPass()->Show(file);
- d.setFile(file);
- d.usePwgen(QtPassSettings::isUsePwgen());
- d.setTemplate(QtPassSettings::getPassTemplate(),
- QtPassSettings::isUseTemplate());
- d.templateAll(QtPassSettings::isTemplateAllFields());
- if (!d.exec()) {
- d.setPassword(QString());
- return;
- }
- QString newValue = d.getPassword();
- if (newValue.isEmpty())
- return;
-
- if (newValue.right(1) != "\n")
- newValue += "\n";
- QtPassSettings::getPass()->Insert(file, newValue, !isNew);
+ d.exec();
}
/**
@@ -1124,17 +1105,15 @@ void MainWindow::generateKeyPair(QString batch, QDialog *keygenWindow) {
* select a more appropriate one to view too
*/
void MainWindow::updateProfileBox() {
- // dbg()<< profiles.size();
- if (QtPassSettings::getProfiles().isEmpty()) {
+ QHash<QString, QString> profiles = QtPassSettings::getProfiles();
+
+ if (profiles.isEmpty()) {
ui->profileBox->hide();
} else {
ui->profileBox->show();
- if (QtPassSettings::getProfiles().size() < 2)
- ui->profileBox->setEnabled(false);
- else
- ui->profileBox->setEnabled(true);
+ ui->profileBox->setEnabled(profiles.size() > 1);
ui->profileBox->clear();
- QHashIterator<QString, QString> i(QtPassSettings::getProfiles());
+ QHashIterator<QString, QString> i(profiles);
while (i.hasNext()) {
i.next();
if (!i.key().isEmpty())
diff --git a/src/passworddialog.cpp b/src/passworddialog.cpp
index b615225d..e5c440a1 100644
--- a/src/passworddialog.cpp
+++ b/src/passworddialog.cpp
@@ -13,14 +13,44 @@
* @param passConfig configuration constant
* @param parent
*/
+PasswordDialog::PasswordDialog(const PasswordConfiguration &passConfig, QWidget *parent) : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
+ m_templating = false;
+ m_allFields = false;
+
+ ui->setupUi(this);
+ setLength(m_passConfig.length);
+ setPasswordCharTemplate(m_passConfig.selected);
+}
+
+/**
+ * @brief PasswordDialog::PasswordDialog complete constructor.
+ * @param passConfig configuration constant
+ * @param file
+ * @param isNew
+ * @param parent
+ */
PasswordDialog::PasswordDialog(const PasswordConfiguration &passConfig,
+ const QString &file,
+ const bool &isNew,
QWidget *parent)
- : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig) {
- templating = false;
- allFields = false;
- ui->setupUi(this);
- setLength(m_passConfig.length);
- setPasswordCharTemplate(m_passConfig.selected);
+ : QDialog(parent), ui(new Ui::PasswordDialog), m_passConfig(passConfig), m_file(file), m_isNew(isNew) {
+
+ QtPassSettings::getPass()->Show(m_file);
+
+ setWindowTitle(this->windowTitle() + " " + m_file);
+ usePwgen(QtPassSettings::isUsePwgen());
+ setTemplate(QtPassSettings::getPassTemplate(),
+ QtPassSettings::isUseTemplate());
+ templateAll(QtPassSettings::isTemplateAllFields());
+
+ ui->setupUi(this);
+ setLength(m_passConfig.length);
+ setPasswordCharTemplate(m_passConfig.selected);
+
+ connect(this, &PasswordDialog::accepted,
+ this, &PasswordDialog::on_accepted);
+ connect(this, &PasswordDialog::rejected,
+ this, &PasswordDialog::on_rejected);
}
/**
@@ -56,12 +86,31 @@ void PasswordDialog::on_createPasswordButton_clicked() {
}
/**
+ * @brief PasswordDialog::on_accepted handle Ok click for QDialog
+ */
+void PasswordDialog::on_accepted() {
+ QString newValue = getPassword();
+ if (newValue.isEmpty())
+ return;
+
+ if (newValue.right(1) != "\n")
+ newValue += "\n";
+
+ QtPassSettings::getPass()->Insert(m_file, newValue, !m_isNew);
+}
+
+/**
+ * @brief PasswordDialog::on_accepted handle Cancel click for QDialog
+ */
+void PasswordDialog::on_rejected() { setPassword(QString()); }
+
+/**
* @brief PasswordDialog::setPassword populate the (templated) fields.
* @param password
*/
void PasswordDialog::setPassword(QString password) {
FileContent fileContent = FileContent::parse(
- password, templating ? fields : QStringList(), allFields);
+ password, m_templating ? m_fields : QStringList(), m_allFields);
ui->lineEditPassword->setText(fileContent.getPassword());
QWidget *previous = ui->checkBoxShow;
@@ -110,13 +159,13 @@ QString PasswordDialog::getPassword() {
* @param rawFields
*/
void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
- fields = rawFields.split('\n');
- templating = useTemplate;
+ m_fields = rawFields.split('\n');
+ m_templating = useTemplate;
templateLines.clear();
- if (templating) {
+ if (m_templating) {
QWidget *previous = ui->checkBoxShow;
- foreach (QString field, fields) {
+ foreach (QString field, m_fields) {
if (field.isEmpty())
continue;
QLineEdit *line = new QLineEdit();
@@ -130,19 +179,11 @@ void PasswordDialog::setTemplate(QString rawFields, bool useTemplate) {
}
/**
- * @brief PasswordDialog::setFile show which (password) file we are editing.
- * @param file
- */
-void PasswordDialog::setFile(QString file) {
- this->setWindowTitle(this->windowTitle() + " " + file);
-}
-
-/**
* @brief PasswordDialog::templateAll basic setter for use in
* PasswordDialog::setPassword templating all tokenisable lines.
* @param templateAll
*/
-void PasswordDialog::templateAll(bool templateAll) { allFields = templateAll; }
+void PasswordDialog::templateAll(bool templateAll) { m_allFields = templateAll; }
/**
* @brief PasswordDialog::setLength
diff --git a/src/passworddialog.h b/src/passworddialog.h
index 1a0085d9..1aeeec34 100644
--- a/src/passworddialog.h
+++ b/src/passworddialog.h
@@ -23,6 +23,10 @@ class PasswordDialog : public QDialog {
public:
explicit PasswordDialog(const PasswordConfiguration &passConfig,
QWidget *parent = 0);
+ PasswordDialog(const PasswordConfiguration &passConfig,
+ const QString &file,
+ const bool &isNew,
+ QWidget *parent = 0);
~PasswordDialog();
/*! Sets content in the password field in the interface.
@@ -43,11 +47,6 @@ public:
*/
void setTemplate(QString rawFields, bool useTemplate);
- /*! Sets the file (name) in the interface.
- \param file name as a QString
- */
- void setFile(QString);
-
void templateAll(bool templateAll);
void setLength(int l);
void setPasswordCharTemplate(int t);
@@ -59,13 +58,17 @@ public slots:
private slots:
void on_checkBoxShow_stateChanged(int arg1);
void on_createPasswordButton_clicked();
+ void on_accepted();
+ void on_rejected();
private:
Ui::PasswordDialog *ui;
const PasswordConfiguration &m_passConfig;
- QStringList fields;
- bool templating;
- bool allFields;
+ QStringList m_fields;
+ QString m_file;
+ bool m_templating;
+ bool m_allFields;
+ bool m_isNew;
QList<QLineEdit *> templateLines;
QList<QLineEdit *> otherLines;
};
diff --git a/src/qtpasssettings.h b/src/qtpasssettings.h
index 55de709b..59660a8e 100644
--- a/src/qtpasssettings.h
+++ b/src/qtpasssettings.h
@@ -27,7 +27,7 @@ private:
QtPassSettings(const QString &organization, const QString &application)
: QSettings(organization, application) {}
- virtual ~QtPassSettings();
+ virtual ~QtPassSettings() {}
static bool initialized;
static QtPassSettings *m_instance;