gtkicontheme.c 169 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GtkIconTheme - a loader for icon themes
 * gtk-icon-theme.c Copyright (C) 2002, 2003 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18 19
#include "config.h"

20 21
#include <sys/types.h>
#include <sys/stat.h>
22
#ifdef HAVE_UNISTD_H
23
#include <unistd.h>
24
#endif
25 26
#include <string.h>
#include <stdlib.h>
27
#include <math.h>
28
#include <glib.h>
29
#include <glib/gstdio.h>
30

31 32 33 34
#ifdef G_OS_WIN32
#ifndef S_ISDIR
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
#endif
35 36 37
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include "win32/gdkwin32.h"
38 39
#endif /* G_OS_WIN32 */

40
#include "gtkiconthemeprivate.h"
41 42
#include "gtkcsspalettevalueprivate.h"
#include "gtkcssrgbavalueprivate.h"
43
#include "gtkdebug.h"
44
#include "deprecated/gtkiconfactory.h"
45
#include "gtkiconcache.h"
46
#include "gtkintl.h"
47
#include "gtkmain.h"
48
#include "deprecated/gtknumerableiconprivate.h"
49
#include "gtksettingsprivate.h"
50
#include "gtkstylecontextprivate.h"
51
#include "gtkprivate.h"
52
#include "gdkpixbufutilsprivate.h"
53 54 55 56 57 58

#undef GDK_DEPRECATED
#undef GDK_DEPRECATED_FOR
#define GDK_DEPRECATED
#define GDK_DEPRECATED_FOR(f)

59
#include "deprecated/gtkstyle.h"
60

61 62 63 64
/* this is in case round() is not provided by the compiler, 
 * such as in the case of C89 compilers, like MSVC
 */
#include "fallback-c89.c"
65 66 67 68 69 70 71 72 73

/**
 * SECTION:gtkicontheme
 * @Short_description: Looking up icons by name
 * @Title: GtkIconTheme
 *
 * #GtkIconTheme provides a facility for looking up icons by name
 * and size. The main reason for using a name rather than simply
 * providing a filename is to allow different icons to be used
74
 * depending on what “icon theme” is selected
75
 * by the user. The operation of icon themes on Linux and Unix
76
 * follows the [Icon Theme Specification](http://www.freedesktop.org/Standards/icon-theme-spec)
77 78 79
 * There is a fallback icon theme, named `hicolor`, where applications
 * should install their icons, but additional icon themes can be installed
 * as operating system vendors and users choose.
80
 *
81 82
 * Named icons are similar to the deprecated [Stock Items][gtkstock],
 * and the distinction between the two may be a bit confusing.
83
 * A few things to keep in mind:
84 85
 * 
 * - Stock images usually are used in conjunction with
86
 *   [Stock Items][gtkstock], such as %GTK_STOCK_OK or
87 88 89 90 91 92 93 94 95 96 97 98 99 100
 *   %GTK_STOCK_OPEN. Named icons are easier to set up and therefore
 *   are more useful for new icons that an application wants to
 *   add, such as application icons or window icons.
 * 
 * - Stock images can only be loaded at the symbolic sizes defined
 *   by the #GtkIconSize enumeration, or by custom sizes defined
 *   by gtk_icon_size_register(), while named icons are more flexible
 *   and any pixel size can be specified.
 * 
 * - Because stock images are closely tied to stock items, and thus
 *   to actions in the user interface, stock images may come in
 *   multiple variants for different widget states or writing
 *   directions.
 *
101 102 103 104 105 106
 * A good rule of thumb is that if there is a stock image for what
 * you want to use, use it, otherwise use a named icon. It turns
 * out that internally stock images are generally defined in
 * terms of one or more named icons. (An example of the
 * more than one case is icons that depend on writing direction;
 * %GTK_STOCK_GO_FORWARD uses the two themed icons
107
 * “gtk-stock-go-forward-ltr” and “gtk-stock-go-forward-rtl”.)
108 109 110 111 112
 *
 * In many cases, named themes are used indirectly, via #GtkImage
 * or stock items, rather than directly, but looking up icons
 * directly is also simple. The #GtkIconTheme object acts
 * as a database of all the icons in the current theme. You
113
 * can create new #GtkIconTheme objects, but it’s much more
114 115
 * efficient to use the standard icon theme for the #GdkScreen
 * so that the icon information is shared with other people
116
 * looking up icons.
117
 * |[<!-- language="C" -->
118 119 120 121 122 123
 * GError *error = NULL;
 * GtkIconTheme *icon_theme;
 * GdkPixbuf *pixbuf;
 *
 * icon_theme = gtk_icon_theme_get_default ();
 * pixbuf = gtk_icon_theme_load_icon (icon_theme,
124 125 126
 *                                    "my-icon-name", // icon name
 *                                    48, // icon size
 *                                    0,  // flags
127 128 129
 *                                    &error);
 * if (!pixbuf)
 *   {
130
 *     g_warning ("Couldn’t load icon: %s", error->message);
131 132 133 134
 *     g_error_free (error);
 *   }
 * else
 *   {
135
 *     // Use the pixbuf
136 137
 *     g_object_unref (pixbuf);
 *   }
138
 * ]|
139 140
 */

141
#define FALLBACK_ICON_THEME "hicolor"
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

typedef enum
{
  ICON_THEME_DIR_FIXED,  
  ICON_THEME_DIR_SCALABLE,  
  ICON_THEME_DIR_THRESHOLD,
  ICON_THEME_DIR_UNTHEMED
} IconThemeDirType;

/* In reverse search order: */
typedef enum
{
  ICON_SUFFIX_NONE = 0,
  ICON_SUFFIX_XPM = 1 << 0,
  ICON_SUFFIX_SVG = 1 << 1,
157
  ICON_SUFFIX_PNG = 1 << 2,
158 159
  HAS_ICON_FILE = 1 << 3,
  ICON_SUFFIX_SYMBOLIC_PNG = 1 << 4
160 161
} IconSuffix;

Alexander Larsson's avatar
Alexander Larsson committed
162 163 164 165 166 167
#define INFO_CACHE_LRU_SIZE 32
#if 0
#define DEBUG_CACHE(args) g_print args
#else
#define DEBUG_CACHE(args)
#endif
168 169 170

struct _GtkIconThemePrivate
{
Alexander Larsson's avatar
Alexander Larsson committed
171 172 173
  GHashTable *info_cache;
  GList *info_cache_lru;

174 175 176
  gchar *current_theme;
  gchar **search_path;
  gint search_path_len;
177
  GList *resource_paths;
178

179
  guint custom_theme        : 1;
180 181
  guint is_screen_singleton : 1;
  guint pixbuf_supports_svg : 1;
182
  guint themes_valid        : 1;
183
  guint loading_themes      : 1;
184 185 186 187 188 189

