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 <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))))
49 struct _GtkIconCache {
57 _gtk_icon_cache_ref (GtkIconCache *cache)
64 _gtk_icon_cache_unref (GtkIconCache *cache)
68 if (cache->ref_count == 0)
71 g_print ("unmapping icon cache\n"));
74 g_mapped_file_free (cache->map);
80 _gtk_icon_cache_new_for_path (const gchar *path)
82 GtkIconCache *cache = NULL;
85 gchar *cache_filename;
91 /* Check if we have a cache file */
92 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
95 g_print ("look for cache in %s\n", path));
97 if (g_stat (path, &path_st) < 0)
100 /* Open the file and map it into memory */
101 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
106 if (fstat (fd, &st) < 0 || st.st_size < 4)
109 /* Verify cache is uptodate */
110 if (st.st_mtime < path_st.st_mtime)
113 g_print ("cache outdated\n"));
117 map = g_mapped_file_new (cache_filename, FALSE, NULL);
123 buffer = g_mapped_file_get_contents (map);
124 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
125 GET_UINT16 (buffer, 2) != MINOR_VERSION)
127 g_mapped_file_free (map);
130 g_print ("wrong cache version\n"));
135 g_print ("found cache for %s\n", path));
137 cache = g_new0 (GtkIconCache, 1);
138 cache->ref_count = 1;
140 cache->buffer = buffer;
143 g_free (cache_filename);
151 _gtk_icon_cache_new (const gchar *data)
155 cache = g_new0 (GtkIconCache, 1);
156 cache->ref_count = 1;
158 cache->buffer = (gchar *)data;
164 get_directory_index (GtkIconCache *cache,
165 const gchar *directory)
167 guint32 dir_list_offset;
171 dir_list_offset = GET_UINT32 (cache->buffer, 8);
173 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
175 for (i = 0; i < n_dirs; i++)
177 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
178 gchar *name = cache->buffer + name_offset;
179 if (strcmp (name, directory) == 0)
187 _gtk_icon_cache_has_directory (GtkIconCache *cache,
188 const gchar *directory)
190 return get_directory_index (cache, directory) != -1;
194 icon_name_hash (gconstpointer key)
196 const signed char *p = key;
200 for (p += 1; *p != '\0'; p++)
201 h = (h << 5) - h + *p;
207 find_image_offset (GtkIconCache *cache,
208 const gchar *icon_name,
209 const gchar *directory)
213 guint32 chain_offset;
214 int hash, directory_index;
215 guint32 image_list_offset, n_images;
216 gboolean found = FALSE;
219 hash_offset = GET_UINT32 (cache->buffer, 4);
220 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
222 hash = icon_name_hash (icon_name) % n_buckets;
224 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
225 while (chain_offset != 0xffffffff)
227 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
228 gchar *name = cache->buffer + name_offset;
230 if (strcmp (name, icon_name) == 0)
236 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
243 /* We've found an icon list, now check if we have the right icon in it */
244 directory_index = get_directory_index (cache, directory);
245 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
246 n_images = GET_UINT32 (cache->buffer, image_list_offset);
248 for (i = 0; i < n_images; i++)
250 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
252 return image_list_offset + 4 + 8 * i;
259 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
260 const gchar *icon_name,
261 const gchar *directory)
263 guint32 image_offset;
265 image_offset = find_image_offset (cache, icon_name, directory);
270 return GET_UINT16 (cache->buffer, image_offset + 2);
274 _gtk_icon_cache_add_icons (GtkIconCache *cache,
275 const gchar *directory,
276 GHashTable *hash_table)
279 guint32 hash_offset, n_buckets;
280 guint32 chain_offset;
281 guint32 image_list_offset, n_images;
284 directory_index = get_directory_index (cache, directory);
286 if (directory_index == -1)
289 hash_offset = GET_UINT32 (cache->buffer, 4);
290 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
292 for (i = 0; i < n_buckets; i++)
294 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
295 while (chain_offset != 0xffffffff)
297 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
298 gchar *name = cache->buffer + name_offset;
300 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
301 n_images = GET_UINT32 (cache->buffer, image_list_offset);
303 for (j = 0; j < n_images; j++)
305 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
307 g_hash_table_insert (hash_table, name, NULL);
310 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
316 _gtk_icon_cache_has_icon (GtkIconCache *cache,
317 const gchar *icon_name)
321 guint32 chain_offset;
324 hash_offset = GET_UINT32 (cache->buffer, 4);
325 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
327 hash = icon_name_hash (icon_name) % n_buckets;
329 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
330 while (chain_offset != 0xffffffff)
332 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
333 gchar *name = cache->buffer + name_offset;
335 if (strcmp (name, icon_name) == 0)
338 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
345 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
346 const gchar *icon_name,
347 const gchar *directory)
351 guint32 chain_offset;
353 gboolean found_icon = FALSE;
354 gint directory_index;
356 directory_index = get_directory_index (cache, directory);
358 if (directory_index == -1)
361 hash_offset = GET_UINT32 (cache->buffer, 4);
362 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
364 hash = icon_name_hash (icon_name) % n_buckets;
366 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
367 while (chain_offset != 0xffffffff)
369 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
370 gchar *name = cache->buffer + name_offset;
372 if (strcmp (name, icon_name) == 0)
378 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
383 guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
384 guint32 n_images = GET_UINT32 (cache->buffer, image_list_offset);
385 guint32 image_offset = image_list_offset + 4;
387 for (i = 0; i < n_images; i++)
389 guint16 index = GET_UINT16 (cache->buffer, image_offset);
391 if (index == directory_index)
401 pixbuf_destroy_cb (guchar *pixels,
404 GtkIconCache *cache = data;
406 _gtk_icon_cache_unref (cache);
410 _gtk_icon_cache_get_icon (GtkIconCache *cache,
411 const gchar *icon_name,
412 const gchar *directory)
414 guint32 offset, image_data_offset, pixel_data_offset;
415 guint32 length, type;
418 GError *error = NULL;
420 offset = find_image_offset (cache, icon_name, directory);
422 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
424 if (!image_data_offset)
427 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
429 type = GET_UINT32 (cache->buffer, pixel_data_offset);
434 g_print ("invalid pixel data type %d\n", type));
438 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
440 if (!gdk_pixdata_deserialize (&pixdata, length,
441 cache->buffer + pixel_data_offset + 8,
445 g_print ("could not deserialize data: %s\n", error->message));
446 g_error_free (error);
451 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
452 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
453 8, pixdata.width, pixdata.height, pixdata.rowstride,
454 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
459 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
460 g_error_free (error);
465 _gtk_icon_cache_ref (cache);
471 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
472 const gchar *icon_name,
473 const gchar *directory)
475 guint32 offset, image_data_offset, meta_data_offset;
479 offset = find_image_offset (cache, icon_name, directory);
483 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
484 if (!image_data_offset)
487 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
489 if (!meta_data_offset)
492 data = g_slice_new0 (GtkIconData);
494 offset = GET_UINT32 (cache->buffer, meta_data_offset);
497 data->has_embedded_rect = TRUE;
498 data->x0 = GET_UINT16 (cache->buffer, offset);
499 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
500 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
501 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
504 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
507 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
508 data->attach_points = g_new (GdkPoint, data->n_attach_points);
509 for (i = 0; i < data->n_attach_points; i++)
511 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
512 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
516 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
522 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
524 n_names = GET_UINT32 (cache->buffer, offset);
526 for (i = 0; i < n_names; i++)
528 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
529 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
531 g_hash_table_insert (table, lang, name);
534 langs = (gchar **)g_get_language_names ();
535 for (i = 0; langs[i]; i++)
537 name = g_hash_table_lookup (table, langs[i]);
540 data->display_name = g_strdup (name);
545 g_hash_table_destroy (table);