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