2018-03-02 08:56:36 +00:00
|
|
|
#include "imageprovider.h"
|
|
|
|
|
2018-08-27 11:29:50 +00:00
|
|
|
#include <QFile>
|
2018-03-02 08:56:36 +00:00
|
|
|
#include <QMetaObject>
|
2018-08-27 11:29:50 +00:00
|
|
|
#include <QStandardPaths>
|
2018-07-09 02:45:26 +00:00
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtCore/QWaitCondition>
|
2018-03-02 08:56:36 +00:00
|
|
|
|
2018-03-15 09:10:27 +00:00
|
|
|
#include "jobs/mediathumbnailjob.h"
|
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
2018-03-02 08:56:36 +00:00
|
|
|
using QMatrixClient::MediaThumbnailJob;
|
|
|
|
|
2018-07-07 09:38:20 +00:00
|
|
|
ImageProvider::ImageProvider(QObject* parent)
|
2018-07-09 02:45:26 +00:00
|
|
|
: QQuickImageProvider(
|
|
|
|
QQmlImageProviderBase::Image,
|
|
|
|
QQmlImageProviderBase::ForceAsynchronousImageLoading) {
|
2018-03-02 08:56:36 +00:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))
|
2018-07-09 02:45:26 +00:00
|
|
|
qRegisterMetaType<MediaThumbnailJob*>();
|
2018-03-02 08:56:36 +00:00
|
|
|
#endif
|
2018-07-09 02:45:26 +00:00
|
|
|
m_connection = new ImageProviderConnection();
|
2018-03-02 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
QImage ImageProvider::requestImage(const QString& id, QSize* pSize,
|
|
|
|
const QSize& requestedSize) {
|
|
|
|
if (!id.startsWith("mxc://")) {
|
|
|
|
qWarning() << "ImageProvider: won't fetch an invalid id:" << id
|
|
|
|
<< "doesn't follow server/mediaId pattern";
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-02 08:56:36 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
QUrl mxcUri{id};
|
2018-03-02 08:56:36 +00:00
|
|
|
|
2018-08-27 11:29:50 +00:00
|
|
|
QUrl tempfilePath = QUrl::fromLocalFile(
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" +
|
|
|
|
mxcUri.fileName() + "-" + QString::number(requestedSize.width()) +
|
|
|
|
"x" + QString::number(requestedSize.height()) + ".png");
|
|
|
|
|
|
|
|
QImage cachedImage;
|
|
|
|
if (cachedImage.load(tempfilePath.toLocalFile())) return cachedImage;
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
MediaThumbnailJob* job = nullptr;
|
|
|
|
QReadLocker locker(&m_lock);
|
2018-07-07 09:38:20 +00:00
|
|
|
|
2018-03-02 08:56:36 +00:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
2018-07-09 02:45:26 +00:00
|
|
|
QMetaObject::invokeMethod(
|
|
|
|
m_connection,
|
|
|
|
[=] {
|
|
|
|
return m_connection->getConnection()->getThumbnail(mxcUri,
|
|
|
|
requestedSize);
|
|
|
|
},
|
|
|
|
Qt::BlockingQueuedConnection, &job);
|
2018-03-02 08:56:36 +00:00
|
|
|
#else
|
2018-08-01 15:49:47 +00:00
|
|
|
QMetaObject::invokeMethod(m_connection->getConnection(), "getThumbnail",
|
2018-07-09 02:45:26 +00:00
|
|
|
Qt::BlockingQueuedConnection,
|
|
|
|
Q_RETURN_ARG(MediaThumbnailJob*, job),
|
|
|
|
Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize));
|
2018-03-02 08:56:36 +00:00
|
|
|
#endif
|
2018-07-09 02:45:26 +00:00
|
|
|
if (!job) {
|
|
|
|
qDebug() << "ImageProvider: failed to send a request";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
QImage result;
|
|
|
|
{
|
|
|
|
QWaitCondition condition; // The most compact way to block on a signal
|
|
|
|
job->connect(job, &MediaThumbnailJob::finished, job, [&] {
|
|
|
|
result = job->thumbnail();
|
|
|
|
condition.wakeAll();
|
|
|
|
});
|
|
|
|
condition.wait(&m_lock);
|
|
|
|
}
|
2018-03-02 08:56:36 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
if (pSize != nullptr) *pSize = result.size();
|
2018-03-02 08:56:36 +00:00
|
|
|
|
2018-08-27 11:29:50 +00:00
|
|
|
result.save(tempfilePath.toLocalFile());
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
return result;
|
2018-03-02 08:56:36 +00:00
|
|
|
}
|