File indexing completed on 2024-05-12 05:01:45

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