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