aboutsummaryrefslogtreecommitdiff
path: root/lib/avatar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/avatar.cpp')
-rw-r--r--lib/avatar.cpp48
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();