1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
|
/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* Thumbnail handling according to the Thumbnail Managing Standard.
* https://specifications.freedesktop.org/thumbnail-spec/
*
* Copyright (C) 2001-2003 Sven Neumann <sven@gimp.org>
* Michael Natterer <mitch@gimp.org>
*
* 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 3 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
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#ifdef PLATFORM_OSX
#include <AppKit/AppKit.h>
#endif
#include <gio/gio.h>
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
#include "libgimpbase/gimpwin32-io.h"
#endif
#include "gimpthumb-error.h"
#include "gimpthumb-types.h"
#include "gimpthumb-utils.h"
#include "libgimp/libgimp-intl.h"
/**
* SECTION: gimpthumb-utils
* @title: GimpThumb-utils
* @short_description: Utility functions provided and used by libgimpthumb
*
* Utility functions provided and used by libgimpthumb
**/
static gint gimp_thumb_size (GimpThumbSize size);
static gchar * gimp_thumb_png_lookup (const gchar *name,
const gchar *basedir,
GimpThumbSize *size) G_GNUC_MALLOC;
static const gchar * gimp_thumb_png_name (const gchar *uri);
static void gimp_thumb_exit (void);
static gboolean gimp_thumb_initialized = FALSE;
static gint thumb_num_sizes = 0;
static gint *thumb_sizes = NULL;
static const gchar **thumb_sizenames = NULL;
static gchar *thumb_dir = NULL;
static gchar **thumb_subdirs = NULL;
static gchar *thumb_fail_subdir = NULL;
/**
* gimp_thumb_init:
* @creator: an ASCII string that identifies the thumbnail creator
* @thumb_basedir: an absolute path or %NULL to use the default
*
* This function initializes the thumbnail system. It must be called
* before any other functions from libgimpthumb are used. You may call
* it more than once if you want to change the @thumb_basedir but if
* you do that, you should make sure that no thread is still using the
* library. Apart from this function, libgimpthumb is multi-thread
* safe.
*
* The @creator string must be 7bit ASCII and should contain the name
* of the software that creates the thumbnails. It is used to handle
* thumbnail creation failures. See the spec for more details.
*
* Usually you will pass %NULL for @thumb_basedir. Thumbnails will
* then be stored in the user's personal thumbnail directory as
* defined in the spec. If you wish to use libgimpthumb to store
* application-specific thumbnails, you can specify a different base
* directory here.
*
* Returns: %TRUE if the library was successfully initialized.
**/
gboolean
gimp_thumb_init (const gchar *creator,
const gchar *thumb_basedir)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
guint i;
g_return_val_if_fail (creator != NULL, FALSE);
g_return_val_if_fail (thumb_basedir == NULL ||
g_path_is_absolute (thumb_basedir), FALSE);
if (gimp_thumb_initialized)
gimp_thumb_exit ();
if (thumb_basedir)
{
thumb_dir = g_strdup (thumb_basedir);
}
else
{
#ifdef PLATFORM_OSX
NSAutoreleasePool *pool;
NSArray *path;
NSString *cache_dir;
pool = [[NSAutoreleasePool alloc] init];
path = NSSearchPathForDirectoriesInDomains (NSCachesDirectory,
NSUserDomainMask, YES);
cache_dir = [path objectAtIndex:0];
thumb_dir = g_build_filename ([cache_dir UTF8String], "org.freedesktop.thumbnails",
NULL);
[pool drain];
#else
const gchar *cache_dir = g_get_user_cache_dir ();
if (cache_dir && g_file_test (cache_dir, G_FILE_TEST_IS_DIR))
{
thumb_dir = g_build_filename (cache_dir, "thumbnails", NULL);
}
#endif
if (! thumb_dir)
{
gchar *name = g_filename_display_name (g_get_tmp_dir ());
g_message (_("Cannot determine a valid thumbnails directory.\n"
"Thumbnails will be stored in the folder for "
"temporary files (%s) instead."), name);
g_free (name);
thumb_dir = g_build_filename (g_get_tmp_dir (), ".thumbnails", NULL);
}
}
enum_class = g_type_class_ref (GIMP_TYPE_THUMB_SIZE);
thumb_num_sizes = enum_class->n_values;
thumb_sizes = g_new (gint, thumb_num_sizes);
thumb_sizenames = g_new (const gchar *, thumb_num_sizes);
thumb_subdirs = g_new (gchar *, thumb_num_sizes);
for (i = 0, enum_value = enum_class->values;
i < enum_class->n_values;
i++, enum_value++)
{
thumb_sizes[i] = enum_value->value;
thumb_sizenames[i] = enum_value->value_nick;
thumb_subdirs[i] = g_build_filename (thumb_dir,
enum_value->value_nick, NULL);
}
thumb_fail_subdir = thumb_subdirs[0];
thumb_subdirs[0] = g_build_filename (thumb_fail_subdir, creator, NULL);
g_type_class_unref (enum_class);
gimp_thumb_initialized = TRUE;
return gimp_thumb_initialized;
}
/**
* gimp_thumb_get_thumb_base_dir:
*
* Returns the base directory of thumbnails cache.
* It uses the Freedesktop Thumbnail Managing Standard on UNIX,
* "~/Library/Caches/org.freedesktop.thumbnails" on OSX, and a cache
* folder determined by glib on Windows (currently the common repository
* for temporary Internet files).
* The returned string belongs to GIMP and must not be changed nor freed.
*
* Returns: the thumbnails cache directory.
*
* Since: 2.10
**/
const gchar *
gimp_thumb_get_thumb_base_dir (void)
{
g_return_val_if_fail (gimp_thumb_initialized, NULL);
return thumb_dir;
}
/**
* gimp_thumb_get_thumb_dir:
* @size: a GimpThumbSize
*
* Retrieve the name of the thumbnail folder for a specific size. The
* returned pointer will become invalid if gimp_thumb_init() is used
* again. It must not be changed or freed.
*
* Returns: the thumbnail directory in the encoding of the filesystem
**/
const gchar *
gimp_thumb_get_thumb_dir (GimpThumbSize size)
{
g_return_val_if_fail (gimp_thumb_initialized, NULL);
size = gimp_thumb_size (size);
return thumb_subdirs[size];
}
/**
* gimp_thumb_get_thumb_dir_local:
* @dirname: the basename of the dir, without the actual dirname itself
* @size: a GimpThumbSize
*
* Retrieve the name of the local thumbnail folder for a specific
* size. Unlike gimp_thumb_get_thumb_dir() the returned string is not
* constant and should be free'd when it is not any longer needed.
*
* Returns: the thumbnail directory in the encoding of the filesystem
*
* Since: 2.2
**/
gchar *
gimp_thumb_get_thumb_dir_local (const gchar *dirname,
GimpThumbSize size)
{
g_return_val_if_fail (gimp_thumb_initialized, NULL);
g_return_val_if_fail (dirname != NULL, NULL);
g_return_val_if_fail (size > GIMP_THUMB_SIZE_FAIL, NULL);
size = gimp_thumb_size (size);
return g_build_filename (dirname, thumb_sizenames[size], NULL);
}
/**
* gimp_thumb_ensure_thumb_dir:
* @size: a GimpThumbSize
* @error: return location for possible errors
*
* This function checks if the directory that is required to store
* thumbnails for a particular @size exist and attempts to create it
* if necessary.
*
* You shouldn't have to call this function directly since
* gimp_thumbnail_save_thumb() and gimp_thumbnail_save_failure() will
* do this for you.
*
* Returns: %TRUE is the directory exists, %FALSE if it could not
* be created
**/
gboolean
gimp_thumb_ensure_thumb_dir (GimpThumbSize size,
GError **error)
{
g_return_val_if_fail (gimp_thumb_initialized, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
size = gimp_thumb_size (size);
if (g_file_test (thumb_subdirs[size], G_FILE_TEST_IS_DIR))
return TRUE;
if (g_file_test (thumb_dir, G_FILE_TEST_IS_DIR) ||
(g_mkdir_with_parents (thumb_dir, S_IRUSR | S_IWUSR | S_IXUSR) == 0))
{
if (size == 0)
g_mkdir_with_parents (thumb_fail_subdir, S_IRUSR | S_IWUSR | S_IXUSR);
g_mkdir_with_parents (thumb_subdirs[size], S_IRUSR | S_IWUSR | S_IXUSR);
}
if (g_file_test (thumb_subdirs[size], G_FILE_TEST_IS_DIR))
return TRUE;
g_set_error (error,
GIMP_THUMB_ERROR, GIMP_THUMB_ERROR_MKDIR,
_("Failed to create thumbnail folder '%s'."),
thumb_subdirs[size]);
return FALSE;
}
/**
* gimp_thumb_ensure_thumb_dir_local:
* @dirname: the basename of the dir, without the actual dirname itself
* @size: a GimpThumbSize
* @error: return location for possible errors
*
* This function checks if the directory that is required to store
* local thumbnails for a particular @size exist and attempts to
* create it if necessary.
*
* You shouldn't have to call this function directly since
* gimp_thumbnail_save_thumb_local() will do this for you.
*
* Returns: %TRUE is the directory exists, %FALSE if it could not
* be created
*
* Since: 2.2
**/
gboolean
gimp_thumb_ensure_thumb_dir_local (const gchar *dirname,
GimpThumbSize size,
GError **error)
{
gchar *basedir;
gchar *subdir;
g_return_val_if_fail (gimp_thumb_initialized, FALSE);
g_return_val_if_fail (dirname != NULL, FALSE);
g_return_val_if_fail (g_path_is_absolute (dirname), FALSE);
g_return_val_if_fail (size > GIMP_THUMB_SIZE_FAIL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
size = gimp_thumb_size (size);
subdir = g_build_filename (dirname,
".thumblocal", thumb_sizenames[size],
NULL);
if (g_file_test (subdir, G_FILE_TEST_IS_DIR))
{
g_free (subdir);
return TRUE;
}
basedir = g_build_filename (dirname, ".thumblocal", NULL);
if (g_file_test (basedir, G_FILE_TEST_IS_DIR) ||
(g_mkdir (thumb_dir, S_IRUSR | S_IWUSR | S_IXUSR) == 0))
{
g_mkdir (subdir, S_IRUSR | S_IWUSR | S_IXUSR);
}
g_free (basedir);
if (g_file_test (subdir, G_FILE_TEST_IS_DIR))
{
g_free (subdir);
return TRUE;
}
g_set_error (error,
GIMP_THUMB_ERROR, GIMP_THUMB_ERROR_MKDIR,
_("Failed to create thumbnail folder '%s'."),
subdir);
g_free (subdir);
return FALSE;
}
/**
* gimp_thumb_name_from_uri:
* @uri: an escaped URI
* @size: a #GimpThumbSize
*
* Creates the name of the thumbnail file of the specified @size that
* belongs to an image file located at the given @uri.
*
* Returns: a newly allocated filename in the encoding of the
* filesystem or %NULL if @uri points to the user's
* thumbnail repository.
**/
gchar *
gimp_thumb_name_from_uri (const gchar *uri,
GimpThumbSize size)
{
g_return_val_if_fail (gimp_thumb_initialized, NULL);
g_return_val_if_fail (uri != NULL, NULL);
if (strstr (uri, thumb_dir))
return NULL;
size = gimp_thumb_size (size);
return g_build_filename (thumb_subdirs[size],
gimp_thumb_png_name (uri),
NULL);
}
/**
* gimp_thumb_name_from_uri_local:
* @uri: an escaped URI
* @size: a #GimpThumbSize
*
* Creates the name of a local thumbnail file of the specified @size
* that belongs to an image file located at the given @uri. Local
* thumbnails have been introduced with version 0.7 of the spec.
*
* Returns: a newly allocated filename in the encoding of the
* filesystem or %NULL if @uri is a remote file or
* points to the user's thumbnail repository.
*
* Since: 2.2
**/
gchar *
gimp_thumb_name_from_uri_local (const gchar *uri,
GimpThumbSize size)
{
gchar *filename;
gchar *result = NULL;
g_return_val_if_fail (gimp_thumb_initialized, NULL);
g_return_val_if_fail (uri != NULL, NULL);
g_return_val_if_fail (size > GIMP_THUMB_SIZE_FAIL, NULL);
if (strstr (uri, thumb_dir))
return NULL;
filename = _gimp_thumb_filename_from_uri (uri);
if (filename)
{
const gchar *baseuri = strrchr (uri, '/');
if (baseuri && baseuri[0] && baseuri[1])
{
gchar *dirname = g_path_get_dirname (filename);
gint i = gimp_thumb_size (size);
result = g_build_filename (dirname,
".thumblocal", thumb_sizenames[i],
gimp_thumb_png_name (uri),
NULL);
g_free (dirname);
}
g_free (filename);
}
return result;
}
/**
* gimp_thumb_find_thumb:
* @uri: an escaped URI
* @size: pointer to a #GimpThumbSize
*
* This function attempts to locate a thumbnail for the given
* @uri. First it tries the size that is stored at @size. If no
* thumbnail of that size is found, it will look for a larger
* thumbnail, then falling back to a smaller size.
*
* If the user's thumbnail repository doesn't provide a thumbnail but
* a local thumbnail repository exists for the folder the image is
* located in, the same search is done among the local thumbnails (if
* there are any).
*
* If a thumbnail is found, it's size is written to the variable
* pointer to by @size and the file location is returned.
*
* Returns: a newly allocated string in the encoding of the
* filesystem or %NULL if no thumbnail for @uri was found
**/
gchar *
gimp_thumb_find_thumb (const gchar *uri,
GimpThumbSize *size)
{
gchar *result;
g_return_val_if_fail (gimp_thumb_initialized, NULL);
g_return_val_if_fail (uri != NULL, NULL);
g_return_val_if_fail (size != NULL, NULL);
g_return_val_if_fail (*size > GIMP_THUMB_SIZE_FAIL, NULL);
result = gimp_thumb_png_lookup (gimp_thumb_png_name (uri), NULL, size);
if (! result)
{
gchar *filename = _gimp_thumb_filename_from_uri (uri);
if (filename)
{
const gchar *baseuri = strrchr (uri, '/');
if (baseuri && baseuri[0] && baseuri[1])
{
gchar *dirname = g_path_get_dirname (filename);
result = gimp_thumb_png_lookup (gimp_thumb_png_name (baseuri + 1),
dirname, size);
g_free (dirname);
}
g_free (filename);
}
}
return result;
}
/**
* gimp_thumb_file_test:
* @filename: a filename in the encoding of the filesystem
* @mtime: return location for modification time
* @size: return location for file size
* @err_no: return location for system "errno"
*
* This is a convenience and portability wrapper around stat(). It
* checks if the given @filename exists and returns modification time
* and file size in 64bit integer values.
*
* Returns: The type of the file, or #GIMP_THUMB_FILE_TYPE_NONE if
* the file doesn't exist.
**/
GimpThumbFileType
gimp_thumb_file_test (const gchar *filename,
gint64 *mtime,
gint64 *size,
gint *err_no)
{
GimpThumbFileType type = GIMP_THUMB_FILE_TYPE_NONE;
GFile *file;
GFileInfo *info;
g_return_val_if_fail (filename != NULL, FALSE);
file = g_file_new_for_path (filename);
info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
G_FILE_ATTRIBUTE_STANDARD_SIZE ","
G_FILE_ATTRIBUTE_TIME_MODIFIED,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (info)
{
if (mtime)
*mtime =
g_file_info_get_attribute_uint64 (info,
G_FILE_ATTRIBUTE_TIME_MODIFIED);
if (size)
*size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
if (err_no)
*err_no = 0;
switch (g_file_info_get_attribute_uint32 (info,
G_FILE_ATTRIBUTE_STANDARD_TYPE))
{
case G_FILE_TYPE_REGULAR:
type = GIMP_THUMB_FILE_TYPE_REGULAR;
break;
case G_FILE_TYPE_DIRECTORY:
type = GIMP_THUMB_FILE_TYPE_FOLDER;
break;
default:
type = GIMP_THUMB_FILE_TYPE_SPECIAL;
break;
}
g_object_unref (info);
}
else
{
if (mtime) *mtime = 0;
if (size) *size = 0;
if (err_no) *err_no = ENOENT;
}
g_object_unref (file);
return type;
}
/**
* gimp_thumbs_delete_for_uri:
* @uri: an escaped URI
*
* Deletes all thumbnails for the image file specified by @uri from the
* user's thumbnail repository.
*
* Since: 2.2
**/
void
gimp_thumbs_delete_for_uri (const gchar *uri)
{
gint i;
g_return_if_fail (gimp_thumb_initialized);
g_return_if_fail (uri != NULL);
for (i = 0; i < thumb_num_sizes; i++)
{
gchar *filename = gimp_thumb_name_from_uri (uri, thumb_sizes[i]);
if (filename)
{
g_unlink (filename);
g_free (filename);
}
}
}
/**
* gimp_thumbs_delete_for_uri_local:
* @uri: an escaped URI
*
* Deletes all thumbnails for the image file specified by @uri from
* the local thumbnail repository.
*
* Since: 2.2
**/
void
gimp_thumbs_delete_for_uri_local (const gchar *uri)
{
gint i;
g_return_if_fail (gimp_thumb_initialized);
g_return_if_fail (uri != NULL);
for (i = 0; i < thumb_num_sizes; i++)
{
gchar *filename = gimp_thumb_name_from_uri_local (uri, thumb_sizes[i]);
if (filename)
{
g_unlink (filename);
g_free (filename);
}
}
}
void
_gimp_thumbs_delete_others (const gchar *uri,
GimpThumbSize size)
{
gint i;
g_return_if_fail (gimp_thumb_initialized);
g_return_if_fail (uri != NULL);
size = gimp_thumb_size (size);
for (i = 0; i < thumb_num_sizes; i++)
{
gchar *filename;
if (i == size)
continue;
filename = gimp_thumb_name_from_uri (uri, thumb_sizes[i]);
if (filename)
{
g_unlink (filename);
g_free (filename);
}
}
}
gchar *
_gimp_thumb_filename_from_uri (const gchar *uri)
{
gchar *filename;
gchar *hostname;
g_return_val_if_fail (uri != NULL, NULL);
filename = g_filename_from_uri (uri, &hostname, NULL);
if (!filename)
return NULL;
if (hostname)
{
/* we have a file: URI with a hostname */
#ifdef G_OS_WIN32
/* on Win32, create a valid UNC path and use it as the filename */
gchar *tmp = g_build_filename ("//", hostname, filename, NULL);
g_free (filename);
filename = tmp;
#else
/* otherwise return NULL, caller should use URI then */
g_free (filename);
filename = NULL;
#endif
g_free (hostname);
}
return filename;
}
static void
gimp_thumb_exit (void)
{
gint i;
g_free (thumb_dir);
g_free (thumb_sizes);
g_free (thumb_sizenames);
for (i = 0; i < thumb_num_sizes; i++)
g_free (thumb_subdirs[i]);
g_free (thumb_subdirs);
g_free (thumb_fail_subdir);
thumb_num_sizes = 0;
thumb_sizes = NULL;
thumb_sizenames = NULL;
thumb_dir = NULL;
thumb_subdirs = NULL;
thumb_fail_subdir = NULL;
gimp_thumb_initialized = FALSE;
}
static gint
gimp_thumb_size (GimpThumbSize size)
{
gint i = 0;
if (size > GIMP_THUMB_SIZE_FAIL)
{
for (i = 1;
i < thumb_num_sizes && thumb_sizes[i] < size;
i++)
/* nothing */;
if (i == thumb_num_sizes)
i--;
}
return i;
}
static gchar *
gimp_thumb_png_lookup (const gchar *name,
const gchar *basedir,
GimpThumbSize *size)
{
gchar *thumb_name = NULL;
gchar **subdirs = NULL;
gint i, n;
if (basedir)
{
gchar *dir = g_build_filename (basedir, ".thumblocal", NULL);
if (g_file_test (basedir, G_FILE_TEST_IS_DIR))
{
gint i;
subdirs = g_new (gchar *, thumb_num_sizes);
subdirs[0] = NULL; /* GIMP_THUMB_SIZE_FAIL */
for (i = 1; i < thumb_num_sizes; i++)
subdirs[i] = g_build_filename (dir, thumb_sizenames[i], NULL);
}
g_free (dir);
}
else
{
subdirs = thumb_subdirs;
}
if (! subdirs)
return NULL;
i = n = gimp_thumb_size (*size);
for (; i < thumb_num_sizes; i++)
{
if (! subdirs[i])
continue;
thumb_name = g_build_filename (subdirs[i], name, NULL);
if (gimp_thumb_file_test (thumb_name,
NULL, NULL,
NULL) == GIMP_THUMB_FILE_TYPE_REGULAR)
{
*size = thumb_sizes[i];
goto finish;
}
g_free (thumb_name);
}
for (i = n - 1; i >= 0; i--)
{
if (! subdirs[i])
continue;
thumb_name = g_build_filename (subdirs[i], name, NULL);
if (gimp_thumb_file_test (thumb_name,
NULL, NULL,
NULL) == GIMP_THUMB_FILE_TYPE_REGULAR)
{
*size = thumb_sizes[i];
goto finish;
}
g_free (thumb_name);
}
thumb_name = NULL;
finish:
if (basedir)
{
for (i = 0; i < thumb_num_sizes; i++)
g_free (subdirs[i]);
g_free (subdirs);
}
return thumb_name;
}
static const gchar *
gimp_thumb_png_name (const gchar *uri)
{
static gchar name[40];
GChecksum *checksum;
guchar digest[16];
gsize len = sizeof (digest);
gsize i;
checksum = g_checksum_new (G_CHECKSUM_MD5);
g_checksum_update (checksum, (const guchar *) uri, -1);
g_checksum_get_digest (checksum, digest, &len);
g_checksum_free (checksum);
for (i = 0; i < len; i++)
{
guchar n;
n = (digest[i] >> 4) & 0xF;
name[i * 2] = (n > 9) ? 'a' + n - 10 : '0' + n;
n = digest[i] & 0xF;
name[i * 2 + 1] = (n > 9) ? 'a' + n - 10 : '0' + n;
}
strncpy (name + 32, ".png", 5);
return (const gchar *) name;
}
|