Split all connection related controller functionality off into seperate ConnectionController.

This commit is contained in:
eelke 2019-08-19 10:05:05 +02:00
parent 8c13bdc2ef
commit 35d1e75d35
8 changed files with 103 additions and 61 deletions

View file

@ -27,10 +27,10 @@ ConnectionManagerWindow::ConnectionManagerWindow(MasterController *master, QWidg
, ui(new Ui::ConnectionManagerWindow) , ui(new Ui::ConnectionManagerWindow)
// , m_listModel(new ConnectionListModel(this)) // , m_listModel(new ConnectionListModel(this))
, m_masterController(master) , m_masterController(master)
, m_connectionController(master->connectionController())
{ {
ui->setupUi(this); ui->setupUi(this);
ui->listView->setModel(m_connectionController->getConnectionListModel());
ui->listView->setModel(m_masterController->getConnectionListModel());
setupWidgetMappings(); setupWidgetMappings();
@ -55,7 +55,7 @@ void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
// c.setName("new"); // c.setName("new");
//m_listModel->add(c); //m_listModel->add(c);
auto clm = m_masterController->getConnectionListModel(); auto clm = m_connectionController->getConnectionListModel();
clm->newItem(); clm->newItem();
// Select the new row // Select the new row
@ -67,7 +67,7 @@ void ConnectionManagerWindow::on_currentChanged(const QModelIndex &current,
const QModelIndex &) const QModelIndex &)
{ {
int currow = current.row(); int currow = current.row();
auto clm = m_masterController->getConnectionListModel(); auto clm = m_connectionController->getConnectionListModel();
if (prevSelection) if (prevSelection)
clm->save(*prevSelection); clm->save(*prevSelection);
m_mapper->setCurrentIndex(currow); m_mapper->setCurrentIndex(currow);
@ -81,11 +81,10 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered()
{ {
auto ci = ui->listView->selectionModel()->currentIndex(); auto ci = ui->listView->selectionModel()->currentIndex();
if (ci.isValid()) { if (ci.isValid()) {
auto res = QMessageBox::question(this, "pglab", auto res = QMessageBox::question(this, "pglab",
tr("Are you sure you want to remove this connection?"), QMessageBox::Yes, QMessageBox::No); tr("Are you sure you want to remove this connection?"), QMessageBox::Yes, QMessageBox::No);
if (res == QMessageBox::Yes) { if (res == QMessageBox::Yes) {
auto clm = m_masterController->getConnectionListModel(); auto clm = m_connectionController->getConnectionListModel();
clm->removeRow(ci.row()); clm->removeRow(ci.row());
} }
} }
@ -94,7 +93,7 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered()
void ConnectionManagerWindow::setupWidgetMappings() void ConnectionManagerWindow::setupWidgetMappings()
{ {
auto clm = m_masterController->getConnectionListModel(); auto clm = m_connectionController->getConnectionListModel();
m_mapper = new QDataWidgetMapper(this); m_mapper = new QDataWidgetMapper(this);
m_mapper->setModel(clm); m_mapper->setModel(clm);
m_mapper->addMapping(ui->edtName, 1); m_mapper->addMapping(ui->edtName, 1);
@ -110,7 +109,7 @@ 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()); auto r = static_cast<size_t>(ci.row());
m_masterController->openSqlWindowForConnection(r); m_connectionController->openSqlWindowForConnection(r);
} }
else { else {
// TODO can we give unobtrusive message why it didn't work? // TODO can we give unobtrusive message why it didn't work?
@ -131,13 +130,13 @@ void ConnectionManagerWindow::on_actionQuit_application_triggered()
void ConnectionManagerWindow::on_actionBackup_database_triggered() void ConnectionManagerWindow::on_actionBackup_database_triggered()
{ {
auto ci = ui->listView->selectionModel()->currentIndex(); auto ci = ui->listView->selectionModel()->currentIndex();
m_masterController->openBackupDlgForConnection(ci.row()); m_connectionController->openBackupDlgForConnection(ci.row());
} }
void ConnectionManagerWindow::on_actionManage_server_triggered() void ConnectionManagerWindow::on_actionManage_server_triggered()
{ {
auto ci = ui->listView->selectionModel()->currentIndex(); auto ci = ui->listView->selectionModel()->currentIndex();
m_masterController->openServerWindowForConnection(ci.row()); m_connectionController->openServerWindowForConnection(ci.row());
} }

View file

@ -9,6 +9,7 @@ class ConnectionManagerWindow;
} }
class ConnectionConfig; class ConnectionConfig;
class ConnectionController;
class MasterController; class MasterController;
class QDataWidgetMapper; class QDataWidgetMapper;
class QStandardItemModel; class QStandardItemModel;
@ -35,6 +36,7 @@ private:
Ui::ConnectionManagerWindow *ui; Ui::ConnectionManagerWindow *ui;
QDataWidgetMapper *m_mapper = nullptr; QDataWidgetMapper *m_mapper = nullptr;
MasterController *m_masterController; MasterController *m_masterController;
ConnectionController *m_connectionController;
std::optional<size_t> prevSelection; std::optional<size_t> prevSelection;

View file

@ -76,12 +76,9 @@ void DatabaseWindow::newCrudPage(Oid tableoid)
void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres) void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres)
{ {
// TODO should this call be this direct or should it go through module system auto cgtab = new CodeGenerator(this);
// yes it should otherwise context cannot properly setup toolbar and menu!!! cgtab->Init(m_database->catalog(), query, dbres);
// auto cgtab = new CodeGenerator(context(), pluginModule(), this); addPage(cgtab, "Codegen");
// cgtab->Init(m_database->catalog(), query, dbres);
// addPage(cgtab, "Codegen");
//
} }
QueryTool *DatabaseWindow::GetActiveQueryTool() QueryTool *DatabaseWindow::GetActiveQueryTool()
@ -325,12 +322,11 @@ void DatabaseWindow::catalogLoaded()
try { try {
m_database = loadWatcher.future().result(); m_database = loadWatcher.future().result();
for (auto f : { "user", "pg_catalog", "information_schema" }) { // for (auto f : { "user", "pg_catalog", "information_schema" }) {
// TODO open inspector windows // // TODO open inspector windows
} // }
// newCreateTablePage();
on_actionNewSql_triggered();
newCreateTablePage();
} catch (const OpenDatabaseException &ex) { } catch (const OpenDatabaseException &ex) {
QMessageBox::critical(this, "Error reading database", ex.text()); QMessageBox::critical(this, "Error reading database", ex.text());
close(); close();
@ -444,6 +440,7 @@ void DatabaseWindow::on_actionGenerateCode_triggered()
auto query_tool = GetActiveQueryTool(); auto query_tool = GetActiveQueryTool();
if (query_tool) { if (query_tool) {
query_tool->generateCode(); query_tool->generateCode();
//newCodeGenPage
} }
} }
@ -511,7 +508,7 @@ void DatabaseWindow::on_actionSaveCopyOfSqlAs_triggered()
void DatabaseWindow::on_actionShowConnectionManager_triggered() void DatabaseWindow::on_actionShowConnectionManager_triggered()
{ {
m_masterController->showConnectionManager(); m_masterController->connectionController()->showConnectionManager();
} }
void DatabaseWindow::on_m_tabWidget_tabCloseRequested(int index) void DatabaseWindow::on_m_tabWidget_tabCloseRequested(int index)

View file

@ -45,7 +45,7 @@ public:
/// Called when a newly created page is added to the QTabWidget /// Called when a newly created page is added to the QTabWidget
void addPage(QWidget* page, QString caption); void addPage(QWidget* page, QString caption);
void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres); virtual void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres) override;
QueryTool *GetActiveQueryTool(); QueryTool *GetActiveQueryTool();
private: private:

View file

@ -3,6 +3,11 @@
#include <QIcon> #include <QIcon>
#include <QString> #include <QString>
#include <memory>
namespace Pgsql {
class Result;
}
class OpenDatabase; class OpenDatabase;
class QWidget; class QWidget;
@ -15,6 +20,7 @@ public:
virtual void setIconForWidget(QWidget* widget, QIcon icon) = 0; virtual void setIconForWidget(QWidget* widget, QIcon icon) = 0;
virtual std::shared_ptr<OpenDatabase> openDatabase() = 0; virtual std::shared_ptr<OpenDatabase> openDatabase() = 0;
virtual void showStatusBarMessage(QString message) = 0; virtual void showStatusBarMessage(QString message) = 0;
virtual void newCodeGenPage(QString query, std::shared_ptr<const Pgsql::Result> dbres) = 0;
}; };
#endif // IDATABASEWINDOW_H #endif // IDATABASEWINDOW_H

