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

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "adminroomsmodel.h"
0008 #include <KLocalizedString>
0009 
0010 AdminRoomsModel::AdminRoomsModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 AdminRoomsModel::~AdminRoomsModel() = default;
0016 
0017 int AdminRoomsModel::rowCount(const QModelIndex &parent) const
0018 {
0019     if (parent.isValid()) {
0020         return 0; // flat model
0021     }
0022     return mAdminRooms.count();
0023 }
0024 
0025 QVariant AdminRoomsModel::headerData(int section, Qt::Orientation orientation, int role) const
0026 {
0027     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0028         switch (static_cast<AdminRoomsRoles>(section)) {
0029         case AdminRoomsRoles::Name:
0030             return i18n("Name");
0031         case AdminRoomsRoles::MessagesCount:
0032             return i18n("Number of Messages");
0033         case AdminRoomsRoles::UsersCount:
0034             return i18n("Number of Users");
0035         case AdminRoomsRoles::Topic:
0036             return i18n("Topic");
0037         case AdminRoomsRoles::Identifier:
0038             return i18n("Identifier");
0039         case AdminRoomsRoles::ReadOnly:
0040             return i18n("Read-only");
0041         case AdminRoomsRoles::DefaultRoom:
0042             return i18n("Default Room");
0043         case AdminRoomsRoles::ChannelType:
0044         case AdminRoomsRoles::ChannelTypeStr:
0045             return i18n("Type");
0046         case AdminRoomsRoles::IsTeam:
0047         case AdminRoomsRoles::Featured:
0048             return {};
0049         }
0050     }
0051     return {};
0052 }
0053 
0054 int AdminRoomsModel::columnCount(const QModelIndex &parent) const
0055 {
0056     Q_UNUSED(parent)
0057     constexpr int val = static_cast<int>(AdminRoomsRoles::LastColumn) + 1;
0058     return val;
0059 }
0060 
0061 QVariant AdminRoomsModel::data(const QModelIndex &index, int role) const
0062 {
0063     if (index.row() < 0 || index.row() >= mAdminRooms.count()) {
0064         return {};
0065     }
0066     if (role != Qt::DisplayRole) {
0067         return {};
0068     }
0069 
0070     const RoomInfo &adminroom = mAdminRooms.at(index.row());
0071     const int col = index.column();
0072     switch (static_cast<AdminRoomsRoles>(col)) {
0073     case AdminRoomsRoles::Name:
0074         return adminroom.roomName();
0075     case AdminRoomsRoles::MessagesCount:
0076         return adminroom.messageCount();
0077     case AdminRoomsRoles::UsersCount:
0078         return adminroom.usersCount();
0079     case AdminRoomsRoles::Topic:
0080         return adminroom.topic();
0081     case AdminRoomsRoles::Identifier:
0082         return adminroom.identifier();
0083     case AdminRoomsRoles::ReadOnly:
0084         return adminroom.readOnly();
0085     case AdminRoomsRoles::DefaultRoom:
0086         return adminroom.defaultRoom();
0087     case AdminRoomsRoles::ChannelType:
0088         return adminroom.channelType();
0089     case AdminRoomsRoles::ChannelTypeStr:
0090         return adminroom.channelTypeStr();
0091     case AdminRoomsRoles::Featured:
0092         return adminroom.featured();
0093     case AdminRoomsRoles::IsTeam:
0094         return adminroom.isTeam();
0095     }
0096 
0097     return {};
0098 }
0099 
0100 RoomsInfo AdminRoomsModel::adminRooms() const
0101 {
0102     return mAdminRooms;
0103 }
0104 
0105 void AdminRoomsModel::setAdminRooms(const RoomsInfo &adminrooms)
0106 {
0107     if (rowCount() != 0) {
0108         beginResetModel();
0109         mAdminRooms.clear();
0110         endResetModel();
0111     }
0112     if (!adminrooms.isEmpty()) {
0113         beginInsertRows(QModelIndex(), 0, adminrooms.count() - 1);
0114         mAdminRooms = adminrooms;
0115         endInsertRows();
0116     }
0117 }
0118 
0119 int AdminRoomsModel::total() const
0120 {
0121     return mAdminRooms.count();
0122 }
0123 
0124 void AdminRoomsModel::parseElements(const QJsonObject &obj)
0125 {
0126     if (rowCount() != 0) {
0127         beginRemoveRows(QModelIndex(), 0, mAdminRooms.count() - 1);
0128         mAdminRooms.clear();
0129         endRemoveRows();
0130     }
0131     mAdminRooms.parseRooms(obj, RoomsInfo::Administrator);
0132     if (!mAdminRooms.isEmpty()) {
0133         beginInsertRows(QModelIndex(), 0, mAdminRooms.count() - 1);
0134         endInsertRows();
0135     }
0136     checkFullList();
0137     Q_EMIT totalChanged();
0138 }
0139 
0140 void AdminRoomsModel::checkFullList()
0141 {
0142     setHasFullList(mAdminRooms.count() == mAdminRooms.total());
0143 }
0144 
0145 void AdminRoomsModel::addMoreElements(const QJsonObject &obj)
0146 {
0147     const int numberOfElement = mAdminRooms.count();
0148     mAdminRooms.parseMoreRooms(obj, RoomsInfo::Directory);
0149     beginInsertRows(QModelIndex(), numberOfElement, mAdminRooms.count() - 1);
0150     endInsertRows();
0151     checkFullList();
0152 }
0153 
0154 void AdminRoomsModel::updateElement(const QJsonObject &obj)
0155 {
0156     const int roomCount = mAdminRooms.count();
0157     const QString identifier{obj.value(QStringLiteral("_id")).toString()};
0158     for (int i = 0; i < roomCount; ++i) {
0159         if (mAdminRooms.at(i).identifier() == identifier) {
0160             beginRemoveRows(QModelIndex(), i, i);
0161             mAdminRooms.takeAt(i);
0162             endRemoveRows();
0163             RoomInfo room;
0164             room.parseRoomInfo(obj);
0165             beginInsertRows(QModelIndex(), i, i);
0166             mAdminRooms.insertRoom(i, room);
0167             endInsertRows();
0168             break;
0169         }
0170     }
0171 }
0172 
0173 QList<int> AdminRoomsModel::hideColumns() const
0174 {
0175     return {AdminRoomsRoles::Identifier, AdminRoomsRoles::ChannelType, AdminRoomsRoles::Featured, AdminRoomsModel::IsTeam};
0176 }
0177 
0178 void AdminRoomsModel::removeElement(const QString &identifier)
0179 {
0180     const int roomCount = mAdminRooms.count();
0181     for (int i = 0; i < roomCount; ++i) {
0182         if (mAdminRooms.at(i).identifier() == identifier) {
0183             beginRemoveRows(QModelIndex(), i, i);
0184             mAdminRooms.takeAt(i);
0185             mAdminRooms.setTotal(mAdminRooms.count()); // Update total
0186             endRemoveRows();
0187             Q_EMIT totalChanged();
0188             break;
0189         }
0190     }
0191 }
0192 
0193 #include "moc_adminroomsmodel.cpp"