File indexing completed on 2024-05-12 16:25:36

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 "customsoundsinfo.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 CustomSoundsInfo::CustomSoundsInfo() = default;
0013 
0014 bool CustomSoundsInfo::isEmpty() const
0015 {
0016     return mCustomSoundInfos.isEmpty();
0017 }
0018 
0019 void CustomSoundsInfo::clear()
0020 {
0021     mCustomSoundInfos.clear();
0022 }
0023 
0024 int CustomSoundsInfo::count() const
0025 {
0026     return mCustomSoundInfos.count();
0027 }
0028 
0029 CustomSoundInfo CustomSoundsInfo::at(int index) const
0030 {
0031     if (index < 0 || index > mCustomSoundInfos.count()) {
0032         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0033         return {};
0034     }
0035     return mCustomSoundInfos.at(index);
0036 }
0037 
0038 void CustomSoundsInfo::parseMoreCustomSounds(const QJsonObject &obj)
0039 {
0040     const int adminRoomsCount = obj[QLatin1String("count")].toInt();
0041     mOffset = obj[QLatin1String("offset")].toInt();
0042     mTotal = obj[QLatin1String("total")].toInt();
0043     parseListCustomSound(obj);
0044     mRoomsCount += adminRoomsCount;
0045 }
0046 
0047 void CustomSoundsInfo::parseListCustomSound(const QJsonObject &obj)
0048 {
0049     const QJsonArray adminRoomsArray = obj[QLatin1String("sounds")].toArray();
0050     mCustomSoundInfos.reserve(mCustomSoundInfos.count() + adminRoomsArray.count());
0051     for (const QJsonValue &current : adminRoomsArray) {
0052         if (current.type() == QJsonValue::Object) {
0053             const QJsonObject adminRoomObject = current.toObject();
0054             CustomSoundInfo m;
0055             m.parseCustomSoundInfo(adminRoomObject);
0056             mCustomSoundInfos.append(std::move(m));
0057         } else {
0058             qCWarning(RUQOLA_LOG) << "Problem when parsing Rooms" << current;
0059         }
0060     }
0061 }
0062 
0063 const QVector<CustomSoundInfo> &CustomSoundsInfo::customSoundInfos() const
0064 {
0065     return mCustomSoundInfos;
0066 }
0067 
0068 void CustomSoundsInfo::setCustomSoundInfos(const QVector<CustomSoundInfo> &newCustomSoundInfos)
0069 {
0070     mCustomSoundInfos = newCustomSoundInfos;
0071 }
0072 
0073 int CustomSoundsInfo::roomsCount() const
0074 {
0075     return mRoomsCount;
0076 }
0077 
0078 void CustomSoundsInfo::setRoomsCount(int count)
0079 {
0080     mRoomsCount = count;
0081 }
0082 
0083 void CustomSoundsInfo::parseCustomSounds(const QJsonObject &obj)
0084 {
0085     mRoomsCount = obj[QLatin1String("count")].toInt();
0086     mOffset = obj[QLatin1String("offset")].toInt();
0087     mTotal = obj[QLatin1String("total")].toInt();
0088     mCustomSoundInfos.clear();
0089     parseListCustomSound(obj);
0090 }
0091 
0092 int CustomSoundsInfo::offset() const
0093 {
0094     return mOffset;
0095 }
0096 
0097 void CustomSoundsInfo::setOffset(int offset)
0098 {
0099     mOffset = offset;
0100 }
0101 
0102 int CustomSoundsInfo::total() const
0103 {
0104     return mTotal;
0105 }
0106 
0107 void CustomSoundsInfo::setTotal(int total)
0108 {
0109     mTotal = total;
0110 }
0111 
0112 QDebug operator<<(QDebug d, const CustomSoundsInfo &t)
0113 {
0114     d.space() << "total" << t.total();
0115     d.space() << "offset" << t.offset();
0116     d.space() << "roomsCount" << t.roomsCount() << "\n";
0117     for (int i = 0, total = t.customSoundInfos().count(); i < total; ++i) {
0118         d.space() << t.customSoundInfos().at(i) << "\n";
0119     }
0120     return d;
0121 }
0122 
0123 CustomSoundInfo CustomSoundsInfo::takeAt(int index)
0124 {
0125     return mCustomSoundInfos.takeAt(index);
0126 }