gthemedicon.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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: Alexander Larsson <alexl@redhat.com>
 */

23
#include "config.h"
24

25 26
#include <string.h>

27
#include "gthemedicon.h"
28
#include "gicon.h"
29
#include "gioerror.h"
30
#include "glibintl.h"
31

32

33 34
/**
 * SECTION:gthemedicon
Matthias Clasen's avatar
Matthias Clasen committed
35
 * @short_description: Icon theming support
Matthias Clasen's avatar
Matthias Clasen committed
36
 * @include: gio/gio.h
37
 * @see_also: #GIcon, #GLoadableIcon
38
 *
39 40 41 42
 * #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.
43 44 45
 * 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.
46 47
 **/

48
static void g_themed_icon_icon_iface_init (GIconIface *iface);
49 50 51 52 53

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

struct _GThemedIconClass
{
  GObjectClass parent_class;
};

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

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

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:
        g_value_set_boxed (value, icon->names);
        break;

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

93 94 95 96 97 98 99 100 101 102 103 104
      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);
105 106
  gchar **names;
  const gchar *name;
107 108 109 110

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

        if (!name)
          break;

116 117
        if (icon->names)
          g_strfreev (icon->names);
118

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

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

        if (!names)
          break;

130 131
        if (icon->names)
          g_strfreev (icon->names);
132 133

        icon->names = names;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        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)
{
  GThemedIcon *themed = G_THEMED_ICON (object);

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

  if (themed->use_default_fallbacks)
    {
      int i = 0, dashes = 0;
      const char *p;
      char *dashp;
      char *last;
158 159 160
      gboolean is_symbolic;
      char *name;
      char **names;
161

162 163 164 165 166 167 168
      is_symbolic = g_str_has_suffix (themed->names[0], "-symbolic");
      if (is_symbolic)
        name = g_strndup (themed->names[0], strlen (themed->names[0]) - 9);
      else
        name = g_strdup (themed->names[0]);

      p = name;
169 170 171 172 173 174 175
      while (*p)
        {
          if (*p == '-')
            dashes++;
          p++;
        }

176
      last = name;
177 178 179

      g_strfreev (themed->names);

180 181
      names = g_new (char *, dashes + 1 + 1);
      names[i++] = last;
182 183

      while ((dashp = strrchr (last, '-')) != NULL)
184 185 186 187 188 189
        names[i++] = last = g_strndup (last, dashp - last);

      names[i++] = NULL;

      if (is_symbolic)
        {
190
          themed->names = g_new (char *, 2 * dashes + 3);
191
          for (i = 0; names[i] != NULL; i++)
192 193 194 195
            {
              themed->names[i] = g_strconcat (names[i], "-symbolic", NULL);
              themed->names[dashes + 1 + i] = names[i];
            }
196

197 198
          themed->names[dashes + 1 + i] = NULL;
          g_free (names);
199 200 201 202 203
        }
      else
        {
          themed->names = names;
        }
204 205 206
    }
}

207 208 209 210 211 212 213 214
static void
g_themed_icon_finalize (GObject *object)
{
  GThemedIcon *themed;

  themed = G_THEMED_ICON (object);

  g_strfreev (themed->names);
215 216

  G_OBJECT_CLASS (g_themed_icon_parent_class)->finalize (object);
217 218 219 220 221 222 223 224
}

