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