2019-09-07 23:22:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QQuickAsyncImageProvider>
|
|
|
|
#include <QQuickImageResponse>
|
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QThreadPool>
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
#include <mtx/common.hpp>
|
|
|
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2019-09-07 23:22:07 +03:00
|
|
|
class MxcImageResponse
|
|
|
|
: public QQuickImageResponse
|
|
|
|
, public QRunnable
|
|
|
|
{
|
|
|
|
public:
|
2019-12-04 01:34:16 +03:00
|
|
|
MxcImageResponse(const QString &id,
|
|
|
|
const QSize &requestedSize,
|
|
|
|
boost::optional<mtx::crypto::EncryptedFile> encryptionInfo)
|
2019-09-07 23:22:07 +03:00
|
|
|
: m_id(id)
|
|
|
|
, m_requestedSize(requestedSize)
|
2019-12-04 01:34:16 +03:00
|
|
|
, m_encryptionInfo(encryptionInfo)
|
2019-09-07 23:22:07 +03:00
|
|
|
{
|
|
|
|
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;
|
2019-12-04 01:34:16 +03:00
|
|
|
boost::optional<mtx::crypto::EncryptedFile> m_encryptionInfo;
|
2019-09-07 23:22:07 +03:00
|
|
|
};
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
class MxcImageProvider
|
|
|
|
: public QObject
|
|
|
|
, public QQuickAsyncImageProvider
|
2019-09-07 23:22:07 +03:00
|
|
|
{
|
2019-12-04 01:34:16 +03:00
|
|
|
Q_OBJECT
|
|
|
|
public slots:
|
2019-09-07 23:22:07 +03:00
|
|
|
QQuickImageResponse *requestImageResponse(const QString &id,
|
|
|
|
const QSize &requestedSize) override
|
|
|
|
{
|
2019-12-04 01:34:16 +03:00
|
|
|
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);
|
2019-09-07 23:22:07 +03:00
|
|
|
pool.start(response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-12-04 01:34:16 +03:00
|
|
|
void addEncryptionInfo(mtx::crypto::EncryptedFile info)
|
|
|
|
{
|
|
|
|
infos.insert(QString::fromStdString(info.url), info);
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:22:07 +03:00
|
|
|
private:
|
|
|
|
QThreadPool pool;
|
2019-12-04 01:34:16 +03:00
|
|
|
QHash<QString, mtx::crypto::EncryptedFile> infos;
|
2019-09-07 23:22:07 +03:00
|
|
|
};
|