File indexing completed on 2024-12-01 07:40:36

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "statemodel.h"
0005 
0006 StateModel::StateModel(QObject *parent)
0007     : QAbstractListModel(parent)
0008 {
0009 }
0010 
0011 QHash<int, QByteArray> StateModel::roleNames() const
0012 {
0013     return {{TypeRole, "type"}, {StateKeyRole, "stateKey"}};
0014 }
0015 QVariant StateModel::data(const QModelIndex &index, int role) const
0016 {
0017     auto row = index.row();
0018     switch (role) {
0019     case TypeRole:
0020         return m_stateEvents[row].first;
0021     case StateKeyRole:
0022         return m_stateEvents[row].second;
0023     }
0024     return {};
0025 }
0026 
0027 int StateModel::rowCount(const QModelIndex &parent) const
0028 {
0029     Q_UNUSED(parent);
0030     return m_room->currentState().events().size();
0031 }
0032 
0033 NeoChatRoom *StateModel::room() const
0034 {
0035     return m_room;
0036 }
0037 
0038 void StateModel::setRoom(NeoChatRoom *room)
0039 {
0040     m_room = room;
0041     Q_EMIT roomChanged();
0042     beginResetModel();
0043     m_stateEvents.clear();
0044     m_stateEvents = m_room->currentState().events().keys();
0045     endResetModel();
0046     connect(room, &NeoChatRoom::changed, this, [this] {
0047         beginResetModel();
0048         m_stateEvents.clear();
0049         m_stateEvents = m_room->currentState().events().keys();
0050         endResetModel();
0051     });
0052 }
0053 
0054 QByteArray StateModel::stateEventJson(const QModelIndex &index)
0055 {
0056     auto row = index.row();
0057     return QJsonDocument(m_room->currentState().events()[m_stateEvents[row]]->fullJson()).toJson();
0058 }
0059 
0060 #include "moc_statemodel.cpp"