[gthumb] icon cache: handle any GIcon type



commit 13dc2c1db8dc4300adb6e33d8d13f89ecc66c012
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Aug 13 17:50:37 2012 +0200

    icon cache: handle any GIcon type

 extensions/catalogs/gth-organize-task.c |    2 +-
 gthumb/gth-folder-tree.c                |    2 +-
 gthumb/gth-icon-cache.c                 |   90 ++++++++----------------
 gthumb/gth-icon-cache.h                 |    7 ++-
 gthumb/gth-location-chooser.c           |    2 +-
 gthumb/gtk-utils.c                      |  115 ++++++++-----------------------
 gthumb/gtk-utils.h                      |    6 +-
 7 files changed, 68 insertions(+), 156 deletions(-)
---
diff --git a/extensions/catalogs/gth-organize-task.c b/extensions/catalogs/gth-organize-task.c
index 74304f5..4bc35c8 100644
--- a/extensions/catalogs/gth-organize-task.c
+++ b/extensions/catalogs/gth-organize-task.c
@@ -797,7 +797,7 @@ gth_organize_task_init (GthOrganizeTask *self)
 
 	icon = g_themed_icon_new ("file-catalog");
 	self->priv->icon_pixbuf = _g_icon_get_pixbuf (icon,
-						      _gtk_icon_get_pixel_size (GET_WIDGET ("organization_treeview"), GTK_ICON_SIZE_MENU),
+						      _gtk_widget_lookup_for_size (GET_WIDGET ("organization_treeview"), GTK_ICON_SIZE_MENU),
 						      gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GET_WIDGET ("organization_treeview"))));
 	g_object_unref (icon);
 
diff --git a/gthumb/gth-folder-tree.c b/gthumb/gth-folder-tree.c
index 6516b0e..c88dbf8 100644
--- a/gthumb/gth-folder-tree.c
+++ b/gthumb/gth-folder-tree.c
@@ -982,7 +982,7 @@ gth_folder_tree_construct (GthFolderTree *folder_tree)
 	GtkTreeSelection *selection;
 
 	folder_tree->priv->icon_cache = gth_icon_cache_new (gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (folder_tree))),
-							    _gtk_icon_get_pixel_size (GTK_WIDGET (folder_tree), GTK_ICON_SIZE_MENU));
+							    _gtk_widget_lookup_for_size (GTK_WIDGET (folder_tree), GTK_ICON_SIZE_MENU));
 
 	folder_tree->priv->tree_store = gtk_tree_store_new (NUM_COLUMNS,
 							    PANGO_TYPE_STYLE,
diff --git a/gthumb/gth-icon-cache.c b/gthumb/gth-icon-cache.c
index 1f9eea0..4c926a0 100644
--- a/gthumb/gth-icon-cache.c
+++ b/gthumb/gth-icon-cache.c
@@ -23,16 +23,13 @@
 #include "glib-utils.h"
 #include "gth-icon-cache.h"
 #include "gtk-utils.h"
-#include "pixbuf-utils.h"
-
-
-#define VOID_PIXBUF_KEY "void-pixbuf"
 
 
 struct _GthIconCache {
 	GtkIconTheme *icon_theme;
 	int           icon_size;
 	GHashTable   *cache;
+	GIcon        *fallback_icon;
 };
 
 
@@ -47,9 +44,7 @@ gth_icon_cache_new (GtkIconTheme *icon_theme,
 	icon_cache = g_new0 (GthIconCache, 1);
 	icon_cache->icon_theme = icon_theme;
 	icon_cache->icon_size = icon_size;
-	icon_cache->cache = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
-
-	g_hash_table_insert (icon_cache->cache, VOID_PIXBUF_KEY, _gdk_pixbuf_new_void (icon_cache->icon_size, icon_cache->icon_size));
+	icon_cache->cache = g_hash_table_new_full (g_icon_hash, (GEqualFunc) g_icon_equal, g_object_unref, g_object_unref);
 
 	return icon_cache;
 }
@@ -63,53 +58,41 @@ gth_icon_cache_new_for_widget (GtkWidget   *widget,
 	int           pixel_size;
 
 	icon_theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget));