  /* A list of all the themes needed to look up icons.
   * In search order, without duplicates
   */
  GList *themes;
  GHashTable *unthemed_icons;
190

191
  /* GdkScreen for the icon theme (may be NULL) */
192
  GdkScreen *screen;
193

194
  /* time when we last stat:ed for theme changes */
195
  glong last_stat_time;
196
  GList *dir_mtimes;
197

198
  gulong theme_changed_idle;
199 200
};

Alexander Larsson's avatar
Alexander Larsson committed
201 202 203
typedef struct {
  gchar **icon_names;
  gint size;
204
  gint scale;
Alexander Larsson's avatar
Alexander Larsson committed
205 206 207
  GtkIconLookupFlags flags;
} IconInfoKey;

208 209 210 211 212 213 214 215 216 217 218 219
typedef struct _SymbolicPixbufCache SymbolicPixbufCache;

struct _SymbolicPixbufCache {
  GdkPixbuf *pixbuf;
  GdkPixbuf *proxy_pixbuf;
  GdkRGBA  fg;
  GdkRGBA  success_color;
  GdkRGBA  warning_color;
  GdkRGBA  error_color;
  SymbolicPixbufCache *next;
};

220 221 222 223 224
struct _GtkIconInfoClass
{
  GObjectClass parent_class;
};

225 226
struct _GtkIconInfo
{
227 228
  GObject parent_instance;

229 230
  /* Information about the source
   */
Alexander Larsson's avatar
Alexander Larsson committed
231 232 233
  IconInfoKey key;
  GtkIconTheme *in_cache;

234
  gchar *filename;
235
  GFile *icon_file;
236
  GLoadableIcon *loadable;
237
  GSList *emblem_infos;
238

Anders Carlsson's avatar
Anders Carlsson committed
239 240 241
  /* Cache pixbuf (if there is any) */
  GdkPixbuf *cache_pixbuf;

242 243 244 245 246
  /* Information about the directory where
   * the source was found
   */
  IconThemeDirType dir_type;
  gint dir_size;
247
  gint dir_scale;
248 249
  gint min_size;
  gint max_size;
250 251 252 253

  /* Parameters influencing the scaled icon
   */
  gint desired_size;
254
  gint desired_scale;
255
  guint forced_size     : 1;
256
  guint emblems_applied : 1;
257
  guint is_svg          : 1;
258
  guint is_resource     : 1;
259

260 261 262 263
  /* Cached information if we go ahead and try to load
   * the icon.
   */
  GdkPixbuf *pixbuf;
Alexander Larsson's avatar
Alexander Larsson committed
264
  GdkPixbuf *proxy_pixbuf;
265
  GError *load_error;
266
  gdouble unscaled_scale;
267
  gdouble scale;
268

269
  SymbolicPixbufCache *symbolic_pixbuf_cache;
270

271 272
  gint symbolic_width;
  gint symbolic_height;
273 274 275 276
};

typedef struct
{
Matthias Clasen's avatar
Matthias Clasen committed
277 278 279 280
  gchar *name;
  gchar *display_name;
  gchar *comment;
  gchar *example;
281 282 283 284 285 286 287 288 289 290

  /* In search order */
  GList *dirs;
} IconTheme;

typedef struct
{
  IconThemeDirType type;
  GQuark context;

Matthias Clasen's avatar
Matthias Clasen committed
291 292 293 294 295
  gint size;
  gint min_size;
  gint max_size;
  gint threshold;
  gint scale;
296
  gboolean is_resource;
297

Matthias Clasen's avatar
Matthias Clasen committed
298 299 300
  gchar *dir;
  gchar *subdir;
  gint subdir_index;
301 302
  
  GtkIconCache *cache;
303 304 305 306 307 308
  
  GHashTable *icons;
} IconThemeDir;

typedef struct
{
Matthias Clasen's avatar
Matthias Clasen committed
309 310
  gchar *svg_filename;
  gchar *no_svg_filename;
311
  gboolean is_resource;
312 313 314 315 316 317 318 319 320 321
} UnthemedIcon;

typedef struct
{
  gint size;
  GdkPixbuf *pixbuf;
} BuiltinIcon;

typedef struct 
{
Matthias Clasen's avatar
Matthias Clasen committed
322
  gchar *dir;
323
  time_t mtime;
324
  GtkIconCache *cache;
325
  gboolean exists;
326 327
} IconThemeDirMtime;

Matthias Clasen's avatar
Matthias Clasen committed
328 329 330 331 332 333 334 335 336 337 338 339
static void         gtk_icon_theme_finalize   (GObject          *object);
static void         theme_dir_destroy         (IconThemeDir     *dir);
static void         theme_destroy              (IconTheme       *theme);
static GtkIconInfo *theme_lookup_icon         (IconTheme        *theme,
                                               const gchar      *icon_name,
                                               gint              size,
                                               gint              scale,
                                               gboolean          allow_svg,
                                               gboolean          use_default_icons);
static void         theme_list_icons          (IconTheme        *theme,
                                               GHashTable       *icons,
                                               GQuark            context);
340 341
static gboolean     theme_has_icon            (IconTheme        *theme,
                                               const gchar      *icon_name);
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 362 363 364
static void         theme_list_contexts       (IconTheme        *theme,
                                               GHashTable       *contexts);
static void         theme_subdir_load         (GtkIconTheme     *icon_theme,
                                               IconTheme        *theme,
                                               GKeyFile         *theme_file,
                                               gchar            *subdir);
static void         do_theme_change           (GtkIconTheme     *icon_theme);
static void         blow_themes               (GtkIconTheme     *icon_themes);
static gboolean     rescan_themes             (GtkIconTheme     *icon_themes);
static IconSuffix   theme_dir_get_icon_suffix (IconThemeDir     *dir,
                                               const gchar      *icon_name,
                                               gboolean         *has_icon_file);
static GtkIconInfo *icon_info_new             (IconThemeDirType  type,
                                               gint              dir_size,
                                               gint              dir_scale);
static GtkIconInfo *icon_info_new_builtin     (BuiltinIcon      *icon);
static IconSuffix   suffix_from_name          (const gchar      *name);
static BuiltinIcon *find_builtin_icon         (const gchar      *icon_name,
                                               gint              size,
                                               gint              scale,
                                               gint             *min_difference_p);
