The complete source code of IdealIRC http://www.idealirc.org/
 
 
 
 
idealirc/IConfig/ServerModel.h

75 lines
2.4 KiB

/*
* IdealIRC - Internet Relay Chat client
* Copyright (C) 2022 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 <QAbstractItemModel>
#include <QList>
#include <QJsonDocument>
#include <QJsonObject>
/**
* @brief Model of servers.json
*/
class ServerModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit ServerModel(QObject* parent = nullptr);
/// Save model to servers.json
bool saveToFile();
/// Resets and reloads entire model
void reloadModel();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant& value, int role = Qt::EditRole) override;
int rowCount(const QModelIndex& parent = {}) const override;
int columnCount(const QModelIndex& parent = {}) const override
{
return 5;
};
QModelIndex index(int row, int column, const QModelIndex& parent = {}) const override;
QModelIndex parent(const QModelIndex& index) const override;
/// Add a server to the model. networkIdx = -1 means server is not part of a network.
void addServer(const QString& name, const QString& address, const QString& password, const QString& sasl, bool isSsl, int networkIdx);
/// Add a network to the model.
void addNetwork(const QString& name, const QString& address, const QString& password, const QString& sasl, bool isSsl);
void deleteEntry(const QModelIndex& index);
QStringList getNetworks() const;
QList<QModelIndex> getEditorColumns();
signals:
void newEntry(const QModelIndex& sslIdx, const QModelIndex& passwordIdx, const QModelIndex& saslIdx);
private:
QModelIndex networkIndex(int row, int col = 0);
void createItems(const QJsonObject& json);
void loadFromFile();
// I don't like this being mutable, but the nature of Qt's item model seems to have forced my hand.
// Or I've probably not done the correct implementation design. In any way, it works, but smells a bit...
// This is the top-level items as shown in the UI, same order.
mutable QList<ServerItem> m_items;
};
#endif // SERVERMODEL_H