4 * Copyright (C) 2008 - 2009 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;
80 tl->requested_width = width;
81 tl->requested_height = height;
86 tl->cache_enable = options->thumbnails.enable_caching;
87 tl->cache_local = FALSE;
88 tl->cache_retry = FALSE;
93 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
94 ThumbLoaderStdFunc func_done,
95 ThumbLoaderStdFunc func_error,
96 ThumbLoaderStdFunc func_progress,
101 tl->func_done = func_done;
102 tl->func_error = func_error;
103 tl->func_progress = func_progress;
107 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
109 image_loader_free(tl->il);
112 file_data_unref(tl->fd);
115 g_free(tl->thumb_path);
116 tl->thumb_path = NULL;
118 g_free(tl->thumb_uri);
119 tl->thumb_uri = NULL;
120 tl->local_uri = NULL;
122 tl->thumb_path_local = FALSE;
124 tl->cache_hit = FALSE;
126 tl->source_mtime = 0;
133 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint local,
134 const gchar *cache_subfolder)
136 gchar *result = NULL;
141 if (!path || !uri || !cache_subfolder) return NULL;
143 md5_get_digest((guchar *)uri, strlen(uri), digest);
144 md5_text = md5_digest_to_text(digest);
146 if (!md5_text) return NULL;
148 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
152 gchar *base = remove_level_from_path(path);
154 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
159 result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL);
168 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPixbuf *pixbuf, gint fail)
173 if (!tl->fd || !tl->thumb_uri) return NULL;
177 w = gdk_pixbuf_get_width(pixbuf);
178 h = gdk_pixbuf_get_height(pixbuf);
182 w = tl->requested_width;
183 h = tl->requested_height;
188 folder = THUMB_FOLDER_FAIL;
190 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
192 folder = THUMB_FOLDER_LARGE;
196 folder = THUMB_FOLDER_NORMAL;
199 return thumb_std_cache_path(tl->fd->path,
200 (local) ? tl->local_uri : tl->thumb_uri,
204 static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl)
209 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
210 if (isfile(fail_path))
222 pathl = path_from_utf8(fail_path);
223 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
229 const gchar *mtime_str;
231 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
232 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
235 DEBUG_1("thumb fail valid: %s", tl->fd->path);
236 DEBUG_1(" thumb: %s", fail_path);
239 g_object_unref(G_OBJECT(pixbuf));
242 if (!result) unlink_file(fail_path);
249 static gint thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
251 const gchar *valid_uri;
253 const gchar *mtime_str;
257 if (!pixbuf) return FALSE;
259 w = gdk_pixbuf_get_width(pixbuf);
260 h = gdk_pixbuf_get_height(pixbuf);
262 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
263 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
265 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
267 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
268 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
270 if (!mtime_str || !uri || !valid_uri) return FALSE;
271 if (strcmp(uri, valid_uri) != 0) return FALSE;
273 mtime = strtol(mtime_str, NULL, 10);
274 if (tl->source_mtime != mtime) return FALSE;
279 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
285 if (!tl->cache_enable || tl->cache_hit) return;
286 if (tl->thumb_path) return;
290 /* local failures are not stored */
291 if (tl->cache_local) return;
293 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
298 g_object_ref(G_OBJECT(pixbuf));
302 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
305 g_object_unref(G_OBJECT(pixbuf));
308 tl->thumb_path_local = tl->cache_local;
310 /* create thumbnail dir if needed */
311 base_path = remove_level_from_path(tl->thumb_path);
314 if (!isdir(base_path))
319 source_base = remove_level_from_path(tl->fd->path);
320 if (stat_utf8(source_base, &st))
322 recursive_mkdir_if_not_exists(base_path, st.st_mode);
329 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
333 DEBUG_1("thumb saving: %s", tl->fd->path);
334 DEBUG_1(" saved: %s", tl->thumb_path);
336 /* save thumb, using a temp file then renaming into place */
337 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
340 const gchar *mark_uri;
346 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
348 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
349 mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
351 pathl = path_from_utf8(tmp_path);
352 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
353 THUMB_MARKER_URI, mark_uri,
354 THUMB_MARKER_MTIME, mark_mtime,
355 THUMB_MARKER_APP, mark_app,
359 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
360 success = rename_file(tmp_path, tl->thumb_path);
371 DEBUG_1("thumb save failed: %s", tl->fd->path);
372 DEBUG_1(" thumb: %s", tl->thumb_path);
377 g_object_unref(G_OBJECT(pixbuf));
380 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
382 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
383 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
386 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk)
388 GdkPixbuf *pixbuf_thumb = NULL;
390 GdkPixbuf *rotated = NULL;
394 if (!tl->cache_hit && options->image.exif_rotate_enable)
396 if (!tl->fd->exif_orientation)
398 tl->fd->exif_orientation = metadata_read_int(tl->fd, "Exif.Image.Orientation", EXIF_ORIENTATION_TOP_LEFT);
401 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
403 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
408 sw = gdk_pixbuf_get_width(pixbuf);
409 sh = gdk_pixbuf_get_height(pixbuf);
411 if (tl->cache_enable)
415 gint cache_w, cache_h;
417 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
419 cache_w = cache_h = THUMB_SIZE_LARGE;
423 cache_w = cache_h = THUMB_SIZE_NORMAL;
426 if (sw > cache_w || sh > cache_h || shrunk)
428 gint thumb_w, thumb_h;
430 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
433 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
434 (GdkInterpType)options->thumbnails.quality);
438 pixbuf_thumb = pixbuf;
439 g_object_ref(G_OBJECT(pixbuf_thumb));
442 thumb_loader_std_save(tl, pixbuf_thumb);
445 else if (tl->cache_hit &&
446 tl->cache_local && !tl->thumb_path_local)
448 /* A local cache save was requested, but a valid thumb is in $HOME,
449 * so specifically save as a local thumbnail.
451 g_free(tl->thumb_path);
452 tl->thumb_path = NULL;
454 tl->cache_hit = FALSE;
456 DEBUG_1("thumb copied: %s", tl->fd->path);
458 thumb_loader_std_save(tl, pixbuf);
462 if (sw <= tl->requested_width && sh <= tl->requested_height)
465 g_object_ref(result);
469 gint thumb_w, thumb_h;
473 pixbuf = pixbuf_thumb;
474 sw = gdk_pixbuf_get_width(pixbuf);
475 sh = gdk_pixbuf_get_height(pixbuf);
478 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
481 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
482 (GdkInterpType)options->thumbnails.quality);
487 g_object_ref(result);
491 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
492 if (rotated) g_object_unref(rotated);
497 static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken)
499 image_loader_free(tl->il);
504 if (!tl->thumb_path_local && remove_broken)
506 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
507 unlink_file(tl->thumb_path);
510 g_free(tl->thumb_path);
511 tl->thumb_path = NULL;
513 if (!tl->thumb_path_local)
515 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
516 if (isfile(tl->thumb_path) && thumb_loader_std_setup_path(tl, tl->thumb_path))
518 tl->thumb_path_local = TRUE;
522 g_free(tl->thumb_path);
523 tl->thumb_path = NULL;
526 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
529 thumb_loader_std_save(tl, NULL);
533 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
535 ThumbLoaderStd *tl = data;
538 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
539 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
541 pixbuf = image_loader_get_pixbuf(tl->il);
544 DEBUG_1("...but no pixbuf");
545 thumb_loader_std_error_cb(il, data);
549 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
551 if (thumb_loader_std_next_source(tl, TRUE)) return;
553 if (tl->func_error) tl->func_error(tl, tl->data);
557 tl->cache_hit = (tl->thumb_path != NULL);
561 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
562 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
565 if (tl->func_done) tl->func_done(tl, tl->data);
568 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
570 ThumbLoaderStd *tl = data;
572 /* if at least some of the image is available, go to done */
573 if (image_loader_get_pixbuf(tl->il) != NULL)
575 thumb_loader_std_done_cb(il, data);
579 DEBUG_1("thumb image error: %s", tl->fd->path);
580 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
582 if (thumb_loader_std_next_source(tl, TRUE)) return;
584 thumb_loader_std_set_fallback(tl);
586 if (tl->func_error) tl->func_error(tl, tl->data);
589 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
591 ThumbLoaderStd *tl = data;
593 tl->progress = (gdouble)percent;
595 if (tl->func_progress) tl->func_progress(tl, tl->data);
598 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
600 tl->il = image_loader_new(fd);
601 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
603 if (options->thumbnails.fast)
605 /* this will speed up jpegs by up to 3x in some cases */
606 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
607 tl->requested_height <= THUMB_SIZE_NORMAL)
609 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
613 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
617 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
618 if (tl->func_progress)
620 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
622 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
624 if (image_loader_start(tl->il))
629 image_loader_free(tl->il);
634 static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path)
636 FileData *fd = file_data_new_simple(path);
637 gint ret = thumb_loader_std_setup(tl, fd);
643 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
644 * a valid existing thumb is found anywhere the local thumb will not be created.
646 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint local, gint retry_failed)
650 tl->cache_enable = enable_cache;
651 tl->cache_local = local;
652 tl->cache_retry = retry_failed;
655 gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
657 static gchar *thumb_cache = NULL;
660 if (!tl || !fd) return FALSE;
662 thumb_loader_std_reset(tl);
665 tl->fd = file_data_ref(fd);
666 if (!stat_utf8(fd->path, &st))
668 thumb_loader_std_set_fallback(tl);
671 tl->source_mtime = st.st_mtime;
672 tl->source_size = st.st_size;
673 tl->source_mode = st.st_mode;
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)
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);
686 if (tl->cache_enable)
690 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
691 tl->thumb_path_local = FALSE;
693 found = isfile(tl->thumb_path);
694 if (found && thumb_loader_std_setup_path(tl, tl->thumb_path)) return TRUE;
696 if (thumb_loader_std_fail_check(tl) ||
697 !thumb_loader_std_next_source(tl, found))
699 thumb_loader_std_set_fallback(tl);
705 if (!thumb_loader_std_setup(tl, tl->fd))
707 thumb_loader_std_save(tl, NULL);
708 thumb_loader_std_set_fallback(tl);
715 void thumb_loader_std_free(ThumbLoaderStd *tl)
719 thumb_loader_std_reset(tl);
723 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
727 if (tl && tl->fd && tl->fd->thumb_pixbuf)
729 pixbuf = tl->fd->thumb_pixbuf;
730 g_object_ref(pixbuf);
734 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
741 typedef struct _ThumbValidate ThumbValidate;
742 struct _ThumbValidate
748 void (*func_valid)(const gchar *path, gint valid, gpointer data);
754 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
756 thumb_loader_std_free(tv->tl);
761 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
769 if (tv->idle_id != -1) g_source_remove(tv->idle_id);
772 thumb_loader_std_thumb_file_validate_free(tv);
775 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gint valid)
777 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
779 thumb_loader_std_thumb_file_validate_free(tv);
782 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
784 ThumbValidate *tv = data;
788 /* this function is called on success, so the pixbuf should not be a fallback*/
789 pixbuf = thumb_loader_std_get_pixbuf(tv->tl);
793 const gchar *mtime_str;
795 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
796 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
797 if (uri && mtime_str)
799 if (strncmp(uri, "file:", strlen("file:")) == 0)
804 target = g_filename_from_uri(uri, NULL, NULL);
805 if (stat(target, &st) == 0 &&
806 st.st_mtime == strtol(mtime_str, NULL, 10))
816 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
818 if (stat_utf8(tv->path, &st))
823 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
831 g_object_unref(pixbuf);
834 thumb_loader_std_thumb_file_validate_finish(tv, valid);
837 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
839 ThumbValidate *tv = data;
841 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
844 static gint thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
846 ThumbValidate *tv = data;
849 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
854 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
855 void (*func_valid)(const gchar *path, gint valid, gpointer data),
860 tv = g_new0(ThumbValidate, 1);
862 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
863 thumb_loader_std_set_callbacks(tv->tl,
864 thumb_loader_std_thumb_file_validate_done_cb,
865 thumb_loader_std_thumb_file_validate_error_cb,
868 thumb_loader_std_reset(tv->tl);
870 tv->path = g_strdup(thumb_path);
871 tv->days = allowed_days;
872 tv->func_valid = func_valid;
875 if (!thumb_loader_std_setup_path(tv->tl, thumb_path))
877 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
887 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gint local,
888 const gchar *subfolder)
892 thumb_path = thumb_std_cache_path(source,
893 (local) ? filename_from_path(uri) : uri,
895 if (isfile(thumb_path))
897 DEBUG_1("thumb removing: %s", thumb_path);
898 unlink_file(thumb_path);
903 /* this also removes local thumbnails (the source is gone so it makes sense) */
904 void thumb_std_maint_removed(const gchar *source)
909 sourcel = path_from_utf8(source);
910 uri = g_filename_to_uri(sourcel, NULL, NULL);
913 /* all this to remove a thumbnail? */
915 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
916 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
917 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
918 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
919 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
924 typedef struct _TMaintMove TMaintMove;
937 static GList *thumb_std_maint_move_list = NULL;
938 static GList *thumb_std_maint_move_tail = NULL;
941 static void thumb_std_maint_move_step(TMaintMove *tm);
942 static gint thumb_std_maint_move_idle(gpointer data);
945 static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data)
947 TMaintMove *tm = data;
950 /* this function is called on success, so the pixbuf should not be a fallback*/
951 pixbuf = thumb_loader_std_get_pixbuf(tm->tl);
955 const gchar *mtime_str;
957 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
958 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
960 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
964 /* The validation utility abuses ThumbLoader, and we
965 * abuse the utility just to load the thumbnail,
966 * but the loader needs to look sane for the save to complete.
969 tm->tl->cache_enable = TRUE;
970 tm->tl->cache_hit = FALSE;
971 tm->tl->cache_local = FALSE;
973 file_data_unref(tm->tl->fd);
974 tm->tl->fd = file_data_new_simple(tm->dest);
975 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
977 pathl = path_from_utf8(tm->tl->fd->path);
978 g_free(tm->tl->thumb_uri);
979 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
980 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
983 g_free(tm->tl->thumb_path);
984 tm->tl->thumb_path = NULL;
985 tm->tl->thumb_path_local = FALSE;
987 DEBUG_1("thumb move attempting save:");
989 thumb_loader_std_save(tm->tl, pixbuf);
992 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
993 unlink_file(tm->thumb_path);
994 g_object_unref(pixbuf);
997 thumb_std_maint_move_step(tm);
1000 static void thumb_std_maint_move_step(TMaintMove *tm)
1002 const gchar *folder;
1009 g_free(tm->source_uri);
1010 g_free(tm->thumb_path);
1013 if (thumb_std_maint_move_list)
1015 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1021 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1023 g_free(tm->thumb_path);
1024 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1025 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1026 thumb_std_maint_move_validate_cb, tm);
1029 static gint thumb_std_maint_move_idle(gpointer data)
1034 if (!thumb_std_maint_move_list) return FALSE;
1036 tm = thumb_std_maint_move_list->data;
1038 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1039 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1041 pathl = path_from_utf8(tm->source);
1042 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1047 thumb_std_maint_move_step(tm);
1052 /* This will schedule a move of the thumbnail for source image to dest when idle.
1053 * We do this so that file renaming or moving speed is not sacrificed by
1054 * moving the thumbnails at the same time because:
1056 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1058 * The thumbnails are processed when the app is idle. If the app
1059 * exits early well too bad - they can simply be regenerated from scratch.
1061 * This does not manage local thumbnails (fixme ?)
1063 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1067 tm = g_new0(TMaintMove, 1);
1068 tm->source = g_strdup(source);
1069 tm->dest = g_strdup(dest);
1071 if (!thumb_std_maint_move_list)
1073 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1076 if (thumb_std_maint_move_tail)
1078 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1079 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1083 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1084 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1087 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */