2 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
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.
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.
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.
25 #include <sys/types.h>
32 #include <sys/utime.h>
39 #include <glib/gstdio.h>
40 #include <gdk-pixbuf/gdk-pixdata.h>
42 static gboolean force_update = FALSE;
43 static gboolean quiet = FALSE;
45 #define CACHE_NAME "icon-theme.cache"
47 #define HAS_SUFFIX_XPM (1 << 0)
48 #define HAS_SUFFIX_SVG (1 << 1)
49 #define HAS_SUFFIX_PNG (1 << 2)
50 #define HAS_ICON_FILE (1 << 3)
52 #define CAN_CACHE_IMAGE_DATA(flags) (((flags) & HAS_SUFFIX_PNG) || ((flags) & HAS_SUFFIX_XPM))
54 #define MAJOR_VERSION 1
55 #define MINOR_VERSION 0
56 #define HASH_OFFSET 12
58 #define ALIGN_VALUE(this, boundary) \
59 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
62 is_cache_up_to_date (const gchar *path)
64 struct stat path_stat, cache_stat;
68 retval = g_stat (path, &path_stat);
72 /* We can't stat the path,
73 * assume we have a updated cache */
77 cache_path = g_build_filename (path, CACHE_NAME, NULL);
78 retval = g_stat (cache_path, &cache_stat);
83 /* Cache file not found */
88 return cache_stat.st_mtime >= path_stat.st_mtime;
96 guint pixel_data_size;
99 static GHashTable *image_data_hash = NULL;
106 ImageData *image_data;
107 guint pixel_data_size;
109 int has_embedded_rect;
116 char **display_names;
120 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
122 Image *image = (Image *)value;
123 GHashTable *files = user_data;
125 gboolean free_key = FALSE;
127 if (image->flags == HAS_ICON_FILE)
131 g_free (image->attach_points);
132 g_strfreev (image->display_names);
137 list = g_hash_table_lookup (files, key);
141 list = g_list_prepend (list, value);
142 g_hash_table_insert (files, key, list);
151 load_icon_data (Image *image, const char *path)
160 GError *error = NULL;
164 icon_file = g_key_file_new ();
165 g_key_file_set_list_separator (icon_file, ',');
166 g_key_file_load_from_file (icon_file, path, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
169 g_error_free (error);
173 ivalues = g_key_file_get_integer_list (icon_file,
174 "Icon Data", "EmbeddedTextRectangle",
180 image->has_embedded_rect = TRUE;
181 image->x0 = ivalues[0];
182 image->y0 = ivalues[1];
183 image->x1 = ivalues[2];
184 image->y1 = ivalues[3];
190 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
193 split = g_strsplit (str, "|", -1);
195 image->n_attach_points = g_strv_length (split);
196 image->attach_points = g_new (int, 2 * image->n_attach_points);
199 while (split[i] != NULL && i < image->n_attach_points)
201 split_point = strchr (split[i], ',');
206 image->attach_points[2 * i] = atoi (split[i]);
207 image->attach_points[2 * i + 1] = atoi (split_point);
216 keys = g_key_file_get_keys (icon_file, "Icon Data", &n_keys, &error);
217 image->display_names = g_new0 (gchar *, 2 * n_keys + 1);
218 image->n_display_names = 0;
220 for (i = 0; i < n_keys; i++)
224 if (g_str_has_prefix (keys[i], "DisplayName"))
226 gchar *open, *close = NULL;
228 open = strchr (keys[i], '[');
231 close = strchr (open, ']');
235 lang = g_strndup (open + 1, close - open - 1);
236 name = g_key_file_get_locale_string (icon_file,
237 "Icon Data", "DisplayName",
242 lang = g_strdup ("C");
243 name = g_key_file_get_string (icon_file,
244 "Icon Data", "DisplayName",
248 image->display_names[2 * image->n_display_names] = lang;
249 image->display_names[2 * image->n_display_names + 1] = name;
250 image->n_display_names++;
256 g_key_file_free (icon_file);
260 * This function was copied from gtkfilesystemunix.c, it should
261 * probably go to GLib
264 canonicalize_filename (gchar *filename)
267 gboolean last_was_slash = FALSE;
274 if (*p == G_DIR_SEPARATOR)
277 *q++ = G_DIR_SEPARATOR;
279 last_was_slash = TRUE;
283 if (last_was_slash && *p == '.')
285 if (*(p + 1) == G_DIR_SEPARATOR ||
288 if (*(p + 1) == '\0')
293 else if (*(p + 1) == '.' &&
294 (*(p + 2) == G_DIR_SEPARATOR ||
297 if (q > filename + 1)
300 while (q > filename + 1 &&
301 *(q - 1) != G_DIR_SEPARATOR)
305 if (*(p + 2) == '\0')
313 last_was_slash = FALSE;
319 last_was_slash = FALSE;
326 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
333 follow_links (const gchar *path)
339 path2 = g_strdup (path);
340 while (g_file_test (path2, G_FILE_TEST_IS_SYMLINK))
342 target = g_file_read_link (path2, NULL);
346 if (target[0] == '/')
351 s = g_build_path ("/", d, target, NULL);
361 if (strcmp (path, path2) == 0)
371 maybe_cache_image_data (Image *image,
374 if (CAN_CACHE_IMAGE_DATA(image->flags) && !image->image_data)
380 idata = g_hash_table_lookup (image_data_hash, path);
382 path2 = follow_links (path);
388 canonicalize_filename (path2);
390 idata2 = g_hash_table_lookup (image_data_hash, path2);
392 if (idata && idata2 && idata != idata2)
393 g_error ("different idatas found for symlinked '%s' and '%s'\n",
396 if (idata && !idata2)
397 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
399 if (!idata && idata2)
401 g_hash_table_insert (image_data_hash, g_strdup (path), idata2);
408 idata = g_new0 (ImageData, 1);
409 g_hash_table_insert (image_data_hash, g_strdup (path), idata);
411 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
414 if (!idata->has_pixdata)
416 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
420 gdk_pixdata_from_pixbuf (&idata->pixdata, pixbuf, FALSE);
421 idata->pixel_data_size = idata->pixdata.length + 8;
422 idata->has_pixdata = TRUE;
426 image->image_data = idata;
434 scan_directory (const gchar *base_path,
440 GHashTable *dir_hash;
444 gboolean dir_added = FALSE;
445 guint dir_index = 0xffff;
447 dir_path = g_build_filename (base_path, subdir, NULL);
449 /* FIXME: Use the gerror */
450 dir = g_dir_open (dir_path, 0, NULL);
455 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
457 while ((name = g_dir_read_name (dir)))
463 gchar *basename, *dot;
465 path = g_build_filename (dir_path, name, NULL);
466 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
472 subsubdir = g_build_filename (subdir, name, NULL);
474 subsubdir = g_strdup (name);
475 directories = scan_directory (base_path, subsubdir, files,
476 directories, depth + 1);
482 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
485 if (g_str_has_suffix (name, ".png"))
486 flags |= HAS_SUFFIX_PNG;
487 else if (g_str_has_suffix (name, ".svg"))
488 flags |= HAS_SUFFIX_SVG;
489 else if (g_str_has_suffix (name, ".xpm"))
490 flags |= HAS_SUFFIX_XPM;
491 else if (g_str_has_suffix (name, ".icon"))
492 flags |= HAS_ICON_FILE;
497 basename = g_strdup (name);
498 dot = strrchr (basename, '.');
501 image = g_hash_table_lookup (dir_hash, basename);
504 image->flags |= flags;
505 maybe_cache_image_data (image, path);
514 dir_index = g_list_length (directories);
515 directories = g_list_append (directories, g_strdup (subdir));
521 image = g_new0 (Image, 1);
522 image->flags = flags;
523 image->dir_index = dir_index;
524 maybe_cache_image_data (image, path);
526 g_hash_table_insert (dir_hash, g_strdup (basename), image);
529 if (g_str_has_suffix (name, ".icon"))
530 load_icon_data (image, path);
540 /* Move dir into the big file hash */
541 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
543 g_hash_table_destroy (dir_hash);
548 typedef struct _HashNode HashNode;
558 icon_name_hash (gconstpointer key)
560 const signed char *p = key;
564 for (p += 1; *p != '\0'; p++)
565 h = (h << 5) - h + *p;
576 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
578 HashContext *context = user_data;
582 hash = icon_name_hash (key) % context->size;
584 node = g_new0 (HashNode, 1);
587 node->image_list = value;
589 if (context->nodes[hash] != NULL)
590 node->next = context->nodes[hash];
592 context->nodes[hash] = node;
598 write_string (FILE *cache, const gchar *n)
603 l = ALIGN_VALUE (strlen (n) + 1, 4);
608 i = fwrite (s, l, 1, cache);
615 write_card16 (FILE *cache, guint16 n)
619 n = GUINT16_TO_BE (n);
621 i = fwrite ((char *)&n, 2, 1, cache);
627 write_card32 (FILE *cache, guint32 n)
631 n = GUINT32_TO_BE (n);
633 i = fwrite ((char *)&n, 4, 1, cache);
640 write_pixdata (FILE *cache, GdkPixdata *pixdata)
646 /* Type 0 is GdkPixdata */
647 if (!write_card32 (cache, 0))
650 s = gdk_pixdata_serialize (pixdata, &len);
652 if (!write_card32 (cache, len))
658 i = fwrite (s, len, 1, cache);
666 write_header (FILE *cache, guint32 dir_list_offset)
668 return (write_card16 (cache, MAJOR_VERSION) &&
669 write_card16 (cache, MINOR_VERSION) &&
670 write_card32 (cache, HASH_OFFSET) &&
671 write_card32 (cache, dir_list_offset));
675 get_image_meta_data_size (Image *image)
680 if (image->has_embedded_rect ||
681 image->attach_points > 0 ||
682 image->n_display_names > 0)
685 if (image->has_embedded_rect)
688 if (image->n_attach_points > 0)
689 len += 4 + image->n_attach_points * 4;
691 if (image->n_display_names > 0)
693 len += 4 + 8 * image->n_display_names;
695 for (i = 0; image->display_names[i]; i++)
696 len += ALIGN_VALUE (strlen (image->display_names[i]) + 1, 4);
703 get_image_pixel_data_size (Image *image)
705 if (image->pixel_data_size == 0)
707 if (image->image_data &&
708 image->image_data->has_pixdata)
710 image->pixel_data_size = image->image_data->pixel_data_size;
711 image->image_data->pixel_data_size = 0;
715 return image->pixel_data_size;
719 get_image_data_size (Image *image)
725 len += get_image_pixel_data_size (image);
726 len += get_image_meta_data_size (image);
728 if (len > 0 || (image->image_data && image->image_data->has_pixdata))
735 get_single_node_size (HashNode *node, gboolean include_image_data)
744 len += ALIGN_VALUE (strlen (node->name) + 1, 4);
747 len += 4 + g_list_length (node->image_list) * 8;
750 if (include_image_data)
751 for (list = node->image_list; list; list = list->next)
753 Image *image = list->data;
755 len += get_image_data_size (image);
762 get_bucket_size (HashNode *node)
767 len += get_single_node_size (node, TRUE);
776 write_bucket (FILE *cache, HashNode *node, int *offset)
780 int next_offset = *offset + get_single_node_size (node, TRUE);
781 int image_data_offset = *offset + get_single_node_size (node, FALSE);
788 if (node->next != NULL)
790 if (!write_card32 (cache, next_offset))
795 if (!write_card32 (cache, 0xffffffff))
799 /* Icon name offset */
800 if (!write_card32 (cache, *offset + 12))
803 /* Image list offset */
804 tmp = *offset + 12 + ALIGN_VALUE (strlen (node->name) + 1, 4);
805 if (!write_card32 (cache, tmp))
809 if (!write_string (cache, node->name))
813 len = g_list_length (node->image_list);
814 if (!write_card32 (cache, len))
817 /* Image data goes right after the image list */
820 list = node->image_list;
821 data_offset = image_data_offset;
822 for (i = 0; i < len; i++)
824 Image *image = list->data;
825 int image_data_size = get_image_data_size (image);
827 /* Directory index */
828 if (!write_card16 (cache, image->dir_index))
832 if (!write_card16 (cache, image->flags))
835 /* Image data offset */
836 if (image_data_size > 0)
838 if (!write_card32 (cache, data_offset))
840 data_offset += image_data_size;
844 if (!write_card32 (cache, 0))
851 /* Now write the image data */
852 list = node->image_list;
853 for (i = 0; i < len; i++, list = list->next)
855 Image *image = list->data;
856 int pixel_data_size = get_image_pixel_data_size (image);
857 int meta_data_size = get_image_meta_data_size (image);
859 if (get_image_data_size (image) == 0)
863 if (pixel_data_size > 0)
865 if (!write_card32 (cache, image_data_offset + 8))
868 image->image_data->offset = image_data_offset + 8;
872 if (!write_card32 (cache, image->image_data->offset))
876 if (meta_data_size > 0)
878 if (!write_card32 (cache, image_data_offset + pixel_data_size + 8))
883 if (!write_card32 (cache, 0))
887 if (pixel_data_size > 0)
889 if (!write_pixdata (cache, &image->image_data->pixdata))
893 if (meta_data_size > 0)
895 int ofs = image_data_offset + pixel_data_size + 20;
897 if (image->has_embedded_rect)
899 if (!write_card32 (cache, ofs))
906 if (!write_card32 (cache, 0))
910 if (image->n_attach_points > 0)
912 if (!write_card32 (cache, ofs))
915 ofs += 4 + 4 * image->n_attach_points;
919 if (!write_card32 (cache, 0))
923 if (image->n_display_names > 0)
925 if (!write_card32 (cache, ofs))
930 if (!write_card32 (cache, 0))
934 if (image->has_embedded_rect)
936 if (!write_card16 (cache, image->x0) ||
937 !write_card16 (cache, image->y0) ||
938 !write_card16 (cache, image->x1) ||
939 !write_card16 (cache, image->y1))
943 if (image->n_attach_points > 0)
945 if (!write_card32 (cache, image->n_attach_points))
948 for (j = 0; j < 2 * image->n_attach_points; j++)
950 if (!write_card16 (cache, image->attach_points[j]))
955 if (image->n_display_names > 0)
957 if (!write_card32 (cache, image->n_display_names))
960 ofs += 4 + 8 * image->n_display_names;
962 for (j = 0; j < 2 * image->n_display_names; j++)
964 if (!write_card32 (cache, ofs))
967 ofs += ALIGN_VALUE (strlen (image->display_names[j]) + 1, 4);
970 for (j = 0; j < 2 * image->n_display_names; j++)
972 if (!write_string (cache, image->display_names[j]))
978 image_data_offset += pixel_data_size + meta_data_size + 8;
981 *offset = next_offset;
989 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
991 int offset = HASH_OFFSET;
995 if (!(write_card32 (cache, context->size)))
998 /* Size int + size * 4 */
999 node_offset = offset + 4 + context->size * 4;
1001 for (i = 0; i < context->size; i++)
1003 if (context->nodes[i] != NULL)
1005 if (!write_card32 (cache, node_offset))
1008 node_offset += get_bucket_size (context->nodes[i]);
1012 if (!write_card32 (cache, 0xffffffff))
1019 *new_offset = node_offset;
1021 /* Now write the buckets */
1022 node_offset = offset + 4 + context->size * 4;
1024 for (i = 0; i < context->size; i++)
1026 if (!context->nodes[i])
1029 if (!write_bucket (cache, context->nodes[i], &node_offset))
1037 write_dir_index (FILE *cache, int offset, GList *directories)
1043 n_dirs = g_list_length (directories);
1045 if (!write_card32 (cache, n_dirs))
1048 offset += 4 + n_dirs * 4;
1050 for (d = directories; d; d = d->next)
1053 if (!write_card32 (cache, offset))
1056 offset += ALIGN_VALUE (strlen (dir) + 1, 4);
1059 for (d = directories; d; d = d->next)
1063 if (!write_string (cache, dir))
1071 write_file (FILE *cache, GHashTable *files, GList *directories)
1073 HashContext context;
1076 /* Convert the hash table into something looking a bit more
1077 * like what we want to write to disk.
1079 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
1080 context.nodes = g_new0 (HashNode *, context.size);
1082 g_hash_table_foreach_remove (files, convert_to_hash, &context);
1084 /* Now write the file */
1085 /* We write 0 as the directory list offset and go
1086 * back and change it later */
1087 if (!write_header (cache, 0))
1089 g_printerr ("Failed to write header\n");
1093 if (!write_hash_table (cache, &context, &new_offset))
1095 g_printerr ("Failed to write hash table\n");
1099 if (!write_dir_index (cache, new_offset, directories))
1101 g_printerr ("Failed to write directory index\n");
1107 if (!write_header (cache, new_offset))
1109 g_printerr ("Failed to rewrite header\n");
1117 build_cache (const gchar *path)
1119 gchar *cache_path, *tmp_cache_path;
1123 struct stat path_stat, cache_stat;
1124 struct utimbuf utime_buf;
1125 GList *directories = NULL;
1127 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
1128 cache = g_fopen (tmp_cache_path, "wb");
1132 g_printerr ("Failed to write cache file: %s\n", g_strerror (errno));
1136 files = g_hash_table_new (g_str_hash, g_str_equal);
1137 image_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1139 directories = scan_directory (path, NULL, files, NULL, 0);
1141 if (g_hash_table_size (files) == 0)
1143 /* Empty table, just close and remove the file */
1146 g_unlink (tmp_cache_path);
1150 /* FIXME: Handle failure */
1151 retval = write_file (cache, files, directories);
1154 g_list_foreach (directories, (GFunc)g_free, NULL);
1155 g_list_free (directories);
1159 g_unlink (tmp_cache_path);
1163 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1165 if (g_rename (tmp_cache_path, cache_path) == -1)
1167 g_unlink (tmp_cache_path);
1172 /* FIXME: What do do if an error occurs here? */
1173 if (g_stat (path, &path_stat) < 0 ||
1174 g_stat (cache_path, &cache_stat))
1177 utime_buf.actime = path_stat.st_atime;
1178 utime_buf.modtime = cache_stat.st_mtime;
1179 utime (path, &utime_buf);
1182 g_printerr ("Cache file created successfully.\n");
1185 static GOptionEntry args[] = {
1186 { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, "Overwrite an existing cache, even if uptodate", NULL },
1187 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Turn off verbose output", NULL },
1192 main (int argc, char **argv)
1195 GOptionContext *context;
1200 context = g_option_context_new ("ICONPATH");
1201 g_option_context_add_main_entries (context, args, NULL);
1203 g_option_context_parse (context, &argc, &argv, NULL);
1207 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
1210 if (!force_update && is_cache_up_to_date (path))