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