summary refs log tree commit diff
path: root/src/JdenticonProvider.h
blob: bcda29c83abf6305b49dc5afcf9ea3489c23ab0e (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-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QImage>
#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>
#include <QThreadPool>

#include <mtx/common.hpp>

#include "jdenticoninterface.h"

namespace Jdenticon {
JdenticonInterface *
getJdenticonInterface();
}

class JdenticonResponse
  : public QQuickImageResponse
  , public QRunnable
{
public:
        JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize);

        QQuickTextureFactory *textureFactory() const override
        {
                return QQuickTextureFactory::textureFactoryForImage(m_pixmap.toImage());
        }

        void run() override;

        QString m_key;
        bool m_crop;
        double m_radius;
        QSize m_requestedSize;
        QPixmap m_pixmap;
        JdenticonInterface *jdenticonInterface_ = nullptr;
};

class JdenticonProvider
  : public QObject
  , public QQuickAsyncImageProvider
{
        Q_OBJECT

public:
        static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; }

public slots:
        QQuickImageResponse *requestImageResponse(const QString &id,
                                                  const QSize &requestedSize) override
        {
                auto id_      = id;
                bool crop     = true;
                double radius = 0;

                auto queryStart = id.lastIndexOf('?');
                if (queryStart != -1) {
                        id_            = id.left(queryStart);
                        auto query     = id.midRef(queryStart + 1);
                        auto queryBits = query.split('&');

                        for (auto b : queryBits) {
                                if (b.startsWith("radius=")) {
                                        radius = b.mid(7).toDouble();
                                }
                        }
                }

                JdenticonResponse *response =
                  new JdenticonResponse(id_, crop, radius, requestedSize);
                pool.start(response);
                return response;
        }

private:
        QThreadPool pool;
};