gemblem.c 8.95 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
Matthias Clasen's avatar
Matthias Clasen committed
3 4 5 6 7
 * Copyright (C) 2008 Clemens N. Buss <cebuzz@gmail.com>
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
Matthias Clasen's avatar
Matthias Clasen committed
9 10 11 12 13 14 15
 *
 * 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
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Matthias Clasen's avatar
Matthias Clasen committed
17 18 19 20
 */

#include <config.h>

21
#include "gicon.h"
Matthias Clasen's avatar
Matthias Clasen committed
22 23 24 25
#include "gemblem.h"
#include "glibintl.h"
#include "gioenums.h"
#include "gioenumtypes.h"
26 27 28
#include "gioerror.h"
#include <stdlib.h>
#include <string.h>
Matthias Clasen's avatar
Matthias Clasen committed
29 30 31 32 33 34 35 36 37 38 39 40


/**
 * SECTION:gemblem
 * @short_description: An object for emblems
 * @include: gio/gio.h
 * @see_also: #GIcon, #GEmblemedIcon, #GLoadableIcon, #GThemedIcon
 *
 * #GEmblem is an implementation of #GIcon that supports
 * having an emblem, which is an icon with additional properties.
 * It can than be added to a #GEmblemedIcon.
 *
41
 * Currently, only metainformation about the emblem's origin is
Matthias Clasen's avatar
Matthias Clasen committed
42
 * supported. More may be added in the future.
43
 */
Matthias Clasen's avatar
Matthias Clasen committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

static void g_emblem_iface_init (GIconIface *iface);

struct _GEmblem
{
  GObject parent_instance;

  GIcon *icon;
  GEmblemOrigin origin;
};

struct _GEmblemClass
{
  GObjectClass parent_class;
};

enum
{
  PROP_0_GEMBLEM,
  PROP_ICON,
  PROP_ORIGIN
};

G_DEFINE_TYPE_WITH_CODE (GEmblem, g_emblem, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, g_emblem_iface_init))

static void
71 72 73
g_emblem_get_property (GObject    *object,
                       guint       prop_id,
                       GValue     *value,
Matthias Clasen's avatar
Matthias Clasen committed
74 75 76
                       GParamSpec *pspec)
{
  GEmblem *emblem = G_EMBLEM (object);
77

Matthias Clasen's avatar
Matthias Clasen committed
78 79 80 81
  switch (prop_id)
    {
      case PROP_ICON:
        g_value_set_object (value, emblem->icon);
82
	break;
Matthias Clasen's avatar
Matthias Clasen committed
83 84 85 86 87 88 89 90

      case PROP_ORIGIN:
        g_value_set_enum (value, emblem->origin);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
91
  }
Matthias Clasen's avatar
Matthias Clasen committed
92 93 94 95 96 97 98 99 100 101 102 103 104
}

