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

#include "CombinedImagePackModel.h"

#include "Cache_p.h"
#include "CompletionModelRoles.h"

CombinedImagePackModel::CombinedImagePackModel(const std::string &roomId,
                                               bool stickers,
                                               QObject *parent)
  : QAbstractListModel(parent)
  , room_id(roomId)
{
    auto packs = cache::client()->getImagePacks(room_id, stickers);

    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)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::OriginalRow, "originalRow"},
    };
}

QVariant
CombinedImagePackModel::data(const QModelIndex &index, int role) const
{
    if (hasIndex(index.row(), index.column(), index.parent())) {
        switch (role) {
        case CompletionModel::CompletionRole:
            return QStringLiteral(
                     "<img data-mx-emoticon height=32 src=\"%1\" alt=\"%2\" title=\"%2\">")
              .arg(QString::fromStdString(images[index.row()].image.url).toHtmlEscaped(),
                   !images[index.row()].image.body.empty()
                     ? QString::fromStdString(images[index.row()].image.body)
                     : images[index.row()].shortcode);
        case Roles::Url:
            return QString::fromStdString(images[index.row()].image.url);
        case CompletionModel::SearchRole:
        case Roles::ShortCode:
            return images[index.row()].shortcode;
        case CompletionModel::SearchRole2:
        case Roles::Body:
            return QString::fromStdString(images[index.row()].image.body);
        case Roles::PackName:
            return images[index.row()].packname;
        case Roles::OriginalRow:
            return index.row();
        default:
            return {};
        }
    }
    return {};
}