Blame gio/gemblem.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
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
Packit ae235b
#include <config.h>
Packit ae235b
Packit ae235b
#include "gicon.h"
Packit ae235b
#include "gemblem.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "gioenums.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gemblem
Packit ae235b
 * @short_description: An object for emblems
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GIcon, #GEmblemedIcon, #GLoadableIcon, #GThemedIcon
Packit ae235b
 *
Packit ae235b
 * #GEmblem is an implementation of #GIcon that supports
Packit ae235b
 * having an emblem, which is an icon with additional properties.
Packit ae235b
 * It can than be added to a #GEmblemedIcon.
Packit ae235b
 *
Packit ae235b
 * Currently, only metainformation about the emblem's origin is
Packit ae235b
 * supported. More may be added in the future.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static void g_emblem_iface_init (GIconIface *iface);
Packit ae235b
Packit ae235b
struct _GEmblem
Packit ae235b
{
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  GIcon *icon;
Packit ae235b
  GEmblemOrigin origin;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GEmblemClass
Packit ae235b
{
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0_GEMBLEM,
Packit ae235b
  PROP_ICON,
Packit ae235b
  PROP_ORIGIN
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
Packit ae235b
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_get_property (GObject    *object,
Packit ae235b
                       guint       prop_id,
Packit ae235b
                       GValue     *value,
Packit ae235b
                       GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
      case PROP_ICON:
Packit ae235b
        g_value_set_object (value, emblem->icon);
Packit ae235b
	break;
Packit ae235b
Packit ae235b
      case PROP_ORIGIN:
Packit ae235b
        g_value_set_enum (value, emblem->origin);
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      default:
Packit ae235b
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
        break;
Packit ae235b
  }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_set_property (GObject      *object,
Packit ae235b
                       guint         prop_id,
Packit ae235b
                       const GValue *value,
Packit ae235b
                       GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
      case PROP_ICON:
Packit ae235b
        emblem->icon = g_value_dup_object (value);
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      case PROP_ORIGIN:
Packit ae235b
        emblem->origin = g_value_get_enum (value);
Packit ae235b
        break;
Packit ae235b
Packit ae235b
      default:
Packit ae235b
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (object);
Packit ae235b
Packit ae235b
  if (emblem->icon)
Packit ae235b
    g_object_unref (emblem->icon);
Packit ae235b
Packit ae235b
  (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_class_init (GEmblemClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_emblem_finalize;
Packit ae235b
  gobject_class->set_property = g_emblem_set_property;
Packit ae235b
  gobject_class->get_property = g_emblem_get_property;
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_ORIGIN,
Packit ae235b
                                   g_param_spec_enum ("origin",
Packit ae235b
                                                      P_("GEmblem’s origin"),
Packit ae235b
                                                      P_("Tells which origin the emblem is derived from"),
Packit ae235b
                                                      G_TYPE_EMBLEM_ORIGIN,
Packit ae235b
                                                      G_EMBLEM_ORIGIN_UNKNOWN,
Packit ae235b
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (gobject_class,
Packit ae235b
                                   PROP_ICON,
Packit ae235b
                                   g_param_spec_object ("icon",
Packit ae235b
                                                      P_("The icon of the emblem"),
Packit ae235b
                                                      P_("The actual icon of the emblem"),
Packit ae235b
                                                      G_TYPE_OBJECT,
Packit ae235b
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_init (GEmblem *emblem)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_emblem_new:
Packit ae235b
 * @icon: a GIcon containing the icon.
Packit ae235b
 *
Packit ae235b
 * Creates a new emblem for @icon.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GEmblem.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
GEmblem *
Packit ae235b
g_emblem_new (GIcon *icon)
Packit ae235b
{
Packit ae235b
  GEmblem* emblem;
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
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
Packit ae235b
Packit ae235b
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
Packit ae235b
  emblem->icon = g_object_ref (icon);
Packit ae235b
  emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;
Packit ae235b
Packit ae235b
  return emblem;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_emblem_new_with_origin:
Packit ae235b
 * @icon: a GIcon containing the icon.
Packit ae235b
 * @origin: a GEmblemOrigin enum defining the emblem's origin
Packit ae235b
 *
Packit ae235b
 * Creates a new emblem for @icon.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GEmblem.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
GEmblem *
Packit ae235b
g_emblem_new_with_origin (GIcon         *icon,
Packit ae235b
                          GEmblemOrigin  origin)
Packit ae235b
{
Packit ae235b
  GEmblem* emblem;
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
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
Packit ae235b
Packit ae235b
  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
Packit ae235b
  emblem->icon = g_object_ref (icon);
Packit ae235b
  emblem->origin = origin;
Packit ae235b
Packit ae235b
  return emblem;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_emblem_get_icon:
Packit ae235b
 * @emblem: a #GEmblem from which the icon should be extracted.
Packit ae235b
 *
Packit ae235b
 * Gives back the icon from @emblem.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GIcon. The returned object belongs to
Packit ae235b
 *          the emblem and should not be modified or freed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
GIcon *
Packit ae235b
g_emblem_get_icon (GEmblem *emblem)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);
Packit ae235b
Packit ae235b
  return emblem->icon;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_emblem_get_origin:
Packit ae235b
 * @emblem: a #GEmblem
Packit ae235b
 *
Packit ae235b
 * Gets the origin of the emblem.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the origin of the emblem
Packit ae235b
 *
Packit ae235b
 * Since: 2.18
Packit ae235b
 */
Packit ae235b
GEmblemOrigin
Packit ae235b
g_emblem_get_origin (GEmblem *emblem)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);
Packit ae235b
Packit ae235b
  return emblem->origin;
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint
Packit ae235b
g_emblem_hash (GIcon *icon)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (icon);
Packit ae235b
  guint hash;
Packit ae235b
Packit ae235b
  hash  = g_icon_hash (g_emblem_get_icon (emblem));
Packit ae235b
  hash ^= emblem->origin;
Packit ae235b
Packit ae235b
  return hash;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_emblem_equal (GIcon *icon1,
Packit ae235b
                GIcon *icon2)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem1 = G_EMBLEM (icon1);
Packit ae235b
  GEmblem *emblem2 = G_EMBLEM (icon2);
Packit ae235b
Packit ae235b
  return emblem1->origin == emblem2->origin &&
Packit ae235b
         g_icon_equal (emblem1->icon, emblem2->icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_emblem_to_tokens (GIcon *icon,
Packit ae235b
		    GPtrArray *tokens,
Packit ae235b
		    gint  *out_version)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (icon);
Packit ae235b
  char *s;
Packit ae235b
Packit ae235b
  /* GEmblem are encoded as
Packit ae235b
   *
Packit ae235b
   * <origin> <icon>
Packit ae235b
   */
Packit ae235b
Packit ae235b
  g_return_val_if_fail (out_version != NULL, FALSE);
Packit ae235b
Packit ae235b
  *out_version = 0;
Packit ae235b
Packit ae235b
  s = g_icon_to_string (emblem->icon);
Packit ae235b
  if (s == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  g_ptr_array_add (tokens, s);
Packit ae235b
Packit ae235b
  s = g_strdup_printf ("%d", emblem->origin);
Packit ae235b
  g_ptr_array_add (tokens, s);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_emblem_from_tokens (gchar  **tokens,
Packit ae235b
		      gint     num_tokens,
Packit ae235b
		      gint     version,
Packit ae235b
		      GError **error)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem;
Packit ae235b
  GIcon *icon;
Packit ae235b
  GEmblemOrigin origin;
Packit ae235b
Packit ae235b
  emblem = NULL;
Packit ae235b
Packit ae235b
  if (version != 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Can’t handle version %d of GEmblem encoding"),
Packit ae235b
                   version);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (num_tokens != 2)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Malformed number of tokens (%d) in GEmblem encoding"),
Packit ae235b
                   num_tokens);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  icon = g_icon_new_for_string (tokens[0], error);
Packit ae235b
Packit ae235b
  if (icon == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  origin = atoi (tokens[1]);
Packit ae235b
Packit ae235b
  emblem = g_emblem_new_with_origin (icon, origin);
Packit ae235b
  g_object_unref (icon);
Packit ae235b
Packit ae235b
  return G_ICON (emblem);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
g_emblem_serialize (GIcon *icon)
Packit ae235b
{
Packit ae235b
  GEmblem *emblem = G_EMBLEM (icon);
Packit ae235b
  GVariant *icon_data;
Packit ae235b
  GEnumValue *origin;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  icon_data = g_icon_serialize (emblem->icon);
Packit ae235b
  if (!icon_data)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
Packit ae235b
  result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
Packit ae235b
                                 icon_data, origin ? origin->value_nick : "unknown");
Packit ae235b
  g_variant_unref (icon_data);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_emblem_iface_init (GIconIface *iface)
Packit ae235b
{
Packit ae235b
  iface->hash  = g_emblem_hash;
Packit ae235b
  iface->equal = g_emblem_equal;
Packit ae235b
  iface->to_tokens = g_emblem_to_tokens;
Packit ae235b
  iface->from_tokens = g_emblem_from_tokens;
Packit ae235b
  iface->serialize = g_emblem_serialize;
Packit ae235b
}