From db2aa7000a264e08465a54cc3d88e4a1871aef94 Mon Sep 17 00:00:00 2001
From: Ilya Fedin <fedin-ilja2010@ya.ru>
Date: Thu, 20 Feb 2020 05:13:15 +0400
Subject: [PATCH] Fallback to non-panel icon when tray counter is disabled

---
 .../platform/linux/main_window_linux.cpp      | 41 ++++++++++++-------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp
index 3d8681a40..fe5e75355 100644
--- a/Telegram/SourceFiles/platform/linux/main_window_linux.cpp
+++ b/Telegram/SourceFiles/platform/linux/main_window_linux.cpp
@@ -44,7 +44,7 @@ int32 _trayIconCount = 0;
 QImage _trayIconImageBack, _trayIconImage;
 QString _trayIconThemeName, _trayIconName;
 
-QString GetTrayIconName() {
+QString GetPanelIconName() {
 	const auto counter = Core::App().unreadBadge();
 	const auto muted = Core::App().unreadBadgeMuted();
 
@@ -55,6 +55,24 @@ QString GetTrayIconName() {
 		: kPanelTrayIconName.utf16();
 }
 
+QString GetTrayIconName() {
+	const auto panelIconName = GetPanelIconName();
+
+	if (QIcon::hasThemeIcon(panelIconName)) {
+		return panelIconName;
+	} else if (InSandbox()) {
+		const auto launcherBasename = GetLauncherBasename();
+
+		if (QIcon::hasThemeIcon(launcherBasename)) {
+			return launcherBasename;
+		}
+	} else if (QIcon::hasThemeIcon(kTrayIconName.utf16())) {
+		return kTrayIconName.utf16();
+	}
+
+	return QString();
+}
+
 QImage TrayIconImageGen() {
 	const auto counter = Core::App().unreadBadge();
 	const auto muted = Core::App().unreadBadgeMuted();
@@ -76,16 +94,8 @@ QImage TrayIconImageGen() {
 			|| _trayIconImageBack.size() != desiredSize
 			|| iconThemeName != _trayIconThemeName
 			|| iconName != _trayIconName) {
-			const auto hasPanelIcon = QIcon::hasThemeIcon(iconName);
-
-			if (hasPanelIcon || QIcon::hasThemeIcon(kTrayIconName.utf16())) {
-				QIcon systemIcon;
-
-				if (hasPanelIcon) {
-					systemIcon = QIcon::fromTheme(iconName);
-				} else {
-					systemIcon = QIcon::fromTheme(kTrayIconName.utf16());
-				}
+			if (!iconName.isEmpty()) {
+				const auto systemIcon = QIcon::fromTheme(iconName);
 
 				if (systemIcon.actualSize(desiredSize) == desiredSize) {
 					_trayIconImageBack = systemIcon
@@ -118,7 +128,8 @@ QImage TrayIconImageGen() {
 		_trayIconThemeName = iconThemeName;
 		_trayIconName = iconName;
 
-		if (counter > 0) {
+		if (!qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())
+			&& counter > 0) {
 			QPainter p(&_trayIconImage);
 			int32 layerSize = -16;
 
@@ -270,8 +281,10 @@ void MainWindow::psTrayMenuUpdated() {
 #ifndef TDESKTOP_DISABLE_DBUS_INTEGRATION
 void MainWindow::setSNITrayIcon(
 		const QIcon &icon, const QImage &iconImage) {
-	if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())) {
-		const auto iconName = GetTrayIconName();
+	const auto iconName = GetTrayIconName();
+
+	if (qEnvironmentVariableIsSet(kDisableTrayCounter.utf8())
+		&& !iconName.isEmpty()) {
 		_sniTrayIcon->setIconByName(iconName);
 		_sniTrayIcon->setToolTipIconByName(iconName);
 	} else if (NeedTrayIconFile()) {