gthemedicon.c 17 KB
Newer Older
1 2 3 4 5 6 7
/* 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
8
 * version 2.1 of the License, or (at your option) any later version.
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/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22

23 24
#include <string.h>

25
#include "gthemedicon.h"
26
#include "gicon.h"
27
#include "gioerror.h"
28
#include "glibintl.h"
29

30

31 32
/**
 * SECTION:gthemedicon
Matthias Clasen's avatar
Matthias Clasen committed
33
 * @short_description: Icon theming support
Matthias Clasen's avatar
Matthias Clasen committed
34
 * @include: gio/gio.h
35
 * @see_also: #GIcon, #GLoadableIcon
36
 *
37 38 39 40
 * #GThemedIcon is an implementation of #GIcon that supports icon themes.
 * #GThemedIcon contains a list of all of the icons present in an icon
 * theme, so that icons can be looked up quickly. #GThemedIcon does
 * not provide actual pixmaps for icons, just the icon names.
41 42 43
 * Ideally something like gtk_icon_theme_choose_icon() should be used to
 * resolve the list of names so that fallback icons work nicely with
 * themes that inherit other themes.
44 45
 **/

46
static void g_themed_icon_icon_iface_init (GIconIface *iface);
47 48 49 50 51

struct _GThemedIcon
{
  GObject parent_instance;
  
52
  char     **init_names;
53 54
  char     **names;
  gboolean   use_default_fallbacks;
55 56 57 58 59 60 61
};

struct _GThemedIconClass
{
  GObjectClass parent_class;
};

62 63 64 65 66 67 68 69
enum
{
  PROP_0,
  PROP_NAME,
  PROP_NAMES,
  PROP_USE_DEFAULT_FALLBACKS
};

70 71
static void g_themed_icon_update_names (GThemedIcon *themed);

72 73 74
G_DEFINE_TYPE_WITH_CODE (GThemedIcon, g_themed_icon, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
						g_themed_icon_icon_iface_init))
75 76 77 78 79 80 81 82 83 84 85 86

