ConnectionManager overhaul
- connection settings are now changed by seperate component currently called in a seperate window - old settings pane on the right of the connections had been removed - new edit config button added between new connection and remove connection
This commit is contained in:
parent
78247c7abe
commit
b09e8a6d4b
20 changed files with 836 additions and 733 deletions
181
pglab/ConnectionConfigurationWidget.cpp
Normal file
181
pglab/ConnectionConfigurationWidget.cpp
Normal file
|
|
@ -0,0 +1,181 @@
|
||||||
|
#include "ConnectionConfigurationWidget.h"
|
||||||
|
#include "SslModeModel.h"
|
||||||
|
#include "ConnectionConfig.h"
|
||||||
|
#include "ConnectionController.h"
|
||||||
|
#include "ConnectionListModel.h"
|
||||||
|
#include "util.h"
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QDataWidgetMapper>
|
||||||
|
#include <QDialogButtonBox>
|
||||||
|
#include <QFormLayout>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QLineEdit>
|
||||||
|
#include <QPushButton>
|
||||||
|
#include <QSpinBox>
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QDialog>
|
||||||
|
|
||||||
|
#define SET_OBJECT_NAME(var) var->setObjectName(#var)
|
||||||
|
|
||||||
|
void ConnectionConfigurationWidget::editExistingInWindow(ConnectionController *ctrl, const ConnectionConfig &cfg)
|
||||||
|
{
|
||||||
|
auto w = new ConnectionConfigurationWidget;
|
||||||
|
w->setData(cfg);
|
||||||
|
|
||||||
|
|
||||||
|
auto btn_hbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
|
||||||
|
//auto btn_test = btn_hbox->addButton(tr("Test"), QDialogButtonBox::ActionRole);
|
||||||
|
|
||||||
|
auto vbox = new QVBoxLayout;
|
||||||
|
vbox->addWidget(w);
|
||||||
|
vbox->addWidget(btn_hbox);
|
||||||
|
|
||||||
|
auto win = new QDialog;
|
||||||
|
win->setWindowTitle(tr("Edit connection configuration"));
|
||||||
|
win->setLayout(vbox);
|
||||||
|
|
||||||
|
win->connect(btn_hbox, &QDialogButtonBox::accepted, [ctrl, w, win] () {
|
||||||
|
auto cc = w->data();
|
||||||
|
ctrl->getConnectionListModel()->save(cc);
|
||||||
|
win->accept();
|
||||||
|
});
|
||||||
|
win->connect(btn_hbox, &QDialogButtonBox::rejected, [win] () { win->reject(); });
|
||||||
|
|
||||||
|
win->connect(win, &QDialog::finished, [win] (int)
|
||||||
|
{
|
||||||
|
delete win;
|
||||||
|
});
|
||||||
|
|
||||||
|
win->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnectionConfigurationWidget::ConnectionConfigurationWidget(QWidget *parent)
|
||||||
|
: QWidget(parent)
|
||||||
|
{
|
||||||
|
lblName = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblName);
|
||||||
|
edtName = new QLineEdit ;
|
||||||
|
SET_OBJECT_NAME(edtName);
|
||||||
|
lblName->setBuddy(edtName);
|
||||||
|
|
||||||
|
lblHost = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblHost);
|
||||||
|
edtHost = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtHost);
|
||||||
|
lblHost->setBuddy(edtHost);
|
||||||
|
|
||||||
|
lblPort = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblPort);
|
||||||
|
spinPort = new QSpinBox;
|
||||||
|
SET_OBJECT_NAME(spinPort);
|
||||||
|
spinPort->setRange(1, std::numeric_limits<uint16_t>::max());
|
||||||
|
lblPort->setBuddy(spinPort);
|
||||||
|
|
||||||
|
lblUser = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblUser);
|
||||||
|
edtUser = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtUser);
|
||||||
|
lblUser->setBuddy(edtUser);
|
||||||
|
|
||||||
|
lblDbName = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblDbName);
|
||||||
|
edtDbname = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtDbname);
|
||||||
|
lblDbName->setBuddy(edtDbname);
|
||||||
|
|
||||||
|
lblSsl = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblSsl);
|
||||||
|
cmbbxSsl = new QComboBox;
|
||||||
|
SET_OBJECT_NAME(cmbbxSsl);
|
||||||
|
cmbbxSsl->setModelColumn(0);
|
||||||
|
auto ssl_model = new SslModeModel(this);
|
||||||
|
cmbbxSsl->setModel(ssl_model);
|
||||||
|
lblSsl->setBuddy(cmbbxSsl);
|
||||||
|
|
||||||
|
lblCert = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblCert);
|
||||||
|
edtCert = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtCert);
|
||||||
|
lblCert->setBuddy(edtCert);
|
||||||
|
|
||||||
|
lblKey = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblKey);
|
||||||
|
edtKey = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtKey);
|
||||||
|
lblKey->setBuddy(edtKey);
|
||||||
|
|
||||||
|
lblRootCert = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblRootCert);
|
||||||
|
edtRootCert = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtRootCert);
|
||||||
|
lblRootCert->setBuddy(edtRootCert);
|
||||||
|
|
||||||
|
lblCrl = new QLabel;
|
||||||
|
SET_OBJECT_NAME(lblCrl);
|
||||||
|
edtCrl = new QLineEdit;
|
||||||
|
SET_OBJECT_NAME(edtCrl);
|
||||||
|
lblCrl->setBuddy(edtCrl);
|
||||||
|
|
||||||
|
formLayout = new QFormLayout;
|
||||||
|
|
||||||
|
setLayout(formLayout);
|
||||||
|
formLayout->addRow(lblName, edtName);
|
||||||
|
formLayout->addRow(lblHost, edtHost);
|
||||||
|
formLayout->addRow(lblPort, spinPort);
|
||||||
|
formLayout->addRow(lblUser, edtUser);
|
||||||
|
formLayout->addRow(lblDbName, edtDbname);
|
||||||
|
formLayout->addRow(lblSsl, cmbbxSsl);
|
||||||
|
formLayout->addRow(lblCert, edtCert);
|
||||||
|
formLayout->addRow(lblKey, edtKey);
|
||||||
|
formLayout->addRow(lblRootCert, edtRootCert);
|
||||||
|
formLayout->addRow(lblCrl, edtCrl);
|
||||||
|
|
||||||
|
retranslateUi();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionConfigurationWidget::retranslateUi()
|
||||||
|
{
|
||||||
|
lblName->setText(QApplication::translate("ConnectionConfigurationWidget", "&Name", nullptr));
|
||||||
|
lblHost->setText(QApplication::translate("ConnectionConfigurationWidget", "&Host", nullptr));
|
||||||
|
lblPort->setText(QApplication::translate("ConnectionConfigurationWidget", "&Port", nullptr));
|
||||||
|
lblUser->setText(QApplication::translate("ConnectionConfigurationWidget", "&Username", nullptr));
|
||||||
|
lblDbName->setText(QApplication::translate("ConnectionConfigurationWidget", "&Database", nullptr));
|
||||||
|
lblSsl->setText(QApplication::translate("ConnectionConfigurationWidget", "&SSL mode", nullptr));
|
||||||
|
lblCert->setText(QApplication::translate("ConnectionConfigurationWidget", "&Certificate", nullptr));
|
||||||
|
lblKey->setText(QApplication::translate("ConnectionConfigurationWidget", "&Key", nullptr));
|
||||||
|
lblRootCert->setText(QApplication::translate("ConnectionConfigurationWidget", "&Root cert.", nullptr));
|
||||||
|
lblCrl->setText(QApplication::translate("ConnectionConfigurationWidget", "Revocation &list", nullptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg)
|
||||||
|
{
|
||||||
|
m_uuid = cfg.uuid();
|
||||||
|
edtName->setText(stdStrToQ(cfg.name()));
|
||||||
|
edtHost->setText(stdStrToQ(cfg.host()));
|
||||||
|
spinPort->setValue(cfg.port());
|
||||||
|
edtUser->setText(stdStrToQ(cfg.user()));
|
||||||
|
edtDbname->setText(stdStrToQ(cfg.dbname()));
|
||||||
|
cmbbxSsl->setCurrentIndex(static_cast<int>(cfg.sslMode()));
|
||||||
|
edtCert->setText(stdStrToQ(cfg.sslCert()));
|
||||||
|
edtKey->setText(stdStrToQ(cfg.sslKey()));
|
||||||
|
edtRootCert->setText(stdStrToQ(cfg.sslRootCert()));
|
||||||
|
edtCrl->setText(stdStrToQ(cfg.sslCrl()));
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnectionConfig ConnectionConfigurationWidget::data() const
|
||||||
|
{
|
||||||
|
ConnectionConfig cfg;
|
||||||
|
cfg.setUuid(m_uuid);
|
||||||
|
cfg.setName(qStrToStd(edtName->text()));
|
||||||
|
cfg.setHost(qStrToStd(edtHost->text()));
|
||||||
|
cfg.setPort(static_cast<uint16_t>(spinPort->value()));
|
||||||
|
cfg.setUser(qStrToStd(edtUser->text()));
|
||||||
|
cfg.setDbname(qStrToStd(edtDbname->text()));
|
||||||
|
cfg.setSslMode(static_cast<SslMode>(cmbbxSsl->currentIndex()));
|
||||||
|
cfg.setSslCert(qStrToStd(edtCert->text()));
|
||||||
|
cfg.setSslKey(qStrToStd(edtKey->text()));
|
||||||
|
cfg.setSslRootCert(qStrToStd(edtRootCert->text()));
|
||||||
|
cfg.setSslCrl(qStrToStd(edtCrl->text()));
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
60
pglab/ConnectionConfigurationWidget.h
Normal file
60
pglab/ConnectionConfigurationWidget.h
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
#ifndef CONNECTIONCONFIGURATIONWIDGET_H
|
||||||
|
#define CONNECTIONCONFIGURATIONWIDGET_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QUuid>
|
||||||
|
|
||||||
|
class ConnectionController;
|
||||||
|
class ConnectionConfig;
|
||||||
|
class QFormLayout;
|
||||||
|
class QLabel;
|
||||||
|
class QLineEdit;
|
||||||
|
class QSpinBox;
|
||||||
|
class QComboBox;
|
||||||
|
class QDataWidgetMapper;
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectionConfigurationWidget : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static void editExistingInWindow(ConnectionController *ctrl, const ConnectionConfig &cfg);
|
||||||
|
|
||||||
|
explicit ConnectionConfigurationWidget(QWidget *parent = nullptr);
|
||||||
|
|
||||||
|
void retranslateUi();
|
||||||
|
void setData(const ConnectionConfig &cfg);
|
||||||
|
ConnectionConfig data() const;
|
||||||
|
signals:
|
||||||
|
|
||||||
|
private:
|
||||||
|
QUuid m_uuid;
|
||||||
|
|
||||||
|
QFormLayout *formLayout;
|
||||||
|
QLabel *lblName;
|
||||||
|
QLineEdit *edtName;
|
||||||
|
QLabel *lblHost;
|
||||||
|
QLineEdit *edtHost;
|
||||||
|
QLabel *lblPort;
|
||||||
|
QSpinBox *spinPort;
|
||||||
|
QLabel *lblUser;
|
||||||
|
QLineEdit *edtUser;
|
||||||
|
QLabel *lblDbName;
|
||||||
|
QLineEdit *edtDbname;
|
||||||
|
QLabel *lblSsl;
|
||||||
|
QComboBox *cmbbxSsl;
|
||||||
|
QLabel *lblCert;
|
||||||
|
QLineEdit *edtCert;
|
||||||
|
QLabel *lblKey;
|
||||||
|
QLineEdit *edtKey;
|
||||||
|
QLabel *lblRootCert;
|
||||||
|
QLineEdit *edtRootCert;
|
||||||
|
QLabel *lblCrl;
|
||||||
|
QLineEdit *edtCrl;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CONNECTIONCONFIGURATIONWIDGET_H
|
||||||
216
pglab/ConnectionController.cpp
Normal file
216
pglab/ConnectionController.cpp
Normal file
|
|
@ -0,0 +1,216 @@
|
||||||
|
#include "ConnectionController.h"
|
||||||
|
#include "MasterController.h"
|
||||||
|
#include "ConnectionManagerWindow.h"
|
||||||
|
#include "ConnectionListModel.h"
|
||||||
|
#include "PasswordManager.h"
|
||||||
|
#include "DatabaseWindow.h"
|
||||||
|
#include "ServerWindow.h"
|
||||||
|
#include "BackupDialog.h"
|
||||||
|
#include "PasswordPromptDialog.h"
|
||||||
|
#include "ConnectionConfigurationWidget.h"
|
||||||
|
|
||||||
|
|
||||||
|
ConnectionController::ConnectionController(MasterController *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_masterController(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
|
ConnectionController::~ConnectionController()
|
||||||
|
{
|
||||||
|
delete m_connectionManagerWindow;
|
||||||
|
delete m_connectionListModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::init()
|
||||||
|
{
|
||||||
|
//std::string dbfilename = QDir::toNativeSeparators(GetUserConfigDatabaseName()).toUtf8().data();
|
||||||
|
//m_userConfigDatabase = std::make_shared<Botan::Sqlite3_Database>(dbfilename);
|
||||||
|
|
||||||
|
|
||||||
|
m_passwordManager = std::make_shared<PasswordManager>();
|
||||||
|
|
||||||
|
m_connectionListModel = new ConnectionListModel(this);
|
||||||
|
m_connectionListModel->load();
|
||||||
|
|
||||||
|
m_connectionManagerWindow = new ConnectionManagerWindow(m_masterController, nullptr);
|
||||||
|
m_connectionManagerWindow->show();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::showConnectionManager()
|
||||||
|
{
|
||||||
|
m_connectionManagerWindow->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::openSqlWindowForConnection(int connection_index)
|
||||||
|
{
|
||||||
|
|
||||||
|
auto res = m_connectionListModel->get(connection_index);
|
||||||
|
if (res.valid()) {
|
||||||
|
auto cc = res.get();
|
||||||
|
|
||||||
|
if (retrieveConnectionPassword(cc)) {
|
||||||
|
m_connectionListModel->save(cc);
|
||||||
|
// TODO instead of directly openening the mainwindow
|
||||||
|
// do async connect and only open window when we have
|
||||||
|
// working connection
|
||||||
|
auto w = new DatabaseWindow(m_masterController, nullptr);
|
||||||
|
w->setAttribute( Qt::WA_DeleteOnClose );
|
||||||
|
w->setConfig(cc);
|
||||||
|
w->showMaximized();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::openBackupDlgForConnection(int connection_index)
|
||||||
|
{
|
||||||
|
auto res = m_connectionListModel->get(connection_index);
|
||||||
|
if (res.valid()) {
|
||||||
|
auto cc = res.get();
|
||||||
|
if (retrieveConnectionPassword(cc)) {
|
||||||
|
m_connectionListModel->save(cc);
|
||||||
|
auto w = new BackupDialog(nullptr); //new ServerWindow(this, nullptr);
|
||||||
|
w->setAttribute( Qt::WA_DeleteOnClose );
|
||||||
|
w->setConfig(cc);
|
||||||
|
w->show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::createConnection()
|
||||||
|
{
|
||||||
|
ConnectionConfig cc;
|
||||||
|
cc.setUuid(QUuid::createUuid());
|
||||||
|
ConnectionConfigurationWidget::editExistingInWindow(this, cc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::editConnection(int connection_index)
|
||||||
|
{
|
||||||
|
auto res = m_connectionListModel->get(connection_index);
|
||||||
|
if (res.valid()) {
|
||||||
|
auto cc = res.get();
|
||||||
|
ConnectionConfigurationWidget::editExistingInWindow(this, cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionController::openServerWindowForConnection(int connection_index)
|
||||||
|
{
|
||||||
|
auto res = m_connectionListModel->get(connection_index);
|
||||||
|
if (res.valid()) {
|
||||||
|
auto cc = res.get();
|
||||||
|
if (retrieveConnectionPassword(cc)) {
|
||||||
|
m_connectionListModel->save(cc);
|
||||||
|
auto w = new ServerWindow(m_masterController, nullptr);
|
||||||
|
w->setAttribute( Qt::WA_DeleteOnClose );
|
||||||
|
w->setConfig(cc);
|
||||||
|
w->show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
||||||
|
{
|
||||||
|
auto pw_state = cc.passwordState();
|
||||||
|
if (pw_state == PasswordState::NotNeeded) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (pw_state == PasswordState::SavedPasswordManager) {
|
||||||
|
std::string pw;
|
||||||
|
bool result = getPasswordFromPskdb(getPskId(cc), pw);
|
||||||
|
if (result) {
|
||||||
|
cc.setPassword(pw);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Geen else hier want als voorgaande blok niet geretourneerd heeft moeten we wachtwoord
|
||||||
|
// ook aan de gebruiker vragen zoals hier gebeurd.
|
||||||
|
QString str = ConnectionListModel::makeLongDescription(cc);
|
||||||
|
auto dlg = std::make_unique<PasswordPromptDialog>(PasswordPromptDialog::SaveOption, nullptr);
|
||||||
|
dlg->setCaption(tr("Connection password prompt"));
|
||||||
|
dlg->setDescription(QString(tr("Please provide password for connection %1")).arg(str));
|
||||||
|
int exec_result = dlg->exec();
|
||||||
|
|
||||||
|
if (exec_result == QDialog::Accepted) {
|
||||||
|
std::string password = dlg->password().toUtf8().data();
|
||||||
|
cc.setPassword(password);
|
||||||
|
if (dlg->saveChecked()) {
|
||||||
|
storePasswordInPskdb(getPskId(cc), password);
|
||||||
|
cc.setPasswordState(PasswordState::SavedPasswordManager);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ConnectionController::getPasswordFromPskdb(const std::string &password_id, std::string &password)
|
||||||
|
{
|
||||||
|
if (!UnlockPasswordManagerIfNeeded())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return m_passwordManager->get(password_id, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool ConnectionController::storePasswordInPskdb(const std::string &password_id, const std::string password)
|
||||||
|
{
|
||||||
|
if (!UnlockPasswordManagerIfNeeded())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_passwordManager->set(password_id, password);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConnectionController::UnlockPasswordManagerIfNeeded()
|
||||||
|
{
|
||||||
|
auto&& user_cfg_db = m_masterController->userConfigDatabase();
|
||||||
|
if (m_passwordManager->initialized(user_cfg_db)) {
|
||||||
|
if (!m_passwordManager->locked())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// ask user for passphrase
|
||||||
|
auto dlg = std::make_unique<PasswordPromptDialog>(nullptr, nullptr);
|
||||||
|
dlg->setCaption(tr("Unlock password manager"));
|
||||||
|
dlg->setDescription(tr("Enter password for password manager"));
|
||||||
|
int exec_result = dlg->exec();
|
||||||
|
bool ok = (exec_result == QDialog::Accepted);
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
// leave this retry loop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// user gave OK, if succeeds return true otherwise loop a prompt for password again.
|
||||||
|
if (m_passwordManager->openDatabase(user_cfg_db, dlg->password()))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Ask user for passphrase + confirmation, clearly instruct this is first setup
|
||||||
|
// create
|
||||||
|
auto dlg = std::make_unique<PasswordPromptDialog>(PasswordPromptDialog::ConfirmPassword, nullptr);
|
||||||
|
dlg->setCaption(tr("Password manager setup"));
|
||||||
|
dlg->setDescription(tr("Enter a strong password for password manager initialization. A strong key will be "
|
||||||
|
"derived from your password and it will be impossible to recover anything from the "
|
||||||
|
"password manager without the password you enter here."));
|
||||||
|
int exec_result = dlg->exec();
|
||||||
|
if (exec_result == QDialog::Accepted) {
|
||||||
|
QString passphrase = dlg->password();
|
||||||
|
if (m_passwordManager->createDatabase(user_cfg_db, passphrase))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ConnectionController::getPskId(const ConnectionConfig &cc)
|
||||||
|
{
|
||||||
|
std::string id = "dbpw/";
|
||||||
|
id += cc.uuid().toString().toUtf8().data();
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
63
pglab/ConnectionController.h
Normal file
63
pglab/ConnectionController.h
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#ifndef CONNECTIONCONTROLLER_H
|
||||||
|
#define CONNECTIONCONTROLLER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class MasterController;
|
||||||
|
class ConnectionConfig;
|
||||||
|
class ConnectionList;
|
||||||
|
class ConnectionListModel;
|
||||||
|
class ConnectionManagerWindow;
|
||||||
|
class PasswordManager;
|
||||||
|
|
||||||
|
class ConnectionController : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit ConnectionController(MasterController *parent = nullptr);
|
||||||
|
~ConnectionController();
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
ConnectionListModel *getConnectionListModel()
|
||||||
|
{
|
||||||
|
return m_connectionListModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
void showConnectionManager();
|
||||||
|
void openSqlWindowForConnection(int connection_index);
|
||||||
|
void openServerWindowForConnection(int connection_index);
|
||||||
|
void openBackupDlgForConnection(int connection_index);
|
||||||
|
|
||||||
|
/// Starts the form for creating a new conncetion.
|
||||||
|
/// This function returns immidiatly!
|
||||||
|
void createConnection();
|
||||||
|
/// Starts the form for editing a conncetion.
|
||||||
|
/// This function returns immidiatly!
|
||||||
|
void editConnection(int connection_index);
|
||||||
|
private:
|
||||||
|
MasterController *m_masterController;
|
||||||
|
ConnectionList *m_connectionList = nullptr;
|
||||||
|
ConnectionListModel *m_connectionListModel = nullptr;
|
||||||
|
ConnectionManagerWindow *m_connectionManagerWindow = nullptr;
|
||||||
|
|
||||||
|
/** Using long lived object so it can remember its master password for sometime
|
||||||
|
* if the user wishes it.
|
||||||
|
*/
|
||||||
|
std::shared_ptr<PasswordManager> m_passwordManager;
|
||||||
|
|
||||||
|
/** Retrieves the connection password from the user (directly or through the psk db)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool retrieveConnectionPassword(ConnectionConfig &cc);
|
||||||
|
|
||||||
|
bool getPasswordFromPskdb(const std::string &password_id, std::string &password);
|
||||||
|
|
||||||
|
bool storePasswordInPskdb(const std::string &password_id, const std::string password);
|
||||||
|
|
||||||
|
bool UnlockPasswordManagerIfNeeded();
|
||||||
|
|
||||||
|
static std::string getPskId(const ConnectionConfig &cc);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CONNECTIONCONTROLLER_H
|
||||||
|
|
@ -1,163 +0,0 @@
|
||||||
#include "ConnectionList.h"
|
|
||||||
#include "ScopeGuard.h"
|
|
||||||
#include "util.h"
|
|
||||||
#include "PasswordManager.h"
|
|
||||||
#include <QDir>
|
|
||||||
#include <QStandardPaths>
|
|
||||||
#include <QSettings>
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
/** Saves a connection configuration.
|
|
||||||
|
|
||||||
Before calling this you may want to call beginGroup.
|
|
||||||
*/
|
|
||||||
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
|
||||||
{
|
|
||||||
settings.setValue("name", stdStrToQ(cc.name()));
|
|
||||||
settings.setValue("host", stdStrToQ(cc.host()));
|
|
||||||
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
|
||||||
settings.setValue("port", cc.port());
|
|
||||||
settings.setValue("user", stdStrToQ(cc.user()));
|
|
||||||
//settings.setValue("password", stdStrToQ(cc.password()));
|
|
||||||
settings.setValue("dbname", stdStrToQ(cc.dbname()));
|
|
||||||
settings.setValue("sslmode", static_cast<int>(cc.sslMode()));
|
|
||||||
settings.setValue("sslcert", stdStrToQ(cc.sslCert()));
|
|
||||||
settings.setValue("sslkey", stdStrToQ(cc.sslKey()));
|
|
||||||
settings.setValue("sslrootcert", stdStrToQ(cc.sslRootCert()));
|
|
||||||
settings.setValue("sslcrl", stdStrToQ(cc.sslCrl()));
|
|
||||||
settings.setValue("passwordState", static_cast<int>(cc.passwordState()));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename S, typename T>
|
|
||||||
bool in_range(T value)
|
|
||||||
{
|
|
||||||
return value >= std::numeric_limits<S>::min() && value <= std::numeric_limits<S>::max();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
|
||||||
{
|
|
||||||
cc.setName(qvarToStdStr(settings.value("name")));
|
|
||||||
cc.setHost(qvarToStdStr(settings.value("host")));
|
|
||||||
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
|
||||||
int p = settings.value("port", 5432).toInt();
|
|
||||||
if (!in_range<uint16_t>(p)) {
|
|
||||||
p = 0; // let the user re-enter a valid value
|
|
||||||
}
|
|
||||||
|
|
||||||
cc.setPort(static_cast<uint16_t>(p));
|
|
||||||
cc.setUser(qvarToStdStr(settings.value("user")));
|
|
||||||
|
|
||||||
//cc.setPassword(qvarToStdStr(settings.value("password")));
|
|
||||||
|
|
||||||
cc.setDbname(qvarToStdStr(settings.value("dbname")));
|
|
||||||
cc.setSslMode(static_cast<SslMode>(settings.value("sslmode").toInt()));
|
|
||||||
cc.setSslCert(qvarToStdStr(settings.value("sslcert")));
|
|
||||||
cc.setSslKey(qvarToStdStr(settings.value("sslkey")));
|
|
||||||
cc.setSslRootCert(qvarToStdStr(settings.value("sslrootcert")));
|
|
||||||
cc.setSslCrl(qvarToStdStr(settings.value("sslcrl")));
|
|
||||||
|
|
||||||
PasswordState pwstate;
|
|
||||||
QVariant v = settings.value("passwordState");
|
|
||||||
if (v.isNull()) pwstate = PasswordState::NotStored;
|
|
||||||
else pwstate = static_cast<PasswordState>(v.toInt());
|
|
||||||
cc.setPasswordState(pwstate);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // end of unnamed namespace
|
|
||||||
|
|
||||||
/// \todo should return an expected as creation of the folder can fail
|
|
||||||
QString ConnectionList::iniFileName()
|
|
||||||
{
|
|
||||||
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
||||||
QDir dir(path);
|
|
||||||
if (!dir.exists()) {
|
|
||||||
dir.mkpath(".");
|
|
||||||
}
|
|
||||||
path += "/connections.ini";
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ConnectionList::ConnectionList() = default;
|
|
||||||
|
|
||||||
size_t ConnectionList::createNew()
|
|
||||||
{
|
|
||||||
ConnectionConfig cc;
|
|
||||||
cc.setUuid(QUuid::createUuid());
|
|
||||||
m_connections.push_back(cc);
|
|
||||||
return m_connections.size()-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionList::remove(size_t idx, size_t count)
|
|
||||||
{
|
|
||||||
auto f = m_connections.begin() + static_cast<int>(idx);
|
|
||||||
auto l = f + static_cast<int>(count);
|
|
||||||
deleteFromIni(f, l);
|
|
||||||
m_connections.erase(f, l);
|
|
||||||
|
|
||||||
// remove from password save
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionList::deleteFromIni(const t_Connections::iterator &begin, const t_Connections::iterator &end)
|
|
||||||
{
|
|
||||||
QString file_name = iniFileName();
|
|
||||||
QSettings settings(file_name, QSettings::IniFormat);
|
|
||||||
for (auto i = begin; i != end; ++i) {
|
|
||||||
settings.remove(i->uuid().toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionList::load()
|
|
||||||
{
|
|
||||||
QString file_name = iniFileName();
|
|
||||||
QSettings settings(file_name, QSettings::IniFormat);
|
|
||||||
auto groups = settings.childGroups();
|
|
||||||
for (auto&& grp : groups) {
|
|
||||||
if (grp == "c_IniGroupSecurity") {
|
|
||||||
// Read security settings
|
|
||||||
|
|
||||||
} else {
|
|
||||||
QUuid uuid(grp);
|
|
||||||
if ( ! uuid.isNull() ) {
|
|
||||||
settings.beginGroup(grp);
|
|
||||||
SCOPE_EXIT { settings.endGroup(); };
|
|
||||||
|
|
||||||
ConnectionConfig cc;
|
|
||||||
cc.setUuid(uuid);
|
|
||||||
LoadConnectionConfig(settings, cc);
|
|
||||||
m_connections.push_back(cc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionList::save()
|
|
||||||
{
|
|
||||||
QString file_name = iniFileName();
|
|
||||||
QSettings settings(file_name, QSettings::IniFormat);
|
|
||||||
for (auto& e : m_connections) {
|
|
||||||
settings.beginGroup(e.uuid().toString());
|
|
||||||
SCOPE_EXIT { settings.endGroup(); };
|
|
||||||
|
|
||||||
SaveConnectionConfig(settings, e);
|
|
||||||
e.clean();
|
|
||||||
}
|
|
||||||
settings.sync();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionList::save(size_t index)
|
|
||||||
{
|
|
||||||
auto& e = m_connections.at(index);
|
|
||||||
if (e.dirty()) {
|
|
||||||
QString file_name = iniFileName();
|
|
||||||
QSettings settings(file_name, QSettings::IniFormat);
|
|
||||||
settings.beginGroup(e.uuid().toString());
|
|
||||||
SaveConnectionConfig(settings, e);
|
|
||||||
e.clean();
|
|
||||||
settings.sync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,54 +0,0 @@
|
||||||
#ifndef CONNECTIONLIST_H
|
|
||||||
#define CONNECTIONLIST_H
|
|
||||||
|
|
||||||
#include "ConnectionConfig.h"
|
|
||||||
|
|
||||||
#include <QString>
|
|
||||||
#include <QUuid>
|
|
||||||
#include <vector>
|
|
||||||
#include "Expected.h"
|
|
||||||
|
|
||||||
class ConnectionList {
|
|
||||||
public:
|
|
||||||
|
|
||||||
ConnectionList();
|
|
||||||
size_t size() const { return m_connections.size(); }
|
|
||||||
|
|
||||||
ConnectionConfig& getConfigByIdx(size_t idx)
|
|
||||||
{
|
|
||||||
return m_connections.at(idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setConfigByIdx(size_t idx, const ConnectionConfig &cc)
|
|
||||||
{
|
|
||||||
m_connections[idx] = cc;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t createNew();
|
|
||||||
|
|
||||||
void remove(size_t idx, size_t count);
|
|
||||||
|
|
||||||
void load();
|
|
||||||
void save();
|
|
||||||
void save(size_t index);
|
|
||||||
|
|
||||||
private:
|
|
||||||
// class LijstElem {
|
|
||||||
// public:
|
|
||||||
// QUuid m_uuid; ///< Unique identifier, used as a key for storing password in psk db.
|
|
||||||
// ConnectionConfig m_config;
|
|
||||||
|
|
||||||
// LijstElem(const QUuid id, const ConnectionConfig &cfg)
|
|
||||||
// : m_uuid(id), m_config(cfg)
|
|
||||||
// {}
|
|
||||||
// };
|
|
||||||
|
|
||||||
using t_Connections = std::vector<ConnectionConfig>;
|
|
||||||
t_Connections m_connections;
|
|
||||||
|
|
||||||
void deleteFromIni(const t_Connections::iterator &begin, const t_Connections::iterator &end);
|
|
||||||
|
|
||||||
static QString iniFileName();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // CONNECTIONLIST_H
|
|
||||||
|
|
@ -1,14 +1,78 @@
|
||||||
#include "ConnectionListModel.h"
|
#include "ConnectionListModel.h"
|
||||||
#include "ConnectionList.h"
|
|
||||||
#include "ScopeGuard.h"
|
#include "ScopeGuard.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <botan/cryptobox.h>
|
#include <botan/cryptobox.h>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
|
|
||||||
ConnectionListModel::ConnectionListModel(ConnectionList *conns, QObject *parent)
|
namespace {
|
||||||
|
|
||||||
|
/** Saves a connection configuration.
|
||||||
|
|
||||||
|
Before calling this you may want to call beginGroup.
|
||||||
|
*/
|
||||||
|
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
||||||
|
{
|
||||||
|
settings.setValue("name", stdStrToQ(cc.name()));
|
||||||
|
settings.setValue("host", stdStrToQ(cc.host()));
|
||||||
|
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
||||||
|
settings.setValue("port", cc.port());
|
||||||
|
settings.setValue("user", stdStrToQ(cc.user()));
|
||||||
|
//settings.setValue("password", stdStrToQ(cc.password()));
|
||||||
|
settings.setValue("dbname", stdStrToQ(cc.dbname()));
|
||||||
|
settings.setValue("sslmode", static_cast<int>(cc.sslMode()));
|
||||||
|
settings.setValue("sslcert", stdStrToQ(cc.sslCert()));
|
||||||
|
settings.setValue("sslkey", stdStrToQ(cc.sslKey()));
|
||||||
|
settings.setValue("sslrootcert", stdStrToQ(cc.sslRootCert()));
|
||||||
|
settings.setValue("sslcrl", stdStrToQ(cc.sslCrl()));
|
||||||
|
settings.setValue("passwordState", static_cast<int>(cc.passwordState()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S, typename T>
|
||||||
|
bool in_range(T value)
|
||||||
|
{
|
||||||
|
return value >= std::numeric_limits<S>::min() && value <= std::numeric_limits<S>::max();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
||||||
|
{
|
||||||
|
cc.setName(qvarToStdStr(settings.value("name")));
|
||||||
|
cc.setHost(qvarToStdStr(settings.value("host")));
|
||||||
|
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
||||||
|
int p = settings.value("port", 5432).toInt();
|
||||||
|
if (!in_range<uint16_t>(p)) {
|
||||||
|
p = 0; // let the user re-enter a valid value
|
||||||
|
}
|
||||||
|
|
||||||
|
cc.setPort(static_cast<uint16_t>(p));
|
||||||
|
cc.setUser(qvarToStdStr(settings.value("user")));
|
||||||
|
|
||||||
|
//cc.setPassword(qvarToStdStr(settings.value("password")));
|
||||||
|
|
||||||
|
cc.setDbname(qvarToStdStr(settings.value("dbname")));
|
||||||
|
cc.setSslMode(static_cast<SslMode>(settings.value("sslmode").toInt()));
|
||||||
|
cc.setSslCert(qvarToStdStr(settings.value("sslcert")));
|
||||||
|
cc.setSslKey(qvarToStdStr(settings.value("sslkey")));
|
||||||
|
cc.setSslRootCert(qvarToStdStr(settings.value("sslrootcert")));
|
||||||
|
cc.setSslCrl(qvarToStdStr(settings.value("sslcrl")));
|
||||||
|
|
||||||
|
PasswordState pwstate;
|
||||||
|
QVariant v = settings.value("passwordState");
|
||||||
|
if (v.isNull()) pwstate = PasswordState::NotStored;
|
||||||
|
else pwstate = static_cast<PasswordState>(v.toInt());
|
||||||
|
cc.setPasswordState(pwstate);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // end of unnamed namespace
|
||||||
|
|
||||||
|
|
||||||
|
ConnectionListModel::ConnectionListModel(QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
, m_connections(conns)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,7 +82,7 @@ int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
if (parent == QModelIndex()) {
|
if (parent == QModelIndex()) {
|
||||||
result = m_connections->size();
|
result = m_connections.size();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +98,7 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
||||||
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
||||||
int row = index.row();
|
int row = index.row();
|
||||||
int col = index.column();
|
int col = index.column();
|
||||||
const ConnectionConfig& cfg = m_connections->getConfigByIdx(row);
|
const ConnectionConfig& cfg = m_connections.at(row);
|
||||||
switch (col) {
|
switch (col) {
|
||||||
case Description:
|
case Description:
|
||||||
result = makeLongDescription(cfg);
|
result = makeLongDescription(cfg);
|
||||||
|
|
@ -72,7 +136,7 @@ bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &valu
|
||||||
// auto& elem = m_connections.at(row);
|
// auto& elem = m_connections.at(row);
|
||||||
// elem.m_dirty = true;
|
// elem.m_dirty = true;
|
||||||
// ConnectionConfig& cfg = elem.m_config;
|
// ConnectionConfig& cfg = elem.m_config;
|
||||||
ConnectionConfig& cfg = m_connections->getConfigByIdx(row);
|
ConnectionConfig& cfg = m_connections[row];
|
||||||
if (col > 0) {
|
if (col > 0) {
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
|
|
@ -109,7 +173,7 @@ Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
Qt::ItemFlags result;
|
Qt::ItemFlags result;
|
||||||
int row = index.row();
|
int row = index.row();
|
||||||
if (row >= 0 && row < (int)m_connections->size()) {
|
if (row >= 0 && row < m_connections.size()) {
|
||||||
result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
||||||
if (index.column() != Description)
|
if (index.column() != Description)
|
||||||
result |= Qt::ItemIsEditable;
|
result |= Qt::ItemIsEditable;
|
||||||
|
|
@ -133,75 +197,122 @@ QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
||||||
return stdStrToQ(result);
|
return stdStrToQ(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionListModel::newItem()
|
|
||||||
{
|
|
||||||
int i = m_connections->createNew();
|
|
||||||
auto idx = createIndex(i, 0);
|
|
||||||
emit dataChanged(idx, idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
Expected<ConnectionConfig> ConnectionListModel::get(size_t row)
|
|
||||||
{
|
|
||||||
if (row < m_connections->size()) {
|
|
||||||
return m_connections->getConfigByIdx(row);
|
|
||||||
}
|
|
||||||
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#include <boost/assert.hpp>
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
size_t as_size_t(T t);
|
|
||||||
|
|
||||||
template <>
|
|
||||||
size_t as_size_t(int t)
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(t >= 0);
|
|
||||||
return static_cast<size_t>(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
size_t as_size_t(long t)
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(t >= 0);
|
|
||||||
return static_cast<size_t>(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//void ConnectionListModel::del(const int idx)
|
|
||||||
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
|
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
if (row >= 0 && row < (int)m_connections->size()) {
|
|
||||||
|
if (row >= 0 && row < m_connections.size()) {
|
||||||
|
|
||||||
beginRemoveRows(parent, row, row + count -1);
|
beginRemoveRows(parent, row, row + count -1);
|
||||||
SCOPE_EXIT { endRemoveRows(); };
|
SCOPE_EXIT { endRemoveRows(); };
|
||||||
|
|
||||||
m_connections->remove(as_size_t(row), as_size_t(count));
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
for (int idx = 0; idx < count; ++idx) {
|
||||||
|
auto&& cc = m_connections[idx+row];
|
||||||
|
{
|
||||||
|
settings.beginGroup(cc.uuid().toString());
|
||||||
|
SCOPE_EXIT { settings.endGroup(); };
|
||||||
|
for (auto&& k : settings.childKeys()) {
|
||||||
|
settings.remove(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_connections.remove(row, count);
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConnectionListModel::newItem()
|
||||||
|
{
|
||||||
|
// int i = m_connections->createNew();
|
||||||
|
// auto idx = createIndex(i, 0);
|
||||||
|
// emit dataChanged(idx, idx);
|
||||||
|
}
|
||||||
|
|
||||||
//void ConnectionListModel::load()
|
Expected<ConnectionConfig> ConnectionListModel::get(int row)
|
||||||
//{
|
{
|
||||||
// m_connections->load();
|
if (row < m_connections.size()) {
|
||||||
//}
|
return m_connections.at(row);
|
||||||
|
}
|
||||||
|
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionListModel::load()
|
||||||
|
{
|
||||||
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
auto groups = settings.childGroups();
|
||||||
|
for (auto&& grp : groups) {
|
||||||
|
if (grp == "c_IniGroupSecurity") {
|
||||||
|
// Read security settings
|
||||||
|
|
||||||
|
} else {
|
||||||
|
QUuid uuid(grp);
|
||||||
|
if ( ! uuid.isNull() ) {
|
||||||
|
settings.beginGroup(grp);
|
||||||
|
SCOPE_EXIT { settings.endGroup(); };
|
||||||
|
|
||||||
|
ConnectionConfig cc;
|
||||||
|
cc.setUuid(uuid);
|
||||||
|
LoadConnectionConfig(settings, cc);
|
||||||
|
m_connections.push_back(cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ConnectionListModel::save()
|
void ConnectionListModel::save()
|
||||||
{
|
{
|
||||||
m_connections->save();
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
for (auto& e : m_connections) {
|
||||||
|
settings.beginGroup(e.uuid().toString());
|
||||||
|
SCOPE_EXIT { settings.endGroup(); };
|
||||||
|
|
||||||
|
SaveConnectionConfig(settings, e);
|
||||||
|
e.clean();
|
||||||
|
}
|
||||||
|
settings.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionListModel::save(size_t index)
|
void ConnectionListModel::save(int index)
|
||||||
{
|
{
|
||||||
m_connections->save(index);
|
auto& e = m_connections[index];
|
||||||
|
if (e.dirty()) {
|
||||||
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
settings.beginGroup(e.uuid().toString());
|
||||||
|
SaveConnectionConfig(settings, e);
|
||||||
|
e.clean();
|
||||||
|
settings.sync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionListModel::save(size_t index, const ConnectionConfig &cc)
|
void ConnectionListModel::save(const ConnectionConfig &cc)
|
||||||
{
|
{
|
||||||
m_connections->setConfigByIdx(index, cc);
|
auto find_res = std::find(m_connections.begin(), m_connections.end(), cc.uuid());
|
||||||
m_connections->save(index);
|
int i;
|
||||||
|
if (find_res == m_connections.end()) {
|
||||||
|
m_connections.push_back(cc);
|
||||||
|
i = m_connections.size() - 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*find_res = cc;
|
||||||
|
i = find_res - m_connections.begin();
|
||||||
|
}
|
||||||
|
emit dataChanged(createIndex(i, 0), createIndex(i, ColCount-1));
|
||||||
|
save(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString ConnectionListModel::iniFileName()
|
||||||
|
{
|
||||||
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||||
|
QDir dir(path);
|
||||||
|
if (!dir.exists()) {
|
||||||
|
dir.mkpath(".");
|
||||||
|
}
|
||||||
|
path += "/connections.ini";
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@
|
||||||
|
|
||||||
#include "ConnectionConfig.h"
|
#include "ConnectionConfig.h"
|
||||||
#include "Expected.h"
|
#include "Expected.h"
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
class ConnectionList;
|
|
||||||
|
|
||||||
/** \brief Model class for the list of connections.
|
/** \brief Model class for the list of connections.
|
||||||
*
|
*
|
||||||
|
|
@ -30,30 +30,44 @@ public:
|
||||||
ColCount
|
ColCount
|
||||||
};
|
};
|
||||||
|
|
||||||
ConnectionListModel(ConnectionList *conns, QObject *parent);
|
ConnectionListModel(QObject *parent);
|
||||||
ConnectionListModel(const ConnectionListModel&) = delete;
|
ConnectionListModel(const ConnectionListModel&) = delete;
|
||||||
~ConnectionListModel() override;
|
~ConnectionListModel() override;
|
||||||
|
|
||||||
|
|
||||||
|
// BEGIN Model/View related functions
|
||||||
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
virtual int columnCount(const QModelIndex &/*parent*/) const override;
|
virtual int columnCount(const QModelIndex &/*parent*/) const override;
|
||||||
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
// virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
// virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||||
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||||
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
||||||
|
// END Model/View related functions
|
||||||
|
|
||||||
void newItem();
|
void newItem();
|
||||||
Expected<ConnectionConfig> get(size_t row);
|
Expected<ConnectionConfig> get(int row);
|
||||||
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
|
|
||||||
|
|
||||||
|
void load();
|
||||||
|
// Writes all entries to storage
|
||||||
void save();
|
void save();
|
||||||
void save(size_t index);
|
// Writes the specified entry to storage
|
||||||
void save(size_t index, const ConnectionConfig &cc);
|
void save(int index);
|
||||||
|
// // Save changes to the config
|
||||||
|
// void save(size_t index, const ConnectionConfig &cc);
|
||||||
|
/** Matches cc to the list by looking at its uuid.
|
||||||
|
*
|
||||||
|
* If it is not in the list it is added. If the uuid is in the list that entry is updated.
|
||||||
|
* In both cases the data is also directly written to long term storage.
|
||||||
|
*/
|
||||||
|
void save(const ConnectionConfig &cc);
|
||||||
static QString makeLongDescription(const ConnectionConfig &cfg);
|
static QString makeLongDescription(const ConnectionConfig &cfg);
|
||||||
private:
|
private:
|
||||||
|
|
||||||
ConnectionList *m_connections;
|
using ConnectionList = QVector<ConnectionConfig>;
|
||||||
|
ConnectionList m_connections;
|
||||||
|
|
||||||
|
QString iniFileName();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONNECTIONLISTMODEL_H
|
#endif // CONNECTIONLISTMODEL_H
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
#include "ConnectionManagerWindow.h"
|
#include "ConnectionManagerWindow.h"
|
||||||
#include "ui_ConnectionManagerWindow.h"
|
#include "ui_ConnectionManagerWindow.h"
|
||||||
//#include "mainwindow.h"
|
|
||||||
#include "MasterController.h"
|
#include "MasterController.h"
|
||||||
#include <QDataWidgetMapper>
|
#include "ConnectionController.h"
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
#include "ConnectionListModel.h"
|
#include "ConnectionListModel.h"
|
||||||
|
|
@ -30,43 +29,17 @@ ConnectionManagerWindow::ConnectionManagerWindow(MasterController *master, QWidg
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
ui->listView->setModel(m_connectionController->getConnectionListModel());
|
ui->listView->setModel(m_connectionController->getConnectionListModel());
|
||||||
|
|
||||||
setupWidgetMappings();
|
|
||||||
|
|
||||||
connect(ui->listView->selectionModel(),
|
|
||||||
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
|
||||||
this, SLOT(on_currentChanged(QModelIndex,QModelIndex)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ConnectionManagerWindow::~ConnectionManagerWindow()
|
ConnectionManagerWindow::~ConnectionManagerWindow()
|
||||||
{
|
{
|
||||||
delete ui;
|
delete ui;
|
||||||
delete m_mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
|
void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
|
||||||
{
|
{
|
||||||
auto clm = m_connectionController->getConnectionListModel();
|
m_connectionController->createConnection();
|
||||||
clm->newItem();
|
|
||||||
|
|
||||||
// Select the new row
|
|
||||||
auto idx = clm->index(clm->rowCount() - 1, 0);
|
|
||||||
ui->listView->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::Select);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionManagerWindow::on_currentChanged(const QModelIndex ¤t,
|
|
||||||
const QModelIndex &)
|
|
||||||
{
|
|
||||||
int currow = current.row();
|
|
||||||
auto clm = m_connectionController->getConnectionListModel();
|
|
||||||
if (prevSelection)
|
|
||||||
clm->save(*prevSelection);
|
|
||||||
m_mapper->setCurrentIndex(currow);
|
|
||||||
if (currow >= 0)
|
|
||||||
prevSelection = static_cast<size_t>(currow);
|
|
||||||
else
|
|
||||||
prevSelection.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionManagerWindow::on_actionDelete_connection_triggered()
|
void ConnectionManagerWindow::on_actionDelete_connection_triggered()
|
||||||
|
|
@ -82,33 +55,18 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ConnectionManagerWindow::setupWidgetMappings()
|
|
||||||
{
|
|
||||||
auto clm = m_connectionController->getConnectionListModel();
|
|
||||||
m_mapper = new QDataWidgetMapper(this);
|
|
||||||
m_mapper->setModel(clm);
|
|
||||||
m_mapper->addMapping(ui->edtName, 1);
|
|
||||||
m_mapper->addMapping(ui->edtHost, 2);
|
|
||||||
m_mapper->addMapping(ui->spinPort, 3);
|
|
||||||
m_mapper->addMapping(ui->edtUser, 4);
|
|
||||||
m_mapper->addMapping(ui->edtDbname, 6);
|
|
||||||
m_mapper->toFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionManagerWindow::on_actionConnect_triggered()
|
void ConnectionManagerWindow::on_actionConnect_triggered()
|
||||||
{
|
{
|
||||||
auto ci = ui->listView->selectionModel()->currentIndex();
|
auto ci = ui->listView->selectionModel()->currentIndex();
|
||||||
if (ci.isValid()) {
|
if (ci.isValid()) {
|
||||||
auto r = static_cast<size_t>(ci.row());
|
m_connectionController->openSqlWindowForConnection(ci.row());
|
||||||
m_connectionController->openSqlWindowForConnection(r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionManagerWindow::on_actionQuit_application_triggered()
|
void ConnectionManagerWindow::on_actionQuit_application_triggered()
|
||||||
{
|
{
|
||||||
auto res = QMessageBox::question(this, "pglab",
|
auto res = QMessageBox::question(this, "pglab",
|
||||||
tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No);
|
tr("Close all windows?"), QMessageBox::Yes, QMessageBox::No);
|
||||||
if (res == QMessageBox::Yes) {
|
if (res == QMessageBox::Yes) {
|
||||||
QApplication::quit();
|
QApplication::quit();
|
||||||
}
|
}
|
||||||
|
|
@ -129,7 +87,12 @@ void ConnectionManagerWindow::on_actionManage_server_triggered()
|
||||||
void ConnectionManagerWindow::on_listView_activated(const QModelIndex &index)
|
void ConnectionManagerWindow::on_listView_activated(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
if (index.isValid()) {
|
if (index.isValid()) {
|
||||||
auto r = static_cast<size_t>(index.row());
|
m_connectionController->openSqlWindowForConnection(index.row());
|
||||||
m_connectionController->openSqlWindowForConnection(r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConnectionManagerWindow::on_actionConfigure_connection_triggered()
|
||||||
|
{
|
||||||
|
auto ci = ui->listView->selectionModel()->currentIndex();
|
||||||
|
m_connectionController->editConnection(ci.row());
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ class ConnectionManagerWindow;
|
||||||
class ConnectionConfig;
|
class ConnectionConfig;
|
||||||
class ConnectionController;
|
class ConnectionController;
|
||||||
class MasterController;
|
class MasterController;
|
||||||
class QDataWidgetMapper;
|
|
||||||
class QStandardItemModel;
|
class QStandardItemModel;
|
||||||
|
|
||||||
/** \brief Class that holds glue code for the ConnectionManager UI.
|
/** \brief Class that holds glue code for the ConnectionManager UI.
|
||||||
|
|
@ -25,7 +24,6 @@ public:
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_actionAdd_Connection_triggered();
|
void on_actionAdd_Connection_triggered();
|
||||||
void on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
|
||||||
void on_actionDelete_connection_triggered();
|
void on_actionDelete_connection_triggered();
|
||||||
void on_actionConnect_triggered();
|
void on_actionConnect_triggered();
|
||||||
void on_actionQuit_application_triggered();
|
void on_actionQuit_application_triggered();
|
||||||
|
|
@ -34,15 +32,12 @@ private slots:
|
||||||
|
|
||||||
void on_listView_activated(const QModelIndex &index);
|
void on_listView_activated(const QModelIndex &index);
|
||||||
|
|
||||||
|
void on_actionConfigure_connection_triggered();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::ConnectionManagerWindow *ui;
|
Ui::ConnectionManagerWindow *ui;
|
||||||
QDataWidgetMapper *m_mapper = nullptr;
|
|
||||||
MasterController *m_masterController;
|
MasterController *m_masterController;
|
||||||
ConnectionController *m_connectionController;
|
ConnectionController *m_connectionController;
|
||||||
|
|
||||||
std::optional<size_t> prevSelection;
|
|
||||||
|
|
||||||
void setupWidgetMappings();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONNECTIONMANAGERWINDOW_H
|
#endif // CONNECTIONMANAGERWINDOW_H
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>800</width>
|
<width>413</width>
|
||||||
<height>600</height>
|
<height>600</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
|
@ -28,148 +28,6 @@
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QWidget" name="layoutWidget">
|
|
||||||
<layout class="QFormLayout" name="formLayout_2">
|
|
||||||
<item row="0" column="0">
|
|
||||||
<widget class="QLabel" name="label">
|
|
||||||
<property name="text">
|
|
||||||
<string>Name</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="0" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtName"/>
|
|
||||||
</item>
|
|
||||||
<item row="1" column="0">
|
|
||||||
<widget class="QLabel" name="label_2">
|
|
||||||
<property name="text">
|
|
||||||
<string>Host</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="1" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtHost"/>
|
|
||||||
</item>
|
|
||||||
<item row="2" column="0">
|
|
||||||
<widget class="QLabel" name="label_3">
|
|
||||||
<property name="text">
|
|
||||||
<string>Port</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="2" column="1">
|
|
||||||
<widget class="QSpinBox" name="spinPort">
|
|
||||||
<property name="maximum">
|
|
||||||
<number>65535</number>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="3" column="0">
|
|
||||||
<widget class="QLabel" name="label_5">
|
|
||||||
<property name="text">
|
|
||||||
<string>Username</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="3" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtUser"/>
|
|
||||||
</item>
|
|
||||||
<item row="4" column="0">
|
|
||||||
<widget class="QLabel" name="label_4">
|
|
||||||
<property name="text">
|
|
||||||
<string>Database</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="4" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtDbname"/>
|
|
||||||
</item>
|
|
||||||
<item row="5" column="0">
|
|
||||||
<widget class="QLabel" name="label_7">
|
|
||||||
<property name="text">
|
|
||||||
<string>SSL</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="5" column="1">
|
|
||||||
<widget class="QComboBox" name="cmbbxSsl">
|
|
||||||
<property name="currentIndex">
|
|
||||||
<number>2</number>
|
|
||||||
</property>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>reject</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>allow</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>prefer</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>require</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>verify-ca</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<property name="text">
|
|
||||||
<string>verify-full</string>
|
|
||||||
</property>
|
|
||||||
</item>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="6" column="0">
|
|
||||||
<widget class="QLabel" name="label_8">
|
|
||||||
<property name="text">
|
|
||||||
<string>Certificate</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="6" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtCert"/>
|
|
||||||
</item>
|
|
||||||
<item row="7" column="0">
|
|
||||||
<widget class="QLabel" name="label_9">
|
|
||||||
<property name="text">
|
|
||||||
<string>Key</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="7" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtKey"/>
|
|
||||||
</item>
|
|
||||||
<item row="8" column="0">
|
|
||||||
<widget class="QLabel" name="label_10">
|
|
||||||
<property name="text">
|
|
||||||
<string>Root cert.</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="8" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtRootCert"/>
|
|
||||||
</item>
|
|
||||||
<item row="9" column="0">
|
|
||||||
<widget class="QLabel" name="label_11">
|
|
||||||
<property name="text">
|
|
||||||
<string>Revocation list</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="9" column="1">
|
|
||||||
<widget class="QLineEdit" name="edtCrl"/>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</widget>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
@ -179,7 +37,7 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>800</width>
|
<width>413</width>
|
||||||
<height>20</height>
|
<height>20</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
|
@ -224,6 +82,7 @@ QToolButton {
|
||||||
<addaction name="actionManage_server"/>
|
<addaction name="actionManage_server"/>
|
||||||
<addaction name="separator"/>
|
<addaction name="separator"/>
|
||||||
<addaction name="actionAdd_Connection"/>
|
<addaction name="actionAdd_Connection"/>
|
||||||
|
<addaction name="actionConfigure_connection"/>
|
||||||
<addaction name="actionDelete_connection"/>
|
<addaction name="actionDelete_connection"/>
|
||||||
<addaction name="separator"/>
|
<addaction name="separator"/>
|
||||||
<addaction name="actionBackup_database"/>
|
<addaction name="actionBackup_database"/>
|
||||||
|
|
@ -285,6 +144,16 @@ QToolButton {
|
||||||
<string>Manage server</string>
|
<string>Manage server</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
|
<action name="actionConfigure_connection">
|
||||||
|
<property name="icon">
|
||||||
|
<iconset>
|
||||||
|
<normalon>:/icons/server_configuration.png</normalon>
|
||||||
|
</iconset>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Configure connection</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
<resources>
|
<resources>
|
||||||
<include location="resources.qrc"/>
|
<include location="resources.qrc"/>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include "CrudTab.h"
|
#include "CrudTab.h"
|
||||||
#include "widgets/CatalogTablesPage.h"
|
#include "widgets/CatalogTablesPage.h"
|
||||||
#include "OpenDatabase.h"
|
#include "OpenDatabase.h"
|
||||||
|
#include "ConnectionController.h"
|
||||||
#include "MasterController.h"
|
#include "MasterController.h"
|
||||||
#include "TaskExecutor.h"
|
#include "TaskExecutor.h"
|
||||||
#include <QAction>
|
#include <QAction>
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,8 @@
|
||||||
#include "MasterController.h"
|
#include "MasterController.h"
|
||||||
#include "ConnectionManagerWindow.h"
|
#include "ConnectionController.h"
|
||||||
#include "ConnectionList.h"
|
|
||||||
#include "ConnectionListModel.h"
|
|
||||||
#include "PasswordManager.h"
|
|
||||||
#include "DatabaseWindow.h"
|
|
||||||
#include "ServerWindow.h"
|
|
||||||
#include "BackupDialog.h"
|
|
||||||
#include "PasswordPromptDialog.h"
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <botan/sqlite3.h>
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
|
@ -60,193 +52,3 @@ QSqlDatabase& MasterController::userConfigDatabase()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ConnectionController::ConnectionController(MasterController *parent)
|
|
||||||
: QObject(parent)
|
|
||||||
, m_masterController(parent)
|
|
||||||
{}
|
|
||||||
|
|
||||||
ConnectionController::~ConnectionController()
|
|
||||||
{
|
|
||||||
delete m_connectionManagerWindow;
|
|
||||||
delete m_connectionListModel;
|
|
||||||
delete m_connectionList;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionController::init()
|
|
||||||
{
|
|
||||||
//std::string dbfilename = QDir::toNativeSeparators(GetUserConfigDatabaseName()).toUtf8().data();
|
|
||||||
//m_userConfigDatabase = std::make_shared<Botan::Sqlite3_Database>(dbfilename);
|
|
||||||
|
|
||||||
|
|
||||||
m_passwordManager = std::make_shared<PasswordManager>();
|
|
||||||
|
|
||||||
m_connectionList = new ConnectionList;
|
|
||||||
m_connectionList->load();
|
|
||||||
m_connectionListModel = new ConnectionListModel(m_connectionList, this);
|
|
||||||
|
|
||||||
m_connectionManagerWindow = new ConnectionManagerWindow(m_masterController, nullptr);
|
|
||||||
m_connectionManagerWindow->show();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionController::showConnectionManager()
|
|
||||||
{
|
|
||||||
m_connectionManagerWindow->show();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionController::openSqlWindowForConnection(size_t connection_index)
|
|
||||||
{
|
|
||||||
|
|
||||||
auto res = m_connectionListModel->get(connection_index);
|
|
||||||
if (res.valid()) {
|
|
||||||
auto cc = res.get();
|
|
||||||
|
|
||||||
if (retrieveConnectionPassword(cc)) {
|
|
||||||
m_connectionListModel->save(connection_index, cc);
|
|
||||||
// TODO instead of directly openening the mainwindow
|
|
||||||
// do async connect and only open window when we have
|
|
||||||
// working connection
|
|
||||||
auto w = new DatabaseWindow(m_masterController, nullptr);
|
|
||||||
w->setAttribute( Qt::WA_DeleteOnClose );
|
|
||||||
w->setConfig(cc);
|
|
||||||
w->showMaximized();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionController::openBackupDlgForConnection(size_t connection_index)
|
|
||||||
{
|
|
||||||
auto res = m_connectionListModel->get(connection_index);
|
|
||||||
if (res.valid()) {
|
|
||||||
auto cc = res.get();
|
|
||||||
if (retrieveConnectionPassword(cc)) {
|
|
||||||
m_connectionListModel->save(connection_index, cc);
|
|
||||||
auto w = new BackupDialog(nullptr); //new ServerWindow(this, nullptr);
|
|
||||||
w->setAttribute( Qt::WA_DeleteOnClose );
|
|
||||||
w->setConfig(cc);
|
|
||||||
w->show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConnectionController::openServerWindowForConnection(size_t connection_index)
|
|
||||||
{
|
|
||||||
auto res = m_connectionListModel->get(connection_index);
|
|
||||||
if (res.valid()) {
|
|
||||||
auto cc = res.get();
|
|
||||||
if (retrieveConnectionPassword(cc)) {
|
|
||||||
m_connectionListModel->save(connection_index, cc);
|
|
||||||
auto w = new ServerWindow(m_masterController, nullptr);
|
|
||||||
w->setAttribute( Qt::WA_DeleteOnClose );
|
|
||||||
w->setConfig(cc);
|
|
||||||
w->show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
|
||||||
{
|
|
||||||
auto pw_state = cc.passwordState();
|
|
||||||
if (pw_state == PasswordState::NotNeeded) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (pw_state == PasswordState::SavedPasswordManager) {
|
|
||||||
std::string pw;
|
|
||||||
bool result = getPasswordFromPskdb(getPskId(cc), pw);
|
|
||||||
if (result) {
|
|
||||||
cc.setPassword(pw);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Geen else hier want als voorgaande blok niet geretourneerd heeft moeten we wachtwoord
|
|
||||||
// ook aan de gebruiker vragen zoals hier gebeurd.
|
|
||||||
QString str = ConnectionListModel::makeLongDescription(cc);
|
|
||||||
auto dlg = std::make_unique<PasswordPromptDialog>(PasswordPromptDialog::SaveOption, nullptr);
|
|
||||||
dlg->setCaption(tr("Connection password prompt"));
|
|
||||||
dlg->setDescription(QString(tr("Please provide password for connection %1")).arg(str));
|
|
||||||
int exec_result = dlg->exec();
|
|
||||||
|
|
||||||
if (exec_result == QDialog::Accepted) {
|
|
||||||
std::string password = dlg->password().toUtf8().data();
|
|
||||||
cc.setPassword(password);
|
|
||||||
if (dlg->saveChecked()) {
|
|
||||||
storePasswordInPskdb(getPskId(cc), password);
|
|
||||||
cc.setPasswordState(PasswordState::SavedPasswordManager);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool ConnectionController::getPasswordFromPskdb(const std::string &password_id, std::string &password)
|
|
||||||
{
|
|
||||||
if (!UnlockPasswordManagerIfNeeded())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return m_passwordManager->get(password_id, password);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool ConnectionController::storePasswordInPskdb(const std::string &password_id, const std::string password)
|
|
||||||
{
|
|
||||||
if (!UnlockPasswordManagerIfNeeded())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
m_passwordManager->set(password_id, password);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConnectionController::UnlockPasswordManagerIfNeeded()
|
|
||||||
{
|
|
||||||
auto&& user_cfg_db = m_masterController->userConfigDatabase();
|
|
||||||
if (m_passwordManager->initialized(user_cfg_db)) {
|
|
||||||
if (!m_passwordManager->locked())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
// ask user for passphrase
|
|
||||||
auto dlg = std::make_unique<PasswordPromptDialog>(nullptr, nullptr);
|
|
||||||
dlg->setCaption(tr("Unlock password manager"));
|
|
||||||
dlg->setDescription(tr("Enter password for password manager"));
|
|
||||||
int exec_result = dlg->exec();
|
|
||||||
bool ok = (exec_result == QDialog::Accepted);
|
|
||||||
|
|
||||||
if (!ok) {
|
|
||||||
// leave this retry loop
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// user gave OK, if succeeds return true otherwise loop a prompt for password again.
|
|
||||||
if (m_passwordManager->openDatabase(user_cfg_db, dlg->password()))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Ask user for passphrase + confirmation, clearly instruct this is first setup
|
|
||||||
// create
|
|
||||||
auto dlg = std::make_unique<PasswordPromptDialog>(PasswordPromptDialog::ConfirmPassword, nullptr);
|
|
||||||
dlg->setCaption(tr("Password manager setup"));
|
|
||||||
dlg->setDescription(tr("Enter a strong password for password manager initialization. A strong key will be "
|
|
||||||
"derived from your password and it will be impossible to recover anything from the "
|
|
||||||
"password manager without the password you enter here."));
|
|
||||||
int exec_result = dlg->exec();
|
|
||||||
if (exec_result == QDialog::Accepted) {
|
|
||||||
QString passphrase = dlg->password();
|
|
||||||
if (m_passwordManager->createDatabase(user_cfg_db, passphrase))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string ConnectionController::getPskId(const ConnectionConfig &cc)
|
|
||||||
{
|
|
||||||
std::string id = "dbpw/";
|
|
||||||
id += cc.uuid().toString().toUtf8().data();
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,6 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
//namespace Botan {
|
|
||||||
// class Sqlite3_Database;
|
|
||||||
//}
|
|
||||||
|
|
||||||
class ConnectionConfig;
|
|
||||||
class ConnectionList;
|
|
||||||
class ConnectionListModel;
|
|
||||||
class ConnectionManagerWindow;
|
|
||||||
class PasswordManager;
|
|
||||||
|
|
||||||
class ConnectionController;
|
class ConnectionController;
|
||||||
/** \brief Controller class responsible for all things global.
|
/** \brief Controller class responsible for all things global.
|
||||||
|
|
@ -43,47 +34,5 @@ private:
|
||||||
ConnectionController* m_connectionController = nullptr;
|
ConnectionController* m_connectionController = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConnectionController : public QObject {
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
explicit ConnectionController(MasterController *parent = nullptr);
|
|
||||||
~ConnectionController();
|
|
||||||
|
|
||||||
void init();
|
|
||||||
|
|
||||||
ConnectionListModel *getConnectionListModel()
|
|
||||||
{
|
|
||||||
return m_connectionListModel;
|
|
||||||
}
|
|
||||||
|
|
||||||
void showConnectionManager();
|
|
||||||
void openSqlWindowForConnection(size_t connection_index);
|
|
||||||
void openServerWindowForConnection(size_t connection_index);
|
|
||||||
void openBackupDlgForConnection(size_t connection_index);
|
|
||||||
|
|
||||||
private:
|
|
||||||
MasterController *m_masterController;
|
|
||||||
ConnectionList *m_connectionList = nullptr;
|
|
||||||
ConnectionListModel *m_connectionListModel = nullptr;
|
|
||||||
ConnectionManagerWindow *m_connectionManagerWindow = nullptr;
|
|
||||||
|
|
||||||
/** Using long lived object so it can remember its master password for sometime
|
|
||||||
* if the user wishes it.
|
|
||||||
*/
|
|
||||||
std::shared_ptr<PasswordManager> m_passwordManager;
|
|
||||||
|
|
||||||
/** Retrieves the connection password from the user (directly or through the psk db)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
bool retrieveConnectionPassword(ConnectionConfig &cc);
|
|
||||||
|
|
||||||
bool getPasswordFromPskdb(const std::string &password_id, std::string &password);
|
|
||||||
|
|
||||||
bool storePasswordInPskdb(const std::string &password_id, const std::string password);
|
|
||||||
|
|
||||||
bool UnlockPasswordManagerIfNeeded();
|
|
||||||
|
|
||||||
static std::string getPskId(const ConnectionConfig &cc);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // MASTERCONTROLLER_H
|
#endif // MASTERCONTROLLER_H
|
||||||
|
|
|
||||||
69
pglab/SslModeModel.cpp
Normal file
69
pglab/SslModeModel.cpp
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
#include "SslModeModel.h"
|
||||||
|
|
||||||
|
SslModeModel::SslModeModel(QObject *parent)
|
||||||
|
: QAbstractListModel(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
|
int SslModeModel::rowCount(const QModelIndex &) const
|
||||||
|
{
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SslModeModel::columnCount(const QModelIndex &) const
|
||||||
|
{
|
||||||
|
return ColCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant SslModeModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
QVariant v;
|
||||||
|
if (role == Qt::DisplayRole) {
|
||||||
|
switch (index.column()) {
|
||||||
|
case Name:
|
||||||
|
switch(index.row()) {
|
||||||
|
case 0:
|
||||||
|
v = tr("disable");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
v = tr("allow");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
v = tr("prefer");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
v = tr("require");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
v = tr("verify_ca");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
v = tr("verify_full");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Description:
|
||||||
|
switch(index.row()) {
|
||||||
|
case 0:
|
||||||
|
v = tr("try a non encrypted connection only");
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
v = tr("try no encryption first then try encrypted");
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
v = tr("try encrypted first then not encrypted");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
v = tr("require an encrypted connection");
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
v = tr("verify encryption certificate has a valid signature");
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
v = tr("verify encryption certificate has a valid signature and matches the host");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
20
pglab/SslModeModel.h
Normal file
20
pglab/SslModeModel.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef SSLMODEMODEL_H
|
||||||
|
#define SSLMODEMODEL_H
|
||||||
|
|
||||||
|
#include <QAbstractListModel>
|
||||||
|
|
||||||
|
class SslModeModel : public QAbstractListModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
enum Column { Name, Description, ColCount };
|
||||||
|
|
||||||
|
SslModeModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
|
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SSLMODEMODEL_H
|
||||||
BIN
pglab/icons/server_configuration.png
Normal file
BIN
pglab/icons/server_configuration.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
|
|
@ -21,6 +21,8 @@ DEFINES += _WIN32_WINNT=0x0501
|
||||||
win32:RC_ICONS += pglab.ico
|
win32:RC_ICONS += pglab.ico
|
||||||
|
|
||||||
SOURCES += main.cpp\
|
SOURCES += main.cpp\
|
||||||
|
ConnectionConfigurationWidget.cpp \
|
||||||
|
ConnectionController.cpp \
|
||||||
NotificationListWidget.cpp \
|
NotificationListWidget.cpp \
|
||||||
NotificationModel.cpp \
|
NotificationModel.cpp \
|
||||||
NotificationService.cpp \
|
NotificationService.cpp \
|
||||||
|
|
@ -30,6 +32,7 @@ SOURCES += main.cpp\
|
||||||
CreateDatabaseDialog.cpp \
|
CreateDatabaseDialog.cpp \
|
||||||
ConnectionManagerWindow.cpp \
|
ConnectionManagerWindow.cpp \
|
||||||
ConnectionListModel.cpp \
|
ConnectionListModel.cpp \
|
||||||
|
SslModeModel.cpp \
|
||||||
stopwatch.cpp \
|
stopwatch.cpp \
|
||||||
TuplesResultWidget.cpp \
|
TuplesResultWidget.cpp \
|
||||||
BackupDialog.cpp \
|
BackupDialog.cpp \
|
||||||
|
|
@ -40,7 +43,6 @@ SOURCES += main.cpp\
|
||||||
ServerWindow.cpp \
|
ServerWindow.cpp \
|
||||||
DatabasesTableModel.cpp \
|
DatabasesTableModel.cpp \
|
||||||
RolesTableModel.cpp \
|
RolesTableModel.cpp \
|
||||||
ConnectionList.cpp \
|
|
||||||
ProcessStdioWidget.cpp \
|
ProcessStdioWidget.cpp \
|
||||||
GlobalIoService.cpp \
|
GlobalIoService.cpp \
|
||||||
ResultTableModelUtil.cpp \
|
ResultTableModelUtil.cpp \
|
||||||
|
|
@ -85,6 +87,8 @@ PropertyProxyModel.cpp \
|
||||||
widgets/CatalogSequencesPage.cpp
|
widgets/CatalogSequencesPage.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
ConnectionConfigurationWidget.h \
|
||||||
|
ConnectionController.h \
|
||||||
IDatabaseWindow.h \
|
IDatabaseWindow.h \
|
||||||
NotificationListWidget.h \
|
NotificationListWidget.h \
|
||||||
NotificationModel.h \
|
NotificationModel.h \
|
||||||
|
|
@ -95,6 +99,7 @@ HEADERS += \
|
||||||
CreateDatabaseDialog.h \
|
CreateDatabaseDialog.h \
|
||||||
ConnectionManagerWindow.h \
|
ConnectionManagerWindow.h \
|
||||||
ConnectionListModel.h \
|
ConnectionListModel.h \
|
||||||
|
SslModeModel.h \
|
||||||
stopwatch.h \
|
stopwatch.h \
|
||||||
TuplesResultWidget.h \
|
TuplesResultWidget.h \
|
||||||
BackupDialog.h \
|
BackupDialog.h \
|
||||||
|
|
@ -105,7 +110,6 @@ HEADERS += \
|
||||||
ServerWindow.h \
|
ServerWindow.h \
|
||||||
DatabasesTableModel.h \
|
DatabasesTableModel.h \
|
||||||
RolesTableModel.h \
|
RolesTableModel.h \
|
||||||
ConnectionList.h \
|
|
||||||
ProcessStdioWidget.h \
|
ProcessStdioWidget.h \
|
||||||
GlobalIoService.h \
|
GlobalIoService.h \
|
||||||
ResultTableModelUtil.h \
|
ResultTableModelUtil.h \
|
||||||
|
|
|
||||||
|
|
@ -28,5 +28,6 @@
|
||||||
<file>icons/constraints/primarykey.png</file>
|
<file>icons/constraints/primarykey.png</file>
|
||||||
<file>icons/constraints/unique.png</file>
|
<file>icons/constraints/unique.png</file>
|
||||||
<file>icons/constraints/index.png</file>
|
<file>icons/constraints/index.png</file>
|
||||||
|
<file>icons/server_configuration.png</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class QString;
|
||||||
|
|
||||||
class ConnectionConfig {
|
class ConnectionConfig {
|
||||||
public:
|
public:
|
||||||
ConnectionConfig();
|
ConnectionConfig(); // Default object containing invalid uuid
|
||||||
|
|
||||||
void setUuid(const QUuid &uuid);
|
void setUuid(const QUuid &uuid);
|
||||||
const QUuid &uuid() const;
|
const QUuid &uuid() const;
|
||||||
|
|
@ -79,6 +79,8 @@ public:
|
||||||
|
|
||||||
bool dirty() const;
|
bool dirty() const;
|
||||||
void clean();
|
void clean();
|
||||||
|
|
||||||
|
bool operator==(QUuid id) const { return m_uuid == id; }
|
||||||
private:
|
private:
|
||||||
QUuid m_uuid;
|
QUuid m_uuid;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue