Index: gio/gicon.c
===================================================================
--- gio/gicon.c	(revision 7593)
+++ gio/gicon.c	(working copy)
@@ -21,7 +21,15 @@
  */
 
 #include "config.h"
+#include <stdlib.h>
+#include <string.h>
+
 #include "gicon.h"
+#include "gthemedicon.h"
+#include "gfileicon.h"
+#include "gemblemedicon.h"
+#include "gfile.h"
+#include "gioerror.h"
 
 #include "glibintl.h"
 
@@ -140,5 +148,335 @@
   return (* iface->equal) (icon1, icon2);
 }
 
+static char *
+_g_icon_to_string_tokenized (GIcon *icon)
+{
+  char *ret;
+
+  g_return_val_if_fail (icon != NULL, NULL);
+  g_return_val_if_fail (G_IS_ICON (icon), NULL);
+
+  /* We encode the icon as a series of tab-separated tokens.  The
+   * second token is the type of the icon. To help decoding, the
+   * number of tokens is the first token (not included in the count).
+   */
+  if (G_IS_FILE_ICON (icon))
+    {
+      GFileIcon *file_icon = G_FILE_ICON (icon);
+      GFile *file;
+      char *uri;
+      char *escaped_uri;
+
+      file = g_file_icon_get_file (file_icon);
+      uri = g_file_get_uri (file);
+      escaped_uri = g_uri_escape_string (uri, NULL, TRUE);
+
+      ret = g_strdup_printf ("2 GFileIcon %s", escaped_uri);
+
+      g_free (uri);
+      g_free (escaped_uri);
+    }
+  else if (G_IS_THEMED_ICON (icon))
+    {
+      GThemedIcon *themed_icon = G_THEMED_ICON (icon);
+      char *escaped_name;
+      char **names;
+      GString *s;
+
+      g_object_get (themed_icon,
+                    "names", &names,
+                    NULL);
+
+      s = g_string_new (0);
+      g_string_append_printf (s, "%d GThemedIcon", g_strv_length (names) + 1);
+
+      if (names != NULL)
+        {
+          int n;
+          for (n = 0; names[n] != NULL; n++)
+            {
+              escaped_name = g_uri_escape_string (names[n], NULL, TRUE);
+              g_string_append_c (s, ' ');
+              g_string_append (s, escaped_name);
+              g_free (escaped_name);
+            }
+        }
+
+      ret = g_string_free (s, FALSE);
+
+      g_strfreev (names);
+    }
+  else if (G_IS_EMBLEMED_ICON (icon))
+    {
+      char *base, *s;
+      GList *emblems, *e;
+      int n;
+      GString *str;
+
+      /* GEmblemedIcons are encoded as
+       *
+       *   <num_tokens> GEmblemedIcon <num_emblems> [<origin> <encoded_icon> ]*
+       */
+      str = g_string_new ("");
+      base = _g_icon_to_string_tokenized (g_emblemed_icon_get_icon (G_EMBLEMED_ICON (icon)));
+      emblems = g_emblemed_icon_get_emblems (G_EMBLEMED_ICON (icon));
+      g_string_append_printf (str, "GEmblemedIcon %s %d", base, g_list_length (emblems));
+      n = atoi (base) + 2;
+      g_free (base);
+      for (e = emblems; e; e = e->next)
+        {
+          s = _g_icon_to_string_tokenized (g_emblem_get_icon (G_EMBLEM (e->data)));
+          g_string_append_printf (str, " %d %s", g_emblem_get_origin (G_EMBLEM (e->data)), s);
+          n += atoi (s) + 2;
+          g_free (s);
+        }
+
+      s = g_string_free (str, FALSE);
+      ret = g_strdup_printf ("%d %s", n + 1, s);
+      g_free (s);
+    }
+  else
+    {
+      ret = NULL;
+      g_warning ("unknown icon type; please add support");
+    }
+
+  return ret;
+}
+
+/**
+ * g_icon_to_string:
+ * @icon: a #GIcon.
+ *
+ * Generates a textual representation of @icon that can be used for
+ * serialization such as when passing @icon to a different process or
+ * saving it to persistent storage. Use g_icon_new_for_string() to
+ * get @icon back from the returned string.
+ *
+ * The encoding of the returned string is proprietary to #GIcon except
+ * in the following two cases
+ *
+ * <itemizedlist>
+ * <listitem>
+ *   If @icon is a #GFileIcon, the returned string is a native path
+ *   (such as <literal>/path/to/icon.png</literal>) without escaping
+ *   if the #GFile for @icon is a native file.  If the file is not
+ *   native, the returned string is the result of g_file_get_uri().
+ * </listitem>
+ * <listitem>
+ *  If @icon is a #GThemedIcon with exactly one name, the encoding is
+ *  simply the name (such as <literal>network-server</literal>).
+ * </listitem>
+ * </itemizedlist>
+ *
+ * Returns: A newly allocated NUL-terminated string. Use g_free() to
+ * free.
+ *
+ * Since: 2.20
+ */
+gchar *
+g_icon_to_string (GIcon *icon)
+{
+  gchar *ret;
+
+  g_return_val_if_fail (icon != NULL, NULL);
+  g_return_val_if_fail (G_IS_ICON (icon), NULL);
+
+  ret = NULL;
+
+  if (G_IS_FILE_ICON (icon))
+    {
+      GFile *file;
+
+      file = g_file_icon_get_file (G_FILE_ICON (icon));
+      if (g_file_is_native (file))
+        ret = g_file_get_path (file);
+      else
+        ret = g_file_get_uri (file);
+    }
+  else if (G_IS_THEMED_ICON (icon))
+    {
+      gchar **names;
+
+      g_object_get (icon,
+                    "names", &names,
+                    NULL);
+
+      if (names != NULL)
+        {
+          if (g_strv_length (names) == 1)
+            {
+              ret = g_strdup (names[0]);
+            }
+          g_strfreev (names);
+        }
+    }
+
+  if (ret == NULL)
+    ret = _g_icon_to_string_tokenized (icon);
+
+  return ret;
+}
+
+static GIcon *
+_g_icon_new_from_tokens (char **tokens, int num_tokens)
+{
+  GIcon *icon;
+
+  icon = NULL;
+
+  if (strcmp (tokens[0], "GFileIcon") == 0)
+    {
+      GFile *file;
+      gchar *unescaped_uri;
+
+      if (num_tokens != 2)
+        goto out;
+
+      unescaped_uri = g_uri_unescape_string (tokens[1], NULL);
+      file = g_file_new_for_uri (unescaped_uri);
+      icon = g_file_icon_new (file);
+      g_object_unref (file);
+      g_free (unescaped_uri);
+    }
+  else if (strcmp (tokens[0], "GThemedIcon") == 0)
+    {
+      int n;
+
+      for (n = 1; n < num_tokens; n++)
+        {
+          gchar *unescaped_name;
+
+          unescaped_name = g_uri_unescape_string (tokens[n], NULL);
+          if (icon == NULL)
+            icon = g_themed_icon_new (unescaped_name);
+          else
+            g_themed_icon_append_name (G_THEMED_ICON (icon), unescaped_name);
+          g_free (unescaped_name);
+        }
+    }
+  else if (strcmp (tokens[0], "GEmblemedIcon") == 0)
+    {
+       int n, m, i;
+       GIcon *base, *e_icon;
+       GEmblem *emblem;
+       GList *emblems, *e;
+       GEmblemOrigin origin;
+       gchar **t = tokens;
+
+       t++;
+       n = atoi (*t);
+       t++;
+       if (t - tokens >= num_tokens)
+         goto out;
+       base = _g_icon_new_from_tokens (t, n);
+       if (base == NULL)
+         goto out;
+
+       t = t + n;
+       m = atoi (*t);
+       t++;
+       emblems = NULL;
+       for (i = 0; i < m; i++)
+         {
+           origin = atoi (*t);
+           t++;
+           n = atoi (*t);
+           t++;
+           if (t - tokens >= num_tokens)
+             goto cleanup;
+           e_icon = _g_icon_new_from_tokens (t, n);
+           t += n;
+           if (e_icon == NULL)
+             goto cleanup;
+           emblem = g_emblem_new_with_origin (e_icon, origin);
+           emblems = g_list_append (emblems, emblem);
+         }
+       icon = g_emblemed_icon_new (base, (GEmblem*)emblems->data);
+       for (e = emblems->next; e; e = e->next)
+         g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon), (GEmblem*)e->data);
+
+ cleanup:
+       g_object_unref (base);
+       g_list_foreach (emblems, (GFunc)g_object_unref, NULL);
+       g_list_free (emblems);
+    }
+
+ out:
+   return icon;
+}
+
+/**
+ * g_icon_new_for_string:
+ * @str: A string obtained via g_icon_to_string().
+ * @error: Return location for error.
+ *
+ * Generate a #GIcon instance from @str. This function can fail if
+ * @str is not valid - see g_icon_to_string() for discussion.
+ *
+ * Returns: An object implementing the #GIcon interface or %NULL if
+ * @error is set.
+ *
+ * Since: 2.20
+ **/
+GIcon *
+g_icon_new_for_string (const gchar   *str,
+                       GError       **error)
+{
+  GIcon *icon;
+
+  g_return_val_if_fail (str != NULL, NULL);
+
+  icon = NULL;
+
+  /* handle special GFileIcon and GThemedIcon cases; only these never start with digits */
+  if (!g_ascii_isdigit (str[0]))
+    {
+      gchar *scheme;
+
+      scheme = g_uri_parse_scheme (str);
+      if (scheme != NULL || str[0] == '/')
+        {
+          GFile *location;
+
+          location = g_file_new_for_commandline_arg (str);
+          icon = g_file_icon_new (location);
+          g_object_unref (location);
+          g_free (scheme);
+        }
+      else
+        {
+          icon = g_themed_icon_new (str);
+        }
+    }
+  else
+    {
+      gchar **tokens;
+
+      tokens = g_strsplit (str, " ", 0);
+
+      /* handle tokenized encoding */
+      if (g_strv_length (tokens) >= 3)
+        {
+          gint num_tokens;
+          num_tokens = atoi (tokens[0]);
+          icon = _g_icon_new_from_tokens (tokens + 1, num_tokens);
+        }
+
+      g_strfreev (tokens);
+    }
+
+  if (icon == NULL)
+    {
+      g_set_error (error,
+                   G_IO_ERROR,
+                   G_IO_ERROR_INVALID_ARGUMENT,
+                   _("Malformed GIcon data \"%s\""), str);
+    }
+
+  return icon;
+}
+
+
 #define __G_ICON_C__
 #include "gioaliasdef.c"
