#133 Removed obsolete files for Server model
parent
f4a95a8eb1
commit
2a600143d8
@ -1,125 +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 "ServerItem.h" |
||||
#include <QDebug> |
||||
|
||||
ServerItem::ServerItem() |
||||
: m_type(Type::Root) |
||||
{} |
||||
|
||||
ServerItem::ServerItem(ServerItem* parentItem, const QString& serverName, const QString& host, const quint16 port, const QString& password) |
||||
: m_type(Type::Server) |
||||
, m_serverName(serverName) |
||||
, m_host(host) |
||||
, m_port(port) |
||||
, m_password(password) |
||||
, m_parent(parentItem) |
||||
{} |
||||
|
||||
ServerItem::ServerItem(const QString& networkName, const QString& host, const quint16 port, const QString& password) |
||||
: m_type(Type::Network) |
||||
, m_networkName(networkName) |
||||
, m_host(host) |
||||
, m_port(port) |
||||
, m_password(password) |
||||
{} |
||||
|
||||
ServerItem::~ServerItem() |
||||
{ |
||||
qDeleteAll(m_children); |
||||
} |
||||
|
||||
ServerItem::Type ServerItem::getType() const |
||||
{ |
||||
return m_type; |
||||
} |
||||
|
||||
const QString& ServerItem::getNetworkName() const |
||||
{ |
||||
return m_networkName; |
||||
} |
||||
|
||||
const QString& ServerItem::getServerName() const |
||||
{ |
||||
return m_serverName; |
||||
} |
||||
|
||||
const QString& ServerItem::getHost() const |
||||
{ |
||||
return m_host; |
||||
} |
||||
|
||||
quint16 ServerItem::getPort() const |
||||
{ |
||||
return m_port; |
||||
} |
||||
|
||||
const QString& ServerItem::getPassword() const |
||||
{ |
||||
return m_password; |
||||
} |
||||
|
||||
ServerItem*ServerItem::getParentItem() const |
||||
{ |
||||
return m_parent; |
||||
} |
||||
|
||||
bool ServerItem::isOrphan() const |
||||
{ |
||||
return m_parent == nullptr; |
||||
} |
||||
|
||||
bool ServerItem::addChild(ServerItem* child) |
||||
{ |
||||
if (m_type == Type::Server) { |
||||
qWarning() << "Tried to add a child item on a server entry!"; |
||||
return false; |
||||
} |
||||
m_children.push_back(child); |
||||
return true; |
||||
} |
||||
|
||||
bool ServerItem::delChild(ServerItem* child) |
||||
{ |
||||
if (m_type == Type::Server) { |
||||
qWarning() << "Tried to delete a child item on a server entry!"; |
||||
return false; |
||||
} |
||||
m_children.removeOne(child); |
||||
return true; |
||||
} |
||||
|
||||
ServerItem* ServerItem::getChild(int row) const |
||||
{ |
||||
if (m_type == Type::Server) { |
||||
qWarning() << "Tried to get a child item on a server entry!"; |
||||
return nullptr; |
||||
} |
||||
|
||||
if (row >= m_children.count()) { |
||||
qWarning() << "Tried to get child from row" << row << "- but it's out of index. Network:" << m_networkName; |
||||
return nullptr; |
||||
} |
||||
|
||||
return m_children[row]; |
||||
} |
||||
|
||||
int ServerItem::childCount() const |
||||
{ |
||||
return m_children.count(); |
||||
} |
||||
|
||||
int ServerItem::childRow(ServerItem* child) const |
||||
{ |
||||
return m_children.indexOf(child); |
||||
} |
||||
|
||||
ServerItem*ServerItem::getParent() const |
||||
{ |
||||
return m_parent; |
||||
} |
@ -1,67 +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 SERVERITEM_H |
||||
#define SERVERITEM_H |
||||
|
||||
#include <QString> |
||||
#include <QList> |
||||
|
||||
class ServerItem |
||||
{ |
||||
public: |
||||
enum class Type { |
||||
Root, |
||||
Server, |
||||
Network |
||||
}; |
||||
|
||||
/**
|
||||
* @brief Construct a root item. Should only be constructed once as the "invisible" root item in the tree. |
||||
*/ |
||||
explicit ServerItem(); |
||||
|
||||
/**
|
||||
* @brief Construct a server item. Set parentItem=nullptr for an orphaned server. |
||||
*/ |
||||
ServerItem(ServerItem* parentItem, const QString& serverName, const QString& host, const quint16 port, const QString& password); |
||||
|
||||
/**
|
||||
* @brief Construct a network item. |
||||
*/ |
||||
ServerItem(const QString& networkName, const QString& host, const quint16 port, const QString& password); |
||||
|
||||
~ServerItem(); |
||||
|
||||
Type getType() const; |
||||
const QString& getNetworkName() const; |
||||
const QString& getServerName() const; |
||||
const QString& getHost() const; |
||||
quint16 getPort() const; |
||||
const QString& getPassword() const; |
||||
ServerItem* getParentItem() const; |
||||
bool isOrphan() const; |
||||
|
||||
bool addChild(ServerItem* child); |
||||
bool delChild(ServerItem* child); |
||||
ServerItem* getChild(int row) const; |
||||
int childCount() const; |
||||
int childRow(ServerItem* child) const; |
||||
ServerItem* getParent() const; |
||||
|
||||
private: |
||||
Type m_type; |
||||
QString m_networkName; |
||||
QString m_serverName; |
||||
QString m_host; |
||||
quint16 m_port; |
||||
QString m_password; |
||||
ServerItem* m_parent{ nullptr }; |
||||
QList<ServerItem*> m_children; |
||||
}; |
||||
|
||||
#endif // SERVERITEM_H
|
@ -1,172 +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 "ServerModel.h" |
||||
#include "config.h" |
||||
#include <QDebug> |
||||
|
||||
ServerModel::ServerModel(QObject *parent) |
||||
: QAbstractItemModel(parent) |
||||
{ |
||||
IniFile ini(LOCAL_PATH+"/servers.ini"); |
||||
buildToplevelItem(ini, "NONE"); // Orphaned servers. Always build first.
|
||||
const int sectionCount = ini.countSections(); |
||||
for (int i = 0; i < sectionCount; ++i) { |
||||
QString section = ini.section(i); |
||||
if (section == "NONE") |
||||
continue; |
||||
|
||||
buildToplevelItem(ini, section); |
||||
} |
||||
|
||||
qDebug() << "Built server tree model."; |
||||
} |
||||
|
||||
ServerModel::~ServerModel() |
||||
{ |
||||
qDeleteAll(toplevelItems); |
||||
} |
||||
|
||||
ServerModel::ItemIniValue::ItemIniValue(QString value) |
||||
{ |
||||
if (value.isEmpty()) |
||||
return; |
||||
|
||||
// Value format: host:port|password
|
||||
if (value.contains('|')) { |
||||
password = value.split('|')[1]; |
||||
value = value.remove("|"+password); |
||||
} |
||||
if (value.contains(':')) { |
||||
port = value.split(':')[1]; |
||||
value = value.remove(":"+port); |
||||
} |
||||
host = value; |
||||
} |
||||
|
||||
QModelIndex ServerModel::index(int row, int column, const QModelIndex &parent) const |
||||
{ |
||||
// TEST
|
||||
//if (!hasIndex(row, column, parent))
|
||||
// return QModelIndex();
|
||||
|
||||
ServerItem* item{ nullptr }; |
||||
if (!parent.isValid()) { |
||||
if (row < toplevelItems.count()) |
||||
item = toplevelItems[row]; |
||||
} |
||||
else |
||||
item = static_cast<ServerItem*>(parent.internalPointer()); |
||||
|
||||
if (item) |
||||
return createIndex(row, column, item); |
||||
else |
||||
return QModelIndex(); |
||||
} |
||||
|
||||
QModelIndex ServerModel::parent(const QModelIndex &index) const |
||||
{ |
||||
// FIXME: Implement me!
|
||||
if (!index.isValid()) |
||||
return QModelIndex(); |
||||
|
||||
ServerItem* childItem = static_cast<ServerItem*>(index.internalPointer()); |
||||
if (!childItem->isOrphan()) { |
||||
ServerItem* parentItem = childItem->getParentItem(); |
||||
return createIndex(parentItem->childRow(childItem), 0, parentItem); |
||||
} |
||||
else { |
||||
return QModelIndex(); |
||||
} |
||||
} |
||||
|
||||
int ServerModel::rowCount(const QModelIndex &parent) const |
||||
{ |
||||
if (!parent.isValid()) |
||||
return toplevelItems.count(); |
||||
|
||||
ServerItem* item = static_cast<ServerItem*>(parent.internalPointer()); |
||||
if (!item) |
||||
return 0; |
||||
else |
||||
return item->childCount(); |
||||
} |
||||
|
||||
int ServerModel::columnCount(const QModelIndex&) const |
||||
{ |
||||
return 1; |
||||
} |
||||
|
||||
QVariant ServerModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (!index.isValid()) |
||||
return QVariant(); |
||||
|
||||
// FIXME: Implement me!
|
||||
return "neger"; |
||||
} |
||||
|
||||
bool ServerModel::setData(const QModelIndex &index, const QVariant &value, int role) |
||||
{ |
||||
if (data(index, role) != value) { |
||||
// FIXME: Implement me!
|
||||
emit dataChanged(index, index, QVector<int>() << role); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
Qt::ItemFlags ServerModel::flags(const QModelIndex &index) const |
||||
{ |
||||
if (!index.isValid()) |
||||
return Qt::NoItemFlags; |
||||
|
||||
return Qt::ItemIsEditable; // FIXME: Implement me!
|
||||
} |
||||
|
||||
bool ServerModel::insertRows(int row, int count, const QModelIndex &parent) |
||||
{ |
||||
beginInsertRows(parent, row, row + count - 1); |
||||
// FIXME: Implement me!
|
||||
endInsertRows(); |
||||
} |
||||
|
||||
bool ServerModel::removeRows(int row, int count, const QModelIndex &parent) |
||||
{ |
||||
beginRemoveRows(parent, row, row + count - 1); |
||||
// FIXME: Implement me!
|
||||
endRemoveRows(); |
||||
} |
||||
|
||||
void ServerModel::buildToplevelItem(IniFile& ini, const QString& section) |
||||
{ |
||||
if (!ini.sectionExists(section)) |
||||
return; |
||||
|
||||
ServerItem* parent{ nullptr }; |
||||
if (section != "NONE") { |
||||
ItemIniValue networkDefault(ini.value(section, "DEFAULT")); |
||||
parent = new ServerItem(section, networkDefault.host, |
||||
networkDefault.port.toUShort(), |
||||
networkDefault.password); |
||||
toplevelItems.push_back(parent); |
||||
} |
||||
|
||||
const int itemCount = ini.countItems(section); |
||||
for (int i = 0; i < itemCount; ++i) { |
||||
QString key = ini.key(section, i); |
||||
if (key == "DEFAULT") |
||||
continue; |
||||
|
||||
ItemIniValue val(ini.value(section, key)); |
||||
ServerItem* item = new ServerItem(parent, key, val.host, val.port.toUShort(), val.password); |
||||
if (parent) |
||||
parent->addChild(item); |
||||
else |
||||
toplevelItems.push_back(item); |
||||
} |
||||
} |
@ -1,54 +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 SERVERMODEL_H |
||||
#define SERVERMODEL_H |
||||
|
||||
#include "ServerItem.h" |
||||
#include "IniFile.h" |
||||
#include <QAbstractItemModel> |
||||
#include <QList> |
||||
|
||||
class ServerModel : public QAbstractItemModel |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
explicit ServerModel(QObject *parent = nullptr); |
||||
~ServerModel() override; |
||||
|
||||
// Basic functionality:
|
||||
QModelIndex index(int row, int column, |
||||
const QModelIndex &parent = QModelIndex()) const override; |
||||
QModelIndex parent(const QModelIndex &index) const override; |
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
||||
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
||||
|
||||
// Editable:
|
||||
bool setData(const QModelIndex &index, const QVariant &value, |
||||
int role = Qt::EditRole) override; |
||||
|
||||
Qt::ItemFlags flags(const QModelIndex& index) const override; |
||||
|
||||
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
||||
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
||||
|
||||
private: |
||||
struct ItemIniValue { |
||||
ItemIniValue(QString value); |
||||
QString host; |
||||
QString port; |
||||
QString password; |
||||
}; |
||||
void buildToplevelItem(IniFile& ini, const QString& section); |
||||
QList<ServerItem*> toplevelItems; |
||||
}; |
||||
|
||||
#endif // SERVERMODEL_H
|
Loading…
Reference in new issue