File indexing completed on 2024-02-25 16:44:36

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 "discussionsmodel.h"
0008 #include "discussions/discussions.h"
0009 
0010 DiscussionsModel::DiscussionsModel(QObject *parent)
0011     : QAbstractListModel(parent)
0012     , mDiscussions(new Discussions)
0013 {
0014 }
0015 
0016 DiscussionsModel::~DiscussionsModel()
0017 {
0018     delete mDiscussions;
0019 }
0020 
0021 void DiscussionsModel::checkFullList()
0022 {
0023     setHasFullList(mDiscussions->discussions().count() == mDiscussions->total());
0024 }
0025 
0026 bool DiscussionsModel::loadMoreDiscussionsInProgress() const
0027 {
0028     return mLoadMoreDiscussionsInProgress;
0029 }
0030 
0031 void DiscussionsModel::setLoadMoreDiscussionsInProgress(bool loadMoreDiscussionsInProgress)
0032 {
0033     if (mLoadMoreDiscussionsInProgress != loadMoreDiscussionsInProgress) {
0034         mLoadMoreDiscussionsInProgress = loadMoreDiscussionsInProgress;
0035         Q_EMIT loadingInProgressChanged();
0036     }
0037 }
0038 
0039 void DiscussionsModel::initialize()
0040 {
0041     mRoomId.clear();
0042     mLoadMoreDiscussionsInProgress = false;
0043     setHasFullList(false);
0044 }
0045 
0046 QString DiscussionsModel::roomId() const
0047 {
0048     return mRoomId;
0049 }
0050 
0051 void DiscussionsModel::setRoomId(const QString &roomId)
0052 {
0053     mRoomId = roomId;
0054 }
0055 
0056 int DiscussionsModel::rowCount(const QModelIndex &parent) const
0057 {
0058     Q_UNUSED(parent)
0059     return mDiscussions->count();
0060 }
0061 
0062 void DiscussionsModel::addMoreDiscussions(const QJsonObject &discussionsObj)
0063 {
0064     const int numberOfElement = mDiscussions->discussions().count();
0065     mDiscussions->parseMoreDiscussions(discussionsObj);
0066     beginInsertRows(QModelIndex(), numberOfElement, mDiscussions->discussions().count() - 1);
0067     endInsertRows();
0068     checkFullList();
0069 }
0070 
0071 Discussions *DiscussionsModel::discussions() const
0072 {
0073     return mDiscussions;
0074 }
0075 
0076 int DiscussionsModel::total() const
0077 {
0078     if (mDiscussions) {
0079         return mDiscussions->total();
0080     }
0081     return -1;
0082 }
0083 
0084 void DiscussionsModel::clear()
0085 {
0086     beginResetModel();
0087     mRoomId.clear();
0088     mDiscussions->clear();
0089     endResetModel();
0090 }
0091 
0092 void DiscussionsModel::parseDiscussions(const QJsonObject &discussionsObj, const QString &roomId)
0093 {
0094     mRoomId = roomId;
0095     if (!mDiscussions->isEmpty()) {
0096         beginResetModel();
0097         mDiscussions->clear();
0098         endResetModel();
0099     }
0100     mDiscussions->parseDiscussions(discussionsObj);
0101     if (!mDiscussions->isEmpty()) {
0102         beginInsertRows(QModelIndex(), 0, mDiscussions->discussions().count() - 1);
0103         endInsertRows();
0104     }
0105     checkFullList();
0106 }
0107 
0108 QVariant DiscussionsModel::data(const QModelIndex &index, int role) const
0109 {
0110     if (index.row() < 0 || index.row() >= mDiscussions->count()) {
0111         return {};
0112     }
0113     const Discussion discussion = mDiscussions->at(index.row());
0114     switch (role) {
0115     case ParentId:
0116         return discussion.parentRoomId();
0117     case Description:
0118         return discussion.description().isEmpty() ? discussion.fname() : discussion.description();
0119     case NumberOfMessages:
0120         return discussion.numberMessages();
0121     case Qt::DisplayRole:
0122     case LastMessage:
0123         return discussion.lastMessageDisplay();
0124     case DiscussionRoomId:
0125         return discussion.discussionRoomId();
0126     case TimeStamp:
0127         return discussion.timeStampDisplay();
0128     case SortByTimeStamp:
0129         return discussion.timeStamp();
0130     case UserName:
0131         return discussion.userName();
0132     }
0133     return {};
0134 }
0135 
0136 void DiscussionsModel::setDiscussions(const Discussions &discussions)
0137 {
0138     if (rowCount() != 0) {
0139         beginResetModel();
0140         mDiscussions->clear();
0141         endResetModel();
0142     }
0143     if (!discussions.isEmpty()) {
0144         beginInsertRows(QModelIndex(), 0, discussions.count() - 1);
0145         mDiscussions->setDiscussions(discussions.discussions());
0146         endInsertRows();
0147     }
0148     checkFullList();
0149 }
0150 
0151 void DiscussionsModel::setHasFullList(bool state)
0152 {
0153     if (mHasFullList != state) {
0154         mHasFullList = state;
0155         Q_EMIT hasFullListChanged();
0156     }
0157 }
0158 
0159 bool DiscussionsModel::hasFullList() const
0160 {
0161     return mHasFullList;
0162 }
0163 
0164 #include "moc_discussionsmodel.cpp"