gimpthumbnail.c 42.3 KB
Newer Older
1 2 3 4 5 6
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * Thumbnail handling according to the Thumbnail Managing Standard.
 * http://triq.net/~pearl/thumbnail-spec/
 *
7
 * Copyright (C) 2001-2004  Sven Neumann <sven@gimp.org>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *                          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 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
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
29 30
#include <errno.h>
#include <sys/types.h>
31

32 33 34
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
35

36
#include <glib/gstdio.h>
37 38
#include <gdk-pixbuf/gdk-pixbuf.h>

39
#ifdef G_OS_WIN32
40
#include "libgimpbase/gimpwin32-io.h"
41 42
#include <process.h>
#define _getpid getpid
43 44
#endif

45 46 47 48
#include "gimpthumb-types.h"
#include "gimpthumb-utils.h"
#include "gimpthumbnail.h"

49 50
#include "libgimp/libgimp-intl.h"

51

52
/*  #define GIMP_THUMB_DEBUG  */
Sven Neumann's avatar
Sven Neumann committed
53

54 55 56 57 58 59 60 61

#if defined (GIMP_THUMB_DEBUG) && defined (__GNUC__)
#define GIMP_THUMB_DEBUG_CALL(t) \
        g_printerr ("%s: %s\n", \
                     __FUNCTION__, t->image_uri ? t->image_uri : "(null)")
#else
#define GIMP_THUMB_DEBUG_CALL(t) ((void)(0))
#endif
62 63


Sven Neumann's avatar
Sven Neumann committed
64 65 66 67 68 69 70 71 72 73
#define TAG_DESCRIPTION           "tEXt::Description"
#define TAG_SOFTWARE              "tEXt::Software"
#define TAG_THUMB_URI             "tEXt::Thumb::URI"
#define TAG_THUMB_MTIME           "tEXt::Thumb::MTime"
#define TAG_THUMB_FILESIZE        "tEXt::Thumb::Size"
#define TAG_THUMB_IMAGE_WIDTH     "tEXt::Thumb::Image::Width"
#define TAG_THUMB_IMAGE_HEIGHT    "tEXt::Thumb::Image::Height"
#define TAG_THUMB_IMAGE_MIMETYPE  "tEXt::Thumb::Image::Mimetype"
#define TAG_THUMB_GIMP_TYPE       "tEXt::Thumb::X-GIMP::Type"
#define TAG_THUMB_GIMP_LAYERS     "tEXt::Thumb::X-GIMP::Layers"
74 75 76 77 78


