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

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