[glib/wip/gicon] GIcon serialize wip



commit c447062e42944470d0b34d581368fcaad58118b1
Author: Ryan Lortie <desrt moonpix lan>
Date:   Fri Feb 15 11:12:47 2013 -0500

    GIcon serialize wip

 gio/Makefile.am     |    2 +
 gio/gbytesicon.c    |  258 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gio/gbytesicon.h    |   57 +++++++++++
 gio/gemblem.c       |   18 ++++
 gio/gemblemedicon.c |   33 +++++++
 gio/gicon.c         |  116 +++++++++++++++++++++++
 gio/gicon.h         |    7 ++
 gio/gio.h           |    1 +
 gio/giotypes.h      |    1 +
 gio/gmenu.c         |   36 +++++++
 gio/gmenu.h         |    4 +
 gio/tests/g-icon.c  |  182 ++++++++++++++++++++++++++++++++++++-
 12 files changed, 714 insertions(+), 1 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 136f9ef..35a581d 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -337,6 +337,7 @@ libgio_2_0_la_SOURCES =             \
        gasyncresult.c          \
        gbufferedinputstream.c  \
        gbufferedoutputstream.c \
+       gbytesicon.c            \
        gcancellable.c          \
        gcharsetconverter.c     \
        gconverter.c            \
@@ -516,6 +517,7 @@ gio_headers =                       \
        gasyncresult.h          \
        gbufferedinputstream.h  \
        gbufferedoutputstream.h \
+       gbytesicon.h            \
        gcancellable.h          \
        gcontenttype.h          \
        gcharsetconverter.h     \
