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"
|
2018-11-04 11:26:20 +01:00
|
|
|
|
#include "PasswordManager.h"
|
2017-02-26 19:29:50 +01:00
|
|
|
|
#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()));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
//settings.setValue("password", stdStrToQ(cc.password()));
|
2017-02-26 19:29:50 +01:00
|
|
|
|
settings.setValue("dbname", stdStrToQ(cc.dbname()));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
settings.setValue("sslmode", static_cast<int>(cc.sslMode()));
|
2017-02-26 19:29:50 +01:00
|
|
|
|
settings.setValue("sslcert", stdStrToQ(cc.sslCert()));
|
|
|
|
|
|
settings.setValue("sslkey", stdStrToQ(cc.sslKey()));
|
|
|
|
|
|
settings.setValue("sslrootcert", stdStrToQ(cc.sslRootCert()));
|
|
|
|
|
|
settings.setValue("sslcrl", stdStrToQ(cc.sslCrl()));
|
2018-11-08 21:50:49 +01:00
|
|
|
|
settings.setValue("passwordState", static_cast<int>(cc.passwordState()));
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
template <typename S, typename T>
|
|
|
|
|
|
bool in_range(T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return value >= std::numeric_limits<S>::min() && value <= std::numeric_limits<S>::max();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-26 19:29:50 +01:00
|
|
|
|
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
|
|
|
|
|
{
|
|
|
|
|
|
cc.setName(qvarToStdStr(settings.value("name")));
|
|
|
|
|
|
cc.setHost(qvarToStdStr(settings.value("host")));
|
|
|
|
|
|
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
int p = settings.value("port", 5432).toInt();
|
|
|
|
|
|
if (!in_range<uint16_t>(p)) {
|
|
|
|
|
|
p = 0; // let the user re-enter a valid value
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cc.setPort(static_cast<uint16_t>(p));
|
2017-02-26 19:29:50 +01:00
|
|
|
|
cc.setUser(qvarToStdStr(settings.value("user")));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
|
|
|
|
|
|
//cc.setPassword(qvarToStdStr(settings.value("password")));
|
|
|
|
|
|
|
2017-02-26 19:29:50 +01:00
|
|
|
|
cc.setDbname(qvarToStdStr(settings.value("dbname")));
|
2018-11-04 11:26:20 +01:00
|
|
|
|
cc.setSslMode(static_cast<SslMode>(settings.value("sslmode").toInt()));
|
2017-02-26 19:29:50 +01:00
|
|
|
|
cc.setSslCert(qvarToStdStr(settings.value("sslcert")));
|
|
|
|
|
|
cc.setSslKey(qvarToStdStr(settings.value("sslkey")));
|
|
|
|
|
|
cc.setSslRootCert(qvarToStdStr(settings.value("sslrootcert")));
|
|
|
|
|
|
cc.setSslCrl(qvarToStdStr(settings.value("sslcrl")));
|
2018-11-08 21:50:49 +01:00
|
|
|
|
|
|
|
|
|
|
PasswordState pwstate;
|
|
|
|
|
|
QVariant v = settings.value("passwordState");
|
|
|
|
|
|
if (v.isNull()) pwstate = PasswordState::NotStored;
|
|
|
|
|
|
else pwstate = static_cast<PasswordState>(v.toInt());
|
|
|
|
|
|
cc.setPasswordState(pwstate);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // 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()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
size_t ConnectionList::createNew()
|
2017-02-26 19:29:50 +01:00
|
|
|
|
{
|
2018-11-04 11:26:20 +01:00
|
|
|
|
ConnectionConfig cc;
|
|
|
|
|
|
cc.setUuid(QUuid::createUuid());
|
|
|
|
|
|
m_connections.push_back(cc);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
return m_connections.size()-1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
void ConnectionList::remove(size_t idx, size_t count)
|
2017-02-26 19:29:50 +01:00
|
|
|
|
{
|
2018-11-04 11:26:20 +01:00
|
|
|
|
auto f = m_connections.begin() + static_cast<int>(idx);
|
|
|
|
|
|
auto l = f + static_cast<int>(count);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
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) {
|
2018-11-04 11:26:20 +01:00
|
|
|
|
settings.remove(i->uuid().toString());
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2018-11-04 11:26:20 +01:00
|
|
|
|
cc.setUuid(uuid);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
LoadConnectionConfig(settings, cc);
|
2018-11-04 11:26:20 +01:00
|
|
|
|
m_connections.push_back(cc);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionList::save()
|
|
|
|
|
|
{
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
for (auto& e : m_connections) {
|
2018-11-04 11:26:20 +01:00
|
|
|
|
settings.beginGroup(e.uuid().toString());
|
2017-02-26 19:29:50 +01:00
|
|
|
|
SCOPE_EXIT { settings.endGroup(); };
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
SaveConnectionConfig(settings, e);
|
|
|
|
|
|
e.clean();
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
settings.sync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-04 11:26:20 +01:00
|
|
|
|
void ConnectionList::save(size_t index)
|
2017-02-26 19:29:50 +01:00
|
|
|
|
{
|
2018-11-04 11:26:20 +01:00
|
|
|
|
auto& e = m_connections.at(index);
|
|
|
|
|
|
if (e.dirty()) {
|
|
|
|
|
|
QString file_name = iniFileName();
|
|
|
|
|
|
QSettings settings(file_name, QSettings::IniFormat);
|
|
|
|
|
|
settings.beginGroup(e.uuid().toString());
|
|
|
|
|
|
SaveConnectionConfig(settings, e);
|
|
|
|
|
|
e.clean();
|
|
|
|
|
|
settings.sync();
|
2017-02-26 19:29:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|