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;
95 /* Check if we have a cache file */
96 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
99 g_print ("look for cache in %s\n", path));
101 if (g_stat (path, &path_st) < 0)
104 /* Open the file and map it into memory */
105 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
111 #undef fstat /* Just in case */
112 #define fstat _fstat32
115 if (fstat (fd, &st) < 0 || st.st_size < 4)
118 /* Verify cache is uptodate */
119 if (st.st_mtime < path_st.st_mtime)
122 g_print ("cache outdated\n"));
126 map = g_mapped_file_new (cache_filename, FALSE, NULL);
131 #ifdef G_ENABLE_DEBUG
132 if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
136 info.cache = g_mapped_file_get_contents (map);
137 info.cache_size = g_mapped_file_get_length (map);
138 info.n_directories = 0;
139 info.flags = CHECK_OFFSETS|CHECK_STRINGS;
141 if (!_gtk_icon_cache_validate (&info))
143 g_mapped_file_unref (map);
144 g_warning ("Icon cache '%s' is invalid\n", cache_filename);
151 GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
153 cache = g_new0 (GtkIconCache, 1);
154 cache->ref_count = 1;
156 cache->buffer = g_mapped_file_get_contents (map);
159 g_free (cache_filename);
167 _gtk_icon_cache_new (const gchar *data)
171 cache = g_new0 (GtkIconCache, 1);
172 cache->ref_count = 1;
174 cache->buffer = (gchar *)data;
180 get_directory_index (GtkIconCache *cache,
181 const gchar *directory)
183 guint32 dir_list_offset;
187 dir_list_offset = GET_UINT32 (cache->buffer, 8);
189 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
191 for (i = 0; i < n_dirs; i++)
193 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
194 gchar *name = cache->buffer + name_offset;
195 if (strcmp (name, directory) == 0)
203 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
204 const gchar *directory)
206 return get_directory_index (cache, directory);
210 icon_name_hash (gconstpointer key)
212 const signed char *p = key;
216 for (p += 1; *p != '\0'; p++)
217 h = (h << 5) - h + *p;
223 find_image_offset (GtkIconCache *cache,
224 const gchar *icon_name,
225 gint directory_index)
229 guint32 chain_offset;
231 guint32 image_list_offset, n_images;
234 chain_offset = cache->last_chain_offset;
237 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
238 gchar *name = cache->buffer + name_offset;
240 if (strcmp (name, icon_name) == 0)
244 hash_offset = GET_UINT32 (cache->buffer, 4);
245 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
246 hash = icon_name_hash (icon_name) % n_buckets;
248 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
249 while (chain_offset != 0xffffffff)
251 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
252 gchar *name = cache->buffer + name_offset;
254 if (strcmp (name, icon_name) == 0)
256 cache->last_chain_offset = chain_offset;
260 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
263 cache->last_chain_offset = 0;
267 /* We've found an icon list, now check if we have the right icon in it */
268 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
269 n_images = GET_UINT32 (cache->buffer, image_list_offset);
271 for (i = 0; i < n_images; i++)
273 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
275 return image_list_offset + 4 + 8 * i;
282 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
283 const gchar *icon_name,
284 gint directory_index)
286 guint32 image_offset;
288 image_offset = find_image_offset (cache, icon_name, directory_index);
293 return GET_UINT16 (cache->buffer, image_offset + 2);
297 _gtk_icon_cache_add_icons (GtkIconCache *cache,
298 const gchar *directory,
299 GHashTable *hash_table)
302 guint32 hash_offset, n_buckets;
303 guint32 chain_offset;
304 guint32 image_list_offset, n_images;
307 directory_index = get_directory_index (cache, directory);
309 if (directory_index == -1)
312 hash_offset = GET_UINT32 (cache->buffer, 4);
313 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
315 for (i = 0; i < n_buckets; i++)
317 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
318 while (chain_offset != 0xffffffff)
320 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
321 gchar *name = cache->buffer + name_offset;
323 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
324 n_images = GET_UINT32 (cache->buffer, image_list_offset);
326 for (j = 0; j < n_images; j++)
328 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
330 g_hash_table_insert (hash_table, name, NULL);
333 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
339 _gtk_icon_cache_has_icon (GtkIconCache *cache,
340 const gchar *icon_name)
344 guint32 chain_offset;
347 hash_offset = GET_UINT32 (cache->buffer, 4);
348 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
350 hash = icon_name_hash (icon_name) % n_buckets;
352 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
353 while (chain_offset != 0xffffffff)
355 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
356 gchar *name = cache->buffer + name_offset;
358 if (strcmp (name, icon_name) == 0)
361 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
368 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
369 const gchar *icon_name,
370 const gchar *directory)
374 guint32 chain_offset;
376 gboolean found_icon = FALSE;
377 gint directory_index;
379 directory_index = get_directory_index (cache, directory);
381 if (directory_index == -1)
384 hash_offset = GET_UINT32 (cache->buffer, 4);
385 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
387 hash = icon_name_hash (icon_name) % n_buckets;
389 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
390 while (chain_offset != 0xffffffff)
392 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
393 gchar *name = cache->buffer + name_offset;
395 if (strcmp (name, icon_name) == 0)
401 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
406 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
407 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
408 guint32 image_offset = image_list_offset + 4;
410 for (i = 0; i < n_images; i++)
412 guint16 index = GET_UINT16 (cache->buffer, image_offset);
414 if (index == directory_index)
424 pixbuf_destroy_cb (guchar *pixels,
427 GtkIconCache *cache = data;
429 _gtk_icon_cache_unref (cache);
433 _gtk_icon_cache_get_icon (GtkIconCache *cache,
434 const gchar *icon_name,
435 gint directory_index)
437 guint32 offset, image_data_offset, pixel_data_offset;
438 guint32 length, type;
441 GError *error = NULL;
443 offset = find_image_offset (cache, icon_name, directory_index);
445 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
447 if (!image_data_offset)
450 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
452 type = GET_UINT32 (cache->buffer, pixel_data_offset);
457 g_print ("invalid pixel data type %u\n", type));
461 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
463 if (!gdk_pixdata_deserialize (&pixdata, length,
464 (guchar *)(cache->buffer + pixel_data_offset + 8),
468 g_print ("could not deserialize data: %s\n", error->message));
469 g_error_free (error);
474 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
475 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
476 8, pixdata.width, pixdata.height, pixdata.rowstride,
477 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
482 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
483 g_error_free (error);
488 _gtk_icon_cache_ref (cache);
494 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
495 const gchar *icon_name,
496 gint directory_index)
498 guint32 offset, image_data_offset, meta_data_offset;
502 offset = find_image_offset (cache, icon_name, directory_index);
506 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
507 if (!image_data_offset)
510 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
512 if (!meta_data_offset)
515 data = g_slice_new0 (GtkIconData);
517 offset = GET_UINT32 (cache->buffer, meta_data_offset);
520 data->has_embedded_rect = TRUE;
521 data->x0 = GET_UINT16 (cache->buffer, offset);
522 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
523 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
524 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
527 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
530 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
531 data->attach_points = g_new (GdkPoint, data->n_attach_points);
532 for (i = 0; i < data->n_attach_points; i++)
534 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
535 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
539 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
545 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
547 n_names = GET_UINT32 (cache->buffer, offset);
549 for (i = 0; i < n_names; i++)
551 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
552 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
554 g_hash_table_insert (table, lang, name);
557 langs = (gchar **)g_get_language_names ();
558 for (i = 0; langs[i]; i++)
560 name = g_hash_table_lookup (table, langs[i]);
563 data->display_name = g_strdup (name);
568 g_hash_table_destroy (table);