From 2bda3c4e2bdf7090544ca13d88cb2c3d7f1ac003 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Sat, 26 Nov 2022 21:59:50 +0400 Subject: [PATCH] Don't save temp file for native notification userpics on Linux --- .../linux/notifications_manager_linux.cpp | 74 ++++++++----------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 84cd55b2cb..569085613c 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "platform/linux/notifications_manager_linux.h" -#include "window/notifications_utilities.h" #include "base/options.h" #include "base/platform/base_platform_info.h" #include "base/platform/linux/base_linux_glibmm_helper.h" @@ -20,10 +19,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_forum_topic.h" #include "history/history.h" #include "history/history_item.h" +#include "ui/empty_userpic.h" #include "main/main_session.h" #include "lang/lang_keys.h" #include "base/weak_ptr.h" +#include "styles/style_window.h" +#include #include #include @@ -335,7 +337,7 @@ public: void show(); void close(); - void setImage(const QString &imagePath); + void setImage(const QImage &image); private: const not_null _manager; @@ -685,24 +687,16 @@ void NotificationData::close() { _manager->clearNotification(_id); } -void NotificationData::setImage(const QString &imagePath) { - if (imagePath.isEmpty()) { - return; - } - +void NotificationData::setImage(const QImage &image) { if (_notification) { const auto imageData = [&] { - QFile f(imagePath); - if (f.open(QIODevice::ReadOnly)) { - return f.readAll(); - } - return QByteArray(); + QByteArray ba; + QBuffer buffer(&ba); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer, "PNG"); + return ba; }(); - if (imageData.isEmpty()) { - return; - } - const auto imageBytes = Glib::Bytes::create( imageData.constData(), imageData.size()); @@ -717,27 +711,20 @@ void NotificationData::setImage(const QString &imagePath) { return; } - const auto image = [&] { - const auto original = QImage(imagePath); - return original.hasAlphaChannel() - ? original.convertToFormat(QImage::Format_RGBA8888) - : original.convertToFormat(QImage::Format_RGB888); - }(); - - if (image.isNull()) { - return; - } + const auto convertedImage = image.hasAlphaChannel() + ? image.convertToFormat(QImage::Format_RGBA8888) + : image.convertToFormat(QImage::Format_RGB888); _hints[_imageKey] = MakeGlibVariant(std::tuple{ - image.width(), - image.height(), - int(image.bytesPerLine()), - image.hasAlphaChannel(), + convertedImage.width(), + convertedImage.height(), + int(convertedImage.bytesPerLine()), + convertedImage.hasAlphaChannel(), 8, - image.hasAlphaChannel() ? 4 : 3, + convertedImage.hasAlphaChannel() ? 4 : 3, std::vector( - image.constBits(), - image.constBits() + image.sizeInBytes()), + convertedImage.constBits(), + convertedImage.constBits() + convertedImage.sizeInBytes()), }); } @@ -910,8 +897,7 @@ void Create(Window::Notifications::System *system) { class Manager::Private : public base::has_weak_ptr { public: - using Type = Window::Notifications::CachedUserpics::Type; - explicit Private(not_null manager, Type type); + explicit Private(not_null manager); void showNotification( not_null peer, @@ -942,17 +928,14 @@ private: ContextId, base::flat_map> _notifications; - Window::Notifications::CachedUserpics _cachedUserpics; - Glib::RefPtr _dbusConnection; bool _inhibited = false; uint _inhibitedSignalId = 0; }; -Manager::Private::Private(not_null manager, Type type) -: _manager(manager) -, _cachedUserpics(type) { +Manager::Private::Private(not_null manager) +: _manager(manager) { const auto serverInformation = CurrentServerInformation; const auto capabilities = CurrentCapabilities; @@ -1054,9 +1037,12 @@ void Manager::Private::showNotification( } if (!options.hideNameAndPhoto) { - const auto userpicKey = peer->userpicUniqueKey(userpicView); - notification->setImage( - _cachedUserpics.get(userpicKey, peer, userpicView)); + const auto userpic = peer->isSelf() + ? Ui::EmptyUserpic::GenerateSavedMessages(st::notifyMacPhotoSize) + : peer->isRepliesChat() + ? Ui::EmptyUserpic::GenerateRepliesMessages(st::notifyMacPhotoSize) + : peer->genUserpic(userpicView, st::notifyMacPhotoSize); + notification->setImage(userpic.toImage()); } auto i = _notifications.find(key); @@ -1181,7 +1167,7 @@ Manager::Private::~Private() { Manager::Manager(not_null system) : NativeManager(system) -, _private(std::make_unique(this, Private::Type::Rounded)) { +, _private(std::make_unique(this)) { } void Manager::clearNotification(NotificationId id) {