Caching in image provider.

Fix room filter.
This commit is contained in:
Black Hat 2019-04-22 21:16:33 +08:00
parent 3779735282
commit 11ede88abc
3 changed files with 45 additions and 16 deletions

View File

@ -71,11 +71,11 @@ Item {
}, },
ExpressionFilter { ExpressionFilter {
enabled: filter === 2 enabled: filter === 2
expression: category === 1 || category === 2 || category === 4 expression: category === 1 || category === 2 || category === 3
}, },
ExpressionFilter { ExpressionFilter {
enabled: filter === 3 enabled: filter === 3
expression: category === 3 || category === 5 expression: category === 4 || category === 5
} }
] ]
} }

View File

@ -1,15 +1,25 @@
#include "imageprovider.h" #include "imageprovider.h"
#include <QDir>
#include <QFileInfo>
#include <QStandardPaths>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QThread> #include <QtCore/QThread>
using QMatrixClient::BaseJob; using QMatrixClient::BaseJob;
ThumbnailResponse::ThumbnailResponse(QMatrixClient::Connection* c, ThumbnailResponse::ThumbnailResponse(QMatrixClient::Connection* c,
QString id, const QSize& size) QString id,
const QSize& size)
: c(c), : 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()))),
errorStr("Image request hasn't started") { errorStr("Image request hasn't started") {
if (requestedSize.isEmpty()) { if (requestedSize.isEmpty()) {
errorStr.clear(); errorStr.clear();
@ -18,11 +28,19 @@ ThumbnailResponse::ThumbnailResponse(QMatrixClient::Connection* c,
} }
if (mediaId.count('/') != 1) { if (mediaId.count('/') != 1) {
errorStr = errorStr =
tr("Media id '%1' doesn't follow server/mediaId pattern") tr("Media id '%1' doesn't follow server/mediaId pattern").arg(mediaId);
.arg(mediaId);
emit finished(); emit finished();
return; return;
} }
QImage cachedImage;
if (cachedImage.load(localFile)) {
image = cachedImage;
errorStr.clear();
emit finished();
return;
}
// Execute a request on the main thread asynchronously // Execute a request on the main thread asynchronously
moveToThread(c->thread()); moveToThread(c->thread());
QMetaObject::invokeMethod(this, &ThumbnailResponse::startRequest, QMetaObject::invokeMethod(this, &ThumbnailResponse::startRequest,
@ -45,6 +63,14 @@ void ThumbnailResponse::prepareResult() {
QWriteLocker _(&lock); QWriteLocker _(&lock);
if (job->error() == BaseJob::Success) { if (job->error() == BaseJob::Success) {
image = job->thumbnail(); image = job->thumbnail();
QString localPath = QFileInfo(localFile).absolutePath();
QDir dir;
if (!dir.exists(localPath))
dir.mkpath(localPath);
image.save(localFile);
errorStr.clear(); errorStr.clear();
} else if (job->error() == BaseJob::Abandoned) { } else if (job->error() == BaseJob::Abandoned) {
errorStr = tr("Image request has been cancelled"); errorStr = tr("Image request has been cancelled");
@ -83,7 +109,7 @@ void ThumbnailResponse::cancel() {
} }
QQuickImageResponse* ImageProvider::requestImageResponse( QQuickImageResponse* ImageProvider::requestImageResponse(
const QString& id, const QSize& requestedSize) { const QString& id,
qDebug() << "ImageProvider: requesting " << id << "of size" << requestedSize; const QSize& requestedSize) {
return new ThumbnailResponse(m_connection.load(), id, requestedSize); return new ThumbnailResponse(m_connection.load(), id, requestedSize);
} }

View File

@ -7,8 +7,8 @@
#include <connection.h> #include <connection.h>
#include <jobs/mediathumbnailjob.h> #include <jobs/mediathumbnailjob.h>
#include <QtCore/QReadWriteLock>
#include <QtCore/QAtomicPointer> #include <QtCore/QAtomicPointer>
#include <QtCore/QReadWriteLock>
namespace QMatrixClient { namespace QMatrixClient {
class Connection; class Connection;
@ -17,11 +17,12 @@ class Connection;
class ThumbnailResponse : public QQuickImageResponse { class ThumbnailResponse : public QQuickImageResponse {
Q_OBJECT Q_OBJECT
public: public:
ThumbnailResponse(QMatrixClient::Connection* c, QString mediaId, ThumbnailResponse(QMatrixClient::Connection* c,
QString mediaId,
const QSize& requestedSize); const QSize& requestedSize);
~ThumbnailResponse() override = default; ~ThumbnailResponse() override = default;
private slots: private slots:
void startRequest(); void startRequest();
void prepareResult(); void prepareResult();
void doCancel(); void doCancel();
@ -30,6 +31,7 @@ private slots:
QMatrixClient::Connection* c; QMatrixClient::Connection* c;
const QString mediaId; const QString mediaId;
const QSize requestedSize; const QSize requestedSize;
const QString localFile;
QMatrixClient::MediaThumbnailJob* job = nullptr; QMatrixClient::MediaThumbnailJob* job = nullptr;
QImage image; QImage image;
@ -49,7 +51,8 @@ class ImageProvider : public QObject, public QQuickAsyncImageProvider {
explicit ImageProvider() = default; explicit ImageProvider() = default;
QQuickImageResponse* requestImageResponse( QQuickImageResponse* requestImageResponse(
const QString& id, const QSize& requestedSize) override; const QString& id,
const QSize& requestedSize) override;
QMatrixClient::Connection* connection() { return m_connection; } QMatrixClient::Connection* connection() { return m_connection; }
void setConnection(QMatrixClient::Connection* connection) { void setConnection(QMatrixClient::Connection* connection) {