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.
22 #include "gtkiconcache.h"
23 #include <glib/gstdio.h>
24 #include <gdk-pixbuf/gdk-pixdata.h>
33 #include <sys/types.h>
46 #define MAP_FAILED ((void *) -1)
49 #define MAJOR_VERSION 1
50 #define MINOR_VERSION 0
52 #define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
53 #define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
55 struct _GtkIconCache {
66 _gtk_icon_cache_ref (GtkIconCache *cache)
73 _gtk_icon_cache_unref (GtkIconCache *cache)
77 if (cache->ref_count == 0)
80 g_print ("unmapping icon cache\n"));
82 munmap (cache->buffer, cache->size);
85 UnmapViewOfFile (cache->buffer);
86 CloseHandle (cache->handle);
93 _gtk_icon_cache_new_for_path (const gchar *path)
95 GtkIconCache *cache = NULL;
97 #if defined(HAVE_MMAP) || defined(G_OS_WIN32)
98 gchar *cache_filename;
102 gchar *buffer = NULL;
104 HANDLE handle = NULL;
107 /* Check if we have a cache file */
108 cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
111 g_print ("look for cache in %s\n", path));
113 if (!g_file_test (cache_filename, G_FILE_TEST_IS_REGULAR))
116 if (g_stat (path, &path_st) < 0)
119 /* Open the file and map it into memory */
120 fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
124 g_free (cache_filename);
128 if (fstat (fd, &st) < 0 || st.st_size < 4)
131 /* Verify cache is uptodate */
132 if (st.st_mtime < path_st.st_mtime)
135 g_print ("cache outdated\n"));
140 buffer = (gchar *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
142 if (buffer == MAP_FAILED)
145 handle = CreateFileMapping (_get_osfhandle (fd), NULL, PAGE_READONLY,
150 buffer = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, 0);
154 CloseHandle (handle);
160 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
161 GET_UINT16 (buffer, 2) != MINOR_VERSION)
164 munmap (buffer, st.st_size);
166 UnmapViewOfFile (buffer);
167 CloseHandle (handle);
170 g_print ("wrong cache version\n"));
175 g_print ("found cache for %s\n", path));
177 cache = g_new0 (GtkIconCache, 1);
178 cache->ref_count = 1;
179 cache->buffer = buffer;
181 cache->handle = handle;
183 cache->size = st.st_size;
185 g_free (cache_filename);
189 #endif /* HAVE_MMAP || G_OS_WIN32 */
195 get_directory_index (GtkIconCache *cache,
196 const gchar *directory)
198 guint32 dir_list_offset;
202 dir_list_offset = GET_UINT32 (cache->buffer, 8);
204 n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
206 for (i = 0; i < n_dirs; i++)
208 guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
209 gchar *name = cache->buffer + name_offset;
210 if (strcmp (name, directory) == 0)
218 _gtk_icon_cache_has_directory (GtkIconCache *cache,
219 const gchar *directory)
221 return get_directory_index (cache, directory) != -1;
225 icon_name_hash (gconstpointer key)
227 const signed char *p = key;
231 for (p += 1; *p != '\0'; p++)
232 h = (h << 5) - h + *p;
238 find_image_offset (GtkIconCache *cache,
239 const gchar *icon_name,
240 const gchar *directory)
244 guint32 chain_offset;
245 int hash, directory_index;
246 guint32 image_list_offset, n_images;
247 gboolean found = FALSE;
250 hash_offset = GET_UINT32 (cache->buffer, 4);
251 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
253 hash = icon_name_hash (icon_name) % n_buckets;
255 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
256 while (chain_offset != 0xffffffff)
258 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
259 gchar *name = cache->buffer + name_offset;
261 if (strcmp (name, icon_name) == 0)
267 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
274 /* We've found an icon list, now check if we have the right icon in it */
275 directory_index = get_directory_index (cache, directory);
276 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
277 n_images = GET_UINT32 (cache->buffer, image_list_offset);
279 for (i = 0; i < n_images; i++)
281 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
283 return image_list_offset + 4 + 8 * i;
290 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
291 const gchar *icon_name,
292 const gchar *directory)
294 guint32 image_offset;
296 image_offset = find_image_offset (cache, icon_name, directory);
301 return GET_UINT16 (cache->buffer, image_offset + 2);
305 _gtk_icon_cache_add_icons (GtkIconCache *cache,
306 const gchar *directory,
307 GHashTable *hash_table)
310 guint32 hash_offset, n_buckets;
311 guint32 chain_offset;
312 guint32 image_list_offset, n_images;
315 directory_index = get_directory_index (cache, directory);
317 if (directory_index == -1)
320 hash_offset = GET_UINT32 (cache->buffer, 4);
321 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
323 for (i = 0; i < n_buckets; i++)
325 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
326 while (chain_offset != 0xffffffff)
328 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
329 gchar *name = cache->buffer + name_offset;
331 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
332 n_images = GET_UINT32 (cache->buffer, image_list_offset);
334 for (j = 0; j < n_images; j++)
336 if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
338 g_hash_table_insert (hash_table, name, NULL);
341 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
347 _gtk_icon_cache_has_icon (GtkIconCache *cache,
348 const gchar *icon_name)
352 guint32 chain_offset;
355 hash_offset = GET_UINT32 (cache->buffer, 4);
356 n_buckets = GET_UINT32 (cache->buffer, hash_offset);
358 hash = icon_name_hash (icon_name) % n_buckets;
360 chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
361 while (chain_offset != 0xffffffff)
363 guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
364 gchar *name = cache->buffer + name_offset;
366 if (strcmp (name, icon_name) == 0)
369 chain_offset = GET_UINT32 (cache->buffer, chain_offset);
376 _gtk_icon_cache_get_icon (GtkIconCache *cache,
377 const gchar *icon_name,
378 const gchar *directory)
380 guint32 offset, image_data_offset, pixel_data_offset;
381 guint32 length, type;
384 GError *error = NULL;
386 offset = find_image_offset (cache, icon_name, directory);
388 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
390 if (!image_data_offset)
393 pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
395 type = GET_UINT32 (cache->buffer, pixel_data_offset);
400 g_print ("invalid pixel data type %d\n", type));
404 length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
406 if (!gdk_pixdata_deserialize (&pixdata, length,
407 cache->buffer + pixel_data_offset + 8,
411 g_print ("could not deserialize data: %s\n", error->message));
412 g_error_free (error);
417 pixbuf = gdk_pixbuf_from_pixdata (&pixdata, FALSE, &error);
422 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
423 g_error_free (error);
432 _gtk_icon_cache_get_icon_data (GtkIconCache *cache,
433 const gchar *icon_name,
434 const gchar *directory)
436 guint32 offset, image_data_offset, meta_data_offset;
440 offset = find_image_offset (cache, icon_name, directory);
444 image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
445 if (!image_data_offset)
448 meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
450 if (!meta_data_offset)
453 data = g_new0 (GtkIconData, 1);
455 offset = GET_UINT32 (cache->buffer, meta_data_offset);
458 data->has_embedded_rect = TRUE;
459 data->x0 = GET_UINT16 (cache->buffer, offset);
460 data->y0 = GET_UINT16 (cache->buffer, offset + 2);
461 data->x1 = GET_UINT16 (cache->buffer, offset + 4);
462 data->y1 = GET_UINT16 (cache->buffer, offset + 6);
465 offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
468 data->n_attach_points = GET_UINT32 (cache->buffer, offset);
469 data->attach_points = g_new (GdkPoint, data->n_attach_points);
470 for (i = 0; i < data->n_attach_points; i++)
472 data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i);
473 data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2);
477 offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
483 GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
485 n_names = GET_UINT32 (cache->buffer, offset);
487 for (i = 0; i < n_names; i++)
489 lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
490 name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
492 g_hash_table_insert (table, lang, name);
495 langs = (gchar **)g_get_language_names ();
496 for (i = 0; langs[i]; i++)
498 name = g_hash_table_lookup (table, langs[i]);
501 data->display_name = g_strdup (name);
506 g_hash_table_destroy (table);