enum
{
  PROP_0,
79 80 81 82 83 84
  PROP_IMAGE_STATE,
  PROP_IMAGE_URI,
  PROP_IMAGE_MTIME,
  PROP_IMAGE_FILESIZE,
  PROP_IMAGE_WIDTH,
  PROP_IMAGE_HEIGHT,
Sven Neumann's avatar
Sven Neumann committed
85
  PROP_IMAGE_MIMETYPE,
86 87 88
  PROP_IMAGE_TYPE,
  PROP_IMAGE_NUM_LAYERS,
  PROP_THUMB_STATE
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
static void      gimp_thumbnail_class_init   (GimpThumbnailClass *klass);
static void      gimp_thumbnail_init         (GimpThumbnail      *thumbnail);
static void      gimp_thumbnail_finalize     (GObject            *object);
static void      gimp_thumbnail_set_property (GObject            *object,
                                              guint               property_id,
                                              const GValue       *value,
                                              GParamSpec         *pspec);
static void      gimp_thumbnail_get_property (GObject            *object,
                                              guint               property_id,
                                              GValue             *value,
                                              GParamSpec         *pspec);
static void      gimp_thumbnail_reset_info   (GimpThumbnail      *thumbnail);

static void      gimp_thumbnail_update_image (GimpThumbnail      *thumbnail);
static void      gimp_thumbnail_update_thumb (GimpThumbnail      *thumbnail,
                                              GimpThumbSize       size);

static gboolean  gimp_thumbnail_save         (GimpThumbnail      *thumbnail,
                                              GimpThumbSize       size,
                                              const gchar        *filename,
                                              GdkPixbuf          *pixbuf,
                                              const gchar        *software,
                                              GError            **error);
115
#ifdef GIMP_THUMB_DEBUG
116 117
static void      gimp_thumbnail_debug_notify (GObject            *object,
                                              GParamSpec         *pspec);
118 119
#endif

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

static GObjectClass *parent_class = NULL;


GType
gimp_thumbnail_get_type (void)
{
  static GType thumbnail_type = 0;

  if (!thumbnail_type)
    {
      static const GTypeInfo thumbnail_info =
      {
        sizeof (GimpThumbnailClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_thumbnail_class_init,
	NULL,           /* class_finalize */
	NULL,		/* class_data     */
	sizeof (GimpThumbnail),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_thumbnail_init,
      };

      thumbnail_type = g_type_register_static (G_TYPE_OBJECT,
					       "GimpThumbnail",
                                               &thumbnail_info, 0);
    }

  return thumbnail_type;
}

static void
gimp_thumbnail_class_init (GimpThumbnailClass *klass)
{
  GObjectClass *object_class;

  parent_class = g_type_class_peek_parent (klass);

  object_class = G_OBJECT_CLASS (klass);

  object_class->finalize     = gimp_thumbnail_finalize;
  object_class->set_property = gimp_thumbnail_set_property;
  object_class->get_property = gimp_thumbnail_get_property;

  g_object_class_install_property (object_class,
166 167
				   PROP_IMAGE_STATE,
				   g_param_spec_enum ("image-state", NULL,
Sven Neumann's avatar
Sven Neumann committed
168
                                                      "State of the image associated to the thumbnail object",
169 170 171 172
                                                      GIMP_TYPE_THUMB_STATE,
                                                      GIMP_THUMB_STATE_UNKNOWN,
                                                      G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
173 174
				   PROP_IMAGE_URI,
				   g_param_spec_string ("image-uri", NULL,
Sven Neumann's avatar
Sven Neumann committed
175
                                                       "URI of the image file",
176 177 178 179
							NULL,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_IMAGE_MTIME,
180
				   g_param_spec_int64 ("image-mtime", NULL,
Sven Neumann's avatar
Sven Neumann committed
181
                                                       "Modification time of the image file in seconds since the Epoch",
182 183 184
                                                       0, G_MAXINT64, 0,
                                                       G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
185
				   PROP_IMAGE_FILESIZE,
186
				   g_param_spec_int64 ("image-filesize", NULL,
Sven Neumann's avatar
Sven Neumann committed
187
                                                       "Size of the image file in bytes",
188 189 190
                                                       0, G_MAXINT64, 0,
                                                       G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
191
				   PROP_IMAGE_WIDTH,
192
				   g_param_spec_int ("image-width", NULL,
Sven Neumann's avatar
Sven Neumann committed
193
                                                     "Width of the image in pixels",
194 195 196
                                                     0, G_MAXINT, 0,
                                                     G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
197
				   PROP_IMAGE_HEIGHT,
198
				   g_param_spec_int ("image-height", NULL,
Sven Neumann's avatar
Sven Neumann committed
199
                                                     "Height of the image in pixels",
200 201
                                                     0, G_MAXINT, 0,
                                                     G_PARAM_READWRITE));
Sven Neumann's avatar
Sven Neumann committed
202 203 204 205 206 207 208 209 210 211 212
  /**
   * GimpThumbnail::image-mimetype:
   *
   * Since: GIMP 2.2
   **/
  g_object_class_install_property (object_class,
				   PROP_IMAGE_MIMETYPE,
				   g_param_spec_string ("image-mimetype", NULL,
                                                        "Image mimetype",
                                                        NULL,
                                                        G_PARAM_READWRITE));
213
  g_object_class_install_property (object_class,
214
                                   PROP_IMAGE_TYPE,
215
                                   g_param_spec_string ("image-type", NULL,
Sven Neumann's avatar
Sven Neumann committed
216
                                                        "String describing the type of the image format",
217 218 219
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
220
                                   PROP_IMAGE_NUM_LAYERS,
221
                                   g_param_spec_int ("image-num-layers", NULL,
Sven Neumann's avatar
Sven Neumann committed
222
                                                     "The number of layers in the image",
223 224
                                                     0, G_MAXINT, 0,
                                                     G_PARAM_READWRITE));
225 226 227
  g_object_class_install_property (object_class,
				   PROP_THUMB_STATE,
				   g_param_spec_enum ("thumb-state", NULL,
Sven Neumann's avatar
Sven Neumann committed
228
                                                      "State of the thumbnail file",
229 230 231
                                                      GIMP_TYPE_THUMB_STATE,
                                                      GIMP_THUMB_STATE_UNKNOWN,
                                                      G_PARAM_READWRITE));
232 233 234 235 236
}

static void
gimp_thumbnail_init (GimpThumbnail *thumbnail)
{
237 238
  thumbnail->image_state      = GIMP_THUMB_STATE_UNKNOWN;
  thumbnail->image_uri        = NULL;
239 240 241 242 243
  thumbnail->image_filename   = NULL;
  thumbnail->image_mtime      = 0;
  thumbnail->image_filesize   = 0;
  thumbnail->image_width      = 0;
  thumbnail->image_height     = 0;
Sven Neumann's avatar
Sven Neumann committed
244
  thumbnail->image_mimetype   = NULL;
245
  thumbnail->image_type       = NULL;
246
  thumbnail->image_num_layers = 0;
247 248

  thumbnail->thumb_state      = GIMP_THUMB_STATE_UNKNOWN;
249
  thumbnail->thumb_size       = -1;
250 251 252
  thumbnail->thumb_filename   = NULL;
  thumbnail->thumb_mtime      = 0;
  thumbnail->thumb_filesize   = 0;
253

254
#ifdef GIMP_THUMB_DEBUG
255 256 257 258
  g_signal_connect (thumbnail, "notify",
                    G_CALLBACK (gimp_thumbnail_debug_notify),
                    NULL);
#endif
259 260 261 262 263 264 265
}

static void
gimp_thumbnail_finalize (GObject *object)
{
  GimpThumbnail *thumbnail = GIMP_THUMBNAIL (object);

266
  if (thumbnail->image_uri)
267
    {
268 269
      g_free (thumbnail->image_uri);
      thumbnail->image_uri = NULL;
270 271 272 273 274 275
    }
  if (thumbnail->image_filename)
    {
      g_free (thumbnail->image_filename);
      thumbnail->image_filename = NULL;
    }
Sven Neumann's avatar
Sven Neumann committed
276 277 278 279 280
  if (thumbnail->image_mimetype)
    {
      g_free (thumbnail->image_mimetype);
      thumbnail->image_mimetype = NULL;
    }
281 282 283 284 285
  if (thumbnail->image_type)
    {
      g_free (thumbnail->image_type);
      thumbnail->image_type = NULL;
    }
286 287 288 289 290
  if (thumbnail->thumb_filename)
    {
      g_free (thumbnail->thumb_filename);
      thumbnail->thumb_filename = NULL;
    }
291 292 293 294 295 296 297 298 299 300 301 302 303 304

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_thumbnail_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GimpThumbnail *thumbnail = GIMP_THUMBNAIL (object);

  switch (property_id)
    {
305 306 307 308
    case PROP_IMAGE_STATE:
      thumbnail->image_state = g_value_get_enum (value);
      break;
    case PROP_IMAGE_URI:
309 310 311
      gimp_thumbnail_set_uri (GIMP_THUMBNAIL (object),
                              g_value_get_string (value));
      break;
312
    case PROP_IMAGE_MTIME:
313
      thumbnail->image_mtime = g_value_get_int64 (value);
314
      break;
315
    case PROP_IMAGE_FILESIZE:
316
      thumbnail->image_filesize = g_value_get_int64 (value);
317
      break;
318
    case PROP_IMAGE_WIDTH:
319 320
      thumbnail->image_width = g_value_get_int (value);
      break;
321
    case PROP_IMAGE_HEIGHT:
322 323
      thumbnail->image_height = g_value_get_int (value);
      break;
Sven Neumann's avatar
Sven Neumann committed
324 325 326 327
    case PROP_IMAGE_MIMETYPE:
      g_free (thumbnail->image_mimetype);
      thumbnail->image_mimetype = g_value_dup_string (value);
      break;
328
    case PROP_IMAGE_TYPE:
329 330 331
      g_free (thumbnail->image_type);
      thumbnail->image_type = g_value_dup_string (value);
      break;
332
    case PROP_IMAGE_NUM_LAYERS:
333 334
      thumbnail->image_num_layers = g_value_get_int (value);
      break;
335 336 337
    case PROP_THUMB_STATE:
      thumbnail->thumb_state = g_value_get_enum (value);
      break;
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_thumbnail_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GimpThumbnail *thumbnail = GIMP_THUMBNAIL (object);

  switch (property_id)
    {
355 356
    case PROP_IMAGE_STATE:
      g_value_set_enum (value, thumbnail->image_state);
357
      break;
358 359
    case PROP_IMAGE_URI:
      g_value_set_string (value, thumbnail->image_uri);
360
      break;
361
    case PROP_IMAGE_MTIME:
362 363
      g_value_set_int64 (value, thumbnail->image_mtime);
      break;
364
    case PROP_IMAGE_FILESIZE:
365 366
      g_value_set_int64 (value, thumbnail->image_filesize);
      break;
367
    case PROP_IMAGE_WIDTH:
368 369
      g_value_set_int (value, thumbnail->image_width);
      break;
370
    case PROP_IMAGE_HEIGHT:
371 372
      g_value_set_int (value, thumbnail->image_height);
      break;
Sven Neumann's avatar
Sven Neumann committed
373 374 375
    case PROP_IMAGE_MIMETYPE:
      g_value_set_string (value, thumbnail->image_mimetype);
      break;
376
    case PROP_IMAGE_TYPE:
377 378
      g_value_set_string (value, thumbnail->image_type);
      break;
379
    case PROP_IMAGE_NUM_LAYERS:
380
      g_value_set_int (value, thumbnail->image_num_layers);
381
      break;
382 383 384
    case PROP_THUMB_STATE:
      g_value_set_enum (value, thumbnail->thumb_state);
      break;
385 386 387 388 389 390 391

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

Sven Neumann's avatar
Sven Neumann committed
392 393 394 395 396 397 398
/**
 * gimp_thumbnail_new:
 *
 * Creates a new #GimpThumbnail object.
 *
 * Return value: a newly allocated GimpThumbnail object
 **/
399 400 401 402 403 404
GimpThumbnail *
gimp_thumbnail_new (void)
{
  return g_object_new (GIMP_TYPE_THUMBNAIL, NULL);
}

Sven Neumann's avatar
Sven Neumann committed
405 406 407
/**
 * gimp_thumbnail_set_uri:
 * @thumbnail: a #GimpThumbnail object
408
 * @uri: an escaped URI
Sven Neumann's avatar
Sven Neumann committed
409 410 411 412 413
 *
 * Sets the location of the image file associated with the #thumbnail.
 *
 * All informations stored in the #GimpThumbnail are reset.
 **/
414 415 416 417 418 419
void
gimp_thumbnail_set_uri (GimpThumbnail *thumbnail,
                        const gchar   *uri)
{
  g_return_if_fail (GIMP_IS_THUMBNAIL (thumbnail));

420 421
  GIMP_THUMB_DEBUG_CALL (thumbnail);

Sven Neumann's avatar
Sven Neumann committed
422 423 424 425 426 427 428 429 430 431 432
  if (thumbnail->image_uri)
    g_free (thumbnail->image_uri);

  thumbnail->image_uri = g_strdup (uri);

  if (thumbnail->image_filename)
    {
      g_free (thumbnail->image_filename);
      thumbnail->image_filename = NULL;
    }

433 434 435 436 437 438 439 440 441 442
  if (thumbnail->thumb_filename)
    {
      g_free (thumbnail->thumb_filename);
      thumbnail->thumb_filename = NULL;
    }

  thumbnail->thumb_size     = -1;
  thumbnail->thumb_filesize = 0;
  thumbnail->thumb_mtime    = 0;

443 444
  g_object_set (thumbnail,
                "image-state",      GIMP_THUMB_STATE_UNKNOWN,
445 446
                "image-filesize",   (gint64) 0,
                "image-mtime",      (gint64) 0,
447 448
                "image-width",      0,
                "image-height",     0,
Sven Neumann's avatar
Sven Neumann committed
449
                "image-mimetype",   NULL,
450 451
                "image-type",       NULL,
                "image-num-layers", 0,
452
                "thumb-state",      GIMP_THUMB_STATE_UNKNOWN,
453 454 455
                NULL);
}

Sven Neumann's avatar
Sven Neumann committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/**
 * gimp_thumbnail_set_filename:
 * @thumbnail: a #GimpThumbnail object
 * @filename: a local filename in the encoding of the filesystem
 * @error: return location for possible errors
 *
 * Sets the location of the image file associated with the #thumbnail.
 *
 * Return value: %TRUE if the filename was successfully set,
 *               %FALSE otherwise
 **/
gboolean
gimp_thumbnail_set_filename (GimpThumbnail  *thumbnail,
                             const gchar    *filename,
                             GError        **error)
{
  gchar *uri = NULL;

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

477 478
  GIMP_THUMB_DEBUG_CALL (thumbnail);

Sven Neumann's avatar
Sven Neumann committed
479 480 481 482 483 484 485 486 487 488
  if (filename)
    uri = g_filename_to_uri (filename, NULL, error);

  gimp_thumbnail_set_uri (thumbnail, uri);

  g_free (uri);

  return (!filename || uri);
}

489 490 491 492 493 494 495 496 497 498 499
/**
 * gimp_thumbnail_set_from_thumb:
 * @thumbnail: a #GimpThumbnail object
 * @filename: filename of a local thumbnail file
 * @error: return location for possible errors
 *
 * This function tries to load the thumbnail file pointed to by
 * @filename and retrieves the URI of the original image file from
 * it. This allows you to find the image file associated with a
 * thumbnail file.
 *
500 501 502
 * This will only work with thumbnails from the global thumbnail
 * directory that contain a valid Thumb::URI tag.
 *
503 504 505 506 507 508 509 510 511 512 513 514 515
 * Return value: %TRUE if the pixbuf could be loaded, %FALSE otherwise
 **/
gboolean
gimp_thumbnail_set_from_thumb (GimpThumbnail  *thumbnail,
                               const gchar    *filename,
                               GError        **error)
{
  GdkPixbuf *pixbuf;

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

516 517
  GIMP_THUMB_DEBUG_CALL (thumbnail);

518 519 520 521 522 523 524 525 526 527 528 529
  pixbuf = gdk_pixbuf_new_from_file (filename, error);
  if (! pixbuf)
    return FALSE;

  gimp_thumbnail_set_uri (thumbnail,
                          gdk_pixbuf_get_option (pixbuf, TAG_THUMB_URI));

  g_object_unref (pixbuf);

  return TRUE;
}

Sven Neumann's avatar
Sven Neumann committed
530 531 532 533 534 535 536 537 538
/**
 * gimp_thumbnail_peek_image:
 * @thumbnail: a #GimpThumbnail object
 *
 * Checks the image file associated with the @thumbnail and updates
 * information such as state, filesize and modification time.
 *
 * Return value: the image's #GimpThumbState after the update
 **/
539 540 541 542 543 544
GimpThumbState
gimp_thumbnail_peek_image (GimpThumbnail *thumbnail)
{
  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail),
                        GIMP_THUMB_STATE_UNKNOWN);

545 546
  GIMP_THUMB_DEBUG_CALL (thumbnail);

547 548 549 550 551 552 553 554 555
  g_object_freeze_notify (G_OBJECT (thumbnail));

  gimp_thumbnail_update_image (thumbnail);

  g_object_thaw_notify (G_OBJECT (thumbnail));

  return thumbnail->image_state;
}

Sven Neumann's avatar
Sven Neumann committed
556 557 558 559 560 561 562 563 564 565
/**
 * gimp_thumbnail_peek_thumb:
 * @thumbnail: a #GimpThumbnail object
 * @size: the preferred size of the thumbnail image
 *
 * Checks if a thumbnail file for the @thumbnail exists. It doesn't
 * load the thumbnail image and thus cannot check if the thumbnail is
 * valid and uptodate for the image file asosciated with the
 * @thumbnail.
 *
566 567 568 569
 * If you want to check the thumbnail, either attempt to load it using
 * gimp_thumbnail_load_thumb(), or, if you don't need the resulting
 * thumbnail pixbuf, use gimp_thumbnail_check_thumb().
 *
Sven Neumann's avatar
Sven Neumann committed
570 571
 * Return value: the thumbnail's #GimpThumbState after the update
 **/
572 573 574
GimpThumbState
gimp_thumbnail_peek_thumb (GimpThumbnail *thumbnail,
                           GimpThumbSize  size)
575
{
576 577 578
  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail),
                        GIMP_THUMB_STATE_UNKNOWN);

579 580
  GIMP_THUMB_DEBUG_CALL (thumbnail);

581 582 583
  g_object_freeze_notify (G_OBJECT (thumbnail));

  gimp_thumbnail_update_image (thumbnail);
584
  gimp_thumbnail_update_thumb (thumbnail, size);
585 586

  g_object_thaw_notify (G_OBJECT (thumbnail));
587 588

  return thumbnail->thumb_state;
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
/**
 * gimp_thumbnail_check_thumb:
 * @thumbnail: a #GimpThumbnail object
 * @size: the preferred size of the thumbnail image
 *
 * Checks if a thumbnail file for the @thumbnail exists, loads it and
 * verifies it is valid and uptodate for the image file asosciated
 * with the @thumbnail.
 *
 * Return value: the thumbnail's #GimpThumbState after the update
 *
 * Since: GIMP 2.2
 **/
GimpThumbState
gimp_thumbnail_check_thumb (GimpThumbnail *thumbnail,
                            GimpThumbSize  size)
{
  GdkPixbuf *pixbuf;

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);

  GIMP_THUMB_DEBUG_CALL (thumbnail);

  if (gimp_thumbnail_peek_thumb (thumbnail, size) == GIMP_THUMB_STATE_OK)
    return GIMP_THUMB_STATE_OK;

  pixbuf = gimp_thumbnail_load_thumb (thumbnail, size, NULL);

  if (pixbuf)
    g_object_unref (pixbuf);

  return thumbnail->thumb_state;
}

625 626
static void
gimp_thumbnail_update_image (GimpThumbnail *thumbnail)
627
{
628 629
  GimpThumbState  state;
  gint64          mtime    = 0;
630
  gint64          filesize = 0;
631

Sven Neumann's avatar
Sven Neumann committed
632 633
  if (! thumbnail->image_uri)
    return;
634

635
  state = thumbnail->image_state;
636 637 638 639 640 641

  switch (state)
    {
    case GIMP_THUMB_STATE_UNKNOWN:
      g_return_if_fail (thumbnail->image_filename == NULL);

642
      thumbnail->image_filename = g_filename_from_uri (thumbnail->image_uri,
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
                                                       NULL, NULL);

      if (! thumbnail->image_filename)
        state = GIMP_THUMB_STATE_REMOTE;

      break;

    case GIMP_THUMB_STATE_REMOTE:
      break;

    default:
      g_return_if_fail (thumbnail->image_filename != NULL);
      break;
    }

  switch (state)
    {
    case GIMP_THUMB_STATE_REMOTE:
      break;

    default:
664
      switch (gimp_thumb_file_test (thumbnail->image_filename,
665 666
                                    &mtime, &filesize,
                                    &thumbnail->image_not_found_errno))
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
        {
        case GIMP_THUMB_FILE_TYPE_REGULAR:
          state = GIMP_THUMB_STATE_EXISTS;
          break;

        case GIMP_THUMB_FILE_TYPE_FOLDER:
          state = GIMP_THUMB_STATE_FOLDER;
          break;

        case GIMP_THUMB_FILE_TYPE_SPECIAL:
          state = GIMP_THUMB_STATE_SPECIAL;
          break;

        default:
          state = GIMP_THUMB_STATE_NOT_FOUND;
          break;
        }
684 685 686
      break;
    }

687
  if (state != thumbnail->image_state)
688
    {
689
      g_object_set (thumbnail,
690 691
                    "image-state", state,
                    NULL);
692
    }
693

694 695 696 697 698 699
  if (mtime != thumbnail->image_mtime || filesize != thumbnail->image_filesize)
    {
      g_object_set (thumbnail,
                    "image-mtime",    mtime,
                    "image-filesize", filesize,
                    NULL);
700 701 702 703 704

      if (thumbnail->thumb_state == GIMP_THUMB_STATE_OK)
        g_object_set (thumbnail,
                      "thumb-state", GIMP_THUMB_STATE_OLD,
                      NULL);
705 706
    }
}
707

708
static void
709 710
gimp_thumbnail_update_thumb (GimpThumbnail *thumbnail,
                             GimpThumbSize  size)
711
{
712
  gchar          *filename;
713
  GimpThumbState  state;
714 715
  gint64          filesize = 0;
  gint64          mtime    = 0;
716

717 718 719
  if (! thumbnail->image_uri)
    return;

720
  state = thumbnail->thumb_state;
721

722 723 724 725 726
  filename = gimp_thumb_find_thumb (thumbnail->image_uri, &size);

  if (! filename)
    state = GIMP_THUMB_STATE_NOT_FOUND;

727 728
  switch (state)
    {
729 730
    case GIMP_THUMB_STATE_EXISTS:
    case GIMP_THUMB_STATE_OLD:
Sven Neumann's avatar
Sven Neumann committed
731
    case GIMP_THUMB_STATE_FAILED:
732 733
    case GIMP_THUMB_STATE_OK:
      g_return_if_fail (thumbnail->thumb_filename != NULL);
734

735 736 737 738 739 740 741
      if (thumbnail->thumb_size     == size     &&
          thumbnail->thumb_filesize == filesize &&
          thumbnail->thumb_mtime    == mtime)
        {
          g_free (filename);
          return;
        }
742 743
      break;
    default:
744 745
      break;
    }
746

747 748
  if (thumbnail->thumb_filename)
    g_free (thumbnail->thumb_filename);
749

750 751 752
  thumbnail->thumb_filename = filename;

  if (filename)
Sven Neumann's avatar
Sven Neumann committed
753
    state = (size > GIMP_THUMB_SIZE_FAIL ?
754
             GIMP_THUMB_STATE_EXISTS : GIMP_THUMB_STATE_FAILED);
755

756
  thumbnail->thumb_size     = size;
757 758
  thumbnail->thumb_filesize = filesize;
  thumbnail->thumb_mtime    = mtime;
759 760

  if (state != thumbnail->thumb_state)
761 762 763 764 765 766 767 768
    {
      g_object_freeze_notify (G_OBJECT (thumbnail));

      g_object_set (thumbnail, "thumb-state", state, NULL);
      gimp_thumbnail_reset_info (thumbnail);

      g_object_thaw_notify (G_OBJECT (thumbnail));
    }
769 770
}

Sven Neumann's avatar
Sven Neumann committed
771 772 773 774 775 776
static void
gimp_thumbnail_reset_info (GimpThumbnail *thumbnail)
{
  g_object_set (thumbnail,
                "image-width",      0,
                "image-height",     0,
Sven Neumann's avatar
Sven Neumann committed
777
                "image-mimetype",   NULL,
Sven Neumann's avatar
Sven Neumann committed
778 779 780
                "image-type",       NULL,
                "image-num-layers", 0,
                NULL);
781 782 783 784 785 786 787
}

static void
gimp_thumbnail_set_info_from_pixbuf (GimpThumbnail *thumbnail,
                                     GdkPixbuf     *pixbuf)
{
  const gchar  *option;
788
  gint          num;
789 790 791 792 793 794

  g_object_freeze_notify (G_OBJECT (thumbnail));

  gimp_thumbnail_reset_info (thumbnail);

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_IMAGE_WIDTH);
795 796
  if (option && sscanf (option, "%d", &num) == 1)
    thumbnail->image_width = num;
797 798

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_IMAGE_HEIGHT);
799 800
  if (option && sscanf (option, "%d", &num) == 1)
    thumbnail->image_height = num;
801

Sven Neumann's avatar
Sven Neumann committed
802 803 804
  thumbnail->image_mimetype =
    g_strdup (gdk_pixbuf_get_option (pixbuf, TAG_THUMB_IMAGE_MIMETYPE));

805 806 807 808
  thumbnail->image_type =
    g_strdup (gdk_pixbuf_get_option (pixbuf, TAG_THUMB_GIMP_TYPE));

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_GIMP_LAYERS);
809 810
  if (option && sscanf (option, "%d", &num) == 1)
    thumbnail->image_num_layers = num;
811 812

  g_object_thaw_notify (G_OBJECT (thumbnail));
813 814
}

815 816 817 818 819 820 821 822 823 824
static gboolean
gimp_thumbnail_save (GimpThumbnail  *thumbnail,
                     GimpThumbSize   size,
                     const gchar    *filename,
                     GdkPixbuf      *pixbuf,
                     const gchar    *software,
                     GError        **error)
{
  const gchar  *keys[12];
  gchar        *values[12];
825 826 827
  gchar        *basename;
  gchar        *dirname;
  gchar        *tmpname;
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 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
  gboolean      success;
  gint          i = 0;

  keys[i]   = TAG_DESCRIPTION;
  values[i] = g_strdup_printf ("Thumbnail of %s",   thumbnail->image_uri);
  i++;

  keys[i]   = TAG_SOFTWARE;
  values[i] = g_strdup (software);
  i++;

  keys[i]   = TAG_THUMB_URI;
  values[i] = g_strdup (thumbnail->image_uri);
  i++;

  keys[i]   = TAG_THUMB_MTIME;
  values[i] = g_strdup_printf ("%" G_GINT64_FORMAT, thumbnail->image_mtime);
  i++;

  keys[i]   = TAG_THUMB_FILESIZE;
  values[i] = g_strdup_printf ("%" G_GINT64_FORMAT, thumbnail->image_filesize);
  i++;

  if (thumbnail->image_width > 0)
    {
      keys[i]   = TAG_THUMB_IMAGE_WIDTH;
      values[i] = g_strdup_printf ("%d", thumbnail->image_width);
      i++;
    }

  if (thumbnail->image_height > 0)
    {
      keys[i]   = TAG_THUMB_IMAGE_HEIGHT;
      values[i] = g_strdup_printf ("%d", thumbnail->image_height);
      i++;
    }

  if (thumbnail->image_mimetype)
    {
      keys[i]   = TAG_THUMB_IMAGE_MIMETYPE;
      values[i] = g_strdup_printf (thumbnail->image_mimetype);
      i++;
    }

  if (thumbnail->image_type)
    {
      keys[i]   = TAG_THUMB_GIMP_TYPE;
      values[i] = g_strdup (thumbnail->image_type);
      i++;
    }

  if (thumbnail->image_num_layers > 0)
    {
      keys[i]   = TAG_THUMB_GIMP_LAYERS;
      values[i] = g_strdup_printf ("%d", thumbnail->image_num_layers);
      i++;
    }

  keys[i]   = NULL;
  values[i] = NULL;

889 890 891 892 893 894 895 896 897 898
  basename = g_path_get_basename (filename);
  dirname  = g_path_get_dirname (filename);

  tmpname = g_strdup_printf ("%s%cgimp-thumb-%d-%.8s",
                             dirname, G_DIR_SEPARATOR, getpid (), basename);

  g_free (dirname);
  g_free (basename);

  success = gdk_pixbuf_savev (pixbuf, tmpname, "png",
899 900 901 902 903 904 905 906 907
                              (gchar **) keys, values,
                              error);

  for (i = 0; keys[i]; i++)
    g_free (values[i]);

  if (success)
    {
#ifdef GIMP_THUMB_DEBUG
908 909 910 911 912
      g_printerr ("thumbnail saved to temporary file %s\n", tmpname);
#endif

#ifdef G_OS_WIN32
      /* win32 rename can't overwrite */
913
      g_unlink (filename);
914 915
#endif

916
      if (g_rename (tmpname, filename) == -1)
917 918 919 920 921 922 923 924 925 926 927 928 929
	{
	  g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not create thumbnail for %s: %s"),
		       thumbnail->image_uri, g_strerror (errno));

          success = FALSE;
        }
    }

  if (success)
    {
#ifdef GIMP_THUMB_DEBUG
      g_printerr ("temporary thumbnail file renamed to %s\n", filename);
930 931 932 933
#endif

      success = (chmod (filename, 0600) == 0);

934
      if (! success)
935 936 937
        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                     "Could not set permissions of thumbnail for %s: %s",
                     thumbnail->image_uri, g_strerror (errno));
938 939 940 941 942 943 944 945 946 947 948 949 950

      g_object_freeze_notify (G_OBJECT (thumbnail));

      gimp_thumbnail_update_thumb (thumbnail, size);

      if (success &&
          thumbnail->thumb_state == GIMP_THUMB_STATE_EXISTS &&
          strcmp (filename, thumbnail->thumb_filename) == 0)
        {
          thumbnail->thumb_state = GIMP_THUMB_STATE_OK;
        }

      g_object_thaw_notify (G_OBJECT (thumbnail));
951 952
    }

953
  g_unlink (tmpname);
954 955
  g_free (tmpname);

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
  return success;
}

#ifdef GIMP_THUMB_DEBUG
static void
gimp_thumbnail_debug_notify (GObject    *object,
                             GParamSpec *pspec)
{
  GValue       value = { 0, };
  gchar       *str   = NULL;
  const gchar *name;

  g_value_init (&value, pspec->value_type);
  g_object_get_property (object, pspec->name, &value);

  if (G_VALUE_HOLDS_STRING (&value))
    {
      str = g_value_dup_string (&value);
    }
  else if (g_value_type_transformable (pspec->value_type, G_TYPE_STRING))
    {
      GValue  tmp = { 0, };

      g_value_init (&tmp, G_TYPE_STRING);
      g_value_transform (&value, &tmp);

      str = g_value_dup_string (&tmp);

      g_value_unset (&tmp);
    }

  g_value_unset (&value);

  name = GIMP_THUMBNAIL (object)->image_uri;

  g_printerr (" GimpThumb (%s) %s: %s\n",
              name ? name : "(null)", pspec->name, str);

  g_free (str);
}
#endif


Sven Neumann's avatar
Sven Neumann committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
/**
 * gimp_thumbnail_load_thumb:
 * @thumbnail: a #GimpThumbnail object
 * @size: the preferred #GimpThumbSize for the preview
 * @error: return location for possible errors
 *
 * Attempts to load a thumbnail preview for the image associated with
 * @thumbnail. Before you use this function you need need to set an
 * image location using gimp_thumbnail_set_uri() or
 * gimp_thumbnail_set_filename(). You can also peek at the thumb
 * before loading it using gimp_thumbnail_peek_thumb.
 *
 * This function will return the best matching pixbuf for the
 * specified @size. It returns the pixbuf as loaded from disk. It is
 * left to the caller to scale it to the desired size. The returned
 * pixbuf may also represent an outdated preview of the image file.
 * In order to verify if the preview is uptodate, you should check the
 * "thumb_state" property after calling this function.
 *
 * Return value: a preview pixbuf or %NULL if no thumbnail was found
 **/
1020
GdkPixbuf *
1021
gimp_thumbnail_load_thumb (GimpThumbnail  *thumbnail,
1022 1023 1024
                           GimpThumbSize   size,
                           GError        **error)
{
1025
  GimpThumbState  state;
1026
  GdkPixbuf      *pixbuf;
1027 1028 1029
  const gchar    *option;
  gint64          image_mtime;
  gint64          image_size;
1030

1031 1032
  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), NULL);

1033 1034
  GIMP_THUMB_DEBUG_CALL (thumbnail);

1035
  if (! thumbnail->image_uri)
1036 1037
    return NULL;

1038
  state = gimp_thumbnail_peek_thumb (thumbnail, size);
1039

1040
  if (state < GIMP_THUMB_STATE_EXISTS || state == GIMP_THUMB_STATE_FAILED)
1041
    return NULL;
1042

1043
  pixbuf = gdk_pixbuf_new_from_file (thumbnail->thumb_filename, NULL);
1044
  if (! pixbuf)
1045
    return NULL;
1046

1047 1048 1049 1050
#ifdef GIMP_THUMB_DEBUG
  g_printerr ("thumbnail loaded from %s\n", thumbnail->thumb_filename);
#endif

1051 1052
  g_object_freeze_notify (G_OBJECT (thumbnail));

1053 1054
  /* URI and mtime from the thumbnail need to match our file */
  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_URI);