static void
g_themed_icon_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GThemedIcon *icon = G_THEMED_ICON (object);

  switch (prop_id)
    {
      case PROP_NAMES:
87
        g_value_set_boxed (value, icon->init_names);
88 89
        break;

90 91 92 93
      case PROP_USE_DEFAULT_FALLBACKS:
        g_value_set_boolean (value, icon->use_default_fallbacks);
        break;

94 95 96 97 98 99 100 101 102 103 104 105
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
g_themed_icon_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  GThemedIcon *icon = G_THEMED_ICON (object);
106 107
  gchar **names;
  const gchar *name;
108 109 110 111

  switch (prop_id)
    {
      case PROP_NAME:
112 113 114 115 116
        name = g_value_get_string (value);

        if (!name)
          break;

117 118
        if (icon->init_names)
          g_strfreev (icon->init_names);
119

120 121 122
        icon->init_names = g_new (char *, 2);
        icon->init_names[0] = g_strdup (name);
        icon->init_names[1] = NULL;
123 124 125
        break;

      case PROP_NAMES:
126 127 128 129 130
        names = g_value_dup_boxed (value);

        if (!names)
          break;

131 132
        if (icon->init_names)
          g_strfreev (icon->init_names);
133

134
        icon->init_names = names;
135 136 137 138 139 140 141 142 143 144 145 146 147 148
        break;

      case PROP_USE_DEFAULT_FALLBACKS:
        icon->use_default_fallbacks = g_value_get_boolean (value);
        break;

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

static void
g_themed_icon_constructed (GObject *object)
{
149
  g_themed_icon_update_names (G_THEMED_ICON (object));
150 151
}

152 153 154 155 156 157 158
static void
g_themed_icon_finalize (GObject *object)
{
  GThemedIcon *themed;

  themed = G_THEMED_ICON (object);

159
  g_strfreev (themed->init_names);
160
  g_strfreev (themed->names);
161 162

  G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
163 164 165 166 167 168 169 170
}

static void
g_themed_icon_class_init (GThemedIconClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
  gobject_class->finalize = g_themed_icon_finalize;
171 172 173 174 175 176 177 178 179
  gobject_class->constructed = g_themed_icon_constructed;
  gobject_class->set_property = g_themed_icon_set_property;
  gobject_class->get_property = g_themed_icon_get_property;

  /**
   * GThemedIcon:name:
   *
   * The icon name.
   */
180
  g_object_class_install_property (gobject_class, PROP_NAME,
181
                                   g_param_spec_string ("name",
182 183
                                                        P_("name"),
                                                        P_("The name of the icon"),
184 185 186 187 188 189 190 191 192 193
                                                        NULL,
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));

  /**
   * GThemedIcon:names:
   *
   * A %NULL-terminated array of icon names.
   */
  g_object_class_install_property (gobject_class, PROP_NAMES,
                                   g_param_spec_boxed ("names",
194 195
                                                       P_("names"),
                                                       P_("An array containing the icon names"),
196 197 198 199 200 201 202 203 204 205 206 207
                                                       G_TYPE_STRV,
                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));

  /**
   * GThemedIcon:use-default-fallbacks:
   *
   * Whether to use the default fallbacks found by shortening the icon name 
   * at '-' characters. If the "names" array has more than one element, 
   * ignores any past the first.
   *
   * For example, if the icon name was "gnome-dev-cdrom-audio", the array 
   * would become
208
   * |[<!-- language="C" -->
209 210 211 212 213 214 215 216 217 218 219
   * {
   *   "gnome-dev-cdrom-audio",
   *   "gnome-dev-cdrom",
   *   "gnome-dev",
   *   "gnome",
   *   NULL
   * };
   * ]|
   */
  g_object_class_install_property (gobject_class, PROP_USE_DEFAULT_FALLBACKS,
                                   g_param_spec_boolean ("use-default-fallbacks",
220
                                                         P_("use default fallbacks"),
221
                                                         P_("Whether to use default fallbacks found by shortening the name at “-” characters. Ignores names after the first if multiple names are given."),
222
                                                         FALSE,
223
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
224 225 226 227 228
}

static void
g_themed_icon_init (GThemedIcon *themed)
{
229 230 231 232
  themed->init_names = NULL;
  themed->names      = NULL;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 * g_themed_icon_update_names:
 * @themed: a #GThemedIcon.
 *
 * Update the actual icon name list, based on the requested names (from
 * construction, or later added with g_themed_icon_prepend_name() and
 * g_themed_icon_append_name()).
 * The order of the list matters, indicating priority:
 * - The first requested icon is first in priority.
 * - If "use-default-fallbacks" is #TRUE, then it is followed by all its
 *   fallbacks (starting from top to lower context levels).
 * - Then next requested icons, and optionally their fallbacks, follow.
 * - Finally all the style variants (symbolic or regular, opposite to whatever
 *   is the requested style) follow in the same order.
 *
 * An icon is not added twice in the list if it was previously added.
 *
 * For instance, if requested names are:
 * [ "some-icon-symbolic", "some-other-icon" ]
 * and use-default-fallbacks is TRUE, the final name list shall be:
 * [ "some-icon-symbolic", "some-symbolic", "some-other-icon",
 *   "some-other", "some", "some-icon", "some-other-icon-symbolic",
 *   "some-other-symbolic" ]
 *
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
 **/
259 260 261 262 263 264
static void
g_themed_icon_update_names (GThemedIcon *themed)
{
  GList *names    = NULL;
  GList *variants = NULL;
  GList *iter;
265
  guint  i;
266 267 268 269 270 271 272 273 274 275

  g_return_if_fail (themed->init_names != NULL && themed->init_names[0] != NULL);

  for (i = 0; themed->init_names[i]; i++)
    {
      gchar    *name;
      gboolean  is_symbolic;

      is_symbolic = g_str_has_suffix (themed->init_names[i], "-symbolic");
      if (is_symbolic)
276
        name = g_strndup (themed->init_names[i], strlen (themed->init_names[i]) - 9);
277
      else
278 279 280
        name = g_strdup (themed->init_names[i]);

      if (g_list_find_custom (names, name, (GCompareFunc) g_strcmp0))
281
        {
282 283
          g_free (name);
          continue;
284 285
        }

286 287 288 289 290
      if (is_symbolic)
        names = g_list_prepend (names, g_strdup (themed->init_names[i]));
      else
        names = g_list_prepend (names, name);

291 292 293 294 295 296 297 298 299
      if (themed->use_default_fallbacks)
        {
          char *dashp;
          char *last;

          last = name;

          while ((dashp = strrchr (last, '-')) != NULL)
            {
300 301 302
              gchar *tmp = last;
              gchar *fallback;

303 304 305
              last = g_strndup (last, dashp - last);
              if (is_symbolic)
                {
306 307
                  g_free (tmp);
                  fallback = g_strdup_printf ("%s-symbolic", last);
308 309
                }
              else
310 311
                fallback = last;
              if (g_list_find_custom (names, fallback, (GCompareFunc) g_strcmp0))
312
                {
313 314
                  g_free (fallback);
                  break;
315
                }
316
              names = g_list_prepend (names, fallback);
317
            }
318 319
          if (is_symbolic)
            g_free (last);
320
        }
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
      else if (is_symbolic)
        g_free (name);
    }
  for (iter = names; iter; iter = iter->next)
    {
      gchar    *name = (gchar *) iter->data;
      gchar    *variant;
      gboolean  is_symbolic;

      is_symbolic = g_str_has_suffix (name, "-symbolic");
      if (is_symbolic)
        variant = g_strndup (name, strlen (name) - 9);
      else
        variant = g_strdup_printf ("%s-symbolic", name);
      if (g_list_find_custom (names, variant, (GCompareFunc) g_strcmp0) ||
          g_list_find_custom (variants, variant, (GCompareFunc) g_strcmp0))
        {
          g_free (variant);
          continue;
        }

      variants = g_list_prepend (variants, variant);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    }
  names = g_list_reverse (names);

  g_strfreev (themed->names);
  themed->names = g_new (char *, g_list_length (names) + g_list_length (variants) + 1);

  for (iter = names, i = 0; iter; iter = iter->next, i++)
    themed->names[i] = iter->data;
  for (iter = variants; iter; iter = iter->next, i++)
    themed->names[i] = iter->data;
  themed->names[i] = NULL;

  g_list_free (names);
  g_list_free (variants);

  g_object_notify (G_OBJECT (themed), "names");
359 360 361 362
}

/**
 * g_themed_icon_new:
363
 * @iconname: a string containing an icon name.
364
 * 
365 366
 * Creates a new themed icon for @iconname.
 * 
367
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
368 369 370 371 372 373
 **/
GIcon *
g_themed_icon_new (const char *iconname)
{
  g_return_val_if_fail (iconname != NULL, NULL);

374
  return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
375 376 377 378
}

/**
 * g_themed_icon_new_from_names:
379
 * @iconnames: (array length=len): an array of strings containing icon names.
380 381
 * @len: the length of the @iconnames array, or -1 if @iconnames is 
 *     %NULL-terminated
382 383
 * 
 * Creates a new themed icon for @iconnames.
384
 * 
385
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
386 387
 **/
GIcon *
388
g_themed_icon_new_from_names (char **iconnames,
389
                              int    len)
390
{
391
  GIcon *icon;
392 393

  g_return_val_if_fail (iconnames != NULL, NULL);
394 395

  if (len >= 0)
396
    {
397 398 399
      char **names;
      int i;

400
      names = g_new (char *, len + 1);
401

402
      for (i = 0; i < len; i++)
403 404 405 406 407 408 409
        names[i] = iconnames[i];

      names[i] = NULL;

      icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", names, NULL));

      g_free (names);
410
    }
411 412 413 414
  else
    icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));

  return icon;
415 416
}

Matthias Clasen's avatar
Matthias Clasen committed
417 418 419 420 421 422 423 424
/**
 * g_themed_icon_new_with_default_fallbacks:
 * @iconname: a string containing an icon name
 *
 * Creates a new themed icon for @iconname, and all the names
 * that can be created by shortening @iconname at '-' characters.
 * 
 * In the following example, @icon1 and @icon2 are equivalent:
425
 * |[<!-- language="C" -->
Matthias Clasen's avatar
Matthias Clasen committed
426 427 428 429 430 431 432 433 434 435 436
 * const char *names[] = { 
 *   "gnome-dev-cdrom-audio",
 *   "gnome-dev-cdrom",
 *   "gnome-dev",
 *   "gnome"
 * };
 *
 * icon1 = g_themed_icon_new_from_names (names, 4);
 * icon2 = g_themed_icon_new_with_default_fallbacks ("gnome-dev-cdrom-audio");
 * ]|
 *
437
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
Matthias Clasen's avatar
Matthias Clasen committed
438
 */
439 440 441 442 443
GIcon *
g_themed_icon_new_with_default_fallbacks (const char *iconname)
{
  g_return_val_if_fail (iconname != NULL, NULL);

444
  return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
445 446 447
}


448 449
/**
 * g_themed_icon_get_names:
450
 * @icon: a #GThemedIcon.
451
 *
452
 * Gets the names of icons from within @icon.
453
 *
454
 * Returns: (transfer none): a list of icon names.
455
 */
456 457 458 459 460 461 462
const char * const *
g_themed_icon_get_names (GThemedIcon *icon)
{
  g_return_val_if_fail (G_IS_THEMED_ICON (icon), NULL);
  return (const char * const *)icon->names;
}

463 464 465 466 467 468
/**
 * g_themed_icon_append_name:
 * @icon: a #GThemedIcon
 * @iconname: name of icon to append to list of icons from within @icon.
 *
 * Append a name to the list of icons from within @icon.
469 470 471
 *
 * Note that doing so invalidates the hash computed by prior calls
 * to g_icon_hash().
472 473
 */
void
474 475
g_themed_icon_append_name (GThemedIcon *icon, 
                           const char  *iconname)
476 477 478 479 480 481
{
  guint num_names;

  g_return_if_fail (G_IS_THEMED_ICON (icon));
  g_return_if_fail (iconname != NULL);

482 483 484 485
  num_names = g_strv_length (icon->init_names);
  icon->init_names = g_realloc (icon->init_names, sizeof (char*) * (num_names + 2));
  icon->init_names[num_names] = g_strdup (iconname);
  icon->init_names[num_names + 1] = NULL;
486

487
  g_themed_icon_update_names (icon);
488 489
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**
 * g_themed_icon_prepend_name:
 * @icon: a #GThemedIcon
 * @iconname: name of icon to prepend to list of icons from within @icon.
 *
 * Prepend a name to the list of icons from within @icon.
 *
 * Note that doing so invalidates the hash computed by prior calls
 * to g_icon_hash().
 *
 * Since: 2.18
 */
void
g_themed_icon_prepend_name (GThemedIcon *icon, 
                            const char  *iconname)
{
  guint num_names;
  gchar **names;
  gint i;

  g_return_if_fail (G_IS_THEMED_ICON (icon));
  g_return_if_fail (iconname != NULL);

513
  num_names = g_strv_length (icon->init_names);
514
  names = g_new (char*, num_names + 2);
515 516
  for (i = 0; icon->init_names[i]; i++)
    names[i + 1] = icon->init_names[i];
517 518 519
  names[0] = g_strdup (iconname);
  names[num_names + 1] = NULL;

520 521
  g_free (icon->init_names);
  icon->init_names = names;
522

523
  g_themed_icon_update_names (icon);
524 525
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static guint
g_themed_icon_hash (GIcon *icon)
{
  GThemedIcon *themed = G_THEMED_ICON (icon);
  guint hash;
  int i;

  hash = 0;

  for (i = 0; themed->names[i] != NULL; i++)
    hash ^= g_str_hash (themed->names[i]);
  
  return hash;
}

static gboolean
g_themed_icon_equal (GIcon *icon1,
543
                     GIcon *icon2)
544 545 546 547 548 549 550 551 552 553 554 555 556 557
{
  GThemedIcon *themed1 = G_THEMED_ICON (icon1);
  GThemedIcon *themed2 = G_THEMED_ICON (icon2);
  int i;

  for (i = 0; themed1->names[i] != NULL && themed2->names[i] != NULL; i++)
    {
      if (!g_str_equal (themed1->names[i], themed2->names[i]))
	return FALSE;
    }

  return themed1->names[i] == NULL && themed2->names[i] == NULL;
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

static gboolean
g_themed_icon_to_tokens (GIcon *icon,
			 GPtrArray *tokens,
                         gint  *out_version)
{
  GThemedIcon *themed_icon = G_THEMED_ICON (icon);
  int n;

  g_return_val_if_fail (out_version != NULL, FALSE);

  *out_version = 0;

  for (n = 0; themed_icon->names[n] != NULL; n++)
    g_ptr_array_add (tokens,
		     g_strdup (themed_icon->names[n]));
  
  return TRUE;
}

static GIcon *
g_themed_icon_from_tokens (gchar  **tokens,
                           gint     num_tokens,
                           gint     version,
                           GError **error)
{
  GIcon *icon;
  gchar **names;
  int n;

588 589 590 591 592 593 594
  icon = NULL;

  if (version != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
595
                   _("Can’t handle version %d of GThemedIcon encoding"),
596 597 598
                   version);
      goto out;
    }
599 600 601 602 603 604 605 606 607
  
  names = g_new0 (gchar *, num_tokens + 1);
  for (n = 0; n < num_tokens; n++)
    names[n] = tokens[n];
  names[n] = NULL;

  icon = g_themed_icon_new_from_names (names, num_tokens);
  g_free (names);

608
 out:
609 610 611
  return icon;
}

612 613 614 615 616 617 618 619
static GVariant *
g_themed_icon_serialize (GIcon *icon)
{
  GThemedIcon *themed_icon = G_THEMED_ICON (icon);

  return g_variant_new ("(sv)", "themed", g_variant_new ("^as", themed_icon->names));
}

620 621 622 623 624
static void
g_themed_icon_icon_iface_init (GIconIface *iface)
{
  iface->hash = g_themed_icon_hash;
  iface->equal = g_themed_icon_equal;
625 626
  iface->to_tokens = g_themed_icon_to_tokens;
  iface->from_tokens = g_themed_icon_from_tokens;
627
  iface->serialize = g_themed_icon_serialize;
628
}