gtkicontheme.c 166 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 "gtkicontheme.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 53 54 55 56 57

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

58
#include "deprecated/gtkstyle.h"
59

60 61 62 63
/* 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"
64 65 66 67 68 69 70 71 72

/**
 * 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
73
 * depending on what “icon theme” is selected
74
 * by the user. The operation of icon themes on Linux and Unix
75
 * follows the [Icon Theme Specification](http://www.freedesktop.org/Standards/icon-theme-spec)
76 77 78
 * 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.
79
 *
80 81
 * Named icons are similar to the deprecated [Stock Items][gtkstock],
 * and the distinction between the two may be a bit confusing.
82
 * A few things to keep in mind:
83 84
 * 
 * - Stock images usually are used in conjunction with
85
 *   [Stock Items][gtkstock], such as %GTK_STOCK_OK or
86 87 88 89 90 91 92 93 94 95 96 97 98 99
 *   %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.
 *
100 101 102 103 104 105
 * 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
106
 * “gtk-stock-go-forward-ltr” and “gtk-stock-go-forward-rtl”.)
107 108 109 110 111
 *
 * 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
112
 * can create new #GtkIconTheme objects, but it’s much more
113 114
 * efficient to use the standard icon theme for the #GdkScreen
 * so that the icon information is shared with other people
115
 * looking up icons.
116
 * |[<!-- language="C" -->
117 118 119 120 121 122
 * GError *error = NULL;
 * GtkIconTheme *icon_theme;
 * GdkPixbuf *pixbuf;
 *
 * icon_theme = gtk_icon_theme_get_default ();
 * pixbuf = gtk_icon_theme_load_icon (icon_theme,
123 124 125
 *                                    "my-icon-name", // icon name
 *                                    48, // icon size
 *                                    0,  // flags
126 127 128
 *                                    &error);
 * if (!pixbuf)
 *   {
129
 *     g_warning ("Couldn’t load icon: %s", error->message);
130 131 132 133
 *     g_error_free (error);
 *   }
 * else
 *   {
134
 *     // Use the pixbuf
135 136
 *     g_object_unref (pixbuf);
 *   }
137
 * ]|
138 139
 */

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

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,
156
  ICON_SUFFIX_PNG = 1 << 2,
157 158
  HAS_ICON_FILE = 1 << 3,
  ICON_SUFFIX_SYMBOLIC_PNG = 1 << 4
159 160
} IconSuffix;

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

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

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

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

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

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

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

197
  gulong theme_changed_idle;
198 199
};

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

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

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

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

224 225
struct _GtkIconInfo
{
226 227
  GObject parent_instance;

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

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

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

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

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

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

268
  SymbolicPixbufCache *symbolic_pixbuf_cache;
269

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

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

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

typedef struct
{
  IconThemeDirType type;
  GQuark context;

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

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

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

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

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

Matthias Clasen's avatar
Matthias Clasen committed
327 328 329 330 331 332 333 334 335 336 337 338
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);
339 340
static gboolean     theme_has_icon            (IconTheme        *theme,
                                               const gchar      *icon_name);
Matthias Clasen's avatar
Matthias Clasen committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
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);
364
static gboolean     icon_info_ensure_scale_and_pixbuf (GtkIconInfo* icon_info);
365 366 367 368 369

static guint signal_changed = 0;

static GHashTable *icon_theme_builtin_icons;

Alexander Larsson's avatar
Alexander Larsson committed
370 371 372 373 374 375 376 377 378 379
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;
380 381
  h ^= key->scale * 0x1000010;
  h ^= key->flags * 0x100000100;
Alexander Larsson's avatar
Alexander Larsson committed
382 383 384 385 386

  return h;
}

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

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

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

Alexander Larsson's avatar
Alexander Larsson committed
400 401 402 403 404 405 406 407
  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
408
        return FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
409 410 411 412 413
    }

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

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

