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