diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2021-03-17 19:08:17 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2021-03-17 19:18:07 +0100 |
commit | e5d75c814b2175dc37beabff3b0421de59a3e93e (patch) | |
tree | 97670bfa02e365ef527ad18fad3d66e16c3ef8d2 /src/AvatarProvider.cpp | |
parent | 95026dcc62d4ba0ea0707c75bbc54f1ac01fb954 (diff) |
Clean up notification code a bit
Diffstat (limited to 'src/AvatarProvider.cpp')
-rw-r--r-- | src/AvatarProvider.cpp | 65 |
1 files changed, 27 insertions, 38 deletions
diff --git a/src/AvatarProvider.cpp b/src/AvatarProvider.cpp index f64f6859..8cc1144f 100644 --- a/src/AvatarProvider.cpp +++ b/src/AvatarProvider.cpp @@ -12,13 +12,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 +34,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 +69,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); } } |