1055
  if (!option)
1056
    goto finish;
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066
  if (strcmp (option, thumbnail->image_uri))
    {
      /*  might be a local thumbnail, try if the local part matches  */
      const gchar *baseuri = strrchr (thumbnail->image_uri, '/');

      if (!baseuri || strcmp (option, baseuri))
        goto finish;
    }

1067 1068 1069 1070
  state = GIMP_THUMB_STATE_OLD;

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_MTIME);
  if (!option || sscanf (option, "%" G_GINT64_FORMAT, &image_mtime) != 1)
1071
    goto finish;
1072 1073 1074

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_FILESIZE);
  if (option && sscanf (option, "%" G_GINT64_FORMAT, &image_size) != 1)
1075
    goto finish;
1076 1077 1078 1079

  /* TAG_THUMB_FILESIZE is optional but must match if present */
  if (image_mtime == thumbnail->image_mtime &&
      (option == NULL || image_size == thumbnail->image_filesize))
1080
    {
1081
      if (thumbnail->thumb_size == GIMP_THUMB_SIZE_FAIL)
1082 1083 1084 1085
        state = GIMP_THUMB_STATE_FAILED;
      else
        state = GIMP_THUMB_STATE_OK;
    }
1086

1087
  if (state == GIMP_THUMB_STATE_FAILED)
