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