diff options
-rw-r--r-- | avatar.cpp | 44 | ||||
-rw-r--r-- | avatar.h | 4 | ||||
-rw-r--r-- | connection.cpp | 2 | ||||
-rw-r--r-- | jobs/basejob.cpp | 2 | ||||
-rw-r--r-- | jobs/basejob.h | 6 | ||||
-rw-r--r-- | room.cpp | 1 |
6 files changed, 31 insertions, 28 deletions
@@ -23,6 +23,7 @@ #include "connection.h" #include <QtGui/QPainter> +#include <QtCore/QPointer> using namespace QMatrixClient; @@ -30,19 +31,19 @@ class Avatar::Private { public: Private(Connection* c, QIcon di) : _connection(c), _defaultIcon(di) { } - QImage get(QSize size, Avatar::notifier_t notifier); + QImage get(QSize size, Avatar::notifier_t notifier) const; Connection* _connection; const QIcon _defaultIcon; QUrl _url; - QImage _originalImage; - std::vector<QPair<QSize, QImage>> _scaledImages; - - QSize _requestedSize; - bool _valid = false; - MediaThumbnailJob* _ongoingRequest = nullptr; - std::vector<notifier_t> notifiers; + // The below are related to image caching, hence mutable + mutable QImage _originalImage; + mutable std::vector<QPair<QSize, QImage>> _scaledImages; + mutable QSize _requestedSize; + mutable bool _valid = false; + mutable QPointer<MediaThumbnailJob> _ongoingRequest = nullptr; + mutable std::vector<notifier_t> notifiers; }; Avatar::Avatar(Connection* connection, QIcon defaultIcon) @@ -51,17 +52,17 @@ Avatar::Avatar(Connection* connection, QIcon defaultIcon) Avatar::~Avatar() = default; -QImage Avatar::get(int dimension, Avatar::notifier_t notifier) +QImage Avatar::get(int dimension, notifier_t notifier) const { return d->get({dimension, dimension}, notifier); } -QImage Avatar::get(int width, int height, Avatar::notifier_t notifier) +QImage Avatar::get(int width, int height, notifier_t notifier) const { return d->get({width, height}, notifier); } -QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) +QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) const { // FIXME: Alternating between longer-width and longer-height requests // is a sure way to trick the below code into constantly getting another @@ -73,22 +74,17 @@ QImage Avatar::Private::get(QSize size, Avatar::notifier_t notifier) { qCDebug(MAIN) << "Getting avatar from" << _url.toString(); _requestedSize = size; - if (_ongoingRequest) + if (isJobRunning(_ongoingRequest)) _ongoingRequest->abandon(); notifiers.emplace_back(std::move(notifier)); _ongoingRequest = _connection->getThumbnail(_url, size); - _ongoingRequest->connect( _ongoingRequest, &MediaThumbnailJob::finished, - _connection, [=]() { - if (_ongoingRequest->status().good()) - { - _valid = true; - _originalImage = - _ongoingRequest->scaledThumbnail(_requestedSize); - _scaledImages.clear(); - for (auto n: notifiers) - n(); - } - _ongoingRequest = nullptr; + QObject::connect( _ongoingRequest, &MediaThumbnailJob::success, [this] + { + _valid = true; + _originalImage = _ongoingRequest->scaledThumbnail(_requestedSize); + _scaledImages.clear(); + for (auto n: notifiers) + n(); }); } @@ -35,8 +35,8 @@ namespace QMatrixClient using notifier_t = std::function<void()>; - QImage get(int dimension, notifier_t notifier); - QImage get(int w, int h, notifier_t notifier); + QImage get(int dimension, notifier_t notifier) const; + QImage get(int w, int h, notifier_t notifier) const; QUrl url() const; bool updateUrl(const QUrl& newUrl); diff --git a/connection.cpp b/connection.cpp index 36da838f..85d310cc 100644 --- a/connection.cpp +++ b/connection.cpp @@ -123,7 +123,7 @@ void Connection::resolveServer(const QString& mxidOrDomain) dns->setType(QDnsLookup::SRV); dns->setName("_matrix._tcp." + domain); - connect(dns, &QDnsLookup::finished, [this,dns,&maybeBaseUrl]() { + connect(dns, &QDnsLookup::finished, [this,dns,maybeBaseUrl]() { QUrl baseUrl { maybeBaseUrl }; if (dns->error() == QDnsLookup::NoError && dns->serviceRecords().isEmpty()) diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 2a28f11e..1f079966 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -70,7 +70,7 @@ class BaseJob::Private QByteArrayList expectedContentTypes; QScopedPointer<QNetworkReply, NetworkReplyDeleter> reply; - Status status = NoError; + Status status = Pending; QTimer timer; QTimer retryTimer; diff --git a/jobs/basejob.h b/jobs/basejob.h index daca1e79..1fe3a24d 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -50,6 +50,7 @@ namespace QMatrixClient * (which BaseJob used to inherit from). */ enum StatusCode { NoError = 0 // To be compatible with Qt conventions , Success = 0 + , Pending = 1 , ErrorLevel = 100 // Errors have codes starting from this , NetworkError = 100 , JsonParseError @@ -291,4 +292,9 @@ namespace QMatrixClient class Private; QScopedPointer<Private> d; }; + + inline bool isJobRunning(BaseJob* job) + { + return job && job->error() == BaseJob::Pending; + } } // namespace QMatrixClient @@ -42,6 +42,7 @@ #include <QtCore/QElapsedTimer> #include <array> +#include <functional> using namespace QMatrixClient; using namespace std::placeholders; |