[Xfce4-commits] <midori:master> Refactor loading cached page icon into katze_load_cached_icon
Christian Dywan
noreply at xfce.org
Tue Nov 17 23:44:01 CET 2009
Updating branch refs/heads/master
to 9eefb25bb6f6d0994e36bead4c8a14daccab6c0a (commit)
from ba70a6fd5ab2b2278c3a5c6984911043bc5f3e9c (commit)
commit 9eefb25bb6f6d0994e36bead4c8a14daccab6c0a
Author: Christian Dywan <christian at twotoasts.de>
Date: Tue Nov 17 23:22:08 2009 +0100
Refactor loading cached page icon into katze_load_cached_icon
The function is optimized for loading icons and independant from
a KatzeNet instance.
extensions/feed-panel/feed-panel.c | 2 +-
katze/katze-arrayaction.c | 12 ++-----
katze/katze-utils.c | 59 ++++++++++++++++++++++++++++++++++++
katze/katze-utils.h | 4 ++
midori/midori-locationaction.c | 7 ++--
midori/midori-searchaction.c | 2 +-
panels/midori-bookmarks.c | 4 +--
panels/midori-history.c | 4 +--
8 files changed, 74 insertions(+), 20 deletions(-)
diff --git a/extensions/feed-panel/feed-panel.c b/extensions/feed-panel/feed-panel.c
index e3a7da4..eacb56a 100644
--- a/extensions/feed-panel/feed-panel.c
+++ b/extensions/feed-panel/feed-panel.c
@@ -91,7 +91,7 @@ feed_panel_treeview_render_icon_cb (GtkTreeViewColumn* column,
uri = katze_item_get_uri (pitem);
if (uri)
{
- pixbuf = katze_net_load_icon (panel->net, uri, NULL, NULL, NULL);
+ pixbuf = katze_load_cached_icon (uri, NULL);
if (!pixbuf)
pixbuf = panel->pixbuf;
}
diff --git a/katze/katze-arrayaction.c b/katze/katze-arrayaction.c
index db17b68..d36095e 100644
--- a/katze/katze-arrayaction.c
+++ b/katze/katze-arrayaction.c
@@ -301,8 +301,7 @@ katze_array_action_generate_menu (KatzeArrayAction* array_action,
icon = gtk_widget_render_icon (menuitem,
GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
else
- icon = katze_net_load_icon (array_action->net,
- katze_item_get_uri (item), NULL, proxy, NULL);
+ icon = katze_load_cached_icon (katze_item_get_uri (item), proxy);
image = gtk_image_new_from_pixbuf (icon);
g_object_unref (icon);
}
@@ -460,8 +459,7 @@ katze_array_action_item_notify_cb (KatzeItem* item,
}
else if (!KATZE_IS_ARRAY (item) && !strcmp (property, "uri"))
{
- icon = katze_net_load_icon (array_action->net, katze_item_get_uri (item),
- NULL, GTK_WIDGET (toolitem), NULL);
+ icon = katze_load_cached_icon (katze_item_get_uri (item), GTK_WIDGET (toolitem));
image = gtk_image_new_from_pixbuf (icon);
g_object_unref (icon);
gtk_widget_show (image);
@@ -497,8 +495,7 @@ katze_array_action_proxy_create_menu_proxy_cb (GtkWidget* proxy,
icon = gtk_widget_render_icon (menuitem,
GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
else
- icon = katze_net_load_icon (array_action->net,
- katze_item_get_uri (item), NULL, proxy, NULL);
+ icon = katze_load_cached_icon (katze_item_get_uri (item), proxy);
image = gtk_image_new_from_pixbuf (icon);
g_object_unref (icon);
}
@@ -569,8 +566,7 @@ katze_array_action_create_tool_item_for (KatzeArrayAction* array_action,
icon = gtk_widget_render_icon (GTK_WIDGET (toolitem),
GTK_STOCK_DIRECTORY, GTK_ICON_SIZE_MENU, NULL);
else
- icon = katze_net_load_icon (array_action->net, uri,
- NULL, GTK_WIDGET (toolitem), NULL);
+ icon = katze_load_cached_icon (uri, GTK_WIDGET (toolitem));
image = gtk_image_new_from_pixbuf (icon);
g_object_unref (icon);
gtk_widget_show (image);
diff --git a/katze/katze-utils.c b/katze/katze-utils.c
index d73041c..b0cffc9 100644
--- a/katze/katze-utils.c
+++ b/katze/katze-utils.c
@@ -14,6 +14,7 @@
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
+#include <libsoup/soup.h>
#include <string.h>
@@ -1402,3 +1403,61 @@ katze_widget_has_touchscreen_mode (GtkWidget* widget)
return enabled;
}
}
+
+/**
+ * katze_load_cached_icon:
+ * @uri: an URI string
+ * @widget: a #GtkWidget, or %NULL
+ *
+ * Loads a cached icon for the specified @uri. If there is no
+ * icon and @widget is specified, a default will be returned.
+ *
+ * Returns: a #GdkPixbuf, or %NULL
+ *
+ * Since: 0.2.2
+ */
+GdkPixbuf*
+katze_load_cached_icon (const gchar* uri,
+ GtkWidget* widget)
+{
+ GdkPixbuf* icon = NULL;
+
+ if (g_str_has_prefix (uri, "http://"))
+ {
+ guint i;
+ gchar* icon_uri;
+ gchar* checksum;
+ gchar* ext;
+ gchar* filename;
+ gchar* path;
+
+ i = 8;
+ while (uri[i] != '\0' && uri[i] != '/')
+ i++;
+ if (uri[i] == '/')
+ {
+ icon_uri = g_strdup (uri);
+ icon_uri[i] = '\0';
+ icon_uri = g_strdup_printf ("%s/favicon.ico", icon_uri);
+ }
+ else
+ icon_uri = g_strdup_printf ("%s/favicon.ico", uri);
+
+ checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, icon_uri, -1);
+ ext = g_strrstr (icon_uri, ".");
+ g_free (icon_uri);
+ filename = g_strdup_printf ("%s%s", checksum, ext ? ext : "");
+ g_free (checksum);
+ path = g_build_filename (g_get_user_cache_dir (), PACKAGE_NAME,
+ "icons", filename, NULL);
+ if ((icon = gdk_pixbuf_new_from_file_at_size (path, 16, 16, NULL)))
+ {
+ g_free (path);
+ return icon;
+ }
+ g_free (path);
+ }
+
+ return icon || !widget ? icon : gtk_widget_render_icon (widget,
+ GTK_STOCK_FILE, GTK_ICON_SIZE_MENU, NULL);
+}
diff --git a/katze/katze-utils.h b/katze/katze-utils.h
index 3b226ee..c8648ff 100644
--- a/katze/katze-utils.h
+++ b/katze/katze-utils.h
@@ -147,6 +147,10 @@ katze_mkdir_with_parents (const gchar* pathname,
gboolean
katze_widget_has_touchscreen_mode (GtkWidget* widget);
+GdkPixbuf*
+katze_load_cached_icon (const gchar* uri,
+ GtkWidget* widget);
+
G_END_DECLS
#endif /* __KATZE_UTILS_H__ */
diff --git a/midori/midori-locationaction.c b/midori/midori-locationaction.c
index da9acf3..7d30ac6 100644
--- a/midori/midori-locationaction.c
+++ b/midori/midori-locationaction.c
@@ -642,13 +642,12 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout,
gtk_tree_model_get (model, iter, URI_COL, &uri, TITLE_COL, &title,
FAVICON_COL, &icon, -1);
- if (G_UNLIKELY (!icon))
+ if (G_UNLIKELY (!icon) && uri)
{
#if !HAVE_HILDON
MidoriLocationAction* action
= g_object_get_data (G_OBJECT (renderer), "location-action");
- icon = katze_net_load_icon (action->net, uri, NULL, NULL, NULL);
- if (G_LIKELY (icon))
+ if ((icon = katze_load_cached_icon (uri, NULL)))
{
midori_location_action_set_icon_for_uri (action, icon, uri);
g_object_unref (icon);
@@ -657,7 +656,7 @@ midori_location_entry_render_text_cb (GtkCellLayout* layout,
midori_location_action_set_icon_for_uri (action, action->default_icon, uri);
#endif
}
- else
+ else if (icon)
g_object_unref (icon);
desc = desc_uri = desc_title = key = NULL;
diff --git a/midori/midori-searchaction.c b/midori/midori-searchaction.c
index 94a4516..9305cb1 100644
--- a/midori/midori-searchaction.c
+++ b/midori/midori-searchaction.c
@@ -432,7 +432,7 @@ midori_search_action_get_icon (KatzeNet* net,
}
if ((icon = katze_item_get_uri (item)) && (g_strstr_len (icon, 8, "://")))
- return katze_net_load_icon (net, icon, NULL, widget, NULL);
+ return katze_load_cached_icon (icon, widget);
return gtk_widget_render_icon (widget, GTK_STOCK_FILE,
GTK_ICON_SIZE_MENU, NULL);
diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c
index 97b3a00..dc12c34 100644
--- a/panels/midori-bookmarks.c
+++ b/panels/midori-bookmarks.c
@@ -563,9 +563,7 @@ midori_bookmarks_treeview_render_icon_cb (GtkTreeViewColumn* column,
pixbuf = gtk_widget_render_icon (treeview, GTK_STOCK_DIRECTORY,
GTK_ICON_SIZE_MENU, NULL);
else if (katze_item_get_uri (item))
- pixbuf = katze_net_load_icon (
- MIDORI_BOOKMARKS (gtk_widget_get_parent (treeview))->net,
- katze_item_get_uri (item), NULL, treeview, NULL);
+ pixbuf = katze_load_cached_icon (katze_item_get_uri (item), treeview);
g_object_set (renderer, "pixbuf", pixbuf, NULL);
if (pixbuf)
diff --git a/panels/midori-history.c b/panels/midori-history.c
index eefb5ed..fcacb62 100644
--- a/panels/midori-history.c
+++ b/panels/midori-history.c
@@ -560,9 +560,7 @@ midori_history_treeview_render_icon_cb (GtkTreeViewColumn* column,
pixbuf = gtk_widget_render_icon (treeview, GTK_STOCK_DIRECTORY,
GTK_ICON_SIZE_MENU, NULL);
else
- pixbuf = katze_net_load_icon (
- MIDORI_HISTORY (gtk_widget_get_parent (treeview))->net,
- katze_item_get_uri (item), NULL, treeview, NULL);
+ pixbuf = katze_load_cached_icon (katze_item_get_uri (item), treeview);
g_object_set (renderer, "pixbuf", pixbuf, NULL);
More information about the Xfce4-commits
mailing list