diff --git a/gio/gbytesicon.c b/gio/gbytesicon.c
new file mode 100644
index 0000000..cafbb03
--- /dev/null
+++ b/gio/gbytesicon.c
@@ -0,0 +1,258 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright © 2013 Canonical Limited
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Ryan Lortie <desrt desrt ca>
+ */
+
+#include "config.h"
+
+#include "gbytesicon.h"
+#include "gbytes.h"
+#include "gicon.h"
+#include "glibintl.h"
+#include "gloadableicon.h"
+#include "gmemoryinputstream.h"
+#include "gtask.h"
+#include "gioerror.h"
+
+
+/**
+ * SECTION:gbytesicon
+ * @short_description: An icon stored in memory as a #GBytes
+ * @include: gio/gio.h
+ * @see_also: #GIcon, #GLoadableIcon, #GBytes
+ * 
+ * #GBytesIcon specifies an image held in memory in a common format (usually
+ * png) to be used as icon.
+ *
+ * Since: 2.36
+ **/
+
+typedef GObjectClass GBytesIconClass;
+
+struct _GBytesIcon
+{
+  GObject parent_instance;
+
+  GBytes *bytes;
+};
+
+enum
+{
+  PROP_0,
+  PROP_BYTES
+};
+
+static void g_bytes_icon_icon_iface_init          (GIconIface          *iface);
+static void g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
+G_DEFINE_TYPE_WITH_CODE (GBytesIcon, g_bytes_icon, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_bytes_icon_icon_iface_init)
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, g_bytes_icon_loadable_icon_iface_init))
+
+static void
+g_bytes_icon_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  GBytesIcon *icon = G_BYTES_ICON (object);
+
+  switch (prop_id)
+    {
+      case PROP_BYTES:
+        g_value_set_boxed (value, icon->bytes);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_bytes_icon_set_property (GObject      *object,
+                          guint         prop_id,
+                          const GValue *value,
+                          GParamSpec   *pspec)
+{
+  GBytesIcon *icon = G_BYTES_ICON (object);
+
+  switch (prop_id)
+    {
+      case PROP_BYTES:
+        icon->bytes = g_value_dup_boxed (value);
+        break;
+
+      default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+g_bytes_icon_finalize (GObject *object)
+{
+  GBytesIcon *icon;
+
+  icon = G_BYTES_ICON (object);
+
+  g_object_unref (icon->bytes);
+
+  G_OBJECT_CLASS (g_bytes_icon_parent_class)->finalize (object);
+}
+
+static void
+g_bytes_icon_class_init (GBytesIconClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  
+  gobject_class->get_property = g_bytes_icon_get_property;
+  gobject_class->set_property = g_bytes_icon_set_property;
+  gobject_class->finalize = g_bytes_icon_finalize;
+
+  /**
+   * GBytesIcon:bytes:
+   *
+   * The bytes containing the icon.
+   */
+  g_object_class_install_property (gobject_class, PROP_BYTES,
+                                   g_param_spec_object ("bytes",
+                                                        P_("bytes"),
+                                                        P_("The bytes containing the icon"),
+                                                        G_TYPE_BYTES,
+                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | 
G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
+}
+
+static void
+g_bytes_icon_init (GBytesIcon *bytes)
+{
+}
+
+/**
+ * g_bytes_icon_new:
+ * @bytes: a #GBytes.
+ * 
+ * Creates a new icon for a bytes.
+ * 
+ * Returns: (transfer full) (type GBytesIcon): a #GIcon for the given
+ *   @bytes, or %NULL on error.
+ **/
+GIcon *
+g_bytes_icon_new (GBytes *bytes)
+{
+  g_return_val_if_fail (bytes != NULL, NULL);
+
+  return g_object_new (G_TYPE_BYTES_ICON, "bytes", bytes, NULL);
+}
+
+/**
+ * g_bytes_icon_get_bytes:
+ * @icon: a #GIcon.
+ * 
+ * Gets the #GBytes associated with the given @icon.
+ * 
+ * Returns: (transfer none): a #GBytes, or %NULL.
+ **/
+GBytes *
+g_bytes_icon_get_bytes (GBytesIcon *icon)
+{
+  g_return_val_if_fail (G_IS_BYTES_ICON (icon), NULL);
+
+  return icon->bytes;
+}
+
+static guint
+g_bytes_icon_hash (GIcon *icon)
+{
+  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
+
+  return g_bytes_hash (bytes_icon->bytes);
+}
+
+static gboolean
+g_bytes_icon_equal (GIcon *icon1,
+                   GIcon *icon2)
+{
+  GBytesIcon *bytes1 = G_BYTES_ICON (icon1);
+  GBytesIcon *bytes2 = G_BYTES_ICON (icon2);
+  
+  return g_bytes_equal (bytes1->bytes, bytes2->bytes);
+}
+
+static GVariant *
+g_bytes_icon_serialize (GIcon *icon)
+{
+  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
+
+  return g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes_icon->bytes, TRUE);
+}
+
+static void
+g_bytes_icon_icon_iface_init (GIconIface *iface)
+{
+  iface->hash = g_bytes_icon_hash;
+  iface->equal = g_bytes_icon_equal;
+  iface->serialize = g_bytes_icon_serialize;
+}
+
+static GInputStream *
+g_bytes_icon_load (GLoadableIcon  *icon,
+                   int            size,
+                   char          **type,
+                   GCancellable   *cancellable,
+                   GError        **error)
+{
+  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
+
+  return g_memory_input_stream_new_from_bytes (bytes_icon->bytes);
+}
+
+static void
+g_bytes_icon_load_async (GLoadableIcon       *icon,
+                         int                  size,
+                         GCancellable        *cancellable,
+                         GAsyncReadyCallback  callback,
+                         gpointer             user_data)
+{
+  GBytesIcon *bytes_icon = G_BYTES_ICON (icon);
+  GTask *task;
+
+  task = g_task_new (icon, cancellable, callback, user_data);
+  g_task_return_pointer (task, g_memory_input_stream_new_from_bytes (bytes_icon->bytes), g_object_unref);
+}
+
+static GInputStream *
+g_bytes_icon_load_finish (GLoadableIcon  *icon,
+                          GAsyncResult   *res,
+                          char          **type,
+                          GError        **error)
+{
+  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
+
+  if (type)
+    *type = NULL;
+  
+  return g_task_propagate_pointer (G_TASK (res), error);
+}
+
+static void
+g_bytes_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
+{
+  iface->load = g_bytes_icon_load;
+  iface->load_async = g_bytes_icon_load_async;
+  iface->load_finish = g_bytes_icon_load_finish;
+}
diff --git a/gio/gbytesicon.h b/gio/gbytesicon.h
new file mode 100644
index 0000000..5d99612
--- /dev/null
+++ b/gio/gbytesicon.h
@@ -0,0 +1,57 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2006-2007 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Ryan Lortie <desrt desrt ca>
+ */
+
+#ifndef __G_BYTES_ICON_H__
+#define __G_BYTES_ICON_H__
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_BYTES_ICON         (g_bytes_icon_get_type ())
+#define G_BYTES_ICON(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_BYTES_ICON, GBytesIcon))
+#define G_BYTES_ICON_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_BYTES_ICON, GBytesIconClass))
+#define G_IS_BYTES_ICON(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_BYTES_ICON))
+#define G_IS_BYTES_ICON_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_BYTES_ICON))
+#define G_BYTES_ICON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_BYTES_ICON, GBytesIconClass))
+
+/**
+ * GBytesIcon:
+ *
+ * Gets an icon for a #GBytes. Implements #GLoadableIcon.
+ **/
+GLIB_AVAILABLE_IN_2_36
+GType   g_bytes_icon_get_type (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_36
+GIcon * g_bytes_icon_new      (GBytes     *bytes);
+
+GLIB_AVAILABLE_IN_2_36
+GBytes * g_bytes_icon_get_bytes (GBytesIcon *icon);
+
+G_END_DECLS
+
+#endif /* __G_BYTES_ICON_H__ */
diff --git a/gio/gemblem.c b/gio/gemblem.c
index 1df712b..77ea405 100644
--- a/gio/gemblem.c
+++ b/gio/gemblem.c
@@ -349,6 +349,23 @@ g_emblem_from_tokens (gchar  **tokens,
   return G_ICON (emblem);
 }
 
+static GVariant *
+g_emblem_serialize (GIcon *icon)
+{
+  GEmblem *emblem = G_EMBLEM (icon);
+  GVariant *icon_data;
+  GVariant *result;
+
+  icon_data = g_icon_serialize (emblem->icon);
+  if (!icon_data)
+    return NULL;
+
+  result = g_variant_new ("(vu)", icon_data, emblem->origin);
+  g_variant_unref (icon_data);
+
+  return result;
+}
+
 static void
 g_emblem_iface_init (GIconIface *iface)
 {
@@ -356,4 +373,5 @@ g_emblem_iface_init (GIconIface *iface)
   iface->equal = g_emblem_equal;
   iface->to_tokens = g_emblem_to_tokens;
   iface->from_tokens = g_emblem_from_tokens;
+  iface->serialize = g_emblem_serialize;
 }
diff --git a/gio/gemblemedicon.c b/gio/gemblemedicon.c
index c10dc9b..51005f0 100644
--- a/gio/gemblemedicon.c
+++ b/gio/gemblemedicon.c
@@ -413,6 +413,38 @@ g_emblemed_icon_from_tokens (gchar  **tokens,
   return NULL;
 }
 
+static GVariant *
+g_emblemed_icon_serialize (GIcon *icon)
+{
+  GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
+  GVariantBuilder builder;
+  GVariant *icon_data;
+  GList *node;
+
+  icon_data = g_icon_serialize (emblemed_icon->priv->icon);
+  if (!icon_data)
+    return NULL;
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(vav)"));
+
+  g_variant_builder_add (&builder, "v", icon_data);
+  g_variant_unref (icon_data);
+
+  g_variant_builder_open (&builder, G_VARIANT_TYPE ("av"));
+  for (node = emblemed_icon->priv->emblems; node != NULL; node = node->next)
+    {
+      icon_data = g_icon_serialize (node->data);
+      if (icon_data)
+        {
+          g_variant_builder_add (&builder, "v", icon_data);
+          g_variant_unref (icon_data);
+        }
+    }
+  g_variant_builder_close (&builder);
+
+  return g_variant_builder_end (&builder);
+}
+
 static void
 g_emblemed_icon_icon_iface_init (GIconIface *iface)
 {
@@ -420,4 +452,5 @@ g_emblemed_icon_icon_iface_init (GIconIface *iface)
   iface->equal = g_emblemed_icon_equal;
   iface->to_tokens = g_emblemed_icon_to_tokens;
   iface->from_tokens = g_emblemed_icon_from_tokens;
+  iface->serialize = g_emblemed_icon_serialize;
 }
diff --git a/gio/gicon.c b/gio/gicon.c
index b75e067..573a5c4 100644
--- a/gio/gicon.c
+++ b/gio/gicon.c
@@ -28,6 +28,7 @@
 #include "gthemedicon.h"
 #include "gfileicon.h"
 #include "gemblemedicon.h"
+#include "gbytesicon.h"
 #include "gfile.h"
 #include "gioerror.h"
 
@@ -447,3 +448,118 @@ g_icon_new_for_string (const gchar   *str,
 
   return icon;
 }
+
+GVariant *
+g_icon_serialize (GIcon *icon)
+{
+  GIconInterface *iface;
+  GVariant *result;
+
+  iface = G_ICON_GET_IFACE (icon);
+
+  if (iface->serialize)
+    result = (* iface->serialize) (icon);
+  else 
+    result = NULL;
+
+  if (result)
+    g_variant_take_ref (result);
+  else
+    {
+      gchar *str;
+
+      /* try this: will get the file and themed cases too. */
+      str = g_icon_to_string (icon);
+      if (str)
+        {
+          result = g_variant_ref_sink (g_variant_new_string (str));
+          g_free (str);
+        }
+    }
+
+  return result;
+}
+
+GIcon *
+g_icon_deserialize (GVariant *value)
+{
+  if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
+    return g_icon_new_for_string (g_variant_get_string (value, NULL), NULL);
+
+  else if (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING))
+    {
+      GBytes *bytes;
+      GIcon *icon;
+
+      bytes = g_variant_get_data_as_bytes (value);
+      icon = g_bytes_icon_new (bytes);
+      g_bytes_unref (bytes);
+
+      return icon;
+    }
+
+  else if (g_variant_is_of_type (value, G_VARIANT_TYPE("(vav)")))
+    {
+      GVariantIter *emblems;
+      GVariant *emblem_data;
+      GVariant *icon_data;
+      GIcon *emblemed;
+      GIcon *icon;
+
+      g_variant_get (value, "(vav)", &icon_data, &emblems);
+      icon = g_icon_deserialize (icon_data);
+
+      if (icon)
+        {
+          emblemed = g_emblemed_icon_new (icon, NULL);
+
+          while (g_variant_iter_loop (emblems, "v", &emblem_data))
+            {
+              GIcon *emblem;
+
+              emblem = g_icon_deserialize (emblem_data);
+              if (emblem)
+               {
+                  if (G_IS_EMBLEM (emblem))
+                    g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (emblemed), G_EMBLEM (emblem));
+                  g_object_unref (emblem);
+               }
+            }
+
+          g_object_unref (icon);
+        }
+      else
+        emblemed = NULL;
+
+      g_variant_iter_free (emblems);
+      g_variant_unref (icon_data);
+
+      return emblemed;
+    }
+
+  else if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(vu)")))
+    {
+      GVariant *icon_data;
+      GIcon *emblem;
+      GIcon *icon;
+      guint origin;
+
+      g_variant_get (value, "(vu)", &icon_data, &origin);
+      icon = g_icon_deserialize (icon_data);
+
+      if (icon)
+        {
+          emblem = G_ICON (g_emblem_new_with_origin (icon, origin));
+          g_object_unref (icon);
+        }
+      else
+        emblem = NULL;
+
+      g_variant_unref (icon_data);
+
+      return emblem;
+    }
+
+  else
+    return NULL;
+}
diff --git a/gio/gicon.h b/gio/gicon.h
index 81e32ac..aeb7dda 100644
--- a/gio/gicon.h
+++ b/gio/gicon.h
@@ -75,6 +75,8 @@ struct _GIconIface
                                gint     num_tokens,
                                gint     version,
                                GError **error);
+
+  GVariant *  (* serialize)   (GIcon   *icon);
 };
 
 GLIB_AVAILABLE_IN_ALL
@@ -91,6 +93,11 @@ GLIB_AVAILABLE_IN_ALL
 GIcon   *g_icon_new_for_string  (const gchar   *str,
                                  GError       **error);
 
+GLIB_AVAILABLE_IN_2_36
+GVariant * g_icon_serialize    (GIcon         *icon);
+GLIB_AVAILABLE_IN_2_36
+GIcon *    g_icon_deserialize   (GVariant      *value);
+
 G_END_DECLS
 
 #endif /* __G_ICON_H__ */
diff --git a/gio/gio.h b/gio/gio.h
index 1998a64..1e669ac 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -38,6 +38,7 @@
 #include <gio/gasyncresult.h>
 #include <gio/gbufferedinputstream.h>
 #include <gio/gbufferedoutputstream.h>
+#include <gio/gbytesicon.h>
 #include <gio/gcancellable.h>
 #include <gio/gcharsetconverter.h>
 #include <gio/gcontenttype.h>
diff --git a/gio/giotypes.h b/gio/giotypes.h
index 2696091..d09bfae 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -117,6 +117,7 @@ typedef struct _GIOExtension                  GIOExtension;
 typedef struct _GIOSchedulerJob               GIOSchedulerJob;
 typedef struct _GIOStreamAdapter              GIOStreamAdapter;
 typedef struct _GLoadableIcon                 GLoadableIcon; /* Dummy typedef */
+typedef struct _GBytesIcon                    GBytesIcon;
 typedef struct _GMemoryInputStream            GMemoryInputStream;
 typedef struct _GMemoryOutputStream           GMemoryOutputStream;
 
diff --git a/gio/gmenu.c b/gio/gmenu.c
index d0b4130..0dafb35 100644
--- a/gio/gmenu.c
+++ b/gio/gmenu.c
@@ -25,6 +25,8 @@
 
 #include <string.h>
 
+#include "gicon.h"
+
 /**
  * SECTION:gmenu
  * @title: GMenu
@@ -1334,3 +1336,37 @@ g_menu_item_new_from_model (GMenuModel *model,
 
   return menu_item;
 }
+
+/**
+ * g_menu_item_set_icon:
+ * @menu_item: a #GMenuItem
+ * @icon: a #GIcon, or %NULL
+ *
+ * Sets (or unsets) the icon on @menu_item.
+ *
+ * This call is the same as calling g_icon_serialize() and using the
+ * result as the value to g_menu_item_set_attribute_value() for the
+ * "icon" attribute.
+ *
+ * If @icon is %NULL then the icon is unset.
+ *
+ * Since: 2.36
+ **/
+void
+g_menu_item_set_icon (GMenuItem *menu_item,
+                      GIcon     *icon)
+{
+  GVariant *value;
+
+  g_return_if_fail (G_IS_MENU_ITEM (menu_item));
+  g_return_if_fail (G_IS_ICON (icon));
+
+  if (icon != NULL)
+    value = g_icon_serialize (icon);
+  else
+    value = NULL;
+
+  g_menu_item_set_attribute_value (menu_item, "icon", value);
+  if (value)
+    g_variant_unref (value);
+}
diff --git a/gio/gmenu.h b/gio/gmenu.h
index 729915d..afb1f61 100644
--- a/gio/gmenu.h
+++ b/gio/gmenu.h
@@ -172,6 +172,10 @@ GLIB_AVAILABLE_IN_2_32
 void        g_menu_item_set_detailed_action         (GMenuItem   *menu_item,
                                                      const gchar *detailed_action);
 
+GLIB_AVAILABLE_IN_2_36
+void        g_menu_item_set_icon                    (GMenuItem   *menu_item,
+                                                     GIcon       *icon);
+
 G_END_DECLS
 
 #endif /* __G_MENU_H__ */
diff --git a/gio/tests/g-icon.c b/gio/tests/g-icon.c
index 8431135..cede1cf 100644
--- a/gio/tests/g-icon.c
+++ b/gio/tests/g-icon.c
@@ -28,7 +28,7 @@
 #include <string.h>
 
 static void
-test_g_icon_serialize (void)
+test_g_icon_to_string (void)
 {
   GIcon *icon;
   GIcon *icon2;
@@ -248,6 +248,185 @@ test_g_icon_serialize (void)
 }
 
 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;
+  GVariant *data;
+  gint origin;
+  GIcon *i;
+  GFile *file;
+
+  /* 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);
+
+  g_object_get (icon, "file", &file, NULL);
+  g_assert (file == location);
+  g_object_unref (file);
+
+  data = g_icon_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S 
"native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon.png");
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (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_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, G_DIR_SEPARATOR_S "some" G_DIR_SEPARATOR_S 
"native" G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" G_DIR_SEPARATOR_S "an" G_DIR_SEPARATOR_S "icon with 
spaces.png");
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (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_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, "sftp:///some/non-native/path/to/an/icon.png";);
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+  g_object_unref (location);
+
+  icon = g_themed_icon_new ("network-server");
+  data = g_icon_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, "network-server");
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  /* Check that we can serialize from well-known specified formats */
+  data = g_variant_new_string ("network-server%");
+  icon = g_icon_deserialize (g_variant_ref_sink (data));
+  g_variant_unref (data);
+  icon2 = g_themed_icon_new ("network-server%");
+  g_assert (g_icon_equal (icon, icon2));
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  data = g_variant_new_string ("/path/to/somewhere.png");
+  icon = g_icon_deserialize (g_variant_ref_sink (data));
+  g_variant_unref (data);
+  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);
+
+  data = g_variant_new_string ("/path/to/somewhere with whitespace.png");
+  icon = g_icon_deserialize (g_variant_ref_sink (data));
+  g_variant_unref (data);
+  data = g_icon_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, G_DIR_SEPARATOR_S "path" G_DIR_SEPARATOR_S "to" 
G_DIR_SEPARATOR_S "somewhere with whitespace.png");
+  g_variant_unref (data);
+  location = g_file_new_for_commandline_arg ("/path/to/somewhere with whitespace.png");
+  icon2 = g_file_icon_new (location);
+  g_assert (g_icon_equal (icon, icon2));
+  g_object_unref (location);
+  g_object_unref (icon2);
+  location = g_file_new_for_commandline_arg ("/path/to/somewhere%20with%20whitespace.png");
+  icon2 = g_file_icon_new (location);
+  g_assert (!g_icon_equal (icon, icon2));
+  g_object_unref (location);
+  g_object_unref (icon2);
+  g_object_unref (icon);
+
+  data = g_variant_new_string ("sftp:///path/to/somewhere.png";);
+  icon = g_icon_deserialize (g_variant_ref_sink (data));
+  g_variant_unref (data);
+  data = g_icon_serialize (icon);
+  g_assert_cmpstr (g_variant_get_string (data, NULL), ==, "sftp:///path/to/somewhere.png";);
+  g_variant_unref (data);
+  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_serialize (icon);
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  icon = g_themed_icon_new ("icon name with whitespace");
+  g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
+  data = g_icon_serialize (icon);
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (data);
+  g_object_unref (icon);
+  g_object_unref (icon2);
+
+  icon = g_themed_icon_new_with_default_fallbacks ("network-server-xyz");
+  g_themed_icon_append_name (G_THEMED_ICON (icon), "computer");
+  data = g_icon_serialize (icon);
+  icon2 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon, icon2));
+  g_variant_unref (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_serialize (icon4);
+  icon5 = g_icon_deserialize (data);
+  g_assert (g_icon_equal (icon4, icon5));
+
+  g_object_get (emblem1, "origin", &origin, "icon", &i, NULL);
+  g_assert (origin == G_EMBLEM_ORIGIN_DEVICE);
+  g_assert (i == icon2);
+  g_object_unref (i);
+
+  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);
+  g_variant_unref (data);
+}
+
+static void
 test_themed_icon (void)
 {
   GIcon *icon1, *icon2, *icon3;
@@ -373,6 +552,7 @@ main (int   argc,
 {
   g_test_init (&argc, &argv, NULL);
 
+  g_test_add_func ("/icons/to-string", test_g_icon_to_string);
   g_test_add_func ("/icons/serialize", test_g_icon_serialize);
   g_test_add_func ("/icons/themed", test_themed_icon);
   g_test_add_func ("/icons/emblemed", test_emblemed_icon);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]