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