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