static void         remove_from_lru_cache     (GtkIconTheme     *icon_theme,
                                               GtkIconInfo      *icon_info);
365
static gboolean     icon_info_ensure_scale_and_pixbuf (GtkIconInfo* icon_info);
366 367 368 369 370

static guint signal_changed = 0;

static GHashTable *icon_theme_builtin_icons;

Alexander Larsson's avatar
Alexander Larsson committed
371 372 373 374 375 376 377 378 379 380
static guint
icon_info_key_hash (gconstpointer _key)
{
  const IconInfoKey *key = _key;
  guint h = 0;
  int i;
  for (i = 0; key->icon_names[i] != NULL; i++)
    h ^= g_str_hash (key->icon_names[i]);

  h ^= key->size * 0x10001;
381 382
  h ^= key->scale * 0x1000010;
  h ^= key->flags * 0x100000100;
Alexander Larsson's avatar
Alexander Larsson committed
383 384 385 386 387

  return h;
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
388 389
icon_info_key_equal (gconstpointer _a,
                     gconstpointer _b)
Alexander Larsson's avatar
Alexander Larsson committed
390 391 392 393 394 395 396 397
{
  const IconInfoKey *a = _a;
  const IconInfoKey *b = _b;
  int i;

  if (a->size != b->size)
    return FALSE;

398 399 400
  if (a->scale != b->scale)
    return FALSE;

Alexander Larsson's avatar
Alexander Larsson committed
401 402 403 404 405 406 407 408
  if (a->flags != b->flags)
    return FALSE;

  for (i = 0;
       a->icon_names[i] != NULL &&
       b->icon_names[i] != NULL; i++)
    {
      if (strcmp (a->icon_names[i], b->icon_names[i]) != 0)
Matthias Clasen's avatar
Matthias Clasen committed
409
        return FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
410 411 412 413 414
    }

  return a->icon_names[i] == NULL && b->icon_names[i] == NULL;
}

415
G_DEFINE_TYPE_WITH_PRIVATE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
416 417 418 419 420 421

/**
 * gtk_icon_theme_new:
 * 
 * Creates a new icon theme object. Icon theme objects are used
 * to lookup up an icon by name in a particular icon theme.
422
 * Usually, you’ll want to use gtk_icon_theme_get_default()
423 424 425
 * or gtk_icon_theme_get_for_screen() rather than creating
 * a new icon theme object for scratch.
 * 
426
 * Returns: the newly created #GtkIconTheme object.
Matthias Clasen's avatar
Matthias Clasen committed
427 428
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
429
 */
430 431 432 433 434 435 436 437 438 439 440
GtkIconTheme *
gtk_icon_theme_new (void)
{
  return g_object_new (GTK_TYPE_ICON_THEME, NULL);
}

/**
 * gtk_icon_theme_get_default:
 * 
 * Gets the icon theme for the default screen. See
 * gtk_icon_theme_get_for_screen().
441
 *
442
 * Returns: (transfer none): A unique #GtkIconTheme associated with
443 444 445
 *     the default screen. This icon theme is associated with
 *     the screen and can be used as long as the screen
 *     is open. Do not ref or unref it.
Matthias Clasen's avatar
Matthias Clasen committed
446 447
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
448
 */
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
GtkIconTheme *
gtk_icon_theme_get_default (void)
{
  return gtk_icon_theme_get_for_screen (gdk_screen_get_default ());
}

/**
 * gtk_icon_theme_get_for_screen:
 * @screen: a #GdkScreen
 * 
 * Gets the icon theme object associated with @screen; if this
 * function has not previously been called for the given
 * screen, a new icon theme object will be created and
 * associated with the screen. Icon theme objects are
 * fairly expensive to create, so using this function
 * is usually a better choice than calling than gtk_icon_theme_new()
 * and setting the screen yourself; by using this function
 * a single icon theme object will be shared between users.
467
 *
468
 * Returns: (transfer none): A unique #GtkIconTheme associated with
469 470
 *  the given screen. This icon theme is associated with
 *  the screen and can be used as long as the screen
471
 *  is open. Do not ref or unref it.
Matthias Clasen's avatar
Matthias Clasen committed
472 473
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
474
 */
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
GtkIconTheme *
gtk_icon_theme_get_for_screen (GdkScreen *screen)
{
  GtkIconTheme *icon_theme;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  icon_theme = g_object_get_data (G_OBJECT (screen), "gtk-icon-theme");
  if (!icon_theme)
    {
      GtkIconThemePrivate *priv;

      icon_theme = gtk_icon_theme_new ();
      gtk_icon_theme_set_screen (icon_theme, screen);

      priv = icon_theme->priv;
      priv->is_screen_singleton = TRUE;

493
      g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), icon_theme);
494 495 496 497 498 499 500 501 502 503 504 505
    }

  return icon_theme;
}

static void
gtk_icon_theme_class_init (GtkIconThemeClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gtk_icon_theme_finalize;

Matthias Clasen's avatar
Matthias Clasen committed
506 507 508 509 510 511 512 513
  /**
   * GtkIconTheme::changed:
   * @icon_theme: the icon theme
   *
   * Emitted when the current icon theme is switched or GTK+ detects
   * that a change has occurred in the contents of the current
   * icon theme.
   */
514
  signal_changed = g_signal_new (I_("changed"),
Matthias Clasen's avatar
Matthias Clasen committed
515 516 517 518 519 520
                                 G_TYPE_FROM_CLASS (klass),
                                 G_SIGNAL_RUN_LAST,
                                 G_STRUCT_OFFSET (GtkIconThemeClass, changed),
                                 NULL, NULL,
                                 g_cclosure_marshal_VOID__VOID,
                                 G_TYPE_NONE, 0);
521 522 523
}


Matthias Clasen's avatar
Matthias Clasen committed
524 525
/* Callback when the display that the icon theme is attached
 * to is closed; unset the screen, and if it’s the unique theme
526 527 528 529
 * for the screen, drop the reference
 */
static void
display_closed (GdkDisplay   *display,
Matthias Clasen's avatar
Matthias Clasen committed
530 531
                gboolean      is_error,
                GtkIconTheme *icon_theme)
532 533 534 535 536 537 538
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  GdkScreen *screen = priv->screen;
  gboolean was_screen_singleton = priv->is_screen_singleton;

  if (was_screen_singleton)
    {
539
      g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
540 541 542 543 544 545 546 547 548 549 550 551 552 553
      priv->is_screen_singleton = FALSE;
    }

  gtk_icon_theme_set_screen (icon_theme, NULL);

  if (was_screen_singleton)
    {
      g_object_unref (icon_theme);
    }
}

