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);
112 /* Bug 660730: _fstat32 is only defined in msvcrt80.dll+/VS 2005+ */
113 /* or possibly in the msvcrt.dll linked to by the Windows DDK */
114 /* (will need to check on the Windows DDK part later) */
115 #if (_MSC_VER >= 1400 || __MSVCRT_VERSION__ >= 0x0800)
116 #undef fstat /* Just in case */
117 #define fstat _fstat32
121 if (fstat (fd, &st) < 0 || st.st_size < 4)
124 /* Verify cache is uptodate */
125 if (st.st_mtime < path_st.st_mtime)
128 g_print ("cache outdated\n"));
132 map = g_mapped_file_new (cache_filename, FALSE, NULL);
137 #ifdef G_ENABLE_DEBUG
138 if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
142 info.cache = g_mapped_file_get_contents (map);
143 info.cache_size = g_mapped_file_get_length (map);
144 info.n_directories = 0;
145 info.flags = CHECK_OFFSETS|CHECK_STRINGS;
147 if (!_gtk_icon_cache_validate (&info))
149 g_mapped_file_unref (map);
150 g_warning ("Icon cache '%s' is invalid\n", cache_filename);
157 GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
159 cache = g_new0 (GtkIconCache, 1);
160 cache->ref_count = 1;
162 cache->buffer = g_mapped_file_get_contents (map);
165 g_free (cache_filename);
173 _gtk_icon_cache_new (const gchar *data)
177 cache = g_new0 (GtkIconCache, 1);
178 cache->ref_count = 1;
180 cache->buffer = (gchar *)data;
186 get_directory_index (GtkIconCache *cache,
187 const gchar *directory)
189 guint32 dir_list_offset;
193 dir_list_offset = GET_UINT32 (cache->buffer, 8);
195 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
197 for (i = 0; i < n_dirs; i++)
199 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
200 gchar *name = cache->buffer + name_offset;
201 if (strcmp (name, directory) == 0)
209 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
210 const gchar *directory)
212 return get_directory_index (cache, directory);
216 icon_name_hash (gconstpointer key)
218 const signed char *p = key;
222 for (p += 1; *p != '\0'; p++)
223 h = (h << 5) - h + *p;
229 find_image_offset (GtkIconCache *cache,
230 const gchar *icon_name,
231 gint directory_index)
235 guint32 chain_offset;
237 guint32 image_list_offset, n_images;
240 chain_offset = cache->last_chain_offset;
243 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
244 gchar *name = cache->buffer + name_offset;
246 if (strcmp (name, icon_name) == 0)
250 hash_offset = GET_UINT32 (cache->buffer, 4);
251 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
252 hash = icon_name_hash (icon_name) % n_buckets;
254 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
255 while (chain_offset != 0xffffffff)
257 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
258 gchar *name = cache->buffer + name_offset;
260 if (strcmp (name, icon_name) == 0)
262 cache->last_chain_offset = chain_offset;
266 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
269 cache->last_chain_offset = 0;
273 /* We've found an icon list, now check if we have the right icon in it */
274 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
275 n_images = GET_UINT32 (cache->buffer, image_list_offset);
277 for (i = 0; i < n_images; i++)
279 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
281 return image_list_offset + 4 + 8 * i;
288 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
289 const gchar *icon_name,
290 gint directory_index)
292 guint32 image_offset;
294 image_offset = find_image_offset (cache, icon_name, directory_index);
299 return GET_UINT16 (cache->buffer, image_offset + 2);
303 _gtk_icon_cache_add_icons (GtkIconCache *cache,
304 const gchar *directory,
305 GHashTable *hash_table)
308 guint32 hash_offset, n_buckets;
309 guint32 chain_offset;
310 guint32 image_list_offset, n_images;
313 directory_index = get_directory_index (cache, directory);
315 if (directory_index == -1)
318 hash_offset = GET_UINT32 (cache->buffer, 4);
319 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
321 for (i = 0; i < n_buckets; i++)
323 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
324 while (chain_offset != 0xffffffff)
326 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
327 gchar *name = cache->buffer + name_offset;
329 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
330 n_images = GET_UINT32 (cache->buffer, image_list_offset);
332 for (j = 0; j < n_images; j++)
334 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
336 g_hash_table_insert (hash_table, name, NULL);
339 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
345 _gtk_icon_cache_has_icon (GtkIconCache *cache,
346 const gchar *icon_name)
350 guint32 chain_offset;
353 hash_offset = GET_UINT32 (cache->buffer, 4);
354 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
356 hash = icon_name_hash (icon_name) % n_buckets;
358 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
359 while (chain_offset != 0xffffffff)
361 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
362 gchar *name = cache->buffer + name_offset;
364 if (strcmp (name, icon_name) == 0)
367 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
374 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
375 const gchar *icon_name,
376 const gchar *directory)
380 guint32 chain_offset;
382 gboolean found_icon = FALSE;
383 gint directory_index;
385 directory_index = get_directory_index (cache, directory);
387 if (directory_index == -1)
390 hash_offset = GET_UINT32 (cache->buffer, 4);
391 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
393 hash = icon_name_hash (icon_name) % n_buckets;
395 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
396 while (chain_offset != 0xffffffff)
398 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
399 gchar *name = cache->buffer + name_offset;
401 if (strcmp (name, icon_name) == 0)
407 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
412 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
413 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
414 guint32 image_offset = image_list_offset + 4;
416 for (i = 0; i < n_images; i++)
418 guint16 index = GET_UINT16 (cache->buffer, image_offset);
420 if (index == directory_index)
430 pixbuf_destroy_cb (guchar *pixels,
433 GtkIconCache *cache = data;
435 _gtk_icon_cache_unref (cache);
439 _gtk_icon_cache_get_icon (GtkIconCache *cache,
440 const gchar *icon_name,
441 gint directory_index)
443 guint32 offset, image_data_offset, pixel_data_offset;
444 guint32 length, type;
447 GError *error = NULL;
449 offset = find_image_offset (cache, icon_name, directory_index);
451 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
453 if (!image_data_offset)
456 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
458 type = GET_UINT32 (cache->buffer, pixel_data_offset);
463 g_print ("invalid pixel data type %u\n", type));
467 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
469 if (!gdk_pixdata_deserialize (&pixdata, length,
470 (guchar *)(cache->buffer + pixel_data_offset + 8),
474 g_print ("could not deserialize data: %s\n", error->message));
475 g_error_free (error);
480 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
481 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
482 8, pixdata.width, pixdata.height, pixdata.rowstride,
483 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
488 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
489 g_error_free (error);
494 _gtk_icon_cache_ref (cache);
500 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
501 const gchar *icon_name,
502 gint directory_index)
504 guint32 offset, image_data_offset, meta_data_offset;
508 offset = find_image_offset (cache, icon_name, directory_index);
512 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
513 if (!image_data_offset)
516 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
518 if (!meta_data_offset)
521 data = g_slice_new0 (GtkIconData);
523 offset = GET_UINT32 (cache->buffer, meta_data_offset);
526 data->has_embedded_rect = TRUE;
527 data->x0 = GET_UINT16 (cache->buffer, offset);
528 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
529 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
530 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
533 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
536 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
537 data->attach_points = g_new (GdkPoint, data->n_attach_points);
538 for (i = 0; i < data->n_attach_points; i++)
540 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
541 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
545 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
551 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
553 n_names = GET_UINT32 (cache->buffer, offset);
555 for (i = 0; i < n_names; i++)
557 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
558 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
560 g_hash_table_insert (table, lang, name);
563 langs = (gchar **)g_get_language_names ();
564 for (i = 0; langs[i]; i++)
566 name = g_hash_table_lookup (table, langs[i]);
569 data->display_name = g_strdup (name);
574 g_hash_table_destroy (table);