ConnectionManager overhaul
- connection settings are now changed by seperate component currently called in a seperate window - old settings pane on the right of the connections had been removed - new edit config button added between new connection and remove connection
This commit is contained in:
parent
78247c7abe
commit
b09e8a6d4b
20 changed files with 836 additions and 733 deletions
|
|
@ -1,14 +1,78 @@
|
|||
#include "ConnectionListModel.h"
|
||||
#include "ConnectionList.h"
|
||||
#include "ScopeGuard.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <botan/cryptobox.h>
|
||||
#include <QDir>
|
||||
#include <QStandardPaths>
|
||||
#include <QSettings>
|
||||
|
||||
|
||||
ConnectionListModel::ConnectionListModel(ConnectionList *conns, QObject *parent)
|
||||
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", static_cast<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()));
|
||||
settings.setValue("passwordState", static_cast<int>(cc.passwordState()));
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
bool in_range(T value)
|
||||
{
|
||||
return value >= std::numeric_limits<S>::min() && value <= std::numeric_limits<S>::max();
|
||||
}
|
||||
|
||||
void LoadConnectionConfig(QSettings &settings, ConnectionConfig &cc)
|
||||
{
|
||||
cc.setName(qvarToStdStr(settings.value("name")));
|
||||
cc.setHost(qvarToStdStr(settings.value("host")));
|
||||
cc.setHostAddr(qvarToStdStr(settings.value("hostaddr")));
|
||||
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));
|
||||
cc.setUser(qvarToStdStr(settings.value("user")));
|
||||
|
||||
//cc.setPassword(qvarToStdStr(settings.value("password")));
|
||||
|
||||
cc.setDbname(qvarToStdStr(settings.value("dbname")));
|
||||
cc.setSslMode(static_cast<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")));
|
||||
|
||||
PasswordState pwstate;
|
||||
QVariant v = settings.value("passwordState");
|
||||
if (v.isNull()) pwstate = PasswordState::NotStored;
|
||||
else pwstate = static_cast<PasswordState>(v.toInt());
|
||||
cc.setPasswordState(pwstate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // end of unnamed namespace
|
||||
|
||||
|
||||
ConnectionListModel::ConnectionListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_connections(conns)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -18,7 +82,7 @@ int ConnectionListModel::rowCount(const QModelIndex &parent) const
|
|||
{
|
||||
int result = 0;
|
||||
if (parent == QModelIndex()) {
|
||||
result = m_connections->size();
|
||||
result = m_connections.size();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -34,7 +98,7 @@ 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->getConfigByIdx(row);
|
||||
const ConnectionConfig& cfg = m_connections.at(row);
|
||||
switch (col) {
|
||||
case Description:
|
||||
result = makeLongDescription(cfg);
|
||||
|
|
@ -72,7 +136,7 @@ bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &valu
|
|||
// auto& elem = m_connections.at(row);
|
||||
// elem.m_dirty = true;
|
||||
// ConnectionConfig& cfg = elem.m_config;
|
||||
ConnectionConfig& cfg = m_connections->getConfigByIdx(row);
|
||||
ConnectionConfig& cfg = m_connections[row];
|
||||
if (col > 0) {
|
||||
result = true;
|
||||
}
|
||||
|
|
@ -109,7 +173,7 @@ Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
|
|||
{
|
||||
Qt::ItemFlags result;
|
||||
int row = index.row();
|
||||
if (row >= 0 && row < (int)m_connections->size()) {
|
||||
if (row >= 0 && row < m_connections.size()) {
|
||||
result = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
||||
if (index.column() != Description)
|
||||
result |= Qt::ItemIsEditable;
|
||||
|
|
@ -133,75 +197,122 @@ QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
|
|||
return stdStrToQ(result);
|
||||
}
|
||||
|
||||
void ConnectionListModel::newItem()
|
||||
{
|
||||
int i = m_connections->createNew();
|
||||
auto idx = createIndex(i, 0);
|
||||
emit dataChanged(idx, idx);
|
||||
}
|
||||
|
||||
Expected<ConnectionConfig> ConnectionListModel::get(size_t row)
|
||||
{
|
||||
if (row < m_connections->size()) {
|
||||
return m_connections->getConfigByIdx(row);
|
||||
}
|
||||
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
||||
}
|
||||
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
template <typename T>
|
||||
size_t as_size_t(T t);
|
||||
|
||||
template <>
|
||||
size_t as_size_t(int t)
|
||||
{
|
||||
BOOST_ASSERT(t >= 0);
|
||||
return static_cast<size_t>(t);
|
||||
}
|
||||
|
||||
template <>
|
||||
size_t as_size_t(long t)
|
||||
{
|
||||
BOOST_ASSERT(t >= 0);
|
||||
return static_cast<size_t>(t);
|
||||
}
|
||||
|
||||
|
||||
//void ConnectionListModel::del(const int idx)
|
||||
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
bool result = false;
|
||||
if (row >= 0 && row < (int)m_connections->size()) {
|
||||
|
||||
if (row >= 0 && row < m_connections.size()) {
|
||||
|
||||
beginRemoveRows(parent, row, row + count -1);
|
||||
SCOPE_EXIT { endRemoveRows(); };
|
||||
|
||||
m_connections->remove(as_size_t(row), as_size_t(count));
|
||||
QString file_name = iniFileName();
|
||||
QSettings settings(file_name, QSettings::IniFormat);
|
||||
for (int idx = 0; idx < count; ++idx) {
|
||||
auto&& cc = m_connections[idx+row];
|
||||
{
|
||||
settings.beginGroup(cc.uuid().toString());
|
||||
SCOPE_EXIT { settings.endGroup(); };
|
||||
for (auto&& k : settings.childKeys()) {
|
||||
settings.remove(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
m_connections.remove(row, count);
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void ConnectionListModel::newItem()
|
||||
{
|
||||
// int i = m_connections->createNew();
|
||||
// auto idx = createIndex(i, 0);
|
||||
// emit dataChanged(idx, idx);
|
||||
}
|
||||
|
||||
//void ConnectionListModel::load()
|
||||
//{
|
||||
// m_connections->load();
|
||||
//}
|
||||
Expected<ConnectionConfig> ConnectionListModel::get(int row)
|
||||
{
|
||||
if (row < m_connections.size()) {
|
||||
return m_connections.at(row);
|
||||
}
|
||||
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
|
||||
}
|
||||
|
||||
void ConnectionListModel::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;
|
||||
cc.setUuid(uuid);
|
||||
LoadConnectionConfig(settings, cc);
|
||||
m_connections.push_back(cc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectionListModel::save()
|
||||
{
|
||||
m_connections->save();
|
||||
QString file_name = iniFileName();
|
||||
QSettings settings(file_name, QSettings::IniFormat);
|
||||
for (auto& e : m_connections) {
|
||||
settings.beginGroup(e.uuid().toString());
|
||||
SCOPE_EXIT { settings.endGroup(); };
|
||||
|
||||
SaveConnectionConfig(settings, e);
|
||||
e.clean();
|
||||
}
|
||||
settings.sync();
|
||||
}
|
||||
|
||||
void ConnectionListModel::save(size_t index)
|
||||
void ConnectionListModel::save(int index)
|
||||
{
|
||||
m_connections->save(index);
|
||||
auto& e = m_connections[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();
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectionListModel::save(size_t index, const ConnectionConfig &cc)
|
||||
void ConnectionListModel::save(const ConnectionConfig &cc)
|
||||
{
|
||||
m_connections->setConfigByIdx(index, cc);
|
||||
m_connections->save(index);
|
||||
auto find_res = std::find(m_connections.begin(), m_connections.end(), cc.uuid());
|
||||
int i;
|
||||
if (find_res == m_connections.end()) {
|
||||
m_connections.push_back(cc);
|
||||
i = m_connections.size() - 1;
|
||||
}
|
||||
else {
|
||||
*find_res = cc;
|
||||
i = find_res - m_connections.begin();
|
||||
}
|
||||
emit dataChanged(createIndex(i, 0), createIndex(i, ColCount-1));
|
||||
save(i);
|
||||
}
|
||||
|
||||
QString ConnectionListModel::iniFileName()
|
||||
{
|
||||
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
QDir dir(path);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(".");
|
||||
}
|
||||
path += "/connections.ini";
|
||||
return path;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue