#133 Better model for handling servers; using json (servers.json) for storing servers
parent
d032da0ce6
commit
f4a95a8eb1
@ -0,0 +1,38 @@ |
||||
#include "ServerItem.h" |
||||
|
||||
namespace Key { |
||||
constexpr auto Name = "Name"; |
||||
constexpr auto Host = "Host"; |
||||
constexpr auto Port = "Port"; |
||||
constexpr auto Password = "Password"; |
||||
constexpr auto SSL = "SSL"; |
||||
} |
||||
|
||||
ServerItem::ServerItem(const QJsonObject& data, int row, ServerItem* parentItem) |
||||
: m_data(data) |
||||
, m_row(row) |
||||
, m_parent(parentItem) |
||||
{} |
||||
|
||||
QString ServerItem::name() const |
||||
{ |
||||
return m_data[Key::Name].toString(); |
||||
} |
||||
|
||||
QString ServerItem::address() const |
||||
{ |
||||
const auto host{ m_data[Key::Host].toString() }; |
||||
const auto port{ m_data[Key::Port].toInt() }; |
||||
|
||||
return QStringLiteral("%1:%2").arg(host).arg(port); |
||||
} |
||||
|
||||
QString ServerItem::password() const |
||||
{ |
||||
return m_data[Key::Password].toString(); |
||||
} |
||||
|
||||
bool ServerItem::ssl() const |
||||
{ |
||||
return m_data[Key::SSL].toBool(); |
||||
} |
@ -0,0 +1,29 @@ |
||||
#ifndef SERVERITEM_H |
||||
#define SERVERITEM_H |
||||
|
||||
#include <QList> |
||||
#include <QString> |
||||
#include <QJsonObject> |
||||
|
||||
class ServerItem |
||||
{ |
||||
public: |
||||
ServerItem(const QJsonObject& data, int row, ServerItem* parentItem); |
||||
|
||||
QString name() const; |
||||
QString address() const; |
||||
QString password() const; |
||||
bool ssl() const; |
||||
|
||||
ServerItem* parent() const { return m_parent; } |
||||
int row() const { return m_row; } |
||||
QList<ServerItem>& children() { return m_children; } |
||||
|
||||
private: |
||||
QJsonObject m_data; |
||||
QList<ServerItem> m_children; |
||||
int m_row; |
||||
ServerItem* m_parent; |
||||
}; |
||||
|
||||
#endif // SERVERITEM_H
|
@ -1,133 +0,0 @@ |
||||
/*
|
||||
* IdealIRC - Internet Relay Chat client |
||||
* Copyright (C) 2021 Tom-Andre Barstad. |
||||
* This software is licensed under the Software Attribution License. |
||||
* See LICENSE for more information. |
||||
*/ |
||||
|
||||
#include "ServerMgr.h" |
||||
#include "config.h" |
||||
#include <fmt/format.h> |
||||
|
||||
ServerMgr::ServerMgr(QObject *parent) : |
||||
QObject(parent), |
||||
ini( QString(LOCAL_PATH+"/servers.ini").toStdString() ) |
||||
{ |
||||
} |
||||
|
||||
QStringList ServerMgr::networkList() |
||||
{ |
||||
int count = ini.count(); |
||||
QStringList r; |
||||
|
||||
for (int i = 0; i < count; i++) { |
||||
const auto name = QString::fromStdString( ini.section(i) ); |
||||
r.push_back(name); |
||||
} |
||||
|
||||
return r; |
||||
} |
||||
|
||||
QHash<QString,QString> ServerMgr::serverList(const QString& network) |
||||
{ |
||||
const auto networkStr = network.toStdString(); |
||||
|
||||
int count = ini.count(networkStr); |
||||
QHash<QString,QString> r; |
||||
|
||||
for (int i = 0; i < count; i++) { |
||||
const auto servername = QString::fromStdString( ini.item(networkStr, i) ); |
||||
const auto serverdetails = QString::fromStdString( ini.read(networkStr, i) ); |
||||
r.insert(servername, serverdetails); |
||||
} |
||||
|
||||
return r; |
||||
} |
||||
|
||||
QString ServerMgr::defaultServer(const QString& network) |
||||
{ |
||||
return QString::fromStdString( ini.read(network.toStdString(), "DEFAULT") ); |
||||
} |
||||
|
||||
bool ServerMgr::addNetwork(const QString& name) |
||||
{ |
||||
const auto nameStr = name.toStdString(); |
||||
|
||||
if (nameStr == "NONE" || ini.exist(nameStr)) |
||||
return false; |
||||
|
||||
ini.write(nameStr, "DEFAULT", "server.name"); |
||||
return true; |
||||
} |
||||
|
||||
bool ServerMgr::renameNetwork(const QString& o_name, const QString& n_name) |
||||
{ |
||||
if ((o_name == "NONE") || (n_name == "NONE")) |
||||
return false; |
||||
|
||||
auto err = ini.rename(o_name.toStdString(), n_name.toStdString()); |
||||
return err == IniFile::Error::NoError; |
||||
} |
||||
|
||||
void ServerMgr::delNetwork(const QString& name, bool keep_servers) |
||||
{ |
||||
// If servers=true, we will keep the servers by moving them to the NONE section.
|
||||
|
||||
const auto nameStr = name.toStdString(); |
||||
if (! ini.exist(nameStr)) |
||||
return; |
||||
|
||||
if (keep_servers) { |
||||
int max = ini.count(nameStr); |
||||
for (int i = 0; i < max; i++) { |
||||
auto item = fmt::format("{}_{}", name.toStdString(), ini.item(nameStr, i)); |
||||
auto value = ini.read(nameStr, i); |
||||
|
||||
ini.write("NONE", item, value); |
||||
} |
||||
} |
||||
|
||||
ini.remove(nameStr); |
||||
} |
||||
|
||||
bool ServerMgr::addServer(const QString& name, const QString& host, const QString& pw, const QString& network) |
||||
{ |
||||
QString details; |
||||
|
||||
if (pw.isEmpty()) { |
||||
details = host; |
||||
} |
||||
else { |
||||
details = QString("%1|%2") |
||||
.arg(host) |
||||
.arg(pw); |
||||
} |
||||
|
||||
const auto networkStr = network.toStdString(); |
||||
|
||||
if (!ini.exist(networkStr) && networkStr != "NONE") |
||||
return false; |
||||
|
||||
ini.write(networkStr, name.toStdString(), details.toStdString()); |
||||
return true; |
||||
} |
||||
|
||||
void ServerMgr::delServer(const QString& name, const QString& network) |
||||
{ |
||||
ini.remove(network.toStdString(), name.toStdString()); |
||||
} |
||||
|
||||
bool ServerMgr::hasNetwork(const QString& name) |
||||
{ |
||||
return ini.exist(name.toStdString()); |
||||
} |
||||
|
||||
bool ServerMgr::hasServer(const QString& name, const QString& network) |
||||
{ |
||||
return ini.exist(network.toStdString(), name.toStdString()); |
||||
} |
||||
|
||||
QString ServerMgr::getServerDetails(const QString& name, const QString& network) |
||||
{ |
||||
return QString::fromStdString( ini.read(network.toStdString(), name.toStdString()) ); |
||||
} |
@ -1,66 +0,0 @@ |
||||
/*
|
||||
* IdealIRC - Internet Relay Chat client |
||||
* Copyright (C) 2021 Tom-Andre Barstad. |
||||
* This software is licensed under the Software Attribution License. |
||||
* See LICENSE for more information. |
||||
*/ |
||||
|
||||
#ifndef SERVERMGR_H |
||||
#define SERVERMGR_H |
||||
|
||||
#include <QObject> |
||||
#include <QStringList> |
||||
#include <QHash> |
||||
|
||||
#include "IniFile.h" |
||||
|
||||
/**
|
||||
* @brief Manages servers.ini |
||||
* @details |
||||
* This class is from IdealIRC 0.x series and is going to be removed/replaced at any time. |
||||
*/ |
||||
class ServerMgr : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
explicit ServerMgr(QObject *parent = nullptr); |
||||
|
||||
// All networks in a string list (Also counts in the NONE network)
|
||||
QStringList networkList(); |
||||
|
||||
// All servers from a network in a hash map <"name","server:port|passwd">
|
||||
QHash<QString,QString> serverList(const QString& network = "NONE"); |
||||
|
||||
// Return default server of given network (The "NONE" network have no default!) - returns empty if no default server is set.
|
||||
QString defaultServer(const QString& network); |
||||
|
||||
// Add new network to servers.ini - returns false if network exist
|
||||
bool addNetwork(const QString& name); |
||||
|
||||
// Rename a network - returns false if new network name already exist
|
||||
bool renameNetwork(const QString& o_name, const QString& n_name); |
||||
|
||||
// Delete network
|
||||
void delNetwork(const QString& name, bool keep_servers = false); |
||||
|
||||
// Add (or update) a server to network - returns false if network doesn't exsist
|
||||
bool addServer(const QString& name, const QString& host /*host:port*/, const QString& pw = "", const QString& network = "NONE"); |
||||
|
||||
// Delete a server from network
|
||||
void delServer(const QString& name, const QString& network = "NONE"); |
||||
|
||||
// Check of we have the given network name
|
||||
bool hasNetwork(const QString& name); |
||||
|
||||
// Check if we have the given server name inside the network
|
||||
bool hasServer(const QString& name, const QString& network = "NONE"); |
||||
|
||||
// Get server details
|
||||
QString getServerDetails(const QString& name, const QString& network = "NONE"); |
||||
|
||||
private: |
||||
IniFile ini;
|
||||
}; |
||||
|
||||
#endif // SERVERMGR_H
|
@ -1,366 +1,137 @@ |
||||
/*
|
||||
* IdealIRC - Internet Relay Chat client |
||||
* Copyright (C) 2021 Tom-Andre Barstad. |
||||
* Copyright (C) 2022 Tom-Andre Barstad. |
||||
* This software is licensed under the Software Attribution License. |
||||
* See LICENSE for more information. |
||||
*/ |
||||
|
||||
#include "ServerModel.h" |
||||
#include <QHashIterator> |
||||
#include "config.h" |
||||
#include <QJsonArray> |
||||
#include <QJsonValue> |
||||
#include <QFile> |
||||
#include <QDebug> |
||||
|
||||
ServerModel::ServerModel(QObject *parent) : |
||||
QStandardItemModel(parent) |
||||
ServerModel::ServerModel(QObject* parent) : |
||||
QAbstractItemModel(parent) |
||||
{ |
||||
resetModel(); |
||||
} |
||||
QJsonDocument jsondoc; |
||||
|
||||
QModelIndex ServerModel::indexFromHost(QString hostname) |
||||
{ |
||||
return hostmap.value(hostname, QModelIndex()); |
||||
} |
||||
const auto path{ QStringLiteral("%1/servers.json").arg(LOCAL_PATH) }; |
||||
QFile f{ path }; |
||||
if (f.open(QIODevice::ReadOnly)) { |
||||
const auto jsonData{ f.readAll() }; |
||||
qDebug() << "Read" << jsonData.size() << "bytes from" << path; |
||||
jsondoc = QJsonDocument::fromJson(jsonData); |
||||
|
||||
QPair<QString, QString> ServerModel::fromIndex(const QModelIndex& index) |
||||
{ |
||||
/* Locate netmap */ |
||||
{ |
||||
QHashIterator<QString,QModelIndex> it(netmap); |
||||
while (it.hasNext()) { |
||||
const QString& network = it.next().key(); |
||||
const QModelIndex& iidx = it.value(); |
||||
if (index == iidx || index == iidx.siblingAtColumn(1)) |
||||
return { network, "DEFAULT" }; |
||||
f.close(); |
||||
|
||||
for (int i = 0 ;; ++i) { |
||||
QModelIndex cidx = iidx.child(i, 0); |
||||
if (!cidx.isValid()) |
||||
break; |
||||
QString name = cidx.data().toString(); |
||||
if (index == cidx || index == cidx.siblingAtColumn(1)) |
||||
return { network, name }; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Locate nonemap */ |
||||
{ |
||||
QHashIterator<QString,QModelIndex> it(hostmap); |
||||
while (it.hasNext()) { |
||||
it.next(); |
||||
const QModelIndex& iidx = it.value(); |
||||
if (index == iidx || index == iidx.siblingAtColumn(1)) |
||||
return { "NONE", iidx.data().toString() }; |
||||
} |
||||
if (!jsondoc.isObject()) |
||||
qWarning() << "Parse error for servers.json"; |
||||
else |
||||
createItems(jsondoc.object()); |
||||
} |
||||
else |
||||
qWarning() << "Unable to open servers.json:" << path; |
||||
|
||||
return {}; |
||||
} |
||||
|
||||
QModelIndex ServerModel::addNetwork(QString name, QString server) |
||||
void ServerModel::createItems(const QJsonObject& json) |
||||
{ |
||||
QStandardItem *root = invisibleRootItem(); |
||||
constexpr auto Servers = "Servers"; |
||||
constexpr auto Networks = "Networks"; |
||||
|
||||
QStandardItem *pname = new QStandardItem(QIcon(":/options/gfx/network.png"), name); |
||||
QStandardItem *phost = new QStandardItem(server); |
||||
QList<QStandardItem*> list; |
||||
list << pname << phost; |
||||
const auto servers = json[Servers].toArray(); |
||||
const auto networks = json[Networks].toArray(); |
||||
|
||||
root->appendRow(list); |
||||
hostmap.insert(server, pname->index()); |
||||
netmap.insert(name, pname->index()); |
||||
int rootRow{ 0 }; |
||||
|
||||
if (smgr.addNetwork(name)) |
||||
smgr.addServer("DEFAULT", server, "", name); |
||||
for (const auto& server : servers) { |
||||
m_items << ServerItem(server.toObject(), rootRow++, nullptr); |
||||
} |
||||
|
||||
return pname->index(); |
||||
} |
||||
for (const auto& network : networks) { |
||||
const auto networkObj = network.toObject(); |
||||
const auto childServers = networkObj[Servers].toArray(); |
||||
|
||||
void ServerModel::setNetworkServer(QString name, QString server) |
||||
{ |
||||
QModelIndex current = netmap.value(name); |
||||
int row = current.row(); |
||||
QModelIndex serverIndex = index(row, 1, current.parent()); |
||||
m_items << ServerItem(networkObj, rootRow++, nullptr); |
||||
auto& networkItem = m_items.back(); |
||||
|
||||
QStandardItem *item = itemFromIndex(serverIndex); |
||||
item->setText(server); |
||||
smgr.addServer("DEFAULT", server, "", name); |
||||
int childRow{ 0 }; |
||||
for (const auto& cs : childServers) { |
||||
auto& networkServers = networkItem.children(); |
||||
networkServers << ServerItem(cs.toObject(), childRow++, &networkItem); |
||||
} |
||||
} |
||||
} |
||||
|
||||
void ServerModel::renameNetwork(QString name, QString newname) |
||||
QVariant ServerModel::data(const QModelIndex& index, int role) const |
||||
{ |
||||
QModelIndex current = netmap.value(name); |
||||
int row = current.row(); |
||||
QModelIndex nameIndex = index(row, 0, current.parent()); |
||||
|
||||
QStandardItem *item = itemFromIndex(nameIndex); |
||||
item->setText(newname); |
||||
|
||||
netmap.remove(name); |
||||
netmap.insert(newname, current); |
||||
smgr.renameNetwork(name, newname); |
||||
} |
||||
if (!index.isValid() || role != Qt::DisplayRole) |
||||
return {}; |
||||
|
||||
void ServerModel::delNetwork(QString name, bool keepServers) |
||||
{ |
||||
smgr.delNetwork(name, keepServers); |
||||
resetModel(); |
||||
} |
||||
auto* item = static_cast<const ServerItem*>(index.internalPointer()); |
||||
|
||||
QModelIndex ServerModel::addServer(QString name, QString server, QString network) |
||||
{ |
||||
QStandardItem *parent; |
||||
if (index.column() == 0) |
||||
return item->name(); |
||||
|
||||
if (network.length() == 0) |
||||
network = "NONE"; |
||||
else if (index.column() == 1) |
||||
return item->address(); |
||||
|
||||
if (network == "NONE") |
||||
parent = invisibleRootItem(); |
||||
else |
||||
parent = itemFromIndex( netmap.value(network) ); |
||||
|
||||
QStandardItem *sname = new QStandardItem(QIcon(":/options/gfx/server.png"), name); |
||||
QStandardItem *shost = new QStandardItem(server); |
||||
QList<QStandardItem*> list; |
||||
list << sname << shost; |
||||
|
||||
parent->appendRow(list); |
||||
hostmap.insert(server, indexFromItem(sname)); |
||||
if (network == "NONE") |
||||
nonemap.insert(name, indexFromItem(sname)); |
||||
|
||||
smgr.addServer(name, server, "", network); |
||||
|
||||
return indexFromItem(sname); |
||||
return {}; |
||||
} |
||||
|
||||
void ServerModel::setServer(QString name, QString server, QString password, QString network) |
||||
QVariant ServerModel::headerData(int section, Qt::Orientation orientation, int role) const |
||||
{ |
||||
QStandardItem *parent; |
||||
|
||||
if (network.length() == 0) |
||||
network = "NONE"; |
||||
|
||||
if (network == "NONE") |
||||
parent = invisibleRootItem(); |
||||
else |
||||
parent = itemFromIndex( netmap.value(network) ); |
||||
|
||||
QString host = server; |
||||
if (host[0] == '$') |
||||
host = host.mid(1); |
||||
|
||||
QModelIndex parentIdx = indexFromItem(parent); // Parent index
|
||||
QModelIndex current; // Item's index
|
||||
|
||||
if (network != "NONE") { |
||||
|
||||
for (int r = 0 ;; r++) { |
||||
QModelIndex idx = parentIdx.child(r, 0); |
||||
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { |
||||
QStringList labels { |
||||
tr("Name"), |
||||
tr("Address") |
||||
}; |
||||
|
||||
if (! idx.isValid()) |
||||
return; // No relevant child found, stop.
|
||||
|
||||
if (idx.data().toString() == name) { |
||||
current = idx; |
||||
break; |
||||
} |
||||
} |
||||
return labels[section]; |
||||
} |
||||
else { |
||||
current = nonemap.value(name); |
||||
return {}; |
||||
} |
||||
|
||||
|
||||
int row = current.row(); |
||||
QModelIndex nameIndex = index(row, 0, current.parent()); |
||||
QModelIndex serverIndex = index(row, 1, current.parent()); |
||||
QStandardItem *serverItem = itemFromIndex(serverIndex); |
||||
serverItem->setText(host); |
||||
|
||||
hostmap.insert(host, nameIndex); |
||||
smgr.addServer(name, server, password, network); |
||||
} |
||||
|
||||
void ServerModel::renameServer(QString name, QString newname, QString network) |
||||
int ServerModel::rowCount(const QModelIndex& parent) const |
||||
{ |
||||
QStandardItem *parent; |
||||
|
||||
if (network.length() == 0) |
||||
network = "NONE"; |
||||
|
||||
if (network == "NONE") |
||||
parent = invisibleRootItem(); |
||||
else |
||||
parent = itemFromIndex( netmap.value(network) ); |
||||
|
||||
QModelIndex parentIdx = indexFromItem(parent); // Parent index
|
||||
QModelIndex current; // Item's index
|
||||
|
||||
if (network != "NONE") { |
||||
|
||||
for (int r = 0 ;; r++) { |
||||
QModelIndex idx = parentIdx.child(r, 0); |
||||
|
||||
if (! idx.isValid()) |
||||
return; // No relevant child found, stop.
|
||||
|
||||
if (idx.data().toString() == name) { |
||||
current = idx; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
if (!parent.isValid()) |
||||
return m_items.count(); |
||||
else { |
||||
current = nonemap.value(name); |
||||
nonemap.remove(name); |
||||
nonemap.insert(newname, current); |
||||
auto* parentItem = static_cast<ServerItem*>(parent.internalPointer()); |
||||
return parentItem->children().count(); |
||||
} |
||||
|
||||
int row = current.row(); |
||||
QModelIndex serverIndex = index(row, 0, current.parent()); |
||||
QStandardItem *item = itemFromIndex(serverIndex); |
||||
item->setText(newname); |
||||
|
||||
QString details = smgr.getServerDetails(name, network); |
||||
smgr.delServer(name, network); |
||||
smgr.addServer(name, details, "", network); |
||||
} |
||||
|
||||
void ServerModel::delServer(QString name, QString network) |
||||
QModelIndex ServerModel::index(int row, int column, const QModelIndex& parent) const |
||||
{ |
||||
QStandardItem *parent; |
||||
|
||||
if (network.length() == 0) |
||||
network = "NONE"; |
||||
|
||||
if (network == "NONE") |
||||
parent = invisibleRootItem(); |
||||
else |
||||
parent = itemFromIndex( netmap.value(network) ); |
||||
|
||||
QModelIndex parentIdx = indexFromItem(parent); // Parent index
|
||||
QModelIndex current; // Item's index
|
||||
if (!hasIndex(row, column, parent)) |
||||
return {}; |
||||
|
||||
if (network != "NONE") { |
||||
ServerItem* item{}; |
||||
|
||||
for (int r = 0 ;; r++) { |
||||
QModelIndex idx = parentIdx.child(r, 0); |
||||
|
||||
if (! idx.isValid()) |
||||
return; // No relevant child found, stop.
|
||||
|
||||
if (idx.data().toString() == name) { |
||||
current = idx; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
if (!parent.isValid()) |
||||
item = &m_items[row]; |
||||
else { |
||||
current = nonemap.value(name); |
||||
nonemap.remove(name); |
||||
auto* parentItem = static_cast<ServerItem*>(parent.internalPointer()); |
||||
item = &parentItem->children()[row]; |
||||
} |
||||
|
||||
|
||||
int row = current.row(); |
||||
removeRow(row, current.parent()); |
||||
smgr.delServer(name, network); |
||||
return createIndex(row, column, item); |
||||
} |
||||
|
||||
void ServerModel::resetModel() |
||||
QModelIndex ServerModel::parent(const QModelIndex& index) const |
||||
{ |
||||
clear(); |
||||
|
||||
QStandardItem *root = invisibleRootItem(); |
||||
|
||||
QStandardItem *i = new QStandardItem(); |
||||
QStringList l; |
||||
l << tr("Name") << tr("Host"); |
||||
setColumnCount(2); |
||||
setHorizontalHeaderItem(0, i); |
||||
setHorizontalHeaderLabels(l); |
||||
|
||||
hostmap.clear(); |
||||
netmap.clear(); |
||||
nonemap.clear(); |
||||
|
||||
QStringList netlist = smgr.networkList(); |
||||
|
||||
if (netlist.contains("NONE")) { // "None" network is a section with servers not assigned to a network.
|
||||
QHash<QString,QString> sl = smgr.serverList("NONE"); |
||||
QHashIterator<QString,QString> i(sl); |
||||
while (i.hasNext()) { |
||||
i.next(); |
||||
// Key: Server name
|
||||
// Value: host:port|pass
|
||||
QString name = i.key(); |
||||
QString detail = i.value(); |
||||
if (!index.isValid()) |
||||
return {}; |
||||
|
||||
QString host; // hostname with port, e.g. irc.network.org:6667
|
||||
host = detail.split('|')[0]; |
||||
auto* parentItem = static_cast<ServerItem*>(index.internalPointer())->parent(); |
||||
if (!parentItem) |
||||
return {}; |
||||
|
||||
if (host[0] == '$') |
||||
host = host.mid(1); |
||||
|
||||
QStandardItem *itemname = new QStandardItem(QIcon(":/options/gfx/server.png"), name); |
||||
QStandardItem *itemhost = new QStandardItem(host); |
||||
QList<QStandardItem*> list; |
||||
list << itemname << itemhost; |
||||
|
||||
root->appendRow(list); |
||||
hostmap.insert(host, indexFromItem(itemname)); |
||||
nonemap.insert(name, indexFromItem(itemname)); |
||||
} |
||||
} |
||||
|
||||
for (int i = 0; i <= netlist.count()-1; ++i) { |
||||
if (netlist[i] == "NONE") |
||||
continue; // The "None" network already taken care of - ignore.
|
||||
|
||||
QString data = smgr.defaultServer(netlist[i]); |
||||
QString host = data.split('|')[0]; |
||||
|
||||
QStandardItem *pname = new QStandardItem(QIcon(":/options/gfx/network.png"), netlist[i]); // parent name
|
||||
QStandardItem *phost = new QStandardItem(host); // parent host
|
||||
QList<QStandardItem*> list; |
||||
list << pname << phost; |
||||
|
||||
root->appendRow(list); |
||||
hostmap.insert(host, pname->index()); |
||||
netmap.insert(netlist[i], pname->index()); |
||||
|
||||
QHash<QString,QString> sl = smgr.serverList(netlist[i]); |
||||
QHashIterator<QString,QString> sli(sl); |
||||
while (sli.hasNext()) { |
||||
sli.next(); |
||||
// Key: Server name
|
||||
// Value: host:port|pass
|
||||
QString name = sli.key(); |
||||
if (name == "DEFAULT") |
||||
continue; // The default value already taken care of, it's the address of parent item.
|
||||
QString detail = sli.value(); |
||||
QString host; // hostname with port, e.g. irc.network.org:6667
|
||||
host = detail.split('|')[0]; |
||||
|
||||
if (host[0] == '$') |
||||
host = host.mid(1); |
||||
|
||||
QStandardItem *itemname = new QStandardItem(QIcon(":/options/gfx/server.png"), name); // parent name
|
||||
QStandardItem *itemhost = new QStandardItem(host); // parent host
|
||||
QList<QStandardItem*> list; |
||||
list << itemname << itemhost; |
||||
|
||||
pname->appendRow(list); |
||||
hostmap.insert(host, indexFromItem(itemname)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
QStringList ServerModel::networkList() |
||||
{ |
||||
return smgr.networkList(); |
||||
} |
||||
|
||||
QString ServerModel::details(QString name, QString network) |
||||
{ |
||||
return smgr.getServerDetails(name, network); |
||||
return createIndex(parentItem->row(), 0, parentItem); |
||||
} |
||||
|
Loading…
Reference in new issue