2017-02-26 19:29:50 +01:00
|
|
|
|
#include "ConnectionList.h"
|
2017-08-23 13:27:23 +02:00
|
|
|
|
#include "ScopeGuard.h"
|
2017-02-26 19:29:50 +01:00
|
|
|
|
#include "util.h"
|
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
/** Saves a connection configuration.
|
|
|
|
|
|
|
|
|
|
|
|
Before calling this you may want to call beginGroup.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void SaveConnectionConfig(QSettings &settings, const ConnectionConfig &cc)
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.setValue("name", stdStrToQ(cc.name()));
|
|
|
|
|
|
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.setName(qvarToStdStr(settings.value("name")));
|
|
|
|
|
|
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")));
|
|
|
|
|
|
// std::string encpw = qvarToStdStr(settings.value("encryptedpw"));
|
|
|
|
|
|
// if (encpw.empty()) {
|
|
|
|
|
|
cc.setPassword(qvarToStdStr(settings.value("password")));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else {
|
|
|
|
|
|
// cc.setEncryptedPassword(encpw);
|
|
|
|
|
|
// }
|
|
|
|
|
|
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")));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // end of unnamed namespace
|
|
|
|
|
|
|
|
|
|
|
|
/// \todo should return an expected as creation of the folder can fail
|
|
|
|
|
|
QString ConnectionList::iniFileName()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
|
|
QDir dir(path);
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
|
dir.mkpath(".");
|
|
|
|
|
|
}
|
|
|
|
|
|
path += "/connections.ini";
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConnectionList::ConnectionList()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ConnectionList::createNew()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_connections.emplace_back(QUuid::createUuid(), ConnectionConfig());
|
|
|
|
|
|
return m_connections.size()-1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionList::remove(int idx, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto f = m_connections.begin() + idx;
|
|
|
|
|
|
auto l = f + count;
|
|
|
|
|
|
deleteFromIni(f, l);
|
|
|
|
|
|
m_connections.erase(f, l);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionList::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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionList::load()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
auto groups = settings.childGroups();
|
|
|
|
|
|
for (auto grp : groups) {
|
|
|
|
|
|
if (grp == "c_IniGroupSecurity") {
|
|
|
|
|
|
// Read security settings
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
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 ConnectionList::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);
|
|
|
|
|
|
e.m_config.clean();
|
|
|
|
|
|
}
|
|
|
|
|
|
settings.sync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionList::save(int index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index >= 0 && index < (int)m_connections.size()) {
|
|
|
|
|
|
auto& e = m_connections[index];
|
|
|
|
|
|
if (e.m_config.dirty()) {
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
settings.beginGroup(e.m_uuid.toString());
|
|
|
|
|
|
SaveConnectionConfig(settings, e.m_config);
|
|
|
|
|
|
e.m_config.clean();
|
|
|
|
|
|
settings.sync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|