2018-02-28 09:10:42 +00:00
|
|
|
#include "roomlistmodel.h"
|
2018-03-15 09:10:27 +00:00
|
|
|
|
2018-08-19 06:32:18 +00:00
|
|
|
#include "user.h"
|
|
|
|
|
2018-07-19 08:04:09 +00:00
|
|
|
#include "events/roomevent.h"
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
#include <QtCore/QDebug>
|
2018-07-07 09:38:20 +00:00
|
|
|
#include <QtGui/QBrush>
|
|
|
|
#include <QtGui/QColor>
|
2018-08-17 04:55:57 +00:00
|
|
|
#include <QtQuick>
|
2018-02-28 09:10:42 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
RoomListModel::RoomListModel(QObject* parent) : QAbstractListModel(parent) {}
|
2018-07-08 05:25:46 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
RoomListModel::~RoomListModel() {}
|
2018-03-14 09:11:45 +00:00
|
|
|
|
2018-07-29 16:04:58 +00:00
|
|
|
void RoomListModel::setConnection(Connection* connection) {
|
2018-09-09 13:13:43 +00:00
|
|
|
if (connection == m_connection) return;
|
2018-09-10 07:01:01 +00:00
|
|
|
if (m_connection) m_connection->disconnect(this);
|
2018-09-09 13:13:43 +00:00
|
|
|
if (!connection) {
|
|
|
|
qDebug() << "Removing current connection...";
|
|
|
|
m_connection = nullptr;
|
|
|
|
beginResetModel();
|
|
|
|
m_rooms.clear();
|
|
|
|
endResetModel();
|
|
|
|
return;
|
|
|
|
}
|
2018-08-19 06:32:18 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
m_connection = connection;
|
2018-07-14 07:35:27 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
for (SpectralRoom* room : m_rooms) room->disconnect(this);
|
2018-09-10 00:06:32 +00:00
|
|
|
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(connection, &Connection::connected, this,
|
2018-07-18 14:16:03 +00:00
|
|
|
&RoomListModel::doResetModel);
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(connection, &Connection::invitedRoom, this,
|
2018-07-14 07:35:27 +00:00
|
|
|
&RoomListModel::updateRoom);
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(connection, &Connection::joinedRoom, this,
|
2018-07-14 07:35:27 +00:00
|
|
|
&RoomListModel::updateRoom);
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(connection, &Connection::leftRoom, this, &RoomListModel::updateRoom);
|
|
|
|
connect(connection, &Connection::aboutToDeleteRoom, this,
|
2018-07-09 05:36:28 +00:00
|
|
|
&RoomListModel::deleteRoom);
|
2018-09-09 10:35:37 +00:00
|
|
|
|
|
|
|
doResetModel();
|
2018-07-18 14:16:03 +00:00
|
|
|
}
|
2018-07-14 07:35:27 +00:00
|
|
|
|
2018-07-18 14:16:03 +00:00
|
|
|
void RoomListModel::doResetModel() {
|
|
|
|
beginResetModel();
|
|
|
|
m_rooms.clear();
|
|
|
|
for (auto r : m_connection->roomMap()) doAddRoom(r);
|
2018-07-09 02:45:26 +00:00
|
|
|
endResetModel();
|
2018-03-14 09:11:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
SpectralRoom* RoomListModel::roomAt(int row) { return m_rooms.at(row); }
|
2018-03-14 09:11:45 +00:00
|
|
|
|
2018-07-29 16:04:58 +00:00
|
|
|
void RoomListModel::doAddRoom(Room* r) {
|
2018-09-17 13:01:02 +00:00
|
|
|
if (auto* room = static_cast<SpectralRoom*>(r)) {
|
2018-07-14 07:35:27 +00:00
|
|
|
m_rooms.append(room);
|
|
|
|
connectRoomSignals(room);
|
2018-08-10 10:58:53 +00:00
|
|
|
emit roomAdded(room);
|
2018-07-14 07:35:27 +00:00
|
|
|
} else {
|
|
|
|
qCritical() << "Attempt to add nullptr to the room list";
|
|
|
|
Q_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void RoomListModel::connectRoomSignals(SpectralRoom* room) {
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(room, &Room::displaynameChanged, this, [=] { namesChanged(room); });
|
|
|
|
connect(room, &Room::unreadMessagesChanged, this,
|
2018-07-14 07:35:27 +00:00
|
|
|
[=] { unreadMessagesChanged(room); });
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(room, &Room::notificationCountChanged, this,
|
2018-07-14 07:35:27 +00:00
|
|
|
[=] { unreadMessagesChanged(room); });
|
2018-07-29 16:04:58 +00:00
|
|
|
connect(room, &Room::tagsChanged, this, [=] { refresh(room); });
|
|
|
|
connect(room, &Room::joinStateChanged, this, [=] { refresh(room); });
|
|
|
|
connect(room, &Room::avatarChanged, this,
|
2018-07-14 08:10:01 +00:00
|
|
|
[=] { refresh(room, {AvatarRole}); });
|
2018-08-22 15:21:39 +00:00
|
|
|
connect(room, &Room::addedMessages, this,
|
|
|
|
[=] { refresh(room, {LastEventRole}); });
|
2018-09-10 00:06:32 +00:00
|
|
|
connect(room, &Room::aboutToAddNewMessages, this,
|
2018-08-19 06:32:18 +00:00
|
|
|
[=](QMatrixClient::RoomEventsRange eventsRange) {
|
|
|
|
RoomEvent* event = (eventsRange.end() - 1)->get();
|
2018-08-26 07:42:37 +00:00
|
|
|
if (event->isStateEvent()) return;
|
2018-08-19 06:32:18 +00:00
|
|
|
User* sender = room->user(event->senderId());
|
|
|
|
if (sender == room->localUser()) return;
|
2018-10-19 14:02:12 +00:00
|
|
|
emit newMessage(room->id(), event->id(), room->displayName(),
|
|
|
|
sender->displayname(),
|
|
|
|
event->contentJson().value("body").toString(),
|
2018-10-20 02:39:39 +00:00
|
|
|
room->avatar(128));
|
2018-08-19 06:32:18 +00:00
|
|
|
});
|
2018-07-14 07:35:27 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 16:04:58 +00:00
|
|
|
void RoomListModel::updateRoom(Room* room, Room* prev) {
|
2018-07-14 07:35:27 +00:00
|
|
|
// 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";
|
2018-09-17 13:01:02 +00:00
|
|
|
refresh(static_cast<SpectralRoom*>(room));
|
2018-07-14 07:35:27 +00:00
|
|
|
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.
|
2018-09-17 13:01:02 +00:00
|
|
|
auto* newRoom = static_cast<SpectralRoom*>(room);
|
2018-08-18 08:02:47 +00:00
|
|
|
const auto it = std::find_if(
|
|
|
|
m_rooms.begin(), m_rooms.end(),
|
2018-09-17 13:01:02 +00:00
|
|
|
[=](const SpectralRoom* r) { return r == prev || r == newRoom; });
|
2018-07-14 07:35:27 +00:00
|
|
|
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-29 16:04:58 +00:00
|
|
|
void RoomListModel::deleteRoom(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();
|
|
|
|
}
|
2018-09-17 13:01:02 +00:00
|
|
|
SpectralRoom* room = m_rooms.at(index.row());
|
2018-08-17 04:55:57 +00:00
|
|
|
if (role == NameRole) return room->displayName();
|
2018-07-13 04:06:27 +00:00
|
|
|
if (role == AvatarRole) {
|
2018-09-09 10:35:37 +00:00
|
|
|
if (!room->avatarUrl().isEmpty()) return room->avatar(64, 64);
|
2018-09-06 08:22:00 +00:00
|
|
|
return QImage();
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-08-17 04:55:57 +00:00
|
|
|
if (role == TopicRole) return room->topic();
|
2018-07-13 04:06:27 +00:00
|
|
|
if (role == CategoryRole) {
|
2018-08-17 04:55:57 +00:00
|
|
|
if (room->joinState() == JoinState::Invite) return RoomType::Invited;
|
|
|
|
if (room->isFavourite()) return RoomType::Favorite;
|
|
|
|
if (room->isDirectChat()) return RoomType::Direct;
|
|
|
|
if (room->isLowPriority()) return RoomType::Deprioritized;
|
|
|
|
return RoomType::Normal;
|
2018-08-06 15:51:22 +00:00
|
|
|
}
|
2018-08-17 04:55:57 +00:00
|
|
|
if (role == UnreadCountRole) return room->unreadCount();
|
2018-09-06 04:34:15 +00:00
|
|
|
if (role == HighlightCountRole) return room->highlightCount();
|
2018-08-22 15:21:39 +00:00
|
|
|
if (role == LastEventRole) return room->lastEvent();
|
2018-09-18 12:57:38 +00:00
|
|
|
if (role == LastActiveTimeRole) return room->lastActiveTime();
|
2018-08-24 05:25:41 +00:00
|
|
|
if (role == CurrentRoomRole) return QVariant::fromValue(room);
|
2018-07-09 02:45:26 +00:00
|
|
|
return QVariant();
|
2018-03-07 08:48:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void RoomListModel::namesChanged(SpectralRoom* room) {
|
2018-07-09 02:45:26 +00:00
|
|
|
int row = m_rooms.indexOf(room);
|
|
|
|
emit dataChanged(index(row), index(row));
|
2018-02-28 09:10:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void RoomListModel::refresh(SpectralRoom* room, const QVector<int>& roles) {
|
2018-07-14 07:35:27 +00:00
|
|
|
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-09-17 13:01:02 +00:00
|
|
|
void RoomListModel::unreadMessagesChanged(SpectralRoom* room) {
|
2018-07-09 02:45:26 +00:00
|
|
|
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
|
|
|
|
2018-07-07 09:38:20 +00:00
|
|
|
QHash<int, QByteArray> RoomListModel::roleNames() const {
|
2018-07-09 02:45:26 +00:00
|
|
|
QHash<int, QByteArray> roles;
|
2018-07-13 04:06:27 +00:00
|
|
|
roles[NameRole] = "name";
|
|
|
|
roles[AvatarRole] = "avatar";
|
|
|
|
roles[TopicRole] = "topic";
|
|
|
|
roles[CategoryRole] = "category";
|
2018-08-06 15:51:22 +00:00
|
|
|
roles[UnreadCountRole] = "unreadCount";
|
2018-09-06 04:34:15 +00:00
|
|
|
roles[HighlightCountRole] = "highlightCount";
|
2018-08-22 15:21:39 +00:00
|
|
|
roles[LastEventRole] = "lastEvent";
|
2018-09-18 12:57:38 +00:00
|
|
|
roles[LastActiveTimeRole] = "lastActiveTime";
|
2018-08-24 05:25:41 +00:00
|
|
|
roles[CurrentRoomRole] = "currentRoom";
|
2018-07-09 02:45:26 +00:00
|
|
|
return roles;
|
2018-02-28 09:10:42 +00:00
|
|
|
}
|