/**
 * 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.
421
 * Usually, you’ll want to use gtk_icon_theme_get_default()
422 423 424
 * or gtk_icon_theme_get_for_screen() rather than creating
 * a new icon theme object for scratch.
 * 
425
 * Returns: the newly created #GtkIconTheme object.
Matthias Clasen's avatar
Matthias Clasen committed
426 427
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
428
 */
429 430 431 432 433 434 435 436 437 438 439
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().
440
 *
441
 * Returns: (transfer none): A unique #GtkIconTheme associated with
442 443 444
 *     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
445 446
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
447
 */
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
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.
466
 *
467
 * Returns: (transfer none): A unique #GtkIconTheme associated with
468 469
 *  the given screen. This icon theme is associated with
 *  the screen and can be used as long as the screen
470
 *  is open. Do not ref or unref it.
Matthias Clasen's avatar
Matthias Clasen committed
471 472
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
473
 */
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
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;

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

  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
505 506 507 508 509 510 511 512
  /**
   * 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.
   */
513
  signal_changed = g_signal_new (I_("changed"),
Matthias Clasen's avatar
Matthias Clasen committed
514 515 516 517 518 519
                                 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);
520 521 522
}


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

  if (was_screen_singleton)
    {
538
      g_object_set_data (G_OBJECT (screen), I_("gtk-icon-theme"), NULL);
539 540 541 542 543 544 545 546 547 548 549 550 551 552
      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)
{
553 554 555
#define theme_changed(_old, _new) \
  ((_old && !_new) || (!_old && _new) || \
   (_old && _new && strcmp (_old, _new) != 0))
556 557 558 559 560
  GtkIconThemePrivate *priv = icon_theme->priv;

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

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

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

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

/* Callback when the icon theme GtkSetting changes
 */
static void
theme_changed (GtkSettings  *settings,
Matthias Clasen's avatar
Matthias Clasen committed
588 589
               GParamSpec   *pspec,
               GtkIconTheme *icon_theme)
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
{
  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
607 608
                                            (gpointer) display_closed,
                                            icon_theme);
609
      g_signal_handlers_disconnect_by_func (settings,
Matthias Clasen's avatar
Matthias Clasen committed
610 611
                                            (gpointer) theme_changed,
                                            icon_theme);
612 613 614 615 616 617 618 619 620 621 622

      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
623
 * to track the user’s currently configured icon theme,
624
 * which might be different for different screens.
Matthias Clasen's avatar
Matthias Clasen committed
625 626
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
627
 */
628 629
void
gtk_icon_theme_set_screen (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
630
                           GdkScreen    *screen)
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
{
  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
651
                        G_CALLBACK (display_closed), icon_theme);
652
      g_signal_connect (settings, "notify::gtk-icon-theme-name",
Matthias Clasen's avatar
Matthias Clasen committed
653
                        G_CALLBACK (theme_changed), icon_theme);
654 655 656 657 658 659 660 661 662
    }

  update_current_theme (icon_theme);
}

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

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

  formats = gdk_pixbuf_get_formats ();

Matthias Clasen's avatar
Matthias Clasen committed
674
  found_svg = FALSE; 
675 676 677 678 679 680
  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
681 682 683 684
        {
          if (strcmp (*mime_type, "image/svg") == 0)
            found_svg = TRUE;
        }
685

686
      g_strfreev (mime_types);
687 688 689
    }

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

