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>
38 #include <glib/gstdio.h>
39 #include <gdk-pixbuf/gdk-pixdata.h>
41 static gboolean force_update = FALSE;
42 static gboolean ignore_theme_index = FALSE;
43 static gboolean quiet = FALSE;
44 static gboolean index_only = FALSE;
46 #define CACHE_NAME "icon-theme.cache"
48 #define HAS_SUFFIX_XPM (1 << 0)
49 #define HAS_SUFFIX_SVG (1 << 1)
50 #define HAS_SUFFIX_PNG (1 << 2)
51 #define HAS_ICON_FILE (1 << 3)
53 #define CAN_CACHE_IMAGE_DATA(flags) (!index_only && (((flags) & HAS_SUFFIX_PNG) || ((flags) & HAS_SUFFIX_XPM)))
55 #define MAJOR_VERSION 1
56 #define MINOR_VERSION 0
57 #define HASH_OFFSET 12
59 #define ALIGN_VALUE(this, boundary) \
60 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
63 is_cache_up_to_date (const gchar *path)
65 struct stat path_stat, cache_stat;
69 retval = g_stat (path, &path_stat);
73 /* We can't stat the path,
74 * assume we have a updated cache */
78 cache_path = g_build_filename (path, CACHE_NAME, NULL);
79 retval = g_stat (cache_path, &cache_stat);
84 /* Cache file not found */
89 return cache_stat.st_mtime >= path_stat.st_mtime;
93 has_theme_index (const gchar *path)
98 index_path = g_build_filename (path, "index.theme", NULL);
100 result = g_file_test (index_path, G_FILE_TEST_IS_REGULAR);
110 gboolean has_pixdata;
113 guint pixel_data_size;
116 static GHashTable *image_data_hash = NULL;
123 ImageData *image_data;
124 guint pixel_data_size;
126 int has_embedded_rect;
133 char **display_names;
137 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
139 Image *image = (Image *)value;
140 GHashTable *files = user_data;
142 gboolean free_key = FALSE;
144 if (image->flags == HAS_ICON_FILE)
148 g_free (image->attach_points);
149 g_strfreev (image->display_names);
154 list = g_hash_table_lookup (files, key);
158 list = g_list_prepend (list, value);
159 g_hash_table_insert (files, key, list);
168 load_icon_data (Image *image, const char *path)
177 GError *error = NULL;
181 icon_file = g_key_file_new ();
182 g_key_file_set_list_separator (icon_file, ',');
183 g_key_file_load_from_file (icon_file, path, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
186 g_error_free (error);
190 ivalues = g_key_file_get_integer_list (icon_file,
191 "Icon Data", "EmbeddedTextRectangle",
197 image->has_embedded_rect = TRUE;
198 image->x0 = ivalues[0];
199 image->y0 = ivalues[1];
200 image->x1 = ivalues[2];
201 image->y1 = ivalues[3];
207 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
210 split = g_strsplit (str, "|", -1);
212 image->n_attach_points = g_strv_length (split);
213 image->attach_points = g_new (int, 2 * image->n_attach_points);
216 while (split[i] != NULL && i < image->n_attach_points)
218 split_point = strchr (split[i], ',');
223 image->attach_points[2 * i] = atoi (split[i]);
224 image->attach_points[2 * i + 1] = atoi (split_point);
233 keys = g_key_file_get_keys (icon_file, "Icon Data", &n_keys, &error);
234 image->display_names = g_new0 (gchar *, 2 * n_keys + 1);
235 image->n_display_names = 0;
237 for (i = 0; i < n_keys; i++)
241 if (g_str_has_prefix (keys[i], "DisplayName"))
243 gchar *open, *close = NULL;
245 open = strchr (keys[i], '[');
248 close = strchr (open, ']');
252 lang = g_strndup (open + 1, close - open - 1);
253 name = g_key_file_get_locale_string (icon_file,
254 "Icon Data", "DisplayName",
259 lang = g_strdup ("C");
260 name = g_key_file_get_string (icon_file,
261 "Icon Data", "DisplayName",
265 image->display_names[2 * image->n_display_names] = lang;
266 image->display_names[2 * image->n_display_names + 1] = name;
267 image->n_display_names++;
273 g_key_file_free (icon_file);
277 * This function was copied from gtkfilesystemunix.c, it should
278 * probably go to GLib
281 canonicalize_filename (gchar *filename)
284 gboolean last_was_slash = FALSE;
291 if (*p == G_DIR_SEPARATOR)
294 *q++ = G_DIR_SEPARATOR;
296 last_was_slash = TRUE;
300 if (last_was_slash && *p == '.')
302 if (*(p + 1) == G_DIR_SEPARATOR ||
305 if (*(p + 1) == '\0')
310 else if (*(p + 1) == '.' &&
311 (*(p + 2) == G_DIR_SEPARATOR ||
314 if (q > filename + 1)
317 while (q > filename + 1 &&
318 *(q - 1) != G_DIR_SEPARATOR)
322 if (*(p + 2) == '\0')
330 last_was_slash = FALSE;
336 last_was_slash = FALSE;
343 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
350 follow_links (const gchar *path)
356 path2 = g_strdup (path);
357 while (g_file_test (path2, G_FILE_TEST_IS_SYMLINK))
359 target = g_file_read_link (path2, NULL);
363 if (g_path_is_absolute (target))
367 d = g_path_get_dirname (path2);
368 s = g_build_filename (d, target, NULL);
379 if (strcmp (path, path2) == 0)
389 maybe_cache_image_data (Image *image,
392 if (CAN_CACHE_IMAGE_DATA(image->flags) && !image->image_data)
398 idata = g_hash_table_lookup (image_data_hash, path);
400 path2 = follow_links (path);
406 canonicalize_filename (path2);
408 idata2 = g_hash_table_lookup (image_data_hash, path2);
410 if (idata && idata2 && idata != idata2)
411 g_error ("different idatas found for symlinked '%s' and '%s'\n",
414 if (idata && !idata2)
415 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
417 if (!idata && idata2)
419 g_hash_table_insert (image_data_hash, g_strdup (path), idata2);
426 idata = g_new0 (ImageData, 1);
427 g_hash_table_insert (image_data_hash, g_strdup (path), idata);
429 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
432 if (!idata->has_pixdata)
434 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
438 gdk_pixdata_from_pixbuf (&idata->pixdata, pixbuf, FALSE);
439 idata->pixel_data_size = idata->pixdata.length + 8;
440 idata->has_pixdata = TRUE;
444 image->image_data = idata;
452 scan_directory (const gchar *base_path,
458 GHashTable *dir_hash;
462 gboolean dir_added = FALSE;
463 guint dir_index = 0xffff;
465 dir_path = g_build_filename (base_path, subdir, NULL);
467 /* FIXME: Use the gerror */
468 dir = g_dir_open (dir_path, 0, NULL);
473 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
475 while ((name = g_dir_read_name (dir)))
481 gchar *basename, *dot;
483 path = g_build_filename (dir_path, name, NULL);
484 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
490 subsubdir = g_build_filename (subdir, name, NULL);
492 subsubdir = g_strdup (name);
493 directories = scan_directory (base_path, subsubdir, files,
494 directories, depth + 1);
500 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
503 if (g_str_has_suffix (name, ".png"))
504 flags |= HAS_SUFFIX_PNG;
505 else if (g_str_has_suffix (name, ".svg"))
506 flags |= HAS_SUFFIX_SVG;
507 else if (g_str_has_suffix (name, ".xpm"))
508 flags |= HAS_SUFFIX_XPM;
509 else if (g_str_has_suffix (name, ".icon"))
510 flags |= HAS_ICON_FILE;
515 basename = g_strdup (name);
516 dot = strrchr (basename, '.');
519 image = g_hash_table_lookup (dir_hash, basename);
522 image->flags |= flags;
523 maybe_cache_image_data (image, path);
532 dir_index = g_list_length (directories);
533 directories = g_list_append (directories, g_strdup (subdir));
539 image = g_new0 (Image, 1);
540 image->flags = flags;
541 image->dir_index = dir_index;
542 maybe_cache_image_data (image, path);
544 g_hash_table_insert (dir_hash, g_strdup (basename), image);
547 if (g_str_has_suffix (name, ".icon"))
548 load_icon_data (image, path);
558 /* Move dir into the big file hash */
559 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
561 g_hash_table_destroy (dir_hash);
566 typedef struct _HashNode HashNode;
576 icon_name_hash (gconstpointer key)
578 const signed char *p = key;
582 for (p += 1; *p != '\0'; p++)
583 h = (h << 5) - h + *p;
594 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
596 HashContext *context = user_data;
600 hash = icon_name_hash (key) % context->size;
602 node = g_new0 (HashNode, 1);
605 node->image_list = value;
607 if (context->nodes[hash] != NULL)
608 node->next = context->nodes[hash];
610 context->nodes[hash] = node;
616 write_string (FILE *cache, const gchar *n)
621 l = ALIGN_VALUE (strlen (n) + 1, 4);
626 i = fwrite (s, l, 1, cache);
633 write_card16 (FILE *cache, guint16 n)
637 n = GUINT16_TO_BE (n);
639 i = fwrite ((char *)&n, 2, 1, cache);
645 write_card32 (FILE *cache, guint32 n)
649 n = GUINT32_TO_BE (n);
651 i = fwrite ((char *)&n, 4, 1, cache);
658 write_pixdata (FILE *cache, GdkPixdata *pixdata)
664 /* Type 0 is GdkPixdata */
665 if (!write_card32 (cache, 0))
668 s = gdk_pixdata_serialize (pixdata, &len);
670 if (!write_card32 (cache, len))
676 i = fwrite (s, len, 1, cache);
684 write_header (FILE *cache, guint32 dir_list_offset)
686 return (write_card16 (cache, MAJOR_VERSION) &&
687 write_card16 (cache, MINOR_VERSION) &&
688 write_card32 (cache, HASH_OFFSET) &&
689 write_card32 (cache, dir_list_offset));
693 get_image_meta_data_size (Image *image)
698 if (image->has_embedded_rect ||
699 image->attach_points > 0 ||
700 image->n_display_names > 0)
703 if (image->has_embedded_rect)
706 if (image->n_attach_points > 0)
707 len += 4 + image->n_attach_points * 4;
709 if (image->n_display_names > 0)
711 len += 4 + 8 * image->n_display_names;
713 for (i = 0; image->display_names[i]; i++)
714 len += ALIGN_VALUE (strlen (image->display_names[i]) + 1, 4);
721 get_image_pixel_data_size (Image *image)
723 if (image->pixel_data_size == 0)
725 if (image->image_data &&
726 image->image_data->has_pixdata)
728 image->pixel_data_size = image->image_data->pixel_data_size;
729 image->image_data->pixel_data_size = 0;
733 return image->pixel_data_size;
737 get_image_data_size (Image *image)
743 len += get_image_pixel_data_size (image);
744 len += get_image_meta_data_size (image);
746 if (len > 0 || (image->image_data && image->image_data->has_pixdata))
753 get_single_node_size (HashNode *node, gboolean include_image_data)
762 len += ALIGN_VALUE (strlen (node->name) + 1, 4);
765 len += 4 + g_list_length (node->image_list) * 8;
768 if (include_image_data)
769 for (list = node->image_list; list; list = list->next)
771 Image *image = list->data;
773 len += get_image_data_size (image);
780 get_bucket_size (HashNode *node)
785 len += get_single_node_size (node, TRUE);
794 write_bucket (FILE *cache, HashNode *node, int *offset)
798 int next_offset = *offset + get_single_node_size (node, TRUE);
799 int image_data_offset = *offset + get_single_node_size (node, FALSE);
806 if (node->next != NULL)
808 if (!write_card32 (cache, next_offset))
813 if (!write_card32 (cache, 0xffffffff))
817 /* Icon name offset */
818 if (!write_card32 (cache, *offset + 12))
821 /* Image list offset */
822 tmp = *offset + 12 + ALIGN_VALUE (strlen (node->name) + 1, 4);
823 if (!write_card32 (cache, tmp))
827 if (!write_string (cache, node->name))
831 len = g_list_length (node->image_list);
832 if (!write_card32 (cache, len))
835 /* Image data goes right after the image list */
838 list = node->image_list;
839 data_offset = image_data_offset;
840 for (i = 0; i < len; i++)
842 Image *image = list->data;
843 int image_data_size = get_image_data_size (image);
845 /* Directory index */
846 if (!write_card16 (cache, image->dir_index))
850 if (!write_card16 (cache, image->flags))
853 /* Image data offset */
854 if (image_data_size > 0)
856 if (!write_card32 (cache, data_offset))
858 data_offset += image_data_size;
862 if (!write_card32 (cache, 0))
869 /* Now write the image data */
870 list = node->image_list;
871 for (i = 0; i < len; i++, list = list->next)
873 Image *image = list->data;
874 int pixel_data_size = get_image_pixel_data_size (image);
875 int meta_data_size = get_image_meta_data_size (image);
877 if (get_image_data_size (image) == 0)
881 if (pixel_data_size > 0)
883 if (!write_card32 (cache, image_data_offset + 8))
886 image->image_data->offset = image_data_offset + 8;
892 if (image->image_data)
893 offset = image->image_data->offset;
897 if (!write_card32 (cache, offset))
901 if (meta_data_size > 0)
903 if (!write_card32 (cache, image_data_offset + pixel_data_size + 8))
908 if (!write_card32 (cache, 0))
912 if (pixel_data_size > 0)
914 if (!write_pixdata (cache, &image->image_data->pixdata))
918 if (meta_data_size > 0)
920 int ofs = image_data_offset + pixel_data_size + 20;
922 if (image->has_embedded_rect)
924 if (!write_card32 (cache, ofs))
931 if (!write_card32 (cache, 0))
935 if (image->n_attach_points > 0)
937 if (!write_card32 (cache, ofs))
940 ofs += 4 + 4 * image->n_attach_points;
944 if (!write_card32 (cache, 0))
948 if (image->n_display_names > 0)
950 if (!write_card32 (cache, ofs))
955 if (!write_card32 (cache, 0))
959 if (image->has_embedded_rect)
961 if (!write_card16 (cache, image->x0) ||
962 !write_card16 (cache, image->y0) ||
963 !write_card16 (cache, image->x1) ||
964 !write_card16 (cache, image->y1))
968 if (image->n_attach_points > 0)
970 if (!write_card32 (cache, image->n_attach_points))
973 for (j = 0; j < 2 * image->n_attach_points; j++)
975 if (!write_card16 (cache, image->attach_points[j]))
980 if (image->n_display_names > 0)
982 if (!write_card32 (cache, image->n_display_names))
985 ofs += 4 + 8 * image->n_display_names;
987 for (j = 0; j < 2 * image->n_display_names; j++)
989 if (!write_card32 (cache, ofs))
992 ofs += ALIGN_VALUE (strlen (image->display_names[j]) + 1, 4);
995 for (j = 0; j < 2 * image->n_display_names; j++)
997 if (!write_string (cache, image->display_names[j]))
1003 image_data_offset += pixel_data_size + meta_data_size + 8;
1006 *offset = next_offset;
1014 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
1016 int offset = HASH_OFFSET;
1020 if (!(write_card32 (cache, context->size)))
1023 /* Size int + size * 4 */
1024 node_offset = offset + 4 + context->size * 4;
1026 for (i = 0; i < context->size; i++)
1028 if (context->nodes[i] != NULL)
1030 if (!write_card32 (cache, node_offset))
1033 node_offset += get_bucket_size (context->nodes[i]);
1037 if (!write_card32 (cache, 0xffffffff))
1044 *new_offset = node_offset;
1046 /* Now write the buckets */
1047 node_offset = offset + 4 + context->size * 4;
1049 for (i = 0; i < context->size; i++)
1051 if (!context->nodes[i])
1054 if (!write_bucket (cache, context->nodes[i], &node_offset))
1062 write_dir_index (FILE *cache, int offset, GList *directories)
1068 n_dirs = g_list_length (directories);
1070 if (!write_card32 (cache, n_dirs))
1073 offset += 4 + n_dirs * 4;
1075 for (d = directories; d; d = d->next)
1078 if (!write_card32 (cache, offset))
1081 offset += ALIGN_VALUE (strlen (dir) + 1, 4);
1084 for (d = directories; d; d = d->next)
1088 if (!write_string (cache, dir))
1096 write_file (FILE *cache, GHashTable *files, GList *directories)
1098 HashContext context;
1101 /* Convert the hash table into something looking a bit more
1102 * like what we want to write to disk.
1104 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
1105 context.nodes = g_new0 (HashNode *, context.size);
1107 g_hash_table_foreach_remove (files, convert_to_hash, &context);
1109 /* Now write the file */
1110 /* We write 0 as the directory list offset and go
1111 * back and change it later */
1112 if (!write_header (cache, 0))
1114 g_printerr ("Failed to write header\n");
1118 if (!write_hash_table (cache, &context, &new_offset))
1120 g_printerr ("Failed to write hash table\n");
1124 if (!write_dir_index (cache, new_offset, directories))
1126 g_printerr ("Failed to write directory index\n");
1132 if (!write_header (cache, new_offset))
1134 g_printerr ("Failed to rewrite header\n");
1142 build_cache (const gchar *path)
1144 gchar *cache_path, *tmp_cache_path;
1148 struct stat path_stat, cache_stat;
1149 struct utimbuf utime_buf;
1150 GList *directories = NULL;
1152 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
1153 cache = g_fopen (tmp_cache_path, "wb");
1157 g_printerr ("Failed to write cache file: %s\n", g_strerror (errno));
1161 files = g_hash_table_new (g_str_hash, g_str_equal);
1162 image_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1164 directories = scan_directory (path, NULL, files, NULL, 0);
1166 if (g_hash_table_size (files) == 0)
1168 /* Empty table, just close and remove the file */
1171 g_unlink (tmp_cache_path);
1175 /* FIXME: Handle failure */
1176 retval = write_file (cache, files, directories);
1179 g_list_foreach (directories, (GFunc)g_free, NULL);
1180 g_list_free (directories);
1184 g_unlink (tmp_cache_path);
1188 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1190 if (g_rename (tmp_cache_path, cache_path) == -1)
1192 g_unlink (tmp_cache_path);
1197 /* FIXME: What do do if an error occurs here? */
1198 if (g_stat (path, &path_stat) < 0 ||
1199 g_stat (cache_path, &cache_stat))
1202 utime_buf.actime = path_stat.st_atime;
1203 utime_buf.modtime = cache_stat.st_mtime;
1204 utime (path, &utime_buf);
1207 g_printerr ("Cache file created successfully.\n");
1210 static GOptionEntry args[] = {
1211 { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, "Overwrite an existing cache, even if uptodate", NULL },
1212 { "ignore-theme-index", 't', 0, G_OPTION_ARG_NONE, &ignore_theme_index, "Don't check for the existence of index.theme", NULL },
1213 { "index-only", 'i', 0, G_OPTION_ARG_NONE, &index_only, "Don't include image data in the cache", NULL },
1214 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Turn off verbose output", NULL },
1219 main (int argc, char **argv)
1222 GOptionContext *context;
1227 context = g_option_context_new ("ICONPATH");
1228 g_option_context_add_main_entries (context, args, NULL);
1230 g_option_context_parse (context, &argc, &argv, NULL);
1234 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
1237 if (!ignore_theme_index && !has_theme_index (path))
1239 g_printerr ("No theme index file in '%s'.\n"
1240 "If you really want to create an icon cache here, use --ignore-theme-index.\n", path);
1244 if (!force_update && is_cache_up_to_date (path))