diff options
Diffstat (limited to 'embed')
-rw-r--r-- | embed/Makefile.am | 4 | ||||
-rw-r--r-- | embed/ephy-embed-favicon.c | 251 | ||||
-rw-r--r-- | embed/ephy-embed-favicon.h | 63 | ||||
-rw-r--r-- | embed/ephy-embed-shell.c | 5 | ||||
-rw-r--r-- | embed/ephy-favicon-cache.c | 493 | ||||
-rw-r--r-- | embed/ephy-favicon-cache.h | 20 | ||||
-rw-r--r-- | embed/ephy-favicon.c | 280 | ||||
-rw-r--r-- | embed/ephy-favicon.h | 64 | ||||
-rw-r--r-- | embed/mozilla/mozilla-embed-shell.cpp | 6 |
9 files changed, 326 insertions, 860 deletions
diff --git a/embed/Makefile.am b/embed/Makefile.am index 2cf70c57c..4a1568ec9 100644 --- a/embed/Makefile.am +++ b/embed/Makefile.am @@ -25,8 +25,6 @@ libephyembed_la_SOURCES = \ ephy-embed-dialog.h \ ephy-embed-event.c \ ephy-embed-event.h \ - ephy-embed-favicon.c \ - ephy-embed-favicon.h \ ephy-embed-persist.c \ ephy-embed-persist.h \ ephy-embed-popup.c \ @@ -41,8 +39,6 @@ libephyembed_la_SOURCES = \ ephy-embed-types.h \ ephy-embed-utils.c \ ephy-embed-utils.h \ - ephy-favicon.c \ - ephy-favicon.h \ ephy-favicon-cache.c \ ephy-favicon-cache.h \ ephy-history.c \ diff --git a/embed/ephy-embed-favicon.c b/embed/ephy-embed-favicon.c deleted file mode 100644 index 7afbbd831..000000000 --- a/embed/ephy-embed-favicon.c +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.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 - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include <string.h> - -#include "ephy-embed-favicon.h" -#include "ephy-embed-shell.h" - -static void ephy_embed_favicon_class_init (EphyEmbedFaviconClass *klass); -static void ephy_embed_favicon_init (EphyEmbedFavicon *ma); -static void ephy_embed_favicon_finalize (GObject *object); -static void ephy_embed_favicon_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void ephy_embed_favicon_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); - -struct EphyEmbedFaviconPrivate -{ - EphyEmbed *embed; -}; - -enum -{ - PROP_0, - PROP_EMBED -}; - -static GObjectClass *parent_class = NULL; - -GType -ephy_embed_favicon_get_type (void) -{ - static GType ephy_embed_favicon_type = 0; - - if (ephy_embed_favicon_type == 0) - { - static const GTypeInfo our_info = - { - sizeof (EphyEmbedFaviconClass), - NULL, - NULL, - (GClassInitFunc) ephy_embed_favicon_class_init, - NULL, - NULL, - sizeof (EphyEmbedFavicon), - 0, - (GInstanceInitFunc) ephy_embed_favicon_init - }; - - ephy_embed_favicon_type = g_type_register_static (EPHY_TYPE_FAVICON, - "EphyEmbedFavicon", - &our_info, 0); - } - - return ephy_embed_favicon_type; -} - -static void -ephy_embed_favicon_class_init (EphyEmbedFaviconClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - parent_class = g_type_class_peek_parent (klass); - - object_class->finalize = ephy_embed_favicon_finalize; - - object_class->set_property = ephy_embed_favicon_set_property; - object_class->get_property = ephy_embed_favicon_get_property; - - g_object_class_install_property (object_class, - PROP_EMBED, - g_param_spec_object ("embed", - "Associated embed", - "Associated embed", - G_TYPE_OBJECT, - G_PARAM_READWRITE)); -} - -static void -ephy_embed_favicon_init (EphyEmbedFavicon *ma) -{ - ma->priv = g_new0 (EphyEmbedFaviconPrivate, 1); -} - -static void -ephy_embed_favicon_finalize (GObject *object) -{ - EphyEmbedFavicon *ma; - - g_return_if_fail (object != NULL); - g_return_if_fail (EPHY_IS_EMBED_FAVICON (object)); - - ma = EPHY_EMBED_FAVICON (object); - - g_return_if_fail (ma->priv != NULL); - - g_free (ma->priv); - - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -static void -update_url (EphyEmbedFavicon *favicon) -{ - char *location; - - ephy_embed_get_location (favicon->priv->embed, - TRUE, &location); - - if (location) - { - ephy_favicon_set_url (EPHY_FAVICON (favicon), location); - g_free (location); - } -} - -static void -favicon_cb (EphyEmbed *embed, - const char *favicon_url, - EphyEmbedFavicon *favicon) -{ - char *url = NULL; - EphyFaviconCache *cache; - - if (favicon->priv->embed == NULL) - return; - - ephy_embed_get_location (favicon->priv->embed, TRUE, &url); - - g_object_get (G_OBJECT (favicon), - "cache", &cache, - NULL); - - ephy_favicon_cache_insert_from_url (cache, - url, - favicon_url); - - g_object_unref (cache); - - g_free (url); -} - -static void -ephy_embed_favicon_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - EphyEmbedFavicon *favicon = EPHY_EMBED_FAVICON (object); - - switch (prop_id) - { - case PROP_EMBED: - favicon->priv->embed = g_value_get_object (value); - - if (favicon->priv->embed != NULL) - { - g_signal_connect_object (G_OBJECT (favicon->priv->embed), - "ge_favicon", - G_CALLBACK (favicon_cb), - favicon, - 0); - update_url (favicon); - } - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -ephy_embed_favicon_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - EphyEmbedFavicon *favicon = EPHY_EMBED_FAVICON (object); - - switch (prop_id) - { - case PROP_EMBED: - g_value_set_object (value, favicon->priv->embed); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -GtkWidget * -ephy_embed_favicon_new (EphyEmbed *embed) -{ - EphyEmbedFavicon *favicon; - EphyFaviconCache *cache = ephy_embed_shell_get_favicon_cache (embed_shell); - - g_return_val_if_fail (EPHY_IS_FAVICON_CACHE (cache), NULL); - - favicon = EPHY_EMBED_FAVICON (g_object_new (EPHY_TYPE_EMBED_FAVICON, - "cache", cache, - "embed", embed, - NULL)); - - g_return_val_if_fail (favicon->priv != NULL, NULL); - - return GTK_WIDGET (favicon); -} - -void -ephy_embed_favicon_set_embed (EphyEmbedFavicon *favicon, - EphyEmbed *embed) -{ - g_return_if_fail (EPHY_IS_EMBED_FAVICON (favicon)); - - g_object_set (G_OBJECT (favicon), - "embed", embed, - NULL); -} - -EphyEmbed * -ephy_embed_favicon_get_embed (EphyEmbedFavicon *favicon) -{ - EphyEmbed *embed; - - g_return_val_if_fail (EPHY_IS_EMBED_FAVICON (favicon), NULL); - - g_object_get (G_OBJECT (favicon), - "embed", &embed, - NULL); - - return embed; -} diff --git a/embed/ephy-embed-favicon.h b/embed/ephy-embed-favicon.h deleted file mode 100644 index 36f69cc82..000000000 --- a/embed/ephy-embed-favicon.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2002 Jorn Baayen - * - * 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 - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include <glib-object.h> -#include <gtk/gtkimage.h> -#include <gdk-pixbuf/gdk-pixbuf.h> - -#include "ephy-favicon.h" -#include "ephy-embed.h" - -#ifndef __EPHY_EMBED_FAVICON_H -#define __EPHY_EMBED_FAVICON_H - -G_BEGIN_DECLS - -#define EPHY_TYPE_EMBED_FAVICON (ephy_embed_favicon_get_type ()) -#define EPHY_EMBED_FAVICON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_EMBED_FAVICON, EphyEmbedFavicon)) -#define EPHY_EMBED_FAVICON_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EPHY_TYPE_EMBED_FAVICON, EphyEmbedFaviconClass)) -#define EPHY_IS_EMBED_FAVICON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_EMBED_FAVICON)) -#define EPHY_IS_EMBED_FAVICON_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_EMBED_FAVICON)) -#define EPHY_EMBED_FAVICON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_EMBED_FAVICON, EphyEmbedFaviconClass)) - -typedef struct EphyEmbedFaviconPrivate EphyEmbedFaviconPrivate; - -typedef struct -{ - EphyFavicon parent; - - EphyEmbedFaviconPrivate *priv; -} EphyEmbedFavicon; - -typedef struct -{ - EphyFaviconClass parent_class; -} EphyEmbedFaviconClass; - -GType ephy_embed_favicon_get_type (void); - -GtkWidget *ephy_embed_favicon_new (EphyEmbed *embed); - -void ephy_embed_favicon_set_embed (EphyEmbedFavicon *favicon, - EphyEmbed *embed); - -EphyEmbed *ephy_embed_favicon_get_embed (EphyEmbedFavicon *favicon); - -G_END_DECLS - -#endif /* __EPHY_EMBED_FAVICON_H */ diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c index 679e6f663..9db8e2db6 100644 --- a/embed/ephy-embed-shell.c +++ b/embed/ephy-embed-shell.c @@ -204,10 +204,7 @@ ephy_embed_shell_get_favicon_cache (EphyEmbedShell *ees) { if (ees->priv->favicon_cache == NULL) { - EphyHistory *history; - - history = ephy_embed_shell_get_global_history (ees); - ees->priv->favicon_cache = ephy_favicon_cache_new (history); + ees->priv->favicon_cache = ephy_favicon_cache_new (); } return ees->priv->favicon_cache; 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 <libgnomevfs/gnome-vfs-uri.h> #include <libxml/tree.h> +#include <libgnomevfs/gnome-vfs-ops.h> #include <string.h> -#include <gtk/gtktoolbar.h> -#include <gtk/gtkstock.h> +#include <time.h> #include <sys/stat.h> #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 }; @@ -96,43 +89,6 @@ ephy_favicon_cache_get_type (void) } 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) { GObjectClass *object_class = G_OBJECT_CLASS (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; } + + diff --git a/embed/ephy-favicon-cache.h b/embed/ephy-favicon-cache.h index ff4ebcd56..4e182cf2c 100644 --- a/embed/ephy-favicon-cache.h +++ b/embed/ephy-favicon-cache.h @@ -16,8 +16,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include "ephy-history.h" - #include <glib-object.h> #include <gdk-pixbuf/gdk-pixbuf.h> @@ -35,6 +33,13 @@ G_BEGIN_DECLS typedef struct EphyFaviconCachePrivate EphyFaviconCachePrivate; +enum +{ + EPHY_NODE_FAVICON_PROP_URL = 2, + EPHY_NODE_FAVICON_PROP_FILENAME = 3, + EPHY_NODE_FAVICON_PROP_LAST_USED = 4 +}; + typedef struct { GObject parent; @@ -51,18 +56,11 @@ typedef struct GType ephy_favicon_cache_get_type (void); -EphyFaviconCache *ephy_favicon_cache_new (EphyHistory *history); +EphyFaviconCache *ephy_favicon_cache_new (void); -GdkPixbuf *ephy_favicon_cache_lookup (EphyFaviconCache *cache, +GdkPixbuf *ephy_favicon_cache_get (EphyFaviconCache *cache, const char *url); -GdkPixbuf *ephy_favicon_cache_lookup_direct (EphyFaviconCache *cache, - const char *cache_url); - -void ephy_favicon_cache_insert_from_url (EphyFaviconCache *cache, - const char *url, - const char *favicon_url); - G_END_DECLS #endif /* __EPHY_FAVICON_CACHE_H */ diff --git a/embed/ephy-favicon.c b/embed/ephy-favicon.c deleted file mode 100644 index 6670c20ab..000000000 --- a/embed/ephy-favicon.c +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.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 - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include <gtk/gtkwidget.h> -#include <string.h> - -#include "ephy-favicon.h" -#include "ephy-embed-shell.h" - -static void ephy_favicon_class_init (EphyFaviconClass *klass); -static void ephy_favicon_init (EphyFavicon *ma); -static void ephy_favicon_finalize (GObject *object); -static void ephy_favicon_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec); -static void ephy_favicon_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec); -static void ephy_favicon_update_image (EphyFavicon *favicon); -static void cache_changed_cb (EphyFaviconCache *cache, - const char *url, - EphyFavicon *favicon); - -struct EphyFaviconPrivate -{ - EphyFaviconCache *cache; - - char *url; -}; - -enum -{ - PROP_0, - PROP_CACHE, - PROP_URL -}; - -enum -{ - CHANGED, - LAST_SIGNAL -}; - -static guint ephy_favicon_signals[LAST_SIGNAL] = { 0 }; - -static GObjectClass *parent_class = NULL; - -GType -ephy_favicon_get_type (void) -{ - static GType ephy_favicon_type = 0; - - if (ephy_favicon_type == 0) - { - static const GTypeInfo our_info = - { - sizeof (EphyFaviconClass), - NULL, - NULL, - (GClassInitFunc) ephy_favicon_class_init, - NULL, - NULL, - sizeof (EphyFavicon), - 0, - (GInstanceInitFunc) ephy_favicon_init - }; - - ephy_favicon_type = g_type_register_static (GTK_TYPE_IMAGE, - "EphyFavicon", - &our_info, 0); - } - - return ephy_favicon_type; -} - -static void -ephy_favicon_class_init (EphyFaviconClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - parent_class = g_type_class_peek_parent (klass); - - object_class->finalize = ephy_favicon_finalize; - - object_class->set_property = ephy_favicon_set_property; - object_class->get_property = ephy_favicon_get_property; - - g_object_class_install_property (object_class, - PROP_CACHE, - g_param_spec_object ("cache", - "Favicon cache", - "Favicon cache", - EPHY_TYPE_FAVICON_CACHE, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); - g_object_class_install_property (object_class, - PROP_URL, - g_param_spec_string ("url", - "Associated URL", - "Associated URL", - NULL, - G_PARAM_READWRITE)); - - ephy_favicon_signals[CHANGED] = - g_signal_new ("changed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (EphyFaviconClass, changed), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, - 0); -} - -static void -ephy_favicon_init (EphyFavicon *ma) -{ - ma->priv = g_new0 (EphyFaviconPrivate, 1); - - gtk_widget_set_size_request (GTK_WIDGET (ma), 16, 16); -} - -static void -ephy_favicon_finalize (GObject *object) -{ - EphyFavicon *ma; - - g_return_if_fail (object != NULL); - g_return_if_fail (EPHY_IS_FAVICON (object)); - - ma = EPHY_FAVICON (object); - - g_return_if_fail (ma->priv != NULL); - - g_free (ma->priv->url); - - g_free (ma->priv); - - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -static void -ephy_favicon_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - EphyFavicon *favicon = EPHY_FAVICON (object); - - switch (prop_id) - { - case PROP_CACHE: - favicon->priv->cache = g_value_get_object (value); - - g_signal_connect_object (G_OBJECT (favicon->priv->cache), - "changed", - G_CALLBACK (cache_changed_cb), - favicon, - 0); - - ephy_favicon_update_image (favicon); - break; - case PROP_URL: - g_free (favicon->priv->url); - favicon->priv->url = g_strdup (g_value_get_string (value)); - - ephy_favicon_update_image (favicon); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -ephy_favicon_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - EphyFavicon *favicon = EPHY_FAVICON (object); - - switch (prop_id) - { - case PROP_CACHE: - g_value_set_object (value, favicon->priv->cache); - break; - case PROP_URL: - g_value_set_string (value, favicon->priv->url); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -GtkWidget * -ephy_favicon_new (const char *url) -{ - EphyFavicon *favicon; - EphyFaviconCache *cache = ephy_embed_shell_get_favicon_cache (embed_shell); - - g_return_val_if_fail (EPHY_IS_FAVICON_CACHE (cache), NULL); - - favicon = EPHY_FAVICON (g_object_new (EPHY_TYPE_FAVICON, - "cache", cache, - "url", url, - NULL)); - - g_return_val_if_fail (favicon->priv != NULL, NULL); - - return GTK_WIDGET (favicon); -} - -void -ephy_favicon_set_url (EphyFavicon *favicon, - const char *url) -{ - g_return_if_fail (EPHY_IS_FAVICON (favicon)); - - g_object_set (G_OBJECT (favicon), - "url", url, - NULL); -} - -const char * -ephy_favicon_get_url (EphyFavicon *favicon) -{ - char *url; - - g_return_val_if_fail (EPHY_IS_FAVICON (favicon), NULL); - - g_object_get (G_OBJECT (favicon), - "url", &url, - NULL); - - return (const char *) url; -} - -static void -cache_changed_cb (EphyFaviconCache *cache, - const char *url, - EphyFavicon *favicon) -{ - if (strcmp (url, favicon->priv->url) == 0) - { - ephy_favicon_update_image (favicon); - } -} - -static void -ephy_favicon_update_image (EphyFavicon *favicon) -{ - GdkPixbuf *pixbuf; - - g_return_if_fail (EPHY_IS_FAVICON_CACHE (favicon->priv->cache)); - - pixbuf = ephy_favicon_cache_lookup (favicon->priv->cache, - favicon->priv->url); - - gtk_image_set_from_pixbuf (GTK_IMAGE (favicon), pixbuf); - - g_signal_emit (G_OBJECT (favicon), ephy_favicon_signals[CHANGED], 0); -} diff --git a/embed/ephy-favicon.h b/embed/ephy-favicon.h deleted file mode 100644 index 97206cde5..000000000 --- a/embed/ephy-favicon.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2002 Jorn Baayen - * - * 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 - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#include <glib-object.h> -#include <gtk/gtkimage.h> -#include <gdk-pixbuf/gdk-pixbuf.h> - -#include "ephy-favicon-cache.h" - -#ifndef __EPHY_FAVICON_H -#define __EPHY_FAVICON_H - -G_BEGIN_DECLS - -#define EPHY_TYPE_FAVICON (ephy_favicon_get_type ()) -#define EPHY_FAVICON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_FAVICON, EphyFavicon)) -#define EPHY_FAVICON_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EPHY_TYPE_FAVICON, EphyFaviconClass)) -#define EPHY_IS_FAVICON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_FAVICON)) -#define EPHY_IS_FAVICON_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_FAVICON)) -#define EPHY_FAVICON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_FAVICON, EphyFaviconClass)) - -typedef struct EphyFaviconPrivate EphyFaviconPrivate; - -typedef struct -{ - GtkImage parent; - - EphyFaviconPrivate *priv; -} EphyFavicon; - -typedef struct -{ - GtkImageClass parent_class; - - void (*changed) (EphyFavicon *favicon); -} EphyFaviconClass; - -GType ephy_favicon_get_type (void); - -GtkWidget *ephy_favicon_new (const char *url); - -void ephy_favicon_set_url (EphyFavicon *favicon, - const char *url); - -const char *ephy_favicon_get_url (EphyFavicon *favicon); - -G_END_DECLS - -#endif /* __EPHY_FAVICON_H */ diff --git a/embed/mozilla/mozilla-embed-shell.cpp b/embed/mozilla/mozilla-embed-shell.cpp index 249b7dd50..58c692eb4 100644 --- a/embed/mozilla/mozilla-embed-shell.cpp +++ b/embed/mozilla/mozilla-embed-shell.cpp @@ -484,6 +484,10 @@ mozilla_embed_shell_finalize (GObject *object) { MozillaEmbedShell *mes; + /* Destroy EphyEmbedShell before because some + * services depend on xpcom */ + G_OBJECT_CLASS (parent_class)->finalize (object); + g_return_if_fail (object != NULL); g_return_if_fail (IS_MOZILLA_EMBED_SHELL (object)); @@ -498,8 +502,6 @@ mozilla_embed_shell_finalize (GObject *object) gtk_moz_embed_pop_startup (); g_free (mes->priv); - - G_OBJECT_CLASS (parent_class)->finalize (object); } static void |