static void
update_current_theme (GtkIconTheme *icon_theme)
{
554 555 556
#define theme_changed(_old, _new) \
  ((_old && !_new) || (!_old && _new) || \
   (_old && _new && strcmp (_old, _new) != 0))
557 558 559 560 561
  GtkIconThemePrivate *priv = icon_theme->priv;

  if (!priv->custom_theme)
    {
      gchar *theme = NULL;
562
      gboolean changed = FALSE;
563 564

      if (priv->screen)
Matthias Clasen's avatar
Matthias Clasen committed
565 566 567 568
        {
          GtkSettings *settings = gtk_settings_get_for_screen (priv->screen);
          g_object_get (settings, "gtk-icon-theme-name", &theme, NULL);
        }
569

570
      if (theme_changed (priv->current_theme, theme))
Matthias Clasen's avatar
Matthias Clasen committed
571 572 573 574 575
        {
          g_free (priv->current_theme);
          priv->current_theme = theme;
          changed = TRUE;
        }
576
      else
Matthias Clasen's avatar
Matthias Clasen committed
577
        g_free (theme);
578 579

      if (changed)
Matthias Clasen's avatar
Matthias Clasen committed
580
        do_theme_change (icon_theme);
581
    }
582
#undef theme_changed
583 584 585 586 587 588
}

/* Callback when the icon theme GtkSetting changes
 */
static void
theme_changed (GtkSettings  *settings,
Matthias Clasen's avatar
Matthias Clasen committed
589 590
               GParamSpec   *pspec,
               GtkIconTheme *icon_theme)
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
{
  update_current_theme (icon_theme);
}

static void
unset_screen (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  GtkSettings *settings;
  GdkDisplay *display;
  
  if (priv->screen)
    {
      settings = gtk_settings_get_for_screen (priv->screen);
      display = gdk_screen_get_display (priv->screen);
      
      g_signal_handlers_disconnect_by_func (display,
Matthias Clasen's avatar
Matthias Clasen committed
608 609
                                            (gpointer) display_closed,
                                            icon_theme);
610
      g_signal_handlers_disconnect_by_func (settings,
Matthias Clasen's avatar
Matthias Clasen committed
611 612
                                            (gpointer) theme_changed,
                                            icon_theme);
613 614 615 616 617 618 619 620 621 622 623

      priv->screen = NULL;
    }
}

/**
 * gtk_icon_theme_set_screen:
 * @icon_theme: a #GtkIconTheme
 * @screen: a #GdkScreen
 * 
 * Sets the screen for an icon theme; the screen is used
624
 * to track the user’s currently configured icon theme,
625
 * which might be different for different screens.
Matthias Clasen's avatar
Matthias Clasen committed
626 627
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
628
 */
629 630
void
gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
631
                           GdkScreen    *screen)
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
  GtkIconThemePrivate *priv;
  GtkSettings *settings;
  GdkDisplay *display;

  g_return_if_fail (GTK_ICON_THEME (icon_theme));
  g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));

  priv = icon_theme->priv;

  unset_screen (icon_theme);
  
  if (screen)
    {
      display = gdk_screen_get_display (screen);
      settings = gtk_settings_get_for_screen (screen);
      
      priv->screen = screen;
      
      g_signal_connect (display, "closed",
Matthias Clasen's avatar
Matthias Clasen committed
652
                        G_CALLBACK (display_closed), icon_theme);
653
      g_signal_connect (settings, "notify::gtk-icon-theme-name",
Matthias Clasen's avatar
Matthias Clasen committed
654
                        G_CALLBACK (theme_changed), icon_theme);
655 656 657 658 659 660 661 662 663
    }

  update_current_theme (icon_theme);
}

/* Checks whether a loader for SVG files has been registered
 * with GdkPixbuf.
 */
static gboolean
664
pixbuf_supports_svg (void)
665
{
666
  GSList *formats;
667
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
668
  static gint found_svg = -1;
669

Matthias Clasen's avatar
Matthias Clasen committed
670
  if (found_svg != -1)
671
    return found_svg;
672 673 674

  formats = gdk_pixbuf_get_formats ();

Matthias Clasen's avatar
Matthias Clasen committed
675
  found_svg = FALSE; 
676 677 678 679 680 681
  for (tmp_list = formats; tmp_list && !found_svg; tmp_list = tmp_list->next)
    {
      gchar **mime_types = gdk_pixbuf_format_get_mime_types (tmp_list->data);
      gchar **mime_type;
      
      for (mime_type = mime_types; *mime_type && !found_svg; mime_type++)
Matthias Clasen's avatar
Matthias Clasen committed
682 683 684 685
        {
          if (strcmp (*mime_type, "image/svg") == 0)
            found_svg = TRUE;
        }
686

687
      g_strfreev (mime_types);
688 689 690
    }

  g_slist_free (formats);
691
  
692 693 694
  return found_svg;
}

Alexander Larsson's avatar
Alexander Larsson committed
695 696 697 698 699 700 701
/* The icon info was removed from the icon_info_hash hash table */
static void
icon_info_uncached (GtkIconInfo *icon_info)
{
  GtkIconTheme *icon_theme = icon_info->in_cache;

  DEBUG_CACHE (("removing %p (%s %d 0x%x) from cache (icon_them: %p)  (cache size %d)\n",
Matthias Clasen's avatar
Matthias Clasen committed
702 703 704 705 706
                icon_info,
                g_strjoinv (",", icon_info->key.icon_names),
                icon_info->key.size, icon_info->key.flags,
                icon_theme,
                icon_theme != NULL ? g_hash_table_size (icon_theme->priv->info_cache) : 0));
Alexander Larsson's avatar
Alexander Larsson committed
707 708 709 710 711 712 713

  icon_info->in_cache = NULL;

  if (icon_theme != NULL)
    remove_from_lru_cache (icon_theme, icon_info);
}

