summary refs log tree commit diff
path: root/src/AvatarProvider.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/AvatarProvider.cpp')
-rw-r--r--src/AvatarProvider.cpp66
1 files changed, 28 insertions, 38 deletions
diff --git a/src/AvatarProvider.cpp b/src/AvatarProvider.cpp

index 1834e040..b9962cef 100644 --- a/src/AvatarProvider.cpp +++ b/src/AvatarProvider.cpp
@@ -5,6 +5,7 @@ #include <QBuffer> #include <QPixmapCache> +#include <QPointer> #include <memory> #include <unordered_map> @@ -12,13 +13,14 @@ #include "Cache.h" #include "Logging.h" #include "MatrixClient.h" +#include "MxcImageProvider.h" #include "Utils.h" static QPixmapCache avatar_cache; namespace AvatarProvider { void -resolve(const QString &avatarUrl, int size, QObject *receiver, AvatarCallback callback) +resolve(QString avatarUrl, int size, QObject *receiver, AvatarCallback callback) { const auto cacheKey = QString("%1_size_%2").arg(avatarUrl).arg(size); @@ -33,44 +35,32 @@ resolve(const QString &avatarUrl, int size, QObject *receiver, AvatarCallback ca return; } - auto data = cache::image(cacheKey); - if (!data.isNull()) { - pixmap = QPixmap::fromImage(utils::readImage(&data)); - avatar_cache.insert(cacheKey, pixmap); - callback(pixmap); - return; - } - - auto proxy = std::make_shared<AvatarProxy>(); - QObject::connect(proxy.get(), - &AvatarProxy::avatarDownloaded, - receiver, - [callback, cacheKey](QByteArray data) { - QPixmap pm = QPixmap::fromImage(utils::readImage(&data)); - avatar_cache.insert(cacheKey, pm); - callback(pm); - }); + MxcImageProvider::download(avatarUrl.remove(QStringLiteral("mxc://")), + QSize(size, size), + [callback, cacheKey, recv = QPointer<QObject>(receiver)]( + QString, QSize, QImage img, QString) { + if (!recv) + return; - mtx::http::ThumbOpts opts; - opts.width = size; - opts.height = size; - opts.mxc_url = avatarUrl.toStdString(); + auto proxy = std::make_shared<AvatarProxy>(); + QObject::connect(proxy.get(), + &AvatarProxy::avatarDownloaded, + recv, + [callback, cacheKey](QPixmap pm) { + if (!pm.isNull()) + avatar_cache.insert( + cacheKey, pm); + callback(pm); + }); - http::client()->get_thumbnail( - opts, - [opts, cacheKey, proxy = std::move(proxy)](const std::string &res, - mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn("failed to download avatar: {} - ({} {})", - opts.mxc_url, - mtx::errors::to_string(err->matrix_error.errcode), - err->matrix_error.error); - } else { - cache::saveImage(cacheKey.toStdString(), res); - } + if (img.isNull()) { + emit proxy->avatarDownloaded(QPixmap{}); + return; + } - emit proxy->avatarDownloaded(QByteArray(res.data(), (int)res.size())); - }); + auto pm = QPixmap::fromImage(std::move(img)); + emit proxy->avatarDownloaded(pm); + }); } void @@ -80,8 +70,8 @@ resolve(const QString &room_id, QObject *receiver, AvatarCallback callback) { - const auto avatarUrl = cache::avatarUrl(room_id, user_id); + auto avatarUrl = cache::avatarUrl(room_id, user_id); - resolve(avatarUrl, size, receiver, callback); + resolve(std::move(avatarUrl), size, receiver, callback); } }