File indexing completed on 2024-03-03 15:46:59

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "deviceinfos.h"
0008 #include "ruqola_debug.h"
0009 
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 
0013 DeviceInfos::DeviceInfos() = default;
0014 
0015 QDebug operator<<(QDebug d, const DeviceInfos &t)
0016 {
0017     d.space() << "total" << t.total();
0018     d.space() << "offset" << t.offset();
0019     d.space() << "deviceInfosCount" << t.deviceInfosCount() << "\n";
0020     for (int i = 0, total = t.deviceInfosList().count(); i < total; ++i) {
0021         d.space() << t.deviceInfosList().at(i) << "\n";
0022     }
0023     return d;
0024 }
0025 
0026 int DeviceInfos::offset() const
0027 {
0028     return mOffset;
0029 }
0030 
0031 void DeviceInfos::setOffset(int newOffset)
0032 {
0033     mOffset = newOffset;
0034 }
0035 
0036 int DeviceInfos::total() const
0037 {
0038     return mTotal;
0039 }
0040 
0041 void DeviceInfos::setTotal(int newTotal)
0042 {
0043     mTotal = newTotal;
0044 }
0045 
0046 int DeviceInfos::deviceInfosCount() const
0047 {
0048     return mDeviceInfosCount;
0049 }
0050 
0051 void DeviceInfos::setDeviceInfosCount(int newDeviceInfosCount)
0052 {
0053     mDeviceInfosCount = newDeviceInfosCount;
0054 }
0055 
0056 const QVector<DeviceInfo> &DeviceInfos::deviceInfosList() const
0057 {
0058     return mDeviceInfosList;
0059 }
0060 
0061 void DeviceInfos::setDeviceInfosList(const QVector<DeviceInfo> &newDeviceInfosList)
0062 {
0063     mDeviceInfosList = newDeviceInfosList;
0064 }
0065 
0066 bool DeviceInfos::isEmpty() const
0067 {
0068     return mDeviceInfosList.isEmpty();
0069 }
0070 
0071 void DeviceInfos::clear()
0072 {
0073     mDeviceInfosList.clear();
0074 }
0075 
0076 int DeviceInfos::count() const
0077 {
0078     return mDeviceInfosList.count();
0079 }
0080 
0081 DeviceInfo DeviceInfos::at(int index) const
0082 {
0083     if (index < 0 || index > mDeviceInfosList.count()) {
0084         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0085         return {};
0086     }
0087     return mDeviceInfosList.at(index);
0088 }
0089 
0090 void DeviceInfos::parseDeviceInfos(const QJsonObject &deviceInfosObj)
0091 {
0092     mDeviceInfosList.clear();
0093     mDeviceInfosCount = deviceInfosObj[QLatin1String("count")].toInt();
0094     mOffset = deviceInfosObj[QLatin1String("offset")].toInt();
0095     mTotal = deviceInfosObj[QLatin1String("total")].toInt();
0096     mDeviceInfosList.reserve(mDeviceInfosCount);
0097     parseDeviceInfosObj(deviceInfosObj);
0098 }
0099 
0100 void DeviceInfos::parseDeviceInfosObj(const QJsonObject &deviceInfosObj)
0101 {
0102     const QJsonArray discussionsArray = deviceInfosObj[QLatin1String("sessions")].toArray();
0103     for (const QJsonValue &current : discussionsArray) {
0104         if (current.type() == QJsonValue::Object) {
0105             const QJsonObject discussionObject = current.toObject();
0106             DeviceInfo m;
0107             m.parseDeviceInfo(discussionObject);
0108             mDeviceInfosList.append(std::move(m));
0109         } else {
0110             qCWarning(RUQOLA_LOG) << "Problem when parsing discussions" << current;
0111         }
0112     }
0113 }
0114 
0115 void DeviceInfos::parseMoreDeviceInfos(const QJsonObject &deviceInfosObj)
0116 {
0117     const int deviceInfosCount = deviceInfosObj[QLatin1String("count")].toInt();
0118     mOffset = deviceInfosObj[QLatin1String("offset")].toInt();
0119     mTotal = deviceInfosObj[QLatin1String("total")].toInt();
0120     parseDeviceInfosObj(deviceInfosObj);
0121     mDeviceInfosCount += deviceInfosCount;
0122 }
0123 
0124 DeviceInfo DeviceInfos::takeAt(int index)
0125 {
0126     return mDeviceInfosList.takeAt(index);
0127 }