714 715 716 717
static void
gtk_icon_theme_init (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
718
  const gchar * const *xdg_data_dirs;
719
  int i, j;
720

721
  priv = gtk_icon_theme_get_instance_private (icon_theme);
722 723
  icon_theme->priv = priv;

Alexander Larsson's avatar
Alexander Larsson committed
724
  priv->info_cache = g_hash_table_new_full (icon_info_key_hash, icon_info_key_equal, NULL,
Matthias Clasen's avatar
Matthias Clasen committed
725
                                            (GDestroyNotify)icon_info_uncached);
Alexander Larsson's avatar
Alexander Larsson committed
726

727
  priv->custom_theme = FALSE;
728 729 730 731

  xdg_data_dirs = g_get_system_data_dirs ();
  for (i = 0; xdg_data_dirs[i]; i++) ;

732
  priv->search_path_len = 2 * i + 2;
733 734 735 736 737
  
  priv->search_path = g_new (char *, priv->search_path_len);
  
  i = 0;
  priv->search_path[i++] = g_build_filename (g_get_user_data_dir (), "icons", NULL);
738
  priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
739
  
740 741
  for (j = 0; xdg_data_dirs[j]; j++) 
    priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
742

743 744
  for (j = 0; xdg_data_dirs[j]; j++) 
    priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "pixmaps", NULL);
745

746
  priv->resource_paths = g_list_append (NULL, g_strdup ("/org/gtk/libgtk/icons/"));
747

748 749 750
  priv->themes_valid = FALSE;
  priv->themes = NULL;
  priv->unthemed_icons = NULL;
751
  
752 753 754 755 756 757
  priv->pixbuf_supports_svg = pixbuf_supports_svg ();
}

static void
free_dir_mtime (IconThemeDirMtime *dir_mtime)
{
758 759 760
  if (dir_mtime->cache)
    _gtk_icon_cache_unref (dir_mtime->cache);

761
  g_free (dir_mtime->dir);
Matthias Clasen's avatar
Matthias Clasen committed
762
  g_slice_free (IconThemeDirMtime, dir_mtime);
763 764
}

765
static gboolean
766
theme_changed_idle (gpointer user_data)
767 768 769 770 771 772 773
{
  GtkIconTheme *icon_theme;
  GtkIconThemePrivate *priv;

  icon_theme = GTK_ICON_THEME (user_data);
  priv = icon_theme->priv;

774 775
  g_signal_emit (icon_theme, signal_changed, 0);

776
  if (priv->screen && priv->is_screen_singleton)
777
    gtk_style_context_reset_widgets (priv->screen);
778

779
  priv->theme_changed_idle = 0;
780 781 782 783

  return FALSE;
}

784 785 786 787 788 789
static void
queue_theme_changed (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;

  if (!priv->theme_changed_idle)
790 791 792 793 794 795
    {
      priv->theme_changed_idle =
        gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2,
                                   theme_changed_idle, icon_theme, NULL);
      g_source_set_name_by_id (priv->theme_changed_idle, "[gtk+] theme_changed_idle");
    }
796 797
}

798 799 800 801
static void
do_theme_change (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
802

Alexander Larsson's avatar
Alexander Larsson committed
803 804
  g_hash_table_remove_all (priv->info_cache);

805 806
  if (!priv->themes_valid)
    return;
807

808 809
  GTK_NOTE (ICONTHEME,
            g_message ("change to icon theme \"%s\"", priv->current_theme));
810
  blow_themes (icon_theme);
811

812 813
  queue_theme_changed (icon_theme);

814 815 816 817 818 819 820 821 822
}

static void
blow_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  
  if (priv->themes_valid)
    {
823 824
      g_list_free_full (priv->themes, (GDestroyNotify) theme_destroy);
      g_list_free_full (priv->dir_mtimes, (GDestroyNotify) free_dir_mtime);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
      g_hash_table_destroy (priv->unthemed_icons);
    }
  priv->themes = NULL;
  priv->unthemed_icons = NULL;
  priv->dir_mtimes = NULL;
  priv->themes_valid = FALSE;
}

static void
gtk_icon_theme_finalize (GObject *object)
{
  GtkIconTheme *icon_theme;
  GtkIconThemePrivate *priv;
  int i;

  icon_theme = GTK_ICON_THEME (object);
  priv = icon_theme->priv;

Alexander Larsson's avatar
Alexander Larsson committed
843 844 845
  g_hash_table_destroy (priv->info_cache);
  g_assert (priv->info_cache_lru == NULL);

846
  if (priv->theme_changed_idle)
847
    g_source_remove (priv->theme_changed_idle);
848

849 850 851 852
  unset_screen (icon_theme);

  g_free (priv->current_theme);

853
  for (i = 0; i < priv->search_path_len; i++)
854 855
    g_free (priv->search_path[i]);
  g_free (priv->search_path);
856 857

  g_list_free_full (priv->resource_paths, g_free);
858 859

  blow_themes (icon_theme);
860

Matthias Clasen's avatar
Matthias Clasen committed
861
  G_OBJECT_CLASS (gtk_icon_theme_parent_class)->finalize (object);  
862 863 864 865 866
}

/**
 * gtk_icon_theme_set_search_path:
 * @icon_theme: a #GtkIconTheme
867 868
 * @path: (array length=n_elements) (element-type filename): array of
 *     directories that are searched for icon themes
869 870 871 872 873
 * @n_elements: number of elements in @path.
 * 
 * Sets the search path for the icon theme object. When looking
 * for an icon theme, GTK+ will search for a subdirectory of
 * one or more of the directories in @path with the same name
874 875 876
 * as the icon theme containing an index.theme file. (Themes from
 * multiple of the path elements are combined to allow themes to be
 * extended by adding icons in the user’s home directory.)
877
 *
878
 * In addition if an icon found isn’t found either in the current
879 880 881 882
 * icon theme or the default icon theme, and an image file with
 * the right name is found directly in one of the elements of
 * @path, then that image will be used for the icon name.
 * (This is legacy feature, and new icons should be put
883
 * into the fallback icon theme, which is called hicolor,
884
 * rather than directly on the icon path.)
Matthias Clasen's avatar
Matthias Clasen committed
885 886
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
887
 */
888 889
void
gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
890 891
                                const gchar  *path[],
                                gint          n_elements)
892 893 894 895 896 897 898 899 900 901 902 903 904 905
{
  GtkIconThemePrivate *priv;
  gint i;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;
  for (i = 0; i < priv->search_path_len; i++)
    g_free (priv->search_path[i]);

  g_free (priv->search_path);

  priv->search_path = g_new (gchar *, n_elements);
  priv->search_path_len = n_elements;
906

907 908 909 910 911 912 913 914 915
  for (i = 0; i < priv->search_path_len; i++)
    priv->search_path[i] = g_strdup (path[i]);

  do_theme_change (icon_theme);
}