static void
g_emblem_set_property (GObject      *object,
                       guint         prop_id,
                       const GValue *value,
                       GParamSpec   *pspec)
{
  GEmblem *emblem = G_EMBLEM (object);

  switch (prop_id)
    {
      case PROP_ICON:
105
        emblem->icon = g_value_dup_object (value);
Matthias Clasen's avatar
Matthias Clasen committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        break;

      case PROP_ORIGIN:
        emblem->origin = g_value_get_enum (value);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
g_emblem_finalize (GObject *object)
{
  GEmblem *emblem = G_EMBLEM (object);

123 124
  if (emblem->icon)
    g_object_unref (emblem->icon);
Matthias Clasen's avatar
Matthias Clasen committed
125 126 127 128 129 130 131 132

  (*G_OBJECT_CLASS (g_emblem_parent_class)->finalize) (object);
}

static void
g_emblem_class_init (GEmblemClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133

Matthias Clasen's avatar
Matthias Clasen committed
134 135 136 137
  gobject_class->finalize = g_emblem_finalize;
  gobject_class->set_property = g_emblem_set_property;
  gobject_class->get_property = g_emblem_get_property;

138
  g_object_class_install_property (gobject_class,
Matthias Clasen's avatar
Matthias Clasen committed
139 140
                                   PROP_ORIGIN,
                                   g_param_spec_enum ("origin",
141
                                                      P_("GEmblem’s origin"),
Matthias Clasen's avatar
Matthias Clasen committed
142 143 144
                                                      P_("Tells which origin the emblem is derived from"),
                                                      G_TYPE_EMBLEM_ORIGIN,
                                                      G_EMBLEM_ORIGIN_UNKNOWN,
145
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Matthias Clasen's avatar
Matthias Clasen committed
146 147 148 149 150 151 152

  g_object_class_install_property (gobject_class,
                                   PROP_ICON,
                                   g_param_spec_object ("icon",
                                                      P_("The icon of the emblem"),
                                                      P_("The actual icon of the emblem"),
                                                      G_TYPE_OBJECT,
153
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Matthias Clasen's avatar
Matthias Clasen committed
154 155 156 157 158 159 160 161 162 163 164

}

static void
g_emblem_init (GEmblem *emblem)
{
}

/**
 * g_emblem_new:
 * @icon: a GIcon containing the icon.
165
 *
Matthias Clasen's avatar
Matthias Clasen committed
166
 * Creates a new emblem for @icon.
167
 *
Matthias Clasen's avatar
Matthias Clasen committed
168 169 170
 * Returns: a new #GEmblem.
 *
 * Since: 2.18
171
 */
Matthias Clasen's avatar
Matthias Clasen committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
GEmblem *
g_emblem_new (GIcon *icon)
{
  GEmblem* emblem;

  g_return_val_if_fail (icon != NULL, NULL);
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);

  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
  emblem->icon = g_object_ref (icon);
  emblem->origin = G_EMBLEM_ORIGIN_UNKNOWN;

  return emblem;
}

/**
 * g_emblem_new_with_origin:
 * @icon: a GIcon containing the icon.
 * @origin: a GEmblemOrigin enum defining the emblem's origin
 *
 * Creates a new emblem for @icon.
194
 *
Matthias Clasen's avatar
Matthias Clasen committed
195 196 197
 * Returns: a new #GEmblem.
 *
 * Since: 2.18
198
 */
Matthias Clasen's avatar
Matthias Clasen committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
GEmblem *
g_emblem_new_with_origin (GIcon         *icon,
                          GEmblemOrigin  origin)
{
  GEmblem* emblem;

  g_return_val_if_fail (icon != NULL, NULL);
  g_return_val_if_fail (G_IS_ICON (icon), NULL);
  g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);

  emblem = g_object_new (G_TYPE_EMBLEM, NULL);
  emblem->icon = g_object_ref (icon);
  emblem->origin = origin;

  return emblem;
}

/**
 * g_emblem_get_icon:
 * @emblem: a #GEmblem from which the icon should be extracted.
219
 *
Matthias Clasen's avatar
Matthias Clasen committed
220
 * Gives back the icon from @emblem.
221
 *
222 223
 * Returns: (transfer none): a #GIcon. The returned object belongs to
 *          the emblem and should not be modified or freed.
Matthias Clasen's avatar
Matthias Clasen committed
224 225
 *
 * Since: 2.18
226
 */
Matthias Clasen's avatar
Matthias Clasen committed
227 228 229 230 231 232 233 234 235 236 237
GIcon *
g_emblem_get_icon (GEmblem *emblem)
{
  g_return_val_if_fail (G_IS_EMBLEM (emblem), NULL);

  return emblem->icon;
}


/**
 * g_emblem_get_origin:
238 239
 * @emblem: a #GEmblem
 *
Matthias Clasen's avatar
Matthias Clasen committed
240
 * Gets the origin of the emblem.
241
 *
242
 * Returns: (transfer none): the origin of the emblem
Matthias Clasen's avatar
Matthias Clasen committed
243 244
 *
 * Since: 2.18
245
 */
Matthias Clasen's avatar
Matthias Clasen committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
GEmblemOrigin
g_emblem_get_origin (GEmblem *emblem)
{
  g_return_val_if_fail (G_IS_EMBLEM (emblem), G_EMBLEM_ORIGIN_UNKNOWN);

  return emblem->origin;
}

static guint
g_emblem_hash (GIcon *icon)
{
  GEmblem *emblem = G_EMBLEM (icon);
  guint hash;

  hash  = g_icon_hash (g_emblem_get_icon (emblem));
261
  hash ^= emblem->origin;
Matthias Clasen's avatar
Matthias Clasen committed
262 263 264 265 266 267 268 269 270 271

  return hash;
}

static gboolean
g_emblem_equal (GIcon *icon1,
                GIcon *icon2)
{
  GEmblem *emblem1 = G_EMBLEM (icon1);
  GEmblem *emblem2 = G_EMBLEM (icon2);
272

Matthias Clasen's avatar
Matthias Clasen committed
273 274 275 276
  return emblem1->origin == emblem2->origin &&
         g_icon_equal (emblem1->icon, emblem2->icon);
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
static gboolean
g_emblem_to_tokens (GIcon *icon,
		    GPtrArray *tokens,
		    gint  *out_version)
{
  GEmblem *emblem = G_EMBLEM (icon);
  char *s;

  /* GEmblem are encoded as
   *
   * <origin> <icon>
   */

  g_return_val_if_fail (out_version != NULL, FALSE);

  *out_version = 0;

  s = g_icon_to_string (emblem->icon);
  if (s == NULL)
    return FALSE;

  g_ptr_array_add (tokens, s);

  s = g_strdup_printf ("%d", emblem->origin);
  g_ptr_array_add (tokens, s);
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  return TRUE;
}

static GIcon *
g_emblem_from_tokens (gchar  **tokens,
		      gint     num_tokens,
		      gint     version,
		      GError **error)
{
  GEmblem *emblem;
  GIcon *icon;
  GEmblemOrigin origin;

  emblem = NULL;

  if (version != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
323
                   _("Can’t handle version %d of GEmblem encoding"),
324 325 326 327 328 329 330 331 332 333 334 335 336
                   version);
      return NULL;
    }

  if (num_tokens != 2)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   _("Malformed number of tokens (%d) in GEmblem encoding"),
                   num_tokens);
      return NULL;
    }
337

338
  icon = g_icon_new_for_string (tokens[0], error);
339

340 341 342 343
  if (icon == NULL)
    return NULL;

  origin = atoi (tokens[1]);
344

345 346
  emblem = g_emblem_new_with_origin (icon, origin);
  g_object_unref (icon);
347

348 349 350
  return G_ICON (emblem);
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static GVariant *
g_emblem_serialize (GIcon *icon)
{
  GEmblem *emblem = G_EMBLEM (icon);
  GVariant *icon_data;
  GEnumValue *origin;
  GVariant *result;

  icon_data = g_icon_serialize (emblem->icon);
  if (!icon_data)
    return NULL;

  origin = g_enum_get_value (g_type_class_peek (G_TYPE_EMBLEM_ORIGIN), emblem->origin);
  result = g_variant_new_parsed ("('emblem', <(%v, {'origin': <%s>})>)",
                                 icon_data, origin ? origin->value_nick : "unknown");
  g_variant_unref (icon_data);

  return result;
}

Matthias Clasen's avatar
Matthias Clasen committed
371 372 373 374 375
static void
g_emblem_iface_init (GIconIface *iface)
{
  iface->hash  = g_emblem_hash;
  iface->equal = g_emblem_equal;
376 377
  iface->to_tokens = g_emblem_to_tokens;
  iface->from_tokens = g_emblem_from_tokens;
378
  iface->serialize = g_emblem_serialize;
Matthias Clasen's avatar
Matthias Clasen committed
379
}