File indexing completed on 2024-10-06 10:24:15

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "directoryroomsmodel.h"
0008 #include <KLocalizedString>
0009 
0010 DirectoryRoomsModel::DirectoryRoomsModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 DirectoryRoomsModel::~DirectoryRoomsModel() = default;
0016 
0017 void DirectoryRoomsModel::checkFullList()
0018 {
0019     setHasFullList(mRoomsInfo.count() == mRoomsInfo.total());
0020 }
0021 
0022 int DirectoryRoomsModel::rowCount(const QModelIndex &parent) const
0023 {
0024     Q_UNUSED(parent)
0025     return mRoomsInfo.count();
0026 }
0027 
0028 void DirectoryRoomsModel::addMoreElements(const QJsonObject &obj)
0029 {
0030     const int numberOfElement = mRoomsInfo.count();
0031     mRoomsInfo.parseMoreRooms(obj, RoomsInfo::Directory);
0032     beginInsertRows(QModelIndex(), numberOfElement, mRoomsInfo.count() - 1);
0033     endInsertRows();
0034     checkFullList();
0035 }
0036 
0037 QList<int> DirectoryRoomsModel::excludeResizeToContentColumns() const
0038 {
0039     return {Topic};
0040 }
0041 
0042 QList<int> DirectoryRoomsModel::hideColumns() const
0043 {
0044     return {Identifier, ChannelType, CreatedDateTime, LastMessageDateTime};
0045 }
0046 
0047 void DirectoryRoomsModel::parseElements(const QJsonObject &roomsObj)
0048 {
0049     if (rowCount() != 0) {
0050         beginResetModel();
0051         mRoomsInfo.clear();
0052         endResetModel();
0053     }
0054     mRoomsInfo.parseRooms(roomsObj, RoomsInfo::Directory);
0055     if (!mRoomsInfo.isEmpty()) {
0056         beginInsertRows(QModelIndex(), 0, mRoomsInfo.count() - 1);
0057         endInsertRows();
0058     }
0059     checkFullList();
0060     Q_EMIT totalChanged();
0061 }
0062 
0063 QVariant DirectoryRoomsModel::data(const QModelIndex &index, int role) const
0064 {
0065     if (index.row() < 0 || index.row() >= mRoomsInfo.count()) {
0066         return {};
0067     }
0068     const RoomInfo &roomInfo = mRoomsInfo.at(index.row());
0069     const int col = index.column();
0070     if (role == Qt::ToolTipRole) {
0071         if (static_cast<DirectoryRoomsRoles>(col) == DirectoryRoomsRoles::Topic) {
0072             return roomInfo.topic();
0073         }
0074     }
0075 
0076     if (role != Qt::DisplayRole) {
0077         return {};
0078     }
0079 
0080     switch (static_cast<DirectoryRoomsRoles>(col)) {
0081     case DirectoryRoomsRoles::Name:
0082         return roomInfo.roomName();
0083     case DirectoryRoomsRoles::UsersCount:
0084         return roomInfo.usersCount();
0085     case DirectoryRoomsRoles::Topic:
0086         return roomInfo.topic();
0087     case DirectoryRoomsRoles::Identifier:
0088         return roomInfo.identifier();
0089     case DirectoryRoomsRoles::ReadOnly:
0090         return roomInfo.readOnly();
0091     case DirectoryRoomsRoles::DefaultRoom:
0092         return roomInfo.defaultRoom();
0093     case DirectoryRoomsRoles::ChannelType:
0094         return roomInfo.channelType();
0095     case DirectoryRoomsRoles::ChannelTypeStr:
0096         return roomInfo.channelTypeStr();
0097     case DirectoryRoomsRoles::LastMessageStr:
0098         return roomInfo.lastMessageDisplayDateTimeStr();
0099     case DirectoryRoomsRoles::CreatedStr:
0100         return roomInfo.createdRoomDisplayDateTimeStr();
0101     case DirectoryRoomsRoles::CreatedDateTime:
0102         return roomInfo.createdRoom();
0103     case DirectoryRoomsRoles::BelongsTo:
0104         return roomInfo.belongsTo();
0105     case DirectoryRoomsRoles::LastMessageDateTime:
0106         return roomInfo.lastMessage();
0107     }
0108     return {};
0109 }
0110 
0111 QVariant DirectoryRoomsModel::headerData(int section, Qt::Orientation orientation, int role) const
0112 {
0113     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0114         switch (static_cast<DirectoryRoomsRoles>(section)) {
0115         case DirectoryRoomsModel::Name:
0116             return i18n("Name");
0117         case DirectoryRoomsModel::UsersCount:
0118             return i18n("Number Of Users");
0119         case DirectoryRoomsModel::Topic:
0120             return i18n("Topic");
0121         case DirectoryRoomsModel::Identifier:
0122             return i18n("Identifier");
0123         case DirectoryRoomsModel::ReadOnly:
0124             return i18n("Read-only");
0125         case DirectoryRoomsModel::DefaultRoom:
0126             return i18n("Default Room");
0127         case DirectoryRoomsModel::LastMessageStr:
0128             return i18n("Last Message");
0129         case DirectoryRoomsModel::CreatedStr:
0130             return i18n("Created");
0131         case DirectoryRoomsModel::ChannelTypeStr:
0132         case DirectoryRoomsModel::ChannelType:
0133             return i18n("Channel Type");
0134         case DirectoryRoomsModel::BelongsTo:
0135             return i18n("Belongs To");
0136         case DirectoryRoomsModel::CreatedDateTime:
0137         case DirectoryRoomsModel::LastMessageDateTime:
0138             return {};
0139         }
0140     }
0141     return {};
0142 }
0143 
0144 int DirectoryRoomsModel::columnCount(const QModelIndex &parent) const
0145 {
0146     Q_UNUSED(parent)
0147     constexpr int val = static_cast<int>(DirectoryRoomsModel::LastColumn) + 1;
0148     return val;
0149 }
0150 
0151 int DirectoryRoomsModel::total() const
0152 {
0153     return mRoomsInfo.total();
0154 }
0155 
0156 #include "moc_directoryroomsmodel.cpp"