/**
 * gtk_icon_theme_get_search_path:
 * @icon_theme: a #GtkIconTheme
916
 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
917 918 919 920
 *     location to store a list of icon theme path directories or %NULL.
 *     The stored value should be freed with g_strfreev().
 * @n_elements: location to store number of elements in @path, or %NULL
 *
921
 * Gets the current search path. See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
922 923
 *
 * Since: 2.4
924
 */
925
void
926 927 928
gtk_icon_theme_get_search_path (GtkIconTheme  *icon_theme,
                                gchar        **path[],
                                gint          *n_elements)
929 930
{
  GtkIconThemePrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
931
  gint i;
932 933 934 935 936 937 938 939 940 941 942 943

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;

  if (n_elements)
    *n_elements = priv->search_path_len;
  
  if (path)
    {
      *path = g_new (gchar *, priv->search_path_len + 1);
      for (i = 0; i < priv->search_path_len; i++)
Matthias Clasen's avatar
Matthias Clasen committed
944
        (*path)[i] = g_strdup (priv->search_path[i]);
945 946 947 948 949 950 951
      (*path)[i] = NULL;
    }
}

/**
 * gtk_icon_theme_append_search_path:
 * @icon_theme: a #GtkIconTheme
952
 * @path: (type filename): directory name to append to the icon path
953
 * 
954 955
 * Appends a directory to the search path. 
 * See gtk_icon_theme_set_search_path(). 
Matthias Clasen's avatar
Matthias Clasen committed
956 957
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
958
 */
959 960
void
gtk_icon_theme_append_search_path (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
961
                                   const gchar  *path)
962 963 964 965 966 967 968 969 970
{
  GtkIconThemePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
  g_return_if_fail (path != NULL);

  priv = icon_theme->priv;
  
  priv->search_path_len++;
971

972 973 974 975 976 977 978 979 980
  priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);
  priv->search_path[priv->search_path_len-1] = g_strdup (path);

  do_theme_change (icon_theme);
}

/**
 * gtk_icon_theme_prepend_search_path:
 * @icon_theme: a #GtkIconTheme
981
 * @path: (type filename): directory name to prepend to the icon path
982
 * 
983 984
 * Prepends a directory to the search path. 
 * See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
985 986
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
987
 */
988 989
void
gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
990
                                    const gchar  *path)
991 992
{
  GtkIconThemePrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
993
  gint i;
994 995 996 997 998 999 1000 1001 1002

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
  g_return_if_fail (path != NULL);

  priv = icon_theme->priv;
  
  priv->search_path_len++;
  priv->search_path = g_renew (gchar *, priv->search_path, priv->search_path_len);

1003 1004
  for (i = priv->search_path_len - 1; i > 0; i--)
    priv->search_path[i] = priv->search_path[i - 1];
1005 1006 1007 1008 1009 1010
  
  priv->search_path[0] = g_strdup (path);

  do_theme_change (icon_theme);
}

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
/**
 * gtk_icon_theme_add_resource_path:
 * @icon_theme: a #GtkIconTheme
 * @path: a resource path
 *
 * Adds a resource path that will be looked at when looking
 * for icons, similar to search paths.
 *
 * This function should be used to make application-specific icons
 * available as part of the icon theme.
 *
 * The resources are considered as part of the hicolor icon theme
 * and must be located in subdirectories that are defined in the
 * hicolor icon theme, such as `@path/16x16/actions/run.png`.
 * Icons that are directly placed in the resource path instead
 * of a subdirectory are also considered as ultimate fallback.
 *
 * Since: 3.14
 */
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
void
gtk_icon_theme_add_resource_path (GtkIconTheme *icon_theme,
                                  const gchar  *path)
{
  GtkIconThemePrivate *priv = icon_theme->priv;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));
  g_return_if_fail (path != NULL);

  priv->resource_paths = g_list_append (priv->resource_paths, g_strdup (path));

  do_theme_change (icon_theme);
}

1044 1045 1046
/**
 * gtk_icon_theme_set_custom_theme:
 * @icon_theme: a #GtkIconTheme
1047 1048
 * @theme_name: (allow-none): name of icon theme to use instead of
 *   configured theme, or %NULL to unset a previously set custom theme
1049 1050 1051 1052
 * 
 * Sets the name of the icon theme that the #GtkIconTheme object uses
 * overriding system configuration. This function cannot be called
 * on the icon theme objects returned from gtk_icon_theme_get_default()
1053
 * and gtk_icon_theme_get_for_screen().
Matthias Clasen's avatar
Matthias Clasen committed
1054 1055
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
1056
 */
1057 1058
void
gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
1059
                                 const gchar  *theme_name)
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
{
  GtkIconThemePrivate *priv;

  g_return_if_fail (GTK_IS_ICON_THEME (icon_theme));

  priv = icon_theme->priv;

  g_return_if_fail (!priv->is_screen_singleton);
  
  if (theme_name != NULL)
    {
      priv->custom_theme = TRUE;
1072
      if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
Matthias Clasen's avatar
Matthias Clasen committed
1073 1074 1075
        {
          g_free (priv->current_theme);
          priv->current_theme = g_strdup (theme_name);
1076

Matthias Clasen's avatar
Matthias Clasen committed
1077 1078
          do_theme_change (icon_theme);
        }
1079 1080 1081
    }
  else
    {
1082
      if (priv->custom_theme)
Matthias Clasen's avatar
Matthias Clasen committed
1083 1084 1085 1086
        {
          priv->custom_theme = FALSE;
          update_current_theme (icon_theme);
        }
1087 1088 1089
    }
}

1090 1091 1092 1093
static const gchar builtin_hicolor_index[] =
"[Icon Theme]\n"
"Name=Hicolor\n"
"Hidden=True\n"
1094
"Directories=16x16/actions,16x16/status,22x22/actions,24x24/actions,24x24/status,32x32/actions,32x32/status,48x48/status,64x64/actions\n"
1095 1096 1097
"[16x16/actions]\n"
"Size=16\n"
"Type=Threshold\n"
1098 1099 1100
"[16x16/status]\n"
"Size=16\n"
"Type=Threshold\n"
1101 1102 1103 1104 1105 1106
"[22x22/actions]\n"
"Size=22\n"
"Type=Threshold\n"
"[24x24/actions]\n"
"Size=24\n"
"Type=Threshold\n"
1107 1108 1109
"[24x24/status]\n"
"Size=24\n"
"Type=Threshold\n"
1110 1111
"[32x32/actions]\n"
"Size=32\n"
1112 1113 1114 1115 1116 1117 1118 1119 1120
"Type=Threshold\n"
"[32x32/status]\n"
"Size=32\n"
"Type=Threshold\n"
"[48x48/status]\n"
"Size=48\n"
"Type=Threshold\n"
"[64x64/actions]\n"
"Size=64\n"
1121 1122
"Type=Threshold\n";