1088 1089 1090
    gimp_thumbnail_reset_info (thumbnail);
  else
    gimp_thumbnail_set_info_from_pixbuf (thumbnail, pixbuf);
1091

1092
 finish:
1093 1094
  if (thumbnail->thumb_size == GIMP_THUMB_SIZE_FAIL ||
      (state != GIMP_THUMB_STATE_OLD && state != GIMP_THUMB_STATE_OK))
1095 1096
    {
      g_object_unref (pixbuf);
1097
      pixbuf = NULL;
1098 1099
    }

1100 1101 1102 1103
  g_object_set (thumbnail,
                "thumb-state", state,
                NULL);

1104 1105
  g_object_thaw_notify (G_OBJECT (thumbnail));

1106 1107 1108
  return pixbuf;
}

Sven Neumann's avatar
Sven Neumann committed
1109 1110 1111 1112 1113 1114 1115 1116
/**
 * gimp_thumbnail_save_thumb:
 * @thumbnail: a #GimpThumbnail object
 * @pixbuf: a #GdkPixbuf representing the preview thumbnail
 * @software: a string describing the software saving the thumbnail
 * @error: return location for possible errors
 *
 * Saves a preview thumbnail for the image associated with @thumbnail.
1117
 * to the global thumbnail repository.
Sven Neumann's avatar
Sven Neumann committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
 *
 * The caller is responsible for setting the image file location, it's
 * filesize, modification time. One way to set this info is to is to
 * call gimp_thumbnail_set_uri() followed by gimp_thumbnail_peek_image().
 * Since this won't work for remote images, it is left to the user of
 * gimp_thumbnail_save_thumb() to do this or to set the information
 * using the @thumbnail object properties.
 *
 * The image format type and the number of layers can optionally be
 * set in order to be stored with the preview image.
 *
 * Return value: %TRUE if a thumbnail was successfully written,
 *               %FALSE otherwise
 **/
