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