diff options
author | Black Hat <bhat@encom.eu.org> | 2018-09-21 19:21:02 +0800 |
---|---|---|
committer | Black Hat <bhat@encom.eu.org> | 2018-09-21 19:21:02 +0800 |
commit | ffc32a7bba6782ab4f341aa0f4bd46596475f5b9 (patch) | |
tree | a76456c380354efe3220054adafda77147fabaf6 | |
parent | 9fb5fd1181fc74a040630df333a20ddd29cd7b28 (diff) | |
download | libquotient-ffc32a7bba6782ab4f341aa0f4bd46596475f5b9.tar.gz libquotient-ffc32a7bba6782ab4f341aa0f4bd46596475f5b9.zip |
Add avatar caching.
-rw-r--r-- | lib/avatar.cpp | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/lib/avatar.cpp b/lib/avatar.cpp index 438268b6..8495df31 100644 --- a/lib/avatar.cpp +++ b/lib/avatar.cpp @@ -26,6 +26,7 @@ #include <QtGui/QPainter> #include <QtCore/QPointer> +#include <QStandardPaths> using namespace QMatrixClient; using std::move; @@ -33,7 +34,9 @@ using std::move; class Avatar::Private { public: - explicit Private(QUrl url = {}) : _url(move(url)) { } + explicit Private(QUrl url = {}) : _url(move(url)) { + _localFile = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + url.authority() + "_" + url.fileName() + ".png"); + } QImage get(Connection* connection, QSize size, get_callback_t callback) const; @@ -42,6 +45,7 @@ class Avatar::Private bool checkUrl(QUrl url) const; QUrl _url; + QUrl _localFile; // The below are related to image caching, hence mutable mutable QImage _originalImage; @@ -109,31 +113,40 @@ QImage Avatar::Private::get(Connection* connection, QSize size, qCCritical(MAIN) << "Null callbacks are not allowed in Avatar::get"; Q_ASSERT(false); } + // FIXME: Alternating between longer-width and longer-height requests // is a sure way to trick the below code into constantly getting another // image from the server because the existing one is alleged unsatisfactory. // This is plain abuse by the client, though; so not critical for now. - if( ( !(_fetched || _thumbnailRequest) - || size.width() > _requestedSize.width() - || size.height() > _requestedSize.height() ) && checkUrl(_url) ) - { - qCDebug(MAIN) << "Getting avatar from" << _url.toString(); - _requestedSize = size; - if (isJobRunning(_thumbnailRequest)) - _thumbnailRequest->abandon(); - if (callback) - callbacks.emplace_back(move(callback)); - _thumbnailRequest = connection->getThumbnail(_url, size); - QObject::connect( _thumbnailRequest, &MediaThumbnailJob::success, - _thumbnailRequest, [this] { + if (!(_fetched || _thumbnailRequest)) { + if (_localFile.isValid() && _originalImage.load(_localFile.toLocalFile())) { + if (!(_originalImage.size().isEmpty() || size.width() > _originalImage.width() || size.height() > _originalImage.height())) { _fetched = true; - _originalImage = - _thumbnailRequest->scaledThumbnail(_requestedSize); _scaledImages.clear(); for (const auto& n: callbacks) n(); callbacks.clear(); - }); + } + } else if (checkUrl(_url)) { + qCDebug(MAIN) << "Getting avatar from" << _url.toString(); + _requestedSize = size; + if (isJobRunning(_thumbnailRequest)) + _thumbnailRequest->abandon(); + if (callback) + callbacks.emplace_back(move(callback)); + _thumbnailRequest = connection->getThumbnail(_url, size); + QObject::connect( _thumbnailRequest, &MediaThumbnailJob::success, + _thumbnailRequest, [this] { + _fetched = true; + _originalImage = + _thumbnailRequest->scaledThumbnail(_requestedSize); + _originalImage.save(_localFile.toLocalFile()); + _scaledImages.clear(); + for (const auto& n: callbacks) + n(); + callbacks.clear(); + }); + } } for (const auto& p: _scaledImages) @@ -178,6 +191,7 @@ bool Avatar::updateUrl(const QUrl& newUrl) return false; d->_url = newUrl; + d->_localFile = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + newUrl.authority() + "_" + newUrl.fileName() + ".png"); d->_fetched = false; if (isJobRunning(d->_thumbnailRequest)) d->_thumbnailRequest->abandon(); |