diff --git a/pglab/ConnectionManagerWindow.cpp b/pglab/ConnectionManagerWindow.cpp index 42c2e03..985bd09 100644 --- a/pglab/ConnectionManagerWindow.cpp +++ b/pglab/ConnectionManagerWindow.cpp @@ -27,10 +27,10 @@ ConnectionManagerWindow::ConnectionManagerWindow(MasterController *master, QWidg , ui(new Ui::ConnectionManagerWindow) // , m_listModel(new ConnectionListModel(this)) , m_masterController(master) + , m_connectionController(master->connectionController()) { ui->setupUi(this); - - ui->listView->setModel(m_masterController->getConnectionListModel()); + ui->listView->setModel(m_connectionController->getConnectionListModel()); setupWidgetMappings(); @@ -55,7 +55,7 @@ void ConnectionManagerWindow::on_actionAdd_Connection_triggered() // c.setName("new"); //m_listModel->add(c); - auto clm = m_masterController->getConnectionListModel(); + auto clm = m_connectionController->getConnectionListModel(); clm->newItem(); // Select the new row @@ -67,7 +67,7 @@ void ConnectionManagerWindow::on_currentChanged(const QModelIndex ¤t, const QModelIndex &) { int currow = current.row(); - auto clm = m_masterController->getConnectionListModel(); + auto clm = m_connectionController->getConnectionListModel(); if (prevSelection) clm->save(*prevSelection); m_mapper->setCurrentIndex(currow); @@ -81,11 +81,10 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered() { auto ci = ui->listView->selectionModel()->currentIndex(); if (ci.isValid()) { - auto res = QMessageBox::question(this, "pglab", tr("Are you sure you want to remove this connection?"), QMessageBox::Yes, QMessageBox::No); if (res == QMessageBox::Yes) { - auto clm = m_masterController->getConnectionListModel(); + auto clm = m_connectionController->getConnectionListModel(); clm->removeRow(ci.row()); } } @@ -94,7 +93,7 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered() void ConnectionManagerWindow::setupWidgetMappings() { - auto clm = m_masterController->getConnectionListModel(); + auto clm = m_connectionController->getConnectionListModel(); m_mapper = new QDataWidgetMapper(this); m_mapper->setModel(clm); m_mapper->addMapping(ui->edtName, 1); @@ -110,7 +109,7 @@ void ConnectionManagerWindow::on_actionConnect_triggered() auto ci = ui->listView->selectionModel()->currentIndex(); if (ci.isValid()) { auto r = static_cast(ci.row()); - m_masterController->openSqlWindowForConnection(r); + m_connectionController->openSqlWindowForConnection(r); } else { // 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() { auto ci = ui->listView->selectionModel()->currentIndex(); - m_masterController->openBackupDlgForConnection(ci.row()); + m_connectionController->openBackupDlgForConnection(ci.row()); } void ConnectionManagerWindow::on_actionManage_server_triggered() { auto ci = ui->listView->selectionModel()->currentIndex(); - m_masterController->openServerWindowForConnection(ci.row()); + m_connectionController->openServerWindowForConnection(ci.row()); } diff --git a/pglab/ConnectionManagerWindow.h b/pglab/ConnectionManagerWindow.h index 0bdf9ad..1beff07 100644 --- a/pglab/ConnectionManagerWindow.h +++ b/pglab/ConnectionManagerWindow.h @@ -9,6 +9,7 @@ class ConnectionManagerWindow; } class ConnectionConfig; +class ConnectionController; class MasterController; class QDataWidgetMapper; class QStandardItemModel; @@ -35,6 +36,7 @@ private: Ui::ConnectionManagerWindow *ui; QDataWidgetMapper *m_mapper = nullptr; MasterController *m_masterController; + ConnectionController *m_connectionController; std::optional prevSelection; diff --git a/pglab/DatabaseWindow.cpp b/pglab/DatabaseWindow.cpp index 6e17070..0993d04 100644 --- a/pglab/DatabaseWindow.cpp +++ b/pglab/DatabaseWindow.cpp @@ -76,12 +76,9 @@ void DatabaseWindow::newCrudPage(Oid tableoid) void DatabaseWindow::newCodeGenPage(QString query, std::shared_ptr dbres) { - // TODO should this call be this direct or should it go through module system - // yes it should otherwise context cannot properly setup toolbar and menu!!! -// auto cgtab = new CodeGenerator(context(), pluginModule(), this); -// cgtab->Init(m_database->catalog(), query, dbres); -// addPage(cgtab, "Codegen"); - // + auto cgtab = new CodeGenerator(this); + cgtab->Init(m_database->catalog(), query, dbres); + addPage(cgtab, "Codegen"); } QueryTool *DatabaseWindow::GetActiveQueryTool() @@ -325,12 +322,11 @@ void DatabaseWindow::catalogLoaded() try { m_database = loadWatcher.future().result(); - for (auto f : { "user", "pg_catalog", "information_schema" }) { - // TODO open inspector windows - } - - - newCreateTablePage(); +// for (auto f : { "user", "pg_catalog", "information_schema" }) { +// // TODO open inspector windows +// } +// newCreateTablePage(); + on_actionNewSql_triggered(); } catch (const OpenDatabaseException &ex) { QMessageBox::critical(this, "Error reading database", ex.text()); close(); @@ -444,6 +440,7 @@ void DatabaseWindow::on_actionGenerateCode_triggered() auto query_tool = GetActiveQueryTool(); if (query_tool) { query_tool->generateCode(); + //newCodeGenPage } } @@ -511,7 +508,7 @@ void DatabaseWindow::on_actionSaveCopyOfSqlAs_triggered() void DatabaseWindow::on_actionShowConnectionManager_triggered() { - m_masterController->showConnectionManager(); + m_masterController->connectionController()->showConnectionManager(); } void DatabaseWindow::on_m_tabWidget_tabCloseRequested(int index) diff --git a/pglab/DatabaseWindow.h b/pglab/DatabaseWindow.h index a3bbb14..b7f62cc 100644 --- a/pglab/DatabaseWindow.h +++ b/pglab/DatabaseWindow.h @@ -45,7 +45,7 @@ public: /// Called when a newly created page is added to the QTabWidget void addPage(QWidget* page, QString caption); - void newCodeGenPage(QString query, std::shared_ptr dbres); + virtual void newCodeGenPage(QString query, std::shared_ptr dbres) override; QueryTool *GetActiveQueryTool(); private: diff --git a/pglab/IDatabaseWindow.h b/pglab/IDatabaseWindow.h index 550b411..8653c53 100644 --- a/pglab/IDatabaseWindow.h +++ b/pglab/IDatabaseWindow.h @@ -3,6 +3,11 @@ #include #include +#include + +namespace Pgsql { + class Result; +} class OpenDatabase; class QWidget; @@ -15,6 +20,7 @@ public: virtual void setIconForWidget(QWidget* widget, QIcon icon) = 0; virtual std::shared_ptr openDatabase() = 0; virtual void showStatusBarMessage(QString message) = 0; + virtual void newCodeGenPage(QString query, std::shared_ptr dbres) = 0; }; #endif // IDATABASEWINDOW_H diff --git a/pglab/MasterController.cpp b/pglab/MasterController.cpp index 8576801..df2fd88 100644 --- a/pglab/MasterController.cpp +++ b/pglab/MasterController.cpp @@ -31,17 +31,10 @@ MasterController::MasterController(QObject *parent) : QObject(parent) {} MasterController::~MasterController() -{ - delete m_connectionManagerWindow; - delete m_connectionListModel; - delete m_connectionList; -} +{} void MasterController::init() { - //std::string dbfilename = QDir::toNativeSeparators(GetUserConfigDatabaseName()).toUtf8().data(); - //m_userConfigDatabase = std::make_shared(dbfilename); - m_userConfigDatabase = QSqlDatabase::addDatabase("QSQLITE"); m_userConfigDatabase.setDatabaseName(GetUserConfigDatabaseName()); @@ -52,23 +45,56 @@ void MasterController::init() 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(dbfilename); + + m_passwordManager = std::make_shared(); m_connectionList = new ConnectionList; m_connectionList->load(); m_connectionListModel = new ConnectionListModel(m_connectionList, this); - m_connectionManagerWindow = new ConnectionManagerWindow(this, nullptr); + m_connectionManagerWindow = new ConnectionManagerWindow(m_masterController, nullptr); m_connectionManagerWindow->show(); } -void MasterController::showConnectionManager() +void ConnectionController::showConnectionManager() { m_connectionManagerWindow->show(); } -void MasterController::openSqlWindowForConnection(size_t connection_index) +void ConnectionController::openSqlWindowForConnection(size_t 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 // do async connect and only open window when we have // working connection - auto w = new DatabaseWindow(this, nullptr); + auto w = new DatabaseWindow(m_masterController, nullptr); w->setAttribute( Qt::WA_DeleteOnClose ); w->setConfig(cc); 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); 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); if (res.valid()) { auto cc = res.get(); if (retrieveConnectionPassword(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->setConfig(cc); 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(); 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()) 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()) return false; @@ -173,9 +199,10 @@ bool MasterController::storePasswordInPskdb(const std::string &password_id, cons 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()) return true; @@ -192,7 +219,7 @@ bool MasterController::UnlockPasswordManagerIfNeeded() break; } // 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; } } @@ -207,22 +234,19 @@ bool MasterController::UnlockPasswordManagerIfNeeded() int exec_result = dlg->exec(); if (exec_result == QDialog::Accepted) { QString passphrase = dlg->password(); - if (m_passwordManager->createDatabase(m_userConfigDatabase, passphrase)) + if (m_passwordManager->createDatabase(user_cfg_db, passphrase)) return true; } } return false; } -std::string MasterController::getPskId(const ConnectionConfig &cc) +std::string ConnectionController::getPskId(const ConnectionConfig &cc) { std::string id = "dbpw/"; id += cc.uuid().toString().toUtf8().data(); return id; } -//std::shared_ptr MasterController::getUserConfigDatabase() -//{ -// return m_userConfigDatabase; -//} + diff --git a/pglab/MasterController.h b/pglab/MasterController.h index 3034203..888d783 100644 --- a/pglab/MasterController.h +++ b/pglab/MasterController.h @@ -18,6 +18,7 @@ class ConnectionListModel; class ConnectionManagerWindow; class PasswordManager; +class ConnectionController; /** \brief Controller class responsible for all things global. */ class MasterController : public QObject { @@ -30,6 +31,26 @@ public: 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() { return m_connectionListModel; @@ -40,18 +61,12 @@ public: void openServerWindowForConnection(size_t connection_index); void openBackupDlgForConnection(size_t connection_index); -// std::shared_ptr getUserConfigDatabase(); - -signals: - -public slots: - private: + MasterController *m_masterController; ConnectionList *m_connectionList = nullptr; ConnectionListModel *m_connectionListModel = nullptr; ConnectionManagerWindow *m_connectionManagerWindow = nullptr; - //std::shared_ptr m_userConfigDatabase; - QSqlDatabase m_userConfigDatabase; + /** Using long lived object so it can remember its master password for sometime * if the user wishes it. */ diff --git a/pglab/QueryTool.cpp b/pglab/QueryTool.cpp index 16a8e67..eb2b0c3 100644 --- a/pglab/QueryTool.cpp +++ b/pglab/QueryTool.cpp @@ -562,13 +562,12 @@ void QueryTool::copyQueryAsRawCppString() void QueryTool::generateCode() { QString command = getCommand(); - if (resultList.empty()) { QMessageBox::question(this, "pglab", tr("Please execute the query first"), QMessageBox::Ok); } if (resultList.size() == 1) { std::shared_ptr dbres = resultList[0]->GetPgsqlResult(); - //context()->newCodeGenPage(command, dbres); + m_context->newCodeGenPage(command, dbres); } }