#134 Saving servers complete, plus some small input checks with fallbacks.

master
Tomatix 3 years ago
parent d7c744baa5
commit 8312170b71
  1. 3
      IConfig/IConfigServers.cpp
  2. 10
      IConfig/ServerItem.cpp
  3. 46
      IConfig/ServerModel.cpp
  4. 4
      IConfig/ServerModel.h

@ -98,6 +98,9 @@ void IConfigServers::save()
cf_Server = conf.connection("Server");
cf_Password = conf.connection("Password");
cf_SSL = conf.connection("SSL").toInt();
if (!smodel.saveToFile())
QMessageBox::warning(this, tr("Failed to save servers.json"), tr("Unable to open servers.json for writing, changes will be lost upon next restart!"));
}
void IConfigServers::reset()

@ -23,8 +23,14 @@ QString ServerItem::name() const
QString ServerItem::address() const
{
const auto host{ m_data[Key::Host].toString() };
const auto port{ m_data[Key::Port].toInt() };
auto host{ m_data[Key::Host].toString() };
auto port{ m_data[Key::Port].toInt() };
if (host.isEmpty())
host = "no.host";
if (port == 0)
port = ssl() ? 6697 : 6667;
return QStringLiteral("%1:%2").arg(host).arg(port);
}

@ -12,13 +12,19 @@
#include <QDebug>
#include <QTimer>
#include <QIcon>
#include <QFile>
namespace {
QJsonObject createJsonObject(const QString& name, const QString& address, const QString& password, bool isSsl)
{
namespace Key = ServerJsonKey;
const auto hostport{ address.split(':') };
auto hostport{ address.split(':') };
if (hostport.size() == 1) {
hostport[0] = "no.host";
hostport << (isSsl ? "6697" : "6667");
}
const auto host{ hostport[0] };
const auto port{ hostport.count() > 1 ? hostport[1].toShort()
: (isSsl ? 6697 : 6667) };
@ -58,9 +64,45 @@ ServerModel::ServerModel(QObject* parent) :
}
void ServerModel::saveToJsonFile(const QString& fileName) const
bool ServerModel::saveToFile()
{
namespace Key = ServerJsonKey;
QJsonArray servers, networks;
for (auto& item : m_items) {
if (item.isNetwork()) {
QJsonArray networkServers;
for (auto& child : item.children()) {
QJsonObject server{ createJsonObject(child.name(), child.address(), child.password(), child.ssl()) };
networkServers << server;
}
QJsonObject network{ createJsonObject(item.name(), item.address(), item.password(), item.ssl()) };
network[Key::Servers] = networkServers;
networks << network;
}
else {
QJsonObject server{ createJsonObject(item.name(), item.address(), item.password(), item.ssl()) };
servers << server;
}
}
QJsonObject root;
root[Key::Servers] = servers;
root[Key::Networks] = networks;
QJsonDocument doc(root);
const auto path{ QStringLiteral("%1/servers.json").arg(LOCAL_PATH) };
QFile f{ path };
if (!f.open(QIODevice::WriteOnly))
return false;
f.write(doc.toJson(QJsonDocument::Indented));
f.flush();
f.close();
return true;
}
Qt::ItemFlags ServerModel::flags(const QModelIndex& index) const

@ -25,8 +25,8 @@ class ServerModel : public QAbstractItemModel
public:
explicit ServerModel(QObject* parent = nullptr);
/// Save model to a json file
void saveToJsonFile(const QString& fileName) const;
/// Save model to servers.json
bool saveToFile();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;

Loading…
Cancel
Save