From 594097cc0181cfea7e8205448a7b6e315e311a36 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 11 Jan 2003 20:12:48 +0000 Subject: Reimplement favicons. Now all exit crashes related to connections left 2003-01-11 Marco Pesenti Gritti * embed/Makefile.am: * embed/ephy-embed-favicon.c: * embed/ephy-embed-favicon.h: * embed/ephy-embed-shell.c: (ephy_embed_shell_get_favicon_cache): * embed/ephy-favicon-cache.c: (ephy_favicon_cache_class_init), (ephy_favicon_cache_new), (ephy_favicon_cache_load), (icon_is_obsolete), (icons_added_cb), (icons_removed_cb), (remove_obsolete_icons), (ephy_favicon_cache_save), (ephy_favicon_cache_init), (kill_download), (cleanup_downloads_hash), (ephy_favicon_cache_finalize), (favicon_name_build), (favicon_download_completed_cb), (ephy_favicon_cache_download), (ephy_favicon_cache_get): * embed/ephy-favicon-cache.h: * embed/ephy-favicon.c: * embed/ephy-favicon.h: * embed/mozilla/mozilla-embed-shell.cpp: * src/ephy-tab.c: (ephy_tab_init), (ephy_tab_favicon_cb), (ephy_tab_location_cb), (ephy_tab_get_location), (ephy_tab_get_favicon_url): * src/ephy-tab.h: * src/ephy-window.c: (update_favicon_control): * src/toolbar.c: (toolbar_setup_favicon_ebox), (toolbar_update_favicon): Reimplement favicons. Now all exit crashes related to connections left open by favicons should be fixed. --- embed/ephy-favicon-cache.c | 493 ++++++++++++++++++++++++++++----------------- 1 file changed, 312 insertions(+), 181 deletions(-) (limited to 'embed/ephy-favicon-cache.c') diff --git a/embed/ephy-favicon-cache.c b/embed/ephy-favicon-cache.c index d289fd4ab..029470d26 100644 --- a/embed/ephy-favicon-cache.c +++ b/embed/ephy-favicon-cache.c @@ -16,34 +16,33 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include #include +#include #include -#include -#include +#include #include #include "ephy-embed-persist.h" #include "ephy-file-helpers.h" #include "ephy-favicon-cache.h" +#include "ephy-node.h" + +#define EPHY_FAVICON_CACHE_XML_VERSION "0.1" + +#define EPHY_FAVICON_CACHE_OBSOLETE_DAYS 30 static void ephy_favicon_cache_class_init (EphyFaviconCacheClass *klass); static void ephy_favicon_cache_init (EphyFaviconCache *ma); static void ephy_favicon_cache_finalize (GObject *object); -static void ephy_favicon_cache_insert (EphyFaviconCache *cache, - const char *url, - const char *pixbuf_location); -static char *ephy_favicon_cache_dest (EphyFaviconCache *cache, - const char *url); -static void favicon_download_completed_cb (EphyEmbedPersist *persist, - EphyFaviconCache *cache); struct EphyFaviconCachePrivate { char *directory; - - GdkPixbuf *default_pixbuf; - EphyHistory *history; + char *xml_file; + EphyNode *icons; + GHashTable *icons_hash; + GStaticRWLock *icons_hash_lock; + GHashTable *downloads_hash; }; enum @@ -54,13 +53,7 @@ enum enum { - PROP_0, - PROP_HISTORY -}; - -enum -{ - EPHY_NODE_PAGE_PROP_FAVICON = 100 + ICONS_NODE_ID = 9, }; static guint ephy_favicon_cache_signals[LAST_SIGNAL] = { 0 }; @@ -95,43 +88,6 @@ ephy_favicon_cache_get_type (void) return ephy_favicon_cache_type; } -static void -ephy_favicon_cache_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - EphyFaviconCache *cache = EPHY_FAVICON_CACHE (object); - - switch (prop_id) - { - case PROP_HISTORY: - cache->priv->history = g_value_get_object (value); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -ephy_favicon_cache_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - EphyFaviconCache *cache = EPHY_FAVICON_CACHE (object); - - switch (prop_id) - { - case PROP_HISTORY: - g_value_set_object (value, cache->priv->history); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} static void ephy_favicon_cache_class_init (EphyFaviconCacheClass *klass) { @@ -140,17 +96,6 @@ ephy_favicon_cache_class_init (EphyFaviconCacheClass *klass) parent_class = g_type_class_peek_parent (klass); object_class->finalize = ephy_favicon_cache_finalize; - object_class->set_property = ephy_favicon_cache_set_property; - object_class->get_property = ephy_favicon_cache_get_property; - - g_object_class_install_property (object_class, - PROP_HISTORY, - g_param_spec_object ("History", - "Source history", - "Source history", - EPHY_HISTORY_TYPE, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - ephy_favicon_cache_signals[CHANGED] = g_signal_new ("changed", @@ -164,180 +109,297 @@ ephy_favicon_cache_class_init (EphyFaviconCacheClass *klass) G_TYPE_STRING); } +EphyFaviconCache * +ephy_favicon_cache_new (void) +{ + EphyFaviconCache *cache; + + cache = EPHY_FAVICON_CACHE (g_object_new (EPHY_TYPE_FAVICON_CACHE, NULL)); + + g_return_val_if_fail (cache->priv != NULL, NULL); + + return cache; +} + static void -ephy_favicon_cache_init (EphyFaviconCache *cache) +ephy_favicon_cache_load (EphyFaviconCache *eb) { - GtkWidget *dummy; + xmlDocPtr doc; + xmlNodePtr root, child; + char *tmp; - cache->priv = g_new0 (EphyFaviconCachePrivate, 1); + if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE) + return; - cache->priv->directory = g_build_filename (ephy_dot_dir (), - "favicon_cache/", - NULL); + doc = xmlParseFile (eb->priv->xml_file); + g_assert (doc != NULL); - if (g_file_test (cache->priv->directory, G_FILE_TEST_IS_DIR) == FALSE) + root = xmlDocGetRootElement (doc); + + tmp = xmlGetProp (root, "version"); + g_assert (tmp != NULL && strcmp (tmp, EPHY_FAVICON_CACHE_XML_VERSION) == 0); + g_free (tmp); + + for (child = root->children; child != NULL; child = child->next) { - if (g_file_test (cache->priv->directory, G_FILE_TEST_EXISTS)) - { - g_error ("Please remove %s to continue.", cache->priv->directory); - } + EphyNode *node; - if (mkdir (cache->priv->directory, 488) != 0) - { - g_error ("Couldn't mkdir %s.", cache->priv->directory); - } + node = ephy_node_new_from_xml (child); } - dummy = gtk_toolbar_new (); - cache->priv->default_pixbuf = gtk_widget_render_icon (dummy, - GTK_STOCK_JUMP_TO, - GTK_ICON_SIZE_MENU, NULL); - gtk_widget_destroy (dummy); + xmlFreeDoc (doc); } -static void -ephy_favicon_cache_finalize (GObject *object) +static gboolean +icon_is_obsolete (EphyNode *node, GDate *now) { - EphyFaviconCache *cache; + int last_visit; + GDate date; - g_return_if_fail (object != NULL); - g_return_if_fail (EPHY_IS_FAVICON_CACHE (object)); + last_visit = ephy_node_get_property_int + (node, EPHY_NODE_FAVICON_PROP_LAST_USED); - cache = EPHY_FAVICON_CACHE (object); + g_date_clear (&date, 1); + g_date_set_time (&date, last_visit); - g_return_if_fail (cache->priv != NULL); + return (g_date_days_between (&date, now) >= + EPHY_FAVICON_CACHE_OBSOLETE_DAYS); +} + +static void +icons_added_cb (EphyNode *node, + EphyNode *child, + EphyFaviconCache *eb) +{ + g_static_rw_lock_writer_lock (eb->priv->icons_hash_lock); - g_object_unref (G_OBJECT (cache->priv->default_pixbuf)); + g_hash_table_insert (eb->priv->icons_hash, + (char *) ephy_node_get_property_string (child, EPHY_NODE_FAVICON_PROP_URL), + child); - g_object_unref (cache->priv->history); + g_static_rw_lock_writer_unlock (eb->priv->icons_hash_lock); +} - g_free (cache->priv->directory); +static void +icons_removed_cb (EphyNode *node, + EphyNode *child, + EphyFaviconCache *eb) +{ + g_static_rw_lock_writer_lock (eb->priv->icons_hash_lock); - g_free (cache->priv); + g_hash_table_remove (eb->priv->icons_hash, + ephy_node_get_property_string (child, EPHY_NODE_FAVICON_PROP_URL)); - G_OBJECT_CLASS (parent_class)->finalize (object); + g_static_rw_lock_writer_unlock (eb->priv->icons_hash_lock); } -EphyFaviconCache * -ephy_favicon_cache_new (EphyHistory *history) +static void +remove_obsolete_icons (EphyFaviconCache *eb) { - EphyFaviconCache *cache; - - cache = EPHY_FAVICON_CACHE (g_object_new (EPHY_TYPE_FAVICON_CACHE, - "History", history, - NULL)); + GPtrArray *children; + int i; + GTime now; + GDate current_date; + + now = time (NULL); + g_date_clear (¤t_date, 1); + g_date_set_time (¤t_date, time (NULL)); + + children = ephy_node_get_children (eb->priv->icons); + ephy_node_thaw (eb->priv->icons); + for (i = 0; i < children->len; i++) + { + EphyNode *kid; - g_return_val_if_fail (cache->priv != NULL, NULL); + kid = g_ptr_array_index (children, i); - return cache; + if (icon_is_obsolete (kid, ¤t_date)) + { + const char *filename; + const char *path; + + filename = ephy_node_get_property_string + (kid, EPHY_NODE_FAVICON_PROP_FILENAME); + path = g_build_filename (eb->priv->directory, + filename, NULL); + gnome_vfs_unlink (path); + g_object_unref (kid); + } + } } -GdkPixbuf * -ephy_favicon_cache_lookup (EphyFaviconCache *cache, - const char *url) +static void +ephy_favicon_cache_save (EphyFaviconCache *eb) { - GdkPixbuf *ret; + xmlDocPtr doc; + xmlNodePtr root; + GPtrArray *children; + int i; - g_return_val_if_fail (EPHY_IS_FAVICON_CACHE (cache), NULL); + /* save nodes to xml */ + xmlIndentTreeOutput = TRUE; + doc = xmlNewDoc ("1.0"); - if (url == NULL) + root = xmlNewDocNode (doc, NULL, "ephy_favicons_cache", NULL); + xmlSetProp (root, "version", EPHY_FAVICON_CACHE_XML_VERSION); + xmlDocSetRootElement (doc, root); + + children = ephy_node_get_children (eb->priv->icons); + for (i = 0; i < children->len; i++) { - return cache->priv->default_pixbuf; + EphyNode *kid; + + kid = g_ptr_array_index (children, i); + + ephy_node_save_to_xml (kid, root); } + ephy_node_thaw (eb->priv->icons); + + xmlSaveFormatFile (eb->priv->xml_file, doc, 1); +} + +static void +ephy_favicon_cache_init (EphyFaviconCache *cache) +{ + cache->priv = g_new0 (EphyFaviconCachePrivate, 1); - ret = ephy_favicon_cache_lookup_direct (cache, url); + cache->priv->xml_file = g_build_filename (ephy_dot_dir (), + "ephy-favicon-cache.xml", + NULL); - if (ret == NULL) + cache->priv->directory = g_build_filename (ephy_dot_dir (), + "favicon_cache/", + NULL); + + if (g_file_test (cache->priv->directory, G_FILE_TEST_IS_DIR) == FALSE) { - return cache->priv->default_pixbuf; + if (mkdir (cache->priv->directory, 488) != 0) + { + g_error ("Couldn't mkdir %s.", cache->priv->directory); + } } - return ret; + cache->priv->icons_hash = g_hash_table_new (g_str_hash, + g_str_equal); + cache->priv->icons_hash_lock = g_new0 (GStaticRWLock, 1); + g_static_rw_lock_init (cache->priv->icons_hash_lock); + cache->priv->downloads_hash = g_hash_table_new (g_str_hash, + g_str_equal); + + /* Icons */ + cache->priv->icons = ephy_node_new_with_id (ICONS_NODE_ID); + ephy_node_ref (cache->priv->icons); + g_signal_connect_object (G_OBJECT (cache->priv->icons), + "child_added", + G_CALLBACK (icons_added_cb), + G_OBJECT (cache), + 0); + g_signal_connect_object (G_OBJECT (cache->priv->icons), + "child_removed", + G_CALLBACK (icons_removed_cb), + G_OBJECT (cache), + 0); + + ephy_favicon_cache_load (cache); } -GdkPixbuf * -ephy_favicon_cache_lookup_direct (EphyFaviconCache *cache, - const char *cache_url) +static gboolean +kill_download (gpointer key, + gpointer value, + gpointer data) { - GdkPixbuf *pixbuf; - EphyNode *node; - const char *pix_file; + EphyEmbedPersist *persist = EPHY_EMBED_PERSIST (value); + EphyFaviconCache *cache = EPHY_FAVICON_CACHE (data); + EphyNode *icon; - node = ephy_history_get_page (cache->priv->history, cache_url); - if (node == NULL) return NULL; + ephy_embed_persist_cancel (persist); + g_object_unref (persist); - pix_file = ephy_node_get_property_string - (node, EPHY_NODE_PAGE_PROP_FAVICON); - if (pix_file == NULL) return NULL; + g_static_rw_lock_reader_lock (cache->priv->icons_hash_lock); + icon = g_hash_table_lookup (cache->priv->icons_hash, (char *)key); + g_static_rw_lock_reader_unlock (cache->priv->icons_hash_lock); - pixbuf = gdk_pixbuf_new_from_file (pix_file, NULL); - g_return_val_if_fail (pixbuf != NULL, NULL); + g_object_unref (icon); - if (gdk_pixbuf_get_width (pixbuf) > 16 || - gdk_pixbuf_get_height (pixbuf) > 16) - { - GdkPixbuf *scaled = gdk_pixbuf_scale_simple (pixbuf, 16, 16, - GDK_INTERP_NEAREST); - g_object_unref (G_OBJECT (pixbuf)); - pixbuf = scaled; - } + return TRUE; +} - return pixbuf; + +static void +cleanup_downloads_hash (EphyFaviconCache *cache) +{ + g_hash_table_foreach_remove (cache->priv->downloads_hash, + kill_download, cache); } static void -ephy_favicon_cache_insert (EphyFaviconCache *cache, - const char *url, - const char *pixbuf_location) +ephy_favicon_cache_finalize (GObject *object) { - EphyNode *node; - GValue value = { 0, }; + EphyFaviconCache *cache; - node = ephy_history_get_page (cache->priv->history, url); - g_return_if_fail (node != NULL); + g_return_if_fail (object != NULL); + g_return_if_fail (EPHY_IS_FAVICON_CACHE (object)); - g_value_init (&value, G_TYPE_STRING); - g_value_set_string (&value, pixbuf_location); - ephy_node_set_property (node, EPHY_NODE_PAGE_PROP_FAVICON, - &value); - g_value_unset (&value); + cache = EPHY_FAVICON_CACHE (object); - g_signal_emit (G_OBJECT (cache), ephy_favicon_cache_signals[CHANGED], 0, url); + g_return_if_fail (cache->priv != NULL); + + cleanup_downloads_hash (cache); + remove_obsolete_icons (cache); + ephy_favicon_cache_save (cache); + + g_free (cache->priv->xml_file); + g_free (cache->priv->directory); + g_hash_table_destroy (cache->priv->icons_hash); + g_static_rw_lock_free (cache->priv->icons_hash_lock); + g_hash_table_destroy (cache->priv->downloads_hash); + + g_free (cache->priv); + + G_OBJECT_CLASS (parent_class)->finalize (object); } static char * -ephy_favicon_cache_dest (EphyFaviconCache *cache, const char *url) +favicon_name_build (const char *url) { - char *slashpos, *dest, *my_url; + char *res; + char *slashpos; - my_url = g_strdup (url); + res = g_strdup (url); - while ((slashpos = strstr (my_url, "/")) != NULL) + while ((slashpos = strstr (res, "/")) != NULL) *slashpos = '_'; - dest = g_build_filename (cache->priv->directory, my_url, NULL); + return res; +} + +static void +favicon_download_completed_cb (EphyEmbedPersist *persist, + EphyFaviconCache *cache) +{ + char *url; + + url = g_object_get_data (G_OBJECT (persist), "url"), - g_free (my_url); + g_hash_table_remove (cache->priv->downloads_hash, url); + g_object_unref (persist); - return dest; + g_signal_emit (G_OBJECT (cache), ephy_favicon_cache_signals[CHANGED], 0, url); } -void -ephy_favicon_cache_insert_from_url (EphyFaviconCache *cache, - const char *url, - const char *favicon_url) +static void +ephy_favicon_cache_download (EphyFaviconCache *cache, + const char *favicon_url, + const char *filename) { EphyEmbedPersist *persist; - char *dest; + const char *dest; g_return_if_fail (EPHY_IS_FAVICON_CACHE (cache)); - g_return_if_fail (url != NULL); g_return_if_fail (favicon_url != NULL); + g_return_if_fail (filename != NULL); - dest = ephy_favicon_cache_dest (cache, favicon_url); - g_return_if_fail (dest != NULL); - - if (g_file_test (dest, G_FILE_TEST_EXISTS)) return; + dest = g_build_filename (cache->priv->directory, filename, NULL); persist = ephy_embed_persist_new (NULL); @@ -346,8 +408,8 @@ ephy_favicon_cache_insert_from_url (EphyFaviconCache *cache, ephy_embed_persist_set_source (persist, favicon_url); ephy_embed_persist_set_dest (persist, dest); - g_object_set_data_full (G_OBJECT (persist), "url", g_strdup (url), g_free); - g_object_set_data_full (G_OBJECT (persist), "favicon", dest, g_free); + g_object_set_data_full (G_OBJECT (persist), "url", + g_strdup (favicon_url), g_free); g_signal_connect (G_OBJECT (persist), "completed", @@ -355,15 +417,84 @@ ephy_favicon_cache_insert_from_url (EphyFaviconCache *cache, cache); ephy_embed_persist_save (persist); + + g_hash_table_insert (cache->priv->downloads_hash, + g_strdup (favicon_url), persist); } -static void -favicon_download_completed_cb (EphyEmbedPersist *persist, - EphyFaviconCache *cache) +GdkPixbuf * +ephy_favicon_cache_get (EphyFaviconCache *cache, + const char *url) { - ephy_favicon_cache_insert (cache, - g_object_get_data (G_OBJECT (persist), "url"), - g_object_get_data (G_OBJECT (persist), "favicon")); + GTime now; + EphyNode *icon; + GValue value = { 0, }; + const char *pix_file; + GdkPixbuf *pixbuf; + + now = time (NULL); + + g_static_rw_lock_reader_lock (cache->priv->icons_hash_lock); + icon = g_hash_table_lookup (cache->priv->icons_hash, url); + g_static_rw_lock_reader_unlock (cache->priv->icons_hash_lock); + + if (!icon) + { + char *filename; + + filename = favicon_name_build (url); + + icon = ephy_node_new (); + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, url); + ephy_node_set_property (icon, EPHY_NODE_FAVICON_PROP_URL, + &value); + g_value_unset (&value); + + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, filename); + ephy_node_set_property (icon, EPHY_NODE_FAVICON_PROP_FILENAME, + &value); + g_value_unset (&value); + + ephy_node_add_child (cache->priv->icons, icon); + + ephy_favicon_cache_download (cache, url, filename); - g_object_unref (G_OBJECT (persist)); + g_free (filename); + } + + g_value_init (&value, G_TYPE_INT); + g_value_set_int (&value, now); + ephy_node_set_property (icon, EPHY_NODE_FAVICON_PROP_LAST_USED, + &value); + + if (g_hash_table_lookup (cache->priv->downloads_hash, url) != NULL) + { + /* still downloading, return NULL */ + return NULL; + } + + pix_file = g_build_filename + (cache->priv->directory, + ephy_node_get_property_string (icon, EPHY_NODE_FAVICON_PROP_FILENAME), + NULL); + + g_hash_table_lookup (cache->priv->icons_hash, url); + + pixbuf = gdk_pixbuf_new_from_file (pix_file, NULL); + + if (pixbuf && + (gdk_pixbuf_get_width (pixbuf) > 16 || + gdk_pixbuf_get_height (pixbuf) > 16)) + { + GdkPixbuf *scaled = gdk_pixbuf_scale_simple (pixbuf, 16, 16, + GDK_INTERP_NEAREST); + g_object_unref (G_OBJECT (pixbuf)); + pixbuf = scaled; + } + + return pixbuf; } + + -- cgit v1.2.3