7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "thumb_standard.h"
16 #include "cache.h" /* for cache_ensure_dir_exists */
17 #include "image-load.h"
19 #include "pixbuf_util.h"
20 #include "ui_fileops.h"
24 * This thumbnail caching implementation attempts to conform
25 * to the Thumbnail Managing Standard proposed on freedesktop.org
26 * The standard is documented here:
27 * http://triq.net/~jens/thumbnail-spec/index.html
28 * (why isn't it actually hosted on freedesktop.org?)
30 * This code attempts to conform to version 0.7.0 of the standard.
33 * > Validation of the thumb's embedded uri is a simple strcmp between our
34 * version of the escaped uri and the thumb's escaped uri. But not all uri
35 * escape functions escape the same set of chars, comparing the unescaped
36 * versions may be more accurate.
37 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
38 * Storing the Size, Width, Height should probably be implemented.
42 #define THUMB_SIZE_NORMAL 128
43 #define THUMB_SIZE_LARGE 256
45 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
46 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
47 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
48 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
49 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
50 #define THUMB_MARKER_APP "tEXt::Software"
52 #define THUMB_PERMS_FOLDER 0700
53 #define THUMB_PERMS_THUMB 0600
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
64 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
65 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path);
68 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
72 tl = g_new0(ThumbLoaderStd, 1);
74 tl->standard_loader = TRUE;
76 tl->requested_width = width;
77 tl->requested_height = height;
81 tl->source_path = NULL;
83 tl->cache_enable = enable_thumb_caching;
84 tl->cache_local = FALSE;
85 tl->cache_retry = FALSE;
90 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
91 ThumbLoaderStdFunc func_done,
92 ThumbLoaderStdFunc func_error,
93 ThumbLoaderStdFunc func_progress,
98 tl->func_done = func_done;
99 tl->func_error = func_error;
100 tl->func_progress = func_progress;
104 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
106 if (tl->pixbuf) g_object_unref(tl->pixbuf);
109 image_loader_free(tl->il);
112 g_free(tl->source_path);
113 tl->source_path = NULL;
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;
147 base = remove_level_from_path(path);
148 cache_base = g_strconcat(base, "/", THUMB_FOLDER, "/", cache_subfolder, NULL);
153 cache_base = g_strconcat(homedir(), "/", THUMB_FOLDER, "/", cache_subfolder, NULL);
156 md5_get_digest(uri, strlen(uri), digest);
157 md5_text = md5_digest_to_text(digest);
159 if (cache_base && md5_text)
161 result = g_strconcat(cache_base, "/", md5_text, THUMB_NAME_EXTENSION, NULL);
170 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPixbuf *pixbuf, gint fail)
173 gchar *result = NULL;
176 const gchar *folder_size;
184 if (!tl->source_path || !tl->thumb_uri) return NULL;
188 w = gdk_pixbuf_get_width(pixbuf);
189 h = gdk_pixbuf_get_height(pixbuf);
193 w = tl->requested_width;
194 h = tl->requested_height;
199 folder_size = THUMB_FOLDER_FAIL;
201 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
203 folder_size = THUMB_FOLDER_LARGE;
207 folder_size = THUMB_FOLDER_NORMAL;
210 return thumb_std_cache_path(tl->source_path,
211 (local) ? tl->local_uri : tl->thumb_uri,
219 base = remove_level_from_path(tl->source_path);
220 cache_base = g_strconcat(base, "/", THUMB_FOLDER, "/", folder_size, NULL);
225 cache_base = g_strconcat(homedir(), "/", THUMB_FOLDER, "/", folder_size, NULL);
228 uri = (local) ? tl->local_uri : tl->thumb_uri;
229 md5_get_digest(uri, strlen(uri), digest);
230 md5_text = md5_digest_to_text(digest);
232 if (cache_base && md5_text)
234 result = g_strconcat(cache_base, "/", md5_text, THUMB_NAME_EXTENSION, NULL);
244 static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl)
249 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
250 if (isfile(fail_path))
262 pathl = path_from_utf8(fail_path);
263 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
269 const gchar *mtime_str;
271 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
272 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
277 printf("thumb fail valid: %s\n", tl->source_path);
278 printf(" thumb: %s\n", fail_path);
282 g_object_unref(G_OBJECT(pixbuf));
285 if (!result) unlink_file(fail_path);
292 static gint thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
294 const gchar *valid_uri;
296 const gchar *mtime_str;
300 if (!pixbuf) return FALSE;
302 w = gdk_pixbuf_get_width(pixbuf);
303 h = gdk_pixbuf_get_height(pixbuf);
305 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
306 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
308 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
310 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
311 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
313 if (!mtime_str || !uri || !valid_uri) return FALSE;
314 if (strcmp(uri, valid_uri) != 0) return FALSE;
316 mtime = strtol(mtime_str, NULL, 10);
317 if (tl->source_mtime != mtime) return FALSE;
322 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
328 if (!tl->cache_enable || tl->cache_hit) return;
329 if (tl->thumb_path) return;
333 /* local failures are not stored */
334 if (tl->cache_local) return;
336 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
341 g_object_ref(G_OBJECT(pixbuf));
345 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
348 g_object_unref(G_OBJECT(pixbuf));
351 tl->thumb_path_local = tl->cache_local;
353 /* create thumbnail dir if needed */
354 base_path = remove_level_from_path(tl->thumb_path);
357 if (!isdir(base_path))
362 source_base = remove_level_from_path(tl->source_path);
363 if (stat_utf8(source_base, &st))
365 cache_ensure_dir_exists(base_path, st.st_mode);
372 cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER);
378 printf("thumb saving: %s\n", tl->source_path);
379 printf(" saved: %s\n", tl->thumb_path);
382 /* save thumb, using a temp file then renaming into place */
383 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
386 const gchar *mark_uri;
392 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
394 mark_app = g_strdup_printf("GQview %s", VERSION);
395 mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
397 pathl = path_from_utf8(tmp_path);
398 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
399 THUMB_MARKER_URI, mark_uri,
400 THUMB_MARKER_MTIME, mark_mtime,
401 THUMB_MARKER_APP, mark_app,
405 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
406 success = rename_file(tmp_path, tl->thumb_path);
415 if (!success && debug)
417 printf("thumb save failed: %s\n", tl->source_path);
418 printf(" thumb: %s\n", tl->thumb_path);
423 g_object_unref(G_OBJECT(pixbuf));
426 static gint thumb_loader_std_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
427 gint *new_w, gint *new_h)
429 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
432 *new_h = (gdouble)*new_w / old_w * old_h;
433 if (*new_h < 1) *new_h = 1;
438 *new_w = (gdouble)*new_h / old_h * old_w;
439 if (*new_w < 1) *new_w = 1;
442 return (*new_w != old_w || *new_h != old_h);
445 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk)
447 GdkPixbuf *pixbuf_thumb = NULL;
451 sw = gdk_pixbuf_get_width(pixbuf);
452 sh = gdk_pixbuf_get_height(pixbuf);
454 if (tl->cache_enable)
458 gint cache_w, cache_h;
460 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
462 cache_w = cache_h = THUMB_SIZE_LARGE;
466 cache_w = cache_h = THUMB_SIZE_NORMAL;
469 if (sw > cache_w || sh > cache_h || shrunk)
471 gint thumb_w, thumb_h;
473 if (thumb_loader_std_scale_aspect(cache_w, cache_h, sw, sh,
476 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
477 (GdkInterpType)thumbnail_quality);
481 pixbuf_thumb = pixbuf;
482 g_object_ref(G_OBJECT(pixbuf_thumb));
485 thumb_loader_std_save(tl, pixbuf_thumb);
488 else if (tl->cache_hit &&
489 tl->cache_local && !tl->thumb_path_local)
491 /* A local cache save was requested, but a valid thumb is in $HOME,
492 * so specifically save as a local thumbnail.
494 g_free(tl->thumb_path);
495 tl->thumb_path = NULL;
497 tl->cache_hit = FALSE;
499 if (debug) printf("thumb copied: %s\n", tl->source_path);
501 thumb_loader_std_save(tl, pixbuf);
505 if (sw <= tl->requested_width && sh <= tl->requested_height)
508 g_object_ref(result);
512 gint thumb_w, thumb_h;
516 pixbuf = pixbuf_thumb;
517 sw = gdk_pixbuf_get_width(pixbuf);
518 sh = gdk_pixbuf_get_height(pixbuf);
521 if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
524 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
525 (GdkInterpType)thumbnail_quality);
530 g_object_ref(result);
534 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
539 static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken)
541 image_loader_free(tl->il);
546 if (!tl->thumb_path_local && remove_broken)
548 if (debug) printf("thumb broken, unlinking: %s\n", tl->thumb_path);
549 unlink_file(tl->thumb_path);
552 g_free(tl->thumb_path);
553 tl->thumb_path = NULL;
555 if (!tl->thumb_path_local)
557 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
558 if (isfile(tl->thumb_path) && thumb_loader_std_setup(tl, tl->thumb_path))
560 tl->thumb_path_local = TRUE;
564 g_free(tl->thumb_path);
565 tl->thumb_path = NULL;
568 if (thumb_loader_std_setup(tl, tl->source_path)) return TRUE;
571 thumb_loader_std_save(tl, NULL);
575 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
577 ThumbLoaderStd *tl = data;
582 printf("thumb image done: %s\n", tl->source_path);
583 printf(" from: %s\n", tl->il->path);
586 pixbuf = image_loader_get_pixbuf(tl->il);
589 if (debug) printf("...but no pixbuf\n");
590 thumb_loader_std_error_cb(il, data);
594 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
596 if (thumb_loader_std_next_source(tl, TRUE)) return;
598 if (tl->func_error) tl->func_error(tl, tl->data);
602 tl->cache_hit = (tl->thumb_path != NULL);
604 tl->pixbuf = thumb_loader_std_finish(tl, pixbuf, il->shrunk);
606 if (tl->func_done) tl->func_done(tl, tl->data);
609 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
611 ThumbLoaderStd *tl = data;
613 /* if at least some of the image is available, go to done */
614 if (image_loader_get_pixbuf(tl->il) != NULL)
616 thumb_loader_std_done_cb(il, data);
622 printf("thumb image error: %s\n", tl->source_path);
623 printf(" from: %s\n", tl->il->path);
626 if (thumb_loader_std_next_source(tl, TRUE)) return;
628 if (tl->func_error) tl->func_error(tl, tl->data);
631 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
633 ThumbLoaderStd *tl = data;
635 tl->progress = (gdouble)percent;
637 if (tl->func_progress) tl->func_progress(tl, tl->data);
640 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path)
642 tl->il = image_loader_new(path);
646 /* this will speed up jpegs by up to 3x in some cases */
647 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
648 tl->requested_height <= THUMB_SIZE_NORMAL)
650 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
654 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
658 image_loader_set_error_func(tl->il, thumb_loader_std_error_cb, tl);
659 if (tl->func_progress)
661 image_loader_set_percent_func(tl->il, thumb_loader_std_progress_cb, tl);
664 if (image_loader_start(tl->il, thumb_loader_std_done_cb, tl))
669 image_loader_free(tl->il);
675 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
676 * a valid existing thumb is found anywhere the local thumb will not be created.
678 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint local, gint retry_failed)
682 tl->cache_enable = enable_cache;
683 tl->cache_local = local;
684 tl->cache_retry = retry_failed;
687 gint thumb_loader_std_start(ThumbLoaderStd *tl, const gchar *path)
689 static gchar *thumb_cache = NULL;
692 if (!tl || !path) return FALSE;
694 thumb_loader_std_reset(tl);
696 if (!stat_utf8(path, &st)) return FALSE;
698 tl->source_path = g_strdup(path);
699 tl->source_mtime = st.st_mtime;
700 tl->source_size = st.st_size;
701 tl->source_mode = st.st_mode;
703 if (!thumb_cache) thumb_cache = g_strconcat(homedir(), "/", THUMB_FOLDER, NULL);
704 if (strncmp(tl->source_path, thumb_cache, strlen(thumb_cache)) != 0)
708 pathl = path_from_utf8(path);
709 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
710 tl->local_uri = filename_from_path(tl->thumb_uri);
714 if (tl->cache_enable)
718 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
719 tl->thumb_path_local = FALSE;
721 found = isfile(tl->thumb_path);
722 if (found && thumb_loader_std_setup(tl, tl->thumb_path)) return TRUE;
724 if (thumb_loader_std_fail_check(tl)) return FALSE;
726 return thumb_loader_std_next_source(tl, found);
729 if (!thumb_loader_std_setup(tl, tl->source_path))
731 thumb_loader_std_save(tl, NULL);
738 void thumb_loader_std_free(ThumbLoaderStd *tl)
742 thumb_loader_std_reset(tl);
746 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl, gint with_fallback)
750 if (tl && tl->pixbuf)
753 g_object_ref(pixbuf);
755 else if (with_fallback)
759 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
760 w = gdk_pixbuf_get_width(pixbuf);
761 h = gdk_pixbuf_get_height(pixbuf);
763 if (w > tl->requested_width || h > tl->requested_height)
767 if (thumb_loader_std_scale_aspect(tl->requested_width, tl->requested_height,
773 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
774 g_object_unref(G_OBJECT(tmp));
787 typedef struct _ThumbValidate ThumbValidate;
788 struct _ThumbValidate
794 void (*func_valid)(const gchar *path, gint valid, gpointer data);
800 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
802 thumb_loader_std_free(tv->tl);
807 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
815 if (tv->idle_id != -1) g_source_remove(tv->idle_id);
818 thumb_loader_std_thumb_file_validate_free(tv);
821 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gint valid)
823 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
825 thumb_loader_std_thumb_file_validate_free(tv);
828 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
830 ThumbValidate *tv = data;
834 pixbuf = thumb_loader_std_get_pixbuf(tv->tl, FALSE);
838 const gchar *mtime_str;
840 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
841 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
842 if (uri && mtime_str)
844 if (strncmp(uri, "file:", strlen("file:")) == 0)
849 target = g_filename_from_uri(uri, NULL, NULL);
850 if (stat(target, &st) == 0 &&
851 st.st_mtime == strtol(mtime_str, NULL, 10))
861 if (debug) printf("thumb uri foreign, doing day check: %s\n", uri);
863 if (stat_utf8(tv->path, &st))
868 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
876 g_object_unref(pixbuf);
879 thumb_loader_std_thumb_file_validate_finish(tv, valid);
882 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
884 ThumbValidate *tv = data;
886 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
889 static gint thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
891 ThumbValidate *tv = data;
894 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
899 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
900 void (*func_valid)(const gchar *path, gint valid, gpointer data),
905 tv = g_new0(ThumbValidate, 1);
907 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
908 thumb_loader_std_set_callbacks(tv->tl,
909 thumb_loader_std_thumb_file_validate_done_cb,
910 thumb_loader_std_thumb_file_validate_error_cb,
913 thumb_loader_std_reset(tv->tl);
915 tv->path = g_strdup(thumb_path);
916 tv->days = allowed_days;
917 tv->func_valid = func_valid;
920 if (!thumb_loader_std_setup(tv->tl, thumb_path))
922 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
932 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gint local,
933 const gchar *subfolder)
937 thumb_path = thumb_std_cache_path(source,
938 (local) ? filename_from_path(uri) : uri,
940 if (isfile(thumb_path))
942 if (debug) printf("thumb removing: %s\n", thumb_path);
943 unlink_file(thumb_path);
948 /* this also removes local thumbnails (the source is gone so it makes sense) */
949 void thumb_std_maint_removed(const gchar *source)
954 sourcel = path_from_utf8(source);
955 uri = g_filename_to_uri(sourcel, NULL, NULL);
958 /* all this to remove a thumbnail? */
960 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
961 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
962 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
963 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
964 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
969 typedef struct _TMaintMove TMaintMove;
982 static GList *thumb_std_maint_move_list = NULL;
983 static GList *thumb_std_maint_move_tail = NULL;
986 static void thumb_std_maint_move_step(TMaintMove *tm);
987 static gint thumb_std_maint_move_idle(gpointer data);
990 static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data)
992 TMaintMove *tm = data;
995 pixbuf = thumb_loader_std_get_pixbuf(tm->tl, FALSE);
999 const gchar *mtime_str;
1001 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1002 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1004 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1008 /* The validation utility abuses ThumbLoader, and we
1009 * abuse the utility just to load the thumbnail,
1010 * but the loader needs to look sane for the save to complete.
1013 tm->tl->cache_enable = TRUE;
1014 tm->tl->cache_hit = FALSE;
1015 tm->tl->cache_local = FALSE;
1017 g_free(tm->tl->source_path);
1018 tm->tl->source_path = g_strdup(tm->dest);
1019 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1021 pathl = path_from_utf8(tm->tl->source_path);
1022 g_free(tm->tl->thumb_uri);
1023 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1024 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1027 g_free(tm->tl->thumb_path);
1028 tm->tl->thumb_path = NULL;
1029 tm->tl->thumb_path_local = FALSE;
1031 if (debug) printf("thumb move attempting save:\n");
1033 thumb_loader_std_save(tm->tl, pixbuf);
1036 if (debug) printf("thumb move unlink: %s\n", tm->thumb_path);
1037 unlink_file(tm->thumb_path);
1040 thumb_std_maint_move_step(tm);
1043 static void thumb_std_maint_move_step(TMaintMove *tm)
1045 const gchar *folder;
1052 g_free(tm->source_uri);
1053 g_free(tm->thumb_path);
1056 if (thumb_std_maint_move_list)
1058 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1064 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1066 g_free(tm->thumb_path);
1067 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1068 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1069 thumb_std_maint_move_validate_cb, tm);
1072 static gint thumb_std_maint_move_idle(gpointer data)
1077 if (!thumb_std_maint_move_list) return FALSE;
1079 tm = thumb_std_maint_move_list->data;
1081 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1082 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1084 pathl = path_from_utf8(tm->source);
1085 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1090 thumb_std_maint_move_step(tm);
1095 /* This will schedule a move of the thumbnail for source image to dest when idle.
1096 * We do this so that file renaming or moving speed is not sacrificed by
1097 * moving the thumbnails at the same time because:
1099 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1101 * The thumbnails are processed when the app is idle. If the app
1102 * exits early well too bad - they can simply be regenerated from scratch.
1104 * This does not manage local thumbnails (fixme ?)
1106 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1110 tm = g_new0(TMaintMove, 1);
1111 tm->source = g_strdup(source);
1112 tm->dest = g_strdup(dest);
1114 if (!thumb_std_maint_move_list)
1116 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1119 if (thumb_std_maint_move_tail)
1121 g_list_append(thumb_std_maint_move_tail, tm);
1122 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1126 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1127 thumb_std_maint_move_tail = thumb_std_maint_move_list;