nheko/src/JdenticonProvider.h

81 lines
1.8 KiB
C
Raw Normal View History

2021-08-13 02:49:25 +02:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-12-25 15:14:00 +01:00
#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:
2021-09-18 00:22:33 +02:00
JdenticonResponse(const QString &key, bool crop, double radius, const QSize &requestedSize);
2020-12-25 15:14:00 +01:00
2021-09-18 00:22:33 +02:00
QQuickTextureFactory *textureFactory() const override
{
return QQuickTextureFactory::textureFactoryForImage(m_pixmap.toImage());
}
2020-12-25 15:14:00 +01:00
2021-09-18 00:22:33 +02:00
void run() override;
2020-12-25 15:14:00 +01:00
2021-09-18 00:22:33 +02:00
QString m_key;
bool m_crop;
double m_radius;
QSize m_requestedSize;
QPixmap m_pixmap;
JdenticonInterface *jdenticonInterface_ = nullptr;
2020-12-25 15:14:00 +01:00
};
class JdenticonProvider
: public QObject
, public QQuickAsyncImageProvider
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2020-12-25 15:14:00 +01:00
public:
2021-09-18 00:22:33 +02:00
static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; }
2020-12-25 15:14:00 +01:00
public slots:
QQuickImageResponse *
requestImageResponse(const QString &id, const QSize &requestedSize) override
2021-09-18 00:22:33 +02:00
{
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();
2021-08-14 02:05:51 +02:00
}
2021-09-18 00:22:33 +02:00
}
2020-12-25 15:14:00 +01:00
}
2021-09-18 00:22:33 +02:00
JdenticonResponse *response = new JdenticonResponse(id_, crop, radius, requestedSize);
pool.start(response);
return response;
}
2020-12-25 15:14:00 +01:00
private:
2021-09-18 00:22:33 +02:00
QThreadPool pool;
2020-12-25 15:14:00 +01:00
};