|
|
|
@ -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 |
|
|
|
|