From ec9060610c40fef18534b414d1979f4359f59a5e Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Sun, 7 Aug 2005 21:47:11 +0000 Subject: Add way to clear the favicon cache. Also removes any extraneous files from 2005-08-07 Christian Persch * embed/ephy-favicon-cache.c: (icons_removed_cb), (remove_obsolete_icons), (delete_file), (ephy_favicon_cache_finalize), (ephy_favicon_cache_get), (ephy_favicon_cache_clear): * embed/ephy-favicon-cache.h: Add way to clear the favicon cache. Also removes any extraneous files from favicon cache directory. Fix a crash which I wonder why we never experienced it! * src/prefs-dialog.c: (prefs_clear_cache_button_clicked_cb): Also clear favicon cache when clearing the cache. --- embed/ephy-favicon-cache.c | 74 ++++++++++++++++++++++++++++++++++++++++------ embed/ephy-favicon-cache.h | 4 ++- 2 files changed, 68 insertions(+), 10 deletions(-) (limited to 'embed') diff --git a/embed/ephy-favicon-cache.c b/embed/ephy-favicon-cache.c index a9ab3b3ff..48535fd04 100644 --- a/embed/ephy-favicon-cache.c +++ b/embed/ephy-favicon-cache.c @@ -1,7 +1,7 @@ /* * Copyright (C) 2002 Jorn Baayen * Copyright (C) 2003-2004 Marco Pesenti Gritti - * Copyright (C) 2004 Christian Persch + * Copyright (C) 2004, 2005 Christian Persch * * 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 @@ -39,6 +39,7 @@ #include #include #include +#include #define EPHY_FAVICON_CACHE_XML_ROOT (const xmlChar *)"ephy_favicons_cache" #define EPHY_FAVICON_CACHE_XML_VERSION (const xmlChar *)"1.1" @@ -87,6 +88,8 @@ typedef struct guint load_failed : 1; } PixbufCacheEntry; +typedef gboolean (* FilterFunc) (EphyNode*, GDate *); + enum { CHANGED, @@ -216,6 +219,7 @@ icons_added_cb (EphyNode *node, static void icons_removed_cb (EphyNode *node, EphyNode *child, + guint old_index, EphyFaviconCache *eb) { g_hash_table_remove (eb->priv->icons_hash, @@ -223,39 +227,41 @@ icons_removed_cb (EphyNode *node, } static void -remove_obsolete_icons (EphyFaviconCache *eb) +remove_obsolete_icons (EphyFaviconCache *cache, + FilterFunc filter) { + EphyFaviconCachePrivate *priv = cache->priv; 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); - for (i = 0; i < children->len; i++) + children = ephy_node_get_children (priv->icons); + for (i = (int) children->len - 1; i >= 0; i--) { EphyNode *kid; kid = g_ptr_array_index (children, i); - if (icon_is_obsolete (kid, ¤t_date)) + if (!filter || filter (kid, ¤t_date)) { const char *filename; char *path; filename = ephy_node_get_property_string (kid, EPHY_NODE_FAVICON_PROP_FILENAME); - path = g_build_filename (eb->priv->directory, + path = g_build_filename (priv->directory, filename, NULL); + g_print ("Removing kid %d, deleting %s\n", i, path); gnome_vfs_unlink (path); g_free (path); ephy_node_unref (kid); } } + g_print ("Now %d children\n", children->len); } static void @@ -412,6 +418,31 @@ kill_download (const char *key, return TRUE; } +static gboolean +delete_file (const char *rel_path, + GnomeVFSFileInfo *info, + gboolean rec_will_loop, + EphyFaviconCache *cache, + gboolean *recurse) +{ + EphyFaviconCachePrivate *priv = cache->priv; + char *path; + + *recurse = FALSE; + + g_return_val_if_fail (info != NULL, TRUE); + + if ((info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_TYPE) == 0 || + info->type != GNOME_VFS_FILE_TYPE_REGULAR) return TRUE; + + path = g_build_filename (priv->directory, rel_path, NULL); + gnome_vfs_unlink (path); + g_free (path); + + /* continue with the visit */ + return TRUE; +} + static void ephy_favicon_cache_finalize (GObject *object) { @@ -433,7 +464,7 @@ ephy_favicon_cache_finalize (GObject *object) g_source_remove (priv->cleanup_timeout); } - remove_obsolete_icons (cache); + remove_obsolete_icons (cache, icon_is_obsolete); ephy_favicon_cache_save (cache); @@ -795,3 +826,28 @@ ephy_favicon_cache_get (EphyFaviconCache *cache, return pixbuf; } + +/** + * ephy_favicons_cache_clear: + * @cache: + * + * Clears the favicon cache and removes any stored icon files from disk. + */ +void +ephy_favicon_cache_clear (EphyFaviconCache *cache) +{ + EphyFaviconCachePrivate *priv = cache->priv; + + g_return_if_fail (EPHY_IS_FAVICON_CACHE (cache)); + + remove_obsolete_icons (cache, NULL); + ephy_favicon_cache_save (cache); + + /* Now remove any remaining files from the cache directory */ + gnome_vfs_directory_visit (priv->directory, + GNOME_VFS_FILE_INFO_DEFAULT, + GNOME_VFS_DIRECTORY_VISIT_SAMEFS | + GNOME_VFS_DIRECTORY_VISIT_LOOPCHECK, + (GnomeVFSDirectoryVisitFunc) delete_file, + cache); +} diff --git a/embed/ephy-favicon-cache.h b/embed/ephy-favicon-cache.h index a2d211821..f2161e6f9 100644 --- a/embed/ephy-favicon-cache.h +++ b/embed/ephy-favicon-cache.h @@ -1,7 +1,7 @@ /* * Copyright (C) 2002 Jorn Baayen * Copyright (C) 2003-2004 Marco Pesenti Gritti - * Copyright (C) 2004 Christian Persch + * Copyright (C) 2004, 2005 Christian Persch * * 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 @@ -63,6 +63,8 @@ EphyFaviconCache *ephy_favicon_cache_new (void); GdkPixbuf *ephy_favicon_cache_get (EphyFaviconCache *cache, const char *url); +void ephy_favicon_cache_clear (EphyFaviconCache *cache); + G_END_DECLS #endif /* EPHY_FAVICON_CACHE_H */ -- cgit v1.2.3