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

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 "directoryteamsmodel.h"
0008 #include <KLocalizedString>
0009 
0010 DirectoryTeamsModel::DirectoryTeamsModel(QObject *parent)
0011     : CustomBaseModel(parent)
0012 {
0013 }
0014 
0015 DirectoryTeamsModel::~DirectoryTeamsModel() = default;
0016 
0017 void DirectoryTeamsModel::checkFullList()
0018 {
0019     setHasFullList(mRoomsInfo.count() == mRoomsInfo.total());
0020 }
0021 
0022 int DirectoryTeamsModel::rowCount(const QModelIndex &parent) const
0023 {
0024     Q_UNUSED(parent)
0025     return mRoomsInfo.count();
0026 }
0027 
0028 QList<int> DirectoryTeamsModel::hideColumns() const
0029 {
0030     return {TeamIdentifier, CreateTeamDateTime, RoomIdentifier};
0031 }
0032 
0033 void DirectoryTeamsModel::addMoreElements(const QJsonObject &obj)
0034 {
0035     const int numberOfElement = mRoomsInfo.count();
0036     mRoomsInfo.parseMoreRooms(obj, RoomsInfo::Directory);
0037     beginInsertRows(QModelIndex(), numberOfElement, mRoomsInfo.count() - 1);
0038     endInsertRows();
0039     checkFullList();
0040 }
0041 
0042 void DirectoryTeamsModel::parseElements(const QJsonObject &obj)
0043 {
0044     if (rowCount() != 0) {
0045         beginResetModel();
0046         mRoomsInfo.clear();
0047         endResetModel();
0048     }
0049     mRoomsInfo.parseRooms(obj, RoomsInfo::Directory);
0050     if (!mRoomsInfo.isEmpty()) {
0051         beginInsertRows(QModelIndex(), 0, mRoomsInfo.count() - 1);
0052         endInsertRows();
0053     }
0054     checkFullList();
0055     Q_EMIT totalChanged();
0056 }
0057 
0058 QVariant DirectoryTeamsModel::data(const QModelIndex &index, int role) const
0059 {
0060     if (index.row() < 0 || index.row() >= mRoomsInfo.count()) {
0061         return {};
0062     }
0063     if (role != Qt::DisplayRole) {
0064         return {};
0065     }
0066     const RoomInfo &roomInfo = mRoomsInfo.at(index.row());
0067     const int col = index.column();
0068     switch (static_cast<DirectoryTeamsRoles>(col)) {
0069     case DirectoryTeamsRoles::TeamName:
0070         return roomInfo.roomName();
0071     case DirectoryTeamsRoles::RoomsCount:
0072         return roomInfo.teamInfo().roomsCount();
0073     case DirectoryTeamsRoles::TeamIdentifier:
0074         return roomInfo.teamInfo().teamId();
0075     case DirectoryTeamsRoles::RoomIdentifier:
0076         return roomInfo.identifier();
0077     case DirectoryTeamsRoles::CreateTeam:
0078         return roomInfo.createdRoomDisplayDateTimeStr();
0079     case DirectoryTeamsRoles::CreateTeamDateTime:
0080         return roomInfo.createdRoom();
0081     }
0082     return {};
0083 }
0084 
0085 QVariant DirectoryTeamsModel::headerData(int section, Qt::Orientation orientation, int role) const
0086 {
0087     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0088         switch (static_cast<DirectoryTeamsRoles>(section)) {
0089         case DirectoryTeamsModel::TeamName:
0090             return i18n("Name");
0091         case DirectoryTeamsRoles::RoomsCount:
0092             return i18n("Rooms");
0093         case DirectoryTeamsRoles::TeamIdentifier:
0094             return i18n("Identifier");
0095         case DirectoryTeamsRoles::CreateTeam:
0096             return i18n("Created");
0097         case DirectoryTeamsRoles::RoomIdentifier:
0098         case DirectoryTeamsRoles::CreateTeamDateTime:
0099             return {};
0100         }
0101     }
0102     return {};
0103 }
0104 
0105 int DirectoryTeamsModel::columnCount(const QModelIndex &parent) const
0106 {
0107     Q_UNUSED(parent)
0108     constexpr int val = static_cast<int>(DirectoryTeamsModel::LastColumn) + 1;
0109     return val;
0110 }
0111 
0112 int DirectoryTeamsModel::total() const
0113 {
0114     return mRoomsInfo.total();
0115 }
0116 
0117 #include "moc_directoryteamsmodel.cpp"