| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QAbstractListModel> |
| 8 |
#include <QHash> |
| 9 |
#include <QQmlEngine> |
| 10 |
#include <QSortFilterProxyModel> |
| 11 |
#include <QString> |
| 12 |
#include <QStringList> |
| 13 |
|
| 14 |
#include <unordered_map> |
| 15 |
|
| 16 |
#include <mtx/responses/sync.hpp> |
| 17 |
|
| 18 |
#include "CacheStructs.h" |
| 19 |
|
| 20 |
class CommunitiesModel; |
| 21 |
|
| 22 |
class FilteredCommunitiesModel final : public QSortFilterProxyModel |
| 23 |
{ |
| 24 |
Q_OBJECT |
| 25 |
QML_ELEMENT |
| 26 |
QML_UNCREATABLE("Use Communities.filtered() to create a FilteredCommunitiesModel") |
| 27 |
|
| 28 |
public: |
| 29 |
explicit FilteredCommunitiesModel(CommunitiesModel *model, QObject *parent = nullptr); |
| 30 |
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; |
| 31 |
bool filterAcceptsRow(int sourceRow, const QModelIndex &) const override; |
| 32 |
}; |
| 33 |
|
| 34 |
class SpaceItem |
| 35 |
{ |
| 36 |
Q_GADGET |
| 37 |
|
| 38 |
Q_PROPERTY(QString roomid MEMBER roomid CONSTANT) |
| 39 |
Q_PROPERTY(QString name MEMBER name CONSTANT) |
| 40 |
Q_PROPERTY(int treeIndex MEMBER treeIndex CONSTANT) |
| 41 |
|
| 42 |
Q_PROPERTY(bool childValid MEMBER childValid CONSTANT) |
| 43 |
Q_PROPERTY(bool parentValid MEMBER parentValid CONSTANT) |
| 44 |
Q_PROPERTY(bool canonical MEMBER canonical CONSTANT) |
| 45 |
|
| 46 |
Q_PROPERTY(bool canEditParent MEMBER canEditParent CONSTANT) |
| 47 |
Q_PROPERTY(bool canEditChild MEMBER canEditChild CONSTANT) |
| 48 |
|
| 49 |
public: |
| 50 |
SpaceItem() {} |
| 51 |
SpaceItem(QString roomid_, |
| 52 |
QString name_, |
| 53 |
int treeIndex_, |
| 54 |
bool childValid_, |
| 55 |
bool parentValid_, |
| 56 |
bool canonical_, |
| 57 |
bool canEditChild_, |
| 58 |
bool canEditParent_) |
| 59 |
: roomid(std::move(roomid_)) |
| 60 |
, name(std::move(name_)) |
| 61 |
, treeIndex(treeIndex_) |
| 62 |
, childValid(childValid_) |
| 63 |
, parentValid(parentValid_) |
| 64 |
, canonical(canonical_) |
| 65 |
, canEditParent(canEditParent_) |
| 66 |
, canEditChild(canEditChild_) |
| 67 |
{ |
| 68 |
} |
| 69 |
|
| 70 |
QString roomid, name; |
| 71 |
int treeIndex = 0; |
| 72 |
bool childValid = false, parentValid = false, canonical = false; |
| 73 |
bool canEditParent = false, canEditChild = false; |
| 74 |
}; |
| 75 |
|
| 76 |
class CommunitiesModel final : public QAbstractListModel |
| 77 |
{ |
| 78 |
Q_OBJECT |
| 79 |
QML_NAMED_ELEMENT(Communities) |
| 80 |
QML_SINGLETON |
| 81 |
|
| 82 |
Q_PROPERTY(QString currentTagId READ currentTagId WRITE setCurrentTagId NOTIFY |
| 83 |
currentTagIdChanged RESET resetCurrentTagId) |
| 84 |
Q_PROPERTY(QStringList tags READ tags NOTIFY tagsChanged) |
| 85 |
Q_PROPERTY(QStringList tagsWithDefault READ tagsWithDefault NOTIFY tagsChanged) |
| 86 |
Q_PROPERTY(bool containsSubspaces READ containsSubspaces NOTIFY containsSubspacesChanged) |
| 87 |
|
| 88 |
public: |
| 89 |
enum Roles |
| 90 |
{ |
| 91 |
AvatarUrl = Qt::UserRole, |
| 92 |
DisplayName, |
| 93 |
Tooltip, |
| 94 |
Collapsed, |
| 95 |
Collapsible, |
| 96 |
Hidden, |
| 97 |
Parent, |
| 98 |
Depth, |
| 99 |
Id, |
| 100 |
UnreadMessages, |
| 101 |
HasLoudNotification, |
| 102 |
Muted, |
| 103 |
IsDirect, |
| 104 |
}; |
| 105 |
|
| 106 |
struct FlatTree |
| 107 |
{ |
| 108 |
struct Elem |
| 109 |
{ |
| 110 |
QString id; |
| 111 |
int depth = 0; |
| 112 |
|
| 113 |
mtx::responses::UnreadNotifications notificationCounts = {0, 0}; |
| 114 |
|
| 115 |
bool collapsed = false; |
| 116 |
}; |
| 117 |
|
| 118 |
std::vector<Elem> tree; |
| 119 |
|
| 120 |
int size() const { return static_cast<int>(tree.size()); } |
| 121 |
int indexOf(const QString &s) const |
| 122 |
{ |
| 123 |
for (int i = 0; i < size(); i++) |
| 124 |
if (tree[i].id == s) |
| 125 |
return i; |
| 126 |
return -1; |
| 127 |
} |
| 128 |
int lastChild(int index) const |
| 129 |
{ |
| 130 |
if (index >= size() || index < 0) |
| 131 |
return index; |
| 132 |
const auto depth = tree[index].depth; |
| 133 |
int i = index + 1; |
| 134 |
for (; i < size(); i++) |
| 135 |
if (tree[i].depth <= depth) |
| 136 |
break; |
| 137 |
return i - 1; |
| 138 |
} |
| 139 |
int parent(int index) const |
| 140 |
{ |
| 141 |
if (index >= size() || index < 0) |
| 142 |
return -1; |
| 143 |
const auto depth = tree[index].depth; |
| 144 |
if (depth == 0) |
| 145 |
return -1; |
| 146 |
int i = index - 1; |
| 147 |
for (; i >= 0; i--) |
| 148 |
if (tree[i].depth < depth) |
| 149 |
break; |
| 150 |
return i; |
| 151 |
} |
| 152 |
|
| 153 |
void storeCollapsed(); |
| 154 |
void restoreCollapsed(); |
| 155 |
}; |
| 156 |
|
| 157 |
CommunitiesModel(QObject *parent); |
| 158 |
|
| 159 |
static CommunitiesModel *create(QQmlEngine *qmlEngine, QJSEngine *) |
| 160 |
{ |
| 161 |
|
| 162 |
Q_ASSERT(instance_); |
| 163 |
|
| 164 |
|
| 165 |
Q_ASSERT(qmlEngine->thread() == instance_->thread()); |
| 166 |
|
| 167 |
|
| 168 |
static QJSEngine *s_engine = nullptr; |
| 169 |
if (s_engine) |
| 170 |
Q_ASSERT(qmlEngine == s_engine); |
| 171 |
else |
| 172 |
s_engine = qmlEngine; |
| 173 |
|
| 174 |
QJSEngine::setObjectOwnership(instance_, QJSEngine::CppOwnership); |
| 175 |
return instance_; |
| 176 |
} |
| 177 |
|
| 178 |
QHash<int, QByteArray> roleNames() const override; |
| 179 |
int rowCount(const QModelIndex &parent = QModelIndex()) const override |
| 180 |
{ |
| 181 |
(void)parent; |
| 182 |
return 2 + tags_.size() + spaceOrder_.size(); |
| 183 |
} |
| 184 |
QVariant data(const QModelIndex &index, int role) const override; |
| 185 |
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; |
| 186 |
|
| 187 |
bool containsSubspaces() const |
| 188 |
{ |
| 189 |
for (const auto &e : spaceOrder_.tree) |
| 190 |
if (e.depth > 0) |
| 191 |
return true; |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
Q_INVOKABLE QVariantList spaceChildrenListFromIndex(const QString &room, int idx = -1) const; |
| 196 |
Q_INVOKABLE void updateSpaceStatus(QString space, |
| 197 |
QString room, |
| 198 |
bool setParent, |
| 199 |
bool setChild, |
| 200 |
bool canonical) const; |
| 201 |
|
| 202 |
public slots: |
| 203 |
void initializeSidebar(); |
| 204 |
void sync(const mtx::responses::Sync &sync_); |
| 205 |
void clear(); |
| 206 |
QString currentTagId() const { return currentTagId_; } |
| 207 |
void setCurrentTagId(const QString &tagId); |
| 208 |
bool trySwitchToSpace(const QString &spaceId); |
| 209 |
void resetCurrentTagId() |
| 210 |
{ |
| 211 |
currentTagId_.clear(); |
| 212 |
emit currentTagIdChanged(currentTagId_); |
| 213 |
} |
| 214 |
QStringList tags() const { return tags_; } |
| 215 |
QStringList tagsWithDefault() const |
| 216 |
{ |
| 217 |
QStringList tagsWD = tags_; |
| 218 |
tagsWD.prepend(QStringLiteral("m.lowpriority")); |
| 219 |
tagsWD.prepend(QStringLiteral("m.favourite")); |
| 220 |
tagsWD.removeOne(QStringLiteral("m.server_notice")); |
| 221 |
tagsWD.removeDuplicates(); |
| 222 |
return tagsWD; |
| 223 |
} |
| 224 |
void toggleTagId(QString tagId); |
| 225 |
void toggleTagMute(QString tagId); |
| 226 |
|
| 227 |
FilteredCommunitiesModel *filtered() { return new FilteredCommunitiesModel(this, this); } |
| 228 |
|
| 229 |
signals: |
| 230 |
void currentTagIdChanged(QString tagId); |
| 231 |
void hiddenTagsChanged(); |
| 232 |
void tagsChanged(); |
| 233 |
void containsSubspacesChanged(); |
| 234 |
|
| 235 |
private: |
| 236 |
QStringList tags_; |
| 237 |
QString currentTagId_; |
| 238 |
QStringList hiddenTagIds_; |
| 239 |
QStringList mutedTagIds_; |
| 240 |
FlatTree spaceOrder_; |
| 241 |
std::map<QString, RoomInfo> spaces_; |
| 242 |
std::vector<std::string> directMessages_; |
| 243 |
|
| 244 |
std::unordered_map<QString, mtx::responses::UnreadNotifications> roomNotificationCache; |
| 245 |
std::unordered_map<QString, mtx::responses::UnreadNotifications> tagNotificationCache; |
| 246 |
mtx::responses::UnreadNotifications globalUnreads{}; |
| 247 |
mtx::responses::UnreadNotifications dmUnreads{}; |
| 248 |
|
| 249 |
friend class FilteredCommunitiesModel; |
| 250 |
|
| 251 |
inline static CommunitiesModel *instance_ = nullptr; |
| 252 |
}; |