2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "thumb_standard.h"
26 #include "image-load.h"
28 #include "pixbuf_util.h"
29 #include "ui_fileops.h"
38 * This thumbnail caching implementation attempts to conform
39 * to the Thumbnail Managing Standard proposed on freedesktop.org
40 * The standard is documented here: \n
41 * http://triq.net/~jens/thumbnail-spec/index.html \n
42 * (why isn't it actually hosted on freedesktop.org?)
44 * This code attempts to conform to version 0.7.0 of the standard.
47 * > Validation of the thumb's embedded uri is a simple strcmp between our
48 * > version of the escaped uri and the thumb's escaped uri. But not all uri
49 * > escape functions escape the same set of chars, comparing the unescaped
50 * > versions may be more accurate. \n
51 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
52 * Storing the Size, Width, Height should probably be implemented.
56 #define THUMB_SIZE_NORMAL 128
57 #define THUMB_SIZE_LARGE 256
59 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
60 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
61 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
62 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
63 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
64 #define THUMB_MARKER_APP "tEXt::Software"
66 #define THUMB_PERMS_FOLDER 0700
67 #define THUMB_PERMS_THUMB 0600
72 *-----------------------------------------------------------------------------
74 *-----------------------------------------------------------------------------
78 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
79 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
82 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
86 tl = g_new0(ThumbLoaderStd, 1);
88 tl->standard_loader = TRUE;
89 tl->requested_width = width;
90 tl->requested_height = height;
91 tl->cache_enable = options->thumbnails.enable_caching;
96 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
97 ThumbLoaderStdFunc func_done,
98 ThumbLoaderStdFunc func_error,
99 ThumbLoaderStdFunc func_progress,
104 tl->func_done = func_done;
105 tl->func_error = func_error;
106 tl->func_progress = func_progress;
110 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
112 image_loader_free(tl->il);
115 file_data_unref(tl->fd);
118 g_free(tl->thumb_path);
119 tl->thumb_path = NULL;
121 g_free(tl->thumb_uri);
122 tl->thumb_uri = NULL;
123 tl->local_uri = NULL;
125 tl->thumb_path_local = FALSE;
127 tl->cache_hit = FALSE;
129 tl->source_mtime = 0;
136 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
137 const gchar *cache_subfolder)
139 gchar *result = NULL;
144 if (!path || !uri || !cache_subfolder) return NULL;
146 md5_get_digest((guchar *)uri, strlen(uri), digest);
147 md5_text = md5_digest_to_text(digest);
149 if (!md5_text) return NULL;
151 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
155 gchar *base = remove_level_from_path(path);
157 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
162 result = g_build_filename(get_thumbnails_standard_cache_dir(),
163 cache_subfolder, name, NULL);
172 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
177 if (!tl->fd || !tl->thumb_uri) return NULL;
181 w = gdk_pixbuf_get_width(pixbuf);
182 h = gdk_pixbuf_get_height(pixbuf);
186 w = tl->requested_width;
187 h = tl->requested_height;
192 folder = THUMB_FOLDER_FAIL;
194 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
196 folder = THUMB_FOLDER_LARGE;
200 folder = THUMB_FOLDER_NORMAL;
203 return thumb_std_cache_path(tl->fd->path,
204 (local) ? tl->local_uri : tl->thumb_uri,
208 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
211 gboolean result = FALSE;
213 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
214 if (isfile(fail_path))
226 pathl = path_from_utf8(fail_path);
227 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
233 const gchar *mtime_str;
235 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
236 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
239 DEBUG_1("thumb fail valid: %s", tl->fd->path);
240 DEBUG_1(" thumb: %s", fail_path);
243 g_object_unref(G_OBJECT(pixbuf));
246 if (!result) unlink_file(fail_path);
253 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
255 const gchar *valid_uri;
257 const gchar *mtime_str;
261 if (!pixbuf) return FALSE;
263 w = gdk_pixbuf_get_width(pixbuf);
264 h = gdk_pixbuf_get_height(pixbuf);
266 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
267 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
269 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
271 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
272 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
274 if (!mtime_str || !uri || !valid_uri) return FALSE;
275 if (strcmp(uri, valid_uri) != 0) return FALSE;
277 mtime = strtol(mtime_str, NULL, 10);
278 if (tl->source_mtime != mtime) return FALSE;
283 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
289 if (!tl->cache_enable || tl->cache_hit) return;
290 if (tl->thumb_path) return;
294 /* local failures are not stored */
295 if (tl->cache_local) return;
297 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
302 g_object_ref(G_OBJECT(pixbuf));
306 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
309 g_object_unref(G_OBJECT(pixbuf));
312 tl->thumb_path_local = tl->cache_local;
314 /* create thumbnail dir if needed */
315 base_path = remove_level_from_path(tl->thumb_path);
318 if (!isdir(base_path))
323 source_base = remove_level_from_path(tl->fd->path);
324 if (stat_utf8(source_base, &st))
326 recursive_mkdir_if_not_exists(base_path, st.st_mode);
333 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
337 DEBUG_1("thumb saving: %s", tl->fd->path);
338 DEBUG_1(" saved: %s", tl->thumb_path);
340 /* save thumb, using a temp file then renaming into place */
341 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
344 const gchar *mark_uri;
350 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
352 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
353 mark_mtime = g_strdup_printf("%llu", (unsigned long long)tl->source_mtime);
354 pathl = path_from_utf8(tmp_path);
355 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
356 THUMB_MARKER_URI, mark_uri,
357 THUMB_MARKER_MTIME, mark_mtime,
358 THUMB_MARKER_APP, mark_app,
362 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
363 success = rename_file(tmp_path, tl->thumb_path);
374 DEBUG_1("thumb save failed: %s", tl->fd->path);
375 DEBUG_1(" thumb: %s", tl->thumb_path);
380 g_object_unref(G_OBJECT(pixbuf));
383 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
385 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
386 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
389 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
391 GdkPixbuf *pixbuf_thumb = NULL;
393 GdkPixbuf *rotated = NULL;
397 if (!tl->cache_hit && options->image.exif_rotate_enable)
399 if (!tl->fd->exif_orientation)
401 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
404 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
406 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
411 sw = gdk_pixbuf_get_width(pixbuf);
412 sh = gdk_pixbuf_get_height(pixbuf);
414 if (tl->cache_enable)
418 gint cache_w, cache_h;
420 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
422 cache_w = cache_h = THUMB_SIZE_LARGE;
426 cache_w = cache_h = THUMB_SIZE_NORMAL;
429 if (sw > cache_w || sh > cache_h || shrunk)
431 gint thumb_w, thumb_h;
434 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
437 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
438 (GdkInterpType)options->thumbnails.quality);
442 pixbuf_thumb = pixbuf;
443 g_object_ref(G_OBJECT(pixbuf_thumb));
446 /* do not save the thumbnail if the source file has changed meanwhile -
447 the thumbnail is most probably broken */
448 if (stat_utf8(tl->fd->path, &st) &&
449 tl->source_mtime == st.st_mtime &&
450 tl->source_size == st.st_size)
452 thumb_loader_std_save(tl, pixbuf_thumb);
456 else if (tl->cache_hit &&
457 tl->cache_local && !tl->thumb_path_local)
459 /* A local cache save was requested, but a valid thumb is in $HOME,
460 * so specifically save as a local thumbnail.
462 g_free(tl->thumb_path);
463 tl->thumb_path = NULL;
465 tl->cache_hit = FALSE;
467 DEBUG_1("thumb copied: %s", tl->fd->path);
469 thumb_loader_std_save(tl, pixbuf);
473 if (sw <= tl->requested_width && sh <= tl->requested_height)
476 g_object_ref(result);
480 gint thumb_w, thumb_h;
484 pixbuf = pixbuf_thumb;
485 sw = gdk_pixbuf_get_width(pixbuf);
486 sh = gdk_pixbuf_get_height(pixbuf);
489 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
492 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
493 (GdkInterpType)options->thumbnails.quality);
498 g_object_ref(result);
502 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
503 if (rotated) g_object_unref(rotated);
508 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
510 image_loader_free(tl->il);
515 if (!tl->thumb_path_local && remove_broken)
517 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
518 unlink_file(tl->thumb_path);
521 g_free(tl->thumb_path);
522 tl->thumb_path = NULL;
524 if (!tl->thumb_path_local)
526 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
527 if (isfile(tl->thumb_path))
529 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
530 if (thumb_loader_std_setup(tl, fd))
533 tl->thumb_path_local = TRUE;
539 g_free(tl->thumb_path);
540 tl->thumb_path = NULL;
543 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
546 thumb_loader_std_save(tl, NULL);
550 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
552 ThumbLoaderStd *tl = data;
555 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
556 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
558 pixbuf = image_loader_get_pixbuf(tl->il);
561 DEBUG_1("...but no pixbuf");
562 thumb_loader_std_error_cb(il, data);
566 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
568 if (thumb_loader_std_next_source(tl, TRUE)) return;
570 if (tl->func_error) tl->func_error(tl, tl->data);
574 tl->cache_hit = (tl->thumb_path != NULL);
578 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
579 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
582 if (tl->func_done) tl->func_done(tl, tl->data);
585 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
587 ThumbLoaderStd *tl = data;
589 /* if at least some of the image is available, go to done */
590 if (image_loader_get_pixbuf(tl->il) != NULL)
592 thumb_loader_std_done_cb(il, data);
596 DEBUG_1("thumb image error: %s", tl->fd->path);
597 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
599 if (thumb_loader_std_next_source(tl, TRUE)) return;
601 thumb_loader_std_set_fallback(tl);
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 gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
617 tl->il = image_loader_new(fd);
618 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
620 /* this will speed up jpegs by up to 3x in some cases */
621 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
622 tl->requested_height <= THUMB_SIZE_NORMAL)
624 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
628 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
631 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
632 if (tl->func_progress)
634 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
636 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
638 if (image_loader_start(tl->il))
643 image_loader_free(tl->il);
649 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
650 * a valid existing thumb is found anywhere the local thumb will not be created.
652 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
656 tl->cache_enable = enable_cache;
657 tl->cache_local = local;
658 tl->cache_retry = retry_failed;
661 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
663 static gchar *thumb_cache = NULL;
666 if (!tl || !fd) return FALSE;
668 thumb_loader_std_reset(tl);
671 tl->fd = file_data_ref(fd);
672 if (!stat_utf8(fd->path, &st) || (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_DOCUMENT && !options->file_filter.disable))
674 thumb_loader_std_set_fallback(tl);
677 tl->source_mtime = st.st_mtime;
678 tl->source_size = st.st_size;
679 tl->source_mode = st.st_mode;
683 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
686 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
690 pathl = path_from_utf8(fd->path);
691 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
692 tl->local_uri = filename_from_path(tl->thumb_uri);
696 if (tl->cache_enable)
700 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
701 tl->thumb_path_local = FALSE;
703 found = isfile(tl->thumb_path);
706 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
707 if (thumb_loader_std_setup(tl, fd))
715 if (thumb_loader_std_fail_check(tl) ||
716 !thumb_loader_std_next_source(tl, found))
718 thumb_loader_std_set_fallback(tl);
724 if (!thumb_loader_std_setup(tl, tl->fd))
726 thumb_loader_std_save(tl, NULL);
727 thumb_loader_std_set_fallback(tl);
734 void thumb_loader_std_free(ThumbLoaderStd *tl)
738 thumb_loader_std_reset(tl);
742 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
746 if (tl && tl->fd && tl->fd->thumb_pixbuf)
748 pixbuf = tl->fd->thumb_pixbuf;
749 g_object_ref(pixbuf);
753 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
760 typedef struct _ThumbValidate ThumbValidate;
761 struct _ThumbValidate
767 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
770 guint idle_id; /* event source id */
773 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
775 thumb_loader_std_free(tv->tl);
780 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
790 g_source_remove(tv->idle_id);
794 thumb_loader_std_thumb_file_validate_free(tv);
797 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
799 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
801 thumb_loader_std_thumb_file_validate_free(tv);
804 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
806 ThumbValidate *tv = data;
808 gboolean valid = FALSE;
810 /* get the original thumbnail pixbuf (unrotated, with original options)
811 this is called from image_loader done callback, so tv->tl->il must exist*/
812 pixbuf = image_loader_get_pixbuf(tv->tl->il);
816 const gchar *mtime_str;
818 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
819 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
820 if (uri && mtime_str)
822 if (strncmp(uri, "file:", strlen("file:")) == 0)
827 target = g_filename_from_uri(uri, NULL, NULL);
828 if (stat(target, &st) == 0 &&
829 st.st_mtime == strtol(mtime_str, NULL, 10))
839 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
841 if (stat_utf8(tv->path, &st))
846 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
855 DEBUG_1("invalid image found in std cache: %s", tv->path);
859 thumb_loader_std_thumb_file_validate_finish(tv, valid);
862 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
864 ThumbValidate *tv = data;
866 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
869 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
871 ThumbValidate *tv = data;
874 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
879 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
880 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
885 tv = g_new0(ThumbValidate, 1);
887 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
888 thumb_loader_std_set_callbacks(tv->tl,
889 thumb_loader_std_thumb_file_validate_done_cb,
890 thumb_loader_std_thumb_file_validate_error_cb,
893 thumb_loader_std_reset(tv->tl);
895 tv->path = g_strdup(thumb_path);
896 tv->days = allowed_days;
897 tv->func_valid = func_valid;
900 FileData *fd = file_data_new_no_grouping(thumb_path);
901 if (!thumb_loader_std_setup(tv->tl, fd))
903 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
914 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
915 const gchar *subfolder)
919 thumb_path = thumb_std_cache_path(source,
920 (local) ? filename_from_path(uri) : uri,
922 if (isfile(thumb_path))
924 DEBUG_1("thumb removing: %s", thumb_path);
925 unlink_file(thumb_path);
930 /* this also removes local thumbnails (the source is gone so it makes sense) */
931 void thumb_std_maint_removed(const gchar *source)
936 sourcel = path_from_utf8(source);
937 uri = g_filename_to_uri(sourcel, NULL, NULL);
940 /* all this to remove a thumbnail? */
942 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
943 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
944 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
945 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
946 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
951 typedef struct _TMaintMove TMaintMove;
964 static GList *thumb_std_maint_move_list = NULL;
965 static GList *thumb_std_maint_move_tail = NULL;
968 static void thumb_std_maint_move_step(TMaintMove *tm);
969 static gboolean thumb_std_maint_move_idle(gpointer data);
972 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
974 TMaintMove *tm = data;
977 /* get the original thumbnail pixbuf (unrotated, with original options)
978 this is called from image_loader done callback, so tm->tl->il must exist*/
979 pixbuf = image_loader_get_pixbuf(tm->tl->il);
983 const gchar *mtime_str;
985 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
986 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
988 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
992 /* The validation utility abuses ThumbLoader, and we
993 * abuse the utility just to load the thumbnail,
994 * but the loader needs to look sane for the save to complete.
997 tm->tl->cache_enable = TRUE;
998 tm->tl->cache_hit = FALSE;
999 tm->tl->cache_local = FALSE;
1000 file_data_unref(tm->tl->fd);
1001 tm->tl->fd = file_data_new_group(tm->dest);
1002 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1004 pathl = path_from_utf8(tm->tl->fd->path);
1005 g_free(tm->tl->thumb_uri);
1006 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1007 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1010 g_free(tm->tl->thumb_path);
1011 tm->tl->thumb_path = NULL;
1012 tm->tl->thumb_path_local = FALSE;
1014 DEBUG_1("thumb move attempting save:");
1016 thumb_loader_std_save(tm->tl, pixbuf);
1019 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1020 unlink_file(tm->thumb_path);
1023 thumb_std_maint_move_step(tm);
1026 static void thumb_std_maint_move_step(TMaintMove *tm)
1028 const gchar *folder;
1035 g_free(tm->source_uri);
1036 g_free(tm->thumb_path);
1039 if (thumb_std_maint_move_list)
1041 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1047 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1049 g_free(tm->thumb_path);
1050 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1051 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1052 thumb_std_maint_move_validate_cb, tm);
1055 static gboolean thumb_std_maint_move_idle(gpointer data)
1060 if (!thumb_std_maint_move_list) return FALSE;
1062 tm = thumb_std_maint_move_list->data;
1064 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1065 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1067 pathl = path_from_utf8(tm->source);
1068 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1073 thumb_std_maint_move_step(tm);
1078 /* This will schedule a move of the thumbnail for source image to dest when idle.
1079 * We do this so that file renaming or moving speed is not sacrificed by
1080 * moving the thumbnails at the same time because:
1082 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1084 * The thumbnails are processed when the app is idle. If the app
1085 * exits early well too bad - they can simply be regenerated from scratch.
1087 * This does not manage local thumbnails (fixme ?)
1089 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1093 tm = g_new0(TMaintMove, 1);
1094 tm->source = g_strdup(source);
1095 tm->dest = g_strdup(dest);
1097 if (!thumb_std_maint_move_list)
1099 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1102 if (thumb_std_maint_move_tail)
1104 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1105 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1109 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1110 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1113 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */