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