[Xfce4-commits] <xfdesktop:xfce-4.8> Use Name field as display name of desktop files (bug #6896).

Jannis Pohlmann noreply at xfce.org
Tue Jan 18 20:48:04 CET 2011


Updating branch refs/heads/xfce-4.8
         to bef2dd0cdec38b914558fa3a8261660cabccfaef (commit)
       from c8f28d279373526ed8dc849e1b7128e7a4d164e5 (commit)

commit bef2dd0cdec38b914558fa3a8261660cabccfaef
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Tue Jan 18 20:42:47 2011 +0100

    Use Name field as display name of desktop files (bug #6896).
    
    To implement this in a somewhat clean way, the function
    thunar_g_file_query_key_file() was copied over as
    xfdesktop_file_utils_query_key_file(). Another method
    xfdesktop_file_utils_get_display_name() was added to determine the
    correct display name to use for a given file and file info.
    
    XfdesktopRegularFileIcon was altered to include a private variable
    display_name in which the cached display name is stored.

 NEWS                              |    6 +++
 src/xfdesktop-file-utils.c        |   78 ++++++++++++++++++++++++++++++++++++-
 src/xfdesktop-file-utils.h        |    9 +++-
 src/xfdesktop-regular-file-icon.c |   46 +++++++++++++++-------
 4 files changed, 121 insertions(+), 18 deletions(-)

diff --git a/NEWS b/NEWS
index c99481f..f509dea 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,9 @@
+Xfdesktop 4.x.y
+---------------
+
+  * Use Name field as display name of desktop files (bug #6896).
+
+
 Xfdesktop 4.8.0
 ---------------
 
diff --git a/src/xfdesktop-file-utils.c b/src/xfdesktop-file-utils.c
index 644b26c..b3a3486 100644
--- a/src/xfdesktop-file-utils.c
+++ b/src/xfdesktop-file-utils.c
@@ -1,8 +1,8 @@
 /*
  *  xfdesktop - xfce4's desktop manager
  *
- *  Copyright(c) 2006 Brian Tarricone, <bjt23 at cornell.edu>
- *  Copyright(c) 2010 Jannis Pohlmann, <jannis at xfce.org>
+ *  Copyright(c) 2006      Brian Tarricone, <bjt23 at cornell.edu>
+ *  Copyright(c) 2010-2011 Jannis Pohlmann, <jannis at xfce.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -159,6 +159,80 @@ xfdesktop_file_utils_format_time_for_display(guint64 file_time)
     return g_strdup(_("Unknown"));
 }
 
+GKeyFile *
+xfdesktop_file_utils_query_key_file(GFile *file,
+                                    GCancellable *cancellable,
+                                    GError **error)
+{
+    GKeyFile *key_file;
+    gchar *contents = NULL;
+    gsize length;
+
+    g_return_val_if_fail(G_IS_FILE(file), NULL);
+    g_return_val_if_fail(cancellable == NULL || G_IS_CANCELLABLE(cancellable), NULL);
+    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
+
+    /* try to load the entire file into memory */
+    if (!g_file_load_contents(file, cancellable, &contents, &length, NULL, error))
+        return NULL;
+
+    /* allocate a new key file */
+    key_file = g_key_file_new();
+
+    /* try to parse the key file from the contents of the file */
+    if (length == 0
+        || g_key_file_load_from_data(key_file, contents, length,
+                                     G_KEY_FILE_KEEP_COMMENTS
+                                     | G_KEY_FILE_KEEP_TRANSLATIONS,
+                                     error))
+    {
+        g_free(contents);
+        return key_file;
+    }
+    else
+    {
+        g_free(contents);
+        g_key_file_free(key_file);
+        return NULL;
+    }
+}
+
+gchar *
+xfdesktop_file_utils_get_display_name(GFile *file,
+                                      GFileInfo *info)
+{
+    GKeyFile *key_file;
+    gchar *display_name = NULL;
+
+    g_return_val_if_fail(G_IS_FILE_INFO(info), NULL);
+
+    /* check if we have a desktop entry */
+    if(xfdesktop_file_utils_is_desktop_file(info)) {
+        /* try to load its data into a GKeyFile */
+        key_file = xfdesktop_file_utils_query_key_file(file, NULL, NULL);
+        if(key_file) {
+            /* try to parse the display name */
+            display_name = g_key_file_get_string(key_file, 
+                                                 G_KEY_FILE_DESKTOP_GROUP,
+                                                 G_KEY_FILE_DESKTOP_KEY_NAME,
+                                                 NULL);
+            
+            /* free the key file */
+            g_key_file_free (key_file);
+        }
+    }
+
+    /* use the default display name as a fallback */
+    if(!display_name 
+       || *display_name == '\0' 
+       || !g_utf8_validate(display_name, -1, NULL))
+    {
+        display_name = g_strdup(g_file_info_get_display_name(info));
+    }
+
+    return display_name;
+}
+
 gboolean
 xfdesktop_file_utils_volume_is_present(GVolume *volume)
 {
diff --git a/src/xfdesktop-file-utils.h b/src/xfdesktop-file-utils.h
index 97a0e21..73891d1 100644
--- a/src/xfdesktop-file-utils.h
+++ b/src/xfdesktop-file-utils.h
@@ -1,8 +1,8 @@
 /*
  *  xfdesktop - xfce4's desktop manager
  *
- *  Copyright(c) 2006 Brian Tarricone, <bjt23 at cornell.edu>
- *  Copyright(c) 2010 Jannis Pohlmann, <jannis at xfce.org>
+ *  Copyright(c) 2006      Brian Tarricone, <bjt23 at cornell.edu>
+ *  Copyright(c) 2010-2011 Jannis Pohlmann, <jannis at xfce.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -33,6 +33,11 @@
 gboolean xfdesktop_file_utils_is_desktop_file(GFileInfo *info);
 gboolean xfdesktop_file_utils_file_is_executable(GFileInfo *info);
 gchar *xfdesktop_file_utils_format_time_for_display(guint64 file_time);
+GKeyFile *xfdesktop_file_utils_query_key_file(GFile *file, 
+                                              GCancellable *cancellable, 
+                                              GError **error);
+gchar *xfdesktop_file_utils_get_display_name(GFile *file,
+                                             GFileInfo *info);
 
 gboolean xfdesktop_file_utils_volume_is_present(GVolume *volume);
 gboolean xfdesktop_file_utils_volume_is_removable(GVolume *volume);
diff --git a/src/xfdesktop-regular-file-icon.c b/src/xfdesktop-regular-file-icon.c
index 323e79b..b360f7f 100644
--- a/src/xfdesktop-regular-file-icon.c
+++ b/src/xfdesktop-regular-file-icon.c
@@ -3,7 +3,7 @@
  *
  *  Copyright(c) 2006 Brian Tarricone, <bjt23 at cornell.edu>
  *  Copyright(c) 2006 Benedikt Meurer, <benny at xfce.org>
- *  Copyright(c) 2010 Jannis Pohlmann, <jannis at xfce.org>
+ *  Copyright(c) 2010-2011 Jannis Pohlmann, <jannis at xfce.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -61,6 +61,7 @@
 struct _XfdesktopRegularFileIconPrivate
 {
     GdkPixbuf *pix;
+    gchar *display_name;
     gchar *tooltip;
     guint pix_opacity;
     gint cur_pix_size;
@@ -142,6 +143,7 @@ xfdesktop_regular_file_icon_init(XfdesktopRegularFileIcon *icon)
                                              XFDESKTOP_TYPE_REGULAR_FILE_ICON,
                                              XfdesktopRegularFileIconPrivate);
     icon->priv->pix_opacity = 100;
+    icon->priv->display_name = NULL;
 }
 
 static void
@@ -153,7 +155,7 @@ xfdesktop_regular_file_icon_finalize(GObject *obj)
     g_signal_handlers_disconnect_by_func(G_OBJECT(itheme),
                                          G_CALLBACK(xfdesktop_regular_file_icon_invalidate_pixbuf),
                                          icon);
-    
+
     if(icon->priv->pix)
         g_object_unref(G_OBJECT(icon->priv->pix));
     
@@ -165,6 +167,8 @@ xfdesktop_regular_file_icon_finalize(GObject *obj)
 
     g_object_unref(icon->priv->file);
     
+    g_free(icon->priv->display_name);
+    
     if(icon->priv->tooltip)
         g_free(icon->priv->tooltip);
     
@@ -295,11 +299,11 @@ xfdesktop_regular_file_icon_peek_pixbuf(XfdesktopIcon *icon,
 static G_CONST_RETURN gchar *
 xfdesktop_regular_file_icon_peek_label(XfdesktopIcon *icon)
 {
-    GFileInfo *info = XFDESKTOP_REGULAR_FILE_ICON(icon)->priv->file_info;
+    XfdesktopRegularFileIcon *regular_file_icon = XFDESKTOP_REGULAR_FILE_ICON(icon);
 
     g_return_val_if_fail(XFDESKTOP_IS_REGULAR_FILE_ICON(icon), NULL);
 
-    return info ? g_file_info_get_display_name(info) : NULL;
+    return regular_file_icon->priv->display_name;
 }
 
 static GdkDragAction
@@ -534,21 +538,15 @@ xfdesktop_regular_file_icon_update_file_info(XfdesktopFileIcon *icon,
                                              GFileInfo *info)
 {
     XfdesktopRegularFileIcon *regular_file_icon = XFDESKTOP_REGULAR_FILE_ICON(icon);
-    const gchar *old_display_name, *new_display_name;
+    const gchar *old_display_name;
+    gchar *new_display_name;
     gboolean label_changed = FALSE;
     
     g_return_if_fail(XFDESKTOP_IS_REGULAR_FILE_ICON(icon));
     g_return_if_fail(G_IS_FILE_INFO(info));
 
+    /* release the old file info */
     if(regular_file_icon->priv->file_info) { 
-        old_display_name = g_file_info_get_attribute_string(regular_file_icon->priv->file_info,
-                                                            G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
-        new_display_name = g_file_info_get_attribute_string(info,
-                                                            G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
-
-        label_changed = (g_strcmp0(old_display_name, new_display_name) != 0);
-
-        /* release the old file info */
         g_object_unref(regular_file_icon->priv->file_info);
         regular_file_icon->priv->file_info = NULL;
     }
@@ -557,12 +555,28 @@ xfdesktop_regular_file_icon_update_file_info(XfdesktopFileIcon *icon,
 
     if(regular_file_icon->priv->filesystem_info)
         g_object_unref(regular_file_icon->priv->filesystem_info);
+
     regular_file_icon->priv->filesystem_info = g_file_query_filesystem_info(regular_file_icon->priv->file,
                                                                             XFDESKTOP_FILESYSTEM_INFO_NAMESPACE,
                                                                             NULL, NULL);
 
-    if(label_changed)
+    /* get both, old and new display name */
+    old_display_name = regular_file_icon->priv->display_name;
+    new_display_name = xfdesktop_file_utils_get_display_name(regular_file_icon->priv->file,
+                                                             regular_file_icon->priv->file_info);
+
+    /* check whether the display name has changed with the info update */
+    if(g_strcmp0 (old_display_name, new_display_name) != 0) {
+        /* replace the display name */
+        g_free (regular_file_icon->priv->display_name);
+        regular_file_icon->priv->display_name = new_display_name;
+
+        /* notify listeners of the label change */
         xfdesktop_icon_label_changed(XFDESKTOP_ICON(icon));
+    } else {
+        /* no change, release the new display name */
+        g_free (new_display_name);
+    }
     
     /* not really easy to check if this changed or not, so just invalidate it */
     xfdesktop_regular_file_icon_invalidate_pixbuf(regular_file_icon);
@@ -588,6 +602,10 @@ xfdesktop_regular_file_icon_new(GFile *file,
     regular_file_icon->priv->file = g_object_ref(file);
     regular_file_icon->priv->file_info = g_object_ref(file_info);
 
+    /* set the display name */
+    regular_file_icon->priv->display_name = xfdesktop_file_utils_get_display_name(file, 
+                                                                                  file_info);
+
     /* query file system information from GIO */
     regular_file_icon->priv->filesystem_info = g_file_query_filesystem_info(regular_file_icon->priv->file,
                                                                             XFDESKTOP_FILESYSTEM_INFO_NAMESPACE,



More information about the Xfce4-commits mailing list