From a6be979f8ea6393ef5c6757580257dfb9673b742 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 10 Mar 2025 19:08:02 +0100 Subject: [PATCH] Allow for saving the configuration used by a window opened from a URL. Note when editing a copy save password works now too. --- pglab/ConnectionConfigurationWidget.cpp | 12 ++++++------ pglab/ConnectionConfigurationWidget.h | 4 ++-- pglab/ConnectionController.cpp | 26 +++++++++++++++++++------ pglab/ConnectionController.h | 2 +- pglab/DatabaseWindow.cpp | 6 ++++++ pglab/DatabaseWindow.h | 1 + pglab/DatabaseWindow.ui | 8 +++++++- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/pglab/ConnectionConfigurationWidget.cpp b/pglab/ConnectionConfigurationWidget.cpp index b1b5603..52f0e2b 100644 --- a/pglab/ConnectionConfigurationWidget.cpp +++ b/pglab/ConnectionConfigurationWidget.cpp @@ -207,13 +207,13 @@ void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg) edtPassword->setText(""); cmbDbname->setCurrentText(cfg.dbname()); - //cmbbxSsl->setCurrentIndex(static_cast(cfg.sslMode())); cmbbxSsl->setCurrentText(cfg.getParameter("sslmode")); edtCert->setText(cfg.sslCert()); edtKey->setText(cfg.sslKey()); edtRootCert->setText(cfg.sslRootCert()); edtCrl->setText(cfg.sslCrl()); + edtPassword->setText(cfg.password()); encodedPassword = cfg.encodedPassword(); cbSavePassword->setCheckState( encodedPassword.isEmpty() @@ -234,7 +234,7 @@ ConnectionConfig ConnectionConfigurationWidget::data() const cfg.setUser(edtUser->text()); cfg.setPassword(edtPassword->text()); cfg.setDbname(cmbDbname->currentText()); - cfg.setSslMode(static_cast(cmbbxSsl->currentIndex())); + cfg.setParameter("sslmode", cmbbxSsl->currentText()); cfg.setSslCert(edtCert->text()); cfg.setSslKey(edtKey->text()); cfg.setSslRootCert(edtRootCert->text()); @@ -248,14 +248,14 @@ QString ConnectionConfigurationWidget::group() const return cmbbxGroup->currentText(); } -bool ConnectionConfigurationWidget::savePassword() const +bool ConnectionConfigurationWidget::savePasswordEnabled() const { - return cbSavePassword->isChecked() && !edtPassword->text().isEmpty() && passwordChanged; + return cbSavePassword->isChecked(); } -bool ConnectionConfigurationWidget::clearPassword() const +bool ConnectionConfigurationWidget::passwordIsChanged() const { - return !cbSavePassword->isChecked(); + return passwordChanged; } void ConnectionConfigurationWidget::testConnection() diff --git a/pglab/ConnectionConfigurationWidget.h b/pglab/ConnectionConfigurationWidget.h index 2f38102..5d14354 100644 --- a/pglab/ConnectionConfigurationWidget.h +++ b/pglab/ConnectionConfigurationWidget.h @@ -35,8 +35,8 @@ public: ConnectionConfig data() const; QString group() const; - bool savePassword() const; - bool clearPassword() const; + bool savePasswordEnabled() const; + bool passwordIsChanged() const; public slots: void testConnection(); diff --git a/pglab/ConnectionController.cpp b/pglab/ConnectionController.cpp index 642441d..75f7d2f 100644 --- a/pglab/ConnectionController.cpp +++ b/pglab/ConnectionController.cpp @@ -84,10 +84,14 @@ void ConnectionController::openBackupDlgForConnection(QModelIndex index) } } -void ConnectionController::createConnection() +void ConnectionController::createConnection(ConnectionConfig *init) { ConnectionConfig cc; - cc.setUuid(QUuid::createUuid()); + if (init) + { + cc = *init; + cc.setUuid(QUuid()); + } editConfig(cc); } @@ -104,7 +108,7 @@ void ConnectionController::editCopy(QModelIndex index) auto config = ConnectionTreeModel::getConfigFromModelIndex(index); if (config) { auto cc = *config; - cc.setUuid(QUuid::createUuid()); + cc.setUuid(QUuid()); cc.setEncodedPassword({}); // maybe we should decode en reencode? editConfig(cc); } @@ -120,10 +124,20 @@ void ConnectionController::saveConnection(ConnectionConfigurationWidget &w) { auto cc = w.data(); auto grp = w.group(); - if (w.savePassword()) - encryptPassword(cc); - if (w.clearPassword()) + bool isNew = cc.uuid().isNull(); + if (isNew) + { + cc.setUuid(QUuid::createUuid()); + } + if (w.savePasswordEnabled()) + { + if (isNew || w.passwordIsChanged()) + encryptPassword(cc); + } + else + { cc.setEncodedPassword({}); + } m_connectionTreeModel->save(grp, cc); } diff --git a/pglab/ConnectionController.h b/pglab/ConnectionController.h index 0a71bd7..5a1d221 100644 --- a/pglab/ConnectionController.h +++ b/pglab/ConnectionController.h @@ -43,7 +43,7 @@ public: /// Starts the form for creating a new conncetion. /// This function returns immidiatly! - void createConnection(); + void createConnection(ConnectionConfig *init = nullptr); /// Starts the form for editing a conncetion. /// This function returns immidiatly! void editConnection(QModelIndex index); diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index e1c1a3c..9d1fee8 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -443,6 +443,12 @@ void DatabaseWindow::on_actionShow_connection_manager_triggered() m_masterController->connectionController()->showConnectionManager(); } +void DatabaseWindow::on_actionSave_connection_triggered() +{ + if (m_config.uuid().isNull()) + m_masterController->connectionController()->createConnection(&m_config); +} + void DatabaseWindow::on_actionManual_triggered() { OpenManual(); diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index ab259ae..b86d917 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -105,6 +105,7 @@ private slots: void on_actionSave_query_as_triggered(); void on_actionSave_copy_of_query_as_triggered(); void on_actionShow_connection_manager_triggered(); + void on_actionSave_connection_triggered(); void on_actionManual_triggered(); diff --git a/pglab/DatabaseWindow.ui b/pglab/DatabaseWindow.ui index 8000735..0db9810 100644 --- a/pglab/DatabaseWindow.ui +++ b/pglab/DatabaseWindow.ui @@ -30,7 +30,7 @@ 0 0 1260 - 29 + 21 @@ -44,6 +44,7 @@ + @@ -331,6 +332,11 @@ Manual + + + Save connection + +