-	pixel_size = _gtk_icon_get_pixel_size (widget, GTK_ICON_SIZE_MENU);
+	pixel_size = _gtk_widget_lookup_for_size (widget, icon_size);
 
 	return gth_icon_cache_new (icon_theme, pixel_size);
 }
 
 
 void
+gth_icon_cache_set_fallback (GthIconCache *icon_cache,
+			     GIcon        *icon)
+{
+	if (icon_cache->fallback_icon != NULL)
+		g_object_unref (icon_cache->fallback_icon);
+	icon_cache->fallback_icon = icon;
+	if (icon_cache->fallback_icon != NULL)
+		g_object_ref (icon_cache->fallback_icon);
+}
+
+
+void
 gth_icon_cache_free (GthIconCache *icon_cache)
 {
 	if (icon_cache == NULL)
 		return;
 	g_hash_table_destroy (icon_cache->cache);
+	if (icon_cache->fallback_icon != NULL)
+		g_object_unref (icon_cache->fallback_icon);
 	g_free (icon_cache);
 }
 
 
-static const char *
-_gth_icon_cache_get_icon_key (GIcon *icon)
+void
+gth_icon_cache_clear (GthIconCache *icon_cache)
 {
-	const char *key = NULL;
-
-	if (G_IS_THEMED_ICON (icon)) {
-		char **icon_names;
-		char  *name;
-
-		g_object_get (icon, "names", &icon_names, NULL);
-		name = g_strjoinv (",", icon_names);
-		key = get_static_string (name);
-
-		g_free (name);
-		g_strfreev (icon_names);
-	}
-	else if (G_IS_FILE_ICON (icon)) {
-		GFile *file;
-
-		file = g_file_icon_get_file (G_FILE_ICON (icon));
-		if (file != NULL) {
-			char *uri;
-
-			uri = g_file_get_uri (file);
-			key = get_static_string (uri);
-
-			g_free (uri);
-		}
-	}
-
-	return key;
+	if (icon_cache != NULL)
+		g_hash_table_remove_all (icon_cache->cache);
 }
 
 
@@ -117,35 +100,20 @@ GdkPixbuf *
 gth_icon_cache_get_pixbuf (GthIconCache *icon_cache,
 			   GIcon        *icon)
 {
-	const char *key;
-	GdkPixbuf  *pixbuf;
+	GdkPixbuf *pixbuf;
 
-	key = NULL;
-	if (icon != NULL)
-		key = _gth_icon_cache_get_icon_key (icon);
-
-	if (key == NULL)
-		key = VOID_PIXBUF_KEY;
-
-	pixbuf = g_hash_table_lookup (icon_cache->cache, key);
-	if (pixbuf != NULL) {
-		g_object_ref (pixbuf);
-		return pixbuf;
-	}
+	pixbuf = g_hash_table_lookup (icon_cache->cache, icon);
+	if (pixbuf != NULL)
+		return g_object_ref (pixbuf);
 
 	if (icon != NULL)
 		pixbuf = _g_icon_get_pixbuf (icon, icon_cache->icon_size, icon_cache->icon_theme);
 
-	if (pixbuf == NULL) {
-		GIcon *unknown_icon;
-
-		unknown_icon = g_themed_icon_new ("missing-image");
-		pixbuf = _g_icon_get_pixbuf (unknown_icon, icon_cache->icon_size, icon_cache->icon_theme);
-
-		g_object_unref (unknown_icon);
-	}
+	if ((pixbuf == NULL) && (icon_cache->fallback_icon != NULL))
+		pixbuf = _g_icon_get_pixbuf (icon_cache->fallback_icon, icon_cache->icon_size, icon_cache->icon_theme);
 
-	g_hash_table_insert (icon_cache->cache, (gpointer) key, g_object_ref (pixbuf));
+	if (pixbuf != NULL)
+		g_hash_table_insert (icon_cache->cache, g_object_ref (icon), g_object_ref (pixbuf));
 
 	return pixbuf;
 }
diff --git a/gthumb/gth-icon-cache.h b/gthumb/gth-icon-cache.h
index 8f6819c..2da8969 100644
--- a/gthumb/gth-icon-cache.h
+++ b/gthumb/gth-icon-cache.h
@@ -18,7 +18,7 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
- 
+
 #ifndef GTH_ICON_CACHE_H
 #define GTH_ICON_CACHE_H
 
