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

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "filesforroommodel.h"
0008 #include "rocketchataccount.h"
0009 
0010 FilesForRoomModel::FilesForRoomModel(RocketChatAccount *account, QObject *parent)
0011     : QAbstractListModel(parent)
0012     , mFileAttachments(new FileAttachments)
0013     , mRochetChantAccount(account)
0014 {
0015 }
0016 
0017 FilesForRoomModel::~FilesForRoomModel()
0018 {
0019     delete mFileAttachments;
0020 }
0021 
0022 void FilesForRoomModel::checkFullList()
0023 {
0024     setHasFullList(mFileAttachments->fileAttachments().count() == mFileAttachments->total());
0025 }
0026 
0027 bool FilesForRoomModel::loadMoreFilesInProgress() const
0028 {
0029     return mLoadMoreFilesInProgress;
0030 }
0031 
0032 void FilesForRoomModel::setLoadMoreFilesInProgress(bool loadMoreFilesInProgress)
0033 {
0034     if (mLoadMoreFilesInProgress != loadMoreFilesInProgress) {
0035         mLoadMoreFilesInProgress = loadMoreFilesInProgress;
0036         Q_EMIT loadingInProgressChanged();
0037     }
0038 }
0039 
0040 void FilesForRoomModel::clear()
0041 {
0042     beginResetModel();
0043     mFileAttachments->clear();
0044     endResetModel();
0045 }
0046 
0047 void FilesForRoomModel::addMoreFileAttachments(const QJsonObject &fileAttachmentsObj)
0048 {
0049     const int numberOfElement = mFileAttachments->fileAttachments().count();
0050     mFileAttachments->parseMoreFileAttachments(fileAttachmentsObj);
0051     beginInsertRows(QModelIndex(), numberOfElement, mFileAttachments->fileAttachments().count() - 1);
0052     endInsertRows();
0053     checkFullList();
0054 }
0055 
0056 void FilesForRoomModel::initialize()
0057 {
0058     mRoomId.clear();
0059     mLoadMoreFilesInProgress = false;
0060     setHasFullList(false);
0061 }
0062 
0063 void FilesForRoomModel::parseFileAttachments(const QJsonObject &fileAttachmentsObj, const QString &roomId)
0064 {
0065     mRoomId = roomId;
0066     if (rowCount() != 0) {
0067         clear();
0068     }
0069     mFileAttachments->parseFileAttachments(fileAttachmentsObj);
0070     if (!mFileAttachments->isEmpty()) {
0071         beginInsertRows(QModelIndex(), 0, mFileAttachments->fileAttachments().count() - 1);
0072         endInsertRows();
0073     }
0074     checkFullList();
0075     Q_EMIT totalChanged();
0076 }
0077 
0078 QString FilesForRoomModel::roomId() const
0079 {
0080     return mRoomId;
0081 }
0082 
0083 void FilesForRoomModel::setRoomId(const QString &roomId)
0084 {
0085     mRoomId = roomId;
0086 }
0087 
0088 void FilesForRoomModel::setFiles(const QVector<File> &files)
0089 {
0090     clear();
0091     if (!files.isEmpty()) {
0092         beginInsertRows(QModelIndex(), 0, files.count() - 1);
0093         mFileAttachments->setFileAttachments(files);
0094         endInsertRows();
0095     }
0096     checkFullList();
0097     Q_EMIT totalChanged();
0098 }
0099 
0100 int FilesForRoomModel::rowCount(const QModelIndex &parent) const
0101 {
0102     Q_UNUSED(parent)
0103     return mFileAttachments->fileAttachments().count();
0104 }
0105 
0106 QVariant FilesForRoomModel::data(const QModelIndex &index, int role) const
0107 {
0108     if (index.row() < 0 || index.row() >= mFileAttachments->fileAttachments().count()) {
0109         return {};
0110     }
0111 
0112     const File &file = mFileAttachments->fileAttachments().at(index.row());
0113     switch (role) {
0114     case FilePointer:
0115         return QVariant::fromValue(&file);
0116     case FileName:
0117         return file.fileName();
0118     case UserId:
0119         return file.userId();
0120     case MimeType:
0121         return file.mimeType();
0122     case Url:
0123         return file.url();
0124     case Description:
0125         return file.description();
0126     case CanBeDeleted:
0127         return mRochetChantAccount->userId() == file.userId();
0128     case FileId:
0129         return file.fileId();
0130     case TimeStamp:
0131         return file.uploadedDateTimeStr();
0132     case UserName:
0133         return file.userName();
0134     case SortByTimeStamp:
0135         return file.uploadedAt();
0136     case Complete:
0137         return file.complete();
0138     case TypeGroup:
0139         return file.typeGroup();
0140     }
0141     return {};
0142 }
0143 
0144 FileAttachments *FilesForRoomModel::fileAttachments() const
0145 {
0146     return mFileAttachments;
0147 }
0148 
0149 int FilesForRoomModel::total() const
0150 {
0151     if (mFileAttachments) {
0152         return mFileAttachments->total();
0153     }
0154     return -1;
0155 }
0156 
0157 void FilesForRoomModel::setHasFullList(bool state)
0158 {
0159     if (mHasFullList != state) {
0160         mHasFullList = state;
0161         Q_EMIT hasFullListChanged();
0162     }
0163 }
0164 
0165 bool FilesForRoomModel::hasFullList() const
0166 {
0167     return mHasFullList;
0168 }
0169 
0170 #include "moc_filesforroommodel.cpp"