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