154 lines
3.9 KiB
C++
154 lines
3.9 KiB
C++
#include "connectionlistmodel.h"
|
|
#include <QSettings>
|
|
|
|
|
|
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)
|
|
{
|
|
settings.setValue("description", stdStrToQ(cc.description()));
|
|
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)
|
|
{
|
|
cc.setDescription(qvarToStdStr(settings.value("description")));
|
|
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)
|
|
{}
|
|
|
|
int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
return m_connections.size();
|
|
}
|
|
|
|
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
|
|
{
|
|
return 4;
|
|
}
|
|
|
|
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
QVariant result;
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
int row = index.row();
|
|
int col = index.column();
|
|
const ConnectionConfig& cfg = m_connections.at(row);
|
|
switch (col) {
|
|
case 0:
|
|
result = makeLongDescription(cfg);
|
|
break;
|
|
case 1:
|
|
result = stdStrToQ(cfg.description());
|
|
break;
|
|
case 2:
|
|
result = stdStrToQ(cfg.host());
|
|
break;
|
|
case 3:
|
|
result = cfg.port();
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
|
|
if (role == Qt::EditRole) {
|
|
int row = index.row();
|
|
int col = index.column();
|
|
ConnectionConfig& cfg = m_connections.at(row);
|
|
switch (col) {
|
|
case 0:
|
|
break;
|
|
case 1:
|
|
cfg.setDescription( qStrToStd(value.toString()) );
|
|
emit dataChanged(index, index);
|
|
break;
|
|
case 2:
|
|
cfg.setHost( qStrToStd(value.toString()) );
|
|
emit dataChanged(index, index);
|
|
break;
|
|
case 3:
|
|
cfg.setPort( value.toInt() );
|
|
emit dataChanged(index, index);
|
|
break;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
|
{
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
|
|
}
|
|
|
|
|
|
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
|
{
|
|
std::string result(cfg.description());
|
|
result += " (";
|
|
result += cfg.user();
|
|
result += "@";
|
|
result += cfg.host();
|
|
result += ":";
|
|
result += std::to_string(cfg.port());
|
|
result += "/";
|
|
result += cfg.dbname();
|
|
result += ")";
|
|
return stdStrToQ(result);
|
|
}
|
|
|
|
void ConnectionListModel::add(const ConnectionConfig &cfg)
|
|
{
|
|
m_connections.push_back(cfg);
|
|
auto idx = createIndex(m_connections.size()-1, 0);
|
|
emit dataChanged(idx, idx);
|
|
}
|
|
|