From 6c11b2400a3e4027ad7bf3fb19c69a4587b0d2ed Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Tue, 14 May 2024 14:49:59 +0200 Subject: [PATCH] Add onError callback to load pixmap code --- src/util/LoadPixmap.cpp | 55 ++++++++++++++++++++++++++--------------- src/util/LoadPixmap.hpp | 7 ++++-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/util/LoadPixmap.cpp b/src/util/LoadPixmap.cpp index 99fdf95f3..5fb045a5f 100644 --- a/src/util/LoadPixmap.cpp +++ b/src/util/LoadPixmap.cpp @@ -11,37 +11,52 @@ namespace chatterino { -void loadPixmapFromUrl(const Url &url, std::function &&callback) +void loadPixmapFromUrl(const Url &url, std::function &&onSuccess, + const std::optional> &onError) { NetworkRequest(url.string) .concurrent() .cache() - .onSuccess( - [callback = std::move(callback), url](const NetworkResult &result) { - auto data = result.getData(); - QBuffer buffer(&data); - buffer.open(QIODevice::ReadOnly); - QImageReader reader(&buffer); + .onError([onError](const NetworkResult & /*result*/) { + if (onError.has_value()) + { + (*onError)(); + } + }) + .onSuccess([callback = std::move(onSuccess), onError, + url](const NetworkResult &result) { + auto data = result.getData(); + QBuffer buffer(&data); + buffer.open(QIODevice::ReadOnly); + QImageReader reader(&buffer); - if (!reader.canRead() || reader.size().isEmpty()) + if (!reader.canRead() || reader.size().isEmpty()) + { + qCWarning(chatterinoImage) + << "Can't read image file at" << url.string << ":" + << reader.errorString(); + if (onError.has_value()) { - qCWarning(chatterinoImage) - << "Can't read image file at" << url.string << ":" - << reader.errorString(); - return; + (*onError)(); } + return; + } - QImage image = reader.read(); - if (image.isNull()) + QImage image = reader.read(); + if (image.isNull()) + { + qCWarning(chatterinoImage) + << "Failed reading image at" << url.string << ":" + << reader.errorString(); + if (onError.has_value()) { - qCWarning(chatterinoImage) - << "Failed reading image at" << url.string << ":" - << reader.errorString(); - return; + (*onError)(); } + return; + } - callback(QPixmap::fromImage(image)); - }) + callback(QPixmap::fromImage(image)); + }) .execute(); } diff --git a/src/util/LoadPixmap.hpp b/src/util/LoadPixmap.hpp index 81fb11921..1be03f1f6 100644 --- a/src/util/LoadPixmap.hpp +++ b/src/util/LoadPixmap.hpp @@ -8,8 +8,11 @@ namespace chatterino { /** * Loads an image from url into a QPixmap. Allows for file:// protocol links. Uses cacheing. * - * @param callback The callback you will get the pixmap by. It will be invoked concurrently with no guarantees on which thread. + * @param onSuccess The callback you will get the pixmap by. It will be invoked concurrently with no guarantees on which thread. + * @param onError The callback that will be called if the request fails or the image cannot be loaded. */ -void loadPixmapFromUrl(const Url &url, std::function &&callback); +void loadPixmapFromUrl( + const Url &url, std::function &&onSuccess, + const std::optional> &onError = {}); } // namespace chatterino