Simplify connection handling in image provider
Controller is a singleton now, no need to pass the connection object around
This commit is contained in:
@@ -85,12 +85,6 @@ Kirigami.ApplicationWindow {
|
|||||||
onErrorOccured: showPassiveNotification(error + ": " + detail)
|
onErrorOccured: showPassiveNotification(error + ": " + detail)
|
||||||
}
|
}
|
||||||
|
|
||||||
Binding {
|
|
||||||
target: imageProvider
|
|
||||||
property: "connection"
|
|
||||||
value: Controller.connection
|
|
||||||
}
|
|
||||||
|
|
||||||
RoomListModel {
|
RoomListModel {
|
||||||
id: spectralRoomListModel
|
id: spectralRoomListModel
|
||||||
|
|
||||||
|
|||||||
@@ -105,9 +105,7 @@ int main(int argc, char *argv[])
|
|||||||
Controller::instance().setAboutData(about);
|
Controller::instance().setAboutData(about);
|
||||||
|
|
||||||
engine.addImportPath("qrc:/imports");
|
engine.addImportPath("qrc:/imports");
|
||||||
MatrixImageProvider *matrixImageProvider = new MatrixImageProvider();
|
engine.addImageProvider(QLatin1String("mxc"), new MatrixImageProvider);
|
||||||
engine.rootContext()->setContextProperty("imageProvider", matrixImageProvider);
|
|
||||||
engine.addImageProvider(QLatin1String("mxc"), matrixImageProvider);
|
|
||||||
|
|
||||||
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
||||||
if (engine.rootObjects().isEmpty())
|
if (engine.rootObjects().isEmpty())
|
||||||
|
|||||||
@@ -12,11 +12,12 @@
|
|||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
|
||||||
|
#include "controller.h"
|
||||||
|
|
||||||
using Quotient::BaseJob;
|
using Quotient::BaseJob;
|
||||||
|
|
||||||
ThumbnailResponse::ThumbnailResponse(Quotient::Connection *c, QString id, QSize size)
|
ThumbnailResponse::ThumbnailResponse(QString id, QSize size)
|
||||||
: c(c)
|
: mediaId(std::move(id))
|
||||||
, mediaId(std::move(id))
|
|
||||||
, requestedSize(size)
|
, 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())))
|
, 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")
|
, errorStr("Image request hasn't started")
|
||||||
@@ -39,21 +40,21 @@ ThumbnailResponse::ThumbnailResponse(Quotient::Connection *c, QString id, QSize
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!c) {
|
if (!Controller::instance().connection()) {
|
||||||
qWarning() << "Current connection is null";
|
qWarning() << "Current connection is null";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute a request on the main thread asynchronously
|
// Execute a request on the main thread asynchronously
|
||||||
moveToThread(c->thread());
|
moveToThread(Controller::instance().connection()->thread());
|
||||||
QMetaObject::invokeMethod(this, &ThumbnailResponse::startRequest, Qt::QueuedConnection);
|
QMetaObject::invokeMethod(this, &ThumbnailResponse::startRequest, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThumbnailResponse::startRequest()
|
void ThumbnailResponse::startRequest()
|
||||||
{
|
{
|
||||||
// Runs in the main thread, not QML thread
|
// Runs in the main thread, not QML thread
|
||||||
Q_ASSERT(QThread::currentThread() == c->thread());
|
Q_ASSERT(QThread::currentThread() == Controller::instance().connection()->thread());
|
||||||
job = c->getThumbnail(mediaId, requestedSize);
|
job = Controller::instance().connection()->getThumbnail(mediaId, requestedSize);
|
||||||
// Connect to any possible outcome including abandonment
|
// Connect to any possible outcome including abandonment
|
||||||
// to make sure the QML thread is not left stuck forever.
|
// to make sure the QML thread is not left stuck forever.
|
||||||
connect(job, &BaseJob::finished, this, &ThumbnailResponse::prepareResult);
|
connect(job, &BaseJob::finished, this, &ThumbnailResponse::prepareResult);
|
||||||
@@ -116,5 +117,5 @@ void ThumbnailResponse::cancel()
|
|||||||
|
|
||||||
QQuickImageResponse *MatrixImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize)
|
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
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
ThumbnailResponse(Quotient::Connection *c, QString mediaId, QSize requestedSize);
|
ThumbnailResponse(QString mediaId, QSize requestedSize);
|
||||||
~ThumbnailResponse() override = default;
|
~ThumbnailResponse() override = default;
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
@@ -34,7 +34,6 @@ private Q_SLOTS:
|
|||||||
void doCancel();
|
void doCancel();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Quotient::Connection *c;
|
|
||||||
const QString mediaId;
|
const QString mediaId;
|
||||||
QSize requestedSize;
|
QSize requestedSize;
|
||||||
const QString localFile;
|
const QString localFile;
|
||||||
@@ -49,30 +48,10 @@ private:
|
|||||||
void cancel() override;
|
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:
|
public:
|
||||||
explicit MatrixImageProvider() = default;
|
|
||||||
|
|
||||||
QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
|
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
|
#endif // MatrixImageProvider_H
|
||||||
|
|||||||
Reference in New Issue
Block a user