pgLab/pglab/ConnectionManagerWindow.cpp
eelke 2230a4bd61 Lot of password related changes all over the place.
Password is no longer saved with the connection list.
Password is not entered along with other connection credentials.
Password is now asked for when required.
Still working on saving the password and auto retrieving it from the password manager.
2018-11-04 11:44:40 +01:00

143 lines
3.6 KiB
C++

#include "ConnectionManagerWindow.h"
#include "ui_ConnectionManagerWindow.h"
//#include "mainwindow.h"
#include "MasterController.h"
#include <QDataWidgetMapper>
#include <QMessageBox>
#include <QStandardItemModel>
#include "ConnectionListModel.h"
#include <QDir>
#include <QStandardPaths>
QString pskFileName()
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(".");
}
path += "/psk.ini";
return path;
}
ConnectionManagerWindow::ConnectionManagerWindow(MasterController *master, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ConnectionManagerWindow)
// , m_listModel(new ConnectionListModel(this))
, m_masterController(master)
{
ui->setupUi(this);
ui->listView->setModel(m_masterController->getConnectionListModel());
setupWidgetMappings();
connect(ui->listView->selectionModel(),
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(on_currentChanged(QModelIndex,QModelIndex)));
}
ConnectionManagerWindow::~ConnectionManagerWindow()
{
// m_listModel->save();
delete ui;
// delete m_listModel;
delete m_mapper;
}
void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
{
// ConnectionConfig c;
// c.setName("new");
//m_listModel->add(c);
auto clm = m_masterController->getConnectionListModel();
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 &current,
const QModelIndex &)
{
int currow = current.row();
auto clm = m_masterController->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()
{
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();
clm->removeRow(ci.row());
}
}
}
void ConnectionManagerWindow::setupWidgetMappings()
{
auto clm = m_masterController->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()
{
auto ci = ui->listView->selectionModel()->currentIndex();
if (ci.isValid()) {
auto r = static_cast<size_t>(ci.row());
m_masterController->openSqlWindowForConnection(r);
}
else {
// TODO can we give unobtrusive message why it didn't work?
}
}
void ConnectionManagerWindow::on_actionQuit_application_triggered()
{
auto res = QMessageBox::question(this, "pglab",
tr("Close ALL windows?"), QMessageBox::Yes, QMessageBox::No);
if (res == QMessageBox::Yes) {
QApplication::quit();
}
//closeAllWindows();
}
void ConnectionManagerWindow::on_actionBackup_database_triggered()
{
auto ci = ui->listView->selectionModel()->currentIndex();
m_masterController->openBackupDlgForConnection(ci.row());
}
void ConnectionManagerWindow::on_actionManage_server_triggered()
{
auto ci = ui->listView->selectionModel()->currentIndex();
m_masterController->openServerWindowForConnection(ci.row());
}