summary refs log tree commit diff
path: root/src/MxcImageProvider.h
blob: 2c197a13c552fe944be896c6f3c487210007c4e3 (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
#pragma once

#include <QQuickAsyncImageProvider>
#include <QQuickImageResponse>

#include <QImage>
#include <QThreadPool>

#include <mtx/common.hpp>

#include <boost/optional.hpp>

class MxcImageResponse
  : public QQuickImageResponse
  , public QRunnable
{
public:
        MxcImageResponse(const QString &id,
                         const QSize &requestedSize,
                         boost::optional<mtx::crypto::EncryptedFile> encryptionInfo)
          : m_id(id)
          , m_requestedSize(requestedSize)
          , m_encryptionInfo(encryptionInfo)
        {
                setAutoDelete(false);
        }

        QQuickTextureFactory *textureFactory() const override
        {
                return QQuickTextureFactory::textureFactoryForImage(m_image);
        }
        QString errorString() const override { return m_error; }

        void run() override;

        QString m_id, m_error;
        QSize m_requestedSize;
        QImage m_image;
        boost::optional<mtx::crypto::EncryptedFile> m_encryptionInfo;
};

class MxcImageProvider
  : public QObject
  , public QQuickAsyncImageProvider
{
        Q_OBJECT
public slots:
        QQuickImageResponse *requestImageResponse(const QString &id,
                                                  const QSize &requestedSize) override
        {
                boost::optional<mtx::crypto::EncryptedFile> info;
                auto temp = infos.find("mxc://" + id);
                if (temp != infos.end())
                        info = *temp;

                MxcImageResponse *response = new MxcImageResponse(id, requestedSize, info);
                pool.start(response);
                return response;
        }

        void addEncryptionInfo(mtx::crypto::EncryptedFile info)
        {
                infos.insert(QString::fromStdString(info.url), info);
        }

private:
        QThreadPool pool;
        QHash<QString, mtx::crypto::EncryptedFile> infos;
};