Skip to content

Commit 8248c2e

Browse files
ui: better app icon discovery
On some DEs like Xfce4 (Ubuntu 24) we were not displaying icons on the popups.
1 parent f077ac8 commit 8248c2e

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

ui/opensnitch/desktop_parser.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def __init__(self):
3636
'/usr/bin/pidgin.orig': '/usr/bin/pidgin'
3737
}
3838

39+
self.terminal_icon = ""
40+
3941
for desktop_path in DESKTOP_PATHS:
4042
if not os.path.exists(desktop_path):
4143
continue
@@ -97,26 +99,14 @@ def get_app_description(self, parser):
9799
@staticmethod
98100
def discover_app_icon(app_name):
99101
# more hacks
100-
# normally qt will find icons if the system if configured properly.
102+
# normally qt will find icons if the system is configured properly.
101103
# if it's not, qt won't be able to find the icon by using QIcon().fromTheme(""),
102104
# so we fallback to try to determine if the icon exist in some well known system paths.
103-
icon_dirs = (
104-
"/usr/share/icons/hicolor/scalable/apps/",
105-
"/usr/share/icons/gnome/48x48/apps/",
106-
"/usr/share/pixmaps/",
107-
"/usr/share/icons/hicolor/48x48/apps/",
108-
"/usr/share/icons/HighContrast/scalable/apps/",
109-
"/usr/share/icons/HighContrast/48x48/apps/"
110-
)
111-
icon_exts = (".svg", ".png", ".xpm")
112-
for idir in icon_dirs:
113-
for iext in icon_exts:
114-
iconPath = idir + app_name
115-
if iext not in app_name:
116-
iconPath = idir + app_name + iext
117-
118-
if os.path.exists(iconPath):
119-
return iconPath
105+
for idir in glob.glob('/usr/share/icons/*/*/apps/*'):
106+
if app_name in idir:
107+
return idir
108+
109+
return None
120110

121111
def _parse_desktop_file(self, desktop_path):
122112
parser = configparser.ConfigParser(strict=False) # Allow duplicate config entries
@@ -134,6 +124,8 @@ def _parse_desktop_file(self, desktop_path):
134124
icon = parser.get('Desktop Entry', 'Icon', raw=True, fallback=None)
135125
name = parser.get('Desktop Entry', 'Name', raw=True, fallback=None)
136126
desc = self.get_app_description(parser)
127+
if "terminal" in icon:
128+
self.terminal_icon = icon
137129

138130
if name == "flatpak":
139131
return
@@ -143,6 +135,8 @@ def _parse_desktop_file(self, desktop_path):
143135
# FIXME: even if we return an icon, if the DE is not properly configured,
144136
# it won't be loaded/displayed.
145137
icon = LinuxDesktopParser.discover_app_icon(basename)
138+
if icon == None:
139+
icon = self.terminal_icon
146140

147141
with self.lock:
148142
# The Exec entry may have an absolute path to a binary or just the binary with parameters.
@@ -173,12 +167,11 @@ def get_info_by_path(self, path, default_icon):
173167
return self.apps.get(def_name, (def_name, default_icon, "", None))
174168

175169
# last try to get a default terminal icon
176-
for def_icon in ("terminal", "utilities-terminal", "xterm", "gnome-terminal", "openterm", "xfce-terminal", "terminator"):
177-
test = self.apps.get(def_name, (def_name, def_icon, "", None))
178-
if test != None:
179-
return test
170+
icon = self.discover_app_icon(def_name)
171+
if icon == None:
172+
icon = self.terminal_icon
180173

181-
return self.apps.get(def_name, (def_name, default_icon, "", None))
174+
return self.apps.get(def_name, (def_name, icon, "", None))
182175

183176
def get_info_by_binname(self, name, default_icon):
184177
def_name = os.path.basename(name)

0 commit comments

Comments
 (0)