4 * Copyright (C) 2008 The Geeqie Team
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!
15 #include "thumb_standard.h"
17 #include "cache.h" /* for cache_ensure_dir_exists */
18 #include "image-load.h"
20 #include "pixbuf_util.h"
21 #include "ui_fileops.h"
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?)
33 * This code attempts to conform to version 0.7.0 of the standard.
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.
45 #define THUMB_SIZE_NORMAL 128
46 #define THUMB_SIZE_LARGE 256
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"
55 #define THUMB_PERMS_FOLDER 0700
56 #define THUMB_PERMS_THUMB 0600
61 *-----------------------------------------------------------------------------
63 *-----------------------------------------------------------------------------
67 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
68 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path);
71 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
75 tl = g_new0(ThumbLoaderStd, 1);
77 tl->standard_loader = TRUE;
79 tl->requested_width = width;
80 tl->requested_height = height;
85 tl->cache_enable = options->thumbnails.enable_caching;
86 tl->cache_local = FALSE;
87 tl->cache_retry = FALSE;
92 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
93 ThumbLoaderStdFunc func_done,
94 ThumbLoaderStdFunc func_error,
95 ThumbLoaderStdFunc func_progress,
100 tl->func_done = func_done;
101 tl->func_error = func_error;
102 tl->func_progress = func_progress;
106 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
108 image_loader_free(tl->il);
111 file_data_unref(tl->fd);
114 g_free(tl->thumb_path);
115 tl->thumb_path = NULL;
117 g_free(tl->thumb_uri);
118 tl->thumb_uri = NULL;
119 tl->local_uri = NULL;
121 tl->thumb_path_local = FALSE;
123 tl->cache_hit = FALSE;
125 tl->source_mtime = 0;
132 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint local,
133 const gchar *cache_subfolder)
135 gchar *result = NULL;
140 if (!path || !uri || !cache_subfolder) return NULL;
142 md5_get_digest((guchar *)uri, strlen(uri), digest);
143 md5_text = md5_digest_to_text(digest);
145 if (!md5_text) return NULL;
147 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
151 gchar *base = remove_level_from_path(path);
153 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
158 result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL);
167 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPixbuf *pixbuf, gint fail)
172 if (!tl->fd || !tl->thumb_uri) return NULL;
176 w = gdk_pixbuf_get_width(pixbuf);
177 h = gdk_pixbuf_get_height(pixbuf);
181 w = tl->requested_width;
182 h = tl->requested_height;
187 folder = THUMB_FOLDER_FAIL;
189 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
191 folder = THUMB_FOLDER_LARGE;
195 folder = THUMB_FOLDER_NORMAL;
198 return thumb_std_cache_path(tl->fd->path,
199 (local) ? tl->local_uri : tl->thumb_uri,
203 static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl)
208 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
209 if (isfile(fail_path))
221 pathl = path_from_utf8(fail_path);
222 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
228 const gchar *mtime_str;
230 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
231 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
234 DEBUG_1("thumb fail valid: %s", tl->fd->path);
235 DEBUG_1(" thumb: %s", fail_path);
238 g_object_unref(G_OBJECT(pixbuf));
241 if (!result) unlink_file(fail_path);
248 static gint thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
250 const gchar *valid_uri;
252 const gchar *mtime_str;
256 if (!pixbuf) return FALSE;
258 w = gdk_pixbuf_get_width(pixbuf);
259 h = gdk_pixbuf_get_height(pixbuf);
261 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
262 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
264 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
266 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
267 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
269 if (!mtime_str || !uri || !valid_uri) return FALSE;
270 if (strcmp(uri, valid_uri) != 0) return FALSE;
272 mtime = strtol(mtime_str, NULL, 10);
273 if (tl->source_mtime != mtime) return FALSE;
278 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
284 if (!tl->cache_enable || tl->cache_hit) return;
285 if (tl->thumb_path) return;
289 /* local failures are not stored */
290 if (tl->cache_local) return;
292 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
297 g_object_ref(G_OBJECT(pixbuf));
301 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
304 g_object_unref(G_OBJECT(pixbuf));
307 tl->thumb_path_local = tl->cache_local;
309 /* create thumbnail dir if needed */
310 base_path = remove_level_from_path(tl->thumb_path);
313 if (!isdir(base_path))
318 source_base = remove_level_from_path(tl->fd->path);
319 if (stat_utf8(source_base, &st))
321 cache_ensure_dir_exists(base_path, st.st_mode);
328 cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER);
332 DEBUG_1("thumb saving: %s", tl->fd->path);
333 DEBUG_1(" saved: %s", tl->thumb_path);
335 /* save thumb, using a temp file then renaming into place */
336 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
339 const gchar *mark_uri;
345 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
347 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
348 mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
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,
358 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
359 success = rename_file(tmp_path, tl->thumb_path);
370 DEBUG_1("thumb save failed: %s", tl->fd->path);
371 DEBUG_1(" thumb: %s", tl->thumb_path);
376 g_object_unref(G_OBJECT(pixbuf));
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)
382 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
385 *new_h = (gdouble)*new_w / old_w * old_h;
386 if (*new_h < 1) *new_h = 1;
391 *new_w = (gdouble)*new_h / old_h * old_w;
392 if (*new_w < 1) *new_w = 1;
395 return (*new_w != old_w || *new_h != old_h);
398 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk)
400 GdkPixbuf *pixbuf_thumb = NULL;
402 GdkPixbuf *rotated = NULL;
406 if (!tl->cache_hit && options->image.exif_rotate_enable)
408 if (!tl->fd->exif_orientation)
410 ExifData *exif = exif_read_fd(tl->fd);
413 if (exif && exif_get_integer(exif, "Exif.Image.Orientation", &orientation))
414 tl->fd->exif_orientation = orientation;
416 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
417 exif_free_fd(tl->fd, exif);
420 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
422 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
427 sw = gdk_pixbuf_get_width(pixbuf);
428 sh = gdk_pixbuf_get_height(pixbuf);
430 if (tl->cache_enable)
434 gint cache_w, cache_h;
436 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
438 cache_w = cache_h = THUMB_SIZE_LARGE;
442 cache_w = cache_h = THUMB_SIZE_NORMAL;
445 if (sw > cache_w || sh > cache_h || shrunk)
447 gint thumb_w, thumb_h;
449 if (thumb_loader_std_scale_aspect(cache_w, cache_h, sw, sh,
452 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
453 (GdkInterpType)options->thumbnails.quality);
457 pixbuf_thumb = pixbuf;
458 g_object_ref(G_OBJECT(pixbuf_thumb));
461 thumb_loader_std_save(tl, pixbuf_thumb);
464 else if (tl->cache_hit &&
465 tl->cache_local && !tl->thumb_path_local)
467 /* A local cache save was requested, but a valid thumb is in $HOME,
468 * so specifically save as a local thumbnail.
470 g_free(tl->thumb_path);
471 tl->thumb_path = NULL;
473 tl->cache_hit = FALSE;
475 DEBUG_1("thumb copied: %s", tl->fd->path);
477 thumb_loader_std_save(tl, pixbuf);
481 if (sw <= tl->requested_width && sh <= tl->requested_height)
484 g_object_ref(result);
488 gint thumb_w, thumb_h;
492 pixbuf = pixbuf_thumb;
493 sw = gdk_pixbuf_get_width(pixbuf);
494 sh = gdk_pixbuf_get_height(pixbuf);
497 if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
500 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
501 (GdkInterpType)options->thumbnails.quality);
506 g_object_ref(result);
510 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
511 if (rotated) g_object_unref(rotated);
516 static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken)
518 image_loader_free(tl->il);
523 if (!tl->thumb_path_local && remove_broken)
525 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
526 unlink_file(tl->thumb_path);
529 g_free(tl->thumb_path);
530 tl->thumb_path = NULL;
532 if (!tl->thumb_path_local)
534 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
535 if (isfile(tl->thumb_path) && thumb_loader_std_setup(tl, tl->thumb_path))
537 tl->thumb_path_local = TRUE;
541 g_free(tl->thumb_path);
542 tl->thumb_path = NULL;
545 if (thumb_loader_std_setup(tl, tl->fd->path)) return TRUE;
548 thumb_loader_std_save(tl, NULL);
552 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
554 ThumbLoaderStd *tl = data;
557 DEBUG_1("thumb image done: %s", tl->fd->path);
558 DEBUG_1(" from: %s", tl->il->path);
560 pixbuf = image_loader_get_pixbuf(tl->il);
563 DEBUG_1("...but no pixbuf");
564 thumb_loader_std_error_cb(il, data);
568 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
570 if (thumb_loader_std_next_source(tl, TRUE)) return;
572 if (tl->func_error) tl->func_error(tl, tl->data);
576 tl->cache_hit = (tl->thumb_path != NULL);
580 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
581 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, il->shrunk);
584 if (tl->func_done) tl->func_done(tl, tl->data);
587 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
589 ThumbLoaderStd *tl = data;
591 /* if at least some of the image is available, go to done */
592 if (image_loader_get_pixbuf(tl->il) != NULL)
594 thumb_loader_std_done_cb(il, data);
598 DEBUG_1("thumb image error: %s", tl->fd->path);
599 DEBUG_1(" from: %s", tl->il->fd->path);
601 if (thumb_loader_std_next_source(tl, TRUE)) return;
603 if (tl->func_error) tl->func_error(tl, tl->data);
606 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
608 ThumbLoaderStd *tl = data;
610 tl->progress = (gdouble)percent;
612 if (tl->func_progress) tl->func_progress(tl, tl->data);
615 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path)
617 tl->il = image_loader_new_from_path(path);
619 if (options->thumbnails.fast)
621 /* this will speed up jpegs by up to 3x in some cases */
622 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
623 tl->requested_height <= THUMB_SIZE_NORMAL)
625 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
629 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
633 image_loader_set_error_func(tl->il, thumb_loader_std_error_cb, tl);
634 if (tl->func_progress)
636 image_loader_set_percent_func(tl->il, thumb_loader_std_progress_cb, tl);
639 if (image_loader_start(tl->il, thumb_loader_std_done_cb, tl))
644 image_loader_free(tl->il);
650 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
651 * a valid existing thumb is found anywhere the local thumb will not be created.
653 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint local, gint retry_failed)
657 tl->cache_enable = enable_cache;
658 tl->cache_local = local;
659 tl->cache_retry = retry_failed;
662 gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
664 static gchar *thumb_cache = NULL;
667 if (!tl || !fd) return FALSE;
669 thumb_loader_std_reset(tl);
671 if (!stat_utf8(fd->path, &st)) return FALSE;
673 tl->fd = file_data_ref(fd);
674 tl->source_mtime = st.st_mtime;
675 tl->source_size = st.st_size;
676 tl->source_mode = st.st_mode;
678 if (!thumb_cache) thumb_cache = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, NULL);
679 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
683 pathl = path_from_utf8(fd->path);
684 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
685 tl->local_uri = filename_from_path(tl->thumb_uri);
689 if (tl->cache_enable)
693 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
694 tl->thumb_path_local = FALSE;
696 found = isfile(tl->thumb_path);
697 if (found && thumb_loader_std_setup(tl, tl->thumb_path)) return TRUE;
699 if (thumb_loader_std_fail_check(tl)) return FALSE;
701 return thumb_loader_std_next_source(tl, found);
704 if (!thumb_loader_std_setup(tl, tl->fd->path))
706 thumb_loader_std_save(tl, NULL);
713 void thumb_loader_std_free(ThumbLoaderStd *tl)
717 thumb_loader_std_reset(tl);
721 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl, gint with_fallback)
725 if (tl && tl->fd && tl->fd->thumb_pixbuf)
727 pixbuf = tl->fd->thumb_pixbuf;
728 g_object_ref(pixbuf);
730 else if (with_fallback)
734 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
735 w = gdk_pixbuf_get_width(pixbuf);
736 h = gdk_pixbuf_get_height(pixbuf);
738 if (w > tl->requested_width || h > tl->requested_height)
742 if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height,
748 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
749 g_object_unref(G_OBJECT(tmp));
762 typedef struct _ThumbValidate ThumbValidate;
763 struct _ThumbValidate
769 void (*func_valid)(const gchar *path, gint valid, gpointer data);
775 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
777 thumb_loader_std_free(tv->tl);
782 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
790 if (tv->idle_id != -1) g_source_remove(tv->idle_id);
793 thumb_loader_std_thumb_file_validate_free(tv);
796 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gint valid)
798 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
800 thumb_loader_std_thumb_file_validate_free(tv);
803 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
805 ThumbValidate *tv = data;
809 pixbuf = thumb_loader_std_get_pixbuf(tv->tl, FALSE);
813 const gchar *mtime_str;
815 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
816 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
817 if (uri && mtime_str)
819 if (strncmp(uri, "file:", strlen("file:")) == 0)
824 target = g_filename_from_uri(uri, NULL, NULL);
825 if (stat(target, &st) == 0 &&
826 st.st_mtime == strtol(mtime_str, NULL, 10))
836 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
838 if (stat_utf8(tv->path, &st))
843 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
851 g_object_unref(pixbuf);
854 thumb_loader_std_thumb_file_validate_finish(tv, valid);
857 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
859 ThumbValidate *tv = data;
861 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
864 static gint thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
866 ThumbValidate *tv = data;
869 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
874 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
875 void (*func_valid)(const gchar *path, gint valid, gpointer data),
880 tv = g_new0(ThumbValidate, 1);
882 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
883 thumb_loader_std_set_callbacks(tv->tl,
884 thumb_loader_std_thumb_file_validate_done_cb,
885 thumb_loader_std_thumb_file_validate_error_cb,
888 thumb_loader_std_reset(tv->tl);
890 tv->path = g_strdup(thumb_path);
891 tv->days = allowed_days;
892 tv->func_valid = func_valid;
895 if (!thumb_loader_std_setup(tv->tl, thumb_path))
897 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
907 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gint local,
908 const gchar *subfolder)
912 thumb_path = thumb_std_cache_path(source,
913 (local) ? filename_from_path(uri) : uri,
915 if (isfile(thumb_path))
917 DEBUG_1("thumb removing: %s", thumb_path);
918 unlink_file(thumb_path);
923 /* this also removes local thumbnails (the source is gone so it makes sense) */
924 void thumb_std_maint_removed(const gchar *source)
929 sourcel = path_from_utf8(source);
930 uri = g_filename_to_uri(sourcel, NULL, NULL);
933 /* all this to remove a thumbnail? */
935 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
936 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
937 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
938 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
939 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
944 typedef struct _TMaintMove TMaintMove;
957 static GList *thumb_std_maint_move_list = NULL;
958 static GList *thumb_std_maint_move_tail = NULL;
961 static void thumb_std_maint_move_step(TMaintMove *tm);
962 static gint thumb_std_maint_move_idle(gpointer data);
965 static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data)
967 TMaintMove *tm = data;
970 pixbuf = thumb_loader_std_get_pixbuf(tm->tl, FALSE);
974 const gchar *mtime_str;
976 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
977 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
979 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
983 /* The validation utility abuses ThumbLoader, and we
984 * abuse the utility just to load the thumbnail,
985 * but the loader needs to look sane for the save to complete.
988 tm->tl->cache_enable = TRUE;
989 tm->tl->cache_hit = FALSE;
990 tm->tl->cache_local = FALSE;
992 file_data_unref(tm->tl->fd);
993 tm->tl->fd = file_data_new_simple(tm->dest);
994 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
996 pathl = path_from_utf8(tm->tl->fd->path);
997 g_free(tm->tl->thumb_uri);
998 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
999 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1002 g_free(tm->tl->thumb_path);
1003 tm->tl->thumb_path = NULL;
1004 tm->tl->thumb_path_local = FALSE;
1006 DEBUG_1("thumb move attempting save:");
1008 thumb_loader_std_save(tm->tl, pixbuf);
1011 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1012 unlink_file(tm->thumb_path);
1015 thumb_std_maint_move_step(tm);
1018 static void thumb_std_maint_move_step(TMaintMove *tm)
1020 const gchar *folder;
1027 g_free(tm->source_uri);
1028 g_free(tm->thumb_path);
1031 if (thumb_std_maint_move_list)
1033 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1039 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1041 g_free(tm->thumb_path);
1042 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1043 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1044 thumb_std_maint_move_validate_cb, tm);
1047 static gint thumb_std_maint_move_idle(gpointer data)
1052 if (!thumb_std_maint_move_list) return FALSE;
1054 tm = thumb_std_maint_move_list->data;
1056 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1057 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1059 pathl = path_from_utf8(tm->source);
1060 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1065 thumb_std_maint_move_step(tm);
1070 /* This will schedule a move of the thumbnail for source image to dest when idle.
1071 * We do this so that file renaming or moving speed is not sacrificed by
1072 * moving the thumbnails at the same time because:
1074 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1076 * The thumbnails are processed when the app is idle. If the app
1077 * exits early well too bad - they can simply be regenerated from scratch.
1079 * This does not manage local thumbnails (fixme ?)
1081 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1085 tm = g_new0(TMaintMove, 1);
1086 tm->source = g_strdup(source);
1087 tm->dest = g_strdup(dest);
1089 if (!thumb_std_maint_move_list)
1091 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1094 if (thumb_std_maint_move_tail)
1096 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1097 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1101 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1102 thumb_std_maint_move_tail = thumb_std_maint_move_list;