1132
gboolean
1133 1134 1135 1136
gimp_thumbnail_save_thumb (GimpThumbnail  *thumbnail,
                           GdkPixbuf      *pixbuf,
                           const gchar    *software,
                           GError        **error)
1137 1138 1139 1140 1141 1142
{
  GimpThumbSize  size;
  gchar         *name;
  gboolean       success;

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
1143
  g_return_val_if_fail (thumbnail->image_uri != NULL, FALSE);
1144 1145 1146 1147
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
  g_return_val_if_fail (software != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

1148 1149
  GIMP_THUMB_DEBUG_CALL (thumbnail);

1150
  size = MAX (gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf));
1151 1152 1153
  if (size < 1)
    return TRUE;

1154
  name = gimp_thumb_name_from_uri (thumbnail->image_uri, size);
1155 1156 1157
  if (! name)
    return TRUE;

1158 1159 1160 1161 1162 1163
  if (! gimp_thumb_ensure_thumb_dir (size, error))
    {
      g_free (name);
      return FALSE;
    }

1164 1165 1166 1167
  success = gimp_thumbnail_save (thumbnail,
                                 size, name, pixbuf, software,
                                 error);
  g_free (name);
Sven Neumann's avatar
Sven Neumann committed
1168

1169 1170
  return success;
}
Sven Neumann's avatar
Sven Neumann committed
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
/**
 * gimp_thumbnail_save_thumb_local:
 * @thumbnail: a #GimpThumbnail object
 * @pixbuf: a #GdkPixbuf representing the preview thumbnail
 * @software: a string describing the software saving the thumbnail
 * @error: return location for possible errors
 *
 * Saves a preview thumbnail for the image associated with @thumbnail
 * to the local thumbnail repository. Local thumbnails have been added
 * with version 0.7 of the spec.
 *
 * Please see also gimp_thumbnail_save_thumb(). The notes made there
 * apply here as well.
 *
 * Return value: %TRUE if a thumbnail was successfully written,
 *               %FALSE otherwise
 *
 * Since: GIMP 2.2
 **/
