gtkiconcache.c 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17
 */

18
#include "config.h"
Matthias Clasen's avatar
Matthias Clasen committed
19

20 21
#include "gtkdebug.h"
#include "gtkiconcache.h"
22
#include "gtkiconcachevalidator.h"
23

24
#include <glib/gstdio.h>
Anders Carlsson's avatar
Anders Carlsson committed
25
#include <gdk-pixbuf/gdk-pixdata.h>
26 27 28 29

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
30 31 32
#ifdef G_OS_WIN32
#include <io.h>
#endif
33
#include <fcntl.h>
Matthias Clasen's avatar
Matthias Clasen committed
34 35
#include <sys/types.h>
#include <sys/stat.h>
36 37
#include <string.h>

Matthias Clasen's avatar
Matthias Clasen committed
38

39 40 41 42
#ifndef _O_BINARY
#define _O_BINARY 0
#endif

43 44 45 46 47 48
#define MAJOR_VERSION 1
#define MINOR_VERSION 0

#define GET_UINT16(cache, offset) (GUINT16_FROM_BE (*(guint16 *)((cache) + (offset))))
#define GET_UINT32(cache, offset) (GUINT32_FROM_BE (*(guint32 *)((cache) + (offset))))

49

50 51 52
struct _GtkIconCache {
  gint ref_count;

Matthias Clasen's avatar
Matthias Clasen committed
53
  GMappedFile *map;
54
  gchar *buffer;
55 56

  guint32 last_chain_offset;
57 58 59 60 61
};

GtkIconCache *
_gtk_icon_cache_ref (GtkIconCache *cache)
{
62
  cache->ref_count++;
63 64 65 66 67 68 69 70 71 72 73 74
  return cache;
}

void
_gtk_icon_cache_unref (GtkIconCache *cache)
{
  cache->ref_count --;

  if (cache->ref_count == 0)
    {
      GTK_NOTE (ICONTHEME, 
		g_print ("unmapping icon cache\n"));
Matthias Clasen's avatar
Matthias Clasen committed
75

76
      if (cache->map)
Ryan Lortie's avatar
Ryan Lortie committed
77
	g_mapped_file_unref (cache->map);
78 79 80 81 82 83 84
      g_free (cache);
    }
}

GtkIconCache *
_gtk_icon_cache_new_for_path (const gchar *path)
{
85
  GtkIconCache *cache = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
86
  GMappedFile *map;
87

88
  gchar *cache_filename;
89
  gint fd = -1;
90 91
  GStatBuf st;
  GStatBuf path_st;
92

93
   /* Check if we have a cache file */
94 95 96 97 98
  cache_filename = g_build_filename (path, "icon-theme.cache", NULL);

  GTK_NOTE (ICONTHEME, 
	    g_print ("look for cache in %s\n", path));

99 100 101 102 103
  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);
104 105

  if (fd < 0)
Matthias Clasen's avatar
Matthias Clasen committed
106
    goto done;
107 108

#ifdef G_OS_WIN32
109 110 111 112 113

/* 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) */
#if (_MSC_VER >= 1400 || __MSVCRT_VERSION__ >= 0x0800)
114 115
#undef fstat /* Just in case */
#define fstat _fstat32  
116
#endif
117 118
#endif

119
  if (fstat (fd, &st) < 0 || st.st_size < 4)
120 121 122 123 124
    goto done;

  /* Verify cache is uptodate */
  if (st.st_mtime < path_st.st_mtime)
    {
125
      GTK_NOTE (ICONTHEME, g_print ("cache outdated\n"));
126 127 128
      goto done; 
    }

Matthias Clasen's avatar
Matthias Clasen committed
129
  map = g_mapped_file_new (cache_filename, FALSE, NULL);
130

Matthias Clasen's avatar
Matthias Clasen committed
131
  if (!map)
132 133
    goto done;

134
#ifdef G_ENABLE_DEBUG
135
  if (GTK_DEBUG_CHECK (ICONTHEME))
136
    {
137 138 139 140 141 142 143
      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;

144 145
      if (!_gtk_icon_cache_validate (&info))
        {
Ryan Lortie's avatar
Ryan Lortie committed
146
          g_mapped_file_unref (map);
147
          g_warning ("Icon cache '%s' is invalid\n", cache_filename);
Matthias Clasen's avatar
Matthias Clasen committed
148

149 150
          goto done;
        }
151
    }
152 153
#endif 

154
  GTK_NOTE (ICONTHEME, g_print ("found cache for %s\n", path));
155 156 157

  cache = g_new0 (GtkIconCache, 1);
  cache->ref_count = 1;
Matthias Clasen's avatar
Matthias Clasen committed
158
  cache->map = map;
159
  cache->buffer = g_mapped_file_get_contents (map);
Matthias Clasen's avatar
Matthias Clasen committed
160

161
 done:
162
  g_free (cache_filename);  
Matthias Clasen's avatar
Matthias Clasen committed
163
  if (fd >= 0)
164
    close (fd);
165 166 167 168

  return cache;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182
GtkIconCache *
_gtk_icon_cache_new (const gchar *data)
{
  GtkIconCache *cache;

  cache = g_new0 (GtkIconCache, 1);
  cache->ref_count = 1;
  cache->map = NULL;
  cache->buffer = (gchar *)data;
  
  return cache;
}

static gint
183 184 185 186
get_directory_index (GtkIconCache *cache,
		     const gchar *directory)
{
  guint32 dir_list_offset;
187 188
  gint n_dirs;
  gint i;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  
  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)
	return i;
    }
  
  return -1;
}

205 206 207
gint
_gtk_icon_cache_get_directory_index (GtkIconCache *cache,
			             const gchar *directory)
208
{
209
  return get_directory_index (cache, directory);
210 211 212 213 214
}

static guint
icon_name_hash (gconstpointer key)
{
215 216
  const signed char *p = key;
  guint32 h = *p;
217 218 219 220 221 222 223 224

  if (h)
    for (p += 1; *p != '\0'; p++)
      h = (h << 5) - h + *p;

  return h;
}

225
static gint
Anders Carlsson's avatar
Anders Carlsson committed
226 227
find_image_offset (GtkIconCache *cache,
		   const gchar  *icon_name,
228
		   gint          directory_index)
229 230 231 232
{
  guint32 hash_offset;
  guint32 n_buckets;
  guint32 chain_offset;
233
  int hash;
234 235
  guint32 image_list_offset, n_images;
  int i;
236

237 238 239
  if (!icon_name)
    return 0;

240 241 242 243 244 245 246 247 248 249
  chain_offset = cache->last_chain_offset;
  if (chain_offset)
    {
      guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
      gchar *name = cache->buffer + name_offset;

      if (strcmp (name, icon_name) == 0)
        goto find_dir;
    }

250 251 252 253 254 255 256 257 258 259 260
  hash_offset = GET_UINT32 (cache->buffer, 4);
  n_buckets = GET_UINT32 (cache->buffer, hash_offset);
  hash = icon_name_hash (icon_name) % n_buckets;

  chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
  while (chain_offset != 0xffffffff)
    {
      guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
      gchar *name = cache->buffer + name_offset;

      if (strcmp (name, icon_name) == 0)
261 262 263
        {
          cache->last_chain_offset = chain_offset;
          goto find_dir;
264
	}
265
  
266 267 268
      chain_offset = GET_UINT32 (cache->buffer, chain_offset);
    }

269 270
  cache->last_chain_offset = 0;
  return 0;
271

272
find_dir:
273 274 275 276 277 278 279
  /* We've found an icon list, now check if we have the right icon in it */
  image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
  n_images = GET_UINT32 (cache->buffer, image_list_offset);
  
  for (i = 0; i < n_images; i++)
    {
      if (GET_UINT16 (cache->buffer, image_list_offset + 4 + 8 * i) ==
Anders Carlsson's avatar
Anders Carlsson committed
280 281
	  directory_index) 
	return image_list_offset + 4 + 8 * i;
282
    }
Anders Carlsson's avatar
Anders Carlsson committed
283

284 285 286
  return 0;
}

Anders Carlsson's avatar
Anders Carlsson committed
287 288 289
gint
_gtk_icon_cache_get_icon_flags (GtkIconCache *cache,
				const gchar  *icon_name,
290
				gint          directory_index)
Anders Carlsson's avatar
Anders Carlsson committed
291 292 293
{
  guint32 image_offset;

294
  image_offset = find_image_offset (cache, icon_name, directory_index);
Anders Carlsson's avatar
Anders Carlsson committed
295 296 297 298 299 300 301

  if (!image_offset)
    return 0;

  return GET_UINT16 (cache->buffer, image_offset + 2);
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
gboolean
_gtk_icon_cache_has_icons (GtkIconCache *cache,
			   const gchar  *directory)
{
  int directory_index;
  guint32 hash_offset, n_buckets;
  guint32 chain_offset;
  guint32 image_list_offset, n_images;
  int i, j;

  directory_index = get_directory_index (cache, directory);

  if (directory_index == -1)
    return FALSE;

  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)
	{
	  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)
		return TRUE;
	    }

	  chain_offset = GET_UINT32 (cache->buffer, chain_offset);
	}
    }

  return FALSE;
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
void
_gtk_icon_cache_add_icons (GtkIconCache *cache,
			   const gchar  *directory,
			   GHashTable   *hash_table)
{
  int directory_index;
  guint32 hash_offset, n_buckets;
  guint32 chain_offset;
  guint32 image_list_offset, n_images;
  int i, j;
  
  directory_index = get_directory_index (cache, directory);

  if (directory_index == -1)
    return;
  
  hash_offset = GET_UINT32 (cache->buffer, 4);
  n_buckets = GET_UINT32 (cache->buffer, hash_offset);
Anders Carlsson's avatar
Anders Carlsson committed
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  for (i = 0; i < n_buckets; i++)
    {
      chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * i);
      while (chain_offset != 0xffffffff)
	{
	  guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
	  gchar *name = cache->buffer + name_offset;
	  
	  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)
		g_hash_table_insert (hash_table, name, NULL);
	    }

	  chain_offset = GET_UINT32 (cache->buffer, chain_offset);
	}
381 382 383 384 385 386 387 388 389 390 391
    }  
}

gboolean
_gtk_icon_cache_has_icon (GtkIconCache *cache,
			  const gchar  *icon_name)
{
  guint32 hash_offset;
  guint32 n_buckets;
  guint32 chain_offset;
  gint hash;
392
  
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  hash_offset = GET_UINT32 (cache->buffer, 4);
  n_buckets = GET_UINT32 (cache->buffer, hash_offset);

  hash = icon_name_hash (icon_name) % n_buckets;

  chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
  while (chain_offset != 0xffffffff)
    {
      guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
      gchar *name = cache->buffer + name_offset;

      if (strcmp (name, icon_name) == 0)
	return TRUE;
	  
      chain_offset = GET_UINT32 (cache->buffer, chain_offset);
    }

  return FALSE;
411
}
412

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
gboolean
_gtk_icon_cache_has_icon_in_directory (GtkIconCache *cache,
				       const gchar  *icon_name,
				       const gchar  *directory)
{
  guint32 hash_offset;
  guint32 n_buckets;
  guint32 chain_offset;
  gint hash;
  gboolean found_icon = FALSE;
  gint directory_index;

  directory_index = get_directory_index (cache, directory);

  if (directory_index == -1)
    return FALSE;
  
  hash_offset = GET_UINT32 (cache->buffer, 4);
  n_buckets = GET_UINT32 (cache->buffer, hash_offset);

  hash = icon_name_hash (icon_name) % n_buckets;

  chain_offset = GET_UINT32 (cache->buffer, hash_offset + 4 + 4 * hash);
  while (chain_offset != 0xffffffff)
    {
      guint32 name_offset = GET_UINT32 (cache->buffer, chain_offset + 4);
      gchar *name = cache->buffer + name_offset;

      if (strcmp (name, icon_name) == 0)
	{
	  found_icon = TRUE;
	  break;
	}
	  
      chain_offset = GET_UINT32 (cache->buffer, chain_offset);
    }

  if (found_icon)
    {
      guint32 image_list_offset = GET_UINT32 (cache->buffer, chain_offset + 8);
      guint32 n_images =  GET_UINT32 (cache->buffer, image_list_offset);
      guint32 image_offset = image_list_offset + 4;
      gint i;
      for (i = 0; i < n_images; i++)
	{
	  guint16 index = GET_UINT16 (cache->buffer, image_offset);
	  
	  if (index == directory_index)
	    return TRUE;
	  image_offset += 8;
	}
    }

  return FALSE;
}

469 470 471 472 473 474 475 476 477
static void
pixbuf_destroy_cb (guchar   *pixels, 
		   gpointer  data)
{
  GtkIconCache *cache = data;

  _gtk_icon_cache_unref (cache);
}

Anders Carlsson's avatar
Anders Carlsson committed
478 479 480
GdkPixbuf *
_gtk_icon_cache_get_icon (GtkIconCache *cache,
			  const gchar  *icon_name,
481
			  gint          directory_index)
Anders Carlsson's avatar
Anders Carlsson committed
482 483 484 485 486 487 488
{
  guint32 offset, image_data_offset, pixel_data_offset;
  guint32 length, type;
  GdkPixbuf *pixbuf;
  GdkPixdata pixdata;
  GError *error = NULL;

489
  offset = find_image_offset (cache, icon_name, directory_index);
Anders Carlsson's avatar
Anders Carlsson committed
490
  
491 492 493
  if (!offset)
    return NULL;

Anders Carlsson's avatar
Anders Carlsson committed
494 495 496 497 498 499 500 501 502 503 504 505
  image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
  
  if (!image_data_offset)
    return NULL;

  pixel_data_offset = GET_UINT32 (cache->buffer, image_data_offset);

  type = GET_UINT32 (cache->buffer, pixel_data_offset);

  if (type != 0)
    {
      GTK_NOTE (ICONTHEME,
506
		g_print ("invalid pixel data type %u\n", type));
Anders Carlsson's avatar
Anders Carlsson committed
507 508 509 510 511
      return NULL;
    }

  length = GET_UINT32 (cache->buffer, pixel_data_offset + 4);
  
512
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Anders Carlsson's avatar
Anders Carlsson committed
513
  if (!gdk_pixdata_deserialize (&pixdata, length, 
514
				(guchar *)(cache->buffer + pixel_data_offset + 8),
Anders Carlsson's avatar
Anders Carlsson committed
515 516 517 518 519 520 521 522
				&error))
    {
      GTK_NOTE (ICONTHEME,
		g_print ("could not deserialize data: %s\n", error->message));
      g_error_free (error);

      return NULL;
    }
523
G_GNUC_END_IGNORE_DEPRECATIONS
Anders Carlsson's avatar
Anders Carlsson committed
524

525 526 527 528 529
  pixbuf = gdk_pixbuf_new_from_data (pixdata.pixel_data, GDK_COLORSPACE_RGB,
				     (pixdata.pixdata_type & GDK_PIXDATA_COLOR_TYPE_MASK) == GDK_PIXDATA_COLOR_TYPE_RGBA,
				     8, pixdata.width, pixdata.height, pixdata.rowstride,
				     (GdkPixbufDestroyNotify)pixbuf_destroy_cb, 
				     cache);
Anders Carlsson's avatar
Anders Carlsson committed
530 531 532 533 534 535 536 537 538
  if (!pixbuf)
    {
      GTK_NOTE (ICONTHEME,
		g_print ("could not convert pixdata to pixbuf: %s\n", error->message));
      g_error_free (error);

      return NULL;
    }

539 540
  _gtk_icon_cache_ref (cache);

Anders Carlsson's avatar
Anders Carlsson committed
541 542 543 544 545 546
  return pixbuf;
}

GtkIconData  *
_gtk_icon_cache_get_icon_data  (GtkIconCache *cache,
				const gchar  *icon_name,
547
				gint          directory_index)
Anders Carlsson's avatar
Anders Carlsson committed
548 549 550 551 552
{
  guint32 offset, image_data_offset, meta_data_offset;
  GtkIconData *data;
  int i;

553
  offset = find_image_offset (cache, icon_name, directory_index);
Anders Carlsson's avatar
Anders Carlsson committed
554 555 556 557 558 559 560 561 562 563 564 565
  if (!offset)
    return NULL;

  image_data_offset = GET_UINT32 (cache->buffer, offset + 4);
  if (!image_data_offset)
    return NULL;

  meta_data_offset = GET_UINT32 (cache->buffer, image_data_offset + 4);
  
  if (!meta_data_offset)
    return NULL;

566
  data = g_slice_new0 (GtkIconData);
567
  data->ref = 1;
Anders Carlsson's avatar
Anders Carlsson committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

  offset = GET_UINT32 (cache->buffer, meta_data_offset);
  if (offset)
    {
      data->has_embedded_rect = TRUE;
      data->x0 = GET_UINT16 (cache->buffer, offset);
      data->y0 = GET_UINT16 (cache->buffer, offset + 2);
      data->x1 = GET_UINT16 (cache->buffer, offset + 4);
      data->y1 = GET_UINT16 (cache->buffer, offset + 6);
    }

  offset = GET_UINT32 (cache->buffer, meta_data_offset + 4);
  if (offset)
    {
      data->n_attach_points = GET_UINT32 (cache->buffer, offset);
      data->attach_points = g_new (GdkPoint, data->n_attach_points);
      for (i = 0; i < data->n_attach_points; i++)
	{
	  data->attach_points[i].x = GET_UINT16 (cache->buffer, offset + 4 + 4 * i); 
	  data->attach_points[i].y = GET_UINT16 (cache->buffer, offset + 4 + 4 * i + 2); 
	}
    }

  offset = GET_UINT32 (cache->buffer, meta_data_offset + 8);
  if (offset)
    {
      gint n_names;
      gchar *lang, *name;
      gchar **langs;
      GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal);

      n_names = GET_UINT32 (cache->buffer, offset);
      
      for (i = 0; i < n_names; i++)
	{
	  lang = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i);
	  name = cache->buffer + GET_UINT32 (cache->buffer, offset + 4 + 8 * i + 4);
	  
	  g_hash_table_insert (table, lang, name);
	}
      
      langs = (gchar **)g_get_language_names ();
      for (i = 0; langs[i]; i++)
	{
	  name = g_hash_table_lookup (table, langs[i]);
	  if (name)
	    {
	      data->display_name = g_strdup (name);
	      break;
	    }
	}

      g_hash_table_destroy (table);
    }

  return data;
}
625