View file

@ -31,17 +31,10 @@ MasterController::MasterController(QObject *parent) : QObject(parent)
{} {}
MasterController::~MasterController() MasterController::~MasterController()
{ {}
delete m_connectionManagerWindow;
delete m_connectionListModel;
delete m_connectionList;
}
void MasterController::init() void MasterController::init()
{ {
//std::string dbfilename = QDir::toNativeSeparators(GetUserConfigDatabaseName()).toUtf8().data();
//m_userConfigDatabase = std::make_shared<Botan::Sqlite3_Database>(dbfilename);
m_userConfigDatabase = QSqlDatabase::addDatabase("QSQLITE"); m_userConfigDatabase = QSqlDatabase::addDatabase("QSQLITE");
m_userConfigDatabase.setDatabaseName(GetUserConfigDatabaseName()); m_userConfigDatabase.setDatabaseName(GetUserConfigDatabaseName());
@ -52,23 +45,56 @@ void MasterController::init()
qDebug() << "Database: connection ok"; qDebug() << "Database: connection ok";
} }
m_connectionController = new ConnectionController(this);
m_connectionController->init();
}
ConnectionController *MasterController::connectionController()
{
return m_connectionController;
}
QSqlDatabase& MasterController::userConfigDatabase()
{
return m_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_passwordManager = std::make_shared<PasswordManager>();
m_connectionList = new ConnectionList; m_connectionList = new ConnectionList;
m_connectionList->load(); m_connectionList->load();
m_connectionListModel = new ConnectionListModel(m_connectionList, this); m_connectionListModel = new ConnectionListModel(m_connectionList, this);
m_connectionManagerWindow = new ConnectionManagerWindow(this, nullptr); m_connectionManagerWindow = new ConnectionManagerWindow(m_masterController, nullptr);
m_connectionManagerWindow->show(); m_connectionManagerWindow->show();
} }
void MasterController::showConnectionManager() void ConnectionController::showConnectionManager()
{ {
m_connectionManagerWindow->show(); m_connectionManagerWindow->show();
} }
void MasterController::openSqlWindowForConnection(size_t connection_index) void ConnectionController::openSqlWindowForConnection(size_t connection_index)
{ {
auto res = m_connectionListModel->get(connection_index); auto res = m_connectionListModel->get(connection_index);
@ -80,7 +106,7 @@ void MasterController::openSqlWindowForConnection(size_t connection_index)
// TODO instead of directly openening the mainwindow // TODO instead of directly openening the mainwindow
// do async connect and only open window when we have // do async connect and only open window when we have
// working connection // working connection
auto w = new DatabaseWindow(this, nullptr); auto w = new DatabaseWindow(m_masterController, nullptr);
w->setAttribute( Qt::WA_DeleteOnClose ); w->setAttribute( Qt::WA_DeleteOnClose );
w->setConfig(cc); w->setConfig(cc);
w->showMaximized(); w->showMaximized();
@ -89,7 +115,7 @@ void MasterController::openSqlWindowForConnection(size_t connection_index)
} }
void MasterController::openBackupDlgForConnection(size_t connection_index) void ConnectionController::openBackupDlgForConnection(size_t connection_index)
{ {
auto res = m_connectionListModel->get(connection_index); auto res = m_connectionListModel->get(connection_index);
if (res.valid()) { if (res.valid()) {
@ -104,14 +130,14 @@ void MasterController::openBackupDlgForConnection(size_t connection_index)
} }
} }
void MasterController::openServerWindowForConnection(size_t connection_index) void ConnectionController::openServerWindowForConnection(size_t connection_index)
{ {
auto res = m_connectionListModel->get(connection_index); auto res = m_connectionListModel->get(connection_index);
if (res.valid()) { if (res.valid()) {
auto cc = res.get(); auto cc = res.get();
if (retrieveConnectionPassword(cc)) { if (retrieveConnectionPassword(cc)) {
m_connectionListModel->save(connection_index, cc); m_connectionListModel->save(connection_index, cc);
auto w = new ServerWindow(this, nullptr); auto w = new ServerWindow(m_masterController, nullptr);
w->setAttribute( Qt::WA_DeleteOnClose ); w->setAttribute( Qt::WA_DeleteOnClose );
w->setConfig(cc); w->setConfig(cc);
w->show(); w->show();
@ -120,7 +146,7 @@ void MasterController::openServerWindowForConnection(size_t connection_index)
} }
bool MasterController::retrieveConnectionPassword(ConnectionConfig &cc) bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
{ {
auto pw_state = cc.passwordState(); auto pw_state = cc.passwordState();
if (pw_state == PasswordState::NotNeeded) { if (pw_state == PasswordState::NotNeeded) {
@ -155,7 +181,7 @@ bool MasterController::retrieveConnectionPassword(ConnectionConfig &cc)
} }
bool MasterController::getPasswordFromPskdb(const std::string &password_id, std::string &password) bool ConnectionController::getPasswordFromPskdb(const std::string &password_id, std::string &password)
{ {
if (!UnlockPasswordManagerIfNeeded()) if (!UnlockPasswordManagerIfNeeded())
return false; return false;
@ -164,7 +190,7 @@ bool MasterController::getPasswordFromPskdb(const std::string &password_id, std:
} }
bool MasterController::storePasswordInPskdb(const std::string &password_id, const std::string password) bool ConnectionController::storePasswordInPskdb(const std::string &password_id, const std::string password)
{ {
if (!UnlockPasswordManagerIfNeeded()) if (!UnlockPasswordManagerIfNeeded())
return false; return false;
@ -173,9 +199,10 @@ bool MasterController::storePasswordInPskdb(const std::string &password_id, cons
return true; return true;
} }
bool MasterController::UnlockPasswordManagerIfNeeded() bool ConnectionController::UnlockPasswordManagerIfNeeded()
{ {
if (m_passwordManager->initialized(m_userConfigDatabase)) { auto&& user_cfg_db = m_masterController->userConfigDatabase();
if (m_passwordManager->initialized(user_cfg_db)) {
if (!m_passwordManager->locked()) if (!m_passwordManager->locked())
return true; return true;
@ -192,7 +219,7 @@ bool MasterController::UnlockPasswordManagerIfNeeded()
break; break;
} }
// user gave OK, if succeeds return true otherwise loop a prompt for password again. // user gave OK, if succeeds return true otherwise loop a prompt for password again.
if (m_passwordManager->openDatabase(m_userConfigDatabase, dlg->password())) if (m_passwordManager->openDatabase(user_cfg_db, dlg->password()))
return true; return true;
} }
} }
@ -207,22 +234,19 @@ bool MasterController::UnlockPasswordManagerIfNeeded()
int exec_result = dlg->exec(); int exec_result = dlg->exec();
if (exec_result == QDialog::Accepted) { if (exec_result == QDialog::Accepted) {
QString passphrase = dlg->password(); QString passphrase = dlg->password();
if (m_passwordManager->createDatabase(m_userConfigDatabase, passphrase)) if (m_passwordManager->createDatabase(user_cfg_db, passphrase))
return true; return true;
} }
} }
return false; return false;
} }
std::string MasterController::getPskId(const ConnectionConfig &cc) std::string ConnectionController::getPskId(const ConnectionConfig &cc)
{ {
std::string id = "dbpw/"; std::string id = "dbpw/";
id += cc.uuid().toString().toUtf8().data(); id += cc.uuid().toString().toUtf8().data();
return id; return id;
} }
//std::shared_ptr<Botan::Sqlite3_Database> MasterController::getUserConfigDatabase()
//{
// return m_userConfigDatabase;
//}

View file

@ -18,6 +18,7 @@ class ConnectionListModel;
class ConnectionManagerWindow; class ConnectionManagerWindow;
class PasswordManager; class PasswordManager;
class ConnectionController;
/** \brief Controller class responsible for all things global. /** \brief Controller class responsible for all things global.
*/ */
class MasterController : public QObject { class MasterController : public QObject {
@ -30,6 +31,26 @@ public:
void init(); void init();
ConnectionController* connectionController();
QSqlDatabase& userConfigDatabase();
signals:
public slots:
private:
QSqlDatabase m_userConfigDatabase;
ConnectionController* m_connectionController = nullptr;
};
class ConnectionController : public QObject {
Q_OBJECT
public:
explicit ConnectionController(MasterController *parent = nullptr);
~ConnectionController();
void init();
ConnectionListModel *getConnectionListModel() ConnectionListModel *getConnectionListModel()
{ {
return m_connectionListModel; return m_connectionListModel;
@ -40,18 +61,12 @@ public:
void openServerWindowForConnection(size_t connection_index); void openServerWindowForConnection(size_t connection_index);
void openBackupDlgForConnection(size_t connection_index); void openBackupDlgForConnection(size_t connection_index);
// std::shared_ptr<Botan::Sqlite3_Database> getUserConfigDatabase();
signals:
public slots:
private: private:
MasterController *m_masterController;
ConnectionList *m_connectionList = nullptr; ConnectionList *m_connectionList = nullptr;
ConnectionListModel *m_connectionListModel = nullptr; ConnectionListModel *m_connectionListModel = nullptr;
ConnectionManagerWindow *m_connectionManagerWindow = nullptr; ConnectionManagerWindow *m_connectionManagerWindow = nullptr;
//std::shared_ptr<Botan::Sqlite3_Database> m_userConfigDatabase;
QSqlDatabase m_userConfigDatabase;
/** Using long lived object so it can remember its master password for sometime /** Using long lived object so it can remember its master password for sometime
* if the user wishes it. * if the user wishes it.
*/ */

View file

@ -562,13 +562,12 @@ void QueryTool::copyQueryAsRawCppString()
void QueryTool::generateCode() void QueryTool::generateCode()
{ {
QString command = getCommand(); QString command = getCommand();
if (resultList.empty()) { if (resultList.empty()) {
QMessageBox::question(this, "pglab", tr("Please execute the query first"), QMessageBox::Ok); QMessageBox::question(this, "pglab", tr("Please execute the query first"), QMessageBox::Ok);
} }
if (resultList.size() == 1) { if (resultList.size() == 1) {
std::shared_ptr<const Pgsql::Result> dbres = resultList[0]->GetPgsqlResult(); std::shared_ptr<const Pgsql::Result> dbres = resultList[0]->GetPgsqlResult();
//context()->newCodeGenPage(command, dbres); m_context->newCodeGenPage(command, dbres);
} }
} }