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"
27 #include <glib/gstdio.h>
28 #include <gdk-pixbuf/gdk-pixdata.h>
37 #include <sys/types.h>
46 #define MAJOR_VERSION 1
47 #define MINOR_VERSION 0
49 #define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
50 #define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
53 struct _GtkIconCache {
59 guint32 last_chain_offset;
63 _gtk_icon_cache_ref (GtkIconCache *cache)
70 _gtk_icon_cache_unref (GtkIconCache *cache)
74 if (cache->ref_count == 0)
77 g_print ("unmapping icon cache\n"));
80 g_mapped_file_free (cache->map);
86 _gtk_icon_cache_new_for_path (const gchar *path)
88 GtkIconCache *cache = NULL;
91 gchar *cache_filename;
97 /* Check if we have a cache file */
98 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
101 g_print ("look for cache in %s\n", path));
103 if (g_stat (path, &path_st) < 0)
106 /* Open the file and map it into memory */
107 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
112 if (fstat (fd, &st) < 0 || st.st_size < 4)
115 /* Verify cache is uptodate */
116 if (st.st_mtime < path_st.st_mtime)
119 g_print ("cache outdated\n"));
123 map = g_mapped_file_new (cache_filename, FALSE, NULL);
128 info.cache = g_mapped_file_get_contents (map);
129 info.cache_size = g_mapped_file_get_length (map);
130 info.n_directories = 0;
131 info.flags = CHECK_OFFSETS|CHECK_STRINGS;
133 #ifdef G_ENABLE_DEBUG
134 if (gtk_debug_flags & GTK_DEBUG_ICONTHEME)
136 if (!_gtk_icon_cache_validate (&info))
138 g_mapped_file_free (map);
139 g_warning ("Icon cache '%s' is invalid\n", cache_filename);
146 GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
148 cache = g_new0 (GtkIconCache, 1);
149 cache->ref_count = 1;
151 cache->buffer = g_mapped_file_get_contents (map);
154 g_free (cache_filename);
162 _gtk_icon_cache_new (const gchar *data)
166 cache = g_new0 (GtkIconCache, 1);
167 cache->ref_count = 1;
169 cache->buffer = (gchar *)data;
175 get_directory_index (GtkIconCache *cache,
176 const gchar *directory)
178 guint32 dir_list_offset;
182 dir_list_offset = GET_UINT32 (cache->buffer, 8);
184 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
186 for (i = 0; i < n_dirs; i++)
188 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
189 gchar *name = cache->buffer + name_offset;
190 if (strcmp (name, directory) == 0)
198 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
199 const gchar *directory)
201 return get_directory_index (cache, directory);
205 icon_name_hash (gconstpointer key)
207 const signed char *p = key;
211 for (p += 1; *p != '\0'; p++)
212 h = (h << 5) - h + *p;
218 find_image_offset (GtkIconCache *cache,
219 const gchar *icon_name,
220 gint directory_index)
224 guint32 chain_offset;
226 guint32 image_list_offset, n_images;
229 chain_offset = cache->last_chain_offset;
232 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
233 gchar *name = cache->buffer + name_offset;
235 if (strcmp (name, icon_name) == 0)
239 hash_offset = GET_UINT32 (cache->buffer, 4);
240 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
241 hash = icon_name_hash (icon_name) % n_buckets;
243 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
244 while (chain_offset != 0xffffffff)
246 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
247 gchar *name = cache->buffer + name_offset;
249 if (strcmp (name, icon_name) == 0)
251 cache->last_chain_offset = chain_offset;
255 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
258 cache->last_chain_offset = 0;
262 /* We've found an icon list, now check if we have the right icon in it */
263 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
264 n_images = GET_UINT32 (cache->buffer, image_list_offset);
266 for (i = 0; i < n_images; i++)
268 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
270 return image_list_offset + 4 + 8 * i;
277 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
278 const gchar *icon_name,
279 gint directory_index)
281 guint32 image_offset;
283 image_offset = find_image_offset (cache, icon_name, directory_index);
288 return GET_UINT16 (cache->buffer, image_offset + 2);
292 _gtk_icon_cache_add_icons (GtkIconCache *cache,
293 const gchar *directory,
294 GHashTable *hash_table)
297 guint32 hash_offset, n_buckets;
298 guint32 chain_offset;
299 guint32 image_list_offset, n_images;
302 directory_index = get_directory_index (cache, directory);
304 if (directory_index == -1)
307 hash_offset = GET_UINT32 (cache->buffer, 4);
308 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
310 for (i = 0; i < n_buckets; i++)
312 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
313 while (chain_offset != 0xffffffff)
315 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
316 gchar *name = cache->buffer + name_offset;
318 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
319 n_images = GET_UINT32 (cache->buffer, image_list_offset);
321 for (j = 0; j < n_images; j++)
323 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
325 g_hash_table_insert (hash_table, name, NULL);
328 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
334 _gtk_icon_cache_has_icon (GtkIconCache *cache,
335 const gchar *icon_name)
339 guint32 chain_offset;
342 hash_offset = GET_UINT32 (cache->buffer, 4);
343 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
345 hash = icon_name_hash (icon_name) % n_buckets;
347 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
348 while (chain_offset != 0xffffffff)
350 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
351 gchar *name = cache->buffer + name_offset;
353 if (strcmp (name, icon_name) == 0)
356 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
363 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
364 const gchar *icon_name,
365 const gchar *directory)
369 guint32 chain_offset;
371 gboolean found_icon = FALSE;
372 gint directory_index;
374 directory_index = get_directory_index (cache, directory);
376 if (directory_index == -1)
379 hash_offset = GET_UINT32 (cache->buffer, 4);
380 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
382 hash = icon_name_hash (icon_name) % n_buckets;
384 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
385 while (chain_offset != 0xffffffff)
387 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
388 gchar *name = cache->buffer + name_offset;
390 if (strcmp (name, icon_name) == 0)
396 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
401 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
402 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
403 guint32 image_offset = image_list_offset + 4;
405 for (i = 0; i < n_images; i++)
407 guint16 index = GET_UINT16 (cache->buffer, image_offset);
409 if (index == directory_index)
419 pixbuf_destroy_cb (guchar *pixels,
422 GtkIconCache *cache = data;
424 _gtk_icon_cache_unref (cache);
428 _gtk_icon_cache_get_icon (GtkIconCache *cache,
429 const gchar *icon_name,
430 gint directory_index)
432 guint32 offset, image_data_offset, pixel_data_offset;
433 guint32 length, type;
436 GError *error = NULL;
438 offset = find_image_offset (cache, icon_name, directory_index);
440 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
442 if (!image_data_offset)
445 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
447 type = GET_UINT32 (cache->buffer, pixel_data_offset);
452 g_print ("invalid pixel data type %u\n", type));
456 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
458 if (!gdk_pixdata_deserialize (&pixdata, length,
459 (guchar *)(cache->buffer + pixel_data_offset + 8),
463 g_print ("could not deserialize data: %s\n", error->message));
464 g_error_free (error);
469 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
470 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
471 8, pixdata.width, pixdata.height, pixdata.rowstride,
472 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
477 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
478 g_error_free (error);
483 _gtk_icon_cache_ref (cache);
489 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
490 const gchar *icon_name,
491 gint directory_index)
493 guint32 offset, image_data_offset, meta_data_offset;
497 offset = find_image_offset (cache, icon_name, directory_index);
501 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
502 if (!image_data_offset)
505 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
507 if (!meta_data_offset)
510 data = g_slice_new0 (GtkIconData);
512 offset = GET_UINT32 (cache->buffer, meta_data_offset);
515 data->has_embedded_rect = TRUE;
516 data->x0 = GET_UINT16 (cache->buffer, offset);
517 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
518 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
519 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
522 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
525 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
526 data->attach_points = g_new (GdkPoint, data->n_attach_points);
527 for (i = 0; i < data->n_attach_points; i++)
529 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
530 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
534 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
540 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
542 n_names = GET_UINT32 (cache->buffer, offset);
544 for (i = 0; i < n_names; i++)
546 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
547 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
549 g_hash_table_insert (table, lang, name);
552 langs = (gchar **)g_get_language_names ();
553 for (i = 0; langs[i]; i++)
555 name = g_hash_table_lookup (table, langs[i]);
558 data->display_name = g_strdup (name);
563 g_hash_table_destroy (table);