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