matrixion/src/timeline/RoomlistModel.h

215 lines
7.1 KiB
C
Raw Normal View History

2021-05-19 20:34:10 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
2021-05-24 15:04:07 +03:00
#include <CacheStructs.h>
2021-05-19 20:34:10 +03:00
#include <QAbstractListModel>
#include <QHash>
#include <QSharedPointer>
2021-05-22 01:57:14 +03:00
#include <QSortFilterProxyModel>
2021-05-19 20:34:10 +03:00
#include <QString>
2021-05-28 18:25:46 +03:00
#include <set>
2021-05-19 20:34:10 +03:00
#include <mtx/responses/sync.hpp>
#include "TimelineModel.h"
2021-05-19 20:34:10 +03:00
class TimelineViewManager;
class RoomPreview
{
Q_GADGET
Q_PROPERTY(QString roomid READ roomid CONSTANT)
Q_PROPERTY(QString roomName READ roomName CONSTANT)
Q_PROPERTY(QString roomTopic READ roomTopic CONSTANT)
Q_PROPERTY(QString roomAvatarUrl READ roomAvatarUrl CONSTANT)
Q_PROPERTY(bool isInvite READ isInvite CONSTANT)
public:
RoomPreview() {}
QString roomid() const { return roomid_; }
QString roomName() const { return roomName_; }
QString roomTopic() const { return roomTopic_; }
QString roomAvatarUrl() const { return roomAvatarUrl_; }
bool isInvite() const { return isInvite_; }
QString roomid_, roomName_, roomAvatarUrl_, roomTopic_;
bool isInvite_ = false;
};
2021-05-19 20:34:10 +03:00
class RoomlistModel : public QAbstractListModel
{
Q_OBJECT
2021-06-08 23:18:51 +03:00
Q_PROPERTY(TimelineModel *currentRoom READ currentRoom NOTIFY currentRoomChanged RESET
resetCurrentRoom)
Q_PROPERTY(RoomPreview currentRoomPreview READ currentRoomPreview NOTIFY currentRoomChanged
RESET resetCurrentRoom)
2021-05-19 20:34:10 +03:00
public:
enum Roles
{
AvatarUrl = Qt::UserRole,
RoomName,
2021-05-21 22:19:03 +03:00
RoomId,
2021-05-19 20:34:10 +03:00
LastMessage,
2021-05-22 01:57:14 +03:00
Time,
2021-05-21 22:19:03 +03:00
Timestamp,
2021-05-19 20:34:10 +03:00
HasUnreadMessages,
2021-05-21 22:19:03 +03:00
HasLoudNotification,
2021-05-19 20:34:10 +03:00
NotificationCount,
2021-05-22 01:57:14 +03:00
IsInvite,
IsSpace,
2021-07-05 00:06:50 +03:00
IsPreview,
IsPreviewFetched,
2021-05-28 18:25:46 +03:00
Tags,
2021-06-18 13:10:13 +03:00
ParentSpaces,
2021-08-31 03:08:47 +03:00
IsDirect,
2021-09-05 03:53:33 +03:00
DirectChatOtherUserId,
2021-05-19 20:34:10 +03:00
};
RoomlistModel(TimelineViewManager *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
(void)parent;
return (int)roomids.size();
}
QVariant data(const QModelIndex &index, int role) const override;
QSharedPointer<TimelineModel> getRoomById(QString id) const
{
if (models.contains(id))
return models.value(id);
else
return {};
}
public slots:
2021-05-24 15:04:07 +03:00
void initializeRooms();
2021-05-19 20:34:10 +03:00
void sync(const mtx::responses::Rooms &rooms);
void clear();
2021-05-21 22:19:03 +03:00
int roomidToIndex(QString roomid)
{
for (int i = 0; i < (int)roomids.size(); i++) {
if (roomids[i] == roomid)
return i;
}
return -1;
}
void joinPreview(QString roomid, QString parentSpace);
2021-05-24 15:04:07 +03:00
void acceptInvite(QString roomid);
void declineInvite(QString roomid);
2021-05-28 18:25:46 +03:00
void leave(QString roomid);
TimelineModel *currentRoom() const { return currentRoom_.get(); }
RoomPreview currentRoomPreview() const
{
return currentRoomPreview_.value_or(RoomPreview{});
}
void setCurrentRoom(QString roomid);
2021-06-08 23:18:51 +03:00
void resetCurrentRoom()
{
currentRoom_ = nullptr;
currentRoomPreview_.reset();
2021-06-08 23:18:51 +03:00
emit currentRoomChanged();
}
2021-05-21 22:19:03 +03:00
private slots:
void updateReadStatus(const std::map<QString, bool> roomReadStatus_);
signals:
void totalUnreadMessageCountUpdated(int unreadMessages);
void currentRoomChanged();
2021-07-05 00:06:50 +03:00
void fetchedPreview(QString roomid, RoomInfo info);
2021-05-19 20:34:10 +03:00
private:
void addRoom(const QString &room_id, bool suppressInsertNotification = false);
2021-07-05 00:06:50 +03:00
void fetchPreview(QString roomid) const;
2021-05-19 20:34:10 +03:00
TimelineViewManager *manager = nullptr;
std::vector<QString> roomids;
2021-05-24 15:04:07 +03:00
QHash<QString, RoomInfo> invites;
2021-05-19 20:34:10 +03:00
QHash<QString, QSharedPointer<TimelineModel>> models;
2021-05-21 22:19:03 +03:00
std::map<QString, bool> roomReadStatus;
2021-07-05 00:06:50 +03:00
QHash<QString, std::optional<RoomInfo>> previewedRooms;
2021-05-22 01:57:14 +03:00
QSharedPointer<TimelineModel> currentRoom_;
std::optional<RoomPreview> currentRoomPreview_;
2021-05-22 01:57:14 +03:00
friend class FilteredRoomlistModel;
};
class FilteredRoomlistModel : public QSortFilterProxyModel
{
Q_OBJECT
2021-06-08 23:18:51 +03:00
Q_PROPERTY(TimelineModel *currentRoom READ currentRoom NOTIFY currentRoomChanged RESET
resetCurrentRoom)
Q_PROPERTY(RoomPreview currentRoomPreview READ currentRoomPreview NOTIFY currentRoomChanged
RESET resetCurrentRoom)
2021-05-22 01:57:14 +03:00
public:
FilteredRoomlistModel(RoomlistModel *model, QObject *parent = nullptr);
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
2021-06-11 15:51:29 +03:00
bool filterAcceptsRow(int sourceRow, const QModelIndex &) const override;
2021-05-22 01:57:14 +03:00
public slots:
int roomidToIndex(QString roomid)
{
return mapFromSource(roomlistmodel->index(roomlistmodel->roomidToIndex(roomid)))
.row();
}
void joinPreview(QString roomid)
{
roomlistmodel->joinPreview(roomid, filterType == FilterBy::Space ? filterStr : "");
}
2021-05-24 15:04:07 +03:00
void acceptInvite(QString roomid) { roomlistmodel->acceptInvite(roomid); }
void declineInvite(QString roomid) { roomlistmodel->declineInvite(roomid); }
2021-05-28 18:25:46 +03:00
void leave(QString roomid) { roomlistmodel->leave(roomid); }
void toggleTag(QString roomid, QString tag, bool on);
2021-05-22 01:57:14 +03:00
TimelineModel *currentRoom() const { return roomlistmodel->currentRoom(); }
RoomPreview currentRoomPreview() const { return roomlistmodel->currentRoomPreview(); }
void setCurrentRoom(QString roomid) { roomlistmodel->setCurrentRoom(std::move(roomid)); }
2021-06-08 23:18:51 +03:00
void resetCurrentRoom() { roomlistmodel->resetCurrentRoom(); }
void nextRoomWithActivity();
2021-05-29 00:25:57 +03:00
void nextRoom();
void previousRoom();
2021-06-11 15:51:29 +03:00
void updateFilterTag(QString tagId)
{
if (tagId.startsWith("tag:")) {
filterType = FilterBy::Tag;
filterStr = tagId.mid(4);
2021-06-16 23:59:41 +03:00
} else if (tagId.startsWith("space:")) {
filterType = FilterBy::Space;
filterStr = tagId.mid(6);
2021-06-11 15:51:29 +03:00
} else {
filterType = FilterBy::Nothing;
filterStr.clear();
}
invalidateFilter();
}
2021-06-11 18:54:05 +03:00
void updateHiddenTagsAndSpaces();
signals:
void currentRoomChanged();
2021-05-22 01:57:14 +03:00
private:
short int calculateImportance(const QModelIndex &idx) const;
RoomlistModel *roomlistmodel;
bool sortByImportance = true;
2021-06-11 15:51:29 +03:00
enum class FilterBy
{
Tag,
Space,
Nothing,
};
QString filterStr = "";
FilterBy filterType = FilterBy::Nothing;
2021-06-11 18:54:05 +03:00
QStringList hiddenTags, hiddenSpaces;
2021-05-19 20:34:10 +03:00
};