2004-10-19 18:45:41 +00:00
|
|
|
/* gtkiconcache.c
|
|
|
|
* Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2004-10-19 18:45:41 +00:00
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2005-06-27 04:49:03 +00:00
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
#include "gtkdebug.h"
|
2018-02-01 14:34:57 +00:00
|
|
|
#include "gtkiconcacheprivate.h"
|
2018-02-01 15:01:38 +00:00
|
|
|
#include "gtkiconcachevalidatorprivate.h"
|
2022-09-23 14:11:59 +00:00
|
|
|
#include "gtkprivate.h"
|
2006-09-10 06:39:16 +00:00
|
|
|
|
2004-12-12 21:09:13 +00:00
|
|
|
#include <glib/gstdio.h>
|
Update spec.
2005-03-15 Anders Carlsson <andersca@imendio.com>
* docs/iconcache.txt:
Update spec.
* gtk/gtkiconcache.c: (find_image_offset),
(_gtk_icon_cache_get_icon_flags), (_gtk_icon_cache_add_icons),
(_gtk_icon_cache_get_icon), (_gtk_icon_cache_get_icon_data):
* gtk/gtkiconcache.h:
Update to be able to fetch pixbuf data and icon metadata.
* gtk/gtkicontheme.c: (theme_lookup_icon), (gtk_icon_info_free),
(icon_info_ensure_scale_and_pixbuf):
Use new cache functions.
* gtk/updateiconcache.c: (foreach_remove_func), (load_icon_data),
(maybe_cache_image_data), (scan_directory), (write_pixdata),
(get_image_meta_data_size), (get_image_pixel_data_size),
(get_image_data_size), (get_single_node_size), (get_bucket_size),
(write_bucket), (main):
Update to write pixbuf data as well as information from .icon
files.
2005-03-15 13:18:25 +00:00
|
|
|
#include <gdk-pixbuf/gdk-pixdata.h>
|
2004-10-19 18:45:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-07-03 15:47:42 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2004-10-19 18:45:41 +00:00
|
|
|
#include <fcntl.h>
|
2005-06-27 04:49:03 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2004-10-19 18:45:41 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-06-27 04:49:03 +00:00
|
|
|
|
2004-12-12 21:09:13 +00:00
|
|
|
#ifndef _O_BINARY
|
|
|
|
#define _O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
#define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
|
|
|
|
#define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))
|
|
|
|
|
|
|
|
struct _GtkIconCache {
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2004-10-19 18:45:41 +00:00
|
|
|
|
2005-06-27 04:49:03 +00:00
|
|
|
GMappedFile *map;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *buffer;
|
2007-12-03 17:44:27 +00:00
|
|
|
|
|
|
|
guint32 last_chain_offset;
|
2004-10-19 18:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GtkIconCache *
|
2018-02-01 15:05:58 +00:00
|
|
|
gtk_icon_cache_ref (GtkIconCache *cache)
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
2007-05-01 20:00:17 +00:00
|
|
|
cache->ref_count++;
|
2004-10-19 18:45:41 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-01 15:05:58 +00:00
|
|
|
gtk_icon_cache_unref (GtkIconCache *cache)
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
|
|
|
cache->ref_count --;
|
|
|
|
|
|
|
|
if (cache->ref_count == 0)
|
|
|
|
{
|
2022-09-23 14:11:59 +00:00
|
|
|
GTK_DEBUG (ICONTHEME, "unmapping icon cache");
|
2005-06-27 04:49:03 +00:00
|
|
|
|
2005-11-04 15:43:40 +00:00
|
|
|
if (cache->map)
|
2020-02-03 10:01:48 +00:00
|
|
|
g_mapped_file_unref (cache->map);
|
2004-10-19 18:45:41 +00:00
|
|
|
g_free (cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkIconCache *
|
2020-07-24 18:40:36 +00:00
|
|
|
gtk_icon_cache_new_for_path (const char *path)
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
2004-10-21 19:01:29 +00:00
|
|
|
GtkIconCache *cache = NULL;
|
2005-06-27 04:49:03 +00:00
|
|
|
GMappedFile *map;
|
2004-12-12 21:09:13 +00:00
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char *cache_filename;
|
2011-10-03 15:25:33 +00:00
|
|
|
GStatBuf st;
|
|
|
|
GStatBuf path_st;
|
2004-10-19 18:45:41 +00:00
|
|
|
|
2005-06-23 17:05:17 +00:00
|
|
|
/* Check if we have a cache file */
|
2004-10-19 18:45:41 +00:00
|
|
|
cache_filename = g_build_filename (path, "icon-theme.cache", NULL);
|
|
|
|
|
2022-09-23 14:11:59 +00:00
|
|
|
GTK_DEBUG (ICONTHEME, "look for icon cache in %s", path);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
2004-12-12 21:09:13 +00:00
|
|
|
if (g_stat (path, &path_st) < 0)
|
|
|
|
goto done;
|
|
|
|
|
2020-06-02 18:31:33 +00:00
|
|
|
if (g_stat (cache_filename, &st) < 0 || st.st_size < 4)
|
2004-10-19 18:45:41 +00:00
|
|
|
goto done;
|
|
|
|
|
2020-08-21 12:41:13 +00:00
|
|
|
/* Verify cache is up-to-date */
|
2004-10-19 18:45:41 +00:00
|
|
|
if (st.st_mtime < path_st.st_mtime)
|
|
|
|
{
|
2022-09-23 14:11:59 +00:00
|
|
|
GTK_DEBUG (ICONTHEME, "icon cache outdated");
|
2020-02-03 10:01:48 +00:00
|
|
|
goto done;
|
2004-10-19 18:45:41 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 04:49:03 +00:00
|
|
|
map = g_mapped_file_new (cache_filename, FALSE, NULL);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
2005-06-27 04:49:03 +00:00
|
|
|
if (!map)
|
2004-10-19 18:45:41 +00:00
|
|
|
goto done;
|
|
|
|
|
2007-09-14 02:11:01 +00:00
|
|
|
#ifdef G_ENABLE_DEBUG
|
2015-09-09 02:48:44 +00:00
|
|
|
if (GTK_DEBUG_CHECK (ICONTHEME))
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
2011-10-03 16:03:57 +00:00
|
|
|
CacheInfo info;
|
|
|
|
|
|
|
|
info.cache = g_mapped_file_get_contents (map);
|
|
|
|
info.cache_size = g_mapped_file_get_length (map);
|
|
|
|
info.n_directories = 0;
|
|
|
|
info.flags = CHECK_OFFSETS|CHECK_STRINGS;
|
|
|
|
|
2018-02-01 15:05:58 +00:00
|
|
|
if (!gtk_icon_cache_validate (&info))
|
2007-09-14 02:11:01 +00:00
|
|
|
{
|
2009-06-18 19:11:57 +00:00
|
|
|
g_mapped_file_unref (map);
|
2016-02-28 16:06:25 +00:00
|
|
|
g_warning ("Icon cache '%s' is invalid", cache_filename);
|
2005-06-27 04:49:03 +00:00
|
|
|
|
2007-09-14 02:11:01 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2004-10-19 18:45:41 +00:00
|
|
|
}
|
2020-02-03 10:01:48 +00:00
|
|
|
#endif
|
2007-09-14 02:11:01 +00:00
|
|
|
|
2022-09-23 14:11:59 +00:00
|
|
|
GTK_DEBUG (ICONTHEME, "found icon cache for %s", path);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
|
|
|
cache = g_new0 (GtkIconCache, 1);
|
|
|
|
cache->ref_count = 1;
|
2005-06-27 04:49:03 +00:00
|
|
|
cache->map = map;
|
2007-05-01 20:00:17 +00:00
|
|
|
cache->buffer = g_mapped_file_get_contents (map);
|
2005-06-27 04:49:03 +00:00
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
done:
|
2020-02-03 10:01:48 +00:00
|
|
|
g_free (cache_filename);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2005-11-04 15:43:40 +00:00
|
|
|
GtkIconCache *
|
2020-07-24 18:40:36 +00:00
|
|
|
gtk_icon_cache_new (const char *data)
|
2005-11-04 15:43:40 +00:00
|
|
|
{
|
|
|
|
GtkIconCache *cache;
|
|
|
|
|
|
|
|
cache = g_new0 (GtkIconCache, 1);
|
|
|
|
cache->ref_count = 1;
|
|
|
|
cache->map = NULL;
|
2020-07-24 18:40:36 +00:00
|
|
|
cache->buffer = (char *)data;
|
2020-02-03 10:01:48 +00:00
|
|
|
|
2005-11-04 15:43:40 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
static int
|
2004-10-19 18:45:41 +00:00
|
|
|
get_directory_index (GtkIconCache *cache,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *directory)
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
|
|
|
guint32 dir_list_offset;
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_dirs;
|
|
|
|
int i;
|
2020-02-03 10:01:48 +00:00
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
dir_list_offset = GET_UINT32 (cache->buffer, 8);
|
|
|
|
|
|
|
|
n_dirs = GET_UINT32 (cache->buffer, dir_list_offset);
|
|
|
|
|
|
|
|
for (i = 0; i < n_dirs; i++)
|
|
|
|
{
|
|
|
|
guint32 name_offset = GET_UINT32 (cache->buffer, dir_list_offset + 4 + 4 * i);
|
2020-07-24 18:40:36 +00:00
|
|
|
char *name = cache->buffer + name_offset;
|
2004-10-19 18:45:41 +00:00
|
|
|
if (strcmp (name, directory) == 0)
|
2020-02-03 10:01:48 +00:00
|
|
|
return i;
|
2004-10-19 18:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:01:48 +00:00
|
|
|
return -1;
|
2014-06-20 16:11:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:00:17 +00:00
|
|
|
GHashTable *
|
|
|
|
gtk_icon_cache_list_icons_in_directory (GtkIconCache *cache,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *directory,
|
2020-02-07 10:54:00 +00:00
|
|
|
GtkStringSet *set)
|
2020-02-03 09:00:17 +00:00
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int directory_index;
|
2020-02-03 09:00:17 +00:00
|
|
|
guint32 hash_offset, n_buckets;
|
|
|
|
guint32 chain_offset;
|
|
|
|
guint32 image_list_offset, n_images;
|
|
|
|
int i, j;
|
|
|
|
GHashTable *icons = NULL;
|
|
|
|
|
|
|
|
directory_index = get_directory_index (cache, directory);
|
|
|
|
|
|
|
|
if (directory_index == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hash_offset = GET_UINT32 (cache->buffer, 4);
|
|
|
|
n_buckets = GET_UINT32 (cache->buffer, hash_offset);
|
|
|
|
|
|
|
|
for (i = 0; i < n_buckets; i++)
|
|
|
|
{
|
|
|
|
chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
|
|
|
|
while (chain_offset != 0xffffffff)
|
|
|
|
{
|
|
|
|
guint32 flags = 0;
|
|
|
|
|
|
|
|
image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
|
|
|
|
n_images = GET_UINT32 (cache->buffer, image_list_offset);
|
|
|
|
|
|
|
|
for (j = 0; j < n_images; j++)
|
|
|
|
{
|
|
|
|
if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j) ==
|
|
|
|
directory_index)
|
|
|
|
{
|
|
|
|
flags = GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * j + 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags != 0)
|
|
|
|
{
|
|
|
|
guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
|
|
|
|
const char *name = cache->buffer + name_offset;
|
2020-02-07 10:54:00 +00:00
|
|
|
const char *interned_name;
|
2020-02-03 09:00:17 +00:00
|
|
|
guint32 hash_flags = 0;
|
|
|
|
|
2020-02-04 08:35:20 +00:00
|
|
|
/* Icons named foo.symbolic.png are stored in the cache as "foo.symbolic" with ICON_CACHE_FLAG_PNG,
|
|
|
|
* but we convert it internally to ICON_CACHE_FLAG_SYMBOLIC_PNG.
|
2020-02-03 09:00:17 +00:00
|
|
|
* Otherwise we use the same enum values and names as on disk. */
|
2020-02-04 08:35:20 +00:00
|
|
|
if (g_str_has_suffix (name, ".symbolic") && (flags & ICON_CACHE_FLAG_PNG_SUFFIX) != 0)
|
2020-02-03 09:00:17 +00:00
|
|
|
{
|
2020-02-07 10:54:00 +00:00
|
|
|
char *converted_name = g_strndup (name, strlen (name) - 9);
|
|
|
|
interned_name = gtk_string_set_add (set, converted_name);
|
|
|
|
g_free (converted_name);
|
2020-02-04 08:35:20 +00:00
|
|
|
flags |= ICON_CACHE_FLAG_SYMBOLIC_PNG_SUFFIX;
|
|
|
|
flags &= ~ICON_CACHE_FLAG_PNG_SUFFIX;
|
2020-02-03 09:00:17 +00:00
|
|
|
}
|
|
|
|
else
|
2020-02-07 10:54:00 +00:00
|
|
|
interned_name = gtk_string_set_add (set, name);
|
2020-02-03 09:00:17 +00:00
|
|
|
|
|
|
|
if (!icons)
|
2020-02-07 10:54:00 +00:00
|
|
|
icons = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
|
2020-02-03 09:00:17 +00:00
|
|
|
|
2020-02-07 10:54:00 +00:00
|
|
|
hash_flags = GPOINTER_TO_INT (g_hash_table_lookup (icons, interned_name));
|
|
|
|
g_hash_table_replace (icons, (char *)interned_name, GUINT_TO_POINTER (hash_flags|flags));
|
2020-02-03 09:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chain_offset = GET_UINT32 (cache->buffer, chain_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return icons;
|
|
|
|
}
|