pgLab/pglab/MasterController.cpp

137 lines
3.3 KiB
C++
Raw Normal View History

#include "MasterController.h"
#include "ConnectionManagerWindow.h"
#include "ConnectionList.h"
#include "ConnectionListModel.h"
2017-02-06 21:41:45 +01:00
#include "MainWindow.h"
#include "ServerWindow.h"
#include "BackupDialog.h"
#include "PasswordPromptDialog.h"
MasterController::MasterController(QObject *parent) : QObject(parent)
{}
MasterController::~MasterController()
{
delete m_connectionManagerWindow;
delete m_connectionListModel;
delete m_connectionList;
}
void MasterController::init()
{
m_connectionList = new ConnectionList;
m_connectionList->load();
m_connectionListModel = new ConnectionListModel(m_connectionList, this);
m_connectionManagerWindow = new ConnectionManagerWindow(this, nullptr);
m_connectionManagerWindow->show();
}
void MasterController::showConnectionManager()
{
m_connectionManagerWindow->show();
}
void MasterController::openSqlWindowForConnection(size_t connection_index)
{
auto res = m_connectionListModel->get(connection_index);
if (res.valid()) {
auto cc = res.get();
m_connectionListModel->save(connection_index, cc);
if (retrieveConnectionPassword(cc)) {
// TODO instead of directly openening the mainwindow
// do async connect and only open window when we have
// working connection
auto w = new MainWindow(this, nullptr);
w->setAttribute( Qt::WA_DeleteOnClose );
w->setConfig(cc);
w->show();
}
}
}
void MasterController::openBackupDlgForConnection(int connection_index)
{
auto res = m_connectionListModel->get(connection_index);
if (res.valid()) {
auto cc = res.get();
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 MasterController::openServerWindowForConnection(int connection_index)
{
auto res = m_connectionListModel->get(connection_index);
if (res.valid()) {
auto cc = res.get();
retrieveConnectionPassword(cc);
m_connectionListModel->save(connection_index, cc);
auto w = new ServerWindow(this, nullptr);
w->setAttribute( Qt::WA_DeleteOnClose );
w->setConfig(cc);
w->show();
}
}
bool MasterController::retrieveConnectionPassword(ConnectionConfig &cc)
{
// Look at config
// - is password required, how do we know?
// - IF is password stored in pskdb
// - ask pskdb for password
// - ELSE
// - ask user for password
QString str = ConnectionListModel::makeLongDescription(cc);
auto dlg = std::make_unique<PasswordPromptDialog>(nullptr);
dlg->setConnectionDescription(str);
int exec_result = dlg->exec();
if (exec_result == QDialog::Accepted) {
cc.setPassword(dlg->password().toUtf8().data());
// - IF user checked remember password
// - ask pskdb to store password
return true;
}
return false;
}
bool MasterController::getPasswordFromPskdb(const std::string &password_id, std::string &password)
{
// func: getPasswordFromPskdb
// IF pskdb locked
// prompt user for pskdb passphrase
// unlock pskdb
// get pwd
return false;
}
bool MasterController::storePasswordInPskdb(const std::string &password_id, const std::string password)
{
// func: storePasswordInPskdb
// IF pskdb not setup
// notify user and ask for passphrase
// init pskdb
// ELSE
// IF pskdb locked
// ask for passphrase
// unlock
// store pwd
return false;
}