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, see <http://www.gnu.org/licenses/>.
21 #include "gtkiconcache.h"
22 #include "gtkiconcachevalidator.h"
24 #include <glib/gstdio.h>
25 #include <gdk-pixbuf/gdk-pixdata.h>
34 #include <sys/types.h>
43 #define MAJOR_VERSION 1
44 #define MINOR_VERSION 0
46 #define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
47 #define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
50 struct _GtkIconCache {
56 guint32 last_chain_offset;
60 _gtk_icon_cache_ref (GtkIconCache *cache)
67 _gtk_icon_cache_unref (GtkIconCache *cache)
71 if (cache->ref_count == 0)
74 g_print ("unmapping icon cache\n"));
77 g_mapped_file_unref (cache->map);
83 _gtk_icon_cache_new_for_path (const gchar *path)
85 GtkIconCache *cache = NULL;
88 gchar *cache_filename;
93 /* Check if we have a cache file */
94 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
97 g_print ("look for cache in %s\n", path));
99 if (g_stat (path, &path_st) < 0)
102 /* Open the file and map it into memory */
103 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
110 /* Bug 660730: _fstat32 is only defined in msvcrt80.dll+/VS 2005+ */
111 /* or possibly in the msvcrt.dll linked to by the Windows DDK */
112 /* (will need to check on the Windows DDK part later) */
113 #if (_MSC_VER >= 1400 || __MSVCRT_VERSION__ >= 0x0800)
114 #undef fstat /* Just in case */
115 #define fstat _fstat32
119 if (fstat (fd, &st) < 0 || st.st_size < 4)
122 /* Verify cache is uptodate */
123 if (st.st_mtime < path_st.st_mtime)
126 g_print ("cache outdated\n"));
130 map = g_mapped_file_new (cache_filename, FALSE, NULL);
135 #ifdef G_ENABLE_DEBUG
136 if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
140 info.cache = g_mapped_file_get_contents (map);
141 info.cache_size = g_mapped_file_get_length (map);
142 info.n_directories = 0;
143 info.flags = CHECK_OFFSETS|CHECK_STRINGS;
145 if (!_gtk_icon_cache_validate (&info))
147 g_mapped_file_unref (map);
148 g_warning ("Icon cache '%s' is invalid\n", cache_filename);
155 GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
157 cache = g_new0 (GtkIconCache, 1);
158 cache->ref_count = 1;
160 cache->buffer = g_mapped_file_get_contents (map);
163 g_free (cache_filename);
171 _gtk_icon_cache_new (const gchar *data)
175 cache = g_new0 (GtkIconCache, 1);
176 cache->ref_count = 1;
178 cache->buffer = (gchar *)data;
184 get_directory_index (GtkIconCache *cache,
185 const gchar *directory)
187 guint32 dir_list_offset;
191 dir_list_offset = GET_UINT32 (cache->buffer, 8);
193 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
195 for (i = 0; i < n_dirs; i++)
197 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
198 gchar *name = cache->buffer + name_offset;
199 if (strcmp (name, directory) == 0)
207 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
208 const gchar *directory)
210 return get_directory_index (cache, directory);
214 icon_name_hash (gconstpointer key)
216 const signed char *p = key;
220 for (p += 1; *p != '\0'; p++)
221 h = (h << 5) - h + *p;
227 find_image_offset (GtkIconCache *cache,
228 const gchar *icon_name,
229 gint directory_index)
233 guint32 chain_offset;
235 guint32 image_list_offset, n_images;
238 chain_offset = cache->last_chain_offset;
241 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
242 gchar *name = cache->buffer + name_offset;
244 if (strcmp (name, icon_name) == 0)
248 hash_offset = GET_UINT32 (cache->buffer, 4);
249 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
250 hash = icon_name_hash (icon_name) % n_buckets;
252 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
253 while (chain_offset != 0xffffffff)
255 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
256 gchar *name = cache->buffer + name_offset;
258 if (strcmp (name, icon_name) == 0)
260 cache->last_chain_offset = chain_offset;
264 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
267 cache->last_chain_offset = 0;
271 /* We've found an icon list, now check if we have the right icon in it */
272 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
273 n_images = GET_UINT32 (cache->buffer, image_list_offset);
275 for (i = 0; i < n_images; i++)
277 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
279 return image_list_offset + 4 + 8 * i;
286 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
287 const gchar *icon_name,
288 gint directory_index)
290 guint32 image_offset;
292 image_offset = find_image_offset (cache, icon_name, directory_index);
297 return GET_UINT16 (cache->buffer, image_offset + 2);
301 _gtk_icon_cache_add_icons (GtkIconCache *cache,
302 const gchar *directory,
303 GHashTable *hash_table)
306 guint32 hash_offset, n_buckets;
307 guint32 chain_offset;
308 guint32 image_list_offset, n_images;
311 directory_index = get_directory_index (cache, directory);
313 if (directory_index == -1)
316 hash_offset = GET_UINT32 (cache->buffer, 4);
317 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
319 for (i = 0; i < n_buckets; i++)
321 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
322 while (chain_offset != 0xffffffff)
324 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
325 gchar *name = cache->buffer + name_offset;
327 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
328 n_images = GET_UINT32 (cache->buffer, image_list_offset);
330 for (j = 0; j < n_images; j++)
332 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
334 g_hash_table_insert (hash_table, name, NULL);
337 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
343 _gtk_icon_cache_has_icon (GtkIconCache *cache,
344 const gchar *icon_name)
348 guint32 chain_offset;
351 hash_offset = GET_UINT32 (cache->buffer, 4);
352 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
354 hash = icon_name_hash (icon_name) % n_buckets;
356 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
357 while (chain_offset != 0xffffffff)
359 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
360 gchar *name = cache->buffer + name_offset;
362 if (strcmp (name, icon_name) == 0)
365 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
372 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
373 const gchar *icon_name,
374 const gchar *directory)
378 guint32 chain_offset;
380 gboolean found_icon = FALSE;
381 gint directory_index;
383 directory_index = get_directory_index (cache, directory);
385 if (directory_index == -1)
388 hash_offset = GET_UINT32 (cache->buffer, 4);
389 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
391 hash = icon_name_hash (icon_name) % n_buckets;
393 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
394 while (chain_offset != 0xffffffff)
396 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
397 gchar *name = cache->buffer + name_offset;
399 if (strcmp (name, icon_name) == 0)
405 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
410 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
411 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
412 guint32 image_offset = image_list_offset + 4;
414 for (i = 0; i < n_images; i++)
416 guint16 index = GET_UINT16 (cache->buffer, image_offset);
418 if (index == directory_index)
428 pixbuf_destroy_cb (guchar *pixels,
431 GtkIconCache *cache = data;
433 _gtk_icon_cache_unref (cache);
437 _gtk_icon_cache_get_icon (GtkIconCache *cache,
438 const gchar *icon_name,
439 gint directory_index)
441 guint32 offset, image_data_offset, pixel_data_offset;
442 guint32 length, type;
445 GError *error = NULL;
447 offset = find_image_offset (cache, icon_name, directory_index);
452 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
454 if (!image_data_offset)
457 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
459 type = GET_UINT32 (cache->buffer, pixel_data_offset);
464 g_print ("invalid pixel data type %u\n", type));
468 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
470 if (!gdk_pixdata_deserialize (&pixdata, length,
471 (guchar *)(cache->buffer + pixel_data_offset + 8),
475 g_print ("could not deserialize data: %s\n", error->message));
476 g_error_free (error);
481 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
482 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
483 8, pixdata.width, pixdata.height, pixdata.rowstride,
484 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
489 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
490 g_error_free (error);
495 _gtk_icon_cache_ref (cache);
501 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
502 const gchar *icon_name,
503 gint directory_index)
505 guint32 offset, image_data_offset, meta_data_offset;
509 offset = find_image_offset (cache, icon_name, directory_index);
513 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
514 if (!image_data_offset)
517 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
519 if (!meta_data_offset)
522 data = g_slice_new0 (GtkIconData);
524 offset = GET_UINT32 (cache->buffer, meta_data_offset);
527 data->has_embedded_rect = TRUE;
528 data->x0 = GET_UINT16 (cache->buffer, offset);
529 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
530 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
531 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
534 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
537 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
538 data->attach_points = g_new (GdkPoint, data->n_attach_points);
539 for (i = 0; i < data->n_attach_points; i++)
541 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
542 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
546 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
552 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
554 n_names = GET_UINT32 (cache->buffer, offset);
556 for (i = 0; i < n_names; i++)
558 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
559 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
561 g_hash_table_insert (table, lang, name);
564 langs = (gchar **)g_get_language_names ();
565 for (i = 0; langs[i]; i++)
567 name = g_hash_table_lookup (table, langs[i]);
570 data->display_name = g_strdup (name);
575 g_hash_table_destroy (table);