fixed moving and maintenance of std. thumbnails
[geeqie.git] / src / thumb_standard.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 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 static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path);
70
71
72 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
73 {
74         ThumbLoaderStd *tl;
75
76         tl = g_new0(ThumbLoaderStd, 1);
77
78         tl->standard_loader = TRUE;
79         tl->requested_width = width;
80         tl->requested_height = height;
81         tl->cache_enable = options->thumbnails.enable_caching;
82
83         return tl;
84 }
85
86 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
87                                     ThumbLoaderStdFunc func_done,
88                                     ThumbLoaderStdFunc func_error,
89                                     ThumbLoaderStdFunc func_progress,
90                                     gpointer data)
91 {
92         if (!tl) return;
93
94         tl->func_done = func_done;
95         tl->func_error = func_error;
96         tl->func_progress = func_progress;
97         tl->data = data;
98 }
99
100 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
101 {
102         image_loader_free(tl->il);
103         tl->il = NULL;
104
105         file_data_unref(tl->fd);
106         tl->fd = NULL;
107
108         g_free(tl->thumb_path);
109         tl->thumb_path = NULL;
110
111         g_free(tl->thumb_uri);
112         tl->thumb_uri = NULL;
113         tl->local_uri = NULL;
114
115         tl->thumb_path_local = FALSE;
116
117         tl->cache_hit = FALSE;
118
119         tl->source_mtime = 0;
120         tl->source_size = 0;
121         tl->source_mode = 0;
122
123         tl->progress = 0.0;
124 }
125
126 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
127                                    const gchar *cache_subfolder)
128 {
129         gchar *result = NULL;
130         gchar *md5_text;
131         guchar digest[16];
132         gchar *name;
133
134         if (!path || !uri || !cache_subfolder) return NULL;
135
136         md5_get_digest((guchar *)uri, strlen(uri), digest);
137         md5_text = md5_digest_to_text(digest);
138
139         if (!md5_text) return NULL;
140
141         name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
142
143         if (local)
144                 {
145                 gchar *base = remove_level_from_path(path);
146
147                 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
148                 g_free(base);
149                 }
150         else
151                 {
152                 result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL);
153                 }
154
155         g_free(name);
156         g_free(md5_text);
157
158         return result;
159 }
160
161 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
162 {
163         const gchar *folder;
164         gint w, h;
165
166         if (!tl->fd || !tl->thumb_uri) return NULL;
167
168         if (pixbuf)
169                 {
170                 w = gdk_pixbuf_get_width(pixbuf);
171                 h = gdk_pixbuf_get_height(pixbuf);
172                 }
173         else
174                 {
175                 w = tl->requested_width;
176                 h = tl->requested_height;
177                 }
178
179         if (fail)
180                 {
181                 folder = THUMB_FOLDER_FAIL;
182                 }
183         else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
184                 {
185                 folder = THUMB_FOLDER_LARGE;
186                 }
187         else
188                 {
189                 folder = THUMB_FOLDER_NORMAL;
190                 }
191
192         return thumb_std_cache_path(tl->fd->path,
193                                     (local) ?  tl->local_uri : tl->thumb_uri,
194                                     local, folder);
195 }
196
197 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
198 {
199         gchar *fail_path;
200         gboolean result = FALSE;
201
202         fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
203         if (isfile(fail_path))
204                 {
205                 GdkPixbuf *pixbuf;
206
207                 if (tl->cache_retry)
208                         {
209                         pixbuf = NULL;
210                         }
211                 else
212                         {
213                         gchar *pathl;
214
215                         pathl = path_from_utf8(fail_path);
216                         pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
217                         g_free(pathl);
218                         }
219
220                 if (pixbuf)
221                         {
222                         const gchar *mtime_str;
223
224                         mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
225                         if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
226                                 {
227                                 result = TRUE;
228                                 DEBUG_1("thumb fail valid: %s", tl->fd->path);
229                                 DEBUG_1("           thumb: %s", fail_path);
230                                 }
231
232                         g_object_unref(G_OBJECT(pixbuf));
233                         }
234
235                 if (!result) unlink_file(fail_path);
236                 }
237         g_free(fail_path);
238
239         return result;
240 }
241
242 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
243 {
244         const gchar *valid_uri;
245         const gchar *uri;
246         const gchar *mtime_str;
247         time_t mtime;
248         gint w, h;
249
250         if (!pixbuf) return FALSE;
251
252         w = gdk_pixbuf_get_width(pixbuf);
253         h = gdk_pixbuf_get_height(pixbuf);
254
255         if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
256             h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
257
258         valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
259
260         uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
261         mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
262
263         if (!mtime_str || !uri || !valid_uri) return FALSE;
264         if (strcmp(uri, valid_uri) != 0) return FALSE;
265
266         mtime = strtol(mtime_str, NULL, 10);
267         if (tl->source_mtime != mtime) return FALSE;
268
269         return TRUE;
270 }
271
272 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
273 {
274         gchar *base_path;
275         gchar *tmp_path;
276         gboolean fail;
277
278         if (!tl->cache_enable || tl->cache_hit) return;
279         if (tl->thumb_path) return;
280
281         if (!pixbuf)
282                 {
283                 /* local failures are not stored */
284                 if (tl->cache_local) return;
285
286                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
287                 fail = TRUE;
288                 }
289         else
290                 {
291                 g_object_ref(G_OBJECT(pixbuf));
292                 fail = FALSE;
293                 }
294
295         tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
296         if (!tl->thumb_path)
297                 {
298                 g_object_unref(G_OBJECT(pixbuf));
299                 return;
300                 }
301         tl->thumb_path_local = tl->cache_local;
302
303         /* create thumbnail dir if needed */
304         base_path = remove_level_from_path(tl->thumb_path);
305         if (tl->cache_local)
306                 {
307                 if (!isdir(base_path))
308                         {
309                         struct stat st;
310                         gchar *source_base;
311
312                         source_base = remove_level_from_path(tl->fd->path);
313                         if (stat_utf8(source_base, &st))
314                                 {
315                                 recursive_mkdir_if_not_exists(base_path, st.st_mode);
316                                 }
317                         g_free(source_base);
318                         }
319                 }
320         else
321                 {
322                 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
323                 }
324         g_free(base_path);
325
326         DEBUG_1("thumb saving: %s", tl->fd->path);
327         DEBUG_1("       saved: %s", tl->thumb_path);
328
329         /* save thumb, using a temp file then renaming into place */
330         tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
331         if (tmp_path)
332                 {
333                 const gchar *mark_uri;
334                 gchar *mark_app;
335                 gchar *mark_mtime;
336                 gchar *pathl;
337                 gboolean success;
338
339                 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
340
341                 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
342                 mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
343
344                 pathl = path_from_utf8(tmp_path);
345                 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
346                                           THUMB_MARKER_URI, mark_uri,
347                                           THUMB_MARKER_MTIME, mark_mtime,
348                                           THUMB_MARKER_APP, mark_app,
349                                           NULL);
350                 if (success)
351                         {
352                         chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
353                         success = rename_file(tmp_path, tl->thumb_path);
354                         }
355
356                 g_free(pathl);
357
358                 g_free(mark_mtime);
359                 g_free(mark_app);
360
361                 g_free(tmp_path);
362                 if (!success)
363                         {
364                         DEBUG_1("thumb save failed: %s", tl->fd->path);
365                         DEBUG_1("            thumb: %s", tl->thumb_path);
366                         }
367
368                 }
369
370         g_object_unref(G_OBJECT(pixbuf));
371 }
372
373 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
374 {
375         if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
376         tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
377 }
378
379 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
380 {
381         GdkPixbuf *pixbuf_thumb = NULL;
382         GdkPixbuf *result;
383         GdkPixbuf *rotated = NULL;
384         gint sw, sh;
385
386
387         if (!tl->cache_hit && options->image.exif_rotate_enable)
388                 {
389                 if (!tl->fd->exif_orientation)
390                         {
391                         tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
392                         }
393                 
394                 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
395                         {
396                         rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
397                         pixbuf = rotated;
398                         }
399                 }
400
401         sw = gdk_pixbuf_get_width(pixbuf);
402         sh = gdk_pixbuf_get_height(pixbuf);
403
404         if (tl->cache_enable)
405                 {
406                 if (!tl->cache_hit)
407                         {
408                         gint cache_w, cache_h;
409
410                         if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
411                                 {
412                                 cache_w = cache_h = THUMB_SIZE_LARGE;
413                                 }
414                         else
415                                 {
416                                 cache_w = cache_h = THUMB_SIZE_NORMAL;
417                                 }
418
419                         if (sw > cache_w || sh > cache_h || shrunk)
420                                 {
421                                 gint thumb_w, thumb_h;
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                                 thumb_loader_std_save(tl, pixbuf_thumb);
436                                 }
437                         }
438                 else if (tl->cache_hit &&
439                          tl->cache_local && !tl->thumb_path_local)
440                         {
441                         /* A local cache save was requested, but a valid thumb is in $HOME,
442                          * so specifically save as a local thumbnail.
443                          */
444                         g_free(tl->thumb_path);
445                         tl->thumb_path = NULL;
446
447                         tl->cache_hit = FALSE;
448
449                         DEBUG_1("thumb copied: %s", tl->fd->path);
450
451                         thumb_loader_std_save(tl, pixbuf);
452                         }
453                 }
454
455         if (sw <= tl->requested_width && sh <= tl->requested_height)
456                 {
457                 result = pixbuf;
458                 g_object_ref(result);
459                 }
460         else
461                 {
462                 gint thumb_w, thumb_h;
463
464                 if (pixbuf_thumb)
465                         {
466                         pixbuf = pixbuf_thumb;
467                         sw = gdk_pixbuf_get_width(pixbuf);
468                         sh = gdk_pixbuf_get_height(pixbuf);
469                         }
470
471                 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
472                                                   &thumb_w, &thumb_h))
473                         {
474                         result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
475                                                          (GdkInterpType)options->thumbnails.quality);
476                         }
477                 else
478                         {
479                         result = pixbuf;
480                         g_object_ref(result);
481                         }
482                 }
483
484         if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
485         if (rotated) g_object_unref(rotated);
486
487         return result;
488 }
489
490 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
491 {
492         image_loader_free(tl->il);
493         tl->il = NULL;
494
495         if (tl->thumb_path)
496                 {
497                 if (!tl->thumb_path_local && remove_broken)
498                         {
499                         DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
500                         unlink_file(tl->thumb_path);
501                         }
502
503                 g_free(tl->thumb_path);
504                 tl->thumb_path = NULL;
505
506                 if (!tl->thumb_path_local)
507                         {
508                         tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
509                         if (isfile(tl->thumb_path) && thumb_loader_std_setup_path(tl, tl->thumb_path))
510                                 {
511                                 tl->thumb_path_local = TRUE;
512                                 return TRUE;
513                                 }
514
515                         g_free(tl->thumb_path);
516                         tl->thumb_path = NULL;
517                         }
518
519                 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
520                 }
521
522         thumb_loader_std_save(tl, NULL);
523         return FALSE;
524 }
525
526 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
527 {
528         ThumbLoaderStd *tl = data;
529         GdkPixbuf *pixbuf;
530
531         DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
532         DEBUG_1("            from: %s", image_loader_get_fd(tl->il)->path);
533
534         pixbuf = image_loader_get_pixbuf(tl->il);
535         if (!pixbuf)
536                 {
537                 DEBUG_1("...but no pixbuf");
538                 thumb_loader_std_error_cb(il, data);
539                 return;
540                 }
541
542         if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
543                 {
544                 if (thumb_loader_std_next_source(tl, TRUE)) return;
545
546                 if (tl->func_error) tl->func_error(tl, tl->data);
547                 return;
548                 }
549
550         tl->cache_hit = (tl->thumb_path != NULL);
551
552         if (tl->fd)
553                 {
554                 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
555                 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
556                 }
557
558         if (tl->func_done) tl->func_done(tl, tl->data);
559 }
560
561 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
562 {
563         ThumbLoaderStd *tl = data;
564
565         /* if at least some of the image is available, go to done */
566         if (image_loader_get_pixbuf(tl->il) != NULL)
567                 {
568                 thumb_loader_std_done_cb(il, data);
569                 return;
570                 }
571         
572         DEBUG_1("thumb image error: %s", tl->fd->path);
573         DEBUG_1("             from: %s", image_loader_get_fd(tl->il)->path);
574
575         if (thumb_loader_std_next_source(tl, TRUE)) return;
576
577         thumb_loader_std_set_fallback(tl);
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 gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
592 {
593         tl->il = image_loader_new(fd);
594         image_loader_set_priority(tl->il, G_PRIORITY_LOW);
595
596         /* this will speed up jpegs by up to 3x in some cases */
597         if (tl->requested_width <= THUMB_SIZE_NORMAL &&
598             tl->requested_height <= THUMB_SIZE_NORMAL)
599                 {
600                 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
601                 }
602         else
603                 {
604                 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
605                 }
606
607         g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
608         if (tl->func_progress)
609                 {
610                 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
611                 }
612         g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
613
614         if (image_loader_start(tl->il))
615                 {
616                 return TRUE;
617                 }
618
619         image_loader_free(tl->il);
620         tl->il = NULL;
621         return FALSE;
622 }
623
624 static gboolean thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path)
625 {
626         FileData *fd = file_data_new_simple(path);
627         gboolean ret = thumb_loader_std_setup(tl, fd);
628         file_data_unref(fd);
629         return ret;
630 }
631
632 /*
633  * Note: Currently local_cache only specifies where to save a _new_ thumb, if
634  *       a valid existing thumb is found anywhere the local thumb will not be created.
635  */
636 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
637 {
638         if (!tl) return;
639
640         tl->cache_enable = enable_cache;
641         tl->cache_local = local;
642         tl->cache_retry = retry_failed;
643 }
644
645 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
646 {
647         static gchar *thumb_cache = NULL;
648         struct stat st;
649
650         if (!tl || !fd) return FALSE;
651
652         thumb_loader_std_reset(tl);
653
654
655         tl->fd = file_data_ref(fd);
656         if (!stat_utf8(fd->path, &st))
657                 {
658                 thumb_loader_std_set_fallback(tl);
659                 return FALSE;
660                 }
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_build_filename(homedir(), THUMB_FOLDER_GLOBAL, NULL);
666         if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
667                 {
668                 gchar *pathl;
669
670                 pathl = path_from_utf8(fd->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_path(tl, tl->thumb_path)) return TRUE;
685
686                 if (thumb_loader_std_fail_check(tl) ||
687                     !thumb_loader_std_next_source(tl, found))
688                         {
689                         thumb_loader_std_set_fallback(tl);
690                         return FALSE;
691                         }
692                 return TRUE;
693                 }
694
695         if (!thumb_loader_std_setup(tl, tl->fd))
696                 {
697                 thumb_loader_std_save(tl, NULL);
698                 thumb_loader_std_set_fallback(tl);
699                 return FALSE;
700                 }
701
702         return TRUE;
703 }
704
705 void thumb_loader_std_free(ThumbLoaderStd *tl)
706 {
707         if (!tl) return;
708
709         thumb_loader_std_reset(tl);
710         g_free(tl);
711 }
712
713 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
714 {
715         GdkPixbuf *pixbuf;
716
717         if (tl && tl->fd && tl->fd->thumb_pixbuf)
718                 {
719                 pixbuf = tl->fd->thumb_pixbuf;
720                 g_object_ref(pixbuf);
721                 }
722         else
723                 {
724                 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
725                 }
726
727         return pixbuf;
728 }
729
730
731 typedef struct _ThumbValidate ThumbValidate;
732 struct _ThumbValidate
733 {
734         ThumbLoaderStd *tl;
735         gchar *path;
736         gint days;
737
738         void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
739         gpointer data;
740
741         guint idle_id; /* event source id */
742 };
743
744 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
745 {
746         thumb_loader_std_free(tv->tl);
747         g_free(tv->path);
748         g_free(tv);
749 }
750
751 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
752 {
753         ThumbValidate *tv;
754
755         if (!tl) return;
756
757         tv = tl->data;
758
759         if (tv->idle_id)
760                 {
761                 g_source_remove(tv->idle_id);
762                 tv->idle_id = 0;
763                 }
764
765         thumb_loader_std_thumb_file_validate_free(tv);
766 }
767
768 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
769 {
770         if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
771
772         thumb_loader_std_thumb_file_validate_free(tv);
773 }
774
775 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
776 {
777         ThumbValidate *tv = data;
778         GdkPixbuf *pixbuf;
779         gboolean valid = FALSE;
780
781         /* get the original thumbnail pixbuf (unrotated, with original options) 
782            this is called from image_loader done callback, so tv->tl->il must exist*/
783         pixbuf = image_loader_get_pixbuf(tv->tl->il); 
784         if (pixbuf)
785                 {
786                 const gchar *uri;
787                 const gchar *mtime_str;
788
789                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
790                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
791                 if (uri && mtime_str)
792                         {
793                         if (strncmp(uri, "file:", strlen("file:")) == 0)
794                                 {
795                                 struct stat st;
796                                 gchar *target;
797
798                                 target = g_filename_from_uri(uri, NULL, NULL);
799                                 if (stat(target, &st) == 0 &&
800                                     st.st_mtime == strtol(mtime_str, NULL, 10))
801                                         {
802                                         valid = TRUE;
803                                         }
804                                 g_free(target);
805                                 }
806                         else
807                                 {
808                                 struct stat st;
809
810                                 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
811
812                                 if (stat_utf8(tv->path, &st))
813                                         {
814                                         time_t now;
815
816                                         now = time(NULL);
817                                         if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
818                                                 {
819                                                 valid = TRUE;
820                                                 }
821                                         }
822                                 }
823                         }
824                 else
825                         {
826                         DEBUG_1("invalid image found in std cache: %s", tv->path);
827                         }
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 gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
841 {
842         ThumbValidate *tv = data;
843
844         tv->idle_id = 0;
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, gboolean 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_path(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 = 0;
878                 }
879
880         return tv->tl;
881 }
882
883 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean 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 gboolean thumb_std_maint_move_idle(gpointer data);
939
940
941 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
942 {
943         TMaintMove *tm = data;
944         GdkPixbuf *pixbuf;
945
946         /* get the original thumbnail pixbuf (unrotated, with original options) 
947            this is called from image_loader done callback, so tm->tl->il must exist*/
948         pixbuf = image_loader_get_pixbuf(tm->tl->il); 
949         if (pixbuf)
950                 {
951                 const gchar *uri;
952                 const gchar *mtime_str;
953
954                 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
955                 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
956
957                 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
958                         {
959                         gchar *pathl;
960
961                         /* The validation utility abuses ThumbLoader, and we
962                          * abuse the utility just to load the thumbnail,
963                          * but the loader needs to look sane for the save to complete.
964                          */
965
966                         tm->tl->cache_enable = TRUE;
967                         tm->tl->cache_hit = FALSE;
968                         tm->tl->cache_local = FALSE;
969
970                         file_data_unref(tm->tl->fd);
971                         tm->tl->fd = file_data_new_simple(tm->dest);
972                         tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
973
974                         pathl = path_from_utf8(tm->tl->fd->path);
975                         g_free(tm->tl->thumb_uri);
976                         tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
977                         tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
978                         g_free(pathl);
979
980                         g_free(tm->tl->thumb_path);
981                         tm->tl->thumb_path = NULL;
982                         tm->tl->thumb_path_local = FALSE;
983
984                         DEBUG_1("thumb move attempting save:");
985
986                         thumb_loader_std_save(tm->tl, pixbuf);
987                         }
988
989                 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
990                 unlink_file(tm->thumb_path);
991                 }
992
993         thumb_std_maint_move_step(tm);
994 }
995
996 static void thumb_std_maint_move_step(TMaintMove *tm)
997 {
998         const gchar *folder;
999
1000         tm->pass++;
1001         if (tm->pass > 2)
1002                 {
1003                 g_free(tm->source);
1004                 g_free(tm->dest);
1005                 g_free(tm->source_uri);
1006                 g_free(tm->thumb_path);
1007                 g_free(tm);
1008
1009                 if (thumb_std_maint_move_list)
1010                         {
1011                         g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1012                         }
1013
1014                 return;
1015                 }
1016
1017         folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1018
1019         g_free(tm->thumb_path);
1020         tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1021         tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1022                                                       thumb_std_maint_move_validate_cb, tm);
1023 }
1024
1025 static gboolean thumb_std_maint_move_idle(gpointer data)
1026 {
1027         TMaintMove *tm;
1028         gchar *pathl;
1029
1030         if (!thumb_std_maint_move_list) return FALSE;
1031
1032         tm = thumb_std_maint_move_list->data;
1033
1034         thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1035         if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1036
1037         pathl = path_from_utf8(tm->source);
1038         tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1039         g_free(pathl);
1040
1041         tm->pass = 0;
1042
1043         thumb_std_maint_move_step(tm);
1044
1045         return FALSE;
1046 }
1047
1048 /* This will schedule a move of the thumbnail for source image to dest when idle.
1049  * We do this so that file renaming or moving speed is not sacrificed by
1050  * moving the thumbnails at the same time because:
1051  *
1052  * This cache design requires the tedious task of loading the png thumbnails and saving them.
1053  *
1054  * The thumbnails are processed when the app is idle. If the app
1055  * exits early well too bad - they can simply be regenerated from scratch.
1056  *
1057  * This does not manage local thumbnails (fixme ?)
1058  */
1059 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1060 {
1061         TMaintMove *tm;
1062
1063         tm = g_new0(TMaintMove, 1);
1064         tm->source = g_strdup(source);
1065         tm->dest = g_strdup(dest);
1066
1067         if (!thumb_std_maint_move_list)
1068                 {
1069                 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1070                 }
1071
1072         if (thumb_std_maint_move_tail)
1073                 {
1074                 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1075                 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1076                 }
1077         else
1078                 {
1079                 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1080                 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1081                 }
1082 }
1083 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */