Skip to content

Commit f9a73f7

Browse files
committed
Improved gtk-utils get_desktop_app_info().
* Also search lower case appid basename. * If appid is divided by dashes search the first component which works for blueman-applet and others. * If appid is divided by dots and nothing was found take each component and try to find a matching desktop file, works for applications that report appid as appname.arch/ext and the desktop file is appname.desktop * Added workaround for visual studio code. * Check if DesktopAppInfo has the 'Icon' key to prevent crash.
1 parent 7cfb188 commit f9a73f7

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

src/util/gtk-utils.cpp

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,15 @@ Glib::RefPtr<Gio::DesktopAppInfo> get_desktop_app_info(std::string app_id)
145145
app_id.rfind(".")+1, app_id.size()
146146
);
147147

148+
std::string app_id_basename_lowercase = app_id;
149+
for (auto& c : app_id_basename_lowercase)
150+
c = std::tolower(c);
151+
148152
std::vector<std::string> app_id_variations = {
149153
app_id,
150154
app_id_lowercase,
151-
app_id_basename
155+
app_id_basename,
156+
app_id_basename_lowercase
152157
};
153158

154159
std::vector<std::string> suffixes = {
@@ -171,27 +176,65 @@ Glib::RefPtr<Gio::DesktopAppInfo> get_desktop_app_info(std::string app_id)
171176
}
172177
}
173178

174-
// As last resort perform a search and select best match
179+
// Perform a search and select best match
175180
if (!app_info)
176181
{
182+
std::vector<std::string> app_id_variations;
183+
184+
if (app_id == "Code")
185+
app_id_variations.push_back("visual-studio-code");
186+
187+
app_id_variations.push_back(app_id);
188+
189+
// If appid has dashes add first component to search
190+
if (app_id.find('-') != std::string::npos)
191+
{
192+
std::istringstream stream(app_id);
193+
std::string token;
194+
std::getline(stream, token, '-');
195+
196+
app_id_variations.push_back(token);
197+
}
198+
177199
std::string desktop_file = "";
178200

179-
gchar*** desktop_list = g_desktop_app_info_search(app_id.c_str());
180-
if (desktop_list != nullptr && desktop_list[0] != nullptr)
201+
for (auto token : app_id_variations)
181202
{
182-
for (size_t i=0; desktop_list[0][i]; i++)
203+
gchar*** desktop_list = g_desktop_app_info_search(token.c_str());
204+
if (desktop_list != nullptr && desktop_list[0] != nullptr)
183205
{
184-
if(desktop_file == "")
185-
desktop_file = desktop_list[0][i];
206+
for (size_t i=0; desktop_list[0][i]; i++)
207+
{
208+
if(desktop_file == "")
209+
desktop_file = desktop_list[0][i];
186210

211+
break;
212+
}
213+
g_strfreev(desktop_list[0]);
214+
}
215+
g_free(desktop_list);
216+
217+
if(desktop_file != "")
218+
{
219+
app_info = Gio::DesktopAppInfo::create(desktop_file);
187220
break;
188221
}
189-
g_strfreev(desktop_list[0]);
190222
}
191-
g_free(desktop_list);
223+
}
192224

193-
if(desktop_file != "")
194-
app_info = Gio::DesktopAppInfo::create(desktop_file);
225+
// If app has dots try each component
226+
if (!app_info && app_id.find('.') != std::string::npos)
227+
{
228+
std::istringstream stream(app_id);
229+
std::string token;
230+
231+
while (std::getline(stream, token, '.'))
232+
{
233+
app_info = Gio::DesktopAppInfo::create(token + ".desktop");
234+
235+
if (app_info)
236+
break;
237+
}
195238
}
196239

197240
if (app_info)
@@ -221,7 +264,7 @@ bool set_image_from_icon(Gtk::Image& image,
221264
// Try to load icon from the DesktopAppInfo
222265
auto app_info = get_desktop_app_info(app_id);
223266

224-
if (app_info)
267+
if (app_info && app_info->has_key("Icon"))
225268
icon_name = app_info->get_icon()->to_string();
226269

227270
// Try directly looking up the icon, if it exists

0 commit comments

Comments
 (0)