summary refs log tree commit diff
path: root/src/RoomsModel.cpp
blob: fff9cbc68a4ae135702db883bcde53eb63ff7360 (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
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "RoomsModel.h"

#include <QUrl>

#include "Cache_p.h"
#include "CompletionModelRoles.h"
#include "UserSettingsPage.h"
#include "Utils.h"

RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent)
  : QAbstractListModel(parent)
  , showOnlyRoomWithAliases_(showOnlyRoomWithAliases)
{
    rooms = cache::client()->roomNamesAndAliases();

    if (showOnlyRoomWithAliases_)
        utils::erase_if(rooms, [](auto &r) { return r.alias.empty(); });

    std::ranges::sort(rooms,
                      [](auto &a, auto &b) { return a.recent_activity > b.recent_activity; });
}

QHash<int, QByteArray>
RoomsModel::roleNames() const
{
    return {
      {CompletionModel::CompletionRole, "completionRole"},
      {CompletionModel::SearchRole, "searchRole"},
      {CompletionModel::SearchRole2, "searchRole2"},
      {Roles::RoomAlias, "roomAlias"},
      {Roles::AvatarUrl, "avatarUrl"},
      {Roles::RoomID, "roomid"},
      {Roles::RoomName, "roomName"},
      {Roles::IsTombstoned, "isTombstoned"},
      {Roles::IsSpace, "isSpace"},
    };
}

QVariant
RoomsModel::data(const QModelIndex &index, int role) const
{
    if (hasIndex(index.row(), index.column(), index.parent())) {
        switch (role) {
        case CompletionModel::CompletionRole: {
            auto alias = QString::fromStdString(rooms[index.row()].alias);
            if (UserSettings::instance()->markdown()) {
                QString percentEncoding = QUrl::toPercentEncoding(alias);
                return QStringLiteral("[%1](https://matrix.to/#/%2)")
                  .arg(alias.replace("[", "\\[").replace("]", "\\]").toHtmlEscaped(),
                       percentEncoding);
            } else {
                return alias;
            }
        }
        case CompletionModel::SearchRole:
        case Qt::DisplayRole:
        case Roles::RoomAlias:
            return QString::fromStdString(rooms[index.row()].alias).toHtmlEscaped();
        case CompletionModel::SearchRole2:
        case Roles::RoomName:
            return QString::fromStdString(rooms[index.row()].name);
        case Roles::AvatarUrl:
            return QString::fromStdString(
              cache::client()->singleRoomInfo(rooms[index.row()].id).avatar_url);
        case Roles::RoomID:
            return QString::fromStdString(rooms[index.row()].id).toHtmlEscaped();
        case Roles::IsTombstoned:
            return rooms[index.row()].is_tombstoned;
        case Roles::IsSpace:
            return rooms[index.row()].is_space;
        }
    }
    return {};
}