File indexing completed on 2024-03-03 15:47:12

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 "roominfo/roomsinfo.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 RoomsInfo::RoomsInfo() = default;
0013 
0014 bool RoomsInfo::isEmpty() const
0015 {
0016     return mRooms.isEmpty();
0017 }
0018 
0019 void RoomsInfo::clear()
0020 {
0021     mRooms.clear();
0022 }
0023 
0024 int RoomsInfo::count() const
0025 {
0026     return mRooms.count();
0027 }
0028 
0029 RoomInfo RoomsInfo::at(int index) const
0030 {
0031     if (index < 0 || index > mRooms.count()) {
0032         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0033         return {};
0034     }
0035     return mRooms.at(index);
0036 }
0037 
0038 void RoomsInfo::parseMoreRooms(const QJsonObject &obj, RoomsInfo::ParseType type)
0039 {
0040     const int adminRoomsCount = obj[QLatin1String("count")].toInt();
0041     mOffset = obj[QLatin1String("offset")].toInt();
0042     mTotal = obj[QLatin1String("total")].toInt();
0043     parseListRooms(obj, type);
0044     mRoomsCount += adminRoomsCount;
0045 }
0046 
0047 void RoomsInfo::parseListRooms(const QJsonObject &adminRoomsObj, RoomsInfo::ParseType type)
0048 {
0049     QString jsonKeyType;
0050     switch (type) {
0051     case Administrator:
0052         jsonKeyType = QStringLiteral("rooms");
0053         break;
0054     case Directory:
0055         jsonKeyType = QStringLiteral("result");
0056         break;
0057     }
0058 
0059     const QJsonArray adminRoomsArray = adminRoomsObj[jsonKeyType].toArray();
0060     mRooms.reserve(mRooms.count() + adminRoomsArray.count());
0061     for (const QJsonValue &current : adminRoomsArray) {
0062         if (current.type() == QJsonValue::Object) {
0063             const QJsonObject adminRoomObject = current.toObject();
0064             RoomInfo m;
0065             m.parseRoomInfo(adminRoomObject);
0066             mRooms.append(std::move(m));
0067         } else {
0068             qCWarning(RUQOLA_LOG) << "Problem when parsing Rooms" << current;
0069         }
0070     }
0071 }
0072 
0073 int RoomsInfo::roomsCount() const
0074 {
0075     return mRoomsCount;
0076 }
0077 
0078 void RoomsInfo::setRoomsCount(int count)
0079 {
0080     mRoomsCount = count;
0081 }
0082 
0083 RoomInfo RoomsInfo::takeAt(int index)
0084 {
0085     return mRooms.takeAt(index);
0086 }
0087 
0088 void RoomsInfo::insertRoom(int index, const RoomInfo &room)
0089 {
0090     mRooms.insert(index, room);
0091 }
0092 
0093 QVector<RoomInfo> RoomsInfo::rooms() const
0094 {
0095     return mRooms;
0096 }
0097 
0098 void RoomsInfo::setRooms(const QVector<RoomInfo> &rooms)
0099 {
0100     mRooms = rooms;
0101 }
0102 
0103 void RoomsInfo::parseRooms(const QJsonObject &obj, RoomsInfo::ParseType type)
0104 {
0105     mRoomsCount = obj[QLatin1String("count")].toInt();
0106     mOffset = obj[QLatin1String("offset")].toInt();
0107     mTotal = obj[QLatin1String("total")].toInt();
0108     mRooms.clear();
0109     parseListRooms(obj, type);
0110 }
0111 
0112 int RoomsInfo::offset() const
0113 {
0114     return mOffset;
0115 }
0116 
0117 void RoomsInfo::setOffset(int offset)
0118 {
0119     mOffset = offset;
0120 }
0121 
0122 int RoomsInfo::total() const
0123 {
0124     return mTotal;
0125 }
0126 
0127 void RoomsInfo::setTotal(int total)
0128 {
0129     mTotal = total;
0130 }
0131 
0132 QDebug operator<<(QDebug d, const RoomsInfo &t)
0133 {
0134     d << "total " << t.total();
0135     d << "offset " << t.offset();
0136     d << "roomsCount " << t.roomsCount() << "\n";
0137     for (int i = 0, total = t.rooms().count(); i < total; ++i) {
0138         d << t.rooms().at(i) << "\n";
0139     }
0140     return d;
0141 }