Skip to content
Snippets Groups Projects
JdenticonProvider.h 1.88 KiB
Newer Older
  • Learn to ignore specific revisions
  • Loren Burkholder's avatar
    Loren Burkholder committed
    // SPDX-FileCopyrightText: 2021 Nheko Contributors
    //
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    
    Loren Burkholder's avatar
    Loren Burkholder committed
    #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;
    
    Loren Burkholder's avatar
    Loren Burkholder committed
    };
    
    class JdenticonProvider
      : public QObject
      , public QQuickAsyncImageProvider
    {
    
        Q_OBJECT
    
    Loren Burkholder's avatar
    Loren Burkholder committed
    
    public:
    
        static bool isAvailable() { return Jdenticon::getJdenticonInterface() != nullptr; }
    
    Loren Burkholder's avatar
    Loren Burkholder committed
    
    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;
        }
    
    
    Loren Burkholder's avatar
    Loren Burkholder committed
    private:
    
        QThreadPool pool;