Simplify connection handling in image provider
Controller is a singleton now, no need to pass the connection object around
This commit is contained in:
@@ -105,9 +105,7 @@ int main(int argc, char *argv[])
|
||||
Controller::instance().setAboutData(about);
|
||||
|
||||
engine.addImportPath("qrc:/imports");
|
||||
MatrixImageProvider *matrixImageProvider = new MatrixImageProvider();
|
||||
engine.rootContext()->setContextProperty("imageProvider", matrixImageProvider);
|
||||
engine.addImageProvider(QLatin1String("mxc"), matrixImageProvider);
|
||||
engine.addImageProvider(QLatin1String("mxc"), new MatrixImageProvider);
|
||||
|
||||
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
||||
if (engine.rootObjects().isEmpty())
|
||||
|
||||
@@ -12,11 +12,12 @@
|
||||
#include <QStandardPaths>
|
||||
#include <QThread>
|
||||
|
||||
#include "controller.h"
|
||||
|
||||
using Quotient::BaseJob;
|
||||
|
||||
ThumbnailResponse::ThumbnailResponse(Quotient::Connection *c, QString id, QSize size)
|
||||
: c(c)
|
||||
, mediaId(std::move(id))
|
||||
ThumbnailResponse::ThumbnailResponse(QString id, QSize size)
|
||||
: mediaId(std::move(id))
|
||||
, requestedSize(size)
|
||||
, localFile(QStringLiteral("%1/image_provider/%2-%3x%4.png").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), mediaId, QString::number(requestedSize.width()), QString::number(requestedSize.height())))
|
||||
, errorStr("Image request hasn't started")
|
||||
@@ -39,21 +40,21 @@ ThumbnailResponse::ThumbnailResponse(Quotient::Connection *c, QString id, QSize
|
||||
return;
|
||||
}
|
||||
|
||||
if (!c) {
|
||||
if (!Controller::instance().connection()) {
|
||||
qWarning() << "Current connection is null";
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute a request on the main thread asynchronously
|
||||
moveToThread(c->thread());
|
||||
moveToThread(Controller::instance().connection()->thread());
|
||||
QMetaObject::invokeMethod(this, &ThumbnailResponse::startRequest, Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
void ThumbnailResponse::startRequest()
|
||||
{
|
||||
// Runs in the main thread, not QML thread
|
||||
Q_ASSERT(QThread::currentThread() == c->thread());
|
||||
job = c->getThumbnail(mediaId, requestedSize);
|
||||
Q_ASSERT(QThread::currentThread() == Controller::instance().connection()->thread());
|
||||
job = Controller::instance().connection()->getThumbnail(mediaId, requestedSize);
|
||||
// Connect to any possible outcome including abandonment
|
||||
// to make sure the QML thread is not left stuck forever.
|
||||
connect(job, &BaseJob::finished, this, &ThumbnailResponse::prepareResult);
|
||||
@@ -116,5 +117,5 @@ void ThumbnailResponse::cancel()
|
||||
|
||||
QQuickImageResponse *MatrixImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
|
||||
{
|
||||
return new ThumbnailResponse(m_connection.loadRelaxed(), id, requestedSize);
|
||||
return new ThumbnailResponse(id, requestedSize);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class ThumbnailResponse : public QQuickImageResponse
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ThumbnailResponse(Quotient::Connection *c, QString mediaId, QSize requestedSize);
|
||||
ThumbnailResponse(QString mediaId, QSize requestedSize);
|
||||
~ThumbnailResponse() override = default;
|
||||
|
||||
private Q_SLOTS:
|
||||
@@ -34,7 +34,6 @@ private Q_SLOTS:
|
||||
void doCancel();
|
||||
|
||||
private:
|
||||
Quotient::Connection *c;
|
||||
const QString mediaId;
|
||||
QSize requestedSize;
|
||||
const QString localFile;
|
||||
@@ -49,30 +48,10 @@ private:
|
||||
void cancel() override;
|
||||
};
|
||||
|
||||
class MatrixImageProvider : public QObject, public QQuickAsyncImageProvider
|
||||
class MatrixImageProvider : public QQuickAsyncImageProvider
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
|
||||
public:
|
||||
explicit MatrixImageProvider() = default;
|
||||
|
||||
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
||||
|
||||
Quotient::Connection *connection()
|
||||
{
|
||||
return m_connection;
|
||||
}
|
||||
void setConnection(Quotient::Connection *connection)
|
||||
{
|
||||
m_connection.storeRelaxed(connection);
|
||||
Q_EMIT connectionChanged();
|
||||
}
|
||||
|
||||
Q_SIGNALS:
|
||||
void connectionChanged();
|
||||
|
||||
private:
|
||||
QAtomicPointer<Quotient::Connection> m_connection;
|
||||
};
|
||||
|
||||
#endif // MatrixImageProvider_H
|
||||
|
||||
Reference in New Issue
Block a user