2008-07-28 17:35:07 +02:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/* 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: Matthias Clasen <mclasen@redhat.com>
|
2008-08-04 19:19:30 +02:00
|
|
|
* Clemens N. Buss <cebuzz@gmail.com>
|
2008-07-28 17:35:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gemblemedicon.h"
|
|
|
|
#include "glibintl.h"
|
2008-10-21 13:51:48 +02:00
|
|
|
#include "gioerror.h"
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gemblemedicon
|
|
|
|
* @short_description: Icon with emblems
|
|
|
|
* @include: gio/gio.h
|
2008-08-04 19:19:30 +02:00
|
|
|
* @see_also: #GIcon, #GLoadableIcon, #GThemedIcon, #GEmblem
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
|
|
|
* #GEmblemedIcon is an implementation of #GIcon that supports
|
2008-08-04 19:19:30 +02:00
|
|
|
* adding an emblem to an icon. Adding multiple emblems to an
|
|
|
|
* icon is ensured via g_emblemed_icon_add_emblem().
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
|
|
|
* Note that #GEmblemedIcon allows no control over the position
|
2008-08-04 19:19:30 +02:00
|
|
|
* of the emblems. See also #GEmblem for more information.
|
2008-07-28 17:35:07 +02:00
|
|
|
**/
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
enum {
|
|
|
|
PROP_GICON = 1,
|
|
|
|
NUM_PROPERTIES
|
|
|
|
};
|
2008-07-28 17:35:07 +02:00
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
struct _GEmblemedIconPrivate {
|
2008-07-28 17:35:07 +02:00
|
|
|
GIcon *icon;
|
2008-08-04 19:19:30 +02:00
|
|
|
GList *emblems;
|
2008-07-28 17:35:07 +02:00
|
|
|
};
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
|
|
|
|
|
|
|
static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
|
2008-08-04 19:19:30 +02:00
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
|
|
|
|
g_emblemed_icon_icon_iface_init))
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_emblemed_icon_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed;
|
|
|
|
|
|
|
|
emblemed = G_EMBLEMED_ICON (object);
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
g_object_unref (emblemed->priv->icon);
|
|
|
|
g_list_foreach (emblemed->priv->emblems, (GFunc) g_object_unref, NULL);
|
|
|
|
g_list_free (emblemed->priv->emblems);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
(*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
|
|
|
|
}
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
static void
|
|
|
|
g_emblemed_icon_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *self = G_EMBLEMED_ICON (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_GICON:
|
|
|
|
self->priv->icon = g_value_dup_object (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_emblemed_icon_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *self = G_EMBLEMED_ICON (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_GICON:
|
|
|
|
g_value_set_object (value, self->priv->icon);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-28 17:35:07 +02:00
|
|
|
static void
|
|
|
|
g_emblemed_icon_class_init (GEmblemedIconClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2010-12-09 18:31:19 +01:00
|
|
|
|
2008-07-28 17:35:07 +02:00
|
|
|
gobject_class->finalize = g_emblemed_icon_finalize;
|
2010-12-09 18:31:19 +01:00
|
|
|
gobject_class->set_property = g_emblemed_icon_set_property;
|
|
|
|
gobject_class->get_property = g_emblemed_icon_get_property;
|
|
|
|
|
|
|
|
properties[PROP_GICON] =
|
|
|
|
g_param_spec_object ("gicon",
|
|
|
|
P_("The base GIcon"),
|
|
|
|
P_("The GIcon to attach emblems to"),
|
|
|
|
G_TYPE_ICON,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GEmblemedIconPrivate));
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_emblemed_icon_init (GEmblemedIcon *emblemed)
|
|
|
|
{
|
2010-12-09 18:31:19 +01:00
|
|
|
emblemed->priv =
|
|
|
|
G_TYPE_INSTANCE_GET_PRIVATE (emblemed, G_TYPE_EMBLEMED_ICON,
|
|
|
|
GEmblemedIconPrivate);
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_emblemed_icon_new:
|
2008-08-04 19:19:30 +02:00
|
|
|
* @icon: a #GIcon
|
2010-11-10 12:03:11 +01:00
|
|
|
* @emblem: (allow-none): a #GEmblem, or %NULL
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2008-08-04 19:19:30 +02:00
|
|
|
* Creates a new emblemed icon for @icon with the emblem @emblem.
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer full): a new #GIcon
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
|
|
|
* Since: 2.18
|
|
|
|
**/
|
|
|
|
GIcon *
|
2008-08-04 19:19:30 +02:00
|
|
|
g_emblemed_icon_new (GIcon *icon,
|
|
|
|
GEmblem *emblem)
|
2008-07-28 17:35:07 +02:00
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed;
|
2008-08-04 19:19:30 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_ICON (icon), NULL);
|
|
|
|
g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON,
|
|
|
|
"gicon", icon,
|
|
|
|
NULL));
|
2010-11-10 12:03:11 +01:00
|
|
|
|
|
|
|
if (emblem != NULL)
|
|
|
|
g_emblemed_icon_add_emblem (emblemed, emblem);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
return G_ICON (emblemed);
|
|
|
|
}
|
|
|
|
|
2008-08-04 19:19:30 +02:00
|
|
|
|
2008-07-28 17:35:07 +02:00
|
|
|
/**
|
|
|
|
* g_emblemed_icon_get_icon:
|
2008-08-04 19:19:30 +02:00
|
|
|
* @emblemed: a #GEmblemedIcon
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2008-08-04 19:19:30 +02:00
|
|
|
* Gets the main icon for @emblemed.
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2010-12-09 18:31:19 +01:00
|
|
|
* Returns: (transfer none): a #GIcon that is owned by @emblemed
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
|
|
|
* Since: 2.18
|
|
|
|
**/
|
|
|
|
GIcon *
|
2008-08-04 19:19:30 +02:00
|
|
|
g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
|
2008-07-28 17:35:07 +02:00
|
|
|
{
|
2008-08-04 19:19:30 +02:00
|
|
|
g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
return emblemed->priv->icon;
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-04 19:19:30 +02:00
|
|
|
* g_emblemed_icon_get_emblems:
|
|
|
|
* @emblemed: a #GEmblemedIcon
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2008-08-04 19:19:30 +02:00
|
|
|
* Gets the list of emblems for the @icon.
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
2010-12-27 16:08:46 +01:00
|
|
|
* Returns: (element-type Gio.Emblem) (transfer none): a #GList of
|
|
|
|
* #GEmblem <!-- -->s that is owned by @emblemed
|
2008-07-28 17:35:07 +02:00
|
|
|
*
|
|
|
|
* Since: 2.18
|
|
|
|
**/
|
2008-08-04 19:19:30 +02:00
|
|
|
|
|
|
|
GList *
|
|
|
|
g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
return emblemed->priv->emblems;
|
2008-08-04 19:19:30 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 12:49:22 +01:00
|
|
|
/**
|
|
|
|
* g_emblemed_icon_clear_emblems:
|
|
|
|
* @emblemed: a #GEmblemedIcon
|
|
|
|
*
|
2010-12-15 12:50:59 +01:00
|
|
|
* Removes all the emblems from @icon.
|
2010-12-15 12:49:22 +01:00
|
|
|
*
|
|
|
|
* Since: 2.28
|
|
|
|
**/
|
2010-12-09 18:16:17 +01:00
|
|
|
void
|
|
|
|
g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
|
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
if (emblemed->priv->emblems == NULL)
|
2010-12-09 18:16:17 +01:00
|
|
|
return;
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
g_list_free_full (emblemed->priv->emblems, g_object_unref);
|
|
|
|
emblemed->priv->emblems = NULL;
|
2010-12-09 18:16:17 +01:00
|
|
|
}
|
|
|
|
|
2010-08-30 16:12:42 +02:00
|
|
|
static gint
|
|
|
|
g_emblem_comp (GEmblem *a,
|
|
|
|
GEmblem *b)
|
|
|
|
{
|
|
|
|
guint hash_a = g_icon_hash (G_ICON (a));
|
|
|
|
guint hash_b = g_icon_hash (G_ICON (b));
|
|
|
|
|
|
|
|
if(hash_a < hash_b)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(hash_a == hash_b)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-04 19:19:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_emblemed_icon_add_emblem:
|
|
|
|
* @emblemed: a #GEmblemedIcon
|
|
|
|
* @emblem: a #GEmblem
|
|
|
|
*
|
|
|
|
* Adds @emblem to the #GList of #GEmblem <!-- -->s.
|
|
|
|
*
|
|
|
|
* Since: 2.18
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
|
|
|
|
GEmblem *emblem)
|
2008-07-28 17:35:07 +02:00
|
|
|
{
|
2008-08-04 19:19:30 +02:00
|
|
|
g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
|
|
|
|
g_return_if_fail (G_IS_EMBLEM (emblem));
|
2008-07-28 17:35:07 +02:00
|
|
|
|
2008-08-04 19:19:30 +02:00
|
|
|
g_object_ref (emblem);
|
2010-12-09 18:31:19 +01:00
|
|
|
emblemed->priv->emblems = g_list_insert_sorted (emblemed->priv->emblems, emblem,
|
|
|
|
(GCompareFunc) g_emblem_comp);
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
|
|
|
g_emblemed_icon_hash (GIcon *icon)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
|
2008-08-04 19:19:30 +02:00
|
|
|
GList *list;
|
2010-12-09 18:31:19 +01:00
|
|
|
guint hash = g_icon_hash (emblemed->priv->icon);
|
2008-07-28 17:35:07 +02:00
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
for (list = emblemed->priv->emblems; list != NULL; list = list->next)
|
2008-08-04 19:19:30 +02:00
|
|
|
hash ^= g_icon_hash (G_ICON (list->data));
|
2008-07-28 17:35:07 +02:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
g_emblemed_icon_equal (GIcon *icon1,
|
|
|
|
GIcon *icon2)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed1 = G_EMBLEMED_ICON (icon1);
|
|
|
|
GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
|
2008-08-04 19:19:30 +02:00
|
|
|
GList *list1, *list2;
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
if (!g_icon_equal (emblemed1->priv->icon, emblemed2->priv->icon))
|
2008-08-04 19:19:30 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
list1 = emblemed1->priv->emblems;
|
|
|
|
list2 = emblemed2->priv->emblems;
|
2008-08-04 19:19:30 +02:00
|
|
|
|
|
|
|
while (list1 && list2)
|
|
|
|
{
|
|
|
|
if (!g_icon_equal (G_ICON (list1->data), G_ICON (list2->data)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
list1 = list1->next;
|
|
|
|
list2 = list2->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list1 == NULL && list2 == NULL;
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 13:51:48 +02:00
|
|
|
static gboolean
|
|
|
|
g_emblemed_icon_to_tokens (GIcon *icon,
|
|
|
|
GPtrArray *tokens,
|
|
|
|
gint *out_version)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed_icon = G_EMBLEMED_ICON (icon);
|
|
|
|
GList *l;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
/* GEmblemedIcons are encoded as
|
|
|
|
*
|
|
|
|
* <encoded_icon> [<encoded_emblem_icon>]*
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_return_val_if_fail (out_version != NULL, FALSE);
|
|
|
|
|
|
|
|
*out_version = 0;
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
s = g_icon_to_string (emblemed_icon->priv->icon);
|
2008-10-21 13:51:48 +02:00
|
|
|
if (s == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_ptr_array_add (tokens, s);
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next)
|
2008-10-21 13:51:48 +02:00
|
|
|
{
|
|
|
|
GIcon *emblem_icon = G_ICON (l->data);
|
|
|
|
|
|
|
|
s = g_icon_to_string (emblem_icon);
|
|
|
|
if (s == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_ptr_array_add (tokens, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GIcon *
|
|
|
|
g_emblemed_icon_from_tokens (gchar **tokens,
|
|
|
|
gint num_tokens,
|
|
|
|
gint version,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GEmblemedIcon *emblemed_icon;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
emblemed_icon = NULL;
|
|
|
|
|
|
|
|
if (version != 0)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Can't handle version %d of GEmblemedIcon encoding"),
|
|
|
|
version);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_tokens < 1)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Malformed number of tokens (%d) in GEmblemedIcon encoding"),
|
|
|
|
num_tokens);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
|
2010-12-09 18:31:19 +01:00
|
|
|
emblemed_icon->priv->icon = g_icon_new_for_string (tokens[0], error);
|
|
|
|
if (emblemed_icon->priv->icon == NULL)
|
2008-10-21 13:51:48 +02:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (n = 1; n < num_tokens; n++)
|
|
|
|
{
|
|
|
|
GIcon *emblem;
|
|
|
|
|
|
|
|
emblem = g_icon_new_for_string (tokens[n], error);
|
|
|
|
if (emblem == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!G_IS_EMBLEM (emblem))
|
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
_("Expected a GEmblem for GEmblemedIcon"));
|
|
|
|
g_object_unref (emblem);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-12-09 18:31:19 +01:00
|
|
|
emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
|
2008-10-21 13:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return G_ICON (emblemed_icon);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (emblemed_icon != NULL)
|
|
|
|
g_object_unref (emblemed_icon);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-28 17:35:07 +02:00
|
|
|
static void
|
|
|
|
g_emblemed_icon_icon_iface_init (GIconIface *iface)
|
|
|
|
{
|
|
|
|
iface->hash = g_emblemed_icon_hash;
|
|
|
|
iface->equal = g_emblemed_icon_equal;
|
2008-10-21 13:51:48 +02:00
|
|
|
iface->to_tokens = g_emblemed_icon_to_tokens;
|
|
|
|
iface->from_tokens = g_emblemed_icon_from_tokens;
|
2008-07-28 17:35:07 +02:00
|
|
|
}
|