@@ -32,7 +32,10 @@ GthIconCache * gth_icon_cache_new            (GtkIconTheme *icon_theme,
 			                      int           icon_size);
 GthIconCache * gth_icon_cache_new_for_widget (GtkWidget    *widget,
 			                      GtkIconSize   icon_size);
-void           gth_icon_cache_free           (GthIconCache *icon_cache);	                  
+void           gth_icon_cache_set_fallback   (GthIconCache *icon_cache,
+					      GIcon        *icon);
+void           gth_icon_cache_free           (GthIconCache *icon_cache);
+void           gth_icon_cache_clear          (GthIconCache *icon_cache);
 GdkPixbuf *    gth_icon_cache_get_pixbuf     (GthIconCache *icon_cache,
 				              GIcon        *icon);
 
diff --git a/gthumb/gth-location-chooser.c b/gthumb/gth-location-chooser.c
index f424e15..f1db35a 100644
--- a/gthumb/gth-location-chooser.c
+++ b/gthumb/gth-location-chooser.c
@@ -493,7 +493,7 @@ gth_location_chooser_realize (GtkWidget *widget)
 
 	GTK_WIDGET_CLASS (gth_location_chooser_parent_class)->realize (widget);
 	self->priv->icon_cache = gth_icon_cache_new (gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (self))),
-						     _gtk_icon_get_pixel_size (GTK_WIDGET (self), GTK_ICON_SIZE_MENU));
+						     _gtk_widget_lookup_for_size (GTK_WIDGET (self), GTK_ICON_SIZE_MENU));
 	entry_points_changed_cb (NULL, self);
 	current_location_changed (self);
 }
diff --git a/gthumb/gtk-utils.c b/gthumb/gtk-utils.c
index e4c0381..9b6da03 100644
--- a/gthumb/gtk-utils.c
+++ b/gthumb/gtk-utils.c
@@ -415,87 +415,29 @@ _gtk_info_dialog_run (GtkWindow        *parent,
 }
 
 
-static GdkPixbuf *
-get_themed_icon_pixbuf (GThemedIcon  *icon,
-			int           size,
-			GtkIconTheme *icon_theme)
-{
-	char        **icon_names;
-	GtkIconInfo  *icon_info;
-	GdkPixbuf    *pixbuf;
-	GError       *error = NULL;
-
-	g_object_get (icon, "names", &icon_names, NULL);
-
-	if (icon_theme == NULL)
-		icon_theme = gtk_icon_theme_get_default ();
-	icon_info = gtk_icon_theme_choose_icon (icon_theme, (const char **)icon_names, size, 0);
-	if (icon_info == NULL)
-		icon_info = gtk_icon_theme_lookup_icon (icon_theme, "text-x-generic", size, GTK_ICON_LOOKUP_USE_BUILTIN);
-
-	pixbuf = gtk_icon_info_load_icon (icon_info, &error);
-	if (pixbuf == NULL) {
-		g_warning ("could not load icon pixbuf: %s\n", error->message);
-		g_clear_error (&error);
-	}
-
-	gtk_icon_info_free (icon_info);
-	g_strfreev (icon_names);
-
-	return pixbuf;
-}
-
-
-static GdkPixbuf *
-get_file_icon_pixbuf (GFileIcon *icon,
-		      int        size)
-{
-	GdkPixbuf *pixbuf = NULL;
-	GFile     *file;
-
-	file = g_file_icon_get_file (icon);
-	if (file != NULL) {
-		char *filename;
-
-		filename = g_file_get_path (file);
-		if (filename != NULL)
-			pixbuf = gdk_pixbuf_new_from_file_at_size (filename, size, -1, NULL);
-
-		g_free (filename);
-	}
-
-	return pixbuf;
-}
-
-
 GdkPixbuf *
 _g_icon_get_pixbuf (GIcon        *icon,
-		    int           size,
-		    GtkIconTheme *theme)
+		    int           icon_size,
+		    GtkIconTheme *icon_theme)
 {
-	GdkPixbuf *pixbuf;
-	int        w, h;
+	GdkPixbuf   *pixbuf = NULL;
+	GtkIconInfo *icon_info;
 
-	if (icon == NULL)
-		return NULL;
+	icon_info = gtk_icon_theme_lookup_by_gicon (icon_theme,
+						    icon,
+						    icon_size,
+						    GTK_ICON_LOOKUP_USE_BUILTIN);
 
-	pixbuf = NULL;
-	if (G_IS_THEMED_ICON (icon))
-		pixbuf = get_themed_icon_pixbuf (G_THEMED_ICON (icon), size, theme);
-	else if (G_IS_FILE_ICON (icon))
-		pixbuf = get_file_icon_pixbuf (G_FILE_ICON (icon), size);
+	if (icon_info != NULL) {
+		GError *error = NULL;
 
-	if (pixbuf == NULL)
-		return NULL;
-
-	w = gdk_pixbuf_get_width (pixbuf);
-	h = gdk_pixbuf_get_height (pixbuf);
-	if (scale_keeping_ratio (&w, &h, size, size, FALSE)) {
-		GdkPixbuf *scaled;
+		pixbuf = gtk_icon_info_load_icon (icon_info, &error);
+		if (error != NULL) {
+			g_print ("%s\n", error->message);
+			g_error_free (error);
+		}
 
-		scaled = gdk_pixbuf_scale_simple (pixbuf, w, h, GDK_INTERP_BILINEAR);
-		g_object_unref (pixbuf);
-		pixbuf = scaled;
+		gtk_icon_info_free (icon_info);
 	}
 
 	return pixbuf;
@@ -521,19 +463,6 @@ get_mime_type_pixbuf (const char   *mime_type,
 }
 
 
-int
-_gtk_icon_get_pixel_size (GtkWidget   *widget,
-			  GtkIconSize  size)
-{
-	int icon_width, icon_height;
-
-	gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (widget),
-					   size,
-					   &icon_width, &icon_height);
-	return MAX (icon_width, icon_height);
-}
-
-
 void
 show_help_dialog (GtkWindow  *parent,
 		  const char *section)
@@ -790,6 +719,18 @@ _gtk_widget_get_allocated_height (GtkWidget *widget)
 }
 
 
