From 716c598f4aa9182b783134b1c3f5da60903c9a8c Mon Sep 17 00:00:00 2001 From: Loren Burkholder Date: Tue, 2 Mar 2021 19:43:25 -0500 Subject: [PATCH] Simplify macOS checks for a null image --- src/notifications/Manager.h | 2 ++ src/notifications/ManagerMac.cpp | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/notifications/Manager.h b/src/notifications/Manager.h index d7820b18..825da4c8 100644 --- a/src/notifications/Manager.h +++ b/src/notifications/Manager.h @@ -80,6 +80,8 @@ private: const QString &subtitle, const QString &informativeText, const QImage *bodyImage); + + QImage *getImgOrNullptr(const QString &path); #endif #if defined(Q_OS_WINDOWS) diff --git a/src/notifications/ManagerMac.cpp b/src/notifications/ManagerMac.cpp index 7e3ad309..0819272b 100644 --- a/src/notifications/ManagerMac.cpp +++ b/src/notifications/ManagerMac.cpp @@ -41,7 +41,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if QImage *image = nullptr; if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Image) - image = new QImage{cacheImage(notification.event)}; + image = getImgOrNullptr(cacheImage(notification.event)); const auto isEncrypted = std::get_if>( @@ -58,6 +58,17 @@ NotificationsManager::postNotification(const mtx::responses::Notification ¬if const QString messageInfo = (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender); objCxxPostNotification( - room_name, messageInfo, formatNotification(notification), (image != nullptr && !image->isNull()) ? image : nullptr); + room_name, messageInfo, formatNotification(notification), image); } } + +QImage * +NotificationsManager::getImgOrNullptr(const QString &path) +{ + auto img = new QImage{path}; + if (img->isNull()) { + delete img; + return nullptr; + } + return img; +}