Commit 30c90d8e authored by Simon Steinbeiss's avatar Simon Steinbeiss Committed by Simon Steinbeiß
Browse files

daemon: Improve application icon matching

Also refactor the code so it doesn't get repeated all over the place.
parent f8885993
Pipeline #16524 passed with stages
in 1 minute and 18 seconds
......@@ -32,6 +32,8 @@
#include <libxfce4util/libxfce4util.h>
#include <gdk/gdkx.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <glib.h>
#include <gtk/gtk.h>
......@@ -90,36 +92,8 @@ notify_pixbuf_from_image_data (GVariant *image_data)
return pix;
}
gchar *
notify_icon_name_from_desktop_id (const gchar *desktop_id)
{
gchar *icon_file = NULL;
gchar *resource;
XfceRc *rcfile;
resource = g_strdup_printf("applications%c%s.desktop",
G_DIR_SEPARATOR,
desktop_id);
rcfile = xfce_rc_config_open(XFCE_RESOURCE_DATA,
resource, TRUE);
g_free (resource);
if (rcfile)
{
if (xfce_rc_has_group (rcfile, G_KEY_FILE_DESKTOP_GROUP))
{
xfce_rc_set_group (rcfile, G_KEY_FILE_DESKTOP_GROUP);
icon_file = g_strdup (xfce_rc_read_entry (rcfile,
G_KEY_FILE_DESKTOP_KEY_ICON,
NULL));
/* At this point: icon_file might be NULL */
}
xfce_rc_close (rcfile);
}
return icon_file;
}
gchar *
notify_get_from_desktop_file (const gchar *desktop_file_path, const gchar *key)
static gchar *
notify_read_from_desktop_file (const gchar *desktop_file_path, const gchar *key)
{
GKeyFile *desktop_file;
gchar *value = NULL;
......@@ -149,6 +123,44 @@ notify_get_from_desktop_file (const gchar *desktop_file_path, const gchar *key)
return value;
}
gchar *
notify_get_from_desktop_file (const gchar *desktop_file, const gchar *key)
{
GDesktopAppInfo *appinfo;
gchar *filename;
gchar *value = NULL;
filename = g_strdup_printf ("%s.desktop", desktop_file);
appinfo = g_desktop_app_info_new (filename);
g_free (filename);
if (appinfo) {
value = notify_read_from_desktop_file (g_desktop_app_info_get_filename (appinfo), key);
}
/* Fallback: Try to find the correct desktop file
As the GIO matching algorithm is unknown and subject to change we naively pick the first match */
else {
gchar ***matches;
matches = g_desktop_app_info_search (desktop_file);
if (matches[0]) {
gchar **match;
match = matches[0];
appinfo = g_desktop_app_info_new (match[0]);
value = notify_read_from_desktop_file (g_desktop_app_info_get_filename (appinfo), key);
for (gchar ***p = matches; *p != NULL; p++)
g_strfreev (*p);
g_free (matches);
}
}
return value;
}
GKeyFile *
xfce_notify_log_get (void)
{
......@@ -287,9 +299,12 @@ xfce_notify_log_keyfile_insert1 (GKeyFile *notify_log,
g_key_file_set_string (notify_log, group, "app_icon", app_icon);
}
else if (desktop_id) {
gchar *icon_name = notify_icon_name_from_desktop_id (desktop_id);
g_key_file_set_string (notify_log, group, "app_icon", icon_name);
g_free (icon_name);
gchar *icon_name = notify_get_from_desktop_file (desktop_id, G_KEY_FILE_DESKTOP_KEY_ICON);
if (icon_name) {
g_key_file_set_string (notify_log, group, "app_icon", icon_name);
g_free (icon_name);
}
}
timeout = g_strdup_printf ("%d", expire_timeout);
......
......@@ -26,9 +26,7 @@
GdkPixbuf *notify_pixbuf_from_image_data (GVariant *image_data);
gchar *notify_icon_name_from_desktop_id (const gchar *desktop_id);
gchar *notify_get_from_desktop_file (const gchar *desktop_file_path,
gchar *notify_get_from_desktop_file (const gchar *desktop_file,
const gchar *key);
GKeyFile *xfce_notify_log_get (void);
......
......@@ -456,11 +456,8 @@ xfce4_notifyd_known_application_insert_row (XfconfChannel *channel,
GtkWidget *icon;
GtkWidget *mute_switch;
guint i;
gchar *desktop_id;
GDesktopAppInfo *appinfo;
gchar *app_name = NULL;
gchar *desktop_icon_name = NULL;
gchar ***matches;
/* Pack all widgets into a box */
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
......@@ -475,9 +472,6 @@ xfce4_notifyd_known_application_insert_row (XfconfChannel *channel,
gtk_widget_set_halign (label, GTK_ALIGN_START);
#endif
desktop_id = g_strdup_printf ("%s.desktop", known_application);
appinfo = g_desktop_app_info_new (desktop_id);
g_free (desktop_id);
/* All applications that don't supply their name at all */
if (xfce_str_is_empty (known_application)) {
......@@ -489,39 +483,13 @@ xfce4_notifyd_known_application_insert_row (XfconfChannel *channel,
gtk_label_set_markup (GTK_LABEL (label), markup);
g_free (markup);
}
/* Try to find the correct desktop file based on the application name */
else if (appinfo) {
desktop_icon_name = notify_get_from_desktop_file (g_desktop_app_info_get_filename (appinfo),
G_KEY_FILE_DESKTOP_KEY_ICON);
gtk_image_set_from_icon_name (GTK_IMAGE (icon), desktop_icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
app_name = notify_get_from_desktop_file (g_desktop_app_info_get_filename (appinfo),
G_KEY_FILE_DESKTOP_KEY_NAME);
gtk_label_set_text (GTK_LABEL (label), app_name);
}
/* Fallback: Try to find the correct desktop file
As the GIO matching algorithm is unknown and subject to change we naively pick the first match */
else {
matches = g_desktop_app_info_search (known_application);
if (matches[0]) {
gchar **match;
match = matches[0];
appinfo = g_desktop_app_info_new (match[0]);
desktop_icon_name = notify_get_from_desktop_file (g_desktop_app_info_get_filename (appinfo),
G_KEY_FILE_DESKTOP_KEY_ICON);
app_name = notify_get_from_desktop_file (g_desktop_app_info_get_filename (appinfo),
G_KEY_FILE_DESKTOP_KEY_NAME);
gtk_label_set_text (GTK_LABEL (label), app_name);
/* Try to find the correct icon based on the desktop file */
desktop_icon_name = notify_get_from_desktop_file (known_application, G_KEY_FILE_DESKTOP_KEY_ICON);
if (desktop_icon_name) {
gtk_image_set_from_icon_name (GTK_IMAGE (icon), desktop_icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR);
for (gchar ***p = matches; *p != NULL; p++)
g_strfreev (*p);
g_free (matches);
}
/* Last resort: Let's try to naively load icon names and just set the name provided by the application */
/* Fallback: Try to naively load icon names */
else {
GdkPixbuf *pix = NULL;
GtkIconInfo *icon_info = NULL;
......@@ -529,7 +497,6 @@ xfce4_notifyd_known_application_insert_row (XfconfChannel *channel,
gchar *icon_name_lower;
const gchar *icon_name;
gtk_label_set_text (GTK_LABEL (label), known_application);
/* Make sure spaces are converted to dashes so GTK_ICON_LOOKUP_GENERIC_FALLBACK works as expected */
icon_name = g_strdelimit ((gchar *) known_application," ",'-');
icon_name_lower = g_ascii_strdown (icon_name, -1);
......@@ -555,10 +522,21 @@ xfce4_notifyd_known_application_insert_row (XfconfChannel *channel,
if (pix)
g_object_unref (G_OBJECT (pix));
}
/* Try to find the correct application name based on the desktop file */
app_name = notify_get_from_desktop_file (known_application, G_KEY_FILE_DESKTOP_KEY_NAME);
if (app_name) {
gtk_label_set_text (GTK_LABEL (label), app_name);
}
/* Fallback: Just set the name provided by the application */
else {
gtk_label_set_text (GTK_LABEL (label), known_application);
}
}
if (app_name)
g_free (app_name);
g_free (desktop_icon_name);
desktop_icon_name = NULL;
......
......@@ -1342,9 +1342,9 @@ notify_notify (XfceNotifyGBus *skeleton,
}
}
else if (desktop_id) {
gchar *icon_name = notify_icon_name_from_desktop_id (desktop_id);
xfce_notify_window_set_icon_name (window, icon_name);
g_free (icon_name);
app_icon = notify_get_from_desktop_file (desktop_id, G_KEY_FILE_DESKTOP_KEY_ICON);
xfce_notify_window_set_icon_name (window, app_icon);
}
if (xndaemon->notification_log == TRUE &&
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment