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"
|
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))))
|
|
|
|
|
2020-02-03 09:00:17 +00:00
|
|
|
/* Keep in sync with gtkicontheme.c */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
/* These are used in the file format: */
|
|
|
|
ICON_SUFFIX_NONE = 0,
|
|
|
|
ICON_SUFFIX_XPM = 1 << 0,
|
|
|
|
ICON_SUFFIX_SVG = 1 << 1,
|
|
|
|
ICON_SUFFIX_PNG = 1 << 2,
|
|
|
|
HAS_ICON_FILE = 1 << 3,
|
|
|
|
/* This is just used by Gtk, so we convert internally to this: */
|
|
|
|
ICON_SUFFIX_SYMBOLIC_PNG = 1 << 4
|
|
|
|
} IconSuffix;
|
2007-12-03 17:44:27 +00:00
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
struct _GtkIconCache {
|
|
|
|
gint ref_count;
|
|
|
|
|
2005-06-27 04:49:03 +00:00
|
|
|
GMappedFile *map;
|
2004-10-19 18:45:41 +00:00
|
|
|
gchar *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)
|
|
|
|
{
|
2016-02-28 20:33:18 +00:00
|
|
|
GTK_NOTE (ICONTHEME, g_message ("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 *
|
2018-02-01 15:05:58 +00:00
|
|
|
gtk_icon_cache_new_for_path (const gchar *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
|
|
|
|
2004-10-19 18:45:41 +00:00
|
|
|
gchar *cache_filename;
|
2004-12-12 21:09:13 +00:00
|
|
|
gint fd = -1;
|
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);
|
|
|
|
|
2016-02-28 20:33:18 +00:00
|
|
|
GTK_NOTE (ICONTHEME, g_message ("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;
|
|
|
|
|
|
|
|
/* Open the file and map it into memory */
|
|
|
|
fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
|
|
|
if (fd < 0)
|
2005-06-27 04:49:03 +00:00
|
|
|
goto done;
|
2011-10-03 15:25:33 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
2011-10-12 03:39:06 +00:00
|
|
|
|
|
|
|
/* Bug 660730: _fstat32 is only defined in msvcrt80.dll+/VS 2005+ */
|
|
|
|
/* or possibly in the msvcrt.dll linked to by the Windows DDK */
|
|
|
|
/* (will need to check on the Windows DDK part later) */
|
2018-06-10 21:04:50 +00:00
|
|
|
#if ((defined (_MSC_VER) && (_MSC_VER >= 1400 || __MSVCRT_VERSION__ >= 0x0800)) || defined (__MINGW64_VERSION_MAJOR)) && !defined(_WIN64)
|
2011-10-03 15:25:33 +00:00
|
|
|
#undef fstat /* Just in case */
|
2020-02-03 10:01:48 +00:00
|
|
|
#define fstat _fstat32
|
2011-10-12 03:39:06 +00:00
|
|
|
#endif
|
2011-10-03 15:25:33 +00:00
|
|
|
#endif
|
|
|
|
|
2004-10-21 05:19:27 +00:00
|
|
|
if (fstat (fd, &st) < 0 || st.st_size < 4)
|
2004-10-19 18:45:41 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Verify cache is uptodate */
|
|
|
|
if (st.st_mtime < path_st.st_mtime)
|
|
|
|
{
|
2016-02-28 20:33:18 +00:00
|
|
|
GTK_NOTE (ICONTHEME, g_message ("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
|
|
|
|
2016-02-28 20:33:18 +00:00
|
|
|
GTK_NOTE (ICONTHEME, g_message ("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);
|
2005-06-27 04:49:03 +00:00
|
|
|
if (fd >= 0)
|
2004-12-12 21:09:13 +00:00
|
|
|
close (fd);
|
2004-10-19 18:45:41 +00:00
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2005-11-04 15:43:40 +00:00
|
|
|
GtkIconCache *
|
2018-02-01 15:05:58 +00:00
|
|
|
gtk_icon_cache_new (const gchar *data)
|
2005-11-04 15:43:40 +00:00
|
|
|
{
|
|
|
|
GtkIconCache *cache;
|
|
|
|
|
|
|
|
cache = g_new0 (GtkIconCache, 1);
|
|
|
|
cache->ref_count = 1;
|
|
|
|
cache->map = NULL;
|
|
|
|
cache->buffer = (gchar *)data;
|
2020-02-03 10:01:48 +00:00
|
|
|
|
2005-11-04 15:43:40 +00:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2004-10-19 18:45:41 +00:00
|
|
|
get_directory_index (GtkIconCache *cache,
|
2020-02-03 10:01:48 +00:00
|
|
|
const gchar *directory)
|
2004-10-19 18:45:41 +00:00
|
|
|
{
|
|
|
|
guint32 dir_list_offset;
|
2005-11-04 15:43:40 +00:00
|
|
|
gint n_dirs;
|
|
|
|
gint 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);
|
|
|
|
gchar *name = cache->buffer + name_offset;
|
|
|
|
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,
|
|
|
|
const gchar *directory)
|
|
|
|
{
|
|
|
|
gint directory_index;
|
|
|
|
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;
|
|
|
|
char *converted_name;
|
|
|
|
guint32 hash_flags = 0;
|
|
|
|
|
|
|
|
/* Icons named foo.symbolic.png are stored in the cache as "foo.symbolic" with ICON_SUFFIX_PNG,
|
|
|
|
* but we convert it internally to ICON_SUFFIX_SYMBOLIC_PNG.
|
|
|
|
* Otherwise we use the same enum values and names as on disk. */
|
|
|
|
if (g_str_has_suffix (name, ".symbolic") && (flags & ICON_SUFFIX_PNG) != 0)
|
|
|
|
{
|
|
|
|
flags |= ICON_SUFFIX_SYMBOLIC_PNG;
|
|
|
|
flags &= ~ICON_SUFFIX_PNG;
|
|
|
|
converted_name = g_strndup (name, strlen(name) - 9);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
converted_name = g_strdup (name);
|
|
|
|
|
|
|
|
if (!icons)
|
|
|
|
icons = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
|
|
|
|
|
|
|
|
hash_flags = GPOINTER_TO_INT (g_hash_table_lookup (icons, converted_name));
|
|
|
|
g_hash_table_replace (icons, converted_name, GUINT_TO_POINTER (hash_flags|flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
chain_offset = GET_UINT32 (cache->buffer, chain_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return icons;
|
|
|
|
}
|