gboolean
gimp_thumbnail_save_thumb_local (GimpThumbnail  *thumbnail,
                                 GdkPixbuf      *pixbuf,
                                 const gchar    *software,
                                 GError        **error)
{
  GimpThumbSize  size;
  gchar         *name;
  gchar         *filename;
  gchar         *dirname;
  gboolean       success;
Sven Neumann's avatar
Sven Neumann committed
1202

1203 1204 1205 1206 1207
  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
  g_return_val_if_fail (thumbnail->image_uri != NULL, FALSE);
  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
  g_return_val_if_fail (software != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Sven Neumann's avatar
Sven Neumann committed
1208

1209
  GIMP_THUMB_DEBUG_CALL (thumbnail);
Sven Neumann's avatar
Sven Neumann committed
1210

1211 1212 1213
  size = MAX (gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf));
  if (size < 1)
    return TRUE;
Sven Neumann's avatar
Sven Neumann committed
1214

1215 1216 1217
  filename = g_filename_from_uri (thumbnail->image_uri, NULL, NULL);
  if (! filename)
    return TRUE;
Sven Neumann's avatar
Sven Neumann committed
1218

1219 1220
  dirname = g_path_get_dirname (filename);
  g_free (filename);
1221

1222 1223
  name = gimp_thumb_name_from_uri_local (thumbnail->image_uri, size);
  if (! name)
Sven Neumann's avatar
Sven Neumann committed
1224
    {
1225 1226
      g_free (dirname);
      return TRUE;
Sven Neumann's avatar
Sven Neumann committed
1227
    }
1228

1229
  if (! gimp_thumb_ensure_thumb_dir_local (dirname, size, error))
Sven Neumann's avatar
Sven Neumann committed
1230
    {
1231 1232 1233
      g_free (name);
      g_free (dirname);
      return FALSE;
Sven Neumann's avatar
Sven Neumann committed
1234
    }
1235

1236
  g_free (dirname);
1237

1238 1239 1240
  success = gimp_thumbnail_save (thumbnail,
                                 size, name, pixbuf, software,
                                 error);
1241 1242 1243 1244 1245
  g_free (name);

  return success;
}

Sven Neumann's avatar
Sven Neumann committed
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
/**
 * gimp_thumbnail_save_failure:
 * @thumbnail: a #GimpThumbnail object
 * @software: a string describing the software saving the thumbnail
 * @error: return location for possible errors
 *
 * Saves a failure thumbnail for the image associated with
 * @thumbnail. This is an empty pixbuf that indicates that an attempt
 * to create a preview for the image file failed. It should be used to
 * prevent the software from further attempts to create this thumbnail.
 *
 * Return value: %TRUE if a failure thumbnail was successfully written,
 *               %FALSE otherwise
 **/
1260 1261 1262 1263 1264
gboolean
gimp_thumbnail_save_failure (GimpThumbnail  *thumbnail,
                             const gchar    *software,
                             GError        **error)
{
1265 1266 1267 1268 1269 1270
  GdkPixbuf *pixbuf;
  gchar     *name;
  gchar     *desc;
  gchar     *time_str;
  gchar     *size_str;
  gboolean   success;
1271 1272

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
1273
  g_return_val_if_fail (thumbnail->image_uri != NULL, FALSE);
1274 1275
  g_return_val_if_fail (software != NULL, FALSE);

1276 1277
  GIMP_THUMB_DEBUG_CALL (thumbnail);

1278
  name = gimp_thumb_name_from_uri (thumbnail->image_uri, GIMP_THUMB_SIZE_FAIL);
1279 1280 1281
  if (! name)
    return TRUE;

1282
  if (! gimp_thumb_ensure_thumb_dir (GIMP_THUMB_SIZE_FAIL, error))
1283 1284 1285 1286 1287
    {
      g_free (name);
      return FALSE;
    }

1288 1289
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);

