diff --git a/pglab/ConnectionController.cpp b/pglab/ConnectionController.cpp index 92faa22..78f5027 100644 --- a/pglab/ConnectionController.cpp +++ b/pglab/ConnectionController.cpp @@ -10,12 +10,21 @@ #include "ConnectionConfigurationWidget.h" #include #include +#include ConnectionController::ConnectionController(MasterController *parent) : QObject(parent) , m_masterController(parent) -{} + , m_relockTimer(new QTimer(this)) +{ + m_relockTimer->setSingleShot(true); + m_relockTimer->setTimerType(Qt::VeryCoarseTimer); + // Force signal to go through queue so when the password manager is relocked after 0msec + // the code that retrieves the password is garanteed to run before the signal is handled + // because only after the password is retrieved the loop has a chance to run. + m_relockTimer->callOnTimeout(this, &ConnectionController::relock, Qt::QueuedConnection); +} ConnectionController::~ConnectionController() { @@ -194,7 +203,7 @@ bool ConnectionController::UnlockPasswordManagerIfNeeded() while (true) { // ask user for passphrase - auto dlg = std::make_unique(nullptr, nullptr); + auto dlg = std::make_unique(PasswordPromptDialog::RememberPassword, nullptr); dlg->setCaption(tr("Unlock password manager")); dlg->setDescription(tr("Enter password for password manager")); int exec_result = dlg->exec(); @@ -205,8 +214,14 @@ bool ConnectionController::UnlockPasswordManagerIfNeeded() break; } // user gave OK, if succeeds return true otherwise loop a prompt for password again. - if (m_passwordManager->openDatabase(user_cfg_db, dlg->password())) + if (m_passwordManager->openDatabase(user_cfg_db, dlg->password())) { + int rem = dlg->remember(); + if (rem >= 0) { + int timeout = rem * 60 * 1000; /// rem is in minutes, timeout in millisec + m_relockTimer->start(timeout); + } return true; + } } } else { @@ -234,5 +249,10 @@ std::string ConnectionController::getPskId(const ConnectionConfig &cc) return id; } +void ConnectionController::relock() +{ + m_passwordManager->closeDatabase(); +} + diff --git a/pglab/ConnectionController.h b/pglab/ConnectionController.h index 2f942d4..4ef8491 100644 --- a/pglab/ConnectionController.h +++ b/pglab/ConnectionController.h @@ -9,6 +9,7 @@ class ConnectionList; class ConnectionTreeModel; class ConnectionManagerWindow; class PasswordManager; +class QTimer; class ConnectionController : public QObject { Q_OBJECT @@ -43,6 +44,7 @@ private: ConnectionList *m_connectionList = nullptr; ConnectionTreeModel *m_connectionTreeModel = nullptr; ConnectionManagerWindow *m_connectionManagerWindow = nullptr; + QTimer *m_relockTimer = nullptr; /** Using long lived object so it can remember its master password for sometime * if the user wishes it. @@ -57,9 +59,15 @@ private: bool decodePassword(const std::string &password_id, const std::string &enc_password, std::string &password); bool encodePassword(const std::string &password_id, const std::string &password, std::string &enc_password); + /// + /// + /// \return bool: succeeded, int: relock timeout + /// bool UnlockPasswordManagerIfNeeded(); static std::string getPskId(const ConnectionConfig &cc); +private slots: + void relock(); };