+int
+_gtk_widget_lookup_for_size (GtkWidget   *widget,
+			     GtkIconSize  icon_size)
+{
+	int w, h;
+	gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (widget),
+					   icon_size,
+					   &w, &h);
+	return MAX (w, h);
+}
+
+
 void
 _gtk_tree_path_list_free (GList *list)
 {
diff --git a/gthumb/gtk-utils.h b/gthumb/gtk-utils.h
index fa702cd..5c65cd2 100644
--- a/gthumb/gtk-utils.h
+++ b/gthumb/gtk-utils.h
@@ -80,13 +80,11 @@ void            _gtk_info_dialog_run                       (GtkWindow        *pa
 					    	    	    const gchar      *format,
 					    	    	    ...) G_GNUC_PRINTF (2, 3);
 GdkPixbuf *     _g_icon_get_pixbuf                         (GIcon            *icon,
-		 			    	    	    int               size,
+		 			    	    	    int               icon_size,
 		 			    	    	    GtkIconTheme     *icon_theme);
 GdkPixbuf *     get_mime_type_pixbuf                       (const char       *mime_type,
 					    	    	    int               icon_size,
 					    	    	    GtkIconTheme     *icon_theme);
-int             _gtk_icon_get_pixel_size                   (GtkWidget        *widget,
-					    	    	    GtkIconSize       size);
 void            show_help_dialog                           (GtkWindow        *parent,
 					    	    	    const char       *section);
 void            _gtk_container_remove_children             (GtkContainer     *container,
@@ -111,6 +109,8 @@ void            _gtk_widget_get_screen_size                (GtkWidget        *wi
 					    	    	    int              *height);
 int             _gtk_widget_get_allocated_width            (GtkWidget        *widget);
 int             _gtk_widget_get_allocated_height           (GtkWidget        *widget);
+int             _gtk_widget_lookup_for_size                (GtkWidget        *widget,
+							    GtkIconSize       icon_size);
 void            _gtk_tree_path_list_free                   (GList            *list);
 int             _gtk_paned_get_position2                   (GtkPaned         *paned);
 void            _gtk_paned_set_position2                   (GtkPaned         *paned,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]