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