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