1123
static void
Matthias Clasen's avatar
Matthias Clasen committed
1124 1125
insert_theme (GtkIconTheme *icon_theme,
              const gchar  *theme_name)
1126
{
Matthias Clasen's avatar
Matthias Clasen committed
1127
  gint i;
1128
  GList *l;
Matthias Clasen's avatar
Matthias Clasen committed
1129 1130 1131
  gchar **dirs;
  gchar **scaled_dirs;
  gchar **themes;
1132
  GtkIconThemePrivate *priv;
1133
  IconTheme *theme = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1134
  gchar *path;
1135 1136
  GKeyFile *theme_file;
  GError *error = NULL;
1137
  IconThemeDirMtime *dir_mtime;
1138
  GStatBuf stat_buf;
1139 1140
  
  priv = icon_theme->priv;
1141

1142 1143 1144 1145
  for (l = priv->themes; l != NULL; l = l->next)
    {
      theme = l->data;
      if (strcmp (theme->name, theme_name) == 0)
Matthias Clasen's avatar
Matthias Clasen committed
1146
        return;
1147 1148 1149 1150 1151
    }
  
  for (i = 0; i < priv->search_path_len; i++)
    {
      path = g_build_filename (priv->search_path[i],
Matthias Clasen's avatar
Matthias Clasen committed
1152 1153
                               theme_name,
                               NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1154
      dir_mtime = g_slice_new (IconThemeDirMtime);
1155
      dir_mtime->cache = NULL;
1156
      dir_mtime->dir = path;
1157
      if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode)) {
Matthias Clasen's avatar
Matthias Clasen committed
1158
        dir_mtime->mtime = stat_buf.st_mtime;
1159 1160
        dir_mtime->exists = TRUE;
      } else {
Matthias Clasen's avatar
Matthias Clasen committed
1161
        dir_mtime->mtime = 0;
1162 1163
        dir_mtime->exists = FALSE;
      }
1164 1165 1166

      priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
    }
1167
  priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
1168 1169

  theme_file = NULL;
1170
  for (i = 0; i < priv->search_path_len && !theme_file; i++)
1171 1172
    {
      path = g_build_filename (priv->search_path[i],
Matthias Clasen's avatar
Matthias Clasen committed
1173 1174 1175
                               theme_name,
                               "index.theme",
                               NULL);
1176
      if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
Matthias Clasen's avatar
Matthias Clasen committed
1177 1178 1179
        {
          theme_file = g_key_file_new ();
          g_key_file_set_list_separator (theme_file, ',');
1180
          if (!g_key_file_load_from_file (theme_file, path, 0, &error))
Matthias Clasen's avatar
Matthias Clasen committed
1181 1182 1183 1184 1185 1186 1187
            {
              g_key_file_free (theme_file);
              theme_file = NULL;
              g_error_free (error);
              error = NULL;
            }
        }
1188 1189 1190
      g_free (path);
    }

1191
  if (theme_file || strcmp (theme_name, FALLBACK_ICON_THEME) == 0)
1192 1193 1194 1195
    {
      theme = g_new0 (IconTheme, 1);
      theme->name = g_strdup (theme_name);
      priv->themes = g_list_prepend (priv->themes, theme);
1196 1197 1198 1199 1200 1201
      if (!theme_file)
        {
          theme_file = g_key_file_new ();
          g_key_file_set_list_separator (theme_file, ',');
          g_key_file_load_from_data (theme_file, builtin_hicolor_index, -1, 0, NULL);
        }
1202 1203
    }

1204 1205
  if (theme_file == NULL)
    return;
1206

1207 1208 1209
  theme->display_name = 
    g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
  if (!theme->display_name)
1210
    g_warning ("Theme file for %s has no name", theme_name);
1211

1212 1213
  dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
  if (!dirs)
Matthias Clasen's avatar
Matthias Clasen committed
1214
    {
1215
      g_warning ("Theme file for %s has no directories", theme_name);
1216 1217
      priv->themes = g_list_remove (priv->themes, theme);
      g_free (theme->name);
Matthias Clasen's avatar
Matthias Clasen committed
1218 1219 1220 1221 1222
      g_free (theme->display_name);
      g_free (theme);
      g_key_file_free (theme_file);
      return;
    }
1223 1224 1225

  scaled_dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "ScaledDirectories", NULL, NULL);

1226 1227
  theme->comment = 
    g_key_file_get_locale_string (theme_file, 
Matthias Clasen's avatar
Matthias Clasen committed
1228 1229
                                  "Icon Theme", "Comment",
                                  NULL, NULL);
1230 1231
  theme->example = 
    g_key_file_get_string (theme_file, 
Matthias Clasen's avatar
Matthias Clasen committed
1232 1233
                           "Icon Theme", "Example",
                           NULL);
1234 1235

  theme->dirs = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1236 1237 1238
  for (i = 0; dirs[i] != NULL; i++)
    theme_subdir_load (icon_theme, theme, theme_file, dirs[i]);

1239 1240 1241 1242 1243
  if (scaled_dirs)
    {
      for (i = 0; scaled_dirs[i] != NULL; i++)
        theme_subdir_load (icon_theme, theme, theme_file, scaled_dirs[i]);
    }
Matthias Clasen's avatar
Matthias Clasen committed
1244
  g_strfreev (dirs);
1245
  g_strfreev (scaled_dirs);
Matthias Clasen's avatar
Matthias Clasen committed
1246

1247 1248
  theme->dirs = g_list_reverse (theme->dirs);

1249
  themes = g_key_file_get_string_list (theme_file,
Matthias Clasen's avatar
Matthias Clasen committed
1250 1251 1252 1253
                                       "Icon Theme",
                                       "Inherits",
                                       NULL,
                                       NULL);
1254
  if (themes)
1255 1256
    {
      for (i = 0; themes[i] != NULL; i++)
Matthias Clasen's avatar
Matthias Clasen committed
1257
        insert_theme (icon_theme, themes[i]);
1258 1259 1260 1261
      
      g_strfreev (themes);
    }

1262
  g_key_file_free (theme_file);
1263 1264 1265 1266 1267
}

static void
free_unthemed_icon (UnthemedIcon *unthemed_icon)
{
1268 1269
  g_free (unthemed_icon->svg_filename);
  g_free (unthemed_icon->no_svg_filename);
1270
  g_slice_free (UnthemedIcon, unthemed_icon);
1271 1272
}

