2 * This file is part of mapper
4 * Copyright (C) 2008 Kaj-Michael Lang
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "image-cache.h"
24 image_cache_new(guint cache_max)
27 ic=g_slice_new0(ImageCache);
28 ic->cache=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
29 ic->cache_max=cache_max;
34 image_cache_free(ImageCache *ic)
38 image_cache_clear(ic);
39 g_hash_table_destroy(ic->cache);
40 g_slice_free(ImageCache, ic);
44 image_cache_set_size(ImageCache *ic, guint cache_size)
49 ic->cache_max=cache_size;
51 image_cache_gc(ic, cache_size);
55 image_cache_stats(ImageCache *ic)
60 g_debug("ICC: %u/%u (E: %u, GC: %u S: %d)", ic->hit, ic->miss, ic->error, ic->gc, g_hash_table_size(ic->cache));
64 image_cache_clear(ImageCache *ic)
69 #if (GLIB_CHECK_VERSION (2, 12, 0))
70 g_hash_table_remove_all(ic->cache);
72 g_hash_table_foreach_remove(ic->cache, gtk_true, NULL);
77 image_cache_invalidate(ImageCache *ic, const gchar *key)
82 g_hash_table_remove(ic->cache, key);
86 image_cache_is_same(gchar *key, GdkPixbuf *cpixbuf, GdkPixbuf *pixbuf)
88 return (pixbuf==cpixbuf) ? TRUE : FALSE;
92 image_cache_invalidate_by_image(ImageCache *ic, GdkPixbuf *pixbuf)
96 g_return_if_fail(GDK_IS_PIXBUF(pixbuf));
97 g_hash_table_foreach_remove(ic->cache, image_cache_is_same, pixbuf);
101 image_cache_replace(ImageCache *ic, const gchar *key, GdkPixbuf *pixbuf)
107 image_cache_gc_check(gchar *key, GdkPixbuf *pixbuf, ImageCache *ic)
110 /* For now just remove everything when over limit */
115 image_cache_gc(ImageCache *ic, gint max)
122 m=(max>0) ? max : ic->cache_max;
123 if (g_hash_table_size(ic->cache)>m) {
124 g_hash_table_foreach_remove(ic->cache,(GHRFunc *)image_cache_gc_check, ic);
130 image_cache_get(ImageCache *ic, const gchar *key, const gchar *image)
140 pixbuf=g_hash_table_lookup(ic->cache, key);
146 pixbuf=gdk_pixbuf_new_from_file(image, &error);
147 if (error || !pixbuf) {
151 g_return_val_if_fail(GDK_IS_PIXBUF(pixbuf), NULL);
153 image_cache_gc(ic, 0);
156 g_hash_table_insert(ic->cache, g_strdup(key), pixbuf);