File indexing completed on 2024-04-28 16:11:13

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