The list of connections is now saved and loaded on program shutdown and start.
This commit is contained in:
parent
cf4d6e769b
commit
7181c7f1e7
8 changed files with 128 additions and 44 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
QT += core gui
|
QT += core gui
|
||||||
|
|
||||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets sql
|
||||||
|
|
||||||
TARGET = Ivory
|
TARGET = Ivory
|
||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
|
|
@ -33,7 +33,7 @@ SOURCES += main.cpp\
|
||||||
databasewindow.cpp \
|
databasewindow.cpp \
|
||||||
connectionmanagerwindow.cpp \
|
connectionmanagerwindow.cpp \
|
||||||
connectionlistmodel.cpp \
|
connectionlistmodel.cpp \
|
||||||
connectionconfig.cpp
|
connectionconfig.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
serverproperties.h \
|
serverproperties.h \
|
||||||
|
|
@ -51,7 +51,9 @@ HEADERS += mainwindow.h \
|
||||||
databasewindow.h \
|
databasewindow.h \
|
||||||
connectionmanagerwindow.h \
|
connectionmanagerwindow.h \
|
||||||
connectionlistmodel.h \
|
connectionlistmodel.h \
|
||||||
connectionconfig.h
|
connectionconfig.h \
|
||||||
|
scopeguard.h \
|
||||||
|
expected.h
|
||||||
|
|
||||||
FORMS += mainwindow.ui \
|
FORMS += mainwindow.ui \
|
||||||
serverproperties.ui \
|
serverproperties.ui \
|
||||||
|
|
|
||||||
|
|
@ -56,14 +56,14 @@ ConnectionConfig::ConnectionConfig()
|
||||||
, m_values(s_keywords.size(), nullptr)
|
, m_values(s_keywords.size(), nullptr)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void ConnectionConfig::setDescription(std::string desc)
|
void ConnectionConfig::setName(std::string desc)
|
||||||
{
|
{
|
||||||
m_description = std::move(desc);
|
m_name = std::move(desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& ConnectionConfig::description() const
|
const std::string& ConnectionConfig::name() const
|
||||||
{
|
{
|
||||||
return m_description;
|
return m_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionConfig::setHost(std::string host)
|
void ConnectionConfig::setHost(std::string host)
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ class ConnectionConfig {
|
||||||
public:
|
public:
|
||||||
ConnectionConfig();
|
ConnectionConfig();
|
||||||
|
|
||||||
void setDescription(std::string desc);
|
void setName(std::string desc);
|
||||||
const std::string& description() const;
|
const std::string& name() const;
|
||||||
|
|
||||||
void setHost(std::string host);
|
void setHost(std::string host);
|
||||||
const std::string& host() const;
|
const std::string& host() const;
|
||||||
|
|
@ -57,7 +57,7 @@ public:
|
||||||
const char * const * getValues() const;
|
const char * const * getValues() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_description;
|
std::string m_name;
|
||||||
std::string m_host;
|
std::string m_host;
|
||||||
std::string m_hostaddr;
|
std::string m_hostaddr;
|
||||||
std::string m_port = "5432";
|
std::string m_port = "5432";
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#include "connectionlistmodel.h"
|
#include "connectionlistmodel.h"
|
||||||
|
#include <QDir>
|
||||||
|
#include <QStandardPaths>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
#include "scopeguard.h"
|
||||||
|
|
||||||
inline QString stdStrToQ(const std::string &s)
|
inline QString stdStrToQ(const std::string &s)
|
||||||
{
|
{
|
||||||
|
|
@ -24,7 +26,7 @@ Before calling this you may want to call beginGroup.
|
||||||
*/
|
*/
|
||||||
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
||||||
{
|
{
|
||||||
settings.setValue("description", stdStrToQ(cc.description()));
|
settings.setValue("name", stdStrToQ(cc.name()));
|
||||||
settings.setValue("host", stdStrToQ(cc.host()));
|
settings.setValue("host", stdStrToQ(cc.host()));
|
||||||
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
||||||
settings.setValue("port", cc.port());
|
settings.setValue("port", cc.port());
|
||||||
|
|
@ -40,7 +42,7 @@ void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
||||||
|
|
||||||
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
||||||
{
|
{
|
||||||
cc.setDescription(qvarToStdStr(settings.value("description")));
|
cc.setName(qvarToStdStr(settings.value("name")));
|
||||||
cc.setHost(qvarToStdStr(settings.value("host")));
|
cc.setHost(qvarToStdStr(settings.value("host")));
|
||||||
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
||||||
cc.setPort(settings.value("port", 5432).toInt());
|
cc.setPort(settings.value("port", 5432).toInt());
|
||||||
|
|
@ -58,7 +60,9 @@ void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
||||||
|
|
||||||
ConnectionListModel::ConnectionListModel(QObject *parent)
|
ConnectionListModel::ConnectionListModel(QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
{}
|
{
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
|
||||||
int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
|
|
@ -67,7 +71,7 @@ int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
||||||
|
|
||||||
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
|
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
|
||||||
{
|
{
|
||||||
return 4;
|
return 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
||||||
|
|
@ -76,13 +80,13 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
||||||
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
||||||
int row = index.row();
|
int row = index.row();
|
||||||
int col = index.column();
|
int col = index.column();
|
||||||
const ConnectionConfig& cfg = m_connections.at(row);
|
const ConnectionConfig& cfg = m_connections.at(row).m_config;
|
||||||
switch (col) {
|
switch (col) {
|
||||||
case 0:
|
case 0:
|
||||||
result = makeLongDescription(cfg);
|
result = makeLongDescription(cfg);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
result = stdStrToQ(cfg.description());
|
result = stdStrToQ(cfg.name());
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
result = stdStrToQ(cfg.host());
|
result = stdStrToQ(cfg.host());
|
||||||
|
|
@ -90,7 +94,15 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
||||||
case 3:
|
case 3:
|
||||||
result = cfg.port();
|
result = cfg.port();
|
||||||
break;
|
break;
|
||||||
|
case 4:
|
||||||
|
result = stdStrToQ(cfg.user());
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
result = stdStrToQ(cfg.password());
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
result = stdStrToQ(cfg.dbname());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,29 +111,41 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
||||||
|
|
||||||
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
|
bool result = false;
|
||||||
if (role == Qt::EditRole) {
|
if (role == Qt::EditRole) {
|
||||||
int row = index.row();
|
int row = index.row();
|
||||||
int col = index.column();
|
int col = index.column();
|
||||||
ConnectionConfig& cfg = m_connections.at(row);
|
ConnectionConfig& cfg = m_connections.at(row).m_config;
|
||||||
|
if (col > 0) {
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
switch (col) {
|
switch (col) {
|
||||||
case 0:
|
case 0:
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
cfg.setDescription( qStrToStd(value.toString()) );
|
cfg.setName( qStrToStd(value.toString()) );
|
||||||
emit dataChanged(index, index);
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cfg.setHost( qStrToStd(value.toString()) );
|
cfg.setHost( qStrToStd(value.toString()) );
|
||||||
emit dataChanged(index, index);
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
cfg.setPort( value.toInt() );
|
cfg.setPort( value.toInt() );
|
||||||
emit dataChanged(index, index);
|
break;
|
||||||
|
case 4:
|
||||||
|
cfg.setUser( qStrToStd(value.toString()) );
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
|
cfg.setPassword( qStrToStd(value.toString()) );
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
cfg.setDbname( qStrToStd(value.toString()) );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
if (result) {
|
||||||
|
emit dataChanged(index, index);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
||||||
|
|
@ -132,7 +156,7 @@ Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
||||||
|
|
||||||
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
||||||
{
|
{
|
||||||
std::string result(cfg.description());
|
std::string result(cfg.name());
|
||||||
result += " (";
|
result += " (";
|
||||||
result += cfg.user();
|
result += cfg.user();
|
||||||
result += "@";
|
result += "@";
|
||||||
|
|
@ -147,8 +171,50 @@ QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
||||||
|
|
||||||
void ConnectionListModel::add(const ConnectionConfig &cfg)
|
void ConnectionListModel::add(const ConnectionConfig &cfg)
|
||||||
{
|
{
|
||||||
m_connections.push_back(cfg);
|
m_connections.emplace_back(QUuid::createUuid(), cfg);
|
||||||
auto idx = createIndex(m_connections.size()-1, 0);
|
auto idx = createIndex(m_connections.size()-1, 0);
|
||||||
emit dataChanged(idx, idx);
|
emit dataChanged(idx, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \todo should return an expected as creation of the folder can fail
|
||||||
|
QString ConnectionListModel::iniFileName()
|
||||||
|
{
|
||||||
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||||
|
QDir dir(path);
|
||||||
|
if (!dir.exists()) {
|
||||||
|
dir.mkpath(".");
|
||||||
|
}
|
||||||
|
path += "/connections.ini";
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ConnectionListModel::load()
|
||||||
|
{
|
||||||
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
auto groups = settings.childGroups();
|
||||||
|
for (auto grp : groups) {
|
||||||
|
QUuid uuid(grp);
|
||||||
|
if ( ! uuid.isNull() ) {
|
||||||
|
settings.beginGroup(grp);
|
||||||
|
SCOPE_EXIT { settings.endGroup(); };
|
||||||
|
|
||||||
|
ConnectionConfig cc;
|
||||||
|
LoadConnectionConfig(settings, cc);
|
||||||
|
m_connections.emplace_back(uuid, cc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConnectionListModel::save()
|
||||||
|
{
|
||||||
|
QString file_name = iniFileName();
|
||||||
|
QSettings settings(file_name, QSettings::IniFormat);
|
||||||
|
for (auto e : m_connections) {
|
||||||
|
settings.beginGroup(e.m_uuid.toString());
|
||||||
|
SCOPE_EXIT { settings.endGroup(); };
|
||||||
|
|
||||||
|
SaveConnectionConfig(settings, e.m_config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
|
#include <QUuid>
|
||||||
|
|
||||||
#include "connectionconfig.h"
|
#include "connectionconfig.h"
|
||||||
|
|
||||||
|
|
@ -23,10 +24,24 @@ public:
|
||||||
|
|
||||||
void add(const ConnectionConfig &cfg);
|
void add(const ConnectionConfig &cfg);
|
||||||
|
|
||||||
|
void load();
|
||||||
|
void save();
|
||||||
private:
|
private:
|
||||||
using t_Connections = std::vector<ConnectionConfig>;
|
class LijstElem {
|
||||||
|
public:
|
||||||
|
QUuid m_uuid;
|
||||||
|
ConnectionConfig m_config;
|
||||||
|
|
||||||
|
LijstElem(const QUuid id, const ConnectionConfig &cfg)
|
||||||
|
: m_uuid(id), m_config(cfg)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
using t_Connections = std::vector<LijstElem>;
|
||||||
t_Connections m_connections;
|
t_Connections m_connections;
|
||||||
|
|
||||||
|
static QString iniFileName();
|
||||||
|
|
||||||
static QString makeLongDescription(const ConnectionConfig &cfg);
|
static QString makeLongDescription(const ConnectionConfig &cfg);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,18 +12,9 @@ ConnectionManagerWindow::ConnectionManagerWindow(QWidget *parent)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
ConnectionConfig c;
|
|
||||||
c.setDescription("test");
|
|
||||||
m_listModel->add(c);
|
|
||||||
|
|
||||||
ui->listView->setModel(m_listModel);
|
ui->listView->setModel(m_listModel);
|
||||||
|
|
||||||
m_mapper = new QDataWidgetMapper(this);
|
setupWidgetMappings();
|
||||||
m_mapper->setModel(m_listModel);
|
|
||||||
m_mapper->addMapping(ui->edtName, 1);
|
|
||||||
m_mapper->addMapping(ui->edtHost, 2);
|
|
||||||
m_mapper->addMapping(ui->spinPort, 3);
|
|
||||||
m_mapper->toFirst();
|
|
||||||
|
|
||||||
connect(ui->listView->selectionModel(),
|
connect(ui->listView->selectionModel(),
|
||||||
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
||||||
|
|
@ -33,6 +24,8 @@ ConnectionManagerWindow::ConnectionManagerWindow(QWidget *parent)
|
||||||
|
|
||||||
ConnectionManagerWindow::~ConnectionManagerWindow()
|
ConnectionManagerWindow::~ConnectionManagerWindow()
|
||||||
{
|
{
|
||||||
|
m_listModel->save();
|
||||||
|
|
||||||
delete ui;
|
delete ui;
|
||||||
delete m_listModel;
|
delete m_listModel;
|
||||||
delete m_mapper;
|
delete m_mapper;
|
||||||
|
|
@ -41,7 +34,7 @@ ConnectionManagerWindow::~ConnectionManagerWindow()
|
||||||
void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
|
void ConnectionManagerWindow::on_actionAdd_Connection_triggered()
|
||||||
{
|
{
|
||||||
ConnectionConfig c;
|
ConnectionConfig c;
|
||||||
c.setDescription("new");
|
c.setName("new");
|
||||||
m_listModel->add(c);
|
m_listModel->add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,7 +64,15 @@ void ConnectionManagerWindow::on_actionDelete_connection_triggered()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ConnectionManagerWindow::controlsToConfig(ConnectionConfig &cfg)
|
void ConnectionManagerWindow::setupWidgetMappings()
|
||||||
{
|
{
|
||||||
// ui->
|
m_mapper = new QDataWidgetMapper(this);
|
||||||
|
m_mapper->setModel(m_listModel);
|
||||||
|
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->edtPassword, 5);
|
||||||
|
m_mapper->addMapping(ui->edtDbname, 6);
|
||||||
|
m_mapper->toFirst();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ private:
|
||||||
ConnectionListModel *m_listModel = nullptr;
|
ConnectionListModel *m_listModel = nullptr;
|
||||||
QDataWidgetMapper *m_mapper = nullptr;
|
QDataWidgetMapper *m_mapper = nullptr;
|
||||||
|
|
||||||
void controlsToConfig(ConnectionConfig &cfg);
|
void setupWidgetMappings();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONNECTIONMANAGERWINDOW_H
|
#endif // CONNECTIONMANAGERWINDOW_H
|
||||||
|
|
|
||||||
4
main.cpp
4
main.cpp
|
|
@ -20,9 +20,9 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
QCoreApplication::setOrganizationName("MySoft");
|
QCoreApplication::setOrganizationName("pglab");
|
||||||
QCoreApplication::setOrganizationDomain("eelkeklein.nl");
|
QCoreApplication::setOrganizationDomain("eelkeklein.nl");
|
||||||
QCoreApplication::setApplicationName("Ivory");
|
QCoreApplication::setApplicationName("pglab");
|
||||||
|
|
||||||
ConnectionManagerWindow w;
|
ConnectionManagerWindow w;
|
||||||
w.show();
|
w.show();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue