Files
amnezia-client/client/ui/models/clientManagementModel.cpp
T

75 lines
2.4 KiB
C++
Raw Normal View History

2023-01-09 12:38:01 +03:00
#include "clientManagementModel.h"
#include <QJsonObject>
#include "core/utils/constants/configKeys.h"
2023-11-23 00:03:43 +07:00
using namespace amnezia;
2023-11-21 20:13:51 +07: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) {
2026-06-04 15:45:53 +01:00
case ClientIdRole: return client.value(configKey::clientId).toString();
2023-11-23 00:03:43 +07:00
case ClientNameRole: return userData.value(configKey::clientName).toString();
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();
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
}
void ClientManagementModel::updateModel(const QJsonArray &clients)
2023-01-09 12:38:01 +03:00
{
2023-11-23 14:32:16 +07:00
beginResetModel();
m_clientsTable = clients;
2023-11-23 00:03:43 +07:00
endResetModel();
}
2023-11-21 20:13:51 +07:00
void ClientManagementModel::updateClientName(int row, const QString &newName)
2023-11-23 00:03:43 +07:00
{
if (row < 0 || row >= m_clientsTable.size()) {
return;
}
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);
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;
2026-06-04 15:45:53 +01:00
roles[ClientIdRole] = "clientId";
2023-11-23 00:03:43 +07:00
roles[ClientNameRole] = "clientName";
roles[CreationDateRole] = "creationDate";
2024-04-28 16:03:41 +03:00
roles[LatestHandshakeRole] = "latestHandshake";
roles[DataReceivedRole] = "dataReceived";
roles[DataSentRole] = "dataSent";
roles[AllowedIpsRole] = "allowedIps";
2023-01-09 12:38:01 +03:00
return roles;
2025-12-11 15:18:36 +08:00
}