Alexander Larsson's avatar
Alexander Larsson committed
694 695 696 697 698 699 700
/* 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
701 702 703 704 705
                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
706 707 708 709 710 711 712

  icon_info->in_cache = NULL;

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

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

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

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

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

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

731
  priv->search_path_len = 2 * i + 2;
732 733 734 735 736
  
  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);
737
  priv->search_path[i++] = g_build_filename (g_get_home_dir (), ".icons", NULL);
738
  
739 740
  for (j = 0; xdg_data_dirs[j]; j++) 
    priv->search_path[i++] = g_build_filename (xdg_data_dirs[j], "icons", NULL);
741

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

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

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

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

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

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

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

773 774
  g_signal_emit (icon_theme, signal_changed, 0);

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

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

  return FALSE;
}

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

  if (!priv->theme_changed_idle)
789 790 791 792 793 794
    {
      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");
    }
795 796
}

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

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

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

807
  GTK_NOTE (ICONTHEME, 
Matthias Clasen's avatar
Matthias Clasen committed
808
            g_print ("change to icon theme \"%s\"\n", priv->current_theme));
809
  blow_themes (icon_theme);
810

811 812
  queue_theme_changed (icon_theme);

813 814 815 816 817 818 819 820 821
}

static void
blow_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
  
  if (priv->themes_valid)
    {
822 823
      g_list_free_full (priv->themes, (GDestroyNotify) theme_destroy);
      g_list_free_full (priv->dir_mtimes, (GDestroyNotify) free_dir_mtime);
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
      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
842 843 844
  g_hash_table_destroy (priv->info_cache);
  g_assert (priv->info_cache_lru == NULL);

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

848 849 850 851
  unset_screen (icon_theme);

  g_free (priv->current_theme);

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

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

  blow_themes (icon_theme);
859

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

/**
 * gtk_icon_theme_set_search_path:
 * @icon_theme: a #GtkIconTheme
866 867
 * @path: (array length=n_elements) (element-type filename): array of
 *     directories that are searched for icon themes
868 869 870 871 872
 * @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
873 874 875
 * 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.)
876
 *
877
 * In addition if an icon found isn’t found either in the current
878 879 880 881
 * 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
882
 * into the fallback icon theme, which is called hicolor,
883
 * rather than directly on the icon path.)
Matthias Clasen's avatar
Matthias Clasen committed
884 885
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
886
 */
887 888
void
gtk_icon_theme_set_search_path (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
889 890
                                const gchar  *path[],
                                gint          n_elements)
891 892 893 894 895 896 897 898 899 900 901 902 903 904
{
  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;
905

906 907 908 909 910 911 912 913 914
  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
915
 * @path: (allow-none) (array length=n_elements) (element-type filename) (out):
916 917 918 919
 *     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
 *
920
 * Gets the current search path. See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
921 922
 *
 * Since: 2.4
923
 */
924
void
925 926 927
gtk_icon_theme_get_search_path (GtkIconTheme  *icon_theme,
                                gchar        **path[],
                                gint          *n_elements)
928 929
{
  GtkIconThemePrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
930
  gint i;
931 932 933 934 935 936 937 938 939 940 941 942

  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
943
        (*path)[i] = g_strdup (priv->search_path[i]);
944 945 946 947 948 949 950
      (*path)[i] = NULL;
    }
}

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

971 972 973 974 975 976 977 978 979
  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
980
 * @path: (type filename): directory name to prepend to the icon path
981
 * 
982 983
 * Prepends a directory to the search path. 
 * See gtk_icon_theme_set_search_path().
Matthias Clasen's avatar
Matthias Clasen committed
984 985
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
986
 */
987 988
void
gtk_icon_theme_prepend_search_path (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
989
                                    const gchar  *path)
990 991
{
  GtkIconThemePrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
992
  gint i;
993 994 995 996 997 998 999 1000 1001

  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);

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

  do_theme_change (icon_theme);
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
/**
 * 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
 */
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
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);
}

1043 1044 1045
/**
 * gtk_icon_theme_set_custom_theme:
 * @icon_theme: a #GtkIconTheme
1046 1047
 * @theme_name: (allow-none): name of icon theme to use instead of
 *   configured theme, or %NULL to unset a previously set custom theme
1048 1049 1050 1051
 * 
 * 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()
1052
 * and gtk_icon_theme_get_for_screen().
Matthias Clasen's avatar
Matthias Clasen committed
1053 1054
 *
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
1055
 */
1056 1057
void
gtk_icon_theme_set_custom_theme (GtkIconTheme *icon_theme,
Matthias Clasen's avatar
Matthias Clasen committed
1058
                                 const gchar  *theme_name)
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
{
  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;
1071
      if (!priv->current_theme || strcmp (theme_name, priv->current_theme) != 0)
Matthias Clasen's avatar
Matthias Clasen committed
1072 1073 1074
        {
          g_free (priv->current_theme);
          priv->current_theme = g_strdup (theme_name);
1075

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

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
static const gchar builtin_hicolor_index[] =
"[Icon Theme]\n"
"Name=Hicolor\n"
"Hidden=True\n"
"Directories=16x16/actions,22x22/actions,24x24/actions,32x32/actions\n"
"[16x16/actions]\n"
"Size=16\n"
"Type=Threshold\n"
"[22x22/actions]\n"
"Size=22\n"
"Type=Threshold\n"
"[24x24/actions]\n"
"Size=24\n"
"Type=Threshold\n"
"[32x32/actions]\n"
"Size=32\n"
"Type=Threshold\n";

1107
static void
Matthias Clasen's avatar
Matthias Clasen committed
1108 1109
insert_theme (GtkIconTheme *icon_theme,
              const gchar  *theme_name)
1110
{
Matthias Clasen's avatar
Matthias Clasen committed
1111
  gint i;
1112
  GList *l;
Matthias Clasen's avatar
Matthias Clasen committed
1113 1114 1115
  gchar **dirs;
  gchar **scaled_dirs;
  gchar **themes;
1116
  GtkIconThemePrivate *priv;
1117
  IconTheme *theme = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1118
  gchar *path;
1119 1120
  GKeyFile *theme_file;
  GError *error = NULL;
1121
  IconThemeDirMtime *dir_mtime;
1122
  GStatBuf stat_buf;
1123 1124
  
  priv = icon_theme->priv;
1125

1126 1127 1128 1129
  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
1130
        return;
1131 1132 1133 1134 1135
    }
  
  for (i = 0; i < priv->search_path_len; i++)
    {
      path = g_build_filename (priv->search_path[i],
Matthias Clasen's avatar
Matthias Clasen committed
1136 1137
                               theme_name,
                               NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1138
      dir_mtime = g_slice_new (IconThemeDirMtime);
1139
      dir_mtime->cache = NULL;
1140
      dir_mtime->dir = path;
1141
      if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode)) {
Matthias Clasen's avatar
Matthias Clasen committed
1142
        dir_mtime->mtime = stat_buf.st_mtime;
1143 1144
        dir_mtime->exists = TRUE;
      } else {
Matthias Clasen's avatar
Matthias Clasen committed
1145
        dir_mtime->mtime = 0;
1146 1147
        dir_mtime->exists = FALSE;
      }
1148 1149 1150

      priv->dir_mtimes = g_list_prepend (priv->dir_mtimes, dir_mtime);
    }
1151
  priv->dir_mtimes = g_list_reverse (priv->dir_mtimes);
1152 1153

  theme_file = NULL;
1154
  for (i = 0; i < priv->search_path_len && !theme_file; i++)
1155 1156
    {
      path = g_build_filename (priv->search_path[i],
Matthias Clasen's avatar
Matthias Clasen committed
1157 1158 1159
                               theme_name,
                               "index.theme",
                               NULL);
1160
      if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) 
Matthias Clasen's avatar
Matthias Clasen committed
1161 1162 1163
        {
          theme_file = g_key_file_new ();
          g_key_file_set_list_separator (theme_file, ',');
1164
          if (!g_key_file_load_from_file (theme_file, path, 0, &error))
Matthias Clasen's avatar
Matthias Clasen committed
1165 1166 1167 1168 1169 1170 1171
            {
              g_key_file_free (theme_file);
              theme_file = NULL;
              g_error_free (error);
              error = NULL;
            }
        }
1172 1173 1174
      g_free (path);
    }

1175
  if (theme_file || strcmp (theme_name, FALLBACK_ICON_THEME) == 0)
1176 1177 1178 1179
    {
      theme = g_new0 (IconTheme, 1);
      theme->name = g_strdup (theme_name);
      priv->themes = g_list_prepend (priv->themes, theme);
1180 1181 1182 1183 1184 1185
      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);
        }
1186 1187
    }

1188 1189
  if (theme_file == NULL)
    return;
1190

1191 1192 1193
  theme->display_name = 
    g_key_file_get_locale_string (theme_file, "Icon Theme", "Name", NULL, NULL);
  if (!theme->display_name)
1194
    g_warning ("Theme file for %s has no name\n", theme_name);
1195

1196 1197
  dirs = g_key_file_get_string_list (theme_file, "Icon Theme", "Directories", NULL, NULL);
  if (!dirs)
Matthias Clasen's avatar
Matthias Clasen committed
1198 1199
    {
      g_warning ("Theme file for %s has no directories\n", theme_name);
1200 1201
      priv->themes = g_list_remove (priv->themes, theme);
      g_free (theme->name);
Matthias Clasen's avatar
Matthias Clasen committed
1202 1203 1204 1205 1206
      g_free (theme->display_name);
      g_free (theme);
      g_key_file_free (theme_file);
      return;
    }
1207 1208 1209

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

1210 1211
  theme->comment = 
    g_key_file_get_locale_string (theme_file, 
Matthias Clasen's avatar
Matthias Clasen committed
1212 1213
                                  "Icon Theme", "Comment",
                                  NULL, NULL);
1214 1215
  theme->example = 
    g_key_file_get_string (theme_file, 
Matthias Clasen's avatar
Matthias Clasen committed
1216 1217
                           "Icon Theme", "Example",
                           NULL);
1218 1219

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

1223 1224 1225 1226 1227
  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
1228
  g_strfreev (dirs);
1229
  g_strfreev (scaled_dirs);
Matthias Clasen's avatar
Matthias Clasen committed
1230

1231 1232
  theme->dirs = g_list_reverse (theme->dirs);

1233
  themes = g_key_file_get_string_list (theme_file,
Matthias Clasen's avatar
Matthias Clasen committed
1234 1235 1236 1237
                                       "Icon Theme",
                                       "Inherits",
                                       NULL,
                                       NULL);
1238
  if (themes)
1239 1240
    {
      for (i = 0; themes[i] != NULL; i++)
Matthias Clasen's avatar
Matthias Clasen committed
1241
        insert_theme (icon_theme, themes[i]);
1242 1243 1244 1245
      
      g_strfreev (themes);
    }

1246
  g_key_file_free (theme_file);
1247 1248 1249 1250 1251
}

static void
free_unthemed_icon (UnthemedIcon *unthemed_icon)
{
1252 1253
  g_free (unthemed_icon->svg_filename);
  g_free (unthemed_icon->no_svg_filename);
1254
  g_slice_free (UnthemedIcon, unthemed_icon);
1255 1256
}

Matthias Clasen's avatar
Matthias Clasen committed
1257 1258
static gchar *
strip_suffix (const gchar *filename)
1259
{
Matthias Clasen's avatar
Matthias Clasen committed
1260
  const gchar *dot;
1261

1262 1263 1264
  if (g_str_has_suffix (filename, ".symbolic.png"))
    return g_strndup (filename, strlen(filename)-13);

1265 1266 1267 1268 1269 1270 1271 1272
  dot = strrchr (filename, '.');

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

  return g_strndup (filename, dot - filename);
}

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 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
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);
    }
}

1339 1340 1341 1342 1343
static void
load_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv;
  GDir *gdir;
Matthias Clasen's avatar
Matthias Clasen committed
1344 1345 1346
  gint base;
  gchar *dir;
  const gchar *file;
1347
  GTimeVal tv;
1348
  IconThemeDirMtime *dir_mtime;
1349
  GStatBuf stat_buf;
1350
  GList *d;
1351 1352 1353
  
  priv = icon_theme->priv;

1354 1355
  if (priv->current_theme)
    insert_theme (icon_theme, priv->current_theme);
1356

1357 1358 1359 1360
  /* 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.
1361
   */
1362
  insert_theme (icon_theme, DEFAULT_ICON_THEME);
1363
  insert_theme (icon_theme, "gnome");
1364
  insert_theme (icon_theme, FALLBACK_ICON_THEME);
1365
  priv->themes = g_list_reverse (priv->themes);
1366 1367


1368
  priv->unthemed_icons = g_hash_table_new_full (g_str_hash, g_str_equal,
Matthias Clasen's avatar
Matthias Clasen committed
1369
                                                g_free, (GDestroyNotify)free_unthemed_icon);
1370 1371 1372 1373

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

Matthias Clasen's avatar
Matthias Clasen committed
1375
      dir_mtime = g_slice_new (IconThemeDirMtime);
1376
      priv->dir_mtimes = g_list_append (priv->dir_mtimes, dir_mtime);
1377
      
1378 1379
      dir_mtime->dir = g_strdup (dir);
      dir_mtime->mtime = 0;
1380
      dir_mtime->exists = FALSE;
1381 1382 1383
      dir_mtime->cache = NULL;

      if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode))
Matthias Clasen's avatar
Matthias Clasen committed
1384
        continue;
1385
      dir_mtime->mtime = stat_buf.st_mtime;
1386
      dir_mtime->exists = TRUE;
1387 1388

      dir_mtime->cache = _gtk_icon_cache_new_for_path (dir);
1389
      if (dir_mtime->cache != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
1390
        continue;
1391

1392 1393
      gdir = g_dir_open (dir, 0, NULL);
      if (gdir == NULL)
Matthias Clasen's avatar
Matthias Clasen committed
1394
        continue;
1395

1396
      while ((file = g_dir_read_name (gdir)))
1397
        add_unthemed_icon (icon_theme, dir, file, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1398

1399 1400
      g_dir_close (gdir);
    }
Matthias Clasen's avatar
Matthias Clasen committed
1401

1402 1403 1404 1405
  for (d = priv->resource_paths; d; d = d->next)
    {
      gchar **children;
      gint i;
Matthias Clasen's avatar
Matthias Clasen committed
1406

1407 1408 1409 1410
      dir = d->data;
      children = g_resources_enumerate_children (dir, 0, NULL);
      if (!children)
        continue;
Matthias Clasen's avatar
Matthias Clasen committed
1411

1412 1413
      for (i = 0; children[i]; i++)
        add_unthemed_icon (icon_theme, dir, children[i], TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
1414

1415
      g_strfreev (children);
1416 1417 1418 1419
    }

  priv->themes_valid = TRUE;
  
Matthias Clasen's avatar
Matthias Clasen committed
1420
  g_get_current_time (&tv);
1421
  priv->last_stat_time = tv.tv_sec;
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432

  GTK_NOTE (ICONTHEME, {
    GList *l;
    g_print ("Current icon themes ");
    for (l = icon_theme->priv->themes; l; l = l->next)
      {
        IconTheme *theme = l->data;
        g_print ("%s ", theme->name);
      }
    g_print ("\n");
  });
1433 1434 1435 1436 1437 1438
}

static void
ensure_valid_themes (GtkIconTheme *icon_theme)
{
  GtkIconThemePrivate *priv = icon_theme->priv;
1439
  GTimeVal tv;
1440
  gboolean was_valid = priv->themes_valid;
1441

1442 1443 1444 1445
  if (priv->loading_themes)
    return;
  priv->loading_themes = TRUE;

1446 1447
  if (priv->themes_valid)
    {
Matthias Clasen's avatar
Matthias Clasen committed
1448
      g_get_current_time (&tv);
1449

1450
      if (ABS (tv.tv_sec - priv->last_stat_time) > 5 &&
Matthias Clasen's avatar
Matthias Clasen committed
1451
          rescan_themes (icon_theme))
1452 1453 1454 1455
        {
          g_hash_table_remove_all (priv->info_cache);
          blow_themes (icon_theme);
        }
1456 1457 1458
    }
  
  if (!priv->themes_valid)
1459 1460 1461
    {
      load_themes (icon_theme);

1462
      if (was_valid)
1463
        queue_theme_changed (icon_theme);