From 7d9f6096fec773b9808b345d8c29b535bc6a8028 Mon Sep 17 00:00:00 2001 From: Till Smejkal Date: Sun, 5 Jul 2020 13:10:23 +0200 Subject: [PATCH] Respect XDG_DATA_DIRS when looking for icons. Use the base folders as defined in $XDG_DATA_DIRS and only fall back to /usr/share and /usr/local/share if the environment variable does not exist. --- src/modules/wlr/taskbar.cpp | 50 +++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/modules/wlr/taskbar.cpp b/src/modules/wlr/taskbar.cpp index f669fa90..8d28438d 100644 --- a/src/modules/wlr/taskbar.cpp +++ b/src/modules/wlr/taskbar.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -42,19 +43,43 @@ static std::string trim(const std::string& s) /* Icon loading functions */ +static std::vector search_prefix() +{ + std::vector prefixes = {""}; + + auto xdg_data_dirs = std::getenv("XDG_DATA_DIRS"); + if (!xdg_data_dirs) { + prefixes.push_back("/usr/share/"); + prefixes.push_back("/usr/local/share/"); + } else { + std::string xdg_data_dirs_str(xdg_data_dirs); + size_t start = 0, end = 0; + + do { + end = xdg_data_dirs_str.find(':', start); + auto p = xdg_data_dirs_str.substr(start, end-start); + prefixes.push_back(trim(p) + "/"); + + start = end == std::string::npos ? end : end + 1; + } while(end != std::string::npos); + } + + for (auto& p : prefixes) + spdlog::debug("Using 'desktop' search path prefix: {}", p); + + return prefixes; +} /* Method 1 - get the correct icon name from the desktop file */ static std::string get_from_desktop_app_info(const std::string &app_id) { - Glib::RefPtr app_info; + static std::vector prefixes = search_prefix(); - std::vector prefixes = { + std::vector app_folders = { "", - "/usr/share/applications/", - "/usr/share/applications/kde/", - "/usr/share/applications/org.kde.", - "/usr/local/share/applications/", - "/usr/local/share/applications/org.kde.", + "applications/", + "applications/kde/", + "applications/org.kde." }; std::string lower_app_id = app_id; @@ -72,11 +97,14 @@ static std::string get_from_desktop_app_info(const std::string &app_id) ".desktop" }; + Glib::RefPtr app_info; + for (auto& prefix : prefixes) - for (auto& id : app_id_variations) - for (auto& suffix : suffixes) - if (!app_info) - app_info = Gio::DesktopAppInfo::create_from_filename(prefix + id + suffix); + for (auto& folder : app_folders) + for (auto& id : app_id_variations) + for (auto& suffix : suffixes) + if (!app_info) + app_info = Gio::DesktopAppInfo::create_from_filename(prefix + folder + id + suffix); if (app_info) return app_info->get_icon()->to_string();