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

#include "CombinedImagePackModel.h"

#include "Cache.h"
#include "CompletionModelRoles.h"
#include "emoji/Provider.h"

CombinedImagePackModel::CombinedImagePackModel(const std::string &roomId, QObject *parent)
  : QAbstractListModel(parent)
  , room_id(roomId)
{
    auto packs = cache::getImagePacks(room_id, false);

    for (const auto &pack : packs) {
        QString packname =
          pack.pack.pack ? QString::fromStdString(pack.pack.pack->display_name) : QString();

        for (const auto &img : pack.pack.images) {
            ImageDesc i{};
            i.shortcode = QString::fromStdString(img.first);
            i.packname  = packname;
            i.image     = img.second;
            images.push_back(std::move(i));
        }
    }
}

int
CombinedImagePackModel::rowCount(const QModelIndex &) const
{
    return (int)(emoji::Provider::emoji.size() + images.size());
}

QHash<int, QByteArray>
CombinedImagePackModel::roleNames() const
{
    return {
      {CompletionModel::CompletionRole, "completionRole"},
      {CompletionModel::SearchRole, "searchRole"},
      {CompletionModel::SearchRole2, "searchRole2"},
      {Roles::Url, "url"},
      {Roles::ShortCode, "shortcode"},
      {Roles::Body, "body"},
      {Roles::PackName, "packname"},
      {Roles::Unicode, "unicode"},
    };
}

QVariant
CombinedImagePackModel::data(const QModelIndex &index, int role) const
{
    using emoji::Provider;
    if (hasIndex(index.row(), index.column(), index.parent())) {
        if (index.row() < (int)emoji::Provider::emoji.size()) {
            switch (role) {
            case CompletionModel::CompletionRole:
            case Roles::Unicode:
                return emoji::Provider::emoji[index.row()].unicode();

            case Qt::ToolTipRole:
                return Provider::emoji[index.row()].shortName() + ", " +
                       Provider::emoji[index.row()].unicodeName();
            case CompletionModel::SearchRole2:
            case Roles::Body:
                return Provider::emoji[index.row()].unicodeName();
            case CompletionModel::SearchRole:
            case Roles::ShortCode:
                return Provider::emoji[index.row()].shortName();
            case Roles::PackName:
                return emoji::categoryToName(Provider::emoji[index.row()].category);
            default:
                return {};
            }
        } else {
            int row = index.row() - static_cast<int>(emoji::Provider::emoji.size());
            switch (role) {
            case CompletionModel::CompletionRole:
                return QStringLiteral(
                         "<img data-mx-emoticon height=\"32\" src=\"%1\" alt=\"%2\" title=\"%2\">")
                  .arg(QString::fromStdString(images[row].image.url).toHtmlEscaped(),
                       !images[row].image.body.empty()
                         ? QString::fromStdString(images[row].image.body)
                         : images[row].shortcode);
            case Roles::Url:
                return QString::fromStdString(images[row].image.url);
            case CompletionModel::SearchRole:
            case Roles::ShortCode:
                return images[row].shortcode;
            case CompletionModel::SearchRole2:
            case Roles::Body:
                return QString::fromStdString(images[row].image.body);
            case Roles::PackName:
                return images[row].packname;
            case Roles::Unicode:
                return QString();
            default:
                return {};
            }
        }
    }
    return {};
}

#include "moc_CombinedImagePackModel.cpp"