static void
g_themed_icon_class_init (GThemedIconClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
  gobject_class->finalize = g_themed_icon_finalize;
225 226 227 228 229 230 231 232 233
  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.
   */
234
  g_object_class_install_property (gobject_class, PROP_NAME,
235
                                   g_param_spec_string ("name",
236 237
                                                        P_("name"),
                                                        P_("The name of the icon"),
238 239 240 241 242 243 244 245 246 247
                                                        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",
248 249
                                                       P_("names"),
                                                       P_("An array containing the icon names"),
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                                                       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
   * |[
   * {
   *   "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",
274 275
                                                         P_("use default fallbacks"),
                                                         P_("Whether to use default fallbacks found by shortening the name at '-' characters. Ignores names after the first if multiple names are given."),
276
                                                         FALSE,
277
                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
278 279 280 281 282
}

static void
g_themed_icon_init (GThemedIcon *themed)
{
283
  themed->names = NULL;
284 285 286 287
}

/**
 * g_themed_icon_new:
288
 * @iconname: a string containing an icon name.
289
 * 
290 291
 * Creates a new themed icon for @iconname.
 * 
292
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
293 294 295 296 297 298
 **/
GIcon *
g_themed_icon_new (const char *iconname)
{
  g_return_val_if_fail (iconname != NULL, NULL);

299
  return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, NULL));
300 301 302 303
}

/**
 * g_themed_icon_new_from_names:
304
 * @iconnames: (array length=len): an array of strings containing icon names.
305 306
 * @len: the length of the @iconnames array, or -1 if @iconnames is 
 *     %NULL-terminated
307 308
 * 
 * Creates a new themed icon for @iconnames.
309
 * 
310
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon
311 312
 **/
GIcon *
313
g_themed_icon_new_from_names (char **iconnames,
314
                              int    len)
315
{
316
  GIcon *icon;
317 318

  g_return_val_if_fail (iconnames != NULL, NULL);
319 320

  if (len >= 0)
321
    {
322 323 324
      char **names;
      int i;

325
      names = g_new (char *, len + 1);
326

327
      for (i = 0; i < len; i++)
328 329 330 331 332 333 334
        names[i] = iconnames[i];

      names[i] = NULL;

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

      g_free (names);
335
    }
336 337 338 339
  else
    icon = G_ICON (g_object_new (G_TYPE_THEMED_ICON, "names", iconnames, NULL));

  return icon;
340 341
}

Matthias Clasen's avatar
Matthias Clasen committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
/**
 * 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:
 * |[
 * 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");
 * ]|
 *
362
 * Returns: (transfer full) (type GThemedIcon): a new #GThemedIcon.
Matthias Clasen's avatar
Matthias Clasen committed
363
 */
364 365 366 367 368
GIcon *
g_themed_icon_new_with_default_fallbacks (const char *iconname)
{
  g_return_val_if_fail (iconname != NULL, NULL);

369
  return G_ICON (g_object_new (G_TYPE_THEMED_ICON, "name", iconname, "use-default-fallbacks", TRUE, NULL));
370 371 372
}


373 374
/**
 * g_themed_icon_get_names:
375
 * @icon: a #GThemedIcon.
376
 *
377
 * Gets the names of icons from within @icon.
378
 *
379
 * Returns: (transfer none): a list of icon names.
380
 */
381 382 383 384 385 386 387
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;
}

388 389 390 391 392 393
/**
 * 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.
394 395 396 397 398
 *
 * <note><para>
 * Note that doing so invalidates the hash computed by prior calls
 * to g_icon_hash().
 * </para></note>
399 400
 */
void
401 402
g_themed_icon_append_name (GThemedIcon *icon, 
                           const char  *iconname)
403 404 405 406 407 408 409 410 411 412
{
  guint num_names;

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

  num_names = g_strv_length (icon->names);
  icon->names = g_realloc (icon->names, sizeof (char*) * (num_names + 2));
  icon->names[num_names] = g_strdup (iconname);
  icon->names[num_names + 1] = NULL;
413 414

  g_object_notify (G_OBJECT (icon), "names");
415 416
}

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/**
 * 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><para>
 * Note that doing so invalidates the hash computed by prior calls
 * to g_icon_hash().
 * </para></note>
 *
 * 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);

  num_names = g_strv_length (icon->names);
  names = g_new (char*, num_names + 2);
  for (i = 0; icon->names[i]; i++)
    names[i + 1] = icon->names[i];
  names[0] = g_strdup (iconname);
  names[num_names + 1] = NULL;

  g_free (icon->names);
  icon->names = names;

  g_object_notify (G_OBJECT (icon), "names");
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
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,
472
                     GIcon *icon2)
473 474 475 476 477 478 479 480 481 482 483 484 485 486
{
  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;
}

487 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 513 514 515 516

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;

517 518 519 520 521 522 523 524 525 526 527
  icon = NULL;

  if (version != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   _("Can't handle version %d of GThemedIcon encoding"),
                   version);
      goto out;
    }
528 529 530 531 532 533 534 535 536
  
  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);

537
 out:
538 539 540
  return icon;
}

541 542 543 544 545 546 547 548
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));
}

549 550 551 552 553
static void
g_themed_icon_icon_iface_init (GIconIface *iface)
{
  iface->hash = g_themed_icon_hash;
  iface->equal = g_themed_icon_equal;
554 555
  iface->to_tokens = g_themed_icon_to_tokens;
  iface->from_tokens = g_themed_icon_from_tokens;
556
  iface->serialize = g_themed_icon_serialize;
557
}