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