Fix #541: Showing existing, or maybe generating thumbnails for MP4 and WEBM
[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("%lu", tl->source_mtime);
352
353                 pathl = path_from_utf8(tmp_path);
354                 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
355                                           THUMB_MARKER_URI, mark_uri,
356                                           THUMB_MARKER_MTIME, mark_mtime,
357                                           THUMB_MARKER_APP, mark_app,
358                                           NULL);
359                 if (success)
360                         {
361                         chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
362                         success = rename_file(tmp_path, tl->thumb_path);
363                         }
364
365                 g_free(pathl);
366
367                 g_free(mark_mtime);
368                 g_free(mark_app);
369
370                 g_free(tmp_path);
371                 if (!success)
372                         {
373                         DEBUG_1("thumb save failed: %s", tl->fd->path);
374                         DEBUG_1("            thumb: %s", tl->thumb_path);
375                         }
376
377                 }
378
379         g_object_unref(G_OBJECT(pixbuf));
380 }
381
382 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
383 {
384         if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
385         tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
386 }
387
388 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
389 {
390         GdkPixbuf *pixbuf_thumb = NULL;
391         GdkPixbuf *result;
392         GdkPixbuf *rotated = NULL;
393         gint sw, sh;
394
395
396         if (!tl->cache_hit && options->image.exif_rotate_enable)
397                 {
398                 if (!tl->fd->exif_orientation)
399                         {
400                         tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
401                         }
402
403                 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
404                         {
405                         rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
406                         pixbuf = rotated;
407                         }
408                 }
409
410         sw = gdk_pixbuf_get_width(pixbuf);
411         sh = gdk_pixbuf_get_height(pixbuf);
412
413         if (tl->cache_enable)
414                 {
415                 if (!tl->cache_hit)
416                         {
417                         gint cache_w, cache_h;
418
419                         if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
420                                 {
421                                 cache_w = cache_h = THUMB_SIZE_LARGE;
422                                 }
423                         else
424                                 {
425                                 cache_w = cache_h = THUMB_SIZE_NORMAL;
426                                 }
427
428                         if (sw > cache_w || sh > cache_h || shrunk)
429                                 {
430                                 gint thumb_w, thumb_h;
431                                 struct stat st;
432
433                                 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
434                                                                   &thumb_w, &thumb_h))
435                                         {
436                                         pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
437                                                                                (GdkInterpType)options->thumbnails.quality);
438                                         }
439                                 else
440                                         {
441                                         pixbuf_thumb = pixbuf;
442                                         g_object_ref(G_OBJECT(pixbuf_thumb));
443                                         }
444
445                                 /* do not save the thumbnail if the source file has changed meanwhile -
446                                    the thumbnail is most probably broken */
447                                 if (stat_utf8(tl->fd->path, &st) &&
448                                     tl->source_mtime == st.st_mtime &&
449                                     tl->source_size == st.st_size)
450                                         {
451                                         thumb_loader_std_save(tl, pixbuf_thumb);
452                                         }
453                                 }
454                         }
455                 else if (tl->cache_hit &&
456                          tl->cache_local && !tl->thumb_path_local)
457                         {
458                         /* A local cache save was requested, but a valid thumb is in $HOME,
459                          * so specifically save as a local thumbnail.
460                          */
461                         g_free(tl->thumb_path);
462                         tl->thumb_path = NULL;
463
464                         tl->cache_hit = FALSE;
465
466                         DEBUG_1("thumb copied: %s", tl->fd->path);
467
468                         thumb_loader_std_save(tl, pixbuf);
469                         }
470                 }
471
472         if (sw <= tl->requested_width && sh <= tl->requested_height)
473                 {
474                 result = pixbuf;
475                 g_object_ref(result);
476                 }
477         else
478                 {
479                 gint thumb_w, thumb_h;
480
481                 if (pixbuf_thumb)
482                         {
483                         pixbuf = pixbuf_thumb;
484                         sw = gdk_pixbuf_get_width(pixbuf);
485                         sh = gdk_pixbuf_get_height(pixbuf);
486                         }
487
488                 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
489                                                   &thumb_w, &thumb_h))
490                         {
491                         result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
492                                                          (GdkInterpType)options->thumbnails.quality);
493                         }
494                 else
495                         {
496                         result = pixbuf;
497                         g_object_ref(result);
498                         }
499                 }
500
501         if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
502         if (rotated) g_object_unref(rotated);
503
504         return result;
505 }
506
507 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
508 {
509         image_loader_free(tl->il);
510         tl->il = NULL;
511
512         if (tl->thumb_path)
513                 {
514                 if (!tl->thumb_path_local && remove_broken)
515                         {
516                         DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
517                         unlink_file(tl->thumb_path);
518                         }
519
520                 g_free(tl->thumb_path);
521                 tl->thumb_path = NULL;
522
523                 if (!tl->thumb_path_local)
524                         {
525                         tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
526                         if (isfile(tl->thumb_path))
527                                 {
528                                 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
529                                 if (thumb_loader_std_setup(tl, fd))
530                                         {
531                                         file_data_unref(fd);
532                                         tl->thumb_path_local = TRUE;
533                                         return TRUE;
534                                         }
535                                 file_data_unref(fd);
536                                 }
537
538                         g_free(tl->thumb_path);
539                         tl->thumb_path = NULL;
540                         }
541
542                 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
543                 }
544
545         thumb_loader_std_save(tl, NULL);
546         return FALSE;
547 }
548
549 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
550 {
551         ThumbLoaderStd *tl = data;
552         GdkPixbuf *pixbuf;
553
554         DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
555         DEBUG_1("            from: %s", image_loader_get_fd(tl->il)->path);
556
557         pixbuf = image_loader_get_pixbuf(tl->il);
558         if (!pixbuf)
559                 {
560                 DEBUG_1("...but no pixbuf");
561                 thumb_loader_std_error_cb(il, data);
562                 return;
563                 }
564
565         if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
566                 {
567                 if (thumb_loader_std_next_source(tl, TRUE)) return;
568
569                 if (tl->func_error) tl->func_error(tl, tl->data);
570                 return;
571                 }
572
573         tl->cache_hit = (tl->thumb_path != NULL);
574
575         if (tl->fd)
576                 {
577                 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
578                 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
579                 }
580
581         if (tl->func_done) tl->func_done(tl, tl->data);
582 }
583
584 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
585 {
586         ThumbLoaderStd *tl = data;
587
588         /* if at least some of the image is available, go to done */
589         if (image_loader_get_pixbuf(tl->il) != NULL)
590                 {
591                 thumb_loader_std_done_cb(il, data);
592                 return;
593                 }
594
595         DEBUG_1("thumb image error: %s", tl->fd->path);
596         DEBUG_1("             from: %s", image_loader_get_fd(tl->il)->path);
597
598         if (thumb_loader_std_next_source(tl, TRUE)) return;
599
600         thumb_loader_std_set_fallback(tl);
601
602         if (tl->func_error) tl->func_error(tl, tl->data);
603 }
604
605 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
606 {
607         ThumbLoaderStd *tl = data;
608
609         tl->progress = (gdouble)percent;
610
611         if (tl->func_progress) tl->func_progress(tl, tl->data);
612 }
613
614 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
615 {
616         tl->il = image_loader_new(fd);
617         image_loader_set_priority(tl->il, G_PRIORITY_LOW);
618
619         /* this will speed up jpegs by up to 3x in some cases */
620         if (tl->requested_width <= THUMB_SIZE_NORMAL &&
621             tl->requested_height <= THUMB_SIZE_NORMAL)
622                 {
623                 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
624                 }
625         else
626                 {
627                 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
628                 }
629
630         g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
631         if (tl->func_progress)
632                 {
633                 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
634                 }
635         g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
636
637         if (image_loader_start(tl->il))
638                 {
639                 return TRUE;
640                 }
641
642         image_loader_free(tl->il);
643         tl->il = NULL;
644         return FALSE;
645 }
646
647 /*
648  * Note: Currently local_cache only specifies where to save a _new_ thumb, if
649  *       a valid existing thumb is found anywhere the local thumb will not be created.
650  */
651 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
652 {
653         if (!tl) return;
654
655         tl->cache_enable = enable_cache;
656         tl->cache_local = local;
657         tl->cache_retry = retry_failed;
658 }
659
660 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
661 {
662         static gchar *thumb_cache = NULL;
663         struct stat st;
664
665         if (!tl || !fd) return FALSE;
666
667         thumb_loader_std_reset(tl);
668
669
670         tl->fd = file_data_ref(fd);
671         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))
672                 {
673                 thumb_loader_std_set_fallback(tl);
674                 return FALSE;
675                 }
676         tl->source_mtime = st.st_mtime;
677         tl->source_size = st.st_size;
678         tl->source_mode = st.st_mode;
679
680         if (!thumb_cache)
681                 {
682                 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
683                 }
684
685         if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
686                 {
687                 gchar *pathl;
688
689                 pathl = path_from_utf8(fd->path);
690                 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
691                 tl->local_uri = filename_from_path(tl->thumb_uri);
692                 g_free(pathl);
693                 }
694
695         if (tl->cache_enable)
696                 {
697                 gint found;
698
699                 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
700                 tl->thumb_path_local = FALSE;
701
702                 found = isfile(tl->thumb_path);
703                 if (found)
704                         {
705                         FileData *fd = file_data_new_no_grouping(tl->thumb_path);
706                         if (thumb_loader_std_setup(tl, fd))
707                                 {
708                                 file_data_unref(fd);
709                                 return TRUE;
710                                 }
711                         file_data_unref(fd);
712                         }
713
714                 if (thumb_loader_std_fail_check(tl) ||
715                     !thumb_loader_std_next_source(tl, found))
716                         {
717                         thumb_loader_std_set_fallback(tl);
718                         return FALSE;
719                         }
720                 return TRUE;
721                 }
722
723         if (!thumb_loader_std_setup(tl, tl->fd))
724                 {
725                 thumb_loader_std_save(tl, NULL);
726                 thumb_loader_std_set_fallback(tl);
727                 return FALSE;
728                 }
729
730         return TRUE;
731 }
732
733 void thumb_loader_std_free(ThumbLoaderStd *tl)
734 {
735         if (!tl) return;
736
737         thumb_loader_std_reset(tl);
738         g_free(tl);
739 }
740
741 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
742 {
743         GdkPixbuf *pixbuf;
744
745         if (tl && tl->fd && tl->fd->thumb_pixbuf)
746                 {
747                 pixbuf = tl->fd->thumb_pixbuf;
748                 g_object_ref(pixbuf);
749                 }
750         else
751                 {
752                 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
753                 }
754
755         return pixbuf;
756 }
757
758
759 typedef struct _ThumbValidate ThumbValidate;
760 struct _ThumbValidate
761 {
762         ThumbLoaderStd *tl;
763         gchar *path;
764         gint days;
765
766         void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
767         gpointer data;
768
769         guint idle_id; /* event source id */
770 };
771
772 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
773 {
774         thumb_loader_std_free(tv->tl);
775         g_free(tv->path);
776         g_free(tv);
777 }
778
779 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
780 {
781         ThumbValidate *tv;
782
783         if (!tl) return;
784
785         tv = tl->data;
786
787         if (tv->idle_id)
788                 {
789                 g_source_remove(tv->idle_id);
790                 tv->idle_id = 0;
791                 }
792
793         thumb_loader_std_thumb_file_validate_free(tv);
794 }
795
796 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
797 {
798         if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
799
800         thumb_loader_std_thumb_file_validate_free(tv);
801 }
802
803 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
804 {
805         ThumbValidate *tv = data;
806         GdkPixbuf *pixbuf;
807         gboolean valid = FALSE;
808
809         /* get the original thumbnail pixbuf (unrotated, with original options)
810            this is called from image_loader done callback, so tv->tl->il must exist*/
811         pixbuf = image_loader_get_pixbuf(tv->tl->il);
812         if (pixbuf)
813                 {
814                 const gchar *uri;
815                 const gchar *mtime_str;
816
817                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
818                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
819                 if (uri && mtime_str)
820                         {
821                         if (strncmp(uri, "file:", strlen("file:")) == 0)
822                                 {
823                                 struct stat st;
824                                 gchar *target;
825
826                                 target = g_filename_from_uri(uri, NULL, NULL);
827                                 if (stat(target, &st) == 0 &&
828                                     st.st_mtime == strtol(mtime_str, NULL, 10))
829                                         {
830                                         valid = TRUE;
831                                         }
832                                 g_free(target);
833                                 }
834                         else
835                                 {
836                                 struct stat st;
837
838                                 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
839
840                                 if (stat_utf8(tv->path, &st))
841                                         {
842                                         time_t now;
843
844                                         now = time(NULL);
845                                         if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
846                                                 {
847                                                 valid = TRUE;
848                                                 }
849                                         }
850                                 }
851                         }
852                 else
853                         {
854                         DEBUG_1("invalid image found in std cache: %s", tv->path);
855                         }
856                 }
857
858         thumb_loader_std_thumb_file_validate_finish(tv, valid);
859 }
860
861 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
862 {
863         ThumbValidate *tv = data;
864
865         thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
866 }
867
868 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
869 {
870         ThumbValidate *tv = data;
871
872         tv->idle_id = 0;
873         thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
874
875         return FALSE;
876 }
877
878 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
879                                                      void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
880                                                      gpointer data)
881 {
882         ThumbValidate *tv;
883
884         tv = g_new0(ThumbValidate, 1);
885
886         tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
887         thumb_loader_std_set_callbacks(tv->tl,
888                                        thumb_loader_std_thumb_file_validate_done_cb,
889                                        thumb_loader_std_thumb_file_validate_error_cb,
890                                        NULL,
891                                        tv);
892         thumb_loader_std_reset(tv->tl);
893
894         tv->path = g_strdup(thumb_path);
895         tv->days = allowed_days;
896         tv->func_valid = func_valid;
897         tv->data = data;
898
899         FileData *fd = file_data_new_no_grouping(thumb_path);
900         if (!thumb_loader_std_setup(tv->tl, fd))
901                 {
902                 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
903                 }
904         else
905                 {
906                 tv->idle_id = 0;
907                 }
908
909         file_data_unref(fd);
910         return tv->tl;
911 }
912
913 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
914                                        const gchar *subfolder)
915 {
916         gchar *thumb_path;
917
918         thumb_path = thumb_std_cache_path(source,
919                                           (local) ? filename_from_path(uri) : uri,
920                                           local, subfolder);
921         if (isfile(thumb_path))
922                 {
923                 DEBUG_1("thumb removing: %s", thumb_path);
924                 unlink_file(thumb_path);
925                 }
926         g_free(thumb_path);
927 }
928
929 /* this also removes local thumbnails (the source is gone so it makes sense) */
930 void thumb_std_maint_removed(const gchar *source)
931 {
932         gchar *uri;
933         gchar *sourcel;
934
935         sourcel = path_from_utf8(source);
936         uri = g_filename_to_uri(sourcel, NULL, NULL);
937         g_free(sourcel);
938
939         /* all this to remove a thumbnail? */
940
941         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
942         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
943         thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
944         thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
945         thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
946
947         g_free(uri);
948 }
949
950 typedef struct _TMaintMove TMaintMove;
951 struct _TMaintMove
952 {
953         gchar *source;
954         gchar *dest;
955
956         ThumbLoaderStd *tl;
957         gchar *source_uri;
958         gchar *thumb_path;
959
960         gint pass;
961 };
962
963 static GList *thumb_std_maint_move_list = NULL;
964 static GList *thumb_std_maint_move_tail = NULL;
965
966
967 static void thumb_std_maint_move_step(TMaintMove *tm);
968 static gboolean thumb_std_maint_move_idle(gpointer data);
969
970
971 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
972 {
973         TMaintMove *tm = data;
974         GdkPixbuf *pixbuf;
975
976         /* get the original thumbnail pixbuf (unrotated, with original options)
977            this is called from image_loader done callback, so tm->tl->il must exist*/
978         pixbuf = image_loader_get_pixbuf(tm->tl->il);
979         if (pixbuf)
980                 {
981                 const gchar *uri;
982                 const gchar *mtime_str;
983
984                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
985                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
986
987                 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
988                         {
989                         gchar *pathl;
990
991                         /* The validation utility abuses ThumbLoader, and we
992                          * abuse the utility just to load the thumbnail,
993                          * but the loader needs to look sane for the save to complete.
994                          */
995
996                         tm->tl->cache_enable = TRUE;
997                         tm->tl->cache_hit = FALSE;
998                         tm->tl->cache_local = FALSE;
999                         file_data_unref(tm->tl->fd);
1000                         tm->tl->fd = file_data_new_group(tm->dest);
1001                         tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1002
1003                         pathl = path_from_utf8(tm->tl->fd->path);
1004                         g_free(tm->tl->thumb_uri);
1005                         tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1006                         tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1007                         g_free(pathl);
1008
1009                         g_free(tm->tl->thumb_path);
1010                         tm->tl->thumb_path = NULL;
1011                         tm->tl->thumb_path_local = FALSE;
1012
1013                         DEBUG_1("thumb move attempting save:");
1014
1015                         thumb_loader_std_save(tm->tl, pixbuf);
1016                         }
1017
1018                 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1019                 unlink_file(tm->thumb_path);
1020                 }
1021
1022         thumb_std_maint_move_step(tm);
1023 }
1024
1025 static void thumb_std_maint_move_step(TMaintMove *tm)
1026 {
1027         const gchar *folder;
1028
1029         tm->pass++;
1030         if (tm->pass > 2)
1031                 {
1032                 g_free(tm->source);
1033                 g_free(tm->dest);
1034                 g_free(tm->source_uri);
1035                 g_free(tm->thumb_path);
1036                 g_free(tm);
1037
1038                 if (thumb_std_maint_move_list)
1039                         {
1040                         g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1041                         }
1042
1043                 return;
1044                 }
1045
1046         folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1047
1048         g_free(tm->thumb_path);
1049         tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1050         tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1051                                                       thumb_std_maint_move_validate_cb, tm);
1052 }
1053
1054 static gboolean thumb_std_maint_move_idle(gpointer data)
1055 {
1056         TMaintMove *tm;
1057         gchar *pathl;
1058
1059         if (!thumb_std_maint_move_list) return FALSE;
1060
1061         tm = thumb_std_maint_move_list->data;
1062
1063         thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1064         if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1065
1066         pathl = path_from_utf8(tm->source);
1067         tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1068         g_free(pathl);
1069
1070         tm->pass = 0;
1071
1072         thumb_std_maint_move_step(tm);
1073
1074         return FALSE;
1075 }
1076
1077 /* This will schedule a move of the thumbnail for source image to dest when idle.
1078  * We do this so that file renaming or moving speed is not sacrificed by
1079  * moving the thumbnails at the same time because:
1080  *
1081  * This cache design requires the tedious task of loading the png thumbnails and saving them.
1082  *
1083  * The thumbnails are processed when the app is idle. If the app
1084  * exits early well too bad - they can simply be regenerated from scratch.
1085  *
1086  * This does not manage local thumbnails (fixme ?)
1087  */
1088 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1089 {
1090         TMaintMove *tm;
1091
1092         tm = g_new0(TMaintMove, 1);
1093         tm->source = g_strdup(source);
1094         tm->dest = g_strdup(dest);
1095
1096         if (!thumb_std_maint_move_list)
1097                 {
1098                 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1099                 }
1100
1101         if (thumb_std_maint_move_tail)
1102                 {
1103                 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1104                 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1105                 }
1106         else
1107                 {
1108                 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1109                 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1110                 }
1111 }
1112 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */