[Xfce4-commits] <xfdesktop:master> Make the GIcon for file icons private

Eric Koegel noreply at xfce.org
Sun Sep 1 16:20:05 CEST 2013


Updating branch refs/heads/master
         to f6252759fcb4aa2e510237aa45c77b84bfab3cc4 (commit)
       from 2cdfde11b827b97576347c2eacb043780abc9415 (commit)

commit f6252759fcb4aa2e510237aa45c77b84bfab3cc4
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Sat Aug 17 15:47:21 2013 +0300

    Make the GIcon for file icons private

 src/xfdesktop-file-icon.c         |  100 +++++++++++++++++++++++++---
 src/xfdesktop-file-icon.h         |    9 ++-
 src/xfdesktop-regular-file-icon.c |  133 +++++++++++++++++++++----------------
 src/xfdesktop-special-file-icon.c |   94 +++++++++++++++-----------
 src/xfdesktop-volume-icon.c       |   45 +++++++++----
 5 files changed, 257 insertions(+), 124 deletions(-)

diff --git a/src/xfdesktop-file-icon.c b/src/xfdesktop-file-icon.c
index f90bf04..7673ea7 100644
--- a/src/xfdesktop-file-icon.c
+++ b/src/xfdesktop-file-icon.c
@@ -30,30 +30,61 @@
 #include "xfdesktop-file-utils.h"
 #include "xfdesktop-file-icon.h"
 
+struct _XfdesktopFileIconPrivate
+{
+    GIcon *gicon;
+};
+
 static void xfdesktop_file_icon_finalize(GObject *obj);
 
 static gboolean xfdesktop_file_icon_activated(XfdesktopIcon *icon);
 
+static void xfdesktop_file_icon_set_property(GObject *object,
+                                             guint property_id,
+                                             const GValue *value,
+                                             GParamSpec *pspec);
+static void xfdesktop_file_icon_get_property(GObject *object,
+                                             guint property_id,
+                                             GValue *value,
+                                             GParamSpec *pspec);
 
 G_DEFINE_ABSTRACT_TYPE(XfdesktopFileIcon, xfdesktop_file_icon,
                        XFDESKTOP_TYPE_ICON)
 
+enum
+{
+    PROP_0,
+    PROP_GICON,
+};
 
 static void
 xfdesktop_file_icon_class_init(XfdesktopFileIconClass *klass)
 {
     GObjectClass *gobject_class = (GObjectClass *)klass;
     XfdesktopIconClass *icon_class = (XfdesktopIconClass *)klass;
-    
+
+    g_type_class_add_private(klass, sizeof(XfdesktopFileIconPrivate));
+
     gobject_class->finalize = xfdesktop_file_icon_finalize;
+    gobject_class->set_property = xfdesktop_file_icon_set_property;
+    gobject_class->get_property = xfdesktop_file_icon_get_property;
     
     icon_class->activated = xfdesktop_file_icon_activated;
+
+    g_object_class_install_property(gobject_class,
+                                    PROP_GICON,
+                                    g_param_spec_pointer("gicon",
+                                                         "gicon",
+                                                         "gicon",
+                                                         G_PARAM_READWRITE));
 }
 
 static void
 xfdesktop_file_icon_init(XfdesktopFileIcon *icon)
 {
-    icon->gicon = NULL;
+    icon->priv = G_TYPE_INSTANCE_GET_PRIVATE(icon,
+                                             XFDESKTOP_TYPE_FILE_ICON,
+                                             XfdesktopFileIconPrivate);
 }
 
 static void
@@ -66,6 +97,45 @@ xfdesktop_file_icon_finalize(GObject *obj)
     G_OBJECT_CLASS(xfdesktop_file_icon_parent_class)->finalize(obj);
 }
 
