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