Warning, file /network/ruqola/src/core/model/deviceinfomodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "deviceinfomodel.h"
0008 #include <KLocalizedString>
0009 
0010 DeviceInfoModel::DeviceInfoModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 DeviceInfoModel::~DeviceInfoModel() = default;
0016 
0017 int DeviceInfoModel::rowCount(const QModelIndex &parent) const
0018 {
0019     if (parent.isValid()) {
0020         return 0; // flat model
0021     }
0022     return mDeviceInfos.count();
0023 }
0024 
0025 QVariant DeviceInfoModel::headerData(int section, Qt::Orientation orientation, int role) const
0026 {
0027     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0028         switch (static_cast<DeviceInfoRoles>(section)) {
0029         case DeviceInfoRoles::Identifier:
0030             return {};
0031         case DeviceInfoRoles::Host:
0032             return i18n("Host");
0033         case DeviceInfoRoles::Os:
0034             return i18n("Os");
0035         case DeviceInfoRoles::Client:
0036             return i18n("Client");
0037         case DeviceInfoRoles::SessionId:
0038             return i18n("Session Id");
0039         case DeviceInfoRoles::Ip:
0040             return i18n("Ip");
0041         case DeviceInfoRoles::UserId:
0042             return {};
0043         case DeviceInfoRoles::LoginAt:
0044             return i18n("Login At");
0045         }
0046     }
0047     return {};
0048 }
0049 
0050 int DeviceInfoModel::columnCount(const QModelIndex &parent) const
0051 {
0052     Q_UNUSED(parent)
0053     constexpr int val = static_cast<int>(DeviceInfoRoles::LastColumn) + 1;
0054     return val;
0055 }
0056 
0057 QVariant DeviceInfoModel::data(const QModelIndex &index, int role) const
0058 {
0059     if (index.row() < 0 || index.row() >= mDeviceInfos.count()) {
0060         return {};
0061     }
0062     if (role != Qt::DisplayRole) {
0063         return {};
0064     }
0065 
0066     const DeviceInfo &deviceInfo = mDeviceInfos.at(index.row());
0067     const int col = index.column();
0068     switch (static_cast<DeviceInfoRoles>(col)) {
0069     case DeviceInfoRoles::Identifier:
0070         return deviceInfo.identifier();
0071     case DeviceInfoRoles::Host:
0072         return deviceInfo.host();
0073     case DeviceInfoRoles::SessionId:
0074         return deviceInfo.sessionId();
0075     case DeviceInfoRoles::Ip:
0076         return deviceInfo.ip();
0077     case DeviceInfoRoles::UserId:
0078         return deviceInfo.userId();
0079     case DeviceInfoRoles::LoginAt:
0080         return deviceInfo.loginAtDisplay();
0081     case DeviceInfoRoles::Os:
0082         return deviceInfo.os();
0083     case DeviceInfoRoles::Client:
0084         return deviceInfo.client();
0085     }
0086     return {};
0087 }
0088 
0089 int DeviceInfoModel::total() const
0090 {
0091     return mDeviceInfos.count();
0092 }
0093 
0094 void DeviceInfoModel::clear()
0095 {
0096     if (!mDeviceInfos.isEmpty()) {
0097         beginResetModel();
0098         mDeviceInfos.clear();
0099         endResetModel();
0100     }
0101 }
0102 
0103 void DeviceInfoModel::parseElements(const QJsonObject &obj)
0104 {
0105     clear();
0106     mDeviceInfos.parseDeviceInfos(obj);
0107     if (!mDeviceInfos.isEmpty()) {
0108         beginInsertRows(QModelIndex(), 0, mDeviceInfos.count() - 1);
0109         endInsertRows();
0110     }
0111     checkFullList();
0112     Q_EMIT totalChanged();
0113 }
0114 
0115 void DeviceInfoModel::checkFullList()
0116 {
0117     setHasFullList(mDeviceInfos.count() == mDeviceInfos.total());
0118 }
0119 
0120 const DeviceInfos &DeviceInfoModel::deviceInfos() const
0121 {
0122     return mDeviceInfos;
0123 }
0124 
0125 void DeviceInfoModel::setDeviceInfos(const DeviceInfos &newDeviceInfos)
0126 {
0127     clear();
0128     if (!mDeviceInfos.isEmpty()) {
0129         beginInsertRows(QModelIndex(), 0, mDeviceInfos.count() - 1);
0130         mDeviceInfos = newDeviceInfos;
0131         endInsertRows();
0132     }
0133 }
0134 
0135 void DeviceInfoModel::addMoreElements(const QJsonObject &obj)
0136 {
0137     const int numberOfElement = mDeviceInfos.count();
0138     mDeviceInfos.parseDeviceInfos(obj);
0139     beginInsertRows(QModelIndex(), numberOfElement, mDeviceInfos.count() - 1);
0140     endInsertRows();
0141     checkFullList();
0142 }
0143 
0144 QList<int> DeviceInfoModel::hideColumns() const
0145 {
0146     return {DeviceInfoRoles::Identifier, DeviceInfoRoles::UserId, DeviceInfoRoles::SessionId};
0147 }
0148 
0149 void DeviceInfoModel::removeElement(const QString &identifier)
0150 {
0151     const int userCount = mDeviceInfos.count();
0152     for (int i = 0; i < userCount; ++i) {
0153         if (mDeviceInfos.at(i).sessionId() == identifier) {
0154             beginRemoveRows(QModelIndex(), i, i);
0155             mDeviceInfos.takeAt(i);
0156             mDeviceInfos.setTotal(mDeviceInfos.count()); // Update total
0157             endRemoveRows();
0158             Q_EMIT totalChanged();
0159             break;
0160         }
0161     }
0162 }
0163 
0164 #include "moc_deviceinfomodel.cpp"