Blame gio/gicon.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gicon.h"
Packit ae235b
#include "gthemedicon.h"
Packit ae235b
#include "gfileicon.h"
Packit ae235b
#include "gemblemedicon.h"
Packit ae235b
#include "gbytesicon.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gvfs.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/* There versioning of this is implicit, version 1 would be ".1 " */
Packit ae235b
#define G_ICON_SERIALIZATION_MAGIC0 ". "
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gicon
Packit ae235b
 * @short_description: Interface for icons
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * #GIcon is a very minimal interface for icons. It provides functions
Packit ae235b
 * for checking the equality of two icons, hashing of icons and
Packit ae235b
 * serializing an icon to and from strings.
Packit ae235b
 *
Packit ae235b
 * #GIcon does not provide the actual pixmap for the icon as this is out 
Packit ae235b
 * of GIO's scope, however implementations of #GIcon may contain the name 
Packit ae235b
 * of an icon (see #GThemedIcon), or the path to an icon (see #GLoadableIcon). 
Packit ae235b
 *
Packit ae235b
 * To obtain a hash of a #GIcon, see g_icon_hash().
Packit ae235b
 *
Packit ae235b
 * To check if two #GIcons are equal, see g_icon_equal().
Packit ae235b
 *
Packit ae235b
 * For serializing a #GIcon, use g_icon_serialize() and
Packit ae235b
 * g_icon_deserialize().
Packit ae235b
 *
Packit ae235b
 * If you want to consume #GIcon (for example, in a toolkit) you must
Packit ae235b
 * be prepared to handle at least the three following cases:
Packit ae235b
 * #GLoadableIcon, #GThemedIcon and #GEmblemedIcon.  It may also make
Packit ae235b
 * sense to have fast-paths for other cases (like handling #GdkPixbuf
Packit ae235b
 * directly, for example) but all compliant #GIcon implementations
Packit ae235b
 * outside of GIO must implement #GLoadableIcon.
Packit ae235b
 *
Packit ae235b
 * If your application or library provides one or more #GIcon
Packit ae235b
 * implementations you need to ensure that your new implementation also
Packit ae235b
 * implements #GLoadableIcon.  Additionally, you must provide an
Packit ae235b
 * implementation of g_icon_serialize() that gives a result that is
Packit ae235b
 * understood by g_icon_deserialize(), yielding one of the built-in icon
Packit ae235b
 * types.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
typedef GIconIface GIconInterface;
Packit ae235b
G_DEFINE_INTERFACE(GIcon, g_icon, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_icon_default_init (GIconInterface *iface)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_hash:
Packit ae235b
 * @icon: (not nullable): #gconstpointer to an icon object.
Packit ae235b
 * 
Packit ae235b
 * Gets a hash for an icon.
Packit ae235b
 *
Packit ae235b
 * Virtual: hash
Packit ae235b
 * Returns: a #guint containing a hash for the @icon, suitable for 
Packit ae235b
 * use in a #GHashTable or similar data structure.
Packit ae235b
 **/
Packit ae235b
guint
Packit ae235b
g_icon_hash (gconstpointer icon)
Packit ae235b
{
Packit ae235b
  GIconIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_ICON (icon), 0);
Packit ae235b
Packit ae235b
  iface = G_ICON_GET_IFACE (icon);
Packit ae235b
Packit ae235b
  return (* iface->hash) ((GIcon *)icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_equal:
Packit ae235b
 * @icon1: (nullable): pointer to the first #GIcon.
Packit ae235b
 * @icon2: (nullable): pointer to the second #GIcon.
Packit ae235b
 * 
Packit ae235b
 * Checks if two icons are equal.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE if @icon1 is equal to @icon2. %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_icon_equal (GIcon *icon1,
Packit ae235b
	      GIcon *icon2)
Packit ae235b
{
Packit ae235b
  GIconIface *iface;
Packit ae235b
Packit ae235b
  if (icon1 == NULL && icon2 == NULL)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (icon1 == NULL || icon2 == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  if (G_TYPE_FROM_INSTANCE (icon1) != G_TYPE_FROM_INSTANCE (icon2))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  iface = G_ICON_GET_IFACE (icon1);
Packit ae235b
  
Packit ae235b
  return (* iface->equal) (icon1, icon2);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_icon_to_string_tokenized (GIcon *icon, GString *s)
Packit ae235b
{
Packit ae235b
  GPtrArray *tokens;
Packit ae235b
  gint version;
Packit ae235b
  GIconIface *icon_iface;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (icon != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (G_IS_ICON (icon), FALSE);
Packit ae235b
Packit ae235b
  icon_iface = G_ICON_GET_IFACE (icon);
Packit ae235b
  if (icon_iface->to_tokens == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  tokens = g_ptr_array_new ();
Packit ae235b
  if (!icon_iface->to_tokens (icon, tokens, &version))
Packit ae235b
    {
Packit ae235b
      g_ptr_array_free (tokens, TRUE);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* format: TypeName[.Version] <token_0> .. <token_N-1>
Packit ae235b
     version 0 is implicit and can be omitted
Packit ae235b
     all the tokens are url escaped to ensure they have no spaces in them */
Packit ae235b
  
Packit ae235b
  g_string_append (s, g_type_name_from_instance ((GTypeInstance *)icon));
Packit ae235b
  if (version != 0)
Packit ae235b
    g_string_append_printf (s, ".%d", version);
Packit ae235b
  
Packit ae235b
  for (i = 0; i < tokens->len; i++)
Packit ae235b
    {
Packit ae235b
      char *token;
Packit ae235b
Packit ae235b
      token = g_ptr_array_index (tokens, i);
Packit ae235b
Packit ae235b
      g_string_append_c (s, ' ');
Packit ae235b
      /* We really only need to escape spaces here, so allow lots of otherwise reserved chars */
Packit ae235b
      g_string_append_uri_escaped (s, token,
Packit ae235b
				   G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
Packit ae235b
Packit ae235b
      g_free (token);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_ptr_array_free (tokens, TRUE);
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_to_string:
Packit ae235b
 * @icon: a #GIcon.
Packit ae235b
 *
Packit ae235b
 * Generates a textual representation of @icon that can be used for
Packit ae235b
 * serialization such as when passing @icon to a different process or
Packit ae235b
 * saving it to persistent storage. Use g_icon_new_for_string() to
Packit ae235b
 * get @icon back from the returned string.
Packit ae235b
 *
Packit ae235b
 * The encoding of the returned string is proprietary to #GIcon except
Packit ae235b
 * in the following two cases
Packit ae235b
 *
Packit ae235b
 * - If @icon is a #GFileIcon, the returned string is a native path
Packit ae235b
 *   (such as `/path/to/my icon.png`) without escaping
Packit ae235b
 *   if the #GFile for @icon is a native file.  If the file is not
Packit ae235b
 *   native, the returned string is the result of g_file_get_uri()
Packit ae235b
 *   (such as `sftp://path/to/my%20icon.png`).
Packit ae235b
 * 
Packit ae235b
 * - If @icon is a #GThemedIcon with exactly one name, the encoding is
Packit ae235b
 *    simply the name (such as `network-server`).
Packit ae235b
 *
Packit ae235b
 * Virtual: to_tokens
Packit ae235b
 * Returns: (nullable): An allocated NUL-terminated UTF8 string or
Packit ae235b
 * %NULL if @icon can't be serialized. Use g_free() to free.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_icon_to_string (GIcon *icon)
Packit ae235b
{
Packit ae235b
  gchar *ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (icon != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
Packit ae235b
  if (G_IS_FILE_ICON (icon))
Packit ae235b
    {
Packit ae235b
      GFile *file;
Packit ae235b
Packit ae235b
      file = g_file_icon_get_file (G_FILE_ICON (icon));
Packit ae235b
      if (g_file_is_native (file))
Packit ae235b
	{
Packit ae235b
	  ret = g_file_get_path (file);
Packit ae235b
	  if (!g_utf8_validate (ret, -1, NULL))
Packit ae235b
	    {
Packit ae235b
	      g_free (ret);
Packit ae235b
	      ret = NULL;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
        ret = g_file_get_uri (file);
Packit ae235b
    }
Packit ae235b
  else if (G_IS_THEMED_ICON (icon))
Packit ae235b
    {
Packit ae235b
      const char * const *names;
Packit ae235b
Packit ae235b
      names = g_themed_icon_get_names (G_THEMED_ICON (icon));
Packit ae235b
      if (names != NULL &&
Packit ae235b
	  names[0] != NULL &&
Packit ae235b
	  names[0][0] != '.' && /* Allowing icons starting with dot would break G_ICON_SERIALIZATION_MAGIC0 */
Packit ae235b
	  g_utf8_validate (names[0], -1, NULL) && /* Only return utf8 strings */
Packit ae235b
	  names[1] == NULL)
Packit ae235b
	ret = g_strdup (names[0]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (ret == NULL)
Packit ae235b
    {
Packit ae235b
      GString *s;
Packit ae235b
Packit ae235b
      s = g_string_new (G_ICON_SERIALIZATION_MAGIC0);
Packit ae235b
Packit ae235b
      if (g_icon_to_string_tokenized (icon, s))
Packit ae235b
	ret = g_string_free (s, FALSE);
Packit ae235b
      else
Packit ae235b
	g_string_free (s, TRUE);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_icon_new_from_tokens (char   **tokens,
Packit ae235b
			GError **error)
Packit ae235b
{
Packit ae235b
  GIcon *icon;
Packit ae235b
  char *typename, *version_str;
Packit ae235b
  GType type;
Packit ae235b
  gpointer klass;
Packit ae235b
  GIconIface *icon_iface;
Packit ae235b
  gint version;
Packit ae235b
  char *endp;
Packit ae235b
  int num_tokens;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  icon = NULL;
Packit ae235b
  klass = NULL;
Packit ae235b
Packit ae235b
  num_tokens = g_strv_length (tokens);
Packit ae235b
Packit ae235b
  if (num_tokens < 1)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Wrong number of tokens (%d)"),
Packit ae235b
                   num_tokens);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  typename = tokens[0];
Packit ae235b
  version_str = strchr (typename, '.');
Packit ae235b
  if (version_str)
Packit ae235b
    {
Packit ae235b
      *version_str = 0;
Packit ae235b
      version_str += 1;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  
Packit ae235b
  type = g_type_from_name (tokens[0]);
Packit ae235b
  if (type == 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("No type for class name %s"),
Packit ae235b
                   tokens[0]);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_type_is_a (type, G_TYPE_ICON))
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Type %s does not implement the GIcon interface"),
Packit ae235b
                   tokens[0]);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  klass = g_type_class_ref (type);
Packit ae235b
  if (klass == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Type %s is not classed"),
Packit ae235b
                   tokens[0]);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  version = 0;
Packit ae235b
  if (version_str)
Packit ae235b
    {
Packit ae235b
      version = strtol (version_str, &endp, 10);
Packit ae235b
      if (endp == NULL || *endp != '\0')
Packit ae235b
	{
Packit ae235b
	  g_set_error (error,
Packit ae235b
		       G_IO_ERROR,
Packit ae235b
		       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
		       _("Malformed version number: %s"),
Packit ae235b
		       version_str);
Packit ae235b
	  goto out;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  icon_iface = g_type_interface_peek (klass, G_TYPE_ICON);
Packit ae235b
  g_assert (icon_iface != NULL);
Packit ae235b
Packit ae235b
  if (icon_iface->from_tokens == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Type %s does not implement from_tokens() on the GIcon interface"),
Packit ae235b
                   tokens[0]);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (i = 1;  i < num_tokens; i++)
Packit ae235b
    {
Packit ae235b
      char *escaped;
Packit ae235b
Packit ae235b
      escaped = tokens[i];
Packit ae235b
      tokens[i] = g_uri_unescape_string (escaped, NULL);
Packit ae235b
      g_free (escaped);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  icon = icon_iface->from_tokens (tokens + 1, num_tokens - 1, version, error);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  if (klass != NULL)
Packit ae235b
    g_type_class_unref (klass);
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
ensure_builtin_icon_types (void)
Packit ae235b
{
Packit ae235b
  g_type_ensure (G_TYPE_THEMED_ICON);
Packit ae235b
  g_type_ensure (G_TYPE_FILE_ICON);
Packit ae235b
  g_type_ensure (G_TYPE_EMBLEMED_ICON);
Packit ae235b
  g_type_ensure (G_TYPE_EMBLEM);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* handles the 'simple' cases: GFileIcon and GThemedIcon */
Packit ae235b
static GIcon *
Packit ae235b
g_icon_new_for_string_simple (const gchar *str)
Packit ae235b
{
Packit ae235b
  gchar *scheme;
Packit ae235b
  GIcon *icon;
Packit ae235b
Packit ae235b
  if (str[0] == '.')
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  /* handle special GFileIcon and GThemedIcon cases */
Packit ae235b
  scheme = g_uri_parse_scheme (str);
Packit ae235b
  if (scheme != NULL || str[0] == '/' || str[0] == G_DIR_SEPARATOR)
Packit ae235b
    {
Packit ae235b
      GFile *location;
Packit ae235b
      location = g_file_new_for_commandline_arg (str);
Packit ae235b
      icon = g_file_icon_new (location);
Packit ae235b
      g_object_unref (location);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    icon = g_themed_icon_new (str);
Packit ae235b
Packit ae235b
  g_free (scheme);
Packit ae235b
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_new_for_string:
Packit ae235b
 * @str: A string obtained via g_icon_to_string().
Packit ae235b
 * @error: Return location for error.
Packit ae235b
 *
Packit ae235b
 * Generate a #GIcon instance from @str. This function can fail if
Packit ae235b
 * @str is not valid - see g_icon_to_string() for discussion.
Packit ae235b
 *
Packit ae235b
 * If your application or library provides one or more #GIcon
Packit ae235b
 * implementations you need to ensure that each #GType is registered
Packit ae235b
 * with the type system prior to calling g_icon_new_for_string().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): An object implementing the #GIcon
Packit ae235b
 *          interface or %NULL if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 **/
Packit ae235b
GIcon *
Packit ae235b
g_icon_new_for_string (const gchar   *str,
Packit ae235b
                       GError       **error)
Packit ae235b
{
Packit ae235b
  GIcon *icon = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (str != NULL, NULL);
Packit ae235b
Packit ae235b
  icon = g_icon_new_for_string_simple (str);
Packit ae235b
  if (icon)
Packit ae235b
    return icon;
Packit ae235b
Packit ae235b
  ensure_builtin_icon_types ();
Packit ae235b
Packit ae235b
  if (g_str_has_prefix (str, G_ICON_SERIALIZATION_MAGIC0))
Packit ae235b
    {
Packit ae235b
      gchar **tokens;
Packit ae235b
Packit ae235b
      /* handle tokenized encoding */
Packit ae235b
      tokens = g_strsplit (str + sizeof (G_ICON_SERIALIZATION_MAGIC0) - 1, " ", 0);
Packit ae235b
      icon = g_icon_new_from_tokens (tokens, error);
Packit ae235b
      g_strfreev (tokens);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_set_error_literal (error,
Packit ae235b
                         G_IO_ERROR,
Packit ae235b
                         G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                         _("Can’t handle the supplied version of the icon encoding"));
Packit ae235b
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GEmblem *
Packit ae235b
g_icon_deserialize_emblem (GVariant *value)
Packit ae235b
{
Packit ae235b
  GVariant *emblem_metadata;
Packit ae235b
  GVariant *emblem_data;
Packit ae235b
  const gchar *origin_nick;
Packit ae235b
  GIcon *emblem_icon;
Packit ae235b
  GEmblem *emblem;
Packit ae235b
Packit ae235b
  g_variant_get (value, "(v@a{sv})", &emblem_data, &emblem_metadata);
Packit ae235b
Packit ae235b
  emblem = NULL;
Packit ae235b
Packit ae235b
  emblem_icon = g_icon_deserialize (emblem_data);
Packit ae235b
  if (emblem_icon != NULL)
Packit ae235b
    {
Packit ae235b
      /* Check if we should create it with an origin. */
Packit ae235b
      if (g_variant_lookup (emblem_metadata, "origin", "&s", &origin_nick))
Packit ae235b
        {
Packit ae235b
          GEnumClass *origin_class;
Packit ae235b
          GEnumValue *origin_value;
Packit ae235b
Packit ae235b
          origin_class = g_type_class_ref (G_TYPE_EMBLEM_ORIGIN);
Packit ae235b
          origin_value = g_enum_get_value_by_nick (origin_class, origin_nick);
Packit ae235b
          if (origin_value)
Packit ae235b
            emblem = g_emblem_new_with_origin (emblem_icon, origin_value->value);
Packit ae235b
          g_type_class_unref (origin_class);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* We didn't create it with an origin, so do it without. */
Packit ae235b
      if (emblem == NULL)
Packit ae235b
        emblem = g_emblem_new (emblem_icon);
Packit ae235b
Packit ae235b
      g_object_unref (emblem_icon);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_unref (emblem_metadata);
Packit ae235b
  g_variant_unref (emblem_data);
Packit ae235b
Packit ae235b
  return emblem;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_icon_deserialize_emblemed (GVariant *value)
Packit ae235b
{
Packit ae235b
  GVariantIter *emblems;
Packit ae235b
  GVariant *icon_data;
Packit ae235b
  GIcon *main_icon;
Packit ae235b
  GIcon *icon;
Packit ae235b
Packit ae235b
  g_variant_get (value, "(va(va{sv}))", &icon_data, &emblems);
Packit ae235b
  main_icon = g_icon_deserialize (icon_data);
Packit ae235b
Packit ae235b
  if (main_icon)
Packit ae235b
    {
Packit ae235b
      GVariant *emblem_data;
Packit ae235b
Packit ae235b
      icon = g_emblemed_icon_new (main_icon, NULL);
Packit ae235b
Packit ae235b
      while ((emblem_data = g_variant_iter_next_value (emblems)))
Packit ae235b
        {
Packit ae235b
          GEmblem *emblem;
Packit ae235b
Packit ae235b
          emblem = g_icon_deserialize_emblem (emblem_data);
Packit ae235b
Packit ae235b
          if (emblem)
Packit ae235b
            {
Packit ae235b
              g_emblemed_icon_add_emblem (G_EMBLEMED_ICON (icon), emblem);
Packit ae235b
              g_object_unref (emblem);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_variant_unref (emblem_data);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_object_unref (main_icon);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    icon = NULL;
Packit ae235b
Packit ae235b
  g_variant_iter_free (emblems);
Packit ae235b
  g_variant_unref (icon_data);
Packit ae235b
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_deserialize:
Packit ae235b
 * @value: a #GVariant created with g_icon_serialize()
Packit ae235b
 *
Packit ae235b
 * Deserializes a #GIcon previously serialized using g_icon_serialize().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GIcon, or %NULL when deserialization fails.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
GIcon *
Packit ae235b
g_icon_deserialize (GVariant *value)
Packit ae235b
{
Packit ae235b
  const gchar *tag;
Packit ae235b
  GVariant *val;
Packit ae235b
  GIcon *icon;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (value != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) ||
Packit ae235b
                        g_variant_is_of_type (value, G_VARIANT_TYPE ("(sv)")), NULL);
Packit ae235b
Packit ae235b
  /* Handle some special cases directly so that people can hard-code
Packit ae235b
   * stuff into GMenuModel xml files without resorting to using GVariant
Packit ae235b
   * text format to describe one of the explicitly-tagged possibilities
Packit ae235b
   * below.
Packit ae235b
   */
Packit ae235b
  if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
Packit ae235b
    return g_icon_new_for_string_simple (g_variant_get_string (value, NULL));
Packit ae235b
Packit ae235b
  /* Otherwise, use the tagged union format */
Packit ae235b
  g_variant_get (value, "(&sv)", &tag, &val;;
Packit ae235b
Packit ae235b
  icon = NULL;
Packit ae235b
Packit ae235b
  if (g_str_equal (tag, "file") && g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
Packit ae235b
    {
Packit ae235b
      GFile *file;
Packit ae235b
Packit ae235b
      file = g_file_new_for_commandline_arg (g_variant_get_string (val, NULL));
Packit ae235b
      icon = g_file_icon_new (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (tag, "themed") && g_variant_is_of_type (val, G_VARIANT_TYPE_STRING_ARRAY))
Packit ae235b
    {
Packit ae235b
      const gchar **names;
Packit ae235b
      gsize size;
Packit ae235b
Packit ae235b
      names = g_variant_get_strv (val, &size);
Packit ae235b
      icon = g_themed_icon_new_from_names ((gchar **) names, size);
Packit ae235b
      g_free (names);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (tag, "bytes") && g_variant_is_of_type (val, G_VARIANT_TYPE_BYTESTRING))
Packit ae235b
    {
Packit ae235b
      GBytes *bytes;
Packit ae235b
Packit ae235b
      bytes = g_variant_get_data_as_bytes (val);
Packit ae235b
      icon = g_bytes_icon_new (bytes);
Packit ae235b
      g_bytes_unref (bytes);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (tag, "emblem") && g_variant_is_of_type (val, G_VARIANT_TYPE ("(va{sv})")))
Packit ae235b
    {
Packit ae235b
      GEmblem *emblem;
Packit ae235b
Packit ae235b
      emblem = g_icon_deserialize_emblem (val);
Packit ae235b
      if (emblem)
Packit ae235b
        icon = G_ICON (emblem);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (tag, "emblemed") && g_variant_is_of_type (val, G_VARIANT_TYPE ("(va(va{sv}))")))
Packit ae235b
    {
Packit ae235b
      icon = g_icon_deserialize_emblemed (val);
Packit ae235b
    }
Packit ae235b
  else if (g_str_equal (tag, "gvfs"))
Packit ae235b
    {
Packit ae235b
      GVfsClass *class;
Packit ae235b
      GVfs *vfs;
Packit ae235b
Packit ae235b
      vfs = g_vfs_get_default ();
Packit ae235b
      class = G_VFS_GET_CLASS (vfs);
Packit ae235b
      if (class->deserialize_icon)
Packit ae235b
        icon = (* class->deserialize_icon) (vfs, val);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_variant_unref (val);
Packit ae235b
Packit ae235b
  return icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_icon_serialize:
Packit ae235b
 * @icon: a #GIcon
Packit ae235b
 *
Packit ae235b
 * Serializes a #GIcon into a #GVariant. An equivalent #GIcon can be retrieved
Packit ae235b
 * back by calling g_icon_deserialize() on the returned value.
Packit ae235b
 * As serialization will avoid using raw icon data when possible, it only
Packit ae235b
 * makes sense to transfer the #GVariant between processes on the same machine,
Packit ae235b
 * (as opposed to over the network), and within the same file system namespace.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a #GVariant, or %NULL when serialization fails.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
GVariant *
Packit ae235b
g_icon_serialize (GIcon *icon)
Packit ae235b
{
Packit ae235b
  GIconInterface *iface;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  iface = G_ICON_GET_IFACE (icon);
Packit ae235b
Packit ae235b
  if (!iface->serialize)
Packit ae235b
    {
Packit ae235b
      g_critical ("g_icon_serialize() on icon type '%s' is not implemented", G_OBJECT_TYPE_NAME (icon));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = (* iface->serialize) (icon);
Packit ae235b
Packit ae235b
  if (result)
Packit ae235b
    {
Packit ae235b
      g_variant_take_ref (result);
Packit ae235b
Packit ae235b
      if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(sv)")))
Packit ae235b
        {
Packit ae235b
          g_critical ("g_icon_serialize() on icon type '%s' returned GVariant of type '%s' but it must return "
Packit ae235b
                      "one with type '(sv)'", G_OBJECT_TYPE_NAME (icon), g_variant_get_type_string (result));
Packit ae235b
          g_variant_unref (result);
Packit ae235b
          result = NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}