Spectral/src/roomlistmodel.cpp

173 lines
5.6 KiB
C++
Raw Normal View History

2018-02-28 09:10:42 +00:00
#include "roomlistmodel.h"
2018-03-15 09:10:27 +00:00
2018-07-09 02:45:26 +00:00
#include <QtCore/QDebug>
#include <QtGui/QBrush>
#include <QtGui/QColor>
2018-02-28 09:10:42 +00:00
2018-07-09 02:45:26 +00:00
RoomListModel::RoomListModel(QObject* parent) : QAbstractListModel(parent) {}
2018-07-09 02:45:26 +00:00
RoomListModel::~RoomListModel() {}
2018-03-14 09:11:45 +00:00
2018-07-09 02:45:26 +00:00
void RoomListModel::setConnection(QMatrixClient::Connection* connection) {
2018-07-09 05:36:28 +00:00
Q_ASSERT(connection);
using QMatrixClient::Room;
2018-07-09 02:45:26 +00:00
beginResetModel();
m_connection = connection;
2018-07-14 07:35:27 +00:00
m_rooms.clear();
connect(connection, &QMatrixClient::Connection::invitedRoom, this,
&RoomListModel::updateRoom);
connect(connection, &QMatrixClient::Connection::joinedRoom, this,
&RoomListModel::updateRoom);
connect(connection, &QMatrixClient::Connection::leftRoom, this,
&RoomListModel::updateRoom);
connect(connection, &QMatrixClient::Connection::aboutToDeleteRoom, this,
2018-07-09 05:36:28 +00:00
&RoomListModel::deleteRoom);
2018-07-14 07:35:27 +00:00
for (auto r : connection->roomMap()) doAddRoom(r);
2018-07-09 02:45:26 +00:00
endResetModel();
2018-03-14 09:11:45 +00:00
}
2018-07-09 02:45:26 +00:00
QMatrixClient::Room* RoomListModel::roomAt(int row) { return m_rooms.at(row); }
2018-03-14 09:11:45 +00:00
2018-07-14 07:35:27 +00:00
void RoomListModel::doAddRoom(QMatrixClient::Room* r) {
if (auto* room = r) {
m_rooms.append(room);
connectRoomSignals(room);
} else {
qCritical() << "Attempt to add nullptr to the room list";
Q_ASSERT(false);
}
}
void RoomListModel::connectRoomSignals(QMatrixClient::Room* room) {
connect(room, &QMatrixClient::Room::displaynameChanged, this,
[=] { namesChanged(room); });
connect(room, &QMatrixClient::Room::unreadMessagesChanged, this,
[=] { unreadMessagesChanged(room); });
connect(room, &QMatrixClient::Room::notificationCountChanged, this,
[=] { unreadMessagesChanged(room); });
connect(room, &QMatrixClient::Room::joinStateChanged, this,
[=] { refresh(room); });
connect(room, &QMatrixClient::Room::avatarChanged, this,
[=] { refresh(room, {Qt::DecorationRole}); });
}
void RoomListModel::updateRoom(QMatrixClient::Room* room,
QMatrixClient::Room* prev) {
// There are two cases when this method is called:
// 1. (prev == nullptr) adding a new room to the room list
// 2. (prev != nullptr) accepting/rejecting an invitation or inviting to
// the previously left room (in both cases prev has the previous state).
if (prev == room) {
qCritical() << "RoomListModel::updateRoom: room tried to replace itself";
refresh(static_cast<QMatrixClient::Room*>(room));
return;
}
if (prev && room->id() != prev->id()) {
qCritical() << "RoomListModel::updateRoom: attempt to update room"
<< room->id() << "to" << prev->id();
// That doesn't look right but technically we still can do it.
}
// Ok, we're through with pre-checks, now for the real thing.
auto* newRoom = room;
const auto it = std::find_if(
m_rooms.begin(), m_rooms.end(),
[=](const QMatrixClient::Room* r) { return r == prev || r == newRoom; });
if (it != m_rooms.end()) {
const int row = it - m_rooms.begin();
// There's no guarantee that prev != newRoom
if (*it == prev && *it != newRoom) {
prev->disconnect(this);
m_rooms.replace(row, newRoom);
connectRoomSignals(newRoom);
}
emit dataChanged(index(row), index(row));
} else {
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
doAddRoom(newRoom);
endInsertRows();
}
2018-02-28 09:10:42 +00:00
}
2018-07-09 05:36:28 +00:00
void RoomListModel::deleteRoom(QMatrixClient::Room* room) {
2018-07-14 07:35:27 +00:00
qDebug() << "Deleting room" << room->id();
2018-07-09 05:36:28 +00:00
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
if (it == m_rooms.end()) return; // Already deleted, nothing to do
2018-07-14 07:35:27 +00:00
qDebug() << "Erasing room" << room->id();
2018-07-09 05:36:28 +00:00
const int row = it - m_rooms.begin();
beginRemoveRows(QModelIndex(), row, row);
m_rooms.erase(it);
endRemoveRows();
}
2018-07-09 02:45:26 +00:00
int RoomListModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) return 0;
return m_rooms.count();
2018-02-28 09:10:42 +00:00
}
2018-07-09 02:45:26 +00:00
QVariant RoomListModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) return QVariant();
2018-02-28 13:11:42 +00:00
2018-07-09 02:45:26 +00:00
if (index.row() >= m_rooms.count()) {
qDebug() << "UserListModel: something wrong here...";
return QVariant();
}
QMatrixClient::Room* room = m_rooms.at(index.row());
if (role == NameRole) {
2018-07-09 02:45:26 +00:00
return room->displayName();
}
if (role == AvatarRole) {
2018-07-09 02:45:26 +00:00
if (room->avatarUrl().toString() != "") {
return room->avatarUrl();
}
return QVariant();
2018-07-09 02:45:26 +00:00
}
if (role == TopicRole) {
2018-07-09 02:45:26 +00:00
return room->topic();
}
if (role == CategoryRole) {
if (room->isFavourite()) return "Favorites";
if (room->isLowPriority()) return "Low Priorities";
return "Rooms";
}
if (role == HighlightRole) {
if (room->highlightCount() > 0) return QBrush(QColor("orange"));
return QVariant();
}
2018-07-09 02:45:26 +00:00
return QVariant();
}
2018-07-09 02:45:26 +00:00
void RoomListModel::namesChanged(QMatrixClient::Room* room) {
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
2018-02-28 09:10:42 +00:00
}
2018-07-14 07:35:27 +00:00
void RoomListModel::refresh(QMatrixClient::Room* room,
const QVector<int>& roles) {
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
if (it == m_rooms.end()) {
qCritical() << "Room" << room->id() << "not found in the room list";
return;
}
const auto idx = index(it - m_rooms.begin());
emit dataChanged(idx, idx, roles);
}
2018-07-09 02:45:26 +00:00
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) {
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
2018-03-14 09:11:45 +00:00
}
2018-02-28 09:10:42 +00:00
QHash<int, QByteArray> RoomListModel::roleNames() const {
2018-07-09 02:45:26 +00:00
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[AvatarRole] = "avatar";
roles[TopicRole] = "topic";
roles[CategoryRole] = "category";
roles[HighlightRole] = "highlight";
2018-07-09 02:45:26 +00:00
return roles;
2018-02-28 09:10:42 +00:00
}