diff options
author | Black Hat <bhat@encom.eu.org> | 2019-09-26 22:22:36 -0700 |
---|---|---|
committer | Black Hat <bhat@encom.eu.org> | 2019-09-26 22:22:36 -0700 |
commit | 363cf452bcdbaf6ff1cf94a83ca66cbb31122346 (patch) | |
tree | c64c8fda885e4e1785130e8ee3e7c47fd18cbf67 /lib/avatar.cpp | |
parent | 412e2cf19449e73aa7da729e9c5de6502687aade (diff) | |
parent | 944653463fe4134c82d85e2d01e2bc0fa43fd727 (diff) | |
download | libquotient-363cf452bcdbaf6ff1cf94a83ca66cbb31122346.tar.gz libquotient-363cf452bcdbaf6ff1cf94a83ca66cbb31122346.zip |
Merge branch 'master' of https://github.com/quotient-im/libQuotient into
bhat-libqtolm-update
Diffstat (limited to 'lib/avatar.cpp')
-rw-r--r-- | lib/avatar.cpp | 140 |
1 files changed, 66 insertions, 74 deletions
diff --git a/lib/avatar.cpp b/lib/avatar.cpp index 0d849eae..cb734984 100644 --- a/lib/avatar.cpp +++ b/lib/avatar.cpp @@ -13,64 +13,58 @@ * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "avatar.h" -#include "jobs/mediathumbnailjob.h" -#include "events/eventcontent.h" #include "connection.h" -#include <QtGui/QPainter> -#include <QtCore/QPointer> +#include "events/eventcontent.h" +#include "jobs/mediathumbnailjob.h" + #include <QtCore/QDir> +#include <QtCore/QPointer> #include <QtCore/QStandardPaths> #include <QtCore/QStringBuilder> +#include <QtGui/QPainter> -using namespace QMatrixClient; +using namespace Quotient; using std::move; -class Avatar::Private -{ - public: - explicit Private(QUrl url = {}) - : _url(move(url)) - { } - ~Private() - { - if (isJobRunning(_thumbnailRequest)) - _thumbnailRequest->abandon(); - if (isJobRunning(_uploadRequest)) - _uploadRequest->abandon(); - } - - QImage get(Connection* connection, QSize size, - get_callback_t callback) const; - bool upload(UploadContentJob* job, upload_callback_t callback); - - bool checkUrl(const QUrl& url) const; - QString localFile() const; - - QUrl _url; - - // The below are related to image caching, hence mutable - mutable QImage _originalImage; - mutable std::vector<QPair<QSize, QImage>> _scaledImages; - mutable QSize _requestedSize; - mutable enum { Unknown, Cache, Network, Banned } _imageSource = Unknown; - mutable QPointer<MediaThumbnailJob> _thumbnailRequest = nullptr; - mutable QPointer<BaseJob> _uploadRequest = nullptr; - mutable std::vector<get_callback_t> callbacks; +class Avatar::Private { +public: + explicit Private(QUrl url = {}) : _url(move(url)) {} + ~Private() + { + if (isJobRunning(_thumbnailRequest)) + _thumbnailRequest->abandon(); + if (isJobRunning(_uploadRequest)) + _uploadRequest->abandon(); + } + + QImage get(Connection* connection, QSize size, + get_callback_t callback) const; + bool upload(UploadContentJob* job, upload_callback_t callback); + + bool checkUrl(const QUrl& url) const; + QString localFile() const; + + QUrl _url; + + // The below are related to image caching, hence mutable + mutable QImage _originalImage; + mutable std::vector<QPair<QSize, QImage>> _scaledImages; + mutable QSize _requestedSize; + mutable enum { Unknown, Cache, Network, Banned } _imageSource = Unknown; + mutable QPointer<MediaThumbnailJob> _thumbnailRequest = nullptr; + mutable QPointer<BaseJob> _uploadRequest = nullptr; + mutable std::vector<get_callback_t> callbacks; }; -Avatar::Avatar() - : d(std::make_unique<Private>()) -{ } +Avatar::Avatar() : d(std::make_unique<Private>()) {} -Avatar::Avatar(QUrl url) - : d(std::make_unique<Private>(std::move(url))) -{ } +Avatar::Avatar(QUrl url) : d(std::make_unique<Private>(std::move(url))) {} Avatar::Avatar(Avatar&&) = default; @@ -81,13 +75,13 @@ Avatar& Avatar::operator=(Avatar&&) = default; QImage Avatar::get(Connection* connection, int dimension, get_callback_t callback) const { - return d->get(connection, {dimension, dimension}, move(callback)); + return d->get(connection, { dimension, dimension }, move(callback)); } QImage Avatar::get(Connection* connection, int width, int height, get_callback_t callback) const { - return d->get(connection, {width, height}, move(callback)); + return d->get(connection, { width, height }, move(callback)); } bool Avatar::upload(Connection* connection, const QString& fileName, @@ -106,22 +100,17 @@ bool Avatar::upload(Connection* connection, QIODevice* source, return d->upload(connection->uploadContent(source), move(callback)); } -QString Avatar::mediaId() const -{ - return d->_url.authority() + d->_url.path(); -} +QString Avatar::mediaId() const { return d->_url.authority() + d->_url.path(); } QImage Avatar::Private::get(Connection* connection, QSize size, get_callback_t callback) const { - if (!callback) - { + if (!callback) { qCCritical(MAIN) << "Null callbacks are not allowed in Avatar::get"; Q_ASSERT(false); } - if (_imageSource == Unknown && _originalImage.load(localFile())) - { + if (_imageSource == Unknown && _originalImage.load(localFile())) { _imageSource = Cache; _requestedSize = _originalImage.size(); } @@ -130,9 +119,10 @@ QImage Avatar::Private::get(Connection* connection, QSize size, // to trick the below code into constantly getting another image from // the server because the existing one is alleged unsatisfactory. // Client authors can only blame themselves if they do so. - if (((_imageSource == Unknown && !_thumbnailRequest) || - size.width() > _requestedSize.width() || - size.height() > _requestedSize.height()) && checkUrl(_url)) { + if (((_imageSource == Unknown && !_thumbnailRequest) + || size.width() > _requestedSize.width() + || size.height() > _requestedSize.height()) + && checkUrl(_url)) { qCDebug(MAIN) << "Getting avatar from" << _url.toString(); _requestedSize = size; if (isJobRunning(_thumbnailRequest)) @@ -140,24 +130,26 @@ QImage Avatar::Private::get(Connection* connection, QSize size, if (callback) callbacks.emplace_back(move(callback)); _thumbnailRequest = connection->getThumbnail(_url, size); - QObject::connect( _thumbnailRequest, &MediaThumbnailJob::success, - _thumbnailRequest, [this] { - _imageSource = Network; - _originalImage = - _thumbnailRequest->scaledThumbnail(_requestedSize); - _originalImage.save(localFile()); - _scaledImages.clear(); - for (const auto& n: callbacks) - n(); - callbacks.clear(); - }); + QObject::connect(_thumbnailRequest, &MediaThumbnailJob::success, + _thumbnailRequest, [this] { + _imageSource = Network; + _originalImage = _thumbnailRequest->scaledThumbnail( + _requestedSize); + _originalImage.save(localFile()); + _scaledImages.clear(); + for (const auto& n : callbacks) + n(); + callbacks.clear(); + }); } - for (const auto& p: _scaledImages) + for (const auto& p : _scaledImages) if (p.first == size) return p.second; - auto result = _originalImage.isNull() ? QImage() : _originalImage.scaled(size, - Qt::KeepAspectRatio, Qt::SmoothTransformation); + auto result = _originalImage.isNull() + ? QImage() + : _originalImage.scaled(size, Qt::KeepAspectRatio, + Qt::SmoothTransformation); _scaledImages.emplace_back(size, result); return result; } @@ -168,7 +160,7 @@ bool Avatar::Private::upload(UploadContentJob* job, upload_callback_t callback) if (!isJobRunning(_uploadRequest)) return false; _uploadRequest->connect(_uploadRequest, &BaseJob::success, _uploadRequest, - [job,callback] { callback(job->contentUri()); }); + [job, callback] { callback(job->contentUri()); }); return true; } @@ -179,8 +171,7 @@ bool Avatar::Private::checkUrl(const QUrl& url) const // FIXME: Make "mxc" a library-wide constant and maybe even make // the URL checker a Connection(?) method. - if (!url.isValid() || url.scheme() != "mxc" || url.path().count('/') != 1) - { + if (!url.isValid() || url.scheme() != "mxc" || url.path().count('/') != 1) { qCWarning(MAIN) << "Avatar URL is invalid or not mxc-based:" << url.toDisplayString(); _imageSource = Banned; @@ -188,7 +179,8 @@ bool Avatar::Private::checkUrl(const QUrl& url) const return _imageSource != Banned; } -QString Avatar::Private::localFile() const { +QString Avatar::Private::localFile() const +{ static const auto cachePath = cacheLocation(QStringLiteral("avatars")); return cachePath % _url.authority() % '_' % _url.fileName() % ".png"; } |