Fix #885: Generate sRGB thumbnails for images with embedded color profile data
[geeqie.git] / src / thumb_standard.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "thumb_standard.h"
24
25 #include "cache.h"
26 #include "image-load.h"
27 #include "md5-util.h"
28 #include "pixbuf_util.h"
29 #include "ui_fileops.h"
30 #include "filedata.h"
31 #include "exif.h"
32 #include "metadata.h"
33 #include "color-man.h"
34
35
36 /*
37  * This thumbnail caching implementation attempts to conform
38  * to the Thumbnail Managing Standard proposed on freedesktop.org
39  * The standard is documented here:
40  *   http://triq.net/~jens/thumbnail-spec/index.html
41  *  (why isn't it actually hosted on freedesktop.org?)
42  *
43  * This code attempts to conform to version 0.7.0 of the standard.
44  *
45  * Notes:
46  *   > Validation of the thumb's embedded uri is a simple strcmp between our
47  *     version of the escaped uri and the thumb's escaped uri. But not all uri
48  *     escape functions escape the same set of chars, comparing the unescaped
49  *     versions may be more accurate.
50  *   > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
51  *     Storing the Size, Width, Height should probably be implemented.
52  */
53
54
55 #define THUMB_SIZE_NORMAL 128
56 #define THUMB_SIZE_LARGE  256
57
58 #define THUMB_MARKER_URI    "tEXt::Thumb::URI"
59 #define THUMB_MARKER_MTIME  "tEXt::Thumb::MTime"
60 #define THUMB_MARKER_SIZE   "tEXt::Thumb::Size"
61 #define THUMB_MARKER_WIDTH  "tEXt::Thumb::Image::Width"
62 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
63 #define THUMB_MARKER_APP    "tEXt::Software"
64
65 #define THUMB_PERMS_FOLDER 0700
66 #define THUMB_PERMS_THUMB  0600
67
68
69
70 /*
71  *-----------------------------------------------------------------------------
72  * thumbnail loader
73  *-----------------------------------------------------------------------------
74  */
75
76
77 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
78 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
79
80
81 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
82 {
83         ThumbLoaderStd *tl;
84
85         tl = g_new0(ThumbLoaderStd, 1);
86
87         tl->standard_loader = TRUE;
88         tl->requested_width = width;
89         tl->requested_height = height;
90         tl->cache_enable = options->thumbnails.enable_caching;
91
92         return tl;
93 }
94
95 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
96                                     ThumbLoaderStdFunc func_done,
97                                     ThumbLoaderStdFunc func_error,
98                                     ThumbLoaderStdFunc func_progress,
99                                     gpointer data)
100 {
101         if (!tl) return;
102
103         tl->func_done = func_done;
104         tl->func_error = func_error;
105         tl->func_progress = func_progress;
106         tl->data = data;
107 }
108
109 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
110 {
111         image_loader_free(tl->il);
112         tl->il = NULL;
113
114         file_data_unref(tl->fd);
115         tl->fd = NULL;
116
117         g_free(tl->thumb_path);
118         tl->thumb_path = NULL;
119
120         g_free(tl->thumb_uri);
121         tl->thumb_uri = NULL;
122         tl->local_uri = NULL;
123
124         tl->thumb_path_local = FALSE;
125
126         tl->cache_hit = FALSE;
127
128         tl->source_mtime = 0;
129         tl->source_size = 0;
130         tl->source_mode = 0;
131
132         tl->progress = 0.0;
133 }
134
135 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
136                                    const gchar *cache_subfolder)
137 {
138         gchar *result = NULL;
139         gchar *md5_text;
140         guchar digest[16];
141         gchar *name;
142
143         if (!path || !uri || !cache_subfolder) return NULL;
144
145         md5_get_digest((guchar *)uri, strlen(uri), digest);
146         md5_text = md5_digest_to_text(digest);
147
148         if (!md5_text) return NULL;
149
150         name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
151
152         if (local)
153                 {
154                 gchar *base = remove_level_from_path(path);
155
156                 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
157                 g_free(base);
158                 }
159         else
160                 {
161                 result = g_build_filename(get_thumbnails_standard_cache_dir(),
162                                                                                                         cache_subfolder, name, NULL);
163                 }
164
165         g_free(name);
166         g_free(md5_text);
167
168         return result;
169 }
170
171 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
172 {
173         const gchar *folder;
174         gint w, h;
175
176         if (!tl->fd || !tl->thumb_uri) return NULL;
177
178         if (pixbuf)
179                 {
180                 w = gdk_pixbuf_get_width(pixbuf);
181                 h = gdk_pixbuf_get_height(pixbuf);
182                 }
183         else
184                 {
185                 w = tl->requested_width;
186                 h = tl->requested_height;
187                 }
188
189         if (fail)
190                 {
191                 folder = THUMB_FOLDER_FAIL;
192                 }
193         else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
194                 {
195                 folder = THUMB_FOLDER_LARGE;
196                 }
197         else
198                 {
199                 folder = THUMB_FOLDER_NORMAL;
200                 }
201
202         return thumb_std_cache_path(tl->fd->path,
203                                     (local) ?  tl->local_uri : tl->thumb_uri,
204                                     local, folder);
205 }
206
207 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
208 {
209         gchar *fail_path;
210         gboolean result = FALSE;
211
212         fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
213         if (isfile(fail_path))
214                 {
215                 GdkPixbuf *pixbuf;
216
217                 if (tl->cache_retry)
218                         {
219                         pixbuf = NULL;
220                         }
221                 else
222                         {
223                         gchar *pathl;
224
225                         pathl = path_from_utf8(fail_path);
226                         pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
227                         g_free(pathl);
228                         }
229
230                 if (pixbuf)
231                         {
232                         const gchar *mtime_str;
233
234                         mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
235                         if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
236                                 {
237                                 result = TRUE;
238                                 DEBUG_1("thumb fail valid: %s", tl->fd->path);
239                                 DEBUG_1("           thumb: %s", fail_path);
240                                 }
241
242                         g_object_unref(G_OBJECT(pixbuf));
243                         }
244
245                 if (!result) unlink_file(fail_path);
246                 }
247         g_free(fail_path);
248
249         return result;
250 }
251
252 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
253 {
254         const gchar *valid_uri;
255         const gchar *uri;
256         const gchar *mtime_str;
257         time_t mtime;
258         gint w, h;
259
260         if (!pixbuf) return FALSE;
261
262         w = gdk_pixbuf_get_width(pixbuf);
263         h = gdk_pixbuf_get_height(pixbuf);
264
265         if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
266             h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
267
268         valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
269
270         uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
271         mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
272
273         if (!mtime_str || !uri || !valid_uri) return FALSE;
274         if (strcmp(uri, valid_uri) != 0) return FALSE;
275
276         mtime = strtol(mtime_str, NULL, 10);
277         if (tl->source_mtime != mtime) return FALSE;
278
279         return TRUE;
280 }
281
282 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
283 {
284         gchar *base_path;
285         gchar *tmp_path;
286         gboolean fail;
287
288         if (!tl->cache_enable || tl->cache_hit) return;
289         if (tl->thumb_path) return;
290
291         if (!pixbuf)
292                 {
293                 /* local failures are not stored */
294                 if (tl->cache_local) return;
295
296                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
297                 fail = TRUE;
298                 }
299         else
300                 {
301                 g_object_ref(G_OBJECT(pixbuf));
302                 fail = FALSE;
303                 }
304
305         tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
306         if (!tl->thumb_path)
307                 {
308                 g_object_unref(G_OBJECT(pixbuf));
309                 return;
310                 }
311         tl->thumb_path_local = tl->cache_local;
312
313         /* create thumbnail dir if needed */
314         base_path = remove_level_from_path(tl->thumb_path);
315         if (tl->cache_local)
316                 {
317                 if (!isdir(base_path))
318                         {
319                         struct stat st;
320                         gchar *source_base;
321
322                         source_base = remove_level_from_path(tl->fd->path);
323                         if (stat_utf8(source_base, &st))
324                                 {
325                                 recursive_mkdir_if_not_exists(base_path, st.st_mode);
326                                 }
327                         g_free(source_base);
328                         }
329                 }
330         else
331                 {
332                 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
333                 }
334         g_free(base_path);
335
336         DEBUG_1("thumb saving: %s", tl->fd->path);
337         DEBUG_1("       saved: %s", tl->thumb_path);
338
339         /* save thumb, using a temp file then renaming into place */
340         tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
341         if (tmp_path)
342                 {
343                 const gchar *mark_uri;
344                 gchar *mark_app;
345                 gchar *mark_mtime;
346                 gchar *pathl;
347                 gboolean success;
348
349                 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
350
351                 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
352                 mark_mtime = g_strdup_printf("%llu", (unsigned long long)tl->source_mtime);
353                 pathl = path_from_utf8(tmp_path);
354                 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
355                                           THUMB_MARKER_URI, mark_uri,
356                                           THUMB_MARKER_MTIME, mark_mtime,
357                                           THUMB_MARKER_APP, mark_app,
358                                           NULL);
359                 if (success)
360                         {
361                         chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
362                         success = rename_file(tmp_path, tl->thumb_path);
363                         }
364
365                 g_free(pathl);
366
367                 g_free(mark_mtime);
368                 g_free(mark_app);
369
370                 g_free(tmp_path);
371                 if (!success)
372                         {
373                         DEBUG_1("thumb save failed: %s", tl->fd->path);
374                         DEBUG_1("            thumb: %s", tl->thumb_path);
375                         }
376
377                 }
378
379         g_object_unref(G_OBJECT(pixbuf));
380 }
381
382 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
383 {
384         if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
385         tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
386 }
387
388
389 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
390         ColorMan *cm = NULL;
391         ExifData *exif = NULL;
392         gint color_profile_from_image = COLOR_PROFILE_NONE;
393         ColorManProfileType input_type = COLOR_PROFILE_MEM;
394         ColorManProfileType screen_type;
395         const gchar *input_file = NULL;
396         guchar *profile = NULL;
397         guint profile_len;
398         gint sw, sh;
399
400         sw = gdk_pixbuf_get_width(pixbuf);
401         sh = gdk_pixbuf_get_height(pixbuf);
402
403         exif = exif_read_fd(fd);
404
405         if (exif)
406                 {
407                 profile = exif_get_color_profile(exif, &profile_len);
408                 if (profile)
409                         {
410                         DEBUG_1("Found embedded color profile");
411                         color_profile_from_image = COLOR_PROFILE_MEM;
412                         }
413                 else
414                         {
415                         gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
416
417                         if (interop_index)
418                                 {
419                                 /* Exif 2.21 specification */
420                                 if (!strcmp(interop_index, "R98"))
421                                         {
422                                         color_profile_from_image = COLOR_PROFILE_SRGB;
423                                         DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
424                                         }
425                                 else if (!strcmp(interop_index, "R03"))
426                                         {
427                                         color_profile_from_image = COLOR_PROFILE_ADOBERGB;
428                                         DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
429                                         }
430                                 g_free(interop_index);
431                                 }
432                         else
433                                 {
434                                 gint cs;
435
436                                 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
437                                 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
438                                 if (cs == 1)
439                                         {
440                                         color_profile_from_image = COLOR_PROFILE_SRGB;
441                                         DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
442                                         }
443                                 else if (cs == 2)
444                                         {
445                                         /* non-standard way of specifying AdobeRGB (used by some software) */
446                                         color_profile_from_image = COLOR_PROFILE_ADOBERGB;
447                                         DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
448                                         }
449                                 }
450                         }
451
452                 if(color_profile_from_image != COLOR_PROFILE_NONE)
453                         {
454                                 // transform image, we always use sRGB as target for thumbnails
455                                 screen_type = COLOR_PROFILE_SRGB;
456
457                                 if (profile)
458                                         {
459                                         cm = color_man_new_embedded(NULL, pixbuf,
460                                                                         profile, profile_len,
461                                                                         screen_type, NULL, NULL, 0);
462                                         g_free(profile);
463                                         }
464                                 else
465                                         {
466                                         cm = color_man_new(NULL, pixbuf,
467                                                         input_type, input_file,
468                                                         screen_type, NULL, NULL, 0);
469                                         }
470
471                                 if(cm) {
472                                         color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
473                                         g_free(cm);
474                                 }
475
476                         }
477                 exif_free_fd(fd, exif);
478                 }
479 }
480
481 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
482 {
483         GdkPixbuf *pixbuf_thumb = NULL;
484         GdkPixbuf *result;
485         GdkPixbuf *rotated = NULL;
486         gint sw, sh;
487
488
489         if (!tl->cache_hit && options->image.exif_rotate_enable)
490                 {
491                 if (!tl->fd->exif_orientation)
492                         {
493                         tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
494                         }
495
496                 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
497                         {
498                         rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
499                         pixbuf = rotated;
500                         }
501                 }
502
503         sw = gdk_pixbuf_get_width(pixbuf);
504         sh = gdk_pixbuf_get_height(pixbuf);
505
506         if (tl->cache_enable)
507                 {
508                 if (!tl->cache_hit)
509                         {
510                         gint cache_w, cache_h;
511
512                         if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
513                                 {
514                                 cache_w = cache_h = THUMB_SIZE_LARGE;
515                                 }
516                         else
517                                 {
518                                 cache_w = cache_h = THUMB_SIZE_NORMAL;
519                                 }
520
521                         if (sw > cache_w || sh > cache_h || shrunk)
522                                 {
523                                 gint thumb_w, thumb_h;
524                                 struct stat st;
525
526                                 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
527                                                                   &thumb_w, &thumb_h))
528                                         {
529                                         pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
530                                                                                (GdkInterpType)options->thumbnails.quality);
531                                         }
532                                 else
533                                         {
534                                         pixbuf_thumb = pixbuf;
535                                         g_object_ref(G_OBJECT(pixbuf_thumb));
536                                         }
537
538                                 /* do not save the thumbnail if the source file has changed meanwhile -
539                                    the thumbnail is most probably broken */
540                                 if (stat_utf8(tl->fd->path, &st) &&
541                                     tl->source_mtime == st.st_mtime &&
542                                     tl->source_size == st.st_size)
543                                         {
544                                         thumb_loader_std_save(tl, pixbuf_thumb);
545                                         }
546                                 }
547                         }
548                 else if (tl->cache_hit &&
549                          tl->cache_local && !tl->thumb_path_local)
550                         {
551                         /* A local cache save was requested, but a valid thumb is in $HOME,
552                          * so specifically save as a local thumbnail.
553                          */
554                         g_free(tl->thumb_path);
555                         tl->thumb_path = NULL;
556
557                         tl->cache_hit = FALSE;
558
559                         DEBUG_1("thumb copied: %s", tl->fd->path);
560
561                         thumb_loader_std_save(tl, pixbuf);
562                         }
563                 }
564
565         if (sw <= tl->requested_width && sh <= tl->requested_height)
566                 {
567                 result = pixbuf;
568                 g_object_ref(result);
569                 }
570         else
571                 {
572                 gint thumb_w, thumb_h;
573
574                 if (pixbuf_thumb)
575                         {
576                         pixbuf = pixbuf_thumb;
577                         sw = gdk_pixbuf_get_width(pixbuf);
578                         sh = gdk_pixbuf_get_height(pixbuf);
579                         }
580
581                 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
582                                                   &thumb_w, &thumb_h))
583                         {
584                         result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
585                                                          (GdkInterpType)options->thumbnails.quality);
586                         }
587                 else
588                         {
589                         result = pixbuf;
590                         g_object_ref(result);
591                         }
592                 }
593
594         // apply color correction, if required
595         thumb_loader_std_calibrate_pixbuf(tl->fd, result);
596
597         if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
598         if (rotated) g_object_unref(rotated);
599
600         return result;
601 }
602
603 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
604 {
605         image_loader_free(tl->il);
606         tl->il = NULL;
607
608         if (tl->thumb_path)
609                 {
610                 if (!tl->thumb_path_local && remove_broken)
611                         {
612                         DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
613                         unlink_file(tl->thumb_path);
614                         }
615
616                 g_free(tl->thumb_path);
617                 tl->thumb_path = NULL;
618
619                 if (!tl->thumb_path_local)
620                         {
621                         tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
622                         if (isfile(tl->thumb_path))
623                                 {
624                                 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
625                                 if (thumb_loader_std_setup(tl, fd))
626                                         {
627                                         file_data_unref(fd);
628                                         tl->thumb_path_local = TRUE;
629                                         return TRUE;
630                                         }
631                                 file_data_unref(fd);
632                                 }
633
634                         g_free(tl->thumb_path);
635                         tl->thumb_path = NULL;
636                         }
637
638                 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
639                 }
640
641         thumb_loader_std_save(tl, NULL);
642         return FALSE;
643 }
644
645 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
646 {
647         ThumbLoaderStd *tl = data;
648         GdkPixbuf *pixbuf;
649
650         DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
651         DEBUG_1("            from: %s", image_loader_get_fd(tl->il)->path);
652
653         pixbuf = image_loader_get_pixbuf(tl->il);
654         if (!pixbuf)
655                 {
656                 DEBUG_1("...but no pixbuf");
657                 thumb_loader_std_error_cb(il, data);
658                 return;
659                 }
660
661         if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
662                 {
663                 if (thumb_loader_std_next_source(tl, TRUE)) return;
664
665                 if (tl->func_error) tl->func_error(tl, tl->data);
666                 return;
667                 }
668
669         tl->cache_hit = (tl->thumb_path != NULL);
670
671         if (tl->fd)
672                 {
673                 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
674                 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
675                 }
676
677         if (tl->func_done) tl->func_done(tl, tl->data);
678 }
679
680 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
681 {
682         ThumbLoaderStd *tl = data;
683
684         /* if at least some of the image is available, go to done */
685         if (image_loader_get_pixbuf(tl->il) != NULL)
686                 {
687                 thumb_loader_std_done_cb(il, data);
688                 return;
689                 }
690
691         DEBUG_1("thumb image error: %s", tl->fd->path);
692         DEBUG_1("             from: %s", image_loader_get_fd(tl->il)->path);
693
694         if (thumb_loader_std_next_source(tl, TRUE)) return;
695
696         thumb_loader_std_set_fallback(tl);
697
698         if (tl->func_error) tl->func_error(tl, tl->data);
699 }
700
701 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
702 {
703         ThumbLoaderStd *tl = data;
704
705         tl->progress = (gdouble)percent;
706
707         if (tl->func_progress) tl->func_progress(tl, tl->data);
708 }
709
710 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
711 {
712         tl->il = image_loader_new(fd);
713         image_loader_set_priority(tl->il, G_PRIORITY_LOW);
714
715         /* this will speed up jpegs by up to 3x in some cases */
716         if (tl->requested_width <= THUMB_SIZE_NORMAL &&
717             tl->requested_height <= THUMB_SIZE_NORMAL)
718                 {
719                 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
720                 }
721         else
722                 {
723                 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
724                 }
725
726         g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
727         if (tl->func_progress)
728                 {
729                 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
730                 }
731         g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
732
733         if (image_loader_start(tl->il))
734                 {
735                 return TRUE;
736                 }
737
738         image_loader_free(tl->il);
739         tl->il = NULL;
740         return FALSE;
741 }
742
743 /*
744  * Note: Currently local_cache only specifies where to save a _new_ thumb, if
745  *       a valid existing thumb is found anywhere the local thumb will not be created.
746  */
747 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
748 {
749         if (!tl) return;
750
751         tl->cache_enable = enable_cache;
752         tl->cache_local = local;
753         tl->cache_retry = retry_failed;
754 }
755
756 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
757 {
758         static gchar *thumb_cache = NULL;
759         struct stat st;
760
761         if (!tl || !fd) return FALSE;
762
763         thumb_loader_std_reset(tl);
764
765
766         tl->fd = file_data_ref(fd);
767         if (!stat_utf8(fd->path, &st) || (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_DOCUMENT && !options->file_filter.disable))
768                 {
769                 thumb_loader_std_set_fallback(tl);
770                 return FALSE;
771                 }
772         tl->source_mtime = st.st_mtime;
773         tl->source_size = st.st_size;
774         tl->source_mode = st.st_mode;
775
776         if (!thumb_cache)
777                 {
778                 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
779                 }
780
781         if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
782                 {
783                 gchar *pathl;
784
785                 pathl = path_from_utf8(fd->path);
786                 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
787                 tl->local_uri = filename_from_path(tl->thumb_uri);
788                 g_free(pathl);
789                 }
790
791         if (tl->cache_enable)
792                 {
793                 gint found;
794
795                 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
796                 tl->thumb_path_local = FALSE;
797
798                 found = isfile(tl->thumb_path);
799                 if (found)
800                         {
801                         FileData *fd = file_data_new_no_grouping(tl->thumb_path);
802                         if (thumb_loader_std_setup(tl, fd))
803                                 {
804                                 file_data_unref(fd);
805                                 return TRUE;
806                                 }
807                         file_data_unref(fd);
808                         }
809
810                 if (thumb_loader_std_fail_check(tl) ||
811                     !thumb_loader_std_next_source(tl, found))
812                         {
813                         thumb_loader_std_set_fallback(tl);
814                         return FALSE;
815                         }
816                 return TRUE;
817                 }
818
819         if (!thumb_loader_std_setup(tl, tl->fd))
820                 {
821                 thumb_loader_std_save(tl, NULL);
822                 thumb_loader_std_set_fallback(tl);
823                 return FALSE;
824                 }
825
826         return TRUE;
827 }
828
829 void thumb_loader_std_free(ThumbLoaderStd *tl)
830 {
831         if (!tl) return;
832
833         thumb_loader_std_reset(tl);
834         g_free(tl);
835 }
836
837 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
838 {
839         GdkPixbuf *pixbuf;
840
841         if (tl && tl->fd && tl->fd->thumb_pixbuf)
842                 {
843                 pixbuf = tl->fd->thumb_pixbuf;
844                 g_object_ref(pixbuf);
845                 }
846         else
847                 {
848                 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
849                 }
850
851         return pixbuf;
852 }
853
854
855 typedef struct _ThumbValidate ThumbValidate;
856 struct _ThumbValidate
857 {
858         ThumbLoaderStd *tl;
859         gchar *path;
860         gint days;
861
862         void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
863         gpointer data;
864
865         guint idle_id; /* event source id */
866 };
867
868 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
869 {
870         thumb_loader_std_free(tv->tl);
871         g_free(tv->path);
872         g_free(tv);
873 }
874
875 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
876 {
877         ThumbValidate *tv;
878
879         if (!tl) return;
880
881         tv = tl->data;
882
883         if (tv->idle_id)
884                 {
885                 g_source_remove(tv->idle_id);
886                 tv->idle_id = 0;
887                 }
888
889         thumb_loader_std_thumb_file_validate_free(tv);
890 }
891
892 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
893 {
894         if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
895
896         thumb_loader_std_thumb_file_validate_free(tv);
897 }
898
899 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
900 {
901         ThumbValidate *tv = data;
902         GdkPixbuf *pixbuf;
903         gboolean valid = FALSE;
904
905         /* get the original thumbnail pixbuf (unrotated, with original options)
906            this is called from image_loader done callback, so tv->tl->il must exist*/
907         pixbuf = image_loader_get_pixbuf(tv->tl->il);
908         if (pixbuf)
909                 {
910                 const gchar *uri;
911                 const gchar *mtime_str;
912
913                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
914                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
915                 if (uri && mtime_str)
916                         {
917                         if (strncmp(uri, "file:", strlen("file:")) == 0)
918                                 {
919                                 struct stat st;
920                                 gchar *target;
921
922                                 target = g_filename_from_uri(uri, NULL, NULL);
923                                 if (stat(target, &st) == 0 &&
924                                     st.st_mtime == strtol(mtime_str, NULL, 10))
925                                         {
926                                         valid = TRUE;
927                                         }
928                                 g_free(target);
929                                 }
930                         else
931                                 {
932                                 struct stat st;
933
934                                 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
935
936                                 if (stat_utf8(tv->path, &st))
937                                         {
938                                         time_t now;
939
940                                         now = time(NULL);
941                                         if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
942                                                 {
943                                                 valid = TRUE;
944                                                 }
945                                         }
946                                 }
947                         }
948                 else
949                         {
950                         DEBUG_1("invalid image found in std cache: %s", tv->path);
951                         }
952                 }
953
954         thumb_loader_std_thumb_file_validate_finish(tv, valid);
955 }
956
957 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
958 {
959         ThumbValidate *tv = data;
960
961         thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
962 }
963
964 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
965 {
966         ThumbValidate *tv = data;
967
968         tv->idle_id = 0;
969         thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
970
971         return FALSE;
972 }
973
974 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
975                                                      void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
976                                                      gpointer data)
977 {
978         ThumbValidate *tv;
979
980         tv = g_new0(ThumbValidate, 1);
981
982         tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
983         thumb_loader_std_set_callbacks(tv->tl,
984                                        thumb_loader_std_thumb_file_validate_done_cb,
985                                        thumb_loader_std_thumb_file_validate_error_cb,
986                                        NULL,
987                                        tv);
988         thumb_loader_std_reset(tv->tl);
989
990         tv->path = g_strdup(thumb_path);
991         tv->days = allowed_days;
992         tv->func_valid = func_valid;
993         tv->data = data;
994
995         FileData *fd = file_data_new_no_grouping(thumb_path);
996         if (!thumb_loader_std_setup(tv->tl, fd))
997                 {
998                 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
999                 }
1000         else
1001                 {
1002                 tv->idle_id = 0;
1003                 }
1004
1005         file_data_unref(fd);
1006         return tv->tl;
1007 }
1008
1009 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1010                                        const gchar *subfolder)
1011 {
1012         gchar *thumb_path;
1013
1014         thumb_path = thumb_std_cache_path(source,
1015                                           (local) ? filename_from_path(uri) : uri,
1016                                           local, subfolder);
1017         if (isfile(thumb_path))
1018                 {
1019                 DEBUG_1("thumb removing: %s", thumb_path);
1020                 unlink_file(thumb_path);
1021                 }
1022         g_free(thumb_path);
1023 }
1024
1025 /* this also removes local thumbnails (the source is gone so it makes sense) */
1026 void thumb_std_maint_removed(const gchar *source)
1027 {
1028         gchar *uri;
1029         gchar *sourcel;
1030
1031         sourcel = path_from_utf8(source);
1032         uri = g_filename_to_uri(sourcel, NULL, NULL);
1033         g_free(sourcel);
1034
1035         /* all this to remove a thumbnail? */
1036
1037         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1038         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1039         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1040         thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1041         thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1042
1043         g_free(uri);
1044 }
1045
1046 typedef struct _TMaintMove TMaintMove;
1047 struct _TMaintMove
1048 {
1049         gchar *source;
1050         gchar *dest;
1051
1052         ThumbLoaderStd *tl;
1053         gchar *source_uri;
1054         gchar *thumb_path;
1055
1056         gint pass;
1057 };
1058
1059 static GList *thumb_std_maint_move_list = NULL;
1060 static GList *thumb_std_maint_move_tail = NULL;
1061
1062
1063 static void thumb_std_maint_move_step(TMaintMove *tm);
1064 static gboolean thumb_std_maint_move_idle(gpointer data);
1065
1066
1067 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
1068 {
1069         TMaintMove *tm = data;
1070         GdkPixbuf *pixbuf;
1071
1072         /* get the original thumbnail pixbuf (unrotated, with original options)
1073            this is called from image_loader done callback, so tm->tl->il must exist*/
1074         pixbuf = image_loader_get_pixbuf(tm->tl->il);
1075         if (pixbuf)
1076                 {
1077                 const gchar *uri;
1078                 const gchar *mtime_str;
1079
1080                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1081                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1082
1083                 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1084                         {
1085                         gchar *pathl;
1086
1087                         /* The validation utility abuses ThumbLoader, and we
1088                          * abuse the utility just to load the thumbnail,
1089                          * but the loader needs to look sane for the save to complete.
1090                          */
1091
1092                         tm->tl->cache_enable = TRUE;
1093                         tm->tl->cache_hit = FALSE;
1094                         tm->tl->cache_local = FALSE;
1095                         file_data_unref(tm->tl->fd);
1096                         tm->tl->fd = file_data_new_group(tm->dest);
1097                         tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1098
1099                         pathl = path_from_utf8(tm->tl->fd->path);
1100                         g_free(tm->tl->thumb_uri);
1101                         tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1102                         tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1103                         g_free(pathl);
1104
1105                         g_free(tm->tl->thumb_path);
1106                         tm->tl->thumb_path = NULL;
1107                         tm->tl->thumb_path_local = FALSE;
1108
1109                         DEBUG_1("thumb move attempting save:");
1110
1111                         thumb_loader_std_save(tm->tl, pixbuf);
1112                         }
1113
1114                 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1115                 unlink_file(tm->thumb_path);
1116                 }
1117
1118         thumb_std_maint_move_step(tm);
1119 }
1120
1121 static void thumb_std_maint_move_step(TMaintMove *tm)
1122 {
1123         const gchar *folder;
1124
1125         tm->pass++;
1126         if (tm->pass > 2)
1127                 {
1128                 g_free(tm->source);
1129                 g_free(tm->dest);
1130                 g_free(tm->source_uri);
1131                 g_free(tm->thumb_path);
1132                 g_free(tm);
1133
1134                 if (thumb_std_maint_move_list)
1135                         {
1136                         g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1137                         }
1138
1139                 return;
1140                 }
1141
1142         folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1143
1144         g_free(tm->thumb_path);
1145         tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1146         tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1147                                                       thumb_std_maint_move_validate_cb, tm);
1148 }
1149
1150 static gboolean thumb_std_maint_move_idle(gpointer data)
1151 {
1152         TMaintMove *tm;
1153         gchar *pathl;
1154
1155         if (!thumb_std_maint_move_list) return FALSE;
1156
1157         tm = thumb_std_maint_move_list->data;
1158
1159         thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1160         if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1161
1162         pathl = path_from_utf8(tm->source);
1163         tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1164         g_free(pathl);
1165
1166         tm->pass = 0;
1167
1168         thumb_std_maint_move_step(tm);
1169
1170         return FALSE;
1171 }
1172
1173 /* This will schedule a move of the thumbnail for source image to dest when idle.
1174  * We do this so that file renaming or moving speed is not sacrificed by
1175  * moving the thumbnails at the same time because:
1176  *
1177  * This cache design requires the tedious task of loading the png thumbnails and saving them.
1178  *
1179  * The thumbnails are processed when the app is idle. If the app
1180  * exits early well too bad - they can simply be regenerated from scratch.
1181  *
1182  * This does not manage local thumbnails (fixme ?)
1183  */
1184 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1185 {
1186         TMaintMove *tm;
1187
1188         tm = g_new0(TMaintMove, 1);
1189         tm->source = g_strdup(source);
1190         tm->dest = g_strdup(dest);
1191
1192         if (!thumb_std_maint_move_list)
1193                 {
1194                 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1195                 }
1196
1197         if (thumb_std_maint_move_tail)
1198                 {
1199                 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1200                 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1201                 }
1202         else
1203                 {
1204                 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1205                 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1206                 }
1207 }
1208 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */