diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index b1da58d24..1da8ae973 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -198,6 +198,7 @@ typedef struct gboolean exists; GtkIconCache *cache; + gboolean cache_tried; /* a theme reads its cache once, not per directory */ } IconThemeDirMtime; static void gtk_icon_theme_finalize (GObject *object); @@ -917,6 +918,7 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name) NULL); dir_mtime = g_slice_new (IconThemeDirMtime); dir_mtime->cache = NULL; + dir_mtime->cache_tried = FALSE; dir_mtime->dir = path; if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode)) { @@ -1078,6 +1080,7 @@ load_themes (GtkIconTheme *icon_theme) dir_mtime->mtime = 0; dir_mtime->exists = FALSE; dir_mtime->cache = NULL; + dir_mtime->cache_tried = TRUE; if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode)) continue; @@ -2492,6 +2495,37 @@ theme_subdir_load (GtkIconTheme *icon_theme, GError *error = NULL; IconThemeDirMtime *dir_mtime; + /* Most directories an index.theme names are not there: those cost no + * reading of their keys */ + for (d = icon_theme->priv->dir_mtimes; d; d = d->next) + { + gboolean there; + + dir_mtime = (IconThemeDirMtime *)d->data; + if (!dir_mtime->exists) + continue; + + if (!dir_mtime->cache_tried) + { + /* NULL if there is no cache, or an outdated one */ + dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir); + dir_mtime->cache_tried = TRUE; + } + + if (dir_mtime->cache) + there = _gtk_icon_cache_get_directory_index (dir_mtime->cache, subdir) >= 0; + else + { + full_dir = g_build_filename (dir_mtime->dir, subdir, NULL); + there = g_file_test (full_dir, G_FILE_TEST_IS_DIR); + g_free (full_dir); + } + if (there) + break; + } + if (d == NULL) + return; + size = g_key_file_get_integer (theme_file, subdir, "Size", &error); if (error) { @@ -2559,15 +2593,9 @@ theme_subdir_load (GtkIconTheme *icon_theme, full_dir = g_build_filename (dir_mtime->dir, subdir, NULL); - /* First, see if we have a cache for the directory */ + /* the cache was read above */ if (dir_mtime->cache != NULL || g_file_test (full_dir, G_FILE_TEST_IS_DIR)) { - if (dir_mtime->cache == NULL) - { - /* This will return NULL if the cache doesn't exist or is outdated */ - dir_mtime->cache = _gtk_icon_cache_new_for_path (dir_mtime->dir); - } - dir = g_new (IconThemeDir, 1); dir->type = type; dir->context = context; @@ -2590,7 +2618,14 @@ theme_subdir_load (GtkIconTheme *icon_theme, scan_directory (icon_theme->priv, dir, full_dir); } - theme->dirs = g_list_prepend (theme->dirs, dir); + /* An index.theme names hundreds of directories (hicolor 649), + * of which a theme fills a few; empty ones would only be walked + * at every lookup. With a cache, it has those that exist. */ + if (dir->cache ? dir->subdir_index < 0 : + g_hash_table_size (dir->icons) == 0 && dir->icon_data == NULL) + theme_dir_destroy (dir); + else + theme->dirs = g_list_prepend (theme->dirs, dir); } else g_free (full_dir);