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
|
|
@ -1,6 +1,8 @@
|
|||
#include "connectionlistmodel.h"
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
|
||||
#include "scopeguard.h"
|
||||
|
||||
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)
|
||||
{
|
||||
settings.setValue("description", stdStrToQ(cc.description()));
|
||||
settings.setValue("name", stdStrToQ(cc.name()));
|
||||
settings.setValue("host", stdStrToQ(cc.host()));
|
||||
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
||||
settings.setValue("port", cc.port());
|
||||
|
|
@ -40,7 +42,7 @@ void SaveConnectionConfig(QSettings &settings, const 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.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
||||
cc.setPort(settings.value("port", 5432).toInt());
|
||||
|
|
@ -58,7 +60,9 @@ void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
|||
|
||||
ConnectionListModel::ConnectionListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{}
|
||||
{
|
||||
load();
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
return 4;
|
||||
return 7;
|
||||
}
|
||||
|
||||
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) {
|
||||
int row = index.row();
|
||||
int col = index.column();
|
||||
const ConnectionConfig& cfg = m_connections.at(row);
|
||||
const ConnectionConfig& cfg = m_connections.at(row).m_config;
|
||||
switch (col) {
|
||||
case 0:
|
||||
result = makeLongDescription(cfg);
|
||||
break;
|
||||
case 1:
|
||||
result = stdStrToQ(cfg.description());
|
||||
result = stdStrToQ(cfg.name());
|
||||
break;
|
||||
case 2:
|
||||
result = stdStrToQ(cfg.host());
|
||||
|
|
@ -90,7 +94,15 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
|||
case 3:
|
||||
result = cfg.port();
|
||||
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 result = false;
|
||||
if (role == Qt::EditRole) {
|
||||
int row = index.row();
|
||||
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) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
cfg.setDescription( qStrToStd(value.toString()) );
|
||||
emit dataChanged(index, index);
|
||||
cfg.setName( qStrToStd(value.toString()) );
|
||||
break;
|
||||
case 2:
|
||||
cfg.setHost( qStrToStd(value.toString()) );
|
||||
emit dataChanged(index, index);
|
||||
break;
|
||||
case 3:
|
||||
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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
if (result) {
|
||||
emit dataChanged(index, index);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
std::string result(cfg.description());
|
||||
std::string result(cfg.name());
|
||||
result += " (";
|
||||
result += cfg.user();
|
||||
result += "@";
|
||||
|
|
@ -147,8 +171,50 @@ QString ConnectionListModel::makeLongDescription(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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue