File indexing completed on 2024-12-08 07:33:47

0001 // SPDX-FileCopyrightText: 2022 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "timelinemodel.h"
0005 
0006 #include "delegatetype.h"
0007 
0008 TimelineModel::TimelineModel(QObject *parent)
0009     : QConcatenateTablesProxyModel(parent)
0010 {
0011     m_messageEventModel = new MessageEventModel(this);
0012     addSourceModel(m_messageEventModel);
0013     m_timelineEndModel = new TimelineEndModel(this);
0014     addSourceModel(m_timelineEndModel);
0015 }
0016 
0017 NeoChatRoom *TimelineModel::room() const
0018 {
0019     return m_messageEventModel->room();
0020 }
0021 
0022 void TimelineModel::setRoom(NeoChatRoom *room)
0023 {
0024     // Both models do their own null checking so just pass along.
0025     m_messageEventModel->setRoom(room);
0026     m_timelineEndModel->setRoom(room);
0027 }
0028 
0029 MessageEventModel *TimelineModel::messageEventModel() const
0030 {
0031     return m_messageEventModel;
0032 }
0033 
0034 QHash<int, QByteArray> TimelineModel::roleNames() const
0035 {
0036     return m_messageEventModel->roleNames();
0037 }
0038 
0039 TimelineEndModel::TimelineEndModel(QObject *parent)
0040     : QAbstractListModel(parent)
0041 {
0042 }
0043 
0044 void TimelineEndModel::setRoom(NeoChatRoom *room)
0045 {
0046     if (room == m_room) {
0047         return;
0048     }
0049 
0050     beginResetModel();
0051 
0052     if (m_room != nullptr) {
0053         m_room->disconnect(this);
0054     }
0055 
0056     m_room = room;
0057 
0058     if (m_room != nullptr) {
0059         connect(m_room, &Quotient::Room::eventsHistoryJobChanged, this, [this]() {
0060             if (m_room->allHistoryLoaded()) {
0061                 // HACK: We have to do it this way because DelegateChooser doesn't update dynamically.
0062                 beginRemoveRows({}, 0, 0);
0063                 endRemoveRows();
0064                 beginInsertRows({}, 0, 0);
0065                 endInsertRows();
0066             }
0067         });
0068     }
0069 
0070     endResetModel();
0071 }
0072 
0073 QVariant TimelineEndModel::data(const QModelIndex &idx, int role) const
0074 {
0075     Q_UNUSED(idx)
0076     if (m_room == nullptr) {
0077         return {};
0078     }
0079 
0080     if (role == DelegateTypeRole) {
0081         return m_room->allHistoryLoaded() ? DelegateType::TimelineEnd : DelegateType::Loading;
0082     }
0083     return {};
0084 }
0085 
0086 int TimelineEndModel::rowCount(const QModelIndex &parent) const
0087 {
0088     Q_UNUSED(parent)
0089     return 1;
0090 }
0091 
0092 QHash<int, QByteArray> TimelineEndModel::roleNames() const
0093 {
0094     return {{DelegateTypeRole, "delegateType"}};
0095 }