1290
  desc = g_strdup_printf ("Thumbnail failure for %s", thumbnail->image_uri);
1291 1292 1293 1294 1295 1296
  time_str = g_strdup_printf ("%" G_GINT64_FORMAT, thumbnail->image_mtime);
  size_str = g_strdup_printf ("%" G_GINT64_FORMAT, thumbnail->image_filesize);

  success = gdk_pixbuf_save (pixbuf, name, "png", error,
                             TAG_DESCRIPTION,    desc,
                             TAG_SOFTWARE,       software,
1297
                             TAG_THUMB_URI,      thumbnail->image_uri,
1298 1299 1300 1301 1302
                             TAG_THUMB_MTIME,    time_str,
                             TAG_THUMB_FILESIZE, size_str,
                             NULL);
  if (success)
    {
1303
      success = (chmod (name, 0600) == 0);
1304

1305
      if (success)
Sven Neumann's avatar
Sven Neumann committed
1306
        gimp_thumbnail_update_thumb (thumbnail, GIMP_THUMB_SIZE_NORMAL);
1307
      else
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                     "Could not set permissions of thumbnail '%s': %s",
                     name, g_strerror (errno));
    }

  g_object_unref (pixbuf);

  g_free (size_str);
  g_free (time_str);
  g_free (desc);
  g_free (name);

  return success;
}
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

/**
 * gimp_thumbnail_delete_failure:
 * @thumbnail: a #GimpThumbnail object
 *
 * Removes a failure thumbnail if one exists. This function should be
 * used after a thumbnail has been successfully created.
 *
 * Since: GIMP 2.2
 **/
void
gimp_thumbnail_delete_failure (GimpThumbnail *thumbnail)
{
  gchar *filename;

  g_return_if_fail (GIMP_IS_THUMBNAIL (thumbnail));
  g_return_if_fail (thumbnail->image_uri != NULL);

  GIMP_THUMB_DEBUG_CALL (thumbnail);

  filename = gimp_thumb_name_from_uri (thumbnail->image_uri,
                                       GIMP_THUMB_SIZE_FAIL);
  if (filename)
    {
1346
      g_unlink (filename);
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
      g_free (filename);
    }
}

/**
 * gimp_thumbnail_delete_others:
 * @thumbnail: a #GimpThumbnail object
 * @size: the thumbnail size which should not be deleted
 *
 * Removes all other thumbnails from the global thumbnail
 * repository. Only the thumbnail for @size is not deleted.  This
 * function should be used after a thumbnail has been successfully
 * updated. See the spec for a more detailed description on when to
 * delete thumbnails.
 *
 * Since: GIMP 2.2
 **/
void
gimp_thumbnail_delete_others (GimpThumbnail *thumbnail,
                              GimpThumbSize  size)
{
  g_return_if_fail (GIMP_IS_THUMBNAIL (thumbnail));
  g_return_if_fail (thumbnail->image_uri != NULL);

  GIMP_THUMB_DEBUG_CALL (thumbnail);

  _gimp_thumbs_delete_others (thumbnail->image_uri, size);
}

/**
 * gimp_thumbnail_has_failed:
 * @thumbnail: a #GimpThumbnail object
 *
 * Checks if a valid failure thumbnail for the given thumbnail exists
 * in the global thumbnail repository. This may be the case even if
 * gimp_thumbnail_peek_thumb() doesn't return %GIMP_THUMB_STATE_FAILED
 * since there might be a real thumbnail and a failure thumbnail for
 * the same image file.
 *
 * The application should not attempt to create the thumbnail if a
 * valid failure thumbnail exists.
 *
 * Return value: %TRUE if a failure thumbnail exists or
 *
 * Since: GIMP 2.2
 **/
gboolean
gimp_thumbnail_has_failed (GimpThumbnail *thumbnail)
{
  GdkPixbuf   *pixbuf;
  const gchar *option;
  gchar       *filename;
  gint64       image_mtime;
  gint64       image_size;
  gboolean     failed = FALSE;

  g_return_val_if_fail (GIMP_IS_THUMBNAIL (thumbnail), FALSE);
  g_return_val_if_fail (thumbnail->image_uri != NULL, FALSE);

  GIMP_THUMB_DEBUG_CALL (thumbnail);

  filename = gimp_thumb_name_from_uri (thumbnail->image_uri,
                                       GIMP_THUMB_SIZE_FAIL);
  if (! filename)
    return FALSE;

  pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
  g_free (filename);

  if (! pixbuf)
    return FALSE;

  if (gimp_thumbnail_peek_image (thumbnail) < GIMP_THUMB_STATE_EXISTS)
    goto finish;

  /* URI and mtime from the thumbnail need to match our file */
  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_URI);
  if (! option || strcmp (option, thumbnail->image_uri))
    goto finish;

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_MTIME);
  if (!option || sscanf (option, "%" G_GINT64_FORMAT, &image_mtime) != 1)
    goto finish;

  option = gdk_pixbuf_get_option (pixbuf, TAG_THUMB_FILESIZE);
  if (option && sscanf (option, "%" G_GINT64_FORMAT, &image_size) != 1)
    goto finish;

  /* TAG_THUMB_FILESIZE is optional but must match if present */
  if (image_mtime == thumbnail->image_mtime &&
      (option == NULL || image_size == thumbnail->image_filesize))
    {
      failed = TRUE;
    }

 finish:
  g_object_unref (pixbuf);

  return failed;
}