Matthias Clasen's avatar
Matthias Clasen committed
1273 1274
static gchar *
strip_suffix (const gchar *filename)
1275
{
Matthias Clasen's avatar
Matthias Clasen committed
1276
  const gchar *dot;
1277

1278 1279 1280
  if (g_str_has_suffix (filename, ".symbolic.png"))
    return g_strndup (filename, strlen(filename)-13);

1281 1282 1283 1284 1285 1286 1287 1288
  dot = strrchr (filename, '.');

  if (dot == NULL)
    return g_strdup (filename);

  return g_strndup (filename, dot - filename);
}

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
static void
add_unthemed_icon (GtkIconTheme *icon_theme,
                   const gchar  *dir,
                   const gchar  *file,
                   gboolean      is_resource)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  IconSuffix new_suffix, old_suffix;
  gchar *abs_file;
  gchar *base_name;
  UnthemedIcon *unthemed_icon;

  new_suffix = suffix_from_name (file);

  if (new_suffix == ICON_SUFFIX_NONE)
    return;

  abs_file = g_build_filename (dir, file, NULL);
  base_name = strip_suffix (file);

  unthemed_icon = g_hash_table_lookup (priv->unthemed_icons, base_name);

  if (unthemed_icon)
    {
      if (new_suffix == ICON_SUFFIX_SVG)
        {
          if (unthemed_icon->svg_filename)
            g_free (abs_file);
          else
            unthemed_icon->svg_filename = abs_file;
        }
      else
        {
          if (unthemed_icon->no_svg_filename)
            {
              old_suffix = suffix_from_name (unthemed_icon->no_svg_filename);
              if (new_suffix > old_suffix)
                {
                  g_free (unthemed_icon->no_svg_filename);
                  unthemed_icon->no_svg_filename = abs_file;
                }
              else
                g_free (abs_file);
            }
          else
            unthemed_icon->no_svg_filename = abs_file;
        }

      g_free (base_name);
    }
  else
    {
      unthemed_icon = g_slice_new0 (UnthemedIcon);

      unthemed_icon->is_resource = is_resource;

      if (new_suffix == ICON_SUFFIX_SVG)
        unthemed_icon->svg_filename = abs_file;
      else
        unthemed_icon->no_svg_filename = abs_file;

      /* takes ownership of base_name */
      g_hash_table_replace (priv->unthemed_icons, base_name, unthemed_icon);
    }
}

1355 1356 1357 1358 1359
static void
load_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
  GDir *gdir;
Matthias Clasen's avatar
Matthias Clasen committed
1360 1361 1362
  gint base;
  gchar *dir;
  const gchar *file;
1363
  GTimeVal tv;
1364
  IconThemeDirMtime *dir_mtime;
1365
  GStatBuf stat_buf;
1366
  GList *d;
1367 1368 1369
  
  priv = icon_theme->priv;

1370 1371
  if (priv->current_theme)
    insert_theme (icon_theme, priv->current_theme);
1372

1373 1374 1375 1376
  /* Always look in the Adwaita, gnome and hicolor icon themes.
   * Looking in hicolor is mandated by the spec, looking in Adwaita
   * and gnome is a pragmatic solution to prevent missing icons in
   * GTK+ applications when run under, e.g. KDE.
1377
   */
1378
  insert_theme (icon_theme, DEFAULT_ICON_THEME);
1379
  insert_theme (icon_theme, "gnome");
1380
  insert_theme (icon_theme, FALLBACK_ICON_THEME);
1381
  priv->themes = g_list_reverse (priv->themes);
1382 1383


1384
  priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
Matthias Clasen's avatar
Matthias Clasen committed
1385
                                                g_free, (GDestroyNotify)free_unthemed_icon);
1386 1387 1388 1389

  for (base = 0; base < icon_theme->priv->search_path_len; base++)
    {
      dir = icon_theme->priv->search_path[base];
1390

Matthias Clasen's avatar
Matthias Clasen committed
1391
      dir_mtime = g_slice_new (IconThemeDirMtime);
1392
      priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1393
      
1394 1395
      dir_mtime->dir = g_strdup (dir);
      dir_mtime->mtime = 0;
1396
      dir_mtime->exists = FALSE;
1397 1398 1399
      dir_mtime->cache = NULL;

      if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
Matthias Clasen's avatar
Matthias Clasen committed
1400
        continue;
1401
      dir_mtime->mtime = stat_buf.st_mtime;
1402
      dir_mtime->exists = TRUE;
1403 1404

      dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1405
      if (dir_mtime->cache != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
1406
        continue;
1407

1408 1409
      gdir = g_dir_open (dir, 0, NULL);
      if (gdir == NULL)
Matthias Clasen's avatar
Matthias Clasen committed
1410
        continue;
1411

1412
      while ((file = g_dir_read_name (gdir)))
1413
        add_unthemed_icon (icon_theme, dir, file, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1414

1415 1416
      g_dir_close (gdir);
    }
Matthias Clasen's avatar
Matthias Clasen committed
1417

1418 1419 1420 1421
  for (d = priv->resource_paths; d; d = d->next)
    {
      gchar **children;
      gint i;
Matthias Clasen's avatar
Matthias Clasen committed
1422

1423 1424 1425 1426
      dir = d->data;
      children = g_resources_enumerate_children (dir, 0, NULL);
      if (!children)
        continue;
Matthias Clasen's avatar
Matthias Clasen committed
1427

1428 1429
      for (i = 0; children[i]; i++)
        add_unthemed_icon (icon_theme, dir, children[i], TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
1430

1431
      g_strfreev (children);
1432 1433 1434 1435
    }

  priv->themes_valid = TRUE;
  
Matthias Clasen's avatar
Matthias Clasen committed
1436
  g_get_current_time (&tv);
1437
  priv->last_stat_time = tv.tv_sec;
1438 1439 1440

  GTK_NOTE (ICONTHEME, {
    GList *l;
1441 1442
    GString *s;
    s = g_string_new ("Current icon themes ");
1443 1444 1445
    for (l = icon_theme->priv->themes; l; l = l->next)
      {
        IconTheme *theme = l->data;
1446 1447
        g_string_append (s, theme->name);
        g_string_append_c (s, ' ');
1448
      }
1449 1450
    g_message ("%s", s->str);
    g_string_free (s, TRUE);
1451
  });
1452 1453 1454 1455 1456 1457
}

static void
ensure_valid_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
1458
  GTimeVal tv;
1459
  gboolean was_valid = priv->themes_valid;
1460

1461