4 * Copyright (C) 2008 - 2010 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 "image-load.h"
19 #include "pixbuf_util.h"
20 #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, FileData *fd);
69 static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path);
72 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
76 tl = g_new0(ThumbLoaderStd, 1);
78 tl->standard_loader = TRUE;
79 tl->requested_width = width;
80 tl->requested_height = height;
81 tl->cache_enable = options->thumbnails.enable_caching;
86 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
87 ThumbLoaderStdFunc func_done,
88 ThumbLoaderStdFunc func_error,
89 ThumbLoaderStdFunc func_progress,
94 tl->func_done = func_done;
95 tl->func_error = func_error;
96 tl->func_progress = func_progress;
100 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
102 image_loader_free(tl->il);
105 file_data_unref(tl->fd);
108 g_free(tl->thumb_path);
109 tl->thumb_path = NULL;
111 g_free(tl->thumb_uri);
112 tl->thumb_uri = NULL;
113 tl->local_uri = NULL;
115 tl->thumb_path_local = FALSE;
117 tl->cache_hit = FALSE;
119 tl->source_mtime = 0;
126 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
127 const gchar *cache_subfolder)
129 gchar *result = NULL;
134 if (!path || !uri || !cache_subfolder) return NULL;
136 md5_get_digest((guchar *)uri, strlen(uri), digest);
137 md5_text = md5_digest_to_text(digest);
139 if (!md5_text) return NULL;
141 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
145 gchar *base = remove_level_from_path(path);
147 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
152 result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL);
161 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
166 if (!tl->fd || !tl->thumb_uri) return NULL;
170 w = gdk_pixbuf_get_width(pixbuf);
171 h = gdk_pixbuf_get_height(pixbuf);
175 w = tl->requested_width;
176 h = tl->requested_height;
181 folder = THUMB_FOLDER_FAIL;
183 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
185 folder = THUMB_FOLDER_LARGE;
189 folder = THUMB_FOLDER_NORMAL;
192 return thumb_std_cache_path(tl->fd->path,
193 (local) ? tl->local_uri : tl->thumb_uri,
197 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
200 gboolean result = FALSE;
202 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
203 if (isfile(fail_path))
215 pathl = path_from_utf8(fail_path);
216 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
222 const gchar *mtime_str;
224 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
225 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
228 DEBUG_1("thumb fail valid: %s", tl->fd->path);
229 DEBUG_1(" thumb: %s", fail_path);
232 g_object_unref(G_OBJECT(pixbuf));
235 if (!result) unlink_file(fail_path);
242 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
244 const gchar *valid_uri;
246 const gchar *mtime_str;
250 if (!pixbuf) return FALSE;
252 w = gdk_pixbuf_get_width(pixbuf);
253 h = gdk_pixbuf_get_height(pixbuf);
255 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
256 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
258 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
260 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
261 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
263 if (!mtime_str || !uri || !valid_uri) return FALSE;
264 if (strcmp(uri, valid_uri) != 0) return FALSE;
266 mtime = strtol(mtime_str, NULL, 10);
267 if (tl->source_mtime != mtime) return FALSE;
272 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
278 if (!tl->cache_enable || tl->cache_hit) return;
279 if (tl->thumb_path) return;
283 /* local failures are not stored */
284 if (tl->cache_local) return;
286 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
291 g_object_ref(G_OBJECT(pixbuf));
295 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
298 g_object_unref(G_OBJECT(pixbuf));
301 tl->thumb_path_local = tl->cache_local;
303 /* create thumbnail dir if needed */
304 base_path = remove_level_from_path(tl->thumb_path);
307 if (!isdir(base_path))
312 source_base = remove_level_from_path(tl->fd->path);
313 if (stat_utf8(source_base, &st))
315 recursive_mkdir_if_not_exists(base_path, st.st_mode);
322 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
326 DEBUG_1("thumb saving: %s", tl->fd->path);
327 DEBUG_1(" saved: %s", tl->thumb_path);
329 /* save thumb, using a temp file then renaming into place */
330 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
333 const gchar *mark_uri;
339 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
341 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
342 mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
344 pathl = path_from_utf8(tmp_path);
345 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
346 THUMB_MARKER_URI, mark_uri,
347 THUMB_MARKER_MTIME, mark_mtime,
348 THUMB_MARKER_APP, mark_app,
352 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
353 success = rename_file(tmp_path, tl->thumb_path);
364 DEBUG_1("thumb save failed: %s", tl->fd->path);
365 DEBUG_1(" thumb: %s", tl->thumb_path);
370 g_object_unref(G_OBJECT(pixbuf));
373 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
375 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
376 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
379 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
381 GdkPixbuf *pixbuf_thumb = NULL;
383 GdkPixbuf *rotated = NULL;
387 if (!tl->cache_hit && options->image.exif_rotate_enable)
389 if (!tl->fd->exif_orientation)
391 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
394 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
396 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
401 sw = gdk_pixbuf_get_width(pixbuf);
402 sh = gdk_pixbuf_get_height(pixbuf);
404 if (tl->cache_enable)
408 gint cache_w, cache_h;
410 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
412 cache_w = cache_h = THUMB_SIZE_LARGE;
416 cache_w = cache_h = THUMB_SIZE_NORMAL;
419 if (sw > cache_w || sh > cache_h || shrunk)
421 gint thumb_w, thumb_h;
424 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
427 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
428 (GdkInterpType)options->thumbnails.quality);
432 pixbuf_thumb = pixbuf;
433 g_object_ref(G_OBJECT(pixbuf_thumb));
436 /* do not save the thumbnail if the source file has changed meanwhile -
437 the thumbnail is most probably broken */
438 if (stat_utf8(tl->fd->path, &st) &&
439 tl->source_mtime == st.st_mtime &&
440 tl->source_size == st.st_size)
442 thumb_loader_std_save(tl, pixbuf_thumb);
446 else if (tl->cache_hit &&
447 tl->cache_local && !tl->thumb_path_local)
449 /* A local cache save was requested, but a valid thumb is in $HOME,
450 * so specifically save as a local thumbnail.
452 g_free(tl->thumb_path);
453 tl->thumb_path = NULL;
455 tl->cache_hit = FALSE;
457 DEBUG_1("thumb copied: %s", tl->fd->path);
459 thumb_loader_std_save(tl, pixbuf);
463 if (sw <= tl->requested_width && sh <= tl->requested_height)
466 g_object_ref(result);
470 gint thumb_w, thumb_h;
474 pixbuf = pixbuf_thumb;
475 sw = gdk_pixbuf_get_width(pixbuf);
476 sh = gdk_pixbuf_get_height(pixbuf);
479 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
482 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
483 (GdkInterpType)options->thumbnails.quality);
488 g_object_ref(result);
492 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
493 if (rotated) g_object_unref(rotated);
498 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
500 image_loader_free(tl->il);
505 if (!tl->thumb_path_local && remove_broken)
507 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
508 unlink_file(tl->thumb_path);
511 g_free(tl->thumb_path);
512 tl->thumb_path = NULL;
514 if (!tl->thumb_path_local)
516 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
517 if (isfile(tl->thumb_path) && thumb_loader_std_setup_path(tl, tl->thumb_path))
519 tl->thumb_path_local = TRUE;
523 g_free(tl->thumb_path);
524 tl->thumb_path = NULL;
527 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
530 thumb_loader_std_save(tl, NULL);
534 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
536 ThumbLoaderStd *tl = data;
539 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
540 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
542 pixbuf = image_loader_get_pixbuf(tl->il);
545 DEBUG_1("...but no pixbuf");
546 thumb_loader_std_error_cb(il, data);
550 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
552 if (thumb_loader_std_next_source(tl, TRUE)) return;
554 if (tl->func_error) tl->func_error(tl, tl->data);
558 tl->cache_hit = (tl->thumb_path != NULL);
562 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
563 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
566 if (tl->func_done) tl->func_done(tl, tl->data);
569 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
571 ThumbLoaderStd *tl = data;
573 /* if at least some of the image is available, go to done */
574 if (image_loader_get_pixbuf(tl->il) != NULL)
576 thumb_loader_std_done_cb(il, data);
580 DEBUG_1("thumb image error: %s", tl->fd->path);
581 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
583 if (thumb_loader_std_next_source(tl, TRUE)) return;
585 thumb_loader_std_set_fallback(tl);
587 if (tl->func_error) tl->func_error(tl, tl->data);
590 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
592 ThumbLoaderStd *tl = data;
594 tl->progress = (gdouble)percent;
596 if (tl->func_progress) tl->func_progress(tl, tl->data);
599 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
601 tl->il = image_loader_new(fd);
602 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
604 /* this will speed up jpegs by up to 3x in some cases */
605 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
606 tl->requested_height <= THUMB_SIZE_NORMAL)
608 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
612 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
615 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
616 if (tl->func_progress)
618 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
620 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
622 if (image_loader_start(tl->il))
627 image_loader_free(tl->il);
632 static gboolean thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path)
634 FileData *fd = file_data_new_simple(path);
635 gboolean ret = thumb_loader_std_setup(tl, fd);
641 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
642 * a valid existing thumb is found anywhere the local thumb will not be created.
644 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
648 tl->cache_enable = enable_cache;
649 tl->cache_local = local;
650 tl->cache_retry = retry_failed;
653 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
655 static gchar *thumb_cache = NULL;
658 if (!tl || !fd) return FALSE;
660 thumb_loader_std_reset(tl);
663 tl->fd = file_data_ref(fd);
664 if (!stat_utf8(fd->path, &st))
666 thumb_loader_std_set_fallback(tl);
669 tl->source_mtime = st.st_mtime;
670 tl->source_size = st.st_size;
671 tl->source_mode = st.st_mode;
673 if (!thumb_cache) thumb_cache = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, NULL);
674 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
678 pathl = path_from_utf8(fd->path);
679 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
680 tl->local_uri = filename_from_path(tl->thumb_uri);
684 if (tl->cache_enable)
688 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
689 tl->thumb_path_local = FALSE;
691 found = isfile(tl->thumb_path);
692 if (found && thumb_loader_std_setup_path(tl, tl->thumb_path)) return TRUE;
694 if (thumb_loader_std_fail_check(tl) ||
695 !thumb_loader_std_next_source(tl, found))
697 thumb_loader_std_set_fallback(tl);
703 if (!thumb_loader_std_setup(tl, tl->fd))
705 thumb_loader_std_save(tl, NULL);
706 thumb_loader_std_set_fallback(tl);
713 void thumb_loader_std_free(ThumbLoaderStd *tl)
717 thumb_loader_std_reset(tl);
721 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
725 if (tl && tl->fd && tl->fd->thumb_pixbuf)
727 pixbuf = tl->fd->thumb_pixbuf;
728 g_object_ref(pixbuf);
732 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
739 typedef struct _ThumbValidate ThumbValidate;
740 struct _ThumbValidate
746 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
749 guint idle_id; /* event source id */
752 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
754 thumb_loader_std_free(tv->tl);
759 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
769 g_source_remove(tv->idle_id);
773 thumb_loader_std_thumb_file_validate_free(tv);
776 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
778 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
780 thumb_loader_std_thumb_file_validate_free(tv);
783 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
785 ThumbValidate *tv = data;
787 gboolean valid = FALSE;
789 /* get the original thumbnail pixbuf (unrotated, with original options)
790 this is called from image_loader done callback, so tv->tl->il must exist*/
791 pixbuf = image_loader_get_pixbuf(tv->tl->il);
795 const gchar *mtime_str;
797 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
798 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
799 if (uri && mtime_str)
801 if (strncmp(uri, "file:", strlen("file:")) == 0)
806 target = g_filename_from_uri(uri, NULL, NULL);
807 if (stat(target, &st) == 0 &&
808 st.st_mtime == strtol(mtime_str, NULL, 10))
818 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
820 if (stat_utf8(tv->path, &st))
825 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
834 DEBUG_1("invalid image found in std cache: %s", tv->path);
838 thumb_loader_std_thumb_file_validate_finish(tv, valid);
841 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
843 ThumbValidate *tv = data;
845 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
848 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
850 ThumbValidate *tv = data;
853 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
858 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
859 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
864 tv = g_new0(ThumbValidate, 1);
866 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
867 thumb_loader_std_set_callbacks(tv->tl,
868 thumb_loader_std_thumb_file_validate_done_cb,
869 thumb_loader_std_thumb_file_validate_error_cb,
872 thumb_loader_std_reset(tv->tl);
874 tv->path = g_strdup(thumb_path);
875 tv->days = allowed_days;
876 tv->func_valid = func_valid;
879 if (!thumb_loader_std_setup_path(tv->tl, thumb_path))
881 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
891 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
892 const gchar *subfolder)
896 thumb_path = thumb_std_cache_path(source,
897 (local) ? filename_from_path(uri) : uri,
899 if (isfile(thumb_path))
901 DEBUG_1("thumb removing: %s", thumb_path);
902 unlink_file(thumb_path);
907 /* this also removes local thumbnails (the source is gone so it makes sense) */
908 void thumb_std_maint_removed(const gchar *source)
913 sourcel = path_from_utf8(source);
914 uri = g_filename_to_uri(sourcel, NULL, NULL);
917 /* all this to remove a thumbnail? */
919 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
920 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
921 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
922 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
923 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
928 typedef struct _TMaintMove TMaintMove;
941 static GList *thumb_std_maint_move_list = NULL;
942 static GList *thumb_std_maint_move_tail = NULL;
945 static void thumb_std_maint_move_step(TMaintMove *tm);
946 static gboolean thumb_std_maint_move_idle(gpointer data);
949 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
951 TMaintMove *tm = data;
954 /* get the original thumbnail pixbuf (unrotated, with original options)
955 this is called from image_loader done callback, so tm->tl->il must exist*/
956 pixbuf = image_loader_get_pixbuf(tm->tl->il);
960 const gchar *mtime_str;
962 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
963 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
965 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
969 /* The validation utility abuses ThumbLoader, and we
970 * abuse the utility just to load the thumbnail,
971 * but the loader needs to look sane for the save to complete.
974 tm->tl->cache_enable = TRUE;
975 tm->tl->cache_hit = FALSE;
976 tm->tl->cache_local = FALSE;
978 file_data_unref(tm->tl->fd);
979 tm->tl->fd = file_data_new_simple(tm->dest);
980 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
982 pathl = path_from_utf8(tm->tl->fd->path);
983 g_free(tm->tl->thumb_uri);
984 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
985 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
988 g_free(tm->tl->thumb_path);
989 tm->tl->thumb_path = NULL;
990 tm->tl->thumb_path_local = FALSE;
992 DEBUG_1("thumb move attempting save:");
994 thumb_loader_std_save(tm->tl, pixbuf);
997 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
998 unlink_file(tm->thumb_path);
1001 thumb_std_maint_move_step(tm);
1004 static void thumb_std_maint_move_step(TMaintMove *tm)
1006 const gchar *folder;
1013 g_free(tm->source_uri);
1014 g_free(tm->thumb_path);
1017 if (thumb_std_maint_move_list)
1019 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1025 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1027 g_free(tm->thumb_path);
1028 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1029 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1030 thumb_std_maint_move_validate_cb, tm);
1033 static gboolean thumb_std_maint_move_idle(gpointer data)
1038 if (!thumb_std_maint_move_list) return FALSE;
1040 tm = thumb_std_maint_move_list->data;
1042 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1043 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1045 pathl = path_from_utf8(tm->source);
1046 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1051 thumb_std_maint_move_step(tm);
1056 /* This will schedule a move of the thumbnail for source image to dest when idle.
1057 * We do this so that file renaming or moving speed is not sacrificed by
1058 * moving the thumbnails at the same time because:
1060 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1062 * The thumbnails are processed when the app is idle. If the app
1063 * exits early well too bad - they can simply be regenerated from scratch.
1065 * This does not manage local thumbnails (fixme ?)
1067 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1071 tm = g_new0(TMaintMove, 1);
1072 tm->source = g_strdup(source);
1073 tm->dest = g_strdup(dest);
1075 if (!thumb_std_maint_move_list)
1077 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1080 if (thumb_std_maint_move_tail)
1082 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1083 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1087 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1088 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1091 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */