2017-01-14 20:07:12 +01:00
|
|
|
|
#include "connectionlistmodel.h"
|
2017-01-15 12:27:36 +01:00
|
|
|
|
#include <QDir>
|
|
|
|
|
|
#include <QStandardPaths>
|
2017-01-14 20:07:12 +01:00
|
|
|
|
#include <QSettings>
|
2017-01-15 12:27:36 +01:00
|
|
|
|
#include "scopeguard.h"
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
|
|
|
|
|
inline QString stdStrToQ(const std::string &s)
|
|
|
|
|
|
{
|
|
|
|
|
|
return QString::fromUtf8(s.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline std::string qStrToStd(const QString &s)
|
|
|
|
|
|
{
|
|
|
|
|
|
return std::string(s.toUtf8().data());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline std::string qvarToStdStr(const QVariant &c)
|
|
|
|
|
|
{
|
|
|
|
|
|
return qStrToStd(c.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Saves a connection configuration.
|
|
|
|
|
|
|
|
|
|
|
|
Before calling this you may want to call beginGroup.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
settings.setValue("name", stdStrToQ(cc.name()));
|
2017-01-14 20:07:12 +01:00
|
|
|
|
settings.setValue("host", stdStrToQ(cc.host()));
|
|
|
|
|
|
settings.setValue("hostaddr", stdStrToQ(cc.hostAddr()));
|
|
|
|
|
|
settings.setValue("port", cc.port());
|
|
|
|
|
|
settings.setValue("user", stdStrToQ(cc.user()));
|
|
|
|
|
|
settings.setValue("password", stdStrToQ(cc.password()));
|
|
|
|
|
|
settings.setValue("dbname", stdStrToQ(cc.dbname()));
|
|
|
|
|
|
settings.setValue("sslmode", (int)cc.sslMode());
|
|
|
|
|
|
settings.setValue("sslcert", stdStrToQ(cc.sslCert()));
|
|
|
|
|
|
settings.setValue("sslkey", stdStrToQ(cc.sslKey()));
|
|
|
|
|
|
settings.setValue("sslrootcert", stdStrToQ(cc.sslRootCert()));
|
|
|
|
|
|
settings.setValue("sslcrl", stdStrToQ(cc.sslCrl()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
cc.setName(qvarToStdStr(settings.value("name")));
|
2017-01-14 20:07:12 +01:00
|
|
|
|
cc.setHost(qvarToStdStr(settings.value("host")));
|
|
|
|
|
|
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
|
|
|
|
|
cc.setPort(settings.value("port", 5432).toInt());
|
|
|
|
|
|
cc.setUser(qvarToStdStr(settings.value("user")));
|
|
|
|
|
|
cc.setPassword(qvarToStdStr(settings.value("password")));
|
|
|
|
|
|
cc.setDbname(qvarToStdStr(settings.value("dbname")));
|
|
|
|
|
|
cc.setSslMode((SslMode)settings.value("sslmode").toInt());
|
|
|
|
|
|
cc.setSslCert(qvarToStdStr(settings.value("sslcert")));
|
|
|
|
|
|
cc.setSslKey(qvarToStdStr(settings.value("sslkey")));
|
|
|
|
|
|
cc.setSslRootCert(qvarToStdStr(settings.value("sslrootcert")));
|
|
|
|
|
|
cc.setSslCrl(qvarToStdStr(settings.value("sslcrl")));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionListModel::ConnectionListModel(QObject *parent)
|
|
|
|
|
|
: QAbstractListModel(parent)
|
2017-01-15 12:27:36 +01:00
|
|
|
|
{
|
|
|
|
|
|
load();
|
|
|
|
|
|
}
|
2017-01-14 20:07:12 +01:00
|
|
|
|
|
|
|
|
|
|
int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
|
{
|
2017-01-15 21:01:40 +01:00
|
|
|
|
int result = 0;
|
|
|
|
|
|
if (parent == QModelIndex()) {
|
|
|
|
|
|
result = m_connections.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-14 20:07:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-14 22:03:58 +01:00
|
|
|
|
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
return 7;
|
2017-01-14 22:03:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-14 20:07:12 +01:00
|
|
|
|
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
|
{
|
|
|
|
|
|
QVariant result;
|
2017-01-14 22:03:58 +01:00
|
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
2017-01-14 20:07:12 +01:00
|
|
|
|
int row = index.row();
|
2017-01-14 22:03:58 +01:00
|
|
|
|
int col = index.column();
|
2017-01-15 12:27:36 +01:00
|
|
|
|
const ConnectionConfig& cfg = m_connections.at(row).m_config;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
switch (col) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
result = makeLongDescription(cfg);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
2017-01-15 12:27:36 +01:00
|
|
|
|
result = stdStrToQ(cfg.name());
|
2017-01-14 22:29:12 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
result = stdStrToQ(cfg.host());
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
result = cfg.port();
|
|
|
|
|
|
break;
|
2017-01-15 12:27:36 +01:00
|
|
|
|
case 4:
|
|
|
|
|
|
result = stdStrToQ(cfg.user());
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
result = stdStrToQ(cfg.password());
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
result = stdStrToQ(cfg.dbname());
|
|
|
|
|
|
break;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
}
|
2017-01-14 20:07:12 +01:00
|
|
|
|
}
|
2017-01-14 22:03:58 +01:00
|
|
|
|
|
2017-01-14 20:07:12 +01:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-14 22:29:12 +01:00
|
|
|
|
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
bool result = false;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
if (role == Qt::EditRole) {
|
|
|
|
|
|
int row = index.row();
|
|
|
|
|
|
int col = index.column();
|
2017-01-18 20:48:31 +01:00
|
|
|
|
auto& elem = m_connections.at(row);
|
|
|
|
|
|
elem.m_dirty = true;
|
|
|
|
|
|
ConnectionConfig& cfg = elem.m_config;
|
2017-01-15 12:27:36 +01:00
|
|
|
|
if (col > 0) {
|
|
|
|
|
|
result = true;
|
|
|
|
|
|
}
|
2017-01-14 22:29:12 +01:00
|
|
|
|
switch (col) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
2017-01-15 12:27:36 +01:00
|
|
|
|
cfg.setName( qStrToStd(value.toString()) );
|
2017-01-14 22:29:12 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
cfg.setHost( qStrToStd(value.toString()) );
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
cfg.setPort( value.toInt() );
|
2017-01-15 12:27:36 +01:00
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
cfg.setUser( qStrToStd(value.toString()) );
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 5:
|
|
|
|
|
|
cfg.setPassword( qStrToStd(value.toString()) );
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 6:
|
|
|
|
|
|
cfg.setDbname( qStrToStd(value.toString()) );
|
2017-01-14 22:29:12 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-01-15 12:27:36 +01:00
|
|
|
|
if (result) {
|
|
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
|
|
|
|
|
{
|
2017-01-15 21:01:40 +01:00
|
|
|
|
Qt::ItemFlags result;
|
|
|
|
|
|
int row = index.row();
|
2017-02-02 07:22:54 +01:00
|
|
|
|
if (row >= 0 && row < (int)m_connections.size()) {
|
2017-01-15 21:01:40 +01:00
|
|
|
|
result = Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-14 20:07:12 +01:00
|
|
|
|
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
std::string result(cfg.name());
|
2017-01-14 20:07:12 +01:00
|
|
|
|
result += " (";
|
|
|
|
|
|
result += cfg.user();
|
|
|
|
|
|
result += "@";
|
|
|
|
|
|
result += cfg.host();
|
2017-01-14 22:29:12 +01:00
|
|
|
|
result += ":";
|
|
|
|
|
|
result += std::to_string(cfg.port());
|
2017-01-14 20:07:12 +01:00
|
|
|
|
result += "/";
|
|
|
|
|
|
result += cfg.dbname();
|
|
|
|
|
|
result += ")";
|
|
|
|
|
|
return stdStrToQ(result);
|
|
|
|
|
|
}
|
2017-01-14 22:29:12 +01:00
|
|
|
|
|
|
|
|
|
|
void ConnectionListModel::add(const ConnectionConfig &cfg)
|
|
|
|
|
|
{
|
2017-01-15 12:27:36 +01:00
|
|
|
|
m_connections.emplace_back(QUuid::createUuid(), cfg);
|
2017-01-14 22:29:12 +01:00
|
|
|
|
auto idx = createIndex(m_connections.size()-1, 0);
|
|
|
|
|
|
emit dataChanged(idx, idx);
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expected<ConnectionConfig> ConnectionListModel::get(int row)
|
|
|
|
|
|
{
|
2017-02-02 07:22:54 +01:00
|
|
|
|
if (row >= 0 && row < (int)m_connections.size()) {
|
2017-01-15 21:01:40 +01:00
|
|
|
|
return m_connections.at(row).m_config;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//void ConnectionListModel::del(const int idx)
|
|
|
|
|
|
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool result = false;
|
2017-02-02 07:22:54 +01:00
|
|
|
|
if (row >= 0 && row < (int)m_connections.size()) {
|
2017-01-18 20:48:31 +01:00
|
|
|
|
|
|
|
|
|
|
beginRemoveRows(parent, row, row + count -1);
|
|
|
|
|
|
SCOPE_EXIT { endRemoveRows(); };
|
|
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
auto f = m_connections.begin() + row;
|
2017-01-18 20:48:31 +01:00
|
|
|
|
auto l = f + count;
|
|
|
|
|
|
deleteFromIni(f, l);
|
|
|
|
|
|
m_connections.erase(f, l);
|
2017-01-15 21:01:40 +01:00
|
|
|
|
result = true;
|
2017-01-18 20:48:31 +01:00
|
|
|
|
|
2017-01-15 21:01:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
return result;
|
2017-01-14 22:29:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-15 12:27:36 +01:00
|
|
|
|
/// \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);
|
2017-01-18 20:48:31 +01:00
|
|
|
|
for (auto& e : m_connections) {
|
2017-01-15 12:27:36 +01:00
|
|
|
|
settings.beginGroup(e.m_uuid.toString());
|
|
|
|
|
|
SCOPE_EXIT { settings.endGroup(); };
|
|
|
|
|
|
|
|
|
|
|
|
SaveConnectionConfig(settings, e.m_config);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-01-18 20:48:31 +01:00
|
|
|
|
|
|
|
|
|
|
void ConnectionListModel::save(int index)
|
|
|
|
|
|
{
|
2017-02-02 07:22:54 +01:00
|
|
|
|
if (index >= 0 && index < (int)m_connections.size()) {
|
2017-01-18 20:48:31 +01:00
|
|
|
|
auto& e = m_connections[index];
|
|
|
|
|
|
if (e.m_dirty) {
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
settings.beginGroup(e.m_uuid.toString());
|
|
|
|
|
|
SaveConnectionConfig(settings, e.m_config);
|
|
|
|
|
|
settings.sync();
|
|
|
|
|
|
e.m_dirty = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionListModel::deleteFromIni(t_Connections::iterator begin, t_Connections::iterator end)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
for (auto i = begin; i != end; ++i) {
|
|
|
|
|
|
settings.remove(i->m_uuid.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|