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 {
enabled: filter === 2
expression: category === 1 || category === 2 || category === 4
expression: category === 1 || category === 2 || category === 3
},
ExpressionFilter {
enabled: filter === 3
expression: category === 3 || category === 5
expression: category === 4 || category === 5
}
]
}

View File

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

View File

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