File indexing completed on 2024-04-21 04:59:27

0001 // SPDX-FileCopyrightText: 2018-2019 Black Hat <bhat@encom.eu.org>
0002 // SPDX-FileCopyrightText: 2019 Kitsune Ral <kitsune-ral@users.sf.net>
0003 // SPDX-License-Identifier: GPL-3.0-only
0004 
0005 #pragma once
0006 
0007 #include <QQuickAsyncImageProvider>
0008 
0009 #include <Quotient/jobs/mediathumbnailjob.h>
0010 
0011 #include <QReadWriteLock>
0012 
0013 class NeoChatConnection;
0014 
0015 /**
0016  * @class ThumbnailResponse
0017  *
0018  * A QQuickImageResponse for an mxc image.
0019  *
0020  * @sa QQuickImageResponse
0021  */
0022 class ThumbnailResponse : public QQuickImageResponse
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit ThumbnailResponse(QString mediaId, QSize requestedSize, NeoChatConnection *m_connection);
0027     ~ThumbnailResponse() override = default;
0028 
0029 private Q_SLOTS:
0030     void startRequest();
0031     void prepareResult();
0032 
0033 private:
0034     QString mediaId;
0035     QSize requestedSize;
0036     const QString localFile;
0037     Quotient::MediaThumbnailJob *job = nullptr;
0038     NeoChatConnection *m_connection;
0039 
0040     QImage image;
0041     QString errorStr;
0042     mutable QReadWriteLock lock; // Guards ONLY these two members above
0043 
0044     QQuickTextureFactory *textureFactory() const override;
0045     QString errorString() const override;
0046 };
0047 
0048 /**
0049  * @class MatrixImageProvider
0050  *
0051  * A QQuickAsyncImageProvider for mxc images.
0052  *
0053  * @sa QQuickAsyncImageProvider
0054  */
0055 class MatrixImageProvider : public QQuickAsyncImageProvider
0056 {
0057     Q_OBJECT
0058     QML_ELEMENT
0059     QML_SINGLETON
0060 
0061     Q_PROPERTY(NeoChatConnection *connection MEMBER m_connection)
0062 public:
0063     static MatrixImageProvider *create(QQmlEngine *engine, QJSEngine *)
0064     {
0065         static MatrixImageProvider *instance = new MatrixImageProvider;
0066         engine->setObjectOwnership(instance, QQmlEngine::CppOwnership);
0067         return instance;
0068     }
0069 
0070     /**
0071      * @brief Return a job to provide the image with the given ID.
0072      *
0073      * @sa QQuickAsyncImageProvider::requestImageResponse
0074      */
0075     QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
0076 
0077 private:
0078     NeoChatConnection *m_connection = nullptr;
0079     MatrixImageProvider() = default;
0080 };