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

@ -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<Botan::Sqlite3_Database>(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<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(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<Botan::Sqlite3_Database> MasterController::getUserConfigDatabase()
//{
// return m_userConfigDatabase;
//}