2023-01-09 12:38:01 +03:00
|
|
|
#include "clientManagementModel.h"
|
|
|
|
|
|
2023-01-17 21:04:15 +03:00
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
#include "core/utils/constants/configKeys.h"
|
2023-11-23 00:03:43 +07:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
using namespace amnezia;
|
2023-11-21 20:13:51 +07:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
ClientManagementModel::ClientManagementModel(QObject *parent)
|
|
|
|
|
: QAbstractListModel(parent)
|
2023-01-09 12:38:01 +03:00
|
|
|
{
|
2023-11-21 20:13:51 +07:00
|
|
|
}
|
2023-01-09 12:38:01 +03:00
|
|
|
|
2023-11-21 20:13:51 +07:00
|
|
|
int ClientManagementModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
return static_cast<int>(m_clientsTable.size());
|
2023-01-09 12:38:01 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 20:13:51 +07:00
|
|
|
QVariant ClientManagementModel::data(const QModelIndex &index, int role) const
|
2023-01-09 12:38:01 +03:00
|
|
|
{
|
2023-11-21 20:13:51 +07:00
|
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_clientsTable.size())) {
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto client = m_clientsTable.at(index.row()).toObject();
|
2023-11-23 00:03:43 +07:00
|
|
|
auto userData = client.value(configKey::userData).toObject();
|
2023-11-21 20:13:51 +07:00
|
|
|
|
|
|
|
|
switch (role) {
|
2023-11-23 00:03:43 +07:00
|
|
|
case ClientNameRole: return userData.value(configKey::clientName).toString();
|
2023-12-21 17:47:34 +07:00
|
|
|
case CreationDateRole: return userData.value(configKey::creationDate).toString();
|
2024-04-28 16:03:41 +03:00
|
|
|
case LatestHandshakeRole: return userData.value(configKey::latestHandshake).toString();
|
|
|
|
|
case DataReceivedRole: return userData.value(configKey::dataReceived).toString();
|
|
|
|
|
case DataSentRole: return userData.value(configKey::dataSent).toString();
|
2024-09-17 08:28:44 +02:00
|
|
|
case AllowedIpsRole: return userData.value(configKey::allowedIps).toString();
|
2023-11-21 20:13:51 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
2023-01-09 12:38:01 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
void ClientManagementModel::updateModel(const QJsonArray &clients)
|
2023-01-09 12:38:01 +03:00
|
|
|
{
|
2023-11-23 14:32:16 +07:00
|
|
|
beginResetModel();
|
2026-04-30 14:53:03 +08:00
|
|
|
m_clientsTable = clients;
|
2023-11-23 00:03:43 +07:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
2023-11-21 20:13:51 +07:00
|
|
|
|
2026-04-30 14:53:03 +08:00
|
|
|
void ClientManagementModel::updateClientName(int row, const QString &newName)
|
2023-11-23 00:03:43 +07:00
|
|
|
{
|
2026-04-30 14:53:03 +08:00
|
|
|
if (row < 0 || row >= m_clientsTable.size()) {
|
|
|
|
|
return;
|
2023-12-21 17:47:34 +07:00
|
|
|
}
|
2026-04-30 14:53:03 +08:00
|
|
|
QJsonObject client = m_clientsTable.at(row).toObject();
|
|
|
|
|
QJsonObject userData = client.value(configKey::userData).toObject();
|
|
|
|
|
userData[configKey::clientName] = newName;
|
2023-11-23 00:03:43 +07:00
|
|
|
client[configKey::userData] = userData;
|
2023-11-21 20:13:51 +07:00
|
|
|
m_clientsTable.replace(row, client);
|
2026-04-30 14:53:03 +08:00
|
|
|
const QModelIndex idx = index(row);
|
|
|
|
|
emit dataChanged(idx, idx, { ClientNameRole });
|
2024-12-10 03:17:16 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-09 12:38:01 +03:00
|
|
|
QHash<int, QByteArray> ClientManagementModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
QHash<int, QByteArray> roles;
|
2023-11-23 00:03:43 +07:00
|
|
|
roles[ClientNameRole] = "clientName";
|
2023-12-21 17:47:34 +07:00
|
|
|
roles[CreationDateRole] = "creationDate";
|
2024-04-28 16:03:41 +03:00
|
|
|
roles[LatestHandshakeRole] = "latestHandshake";
|
|
|
|
|
roles[DataReceivedRole] = "dataReceived";
|
|
|
|
|
roles[DataSentRole] = "dataSent";
|
2024-10-11 05:58:30 +04:00
|
|
|
roles[AllowedIpsRole] = "allowedIps";
|
2023-01-09 12:38:01 +03:00
|
|
|
return roles;
|
2025-12-11 15:18:36 +08:00
|
|
|
}
|