2 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "gtkiconcache.h"
24 #include "gtkiconcachevalidator.h"
26 #include <glib/gstdio.h>
27 #include <gdk-pixbuf/gdk-pixdata.h>
36 #include <sys/types.h>
45 #define MAJOR_VERSION 1
46 #define MINOR_VERSION 0
48 #define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
49 #define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
52 struct _GtkIconCache {
58 guint32 last_chain_offset;
62 _gtk_icon_cache_ref (GtkIconCache *cache)
69 _gtk_icon_cache_unref (GtkIconCache *cache)
73 if (cache->ref_count == 0)
76 g_print ("unmapping icon cache\n"));
79 g_mapped_file_unref (cache->map);
85 _gtk_icon_cache_new_for_path (const gchar *path)
87 GtkIconCache *cache = NULL;
90 gchar *cache_filename;
96 /* Check if we have a cache file */
97 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
100 g_print ("look for cache in %s\n", path));
102 if (g_stat (path, &path_st) < 0)
105 /* Open the file and map it into memory */
106 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
111 if (fstat (fd, &st) < 0 || st.st_size < 4)
114 /* Verify cache is uptodate */
115 if (st.st_mtime < path_st.st_mtime)
118 g_print ("cache outdated\n"));
122 map = g_mapped_file_new (cache_filename, FALSE, NULL);
127 info.cache = g_mapped_file_get_contents (map);
128 info.cache_size = g_mapped_file_get_length (map);
129 info.n_directories = 0;
130 info.flags = CHECK_OFFSETS|CHECK_STRINGS;
132 #ifdef G_ENABLE_DEBUG
133 if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
135 if (!_gtk_icon_cache_validate (&info))
137 g_mapped_file_unref (map);
138 g_warning ("Icon cache '%s' is invalid\n", cache_filename);
145 GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
147 cache = g_new0 (GtkIconCache, 1);
148 cache->ref_count = 1;
150 cache->buffer = g_mapped_file_get_contents (map);
153 g_free (cache_filename);
161 _gtk_icon_cache_new (const gchar *data)
165 cache = g_new0 (GtkIconCache, 1);
166 cache->ref_count = 1;
168 cache->buffer = (gchar *)data;
174 get_directory_index (GtkIconCache *cache,
175 const gchar *directory)
177 guint32 dir_list_offset;
181 dir_list_offset = GET_UINT32 (cache->buffer, 8);
183 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
185 for (i = 0; i < n_dirs; i++)
187 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
188 gchar *name = cache->buffer + name_offset;
189 if (strcmp (name, directory) == 0)
197 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
198 const gchar *directory)
200 return get_directory_index (cache, directory);
204 icon_name_hash (gconstpointer key)
206 const signed char *p = key;
210 for (p += 1; *p != '\0'; p++)
211 h = (h << 5) - h + *p;
217 find_image_offset (GtkIconCache *cache,
218 const gchar *icon_name,
219 gint directory_index)
223 guint32 chain_offset;
225 guint32 image_list_offset, n_images;
228 chain_offset = cache->last_chain_offset;
231 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
232 gchar *name = cache->buffer + name_offset;
234 if (strcmp (name, icon_name) == 0)
238 hash_offset = GET_UINT32 (cache->buffer, 4);
239 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
240 hash = icon_name_hash (icon_name) % n_buckets;
242 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
243 while (chain_offset != 0xffffffff)
245 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
246 gchar *name = cache->buffer + name_offset;
248 if (strcmp (name, icon_name) == 0)
250 cache->last_chain_offset = chain_offset;
254 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
257 cache->last_chain_offset = 0;
261 /* We've found an icon list, now check if we have the right icon in it */
262 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
263 n_images = GET_UINT32 (cache->buffer, image_list_offset);
265 for (i = 0; i < n_images; i++)
267 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
269 return image_list_offset + 4 + 8 * i;
276 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
277 const gchar *icon_name,
278 gint directory_index)
280 guint32 image_offset;
282 image_offset = find_image_offset (cache, icon_name, directory_index);
287 return GET_UINT16 (cache->buffer, image_offset + 2);
291 _gtk_icon_cache_add_icons (GtkIconCache *cache,
292 const gchar *directory,
293 GHashTable *hash_table)
296 guint32 hash_offset, n_buckets;
297 guint32 chain_offset;
298 guint32 image_list_offset, n_images;
301 directory_index = get_directory_index (cache, directory);
303 if (directory_index == -1)
306 hash_offset = GET_UINT32 (cache->buffer, 4);
307 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
309 for (i = 0; i < n_buckets; i++)
311 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
312 while (chain_offset != 0xffffffff)
314 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
315 gchar *name = cache->buffer + name_offset;
317 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
318 n_images = GET_UINT32 (cache->buffer, image_list_offset);
320 for (j = 0; j < n_images; j++)
322 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
324 g_hash_table_insert (hash_table, name, NULL);
327 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
333 _gtk_icon_cache_has_icon (GtkIconCache *cache,
334 const gchar *icon_name)
338 guint32 chain_offset;
341 hash_offset = GET_UINT32 (cache->buffer, 4);
342 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
344 hash = icon_name_hash (icon_name) % n_buckets;
346 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
347 while (chain_offset != 0xffffffff)
349 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
350 gchar *name = cache->buffer + name_offset;
352 if (strcmp (name, icon_name) == 0)
355 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
362 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
363 const gchar *icon_name,
364 const gchar *directory)
368 guint32 chain_offset;
370 gboolean found_icon = FALSE;
371 gint directory_index;
373 directory_index = get_directory_index (cache, directory);
375 if (directory_index == -1)
378 hash_offset = GET_UINT32 (cache->buffer, 4);
379 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
381 hash = icon_name_hash (icon_name) % n_buckets;
383 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
384 while (chain_offset != 0xffffffff)
386 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
387 gchar *name = cache->buffer + name_offset;
389 if (strcmp (name, icon_name) == 0)
395 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
400 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
401 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
402 guint32 image_offset = image_list_offset + 4;
404 for (i = 0; i < n_images; i++)
406 guint16 index = GET_UINT16 (cache->buffer, image_offset);
408 if (index == directory_index)
418 pixbuf_destroy_cb (guchar *pixels,
421 GtkIconCache *cache = data;
423 _gtk_icon_cache_unref (cache);
427 _gtk_icon_cache_get_icon (GtkIconCache *cache,
428 const gchar *icon_name,
429 gint directory_index)
431 guint32 offset, image_data_offset, pixel_data_offset;
432 guint32 length, type;
435 GError *error = NULL;
437 offset = find_image_offset (cache, icon_name, directory_index);
439 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
441 if (!image_data_offset)
444 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
446 type = GET_UINT32 (cache->buffer, pixel_data_offset);
451 g_print ("invalid pixel data type %u\n", type));
455 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
457 if (!gdk_pixdata_deserialize (&pixdata, length,
458 (guchar *)(cache->buffer + pixel_data_offset + 8),
462 g_print ("could not deserialize data: %s\n", error->message));
463 g_error_free (error);
468 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
469 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
470 8, pixdata.width, pixdata.height, pixdata.rowstride,
471 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
476 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
477 g_error_free (error);
482 _gtk_icon_cache_ref (cache);
488 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
489 const gchar *icon_name,
490 gint directory_index)
492 guint32 offset, image_data_offset, meta_data_offset;
496 offset = find_image_offset (cache, icon_name, directory_index);
500 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
501 if (!image_data_offset)
504 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
506 if (!meta_data_offset)
509 data = g_slice_new0 (GtkIconData);
511 offset = GET_UINT32 (cache->buffer, meta_data_offset);
514 data->has_embedded_rect = TRUE;
515 data->x0 = GET_UINT16 (cache->buffer, offset);
516 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
517 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
518 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
521 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
524 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
525 data->attach_points = g_new (GdkPoint, data->n_attach_points);
526 for (i = 0; i < data->n_attach_points; i++)
528 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
529 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
533 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
539 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
541 n_names = GET_UINT32 (cache->buffer, offset);
543 for (i = 0; i < n_names; i++)
545 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
546 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
548 g_hash_table_insert (table, lang, name);
551 langs = (gchar **)g_get_language_names ();
552 for (i = 0; langs[i]; i++)
554 name = g_hash_table_lookup (table, langs[i]);
557 data->display_name = g_strdup (name);
562 g_hash_table_destroy (table);