+static void
+xfdesktop_file_icon_set_property(GObject *object,
+                                 guint property_id,
+                                 const GValue *value,
+                                 GParamSpec *pspec)
+{
+    XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(object);
+
+    switch(property_id) {
+        case PROP_GICON:
+            xfdesktop_file_icon_invalidate_icon(file_icon);
+            file_icon->priv->gicon = g_value_get_pointer(value);
+            break;
+
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+            break;
+    }
+}
+
+static void
+xfdesktop_file_icon_get_property(GObject *object,
+                                 guint property_id,
+                                 GValue *value,
+                                 GParamSpec *pspec)
+{
+    XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(object);
+
+    switch(property_id) {
+        case PROP_GICON:
+            g_value_set_pointer(value, file_icon->priv->gicon);
+            break;
+
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+            break;
+    }
+}
+
 static gboolean
 xfdesktop_file_icon_activated(XfdesktopIcon *icon)
 {
@@ -184,7 +254,7 @@ xfdesktop_file_icon_can_delete_file(XfdesktopFileIcon *icon)
         return FALSE;
 }
 
-void
+GIcon *
 xfdesktop_file_icon_add_emblems(XfdesktopFileIcon *icon)
 {
     GIcon *emblemed_icon = NULL;
@@ -192,12 +262,12 @@ xfdesktop_file_icon_add_emblems(XfdesktopFileIcon *icon)
 
     TRACE("entering");
 
-    g_return_if_fail(XFDESKTOP_IS_FILE_ICON(icon));
+    g_return_val_if_fail(XFDESKTOP_IS_FILE_ICON(icon), NULL);
 
-    if(G_IS_ICON(icon->gicon))
-        emblemed_icon = g_emblemed_icon_new(icon->gicon, NULL);
+    if(G_IS_ICON(icon->priv->gicon))
+        emblemed_icon = g_emblemed_icon_new(icon->priv->gicon, NULL);
     else
-        return;
+        return NULL;
 
     /* Get the list of emblems */
     emblem_names = g_file_info_get_attribute_stringv(xfdesktop_file_icon_peek_file_info(icon),
@@ -219,7 +289,7 @@ xfdesktop_file_icon_add_emblems(XfdesktopFileIcon *icon)
 
     /* Clear out the old icon and set the new one */
     xfdesktop_file_icon_invalidate_icon(icon);
-    icon->gicon = emblemed_icon;
+    return icon->priv->gicon = emblemed_icon;
 }
 
 void
@@ -227,8 +297,16 @@ xfdesktop_file_icon_invalidate_icon(XfdesktopFileIcon *icon)
 {
     g_return_if_fail(XFDESKTOP_IS_FILE_ICON(icon));
 
-    if(G_IS_ICON(icon->gicon)) {
-        g_object_unref(icon->gicon);
-        icon->gicon = NULL;
+    if(G_IS_ICON(icon->priv->gicon)) {
+        g_object_unref(icon->priv->gicon);
+        icon->priv->gicon = NULL;
     }
 }
+
+gboolean
+xfdesktop_file_icon_has_gicon(XfdesktopFileIcon *icon)
+{
+    g_return_val_if_fail(XFDESKTOP_IS_FILE_ICON(icon), FALSE);
+
+    return icon->priv->gicon != NULL ? TRUE : FALSE;
+}
diff --git a/src/xfdesktop-file-icon.h b/src/xfdesktop-file-icon.h
index 26cf816..2cd7319 100644
--- a/src/xfdesktop-file-icon.h
+++ b/src/xfdesktop-file-icon.h
@@ -36,11 +36,14 @@ G_BEGIN_DECLS
 
 typedef struct _XfdesktopFileIcon        XfdesktopFileIcon;
 typedef struct _XfdesktopFileIconClass   XfdesktopFileIconClass;
+typedef struct _XfdesktopFileIconPrivate XfdesktopFileIconPrivate;
 
 struct _XfdesktopFileIcon
 {
     XfdesktopIcon parent;
-    GIcon *gicon;
+
+    /*< private >*/
+    XfdesktopFileIconPrivate *priv;
 };
 
 struct _XfdesktopFileIconClass
@@ -69,10 +72,12 @@ gboolean xfdesktop_file_icon_can_rename_file(XfdesktopFileIcon *icon);
 
 gboolean xfdesktop_file_icon_can_delete_file(XfdesktopFileIcon *icon);
 
-void xfdesktop_file_icon_add_emblems(XfdesktopFileIcon *icon);
+GIcon *xfdesktop_file_icon_add_emblems(XfdesktopFileIcon *icon);
 
 void xfdesktop_file_icon_invalidate_icon(XfdesktopFileIcon *icon);
 
+gboolean xfdesktop_file_icon_has_gicon(XfdesktopFileIcon *icon);
+
 G_END_DECLS
 
 #endif  /* __XFDESKTOP_FILE_ICON_H__ */
diff --git a/src/xfdesktop-regular-file-icon.c b/src/xfdesktop-regular-file-icon.c
index 974407b..58b7ba7 100644
--- a/src/xfdesktop-regular-file-icon.c
+++ b/src/xfdesktop-regular-file-icon.c
@@ -290,80 +290,97 @@ xfdesktop_load_icon_from_desktop_file(XfdesktopRegularFileIcon *regular_icon)
     return gicon;
 }
 
-static GdkPixbuf *
-xfdesktop_regular_file_icon_peek_pixbuf(XfdesktopIcon *icon,
-                                        gint width, gint height)
+static GIcon *
+xfdesktop_regular_file_icon_load_icon(XfdesktopIcon *icon)
 {
     XfdesktopRegularFileIcon *regular_icon = XFDESKTOP_REGULAR_FILE_ICON(icon);
     XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(icon);
+    GIcon *gicon;
 
-    if(width != regular_icon->priv->cur_pix_width
-       || height != regular_icon->priv->cur_pix_height)
-        xfdesktop_regular_file_icon_invalidate_pixbuf(regular_icon);
-
-    /* Still valid */
-    if(regular_icon->priv->pix != NULL)
-        return regular_icon->priv->pix;
+    TRACE("entering");
 
-    if(!G_IS_ICON(file_icon->gicon)) {
-        /* Try to load the icon referenced in the .desktop file */
-        if(xfdesktop_file_utils_is_desktop_file(regular_icon->priv->file_info)) {
-            file_icon->gicon = xfdesktop_load_icon_from_desktop_file(regular_icon);
-        } else {
-            /* If we have a thumbnail then they are enabled, use it. */
-            if(regular_icon->priv->thumbnail_file) {
-                gchar *file = g_file_get_path(regular_icon->priv->file);
-                gchar *mimetype = xfdesktop_get_file_mimetype(file);
-
-                /* Don't use thumbnails for svg, use the file itself */
-                if(g_strcmp0(mimetype, "image/svg+xml") == 0)
-                    file_icon->gicon = g_file_icon_new(regular_icon->priv->file);
-                else
-                    file_icon->gicon = g_file_icon_new(regular_icon->priv->thumbnail_file);
-
-                g_free(mimetype);
-                g_free(file);
-            }
+    /* Try to load the icon referenced in the .desktop file */
+    if(xfdesktop_file_utils_is_desktop_file(regular_icon->priv->file_info)) {
+        gicon = xfdesktop_load_icon_from_desktop_file(regular_icon);
+    } else {
+        /* If we have a thumbnail then they are enabled, use it. */
+        if(regular_icon->priv->thumbnail_file) {
+            gchar *file = g_file_get_path(regular_icon->priv->file);
+            gchar *mimetype = xfdesktop_get_file_mimetype(file);
+
+            /* Don't use thumbnails for svg, use the file itself */
+            if(g_strcmp0(mimetype, "image/svg+xml") == 0)
+                gicon = g_file_icon_new(regular_icon->priv->file);
+            else
+                gicon = g_file_icon_new(regular_icon->priv->thumbnail_file);
+
+            g_free(mimetype);
+            g_free(file);
         }
+    }
 
-        /* If we still don't have an icon, use the default */
-        if(!G_IS_ICON(file_icon->gicon)) {
-            file_icon->gicon = g_file_info_get_icon(regular_icon->priv->file_info);
-            if(G_IS_ICON(file_icon->gicon))
-                g_object_ref(file_icon->gicon);
-        }
+    /* If we still don't have an icon, use the default */
+    if(!G_IS_ICON(gicon)) {
+        gicon = g_file_info_get_icon(regular_icon->priv->file_info);
+        if(G_IS_ICON(gicon))
+            g_object_ref(gicon);
+    }
 
-        /* Add any user set emblems */
-        xfdesktop_file_icon_add_emblems(file_icon);
+    g_object_set(file_icon, "gicon", gicon, NULL);
 
-        /* load the read only emblem if necessary */
-        if(!g_file_info_get_attribute_boolean(regular_icon->priv->file_info,
-                                              G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
-        {
-            GIcon *themed_icon = g_themed_icon_new(EMBLEM_READONLY);
-            GEmblem *emblem = g_emblem_new(themed_icon);
+    /* Add any user set emblems */
+    gicon = xfdesktop_file_icon_add_emblems(file_icon);
 
-            g_emblemed_icon_add_emblem(G_EMBLEMED_ICON(file_icon->gicon), emblem);
+    /* load the read only emblem if necessary */
+    if(!g_file_info_get_attribute_boolean(regular_icon->priv->file_info,
+                                          G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
+    {
+        GIcon *themed_icon = g_themed_icon_new(EMBLEM_READONLY);
+        GEmblem *emblem = g_emblem_new(themed_icon);
 
-            g_object_unref(emblem);
-            g_object_unref(themed_icon);
-        }
+        g_emblemed_icon_add_emblem(G_EMBLEMED_ICON(gicon), emblem);
 
-        /* load the symlink emblem if necessary */
-        if(g_file_info_get_attribute_boolean(regular_icon->priv->file_info,
-                                             G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK))
-        {
-            GIcon *themed_icon = g_themed_icon_new(EMBLEM_SYMLINK);
-            GEmblem *emblem = g_emblem_new(themed_icon);
+        g_object_unref(emblem);
+        g_object_unref(themed_icon);
+    }
 
-            g_emblemed_icon_add_emblem(G_EMBLEMED_ICON(file_icon->gicon), emblem);
+    /* load the symlink emblem if necessary */
+    if(g_file_info_get_attribute_boolean(regular_icon->priv->file_info,
+                                         G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK))
+    {
+        GIcon *themed_icon = g_themed_icon_new(EMBLEM_SYMLINK);
+        GEmblem *emblem = g_emblem_new(themed_icon);
 
-            g_object_unref(emblem);
-            g_object_unref(themed_icon);
-        }
+        g_emblemed_icon_add_emblem(G_EMBLEMED_ICON(gicon), emblem);
+
+        g_object_unref(emblem);
+        g_object_unref(themed_icon);
     }
 
-    regular_icon->priv->pix = xfdesktop_file_utils_get_icon(file_icon->gicon,
+    return gicon;
+}
+
+static GdkPixbuf *
+xfdesktop_regular_file_icon_peek_pixbuf(XfdesktopIcon *icon,
+                                        gint width, gint height)
+{
+    XfdesktopRegularFileIcon *regular_icon = XFDESKTOP_REGULAR_FILE_ICON(icon);
+    GIcon *gicon = NULL;
+
+    if(width != regular_icon->priv->cur_pix_width
+       || height != regular_icon->priv->cur_pix_height)
+        xfdesktop_regular_file_icon_invalidate_pixbuf(regular_icon);
+
+    /* Still valid */
+    if(regular_icon->priv->pix != NULL)
+        return regular_icon->priv->pix;
+
+    if(!xfdesktop_file_icon_has_gicon(XFDESKTOP_FILE_ICON(icon)))
+        gicon = xfdesktop_regular_file_icon_load_icon(icon);
+    else
+        g_object_get(XFDESKTOP_FILE_ICON(icon), "gicon", &gicon, NULL);
+
+    regular_icon->priv->pix = xfdesktop_file_utils_get_icon(gicon,
                                                             width, height,
                                                             regular_icon->priv->pix_opacity);
 
diff --git a/src/xfdesktop-special-file-icon.c b/src/xfdesktop-special-file-icon.c
index 3f4b96e..17c3fb5 100644
--- a/src/xfdesktop-special-file-icon.c
+++ b/src/xfdesktop-special-file-icon.c
@@ -216,59 +216,75 @@ xfdesktop_special_file_icon_invalidate_pixbuf(XfdesktopSpecialFileIcon *icon)
     }
 }
 
-static GdkPixbuf *
-xfdesktop_special_file_icon_peek_pixbuf(XfdesktopIcon *icon,
-                                        gint width, gint height)
+static GIcon *
+xfdesktop_special_file_icon_load_icon(XfdesktopIcon *icon)
 {
     XfdesktopSpecialFileIcon *special_icon = XFDESKTOP_SPECIAL_FILE_ICON(icon);
     XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(icon);
+    gchar *icon_name = NULL;
     GFile *parent = NULL;
+    GIcon *gicon;
 
-    if(height != special_icon->priv->cur_pix_height)
-        xfdesktop_special_file_icon_invalidate_pixbuf(special_icon);
+    TRACE("entering");
 
-    /* Still valid */
-    if(special_icon->priv->pix != NULL)
-        return special_icon->priv->pix;
+    /* use a custom icon name for the local filesystem root */
+    parent = g_file_get_parent(special_icon->priv->file);
+    if(!parent && g_file_has_uri_scheme(special_icon->priv->file, "file"))
+        icon_name = g_strdup("drive-harddisk");
 
-    if(!G_IS_ICON(file_icon->gicon)) {
-        gchar *icon_name = NULL;
+    if(parent)
+        g_object_unref(parent);
 
-        /* use a custom icon name for the local filesystem root */
-        parent = g_file_get_parent(special_icon->priv->file);
-        if(!parent && g_file_has_uri_scheme(special_icon->priv->file, "file"))
-            icon_name = g_strdup("drive-harddisk");
+    /* use a custom icon for the trash, based on it having files
+     * the user can delete */
+    if(special_icon->priv->type == XFDESKTOP_SPECIAL_FILE_ICON_TRASH) {
+        if(special_icon->priv->trash_item_count == 0)
+            icon_name = g_strdup("user-trash");
+        else
+            icon_name = g_strdup("user-trash-full");
+    }
 
-        if(parent)
-            g_object_unref(parent);
+    /* Create the themed icon for it */
+    if(icon_name) {
+        gicon = g_themed_icon_new(icon_name);
+        g_free(icon_name);
+    }
 
-        /* use a custom icon for the trash, based on it having files
-         * the user can delete */
-        if(special_icon->priv->type == XFDESKTOP_SPECIAL_FILE_ICON_TRASH) {
-            if(special_icon->priv->trash_item_count == 0)
-                icon_name = g_strdup("user-trash");
-            else
-                icon_name = g_strdup("user-trash-full");
-        }
+    /* If we still don't have an icon, use the default */
+    if(!G_IS_ICON(gicon)) {
+        gicon = g_file_info_get_icon(special_icon->priv->file_info);
+        if(G_IS_ICON(gicon))
+            g_object_ref(gicon);
+    }
 
-        /* Create the themed icon for it */
-        if(icon_name) {
-            file_icon->gicon = g_themed_icon_new(icon_name);
-            g_free(icon_name);
-        }
+    g_object_set(file_icon, "gicon", gicon, NULL);
 
-        /* If we still don't have an icon, use the default */
-        if(!G_IS_ICON(file_icon->gicon)) {
-            file_icon->gicon = g_file_info_get_icon(special_icon->priv->file_info);
-            if(G_IS_ICON(file_icon->gicon))
-                g_object_ref(file_icon->gicon);
-        }
+    /* Add any user set emblems */
+    gicon = xfdesktop_file_icon_add_emblems(file_icon);
 
-        /* Add any user set emblems */
-        xfdesktop_file_icon_add_emblems(file_icon);
-    }
+    return gicon;
+}
+
+static GdkPixbuf *
+xfdesktop_special_file_icon_peek_pixbuf(XfdesktopIcon *icon,
+                                        gint width, gint height)
+{
+    XfdesktopSpecialFileIcon *special_icon = XFDESKTOP_SPECIAL_FILE_ICON(icon);
+    GIcon *gicon = NULL;
+
+    if(height != special_icon->priv->cur_pix_height)
+        xfdesktop_special_file_icon_invalidate_pixbuf(special_icon);
+
+    /* Still valid */
+    if(special_icon->priv->pix != NULL)
+        return special_icon->priv->pix;
+
+    if(!xfdesktop_file_icon_has_gicon(XFDESKTOP_FILE_ICON(icon)))
+        gicon = xfdesktop_special_file_icon_load_icon(icon);
+    else
+        g_object_get(XFDESKTOP_FILE_ICON(icon), "gicon", &gicon, NULL);
 
-    special_icon->priv->pix = xfdesktop_file_utils_get_icon(file_icon->gicon,
+    special_icon->priv->pix = xfdesktop_file_utils_get_icon(gicon,
                                                             height,
                                                             height,
                                                             100);
diff --git a/src/xfdesktop-volume-icon.c b/src/xfdesktop-volume-icon.c
index 024bca2..f347381 100644
--- a/src/xfdesktop-volume-icon.c
+++ b/src/xfdesktop-volume-icon.c
@@ -252,13 +252,38 @@ xfdesktop_volume_icon_is_mounted(XfdesktopIcon *icon)
     return ret;
 }
 
+static GIcon *
+xfdesktop_volume_icon_load_icon(XfdesktopIcon *icon)
+{
+    XfdesktopVolumeIcon *volume_icon = XFDESKTOP_VOLUME_ICON(icon);
+    XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(icon);
+    GIcon *gicon = NULL;
+
+    TRACE("entering");
+
+    /* load icon and keep a ref to it */
+    if(volume_icon->priv->volume) {
+        gicon = g_volume_get_icon(volume_icon->priv->volume);
+
+        if(G_IS_ICON(gicon))
+            g_object_ref(gicon);
+
+        g_object_set(file_icon, "gicon", gicon, NULL);
+
+        /* Add any user set emblems */
+        gicon = xfdesktop_file_icon_add_emblems(file_icon);
+    }
+
+    return gicon;
+}
+
 static GdkPixbuf *
 xfdesktop_volume_icon_peek_pixbuf(XfdesktopIcon *icon,
                                   gint width, gint height)
 {
     XfdesktopVolumeIcon *volume_icon = XFDESKTOP_VOLUME_ICON(icon);
-    XfdesktopFileIcon *file_icon = XFDESKTOP_FILE_ICON(icon);
     gint opacity = 100;
+    GIcon *gicon = NULL;
     
     g_return_val_if_fail(XFDESKTOP_IS_VOLUME_ICON(icon), NULL);
     
@@ -269,24 +294,16 @@ xfdesktop_volume_icon_peek_pixbuf(XfdesktopIcon *icon,
     if(volume_icon->priv->pix != NULL)
         return volume_icon->priv->pix;
 
-    if(!G_IS_ICON(file_icon->gicon)) {
-        /* icon changed, get a new one and keep a ref to it */
-        if(volume_icon->priv->volume) {
-            file_icon->gicon = g_volume_get_icon(volume_icon->priv->volume);
-
-            if(G_IS_ICON(file_icon->gicon))
-                g_object_ref(file_icon->gicon);
-
-            /* Add any user set emblems */
-            xfdesktop_file_icon_add_emblems(file_icon);
-         }
-    }
+    if(!xfdesktop_file_icon_has_gicon(XFDESKTOP_FILE_ICON(icon)))
+        gicon = xfdesktop_volume_icon_load_icon(icon);
+    else
+        g_object_get(XFDESKTOP_FILE_ICON(icon), "gicon", &gicon, NULL);
 
     /* If the volume isn't mounted show it as semi-transparent */
     if(!xfdesktop_volume_icon_is_mounted(icon))
         opacity = 50;
 
-    volume_icon->priv->pix = xfdesktop_file_utils_get_icon(file_icon->gicon,
+    volume_icon->priv->pix = xfdesktop_file_utils_get_icon(gicon,
                                                            height, height, 
                                                            opacity);
 


More information about the Xfce4-commits mailing list