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"));
73 g_mapped_file_free (cache->map);
79 _gtk_icon_cache_new_for_path (const gchar *path)
81 GtkIconCache *cache = NULL;
84 gchar *cache_filename;
90 /* Check if we have a cache file */
91 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
94 g_print ("look for cache in %s\n", path));
96 if (g_stat (path, &path_st) < 0)
99 /* Open the file and map it into memory */
100 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
105 if (fstat (fd, &st) < 0 || st.st_size < 4)
108 /* Verify cache is uptodate */
109 if (st.st_mtime < path_st.st_mtime)
112 g_print ("cache outdated\n"));
116 map = g_mapped_file_new (cache_filename, FALSE, NULL);
122 buffer = g_mapped_file_get_contents (map);
123 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
124 GET_UINT16 (buffer, 2) != MINOR_VERSION)
126 g_mapped_file_free (map);
129 g_print ("wrong cache version\n"));
134 g_print ("found cache for %s\n", path));
136 cache = g_new0 (GtkIconCache, 1);
137 cache->ref_count = 1;
139 cache->buffer = buffer;
142 g_free (cache_filename);
150 get_directory_index (GtkIconCache *cache,
151 const gchar *directory)
153 guint32 dir_list_offset;
157 dir_list_offset = GET_UINT32 (cache->buffer, 8);
159 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
161 for (i = 0; i < n_dirs; i++)
163 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
164 gchar *name = cache->buffer + name_offset;
165 if (strcmp (name, directory) == 0)
173 _gtk_icon_cache_has_directory (GtkIconCache *cache,
174 const gchar *directory)
176 return get_directory_index (cache, directory) != -1;
180 icon_name_hash (gconstpointer key)
182 const signed char *p = key;
186 for (p += 1; *p != '\0'; p++)
187 h = (h << 5) - h + *p;
193 find_image_offset (GtkIconCache *cache,
194 const gchar *icon_name,
195 const gchar *directory)
199 guint32 chain_offset;
200 int hash, directory_index;
201 guint32 image_list_offset, n_images;
202 gboolean found = FALSE;
205 hash_offset = GET_UINT32 (cache->buffer, 4);
206 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
208 hash = icon_name_hash (icon_name) % n_buckets;
210 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
211 while (chain_offset != 0xffffffff)
213 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
214 gchar *name = cache->buffer + name_offset;
216 if (strcmp (name, icon_name) == 0)
222 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
229 /* We've found an icon list, now check if we have the right icon in it */
230 directory_index = get_directory_index (cache, directory);
231 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
232 n_images = GET_UINT32 (cache->buffer, image_list_offset);
234 for (i = 0; i < n_images; i++)
236 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
238 return image_list_offset + 4 + 8 * i;
245 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
246 const gchar *icon_name,
247 const gchar *directory)
249 guint32 image_offset;
251 image_offset = find_image_offset (cache, icon_name, directory);
256 return GET_UINT16 (cache->buffer, image_offset + 2);
260 _gtk_icon_cache_add_icons (GtkIconCache *cache,
261 const gchar *directory,
262 GHashTable *hash_table)
265 guint32 hash_offset, n_buckets;
266 guint32 chain_offset;
267 guint32 image_list_offset, n_images;
270 directory_index = get_directory_index (cache, directory);
272 if (directory_index == -1)
275 hash_offset = GET_UINT32 (cache->buffer, 4);
276 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
278 for (i = 0; i < n_buckets; i++)
280 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
281 while (chain_offset != 0xffffffff)
283 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
284 gchar *name = cache->buffer + name_offset;
286 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
287 n_images = GET_UINT32 (cache->buffer, image_list_offset);
289 for (j = 0; j < n_images; j++)
291 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
293 g_hash_table_insert (hash_table, name, NULL);
296 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
302 _gtk_icon_cache_has_icon (GtkIconCache *cache,
303 const gchar *icon_name)
307 guint32 chain_offset;
310 hash_offset = GET_UINT32 (cache->buffer, 4);
311 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
313 hash = icon_name_hash (icon_name) % n_buckets;
315 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
316 while (chain_offset != 0xffffffff)
318 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
319 gchar *name = cache->buffer + name_offset;
321 if (strcmp (name, icon_name) == 0)
324 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
331 pixbuf_destroy_cb (guchar *pixels,
334 GtkIconCache *cache = data;
336 _gtk_icon_cache_unref (cache);
340 _gtk_icon_cache_get_icon (GtkIconCache *cache,
341 const gchar *icon_name,
342 const gchar *directory)
344 guint32 offset, image_data_offset, pixel_data_offset;
345 guint32 length, type;
348 GError *error = NULL;
350 offset = find_image_offset (cache, icon_name, directory);
352 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
354 if (!image_data_offset)
357 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
359 type = GET_UINT32 (cache->buffer, pixel_data_offset);
364 g_print ("invalid pixel data type %d\n", type));
368 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
370 if (!gdk_pixdata_deserialize (&pixdata, length,
371 cache->buffer + pixel_data_offset + 8,
375 g_print ("could not deserialize data: %s\n", error->message));
376 g_error_free (error);
381 pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
382 (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
383 8, pixdata.width, pixdata.height, pixdata.rowstride,
384 (GdkPixbufDestroyNotify)pixbuf_destroy_cb,
389 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
390 g_error_free (error);
395 _gtk_icon_cache_ref (cache);
401 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
402 const gchar *icon_name,
403 const gchar *directory)
405 guint32 offset, image_data_offset, meta_data_offset;
409 offset = find_image_offset (cache, icon_name, directory);
413 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
414 if (!image_data_offset)
417 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
419 if (!meta_data_offset)
422 data = g_new0 (GtkIconData, 1);
424 offset = GET_UINT32 (cache->buffer, meta_data_offset);
427 data->has_embedded_rect = TRUE;
428 data->x0 = GET_UINT16 (cache->buffer, offset);
429 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
430 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
431 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
434 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
437 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
438 data->attach_points = g_new (GdkPoint, data->n_attach_points);
439 for (i = 0; i < data->n_attach_points; i++)
441 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
442 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
446 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
452 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
454 n_names = GET_UINT32 (cache->buffer, offset);
456 for (i = 0; i < n_names; i++)
458 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
459 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
461 g_hash_table_insert (table, lang, name);
464 langs = (gchar **)g_get_language_names ();
465 for (i = 0; langs[i]; i++)
467 name = g_hash_table_lookup (table, langs[i]);
470 data->display_name = g_strdup (name);
475 g_hash_table_destroy (table);