]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconcache.c
Strip "Custom." prefix when getting default options from cups.
[~andy/gtk] / gtk / gtkiconcache.c
1 /* gtkiconcache.c
2  * Copyright (C) 2004  Anders Carlsson <andersca@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "gtkdebug.h"
23 #include "gtkiconcache.h"
24 #include "gtkiconcachevalidator.h"
25
26 #include <glib/gstdio.h>
27 #include <gdk-pixbuf/gdk-pixdata.h>
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef G_OS_WIN32
33 #include <io.h>
34 #endif
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <string.h>
39
40
41 #ifndef _O_BINARY
42 #define _O_BINARY 0
43 #endif
44
45 #define MAJOR_VERSION 1
46 #define MINOR_VERSION 0
47
48 #define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
49 #define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
50
51
52 struct _GtkIconCache {
53   gint ref_count;
54
55   GMappedFile *map;
56   gchar *buffer;
57
58   guint32 last_chain_offset;
59 };
60
61 GtkIconCache *
62 _gtk_icon_cache_ref (GtkIconCache *cache)
63 {
64   cache->ref_count++;
65   return cache;
66 }
67
68 void
69 _gtk_icon_cache_unref (GtkIconCache *cache)
70 {
71   cache->ref_count --;
72
73   if (cache->ref_count == 0)
74     {
75       GTK_NOTE (ICONTHEME, 
76                 g_print ("unmapping icon cache\n"));
77
78       if (cache->map)
79         g_mapped_file_unref (cache->map);
80       g_free (cache);
81     }
82 }
83
84 GtkIconCache *
85 _gtk_icon_cache_new_for_path (const gchar *path)
86 {
87   GtkIconCache *cache = NULL;
88   GMappedFile *map;
89
90   gchar *cache_filename;
91   gint fd = -1;
92   GStatBuf st;
93   GStatBuf path_st;
94
95    /* Check if we have a cache file */
96   cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
97
98   GTK_NOTE (ICONTHEME, 
99             g_print ("look for cache in %s\n", path));
100
101   if (g_stat (path, &path_st) < 0)
102     goto done;
103
104   /* Open the file and map it into memory */
105   fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
106
107   if (fd < 0)
108     goto done;
109
110 #ifdef G_OS_WIN32
111
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  
118 #endif
119 #endif
120
121   if (fstat (fd, &st) < 0 || st.st_size < 4)
122     goto done;
123
124   /* Verify cache is uptodate */
125   if (st.st_mtime < path_st.st_mtime)
126     {
127       GTK_NOTE (ICONTHEME, 
128                 g_print ("cache outdated\n"));
129       goto done; 
130     }
131
132   map = g_mapped_file_new (cache_filename, FALSE, NULL);
133
134   if (!map)
135     goto done;
136
137 #ifdef G_ENABLE_DEBUG
138   if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
139     {
140       CacheInfo info;
141
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;
146
147       if (!_gtk_icon_cache_validate (&info))
148         {
149           g_mapped_file_unref (map);
150           g_warning ("Icon cache '%s' is invalid\n", cache_filename);
151
152           goto done;
153         }
154     }
155 #endif 
156
157   GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
158
159   cache = g_new0 (GtkIconCache, 1);
160   cache->ref_count = 1;
161   cache->map = map;
162   cache->buffer = g_mapped_file_get_contents (map);
163
164  done:
165   g_free (cache_filename);  
166   if (fd >= 0)
167     close (fd);
168
169   return cache;
170 }
171
172 GtkIconCache *
173 _gtk_icon_cache_new (const gchar *data)
174 {
175   GtkIconCache *cache;
176
177   cache = g_new0 (GtkIconCache, 1);
178   cache->ref_count = 1;
179   cache->map = NULL;
180   cache->buffer = (gchar *)data;
181   
182   return cache;
183 }
184
185 static gint
186 get_directory_index (GtkIconCache *cache,
187                      const gchar *directory)
188 {
189   guint32 dir_list_offset;
190   gint n_dirs;
191   gint i;
192   
193   dir_list_offset = GET_UINT32 (cache->buffer, 8);
194
195   n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
196
197   for (i = 0; i < n_dirs; i++)
198     {
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)
202         return i;
203     }
204   
205   return -1;
206 }
207
208 gint
209 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
210                                      const gchar *directory)
211 {
212   return get_directory_index (cache, directory);
213 }
214
215 static guint
216 icon_name_hash (gconstpointer key)
217 {
218   const signed char *p = key;
219   guint32 h = *p;
220
221   if (h)
222     for (p += 1; *p != '\0'; p++)
223       h = (h << 5) - h + *p;
224
225   return h;
226 }
227
228 static gint
229 find_image_offset (GtkIconCache *cache,
230                    const gchar  *icon_name,
231                    gint          directory_index)
232 {
233   guint32 hash_offset;
234   guint32 n_buckets;
235   guint32 chain_offset;
236   int hash;
237   guint32 image_list_offset, n_images;
238   int i;
239
240   chain_offset = cache->last_chain_offset;
241   if (chain_offset)
242     {
243       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
244       gchar *name = cache->buffer + name_offset;
245
246       if (strcmp (name, icon_name) == 0)
247         goto find_dir;
248     }
249
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;
253
254   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
255   while (chain_offset != 0xffffffff)
256     {
257       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
258       gchar *name = cache->buffer + name_offset;
259
260       if (strcmp (name, icon_name) == 0)
261         {
262           cache->last_chain_offset = chain_offset;
263           goto find_dir;
264         }
265   
266       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
267     }
268
269   cache->last_chain_offset = 0;
270   return 0;
271
272 find_dir:
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);
276   
277   for (i = 0; i < n_images; i++)
278     {
279       if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
280           directory_index) 
281         return image_list_offset + 4 + 8 * i;
282     }
283
284   return 0;
285 }
286
287 gint
288 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
289                                 const gchar  *icon_name,
290                                 gint          directory_index)
291 {
292   guint32 image_offset;
293
294   image_offset = find_image_offset (cache, icon_name, directory_index);
295
296   if (!image_offset)
297     return 0;
298
299   return GET_UINT16 (cache->buffer, image_offset + 2);
300 }
301
302 void
303 _gtk_icon_cache_add_icons (GtkIconCache *cache,
304                            const gchar  *directory,
305                            GHashTable   *hash_table)
306 {
307   int directory_index;
308   guint32 hash_offset, n_buckets;
309   guint32 chain_offset;
310   guint32 image_list_offset, n_images;
311   int i, j;
312   
313   directory_index = get_directory_index (cache, directory);
314
315   if (directory_index == -1)
316     return;
317   
318   hash_offset = GET_UINT32 (cache->buffer, 4);
319   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
320
321   for (i = 0; i < n_buckets; i++)
322     {
323       chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
324       while (chain_offset != 0xffffffff)
325         {
326           guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
327           gchar *name = cache->buffer + name_offset;
328           
329           image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
330           n_images = GET_UINT32 (cache->buffer, image_list_offset);
331   
332           for (j = 0; j < n_images; j++)
333             {
334               if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
335                   directory_index)
336                 g_hash_table_insert (hash_table, name, NULL);
337             }
338
339           chain_offset = GET_UINT32 (cache->buffer, chain_offset);
340         }
341     }  
342 }
343
344 gboolean
345 _gtk_icon_cache_has_icon (GtkIconCache *cache,
346                           const gchar  *icon_name)
347 {
348   guint32 hash_offset;
349   guint32 n_buckets;
350   guint32 chain_offset;
351   gint hash;
352   
353   hash_offset = GET_UINT32 (cache->buffer, 4);
354   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
355
356   hash = icon_name_hash (icon_name) % n_buckets;
357
358   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
359   while (chain_offset != 0xffffffff)
360     {
361       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
362       gchar *name = cache->buffer + name_offset;
363
364       if (strcmp (name, icon_name) == 0)
365         return TRUE;
366           
367       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
368     }
369
370   return FALSE;
371 }
372
373 gboolean
374 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
375                                        const gchar  *icon_name,
376                                        const gchar  *directory)
377 {
378   guint32 hash_offset;
379   guint32 n_buckets;
380   guint32 chain_offset;
381   gint hash;
382   gboolean found_icon = FALSE;
383   gint directory_index;
384
385   directory_index = get_directory_index (cache, directory);
386
387   if (directory_index == -1)
388     return FALSE;
389   
390   hash_offset = GET_UINT32 (cache->buffer, 4);
391   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
392
393   hash = icon_name_hash (icon_name) % n_buckets;
394
395   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
396   while (chain_offset != 0xffffffff)
397     {
398       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
399       gchar *name = cache->buffer + name_offset;
400
401       if (strcmp (name, icon_name) == 0)
402         {
403           found_icon = TRUE;
404           break;
405         }
406           
407       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
408     }
409
410   if (found_icon)
411     {
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;
415       gint i;
416       for (i = 0; i < n_images; i++)
417         {
418           guint16 index = GET_UINT16 (cache->buffer, image_offset);
419           
420           if (index == directory_index)
421             return TRUE;
422           image_offset += 8;
423         }
424     }
425
426   return FALSE;
427 }
428
429 static void
430 pixbuf_destroy_cb (guchar   *pixels, 
431                    gpointer  data)
432 {
433   GtkIconCache *cache = data;
434
435   _gtk_icon_cache_unref (cache);
436 }
437
438 GdkPixbuf *
439 _gtk_icon_cache_get_icon (GtkIconCache *cache,
440                           const gchar  *icon_name,
441                           gint          directory_index)
442 {
443   guint32 offset, image_data_offset, pixel_data_offset;
444   guint32 length, type;
445   GdkPixbuf *pixbuf;
446   GdkPixdata pixdata;
447   GError *error = NULL;
448
449   offset = find_image_offset (cache, icon_name, directory_index);
450   
451   image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
452   
453   if (!image_data_offset)
454     return NULL;
455
456   pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
457
458   type = GET_UINT32 (cache->buffer, pixel_data_offset);
459
460   if (type != 0)
461     {
462       GTK_NOTE (ICONTHEME,
463                 g_print ("invalid pixel data type %u\n", type));
464       return NULL;
465     }
466
467   length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
468   
469   if (!gdk_pixdata_deserialize (&pixdata, length, 
470                                 (guchar *)(cache->buffer + pixel_data_offset + 8),
471                                 &error))
472     {
473       GTK_NOTE (ICONTHEME,
474                 g_print ("could not deserialize data: %s\n", error->message));
475       g_error_free (error);
476
477       return NULL;
478     }
479
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, 
484                                      cache);
485   if (!pixbuf)
486     {
487       GTK_NOTE (ICONTHEME,
488                 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
489       g_error_free (error);
490
491       return NULL;
492     }
493
494   _gtk_icon_cache_ref (cache);
495
496   return pixbuf;
497 }
498
499 GtkIconData  *
500 _gtk_icon_cache_get_icon_data  (GtkIconCache *cache,
501                                 const gchar  *icon_name,
502                                 gint          directory_index)
503 {
504   guint32 offset, image_data_offset, meta_data_offset;
505   GtkIconData *data;
506   int i;
507
508   offset = find_image_offset (cache, icon_name, directory_index);
509   if (!offset)
510     return NULL;
511
512   image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
513   if (!image_data_offset)
514     return NULL;
515
516   meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
517   
518   if (!meta_data_offset)
519     return NULL;
520
521   data = g_slice_new0 (GtkIconData);
522
523   offset = GET_UINT32 (cache->buffer, meta_data_offset);
524   if (offset)
525     {
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);
531     }
532
533   offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
534   if (offset)
535     {
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++)
539         {
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); 
542         }
543     }
544
545   offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
546   if (offset)
547     {
548       gint n_names;
549       gchar *lang, *name;
550       gchar **langs;
551       GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
552
553       n_names = GET_UINT32 (cache->buffer, offset);
554       
555       for (i = 0; i < n_names; i++)
556         {
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);
559           
560           g_hash_table_insert (table, lang, name);
561         }
562       
563       langs = (gchar **)g_get_language_names ();
564       for (i = 0; langs[i]; i++)
565         {
566           name = g_hash_table_lookup (table, langs[i]);
567           if (name)
568             {
569               data->display_name = g_strdup (name);
570               break;
571             }
572         }
573
574       g_hash_table_destroy (table);
575     }
576
577   return data;
578 }
579