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