]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconcache.c
iconcache: Fix gcc warning
[~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 #undef fstat /* Just in case */
112 #define fstat _fstat32  
113 #endif
114
115   if (fstat (fd, &st) < 0 || st.st_size < 4)
116     goto done;
117
118   /* Verify cache is uptodate */
119   if (st.st_mtime < path_st.st_mtime)
120     {
121       GTK_NOTE (ICONTHEME, 
122                 g_print ("cache outdated\n"));
123       goto done; 
124     }
125
126   map = g_mapped_file_new (cache_filename, FALSE, NULL);
127
128   if (!map)
129     goto done;
130
131 #ifdef G_ENABLE_DEBUG
132   if (gtk_get_debug_flags () & GTK_DEBUG_ICONTHEME)
133     {
134       CacheInfo info;
135
136       info.cache = g_mapped_file_get_contents (map);
137       info.cache_size = g_mapped_file_get_length (map);
138       info.n_directories = 0;
139       info.flags = CHECK_OFFSETS|CHECK_STRINGS;
140
141       if (!_gtk_icon_cache_validate (&info))
142         {
143           g_mapped_file_unref (map);
144           g_warning ("Icon cache '%s' is invalid\n", cache_filename);
145
146           goto done;
147         }
148     }
149 #endif 
150
151   GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
152
153   cache = g_new0 (GtkIconCache, 1);
154   cache->ref_count = 1;
155   cache->map = map;
156   cache->buffer = g_mapped_file_get_contents (map);
157
158  done:
159   g_free (cache_filename);  
160   if (fd >= 0)
161     close (fd);
162
163   return cache;
164 }
165
166 GtkIconCache *
167 _gtk_icon_cache_new (const gchar *data)
168 {
169   GtkIconCache *cache;
170
171   cache = g_new0 (GtkIconCache, 1);
172   cache->ref_count = 1;
173   cache->map = NULL;
174   cache->buffer = (gchar *)data;
175   
176   return cache;
177 }
178
179 static gint
180 get_directory_index (GtkIconCache *cache,
181                      const gchar *directory)
182 {
183   guint32 dir_list_offset;
184   gint n_dirs;
185   gint i;
186   
187   dir_list_offset = GET_UINT32 (cache->buffer, 8);
188
189   n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
190
191   for (i = 0; i < n_dirs; i++)
192     {
193       guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
194       gchar *name = cache->buffer + name_offset;
195       if (strcmp (name, directory) == 0)
196         return i;
197     }
198   
199   return -1;
200 }
201
202 gint
203 _gtk_icon_cache_get_directory_index (GtkIconCache *cache,
204                                      const gchar *directory)
205 {
206   return get_directory_index (cache, directory);
207 }
208
209 static guint
210 icon_name_hash (gconstpointer key)
211 {
212   const signed char *p = key;
213   guint32 h = *p;
214
215   if (h)
216     for (p += 1; *p != '\0'; p++)
217       h = (h << 5) - h + *p;
218
219   return h;
220 }
221
222 static gint
223 find_image_offset (GtkIconCache *cache,
224                    const gchar  *icon_name,
225                    gint          directory_index)
226 {
227   guint32 hash_offset;
228   guint32 n_buckets;
229   guint32 chain_offset;
230   int hash;
231   guint32 image_list_offset, n_images;
232   int i;
233
234   chain_offset = cache->last_chain_offset;
235   if (chain_offset)
236     {
237       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
238       gchar *name = cache->buffer + name_offset;
239
240       if (strcmp (name, icon_name) == 0)
241         goto find_dir;
242     }
243
244   hash_offset = GET_UINT32 (cache->buffer, 4);
245   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
246   hash = icon_name_hash (icon_name) % n_buckets;
247
248   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
249   while (chain_offset != 0xffffffff)
250     {
251       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
252       gchar *name = cache->buffer + name_offset;
253
254       if (strcmp (name, icon_name) == 0)
255         {
256           cache->last_chain_offset = chain_offset;
257           goto find_dir;
258         }
259   
260       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
261     }
262
263   cache->last_chain_offset = 0;
264   return 0;
265
266 find_dir:
267   /* We've found an icon list, now check if we have the right icon in it */
268   image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
269   n_images = GET_UINT32 (cache->buffer, image_list_offset);
270   
271   for (i = 0; i < n_images; i++)
272     {
273       if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
274           directory_index) 
275         return image_list_offset + 4 + 8 * i;
276     }
277
278   return 0;
279 }
280
281 gint
282 _gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
283                                 const gchar  *icon_name,
284                                 gint          directory_index)
285 {
286   guint32 image_offset;
287
288   image_offset = find_image_offset (cache, icon_name, directory_index);
289
290   if (!image_offset)
291     return 0;
292
293   return GET_UINT16 (cache->buffer, image_offset + 2);
294 }
295
296 void
297 _gtk_icon_cache_add_icons (GtkIconCache *cache,
298                            const gchar  *directory,
299                            GHashTable   *hash_table)
300 {
301   int directory_index;
302   guint32 hash_offset, n_buckets;
303   guint32 chain_offset;
304   guint32 image_list_offset, n_images;
305   int i, j;
306   
307   directory_index = get_directory_index (cache, directory);
308
309   if (directory_index == -1)
310     return;
311   
312   hash_offset = GET_UINT32 (cache->buffer, 4);
313   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
314
315   for (i = 0; i < n_buckets; i++)
316     {
317       chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
318       while (chain_offset != 0xffffffff)
319         {
320           guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
321           gchar *name = cache->buffer + name_offset;
322           
323           image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
324           n_images = GET_UINT32 (cache->buffer, image_list_offset);
325   
326           for (j = 0; j < n_images; j++)
327             {
328               if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
329                   directory_index)
330                 g_hash_table_insert (hash_table, name, NULL);
331             }
332
333           chain_offset = GET_UINT32 (cache->buffer, chain_offset);
334         }
335     }  
336 }
337
338 gboolean
339 _gtk_icon_cache_has_icon (GtkIconCache *cache,
340                           const gchar  *icon_name)
341 {
342   guint32 hash_offset;
343   guint32 n_buckets;
344   guint32 chain_offset;
345   gint hash;
346   
347   hash_offset = GET_UINT32 (cache->buffer, 4);
348   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
349
350   hash = icon_name_hash (icon_name) % n_buckets;
351
352   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
353   while (chain_offset != 0xffffffff)
354     {
355       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
356       gchar *name = cache->buffer + name_offset;
357
358       if (strcmp (name, icon_name) == 0)
359         return TRUE;
360           
361       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
362     }
363
364   return FALSE;
365 }
366
367 gboolean
368 _gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
369                                        const gchar  *icon_name,
370                                        const gchar  *directory)
371 {
372   guint32 hash_offset;
373   guint32 n_buckets;
374   guint32 chain_offset;
375   gint hash;
376   gboolean found_icon = FALSE;
377   gint directory_index;
378
379   directory_index = get_directory_index (cache, directory);
380
381   if (directory_index == -1)
382     return FALSE;
383   
384   hash_offset = GET_UINT32 (cache->buffer, 4);
385   n_buckets = GET_UINT32 (cache->buffer, hash_offset);
386
387   hash = icon_name_hash (icon_name) % n_buckets;
388
389   chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
390   while (chain_offset != 0xffffffff)
391     {
392       guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
393       gchar *name = cache->buffer + name_offset;
394
395       if (strcmp (name, icon_name) == 0)
396         {
397           found_icon = TRUE;
398           break;
399         }
400           
401       chain_offset = GET_UINT32 (cache->buffer, chain_offset);
402     }
403
404   if (found_icon)
405     {
406       guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
407       guint32 n_images =  GET_UINT32 (cache->buffer, image_list_offset);
408       guint32 image_offset = image_list_offset + 4;
409       gint i;
410       for (i = 0; i < n_images; i++)
411         {
412           guint16 index = GET_UINT16 (cache->buffer, image_offset);
413           
414           if (index == directory_index)
415             return TRUE;
416           image_offset += 8;
417         }
418     }
419
420   return FALSE;
421 }
422
423 static void
424 pixbuf_destroy_cb (guchar   *pixels, 
425                    gpointer  data)
426 {
427   GtkIconCache *cache = data;
428
429   _gtk_icon_cache_unref (cache);
430 }
431
432 GdkPixbuf *
433 _gtk_icon_cache_get_icon (GtkIconCache *cache,
434                           const gchar  *icon_name,
435                           gint          directory_index)
436 {
437   guint32 offset, image_data_offset, pixel_data_offset;
438   guint32 length, type;
439   GdkPixbuf *pixbuf;
440   GdkPixdata pixdata;
441   GError *error = NULL;
442
443   offset = find_image_offset (cache, icon_name, directory_index);
444   
445   image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
446   
447   if (!image_data_offset)
448     return NULL;
449
450   pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);
451
452   type = GET_UINT32 (cache->buffer, pixel_data_offset);
453
454   if (type != 0)
455     {
456       GTK_NOTE (ICONTHEME,
457                 g_print ("invalid pixel data type %u\n", type));
458       return NULL;
459     }
460
461   length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
462   
463   if (!gdk_pixdata_deserialize (&pixdata, length, 
464                                 (guchar *)(cache->buffer + pixel_data_offset + 8),
465                                 &error))
466     {
467       GTK_NOTE (ICONTHEME,
468                 g_print ("could not deserialize data: %s\n", error->message));
469       g_error_free (error);
470
471       return NULL;
472     }
473
474   pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
475                                      (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
476                                      8, pixdata.width, pixdata.height, pixdata.rowstride,
477                                      (GdkPixbufDestroyNotify)pixbuf_destroy_cb, 
478                                      cache);
479   if (!pixbuf)
480     {
481       GTK_NOTE (ICONTHEME,
482                 g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
483       g_error_free (error);
484
485       return NULL;
486     }
487
488   _gtk_icon_cache_ref (cache);
489
490   return pixbuf;
491 }
492
493 GtkIconData  *
494 _gtk_icon_cache_get_icon_data  (GtkIconCache *cache,
495                                 const gchar  *icon_name,
496                                 gint          directory_index)
497 {
498   guint32 offset, image_data_offset, meta_data_offset;
499   GtkIconData *data;
500   int i;
501
502   offset = find_image_offset (cache, icon_name, directory_index);
503   if (!offset)
504     return NULL;
505
506   image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
507   if (!image_data_offset)
508     return NULL;
509
510   meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
511   
512   if (!meta_data_offset)
513     return NULL;
514
515   data = g_slice_new0 (GtkIconData);
516
517   offset = GET_UINT32 (cache->buffer, meta_data_offset);
518   if (offset)
519     {
520       data->has_embedded_rect = TRUE;
521       data->x0 = GET_UINT16 (cache->buffer, offset);
522       data->y0 = GET_UINT16 (cache->buffer, offset + 2);
523       data->x1 = GET_UINT16 (cache->buffer, offset + 4);
524       data->y1 = GET_UINT16 (cache->buffer, offset + 6);
525     }
526
527   offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
528   if (offset)
529     {
530       data->n_attach_points = GET_UINT32 (cache->buffer, offset);
531       data->attach_points = g_new (GdkPoint, data->n_attach_points);
532       for (i = 0; i < data->n_attach_points; i++)
533         {
534           data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i); 
535           data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2); 
536         }
537     }
538
539   offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
540   if (offset)
541     {
542       gint n_names;
543       gchar *lang, *name;
544       gchar **langs;
545       GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);
546
547       n_names = GET_UINT32 (cache->buffer, offset);
548       
549       for (i = 0; i < n_names; i++)
550         {
551           lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
552           name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
553           
554           g_hash_table_insert (table, lang, name);
555         }
556       
557       langs = (gchar **)g_get_language_names ();
558       for (i = 0; langs[i]; i++)
559         {
560           name = g_hash_table_lookup (table, langs[i]);
561           if (name)
562             {
563               data->display_name = g_strdup (name);
564               break;
565             }
566         }
567
568       g_hash_table_destroy (table);
569     }
570
571   return data;
572 }
573