]> Pileus Git - ~andy/gtk/blobdiff - gtk/updateiconcache.c
Apply a cleanup patch by Kjartan Maraas (#341812)
[~andy/gtk] / gtk / updateiconcache.c
index f38dde7352dec5a3371ab69be433ee6d653ec08d..f19d03c9c3f6ab058ae762bd384ec53cfc7521da 100644 (file)
 #else
 #include <utime.h>
 #endif
-#include <libgen.h>
 
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <gdk-pixbuf/gdk-pixdata.h>
+#include <glib/gi18n.h>
 
 static gboolean force_update = FALSE;
+static gboolean ignore_theme_index = FALSE;
 static gboolean quiet = FALSE;
+static gboolean index_only = FALSE;
+static gchar *var_name = "-";
 
 #define CACHE_NAME "icon-theme.cache"
 
@@ -49,7 +52,7 @@ static gboolean quiet = FALSE;
 #define HAS_SUFFIX_PNG (1 << 2)
 #define HAS_ICON_FILE  (1 << 3)
 
-#define CAN_CACHE_IMAGE_DATA(flags) (((flags) & HAS_SUFFIX_PNG) || ((flags) & HAS_SUFFIX_XPM))
+#define CAN_CACHE_IMAGE_DATA(flags) (!index_only && (((flags) & HAS_SUFFIX_PNG) || ((flags) & HAS_SUFFIX_XPM)))
 
 #define MAJOR_VERSION 1
 #define MINOR_VERSION 0
@@ -58,7 +61,7 @@ static gboolean quiet = FALSE;
 #define ALIGN_VALUE(this, boundary) \
   (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
 
-gboolean
+static gboolean
 is_cache_up_to_date (const gchar *path)
 {
   struct stat path_stat, cache_stat;
@@ -88,6 +91,22 @@ is_cache_up_to_date (const gchar *path)
   return cache_stat.st_mtime >= path_stat.st_mtime;
 }
 
+static gboolean
+has_theme_index (const gchar *path)
+{
+  gboolean result;
+  gchar *index_path;
+
+  index_path = g_build_filename (path, "index.theme", NULL);
+
+  result = g_file_test (index_path, G_FILE_TEST_IS_REGULAR);
+  
+  g_free (index_path);
+
+  return result;
+}
+
+
 typedef struct 
 {
   gboolean has_pixdata;
@@ -127,9 +146,9 @@ foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
   if (image->flags == HAS_ICON_FILE)
     {
       g_free (key);
-      g_free (image);
       g_free (image->attach_points);
       g_strfreev (image->display_names);
+      g_free (image);
 
       return TRUE;
     }
@@ -343,12 +362,13 @@ follow_links (const gchar *path)
       
       if (target)
        {
-         if (target[0] == '/')
-           path2 =  target;
+         if (g_path_is_absolute (target))
+           path2 = target;
          else
            {
-             d = dirname (path2);
-             s = g_build_path ("/", d, target, NULL);
+             d = g_path_get_dirname (path2);
+             s = g_build_filename (d, target, NULL);
+             g_free (d);
              g_free (target);
              g_free (path2);
              path2 = s;
@@ -390,7 +410,7 @@ maybe_cache_image_data (Image       *image,
          idata2 = g_hash_table_lookup (image_data_hash, path2);
 
          if (idata && idata2 && idata != idata2)
-           g_error ("different idatas found for symlinked '%s' and '%s'\n",
+           g_error (_("different idatas found for symlinked '%s' and '%s'\n"),
                     path, path2);
 
          if (idata && !idata2)
@@ -430,7 +450,7 @@ maybe_cache_image_data (Image       *image,
     }
 }
 
-GList *
+static GList *
 scan_directory (const gchar *base_path, 
                const gchar *subdir, 
                GHashTable  *files, 
@@ -594,7 +614,7 @@ convert_to_hash (gpointer key, gpointer value, gpointer user_data)
   return TRUE;
 }
 
-gboolean
+static gboolean
 write_string (FILE *cache, const gchar *n)
 {
   gchar *s;
@@ -611,7 +631,7 @@ write_string (FILE *cache, const gchar *n)
   
 }
 
-gboolean
+static gboolean
 write_card16 (FILE *cache, guint16 n)
 {
   int i;
@@ -623,7 +643,7 @@ write_card16 (FILE *cache, guint16 n)
   return i == 1;
 }
 
-gboolean
+static gboolean
 write_card32 (FILE *cache, guint32 n)
 {
   int i;
@@ -636,7 +656,7 @@ write_card32 (FILE *cache, guint32 n)
 }
 
 
-gboolean
+static gboolean
 write_pixdata (FILE *cache, GdkPixdata *pixdata)
 {
   guint8 *s;
@@ -671,7 +691,7 @@ write_header (FILE *cache, guint32 dir_list_offset)
          write_card32 (cache, dir_list_offset));
 }
 
-guint
+static guint
 get_image_meta_data_size (Image *image)
 {
   gint i;
@@ -699,7 +719,7 @@ get_image_meta_data_size (Image *image)
   return len;
 }
 
-guint
+static guint
 get_image_pixel_data_size (Image *image)
 {
   if (image->pixel_data_size == 0)
@@ -715,7 +735,7 @@ get_image_pixel_data_size (Image *image)
   return image->pixel_data_size;
 }
 
-guint
+static guint
 get_image_data_size (Image *image)
 {
   guint len;
@@ -731,7 +751,7 @@ get_image_data_size (Image *image)
   return len;
 }
 
-guint
+static guint
 get_single_node_size (HashNode *node, gboolean include_image_data)
 {
   int len = 0;
@@ -758,7 +778,7 @@ get_single_node_size (HashNode *node, gboolean include_image_data)
   return len;
 }
 
-guint
+static guint
 get_bucket_size (HashNode *node)
 {
   int len = 0;
@@ -772,7 +792,7 @@ get_bucket_size (HashNode *node)
   return len;
 }
 
-gboolean
+static gboolean
 write_bucket (FILE *cache, HashNode *node, int *offset)
 {
   while (node != NULL)
@@ -869,7 +889,14 @@ write_bucket (FILE *cache, HashNode *node, int *offset)
            }
          else
            {
-             if (!write_card32 (cache, image->image_data->offset))
+             gint offset;
+
+             if (image->image_data)
+               offset = image->image_data->offset;
+             else
+               offset = 0;
+
+             if (!write_card32 (cache, offset))
                return FALSE;
            }
 
@@ -985,7 +1012,7 @@ write_bucket (FILE *cache, HashNode *node, int *offset)
   return TRUE;
 }
 
-gboolean
+static gboolean
 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
 {
   int offset = HASH_OFFSET;
@@ -1033,7 +1060,7 @@ write_hash_table (FILE *cache, HashContext *context, int *new_offset)
   return TRUE;
 }
 
-gboolean
+static gboolean
 write_dir_index (FILE *cache, int offset, GList *directories)
 {
   int n_dirs;
@@ -1067,7 +1094,7 @@ write_dir_index (FILE *cache, int offset, GList *directories)
   return TRUE;
 }
 
-gboolean
+static gboolean
 write_file (FILE *cache, GHashTable *files, GList *directories)
 {
   HashContext context;
@@ -1086,19 +1113,19 @@ write_file (FILE *cache, GHashTable *files, GList *directories)
    * back and change it later */
   if (!write_header (cache, 0))
     {
-      g_printerr ("Failed to write header\n");
+      g_printerr (_("Failed to write header\n"));
       return FALSE;
     }
 
   if (!write_hash_table (cache, &context, &new_offset))
     {
-      g_printerr ("Failed to write hash table\n");
+      g_printerr (_("Failed to write hash table\n"));
       return FALSE;
     }
 
   if (!write_dir_index (cache, new_offset, directories))
     {
-      g_printerr ("Failed to write directory index\n");
+      g_printerr (_("Failed to write directory index\n"));
       return FALSE;
     }
   
@@ -1106,17 +1133,20 @@ write_file (FILE *cache, GHashTable *files, GList *directories)
 
   if (!write_header (cache, new_offset))
     {
-      g_printerr ("Failed to rewrite header\n");
+      g_printerr (_("Failed to rewrite header\n"));
       return FALSE;
     }
     
   return TRUE;
 }
 
-void
+static void
 build_cache (const gchar *path)
 {
   gchar *cache_path, *tmp_cache_path;
+#ifdef G_OS_WIN32
+  gchar *bak_cache_path = NULL;
+#endif
   GHashTable *files;
   gboolean retval;
   FILE *cache;
@@ -1129,7 +1159,7 @@ build_cache (const gchar *path)
   
   if (!cache)
     {
-      g_printerr ("Failed to write cache file: %s\n", g_strerror (errno));
+      g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
       exit (1);
     }
 
@@ -1162,11 +1192,42 @@ build_cache (const gchar *path)
 
   cache_path = g_build_filename (path, CACHE_NAME, NULL);
 
+#ifdef G_OS_WIN32
+  if (g_file_test (cache_path, G_FILE_TEST_EXISTS))
+    {
+      bak_cache_path = g_strconcat (cache_path, ".bak", NULL);
+      g_unlink (bak_cache_path);
+      if (g_rename (cache_path, bak_cache_path) == -1)
+       {
+         g_printerr (_("Could not rename %s to %s: %s, removing %s then.\n"),
+                     cache_path, bak_cache_path,
+                     g_strerror (errno),
+                     cache_path);
+         g_unlink (cache_path);
+         bak_cache_path = NULL;
+       }
+    }
+#endif
+
   if (g_rename (tmp_cache_path, cache_path) == -1)
     {
+      g_printerr (_("Could not rename %s to %s: %s\n"),
+                 tmp_cache_path, cache_path,
+                 g_strerror (errno));
       g_unlink (tmp_cache_path);
+#ifdef G_OS_WIN32
+      if (bak_cache_path != NULL)
+       if (g_rename (bak_cache_path, cache_path) == -1)
+         g_printerr (_("Could not rename %s back to %s: %s.\n"),
+                     bak_cache_path, cache_path,
+                     g_strerror (errno));
+#endif
       exit (1);
     }
+#ifdef G_OS_WIN32
+  if (bak_cache_path != NULL)
+    g_unlink (bak_cache_path);
+#endif
 
   /* Update time */
   /* FIXME: What do do if an error occurs here? */
@@ -1179,12 +1240,50 @@ build_cache (const gchar *path)
   utime (path, &utime_buf);
   
   if (!quiet)
-    g_printerr ("Cache file created successfully.\n");
+    g_printerr (_("Cache file created successfully.\n"));
+}
+
+static void
+write_csource (const gchar *path)
+{
+  gchar *cache_path;
+  gchar *data;
+  gsize len;
+  gint i;
+
+  cache_path = g_build_filename (path, CACHE_NAME, NULL);
+  if (!g_file_get_contents (cache_path, &data, &len, NULL))
+    exit (1);
+  
+  g_printf ("#ifdef __SUNPRO_C\n");
+  g_printf ("#pragma align 4 (%s)\n", var_name);   
+  g_printf ("#endif\n");
+  
+  g_printf ("#ifdef __GNUC__\n");
+  g_printf ("static const guint8 %s[] __attribute__ ((__aligned__ (4))) = \n", var_name);
+  g_printf ("#else\n");
+  g_printf ("static const guint8 %s[] = \n", var_name);
+  g_printf ("#endif\n");
+
+  g_printf ("{\n");
+  for (i = 0; i < len - 1; i++)
+    {
+      if (i %12 == 0)
+       g_printf ("  ");
+      g_printf ("0x%02x, ", (guint8)data[i]);
+      if (i % 12 == 11)
+        g_printf ("\n");
+    }
+  
+  g_printf ("0x%02x\n};\n", (guint8)data[i]);
 }
 
 static GOptionEntry args[] = {
-  { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, "Overwrite an existing cache, even if uptodate", NULL },
-  { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, "Turn off verbose output", NULL },
+  { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, N_("Overwrite an existing cache, even if up to date"), NULL },
+  { "ignore-theme-index", 't', 0, G_OPTION_ARG_NONE, &ignore_theme_index, N_("Don't check for the existence of index.theme"), NULL },
+  { "index-only", 'i', 0, G_OPTION_ARG_NONE, &index_only, N_("Don't include image data in the cache"), NULL },
+  { "source", 'c', 0, G_OPTION_ARG_STRING, &var_name, N_("Output a C header file"), "NAME" },
+  { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, N_("Turn off verbose output"), NULL },
   { NULL }
 };
 
@@ -1196,9 +1295,12 @@ main (int argc, char **argv)
 
   if (argc < 2)
     return 0;
+  
+  bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
   context = g_option_context_new ("ICONPATH");
-  g_option_context_add_main_entries (context, args, NULL);
+  g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE);
 
   g_option_context_parse (context, &argc, &argv, NULL);
   
@@ -1207,11 +1309,21 @@ main (int argc, char **argv)
   path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
 #endif
   
+  if (!ignore_theme_index && !has_theme_index (path))
+    {
+      g_printerr (_("No theme index file in '%s'.\n"
+                   "If you really want to create an icon cache here, use --ignore-theme-index.\n"), path);
+      return 1;
+    }
+  
   if (!force_update && is_cache_up_to_date (path))
     return 0;
 
   g_type_init ();
   build_cache (path);
 
+  if (strcmp (var_name, "-") != 0)
+    write_csource (path);
+
   return 0;
 }