mirror of https://github.com/Nheko-Reborn/nheko
parent
d364c29c43
commit
2cd1a931c2
@ -0,0 +1,158 @@ |
|||||||
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "CommunitiesModel.h" |
||||||
|
|
||||||
|
#include <set> |
||||||
|
|
||||||
|
#include "Cache.h" |
||||||
|
#include "UserSettingsPage.h" |
||||||
|
|
||||||
|
CommunitiesModel::CommunitiesModel(QObject *parent) |
||||||
|
: QAbstractListModel(parent) |
||||||
|
{} |
||||||
|
|
||||||
|
QHash<int, QByteArray> |
||||||
|
CommunitiesModel::roleNames() const |
||||||
|
{ |
||||||
|
return { |
||||||
|
{AvatarUrl, "avatarUrl"}, |
||||||
|
{DisplayName, "displayName"}, |
||||||
|
{Tooltip, "tooltip"}, |
||||||
|
{ChildrenHidden, "childrenHidden"}, |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
QVariant |
||||||
|
CommunitiesModel::data(const QModelIndex &index, int role) const |
||||||
|
{ |
||||||
|
if (index.row() == 0) { |
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::AvatarUrl: |
||||||
|
return QString(":/icons/icons/ui/world.png"); |
||||||
|
case CommunitiesModel::Roles::DisplayName: |
||||||
|
return tr("All rooms"); |
||||||
|
case CommunitiesModel::Roles::Tooltip: |
||||||
|
return tr("Shows all rooms without filtering."); |
||||||
|
case CommunitiesModel::Roles::ChildrenHidden: |
||||||
|
return false; |
||||||
|
case CommunitiesModel::Roles::Id: |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} else if (index.row() - 1 < tags_.size()) { |
||||||
|
auto tag = tags_.at(index.row() - 1); |
||||||
|
if (tag == "m.favourite") { |
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::AvatarUrl: |
||||||
|
return QString(":/icons/icons/ui/star.png"); |
||||||
|
case CommunitiesModel::Roles::DisplayName: |
||||||
|
return tr("Favourites"); |
||||||
|
case CommunitiesModel::Roles::Tooltip: |
||||||
|
return tr("Rooms you have favourited."); |
||||||
|
} |
||||||
|
} else if (tag == "m.lowpriority") { |
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::AvatarUrl: |
||||||
|
return QString(":/icons/icons/ui/star.png"); |
||||||
|
case CommunitiesModel::Roles::DisplayName: |
||||||
|
return tr("Low Priority"); |
||||||
|
case CommunitiesModel::Roles::Tooltip: |
||||||
|
return tr("Rooms with low priority."); |
||||||
|
} |
||||||
|
} else if (tag == "m.server_notice") { |
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::AvatarUrl: |
||||||
|
return QString(":/icons/icons/ui/tag.png"); |
||||||
|
case CommunitiesModel::Roles::DisplayName: |
||||||
|
return tr("Server Notices"); |
||||||
|
case CommunitiesModel::Roles::Tooltip: |
||||||
|
return tr("Messages from your server or administrator."); |
||||||
|
} |
||||||
|
} else { |
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::AvatarUrl: |
||||||
|
return QString(":/icons/icons/ui/tag.png"); |
||||||
|
case CommunitiesModel::Roles::DisplayName: |
||||||
|
return tag.right(2); |
||||||
|
case CommunitiesModel::Roles::Tooltip: |
||||||
|
return tag.right(2); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
switch (role) { |
||||||
|
case CommunitiesModel::Roles::ChildrenHidden: |
||||||
|
return UserSettings::instance()->hiddenTags().contains("tag:" + tag); |
||||||
|
case CommunitiesModel::Roles::Id: |
||||||
|
return "tag:" + tag; |
||||||
|
} |
||||||
|
} |
||||||
|
return QVariant(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
CommunitiesModel::initializeSidebar() |
||||||
|
{ |
||||||
|
std::set<std::string> ts; |
||||||
|
for (const auto &e : cache::roomInfo()) { |
||||||
|
for (const auto &t : e.tags) { |
||||||
|
if (t.find("u.") == 0 || t.find("m." == 0)) { |
||||||
|
ts.insert(t); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
beginResetModel(); |
||||||
|
tags_.clear(); |
||||||
|
for (const auto &t : ts) |
||||||
|
tags_.push_back(QString::fromStdString(t)); |
||||||
|
endResetModel(); |
||||||
|
|
||||||
|
emit tagsChanged(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
CommunitiesModel::clear() |
||||||
|
{ |
||||||
|
beginResetModel(); |
||||||
|
tags_.clear(); |
||||||
|
endResetModel(); |
||||||
|
|
||||||
|
emit tagsChanged(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
CommunitiesModel::sync(const mtx::responses::Rooms &rooms) |
||||||
|
{ |
||||||
|
bool tagsUpdated = false; |
||||||
|
|
||||||
|
for (const auto &[roomid, room] : rooms.join) { |
||||||
|
(void)roomid; |
||||||
|
for (const auto &e : room.account_data.events) |
||||||
|
if (std::holds_alternative< |
||||||
|
mtx::events::AccountDataEvent<mtx::events::account_data::Tags>>(e)) { |
||||||
|
tagsUpdated = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (tagsUpdated) |
||||||
|
initializeSidebar(); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
CommunitiesModel::setCurrentTagId(QString tagId) |
||||||
|
{ |
||||||
|
if (tagId.startsWith("tag:")) { |
||||||
|
auto tag = tagId.remove(0, 4); |
||||||
|
for (const auto &t : tags_) { |
||||||
|
if (t == tag) { |
||||||
|
this->currentTagId_ = tagId; |
||||||
|
emit currentTagIdChanged(); |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
this->currentTagId_ = ""; |
||||||
|
emit currentTagIdChanged(); |
||||||
|
} |
@ -0,0 +1,60 @@ |
|||||||
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <QAbstractListModel> |
||||||
|
#include <QHash> |
||||||
|
#include <QString> |
||||||
|
#include <QStringList> |
||||||
|
|
||||||
|
#include <mtx/responses/sync.hpp> |
||||||
|
|
||||||
|
class CommunitiesModel : public QAbstractListModel |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
Q_PROPERTY(QString currentTagId READ currentTagId WRITE setCurrentTagId NOTIFY |
||||||
|
currentTagIdChanged RESET resetCurrentTagId) |
||||||
|
Q_PROPERTY(QStringList tags READ tags NOTIFY tagsChanged) |
||||||
|
|
||||||
|
public: |
||||||
|
enum Roles |
||||||
|
{ |
||||||
|
AvatarUrl = Qt::UserRole, |
||||||
|
DisplayName, |
||||||
|
Tooltip, |
||||||
|
ChildrenHidden, |
||||||
|
Id, |
||||||
|
}; |
||||||
|
|
||||||
|
CommunitiesModel(QObject *parent = nullptr); |
||||||
|
QHash<int, QByteArray> roleNames() const override; |
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override |
||||||
|
{ |
||||||
|
(void)parent; |
||||||
|
return 1 + tags_.size(); |
||||||
|
} |
||||||
|
QVariant data(const QModelIndex &index, int role) const override; |
||||||
|
|
||||||
|
public slots: |
||||||
|
void initializeSidebar(); |
||||||
|
void sync(const mtx::responses::Rooms &rooms); |
||||||
|
void clear(); |
||||||
|
QString currentTagId() const { return currentTagId_; } |
||||||
|
void setCurrentTagId(QString tagId); |
||||||
|
void resetCurrentTagId() |
||||||
|
{ |
||||||
|
currentTagId_.clear(); |
||||||
|
emit currentTagIdChanged(); |
||||||
|
} |
||||||
|
QStringList tags() const { return tags_; } |
||||||
|
|
||||||
|
signals: |
||||||
|
void currentTagIdChanged(); |
||||||
|
void tagsChanged(); |
||||||
|
|
||||||
|
private: |
||||||
|
QStringList tags_; |
||||||
|
QString currentTagId_; |
||||||
|
}; |
Loading…
Reference in new issue