summary refs log tree commit diff
path: root/src/timeline/CommunitiesModel.cpp
blob: 9b758e97cffa52421ab0f77da43682550128a02b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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"},
          {Id, "id"},
        };
}

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.mid(2);
                        case CommunitiesModel::Roles::Tooltip:
                                return tag.mid(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();
        resetCurrentTagId();

        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.mid(4);
                for (const auto &t : tags_) {
                        if (t == tag) {
                                this->currentTagId_ = tagId;
                                emit currentTagIdChanged(currentTagId_);
                                return;
                        }
                }
        }

        this->currentTagId_ = "";
        emit currentTagIdChanged(currentTagId_);
}