summary refs log tree commit diff
path: root/src/RoomsModel.cpp
blob: 782c4971421df5f5ce2ec3a10b777fe9459a25b4 (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
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 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"

RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent)
  : QAbstractListModel(parent)
  , showOnlyRoomWithAliases_(showOnlyRoomWithAliases)
{
    std::vector<std::string> rooms_ = cache::joinedRooms();
    roomInfos                       = cache::getRoomInfo(rooms_);
    if (!showOnlyRoomWithAliases_) {
        roomids.reserve(rooms_.size());
        roomAliases.reserve(rooms_.size());
    }

    for (const auto &r : rooms_) {
        auto roomAliasesList = cache::client()->getRoomAliases(r);

        if (showOnlyRoomWithAliases_) {
            if (roomAliasesList && !roomAliasesList->alias.empty()) {
                roomids.push_back(QString::fromStdString(r));
                roomAliases.push_back(QString::fromStdString(roomAliasesList->alias));
            }
        } else {
            roomids.push_back(QString::fromStdString(r));
            roomAliases.push_back(roomAliasesList ? QString::fromStdString(roomAliasesList->alias)
                                                  : QLatin1String(""));
        }
    }
}

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"}};
}

QVariant
RoomsModel::data(const QModelIndex &index, int role) const
{
    if (hasIndex(index.row(), index.column(), index.parent())) {
        switch (role) {
        case CompletionModel::CompletionRole: {
            if (UserSettings::instance()->markdown()) {
                QString percentEncoding = QUrl::toPercentEncoding(roomAliases[index.row()]);
                return QStringLiteral("[%1](https://matrix.to/#/%2)")
                  .arg(roomAliases[index.row()], percentEncoding);
            } else {
                return roomAliases[index.row()];
            }
        }
        case CompletionModel::SearchRole:
        case Qt::DisplayRole:
        case Roles::RoomAlias:
            return roomAliases[index.row()].toHtmlEscaped();
        case CompletionModel::SearchRole2:
        case Roles::RoomName:
            return QString::fromStdString(roomInfos.at(roomids[index.row()]).name).toHtmlEscaped();
        case Roles::AvatarUrl:
            return QString::fromStdString(roomInfos.at(roomids[index.row()]).avatar_url);
        case Roles::RoomID:
            return roomids[index.row()].toHtmlEscaped();
        }
    }
    return {};
}