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