File indexing completed on 2024-12-01 07:41:14

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "listmessages.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 #include <QJsonObject>
0011 
0012 ListMessages::ListMessages() = default;
0013 ListMessages::~ListMessages() = default;
0014 
0015 void ListMessages::parseListInfo(const QJsonObject &messagesObj)
0016 {
0017     mMessagesCount = messagesObj[QLatin1String("count")].toInt();
0018     mOffset = messagesObj[QLatin1String("offset")].toInt();
0019     mTotal = messagesObj[QLatin1String("total")].toInt();
0020     mListMessages.clear();
0021 }
0022 
0023 void ListMessages::parseMessagesList(const QJsonObject &messagesObj, const QString &arrayName)
0024 {
0025     const QJsonArray messagesArray = messagesObj[arrayName.isEmpty() ? QStringLiteral("messages") : arrayName].toArray();
0026     mListMessages.reserve(messagesArray.count());
0027     for (const QJsonValue &current : messagesArray) {
0028         if (current.type() == QJsonValue::Object) {
0029             const QJsonObject messageObject = current.toObject();
0030             Message m;
0031             m.parseMessage(messageObject, true);
0032             mListMessages.append(std::move(m));
0033         } else {
0034             qCWarning(RUQOLA_LOG) << "Problem when parsing thread" << current;
0035         }
0036     }
0037 }
0038 
0039 void ListMessages::parseMessages(const QJsonObject &messagesObj, const QString &arrayName)
0040 {
0041     parseListInfo(messagesObj);
0042     parseMessagesList(messagesObj, arrayName);
0043 }
0044 
0045 int ListMessages::offset() const
0046 {
0047     return mOffset;
0048 }
0049 
0050 void ListMessages::setOffset(int offset)
0051 {
0052     mOffset = offset;
0053 }
0054 
0055 int ListMessages::total() const
0056 {
0057     return mTotal;
0058 }
0059 
0060 void ListMessages::setTotal(int total)
0061 {
0062     mTotal = total;
0063 }
0064 
0065 int ListMessages::messagesCount() const
0066 {
0067     return mMessagesCount;
0068 }
0069 
0070 void ListMessages::setMessagesCount(int count)
0071 {
0072     mMessagesCount = count;
0073 }
0074 
0075 bool ListMessages::isEmpty() const
0076 {
0077     return mListMessages.isEmpty();
0078 }
0079 
0080 void ListMessages::clear()
0081 {
0082     mListMessages.clear();
0083 }
0084 
0085 int ListMessages::count() const
0086 {
0087     return mListMessages.count();
0088 }
0089 
0090 Message ListMessages::at(int index) const
0091 {
0092     if (index < 0 || index > mListMessages.count()) {
0093         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0094         return Message();
0095     }
0096     return mListMessages.at(index);
0097 }
0098 
0099 QVector<Message> ListMessages::listMessages() const
0100 {
0101     return mListMessages;
0102 }
0103 
0104 QDebug operator<<(QDebug d, const ListMessages &t)
0105 {
0106     d.space() << "total" << t.total();
0107     d.space() << "offset" << t.offset();
0108     d.space() << "MessagesCount" << t.messagesCount() << "\n";
0109     for (int i = 0, total = t.count(); i < total; ++i) {
0110         d.space() << t.at(i) << "\n";
0111     }
0112     return d;
0113 }