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