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