Index: gio/gicon.h
===================================================================
--- gio/gicon.h	(revision 7593)
+++ gio/gicon.h	(working copy)
@@ -59,17 +59,21 @@
 
   /* Virtual Table */
 
-  guint    (* hash)  (GIcon *icon);
-  gboolean (* equal) (GIcon *icon1,
-                      GIcon *icon2);
+  guint       (* hash)     (GIcon *icon);
+  gboolean    (* equal)    (GIcon *icon1,
+                            GIcon *icon2);
 };
 
-GType    g_icon_get_type (void) G_GNUC_CONST;
+GType    g_icon_get_type  (void) G_GNUC_CONST;
 
-guint    g_icon_hash     (gconstpointer  icon);
-gboolean g_icon_equal    (GIcon         *icon1,
-                          GIcon         *icon2);
+guint    g_icon_hash      (gconstpointer  icon);
+gboolean g_icon_equal     (GIcon         *icon1,
+                           GIcon         *icon2);
 
+gchar   *g_icon_to_string       (GIcon         *icon);
+GIcon   *g_icon_new_for_string  (const gchar   *str,
+                                 GError       **error);
+
 G_END_DECLS
 
 #endif /* __G_ICON_H__ */
Index: gio/ChangeLog
===================================================================
--- gio/ChangeLog	(revision 7594)
+++ gio/ChangeLog	(working copy)
@@ -1,5 +1,17 @@
 2008-10-12  David Zeuthen  <davidz@redhat.com>
 
+	Add GIcon serialization code (#555740).
+
+	* gio/gicon.[ch]: Add g_icon_to_string() and
+	g_icon_new_for_string(). Originally written by Matthias Clasen for
+	gvfs.
+
+	* gio/tests/Makefile.am:
+	* gio/tests/g-icon.c:
+	test cases for GIcon serialization
+
+2008-10-12  David Zeuthen  <davidz@redhat.com>
+
 	* gio.symbols:
 	* gunionvolumemonitor.c:
 	* gvolumemonitor.h:
Index: gio/tests/Makefile.am
===================================================================
--- gio/tests/Makefile.am	(revision 7593)
+++ gio/tests/Makefile.am	(working copy)
@@ -22,7 +22,8 @@
 	g-file 			\
 	g-file-info 		\
 	data-input-stream 	\
-	data-output-stream 
+	data-output-stream 	\
+	g-icon
 
 if OS_UNIX
 TEST_PROGS += live-g-file unix-streams desktop-app-info
@@ -46,6 +47,9 @@
 data_output_stream_SOURCES	= data-output-stream.c
 data_output_stream_LDADD	= $(progs_ldadd)
 
+g_icon_SOURCES	= g-icon.c
+g_icon_LDADD	= $(progs_ldadd)
+
 live_g_file_SOURCES	  = live-g-file.c
 live_g_file_LDADD	  = $(progs_ldadd)
 
Index: docs/reference/gio/gio-sections.txt
===================================================================
--- docs/reference/gio/gio-sections.txt	(revision 7593)
+++ docs/reference/gio/gio-sections.txt	(working copy)
@@ -351,6 +351,8 @@
 GIconIface
 g_icon_hash
 g_icon_equal
+g_icon_to_string
+g_icon_new_for_string
 <SUBSECTION Standard>
 G_ICON
 G_IS_ICON
--- /dev/null	2008-10-04 13:13:26.878056362 -0400
+++ gio/tests/g-icon.c	2008-10-12 13:08:40.000000000 -0400
@@ -0,0 +1,184 @@
+/* GLib testing framework examples and tests
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * This work is provided "as is"; redistribution and modification
+ * in whole or in part, in any medium, physical or electronic is
+ * permitted without restriction.
+ *
+ * This work is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * In no event shall the authors or contributors be liable for any
+ * direct, indirect, incidental, special, exemplary, or consequential
+ * damages (including, but not limited to, procurement of substitute
+ * goods or services; loss of use, data, or profits; or business
+ * interruption) however caused and on any theory of liability, whether
+ * in contract, strict liability, or tort (including negligence or
+ * otherwise) arising in any way out of the use of this software, even
+ * if advised of the possibility of such damage.
+ *
+ * Authors: David Zeuthen <davidz@redhat.com>
+ */
+
+#include <glib/glib.h>
+#include <gio/gio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void
+test_g_icon_serialize (void)
+{
+  GIcon *icon;
+  GIcon *icon2;
+  GIcon *icon3;
+  GIcon *icon4;
+  GIcon *icon5;
+  GEmblem *emblem1;
+  GEmblem *emblem2;
+  const char *uri;
+  GFile *location;
+  char *data;
+  GError *error;
+
+  error = NULL;
+
+  /* check that GFileIcon and GThemedIcon serialize to the encoding specified */
+
+  uri = "file:///some/native/path/to/an/icon.png";
+  location = g_file_new_for_uri (uri);
+  icon = g_file_icon_new (location);
+  data = g_icon_to_string (icon);
+  g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon.png");
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  uri = "file:///some/native/path/to/an/icon with spaces.png";
+  location = g_file_new_for_uri (uri);
+  icon = g_file_icon_new (location);
+  data = g_icon_to_string (icon);
+  g_assert_cmpstr (data, ==, "/some/native/path/to/an/icon with spaces.png");
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  uri = "sftp:///some/non-native/path/to/an/icon.png";
+  location = g_file_new_for_uri (uri);
+  icon = g_file_icon_new (location);
+  data = g_icon_to_string (icon);
+  g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon.png");
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  uri = "sftp:///some/non-native/path/to/an/icon with spaces.png";
+  location = g_file_new_for_uri (uri);
+  icon = g_file_icon_new (location);
+  data = g_icon_to_string (icon);
+  g_assert_cmpstr (data, ==, "sftp:///some/non-native/path/to/an/icon%20with%20spaces.png");
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  icon = g_themed_icon_new ("network-server");
+  data = g_icon_to_string (icon);
+  g_assert_cmpstr (data, ==, "network-server");
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  /* Check that we can serialize from well-known specified formats */
+  icon = g_icon_new_for_string ("network-server", &error);
+  g_assert_no_error (error);
+  icon2 = g_themed_icon_new ("network-server");
+  g_assert (g_icon_equal (icon, icon2));
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  icon = g_icon_new_for_string ("/path/to/somewhere.png", &error);
+  g_assert_no_error (error);
+  location = g_file_new_for_commandline_arg ("/path/to/somewhere.png");
+  icon2 = g_file_icon_new (location);
+  g_assert (g_icon_equal (icon, icon2));
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  icon = g_icon_new_for_string ("sftp:///path/to/somewhere.png", &error);
+  g_assert_no_error (error);
+  location = g_file_new_for_commandline_arg ("sftp:///path/to/somewhere.png");
+  icon2 = g_file_icon_new (location);
+  g_assert (g_icon_equal (icon, icon2));
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  /* Check that GThemedIcon serialization works */
+
+  icon = g_themed_icon_new ("network-server");
+  g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
+  data = g_icon_to_string (icon);
+  icon2 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon, icon2));
+  g_free (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  /* Check that GEmblemedIcon serialization works */
+
+  icon = g_themed_icon_new ("face-smirk");
+  icon2 = g_themed_icon_new ("emblem-important");
+  g_themed_icon_append_name (G_THEMED_ICON (icon2), "emblem-shared");
+  location = g_file_new_for_uri ("file:///some/path/somewhere.png");
+  icon3 = g_file_icon_new (location);
+  g_object_unref (location);
+  emblem1 = g_emblem_new_with_origin (icon2, G_EMBLEM_ORIGIN_DEVICE);
+  emblem2 = g_emblem_new_with_origin (icon3, G_EMBLEM_ORIGIN_LIVEMETADATA);
+  icon4 = g_emblemed_icon_new (icon, emblem1);
+  g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon4), emblem2);
+  data = g_icon_to_string (icon4);
+  icon5 = g_icon_new_for_string (data, &error);
+  g_assert_no_error (error);
+  g_assert (g_icon_equal (icon4, icon5));
+  g_object_unref (emblem1);
+  g_object_unref (emblem2);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (icon3);
+  g_object_unref (icon4);
+  g_object_unref (icon5);
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/g-icon/serialize", test_g_icon_serialize);
+
+  return g_test_run();
+}
