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>
|
2018-07-07 09:38:20 +00:00
|
|
|
#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-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-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::Connection;
|
|
|
|
using QMatrixClient::Room;
|
2018-07-09 02:45:26 +00:00
|
|
|
beginResetModel();
|
|
|
|
m_connection = connection;
|
2018-07-09 05:36:28 +00:00
|
|
|
connect(connection, &Connection::loggedOut, this,
|
|
|
|
[=] { setConnection(connection); });
|
2018-07-13 04:06:27 +00:00
|
|
|
// connect(connection, &Connection::invitedRoom, this,
|
|
|
|
// &RoomListModel::updateRoom);
|
|
|
|
// connect(connection, &Connection::joinedRoom, this,
|
|
|
|
// &RoomListModel::updateRoom);
|
|
|
|
// connect(connection, &Connection::leftRoom, this,
|
|
|
|
// &RoomListModel::updateRoom);
|
2018-07-09 05:36:28 +00:00
|
|
|
connect(connection, &Connection::aboutToDeleteRoom, this,
|
|
|
|
&RoomListModel::deleteRoom);
|
|
|
|
|
|
|
|
for (auto r : connection->roomMap()) addRoom(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-09 02:45:26 +00:00
|
|
|
void RoomListModel::addRoom(QMatrixClient::Room* room) {
|
|
|
|
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
|
|
|
|
connect(room, &QMatrixClient::Room::namesChanged, this,
|
|
|
|
&RoomListModel::namesChanged);
|
|
|
|
m_rooms.append(room);
|
|
|
|
endInsertRows();
|
2018-02-28 09:10:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 05:36:28 +00:00
|
|
|
void RoomListModel::deleteRoom(QMatrixClient::Room* room) {
|
|
|
|
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
|
|
|
|
if (it == m_rooms.end()) return; // Already deleted, nothing to do
|
|
|
|
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());
|
2018-07-13 04:06:27 +00:00
|
|
|
if (role == NameRole) {
|
2018-07-09 02:45:26 +00:00
|
|
|
return room->displayName();
|
|
|
|
}
|
2018-07-13 04:06:27 +00:00
|
|
|
if (role == AvatarRole) {
|
2018-07-09 02:45:26 +00:00
|
|
|
if (room->avatarUrl().toString() != "") {
|
|
|
|
return room->avatarUrl();
|
2018-07-07 09:38:20 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-07-13 04:06:27 +00:00
|
|
|
if (role == TopicRole) {
|
2018-07-09 02:45:26 +00:00
|
|
|
return room->topic();
|
|
|
|
}
|
2018-07-13 04:06:27 +00:00
|
|
|
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-03-07 08:48:27 +00:00
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
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";
|
|
|
|
roles[HighlightRole] = "highlight";
|
2018-07-09 02:45:26 +00:00
|
|
|
return roles;
|
2018-02-28 09:10:42 +00:00
|
|
|
}
|