File indexing completed on 2024-02-25 16:44:38

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