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"
33 #include "color-man.h"
39 * This thumbnail caching implementation attempts to conform
40 * to the Thumbnail Managing Standard proposed on freedesktop.org
41 * The standard is documented here: \n
42 * http://triq.net/~jens/thumbnail-spec/index.html \n
43 * (why isn't it actually hosted on freedesktop.org?)
45 * This code attempts to conform to version 0.7.0 of the standard.
48 * > Validation of the thumb's embedded uri is a simple strcmp between our
49 * > version of the escaped uri and the thumb's escaped uri. But not all uri
50 * > escape functions escape the same set of chars, comparing the unescaped
51 * > versions may be more accurate. \n
52 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
53 * Storing the Size, Width, Height should probably be implemented.
57 #define THUMB_SIZE_NORMAL 128
58 #define THUMB_SIZE_LARGE 256
60 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
61 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
62 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
63 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
64 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
65 #define THUMB_MARKER_APP "tEXt::Software"
67 #define THUMB_PERMS_FOLDER 0700
68 #define THUMB_PERMS_THUMB 0600
73 *-----------------------------------------------------------------------------
75 *-----------------------------------------------------------------------------
79 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
80 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
83 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
87 tl = g_new0(ThumbLoaderStd, 1);
89 tl->standard_loader = TRUE;
90 tl->requested_width = width;
91 tl->requested_height = height;
92 tl->cache_enable = options->thumbnails.enable_caching;
97 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
98 ThumbLoaderStdFunc func_done,
99 ThumbLoaderStdFunc func_error,
100 ThumbLoaderStdFunc func_progress,
105 tl->func_done = func_done;
106 tl->func_error = func_error;
107 tl->func_progress = func_progress;
111 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
113 image_loader_free(tl->il);
116 file_data_unref(tl->fd);
119 g_free(tl->thumb_path);
120 tl->thumb_path = NULL;
122 g_free(tl->thumb_uri);
123 tl->thumb_uri = NULL;
124 tl->local_uri = NULL;
126 tl->thumb_path_local = FALSE;
128 tl->cache_hit = FALSE;
130 tl->source_mtime = 0;
137 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
138 const gchar *cache_subfolder)
140 gchar *result = NULL;
145 if (!path || !uri || !cache_subfolder) return NULL;
147 md5_get_digest((guchar *)uri, strlen(uri), digest);
148 md5_text = md5_digest_to_text(digest);
150 if (!md5_text) return NULL;
152 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
156 gchar *base = remove_level_from_path(path);
158 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
163 result = g_build_filename(get_thumbnails_standard_cache_dir(),
164 cache_subfolder, name, NULL);
173 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
178 if (!tl->fd || !tl->thumb_uri) return NULL;
182 w = gdk_pixbuf_get_width(pixbuf);
183 h = gdk_pixbuf_get_height(pixbuf);
187 w = tl->requested_width;
188 h = tl->requested_height;
193 folder = THUMB_FOLDER_FAIL;
195 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
197 folder = THUMB_FOLDER_LARGE;
201 folder = THUMB_FOLDER_NORMAL;
204 return thumb_std_cache_path(tl->fd->path,
205 (local) ? tl->local_uri : tl->thumb_uri,
209 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
212 gboolean result = FALSE;
214 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
215 if (isfile(fail_path))
227 pathl = path_from_utf8(fail_path);
228 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
234 const gchar *mtime_str;
236 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
237 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
240 DEBUG_1("thumb fail valid: %s", tl->fd->path);
241 DEBUG_1(" thumb: %s", fail_path);
244 g_object_unref(G_OBJECT(pixbuf));
247 if (!result) unlink_file(fail_path);
254 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
256 const gchar *valid_uri;
258 const gchar *mtime_str;
262 if (!pixbuf) return FALSE;
264 w = gdk_pixbuf_get_width(pixbuf);
265 h = gdk_pixbuf_get_height(pixbuf);
267 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
268 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
270 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
272 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
273 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
275 if (!mtime_str || !uri || !valid_uri) return FALSE;
276 if (strcmp(uri, valid_uri) != 0) return FALSE;
278 mtime = strtol(mtime_str, NULL, 10);
279 if (tl->source_mtime != mtime) return FALSE;
284 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
290 if (!tl->cache_enable || tl->cache_hit) return;
291 if (tl->thumb_path) return;
295 /* local failures are not stored */
296 if (tl->cache_local) return;
298 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
303 g_object_ref(G_OBJECT(pixbuf));
307 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
310 g_object_unref(G_OBJECT(pixbuf));
313 tl->thumb_path_local = tl->cache_local;
315 /* create thumbnail dir if needed */
316 base_path = remove_level_from_path(tl->thumb_path);
319 if (!isdir(base_path))
324 source_base = remove_level_from_path(tl->fd->path);
325 if (stat_utf8(source_base, &st))
327 recursive_mkdir_if_not_exists(base_path, st.st_mode);
334 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
338 DEBUG_1("thumb saving: %s", tl->fd->path);
339 DEBUG_1(" saved: %s", tl->thumb_path);
341 /* save thumb, using a temp file then renaming into place */
342 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
345 const gchar *mark_uri;
351 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
353 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
354 mark_mtime = g_strdup_printf("%llu", (unsigned long long)tl->source_mtime);
355 pathl = path_from_utf8(tmp_path);
356 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
357 THUMB_MARKER_URI, mark_uri,
358 THUMB_MARKER_MTIME, mark_mtime,
359 THUMB_MARKER_APP, mark_app,
363 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
364 success = rename_file(tmp_path, tl->thumb_path);
375 DEBUG_1("thumb save failed: %s", tl->fd->path);
376 DEBUG_1(" thumb: %s", tl->thumb_path);
381 g_object_unref(G_OBJECT(pixbuf));
384 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
386 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
387 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
391 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
393 ExifData *exif = NULL;
394 gint color_profile_from_image = COLOR_PROFILE_NONE;
395 ColorManProfileType input_type = COLOR_PROFILE_MEM;
396 ColorManProfileType screen_type;
397 const gchar *input_file = NULL;
398 guchar *profile = NULL;
402 sw = gdk_pixbuf_get_width(pixbuf);
403 sh = gdk_pixbuf_get_height(pixbuf);
405 exif = exif_read_fd(fd);
409 profile = exif_get_color_profile(exif, &profile_len);
412 DEBUG_1("Found embedded color profile");
413 color_profile_from_image = COLOR_PROFILE_MEM;
417 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
421 /* Exif 2.21 specification */
422 if (!strcmp(interop_index, "R98"))
424 color_profile_from_image = COLOR_PROFILE_SRGB;
425 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
427 else if (!strcmp(interop_index, "R03"))
429 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
430 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
432 g_free(interop_index);
438 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
439 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
442 color_profile_from_image = COLOR_PROFILE_SRGB;
443 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
447 /* non-standard way of specifying AdobeRGB (used by some software) */
448 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
449 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
454 if(color_profile_from_image != COLOR_PROFILE_NONE)
456 // transform image, we always use sRGB as target for thumbnails
457 screen_type = COLOR_PROFILE_SRGB;
461 cm = color_man_new_embedded(NULL, pixbuf,
462 profile, profile_len,
463 screen_type, NULL, NULL, 0);
468 cm = color_man_new(NULL, pixbuf,
469 input_type, input_file,
470 screen_type, NULL, NULL, 0);
474 color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
479 exif_free_fd(fd, exif);
483 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
485 GdkPixbuf *pixbuf_thumb = NULL;
487 GdkPixbuf *rotated = NULL;
491 if (!tl->cache_hit && options->image.exif_rotate_enable)
493 if (!tl->fd->exif_orientation)
495 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
498 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
500 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
505 sw = gdk_pixbuf_get_width(pixbuf);
506 sh = gdk_pixbuf_get_height(pixbuf);
508 if (tl->cache_enable)
512 gint cache_w, cache_h;
514 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
516 cache_w = cache_h = THUMB_SIZE_LARGE;
520 cache_w = cache_h = THUMB_SIZE_NORMAL;
523 if (sw > cache_w || sh > cache_h || shrunk)
525 gint thumb_w, thumb_h;
528 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
531 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
532 (GdkInterpType)options->thumbnails.quality);
536 pixbuf_thumb = pixbuf;
537 g_object_ref(G_OBJECT(pixbuf_thumb));
540 /* do not save the thumbnail if the source file has changed meanwhile -
541 the thumbnail is most probably broken */
542 if (stat_utf8(tl->fd->path, &st) &&
543 tl->source_mtime == st.st_mtime &&
544 tl->source_size == st.st_size)
546 thumb_loader_std_save(tl, pixbuf_thumb);
550 else if (tl->cache_hit &&
551 tl->cache_local && !tl->thumb_path_local)
553 /* A local cache save was requested, but a valid thumb is in $HOME,
554 * so specifically save as a local thumbnail.
556 g_free(tl->thumb_path);
557 tl->thumb_path = NULL;
559 tl->cache_hit = FALSE;
561 DEBUG_1("thumb copied: %s", tl->fd->path);
563 thumb_loader_std_save(tl, pixbuf);
567 if (sw <= tl->requested_width && sh <= tl->requested_height)
570 g_object_ref(result);
574 gint thumb_w, thumb_h;
578 pixbuf = pixbuf_thumb;
579 sw = gdk_pixbuf_get_width(pixbuf);
580 sh = gdk_pixbuf_get_height(pixbuf);
583 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
586 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
587 (GdkInterpType)options->thumbnails.quality);
592 g_object_ref(result);
596 // apply color correction, if required
597 thumb_loader_std_calibrate_pixbuf(tl->fd, result);
599 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
600 if (rotated) g_object_unref(rotated);
605 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
607 image_loader_free(tl->il);
612 if (!tl->thumb_path_local && remove_broken)
614 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
615 unlink_file(tl->thumb_path);
618 g_free(tl->thumb_path);
619 tl->thumb_path = NULL;
621 if (!tl->thumb_path_local)
623 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
624 if (isfile(tl->thumb_path))
626 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
627 if (thumb_loader_std_setup(tl, fd))
630 tl->thumb_path_local = TRUE;
636 g_free(tl->thumb_path);
637 tl->thumb_path = NULL;
640 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
643 thumb_loader_std_save(tl, NULL);
647 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
649 ThumbLoaderStd *tl = data;
652 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
653 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
655 pixbuf = image_loader_get_pixbuf(tl->il);
658 DEBUG_1("...but no pixbuf");
659 thumb_loader_std_error_cb(il, data);
663 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
665 if (thumb_loader_std_next_source(tl, TRUE)) return;
667 if (tl->func_error) tl->func_error(tl, tl->data);
671 tl->cache_hit = (tl->thumb_path != NULL);
675 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
676 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
679 if (tl->func_done) tl->func_done(tl, tl->data);
682 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
684 ThumbLoaderStd *tl = data;
686 /* if at least some of the image is available, go to done */
687 if (image_loader_get_pixbuf(tl->il) != NULL)
689 thumb_loader_std_done_cb(il, data);
693 DEBUG_1("thumb image error: %s", tl->fd->path);
694 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
696 if (thumb_loader_std_next_source(tl, TRUE)) return;
698 thumb_loader_std_set_fallback(tl);
700 if (tl->func_error) tl->func_error(tl, tl->data);
703 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
705 ThumbLoaderStd *tl = data;
707 tl->progress = (gdouble)percent;
709 if (tl->func_progress) tl->func_progress(tl, tl->data);
712 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
714 tl->il = image_loader_new(fd);
715 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
717 /* this will speed up jpegs by up to 3x in some cases */
718 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
719 tl->requested_height <= THUMB_SIZE_NORMAL)
721 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
725 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
728 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
729 if (tl->func_progress)
731 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
733 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
735 if (image_loader_start(tl->il))
740 image_loader_free(tl->il);
746 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
747 * a valid existing thumb is found anywhere the local thumb will not be created.
749 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
753 tl->cache_enable = enable_cache;
754 tl->cache_local = local;
755 tl->cache_retry = retry_failed;
758 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
760 static gchar *thumb_cache = NULL;
763 if (!tl || !fd) return FALSE;
765 thumb_loader_std_reset(tl);
768 tl->fd = file_data_ref(fd);
769 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))
771 thumb_loader_std_set_fallback(tl);
774 tl->source_mtime = st.st_mtime;
775 tl->source_size = st.st_size;
776 tl->source_mode = st.st_mode;
780 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
783 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
787 pathl = path_from_utf8(fd->path);
788 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
789 tl->local_uri = filename_from_path(tl->thumb_uri);
793 if (tl->cache_enable)
797 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
798 tl->thumb_path_local = FALSE;
800 found = isfile(tl->thumb_path);
803 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
804 if (thumb_loader_std_setup(tl, fd))
812 if (thumb_loader_std_fail_check(tl) ||
813 !thumb_loader_std_next_source(tl, found))
815 thumb_loader_std_set_fallback(tl);
821 if (!thumb_loader_std_setup(tl, tl->fd))
823 thumb_loader_std_save(tl, NULL);
824 thumb_loader_std_set_fallback(tl);
831 void thumb_loader_std_free(ThumbLoaderStd *tl)
835 thumb_loader_std_reset(tl);
839 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
843 if (tl && tl->fd && tl->fd->thumb_pixbuf)
845 pixbuf = tl->fd->thumb_pixbuf;
846 g_object_ref(pixbuf);
850 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
857 typedef struct _ThumbValidate ThumbValidate;
858 struct _ThumbValidate
864 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
867 guint idle_id; /* event source id */
870 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
872 thumb_loader_std_free(tv->tl);
877 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
887 g_source_remove(tv->idle_id);
891 thumb_loader_std_thumb_file_validate_free(tv);
894 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
896 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
898 thumb_loader_std_thumb_file_validate_free(tv);
901 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
903 ThumbValidate *tv = data;
905 gboolean valid = FALSE;
907 /* get the original thumbnail pixbuf (unrotated, with original options)
908 this is called from image_loader done callback, so tv->tl->il must exist*/
909 pixbuf = image_loader_get_pixbuf(tv->tl->il);
913 const gchar *mtime_str;
915 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
916 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
917 if (uri && mtime_str)
919 if (strncmp(uri, "file:", strlen("file:")) == 0)
924 target = g_filename_from_uri(uri, NULL, NULL);
925 if (stat(target, &st) == 0 &&
926 st.st_mtime == strtol(mtime_str, NULL, 10))
936 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
938 if (stat_utf8(tv->path, &st))
943 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
952 DEBUG_1("invalid image found in std cache: %s", tv->path);
956 thumb_loader_std_thumb_file_validate_finish(tv, valid);
959 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
961 ThumbValidate *tv = data;
963 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
966 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
968 ThumbValidate *tv = data;
971 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
976 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
977 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
982 tv = g_new0(ThumbValidate, 1);
984 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
985 thumb_loader_std_set_callbacks(tv->tl,
986 thumb_loader_std_thumb_file_validate_done_cb,
987 thumb_loader_std_thumb_file_validate_error_cb,
990 thumb_loader_std_reset(tv->tl);
992 tv->path = g_strdup(thumb_path);
993 tv->days = allowed_days;
994 tv->func_valid = func_valid;
997 FileData *fd = file_data_new_no_grouping(thumb_path);
998 if (!thumb_loader_std_setup(tv->tl, fd))
1000 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
1007 file_data_unref(fd);
1011 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1012 const gchar *subfolder)
1016 thumb_path = thumb_std_cache_path(source,
1017 (local) ? filename_from_path(uri) : uri,
1019 if (isfile(thumb_path))
1021 DEBUG_1("thumb removing: %s", thumb_path);
1022 unlink_file(thumb_path);
1027 /* this also removes local thumbnails (the source is gone so it makes sense) */
1028 void thumb_std_maint_removed(const gchar *source)
1033 sourcel = path_from_utf8(source);
1034 uri = g_filename_to_uri(sourcel, NULL, NULL);
1037 /* all this to remove a thumbnail? */
1039 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1040 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1041 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1042 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1043 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1048 typedef struct _TMaintMove TMaintMove;
1061 static GList *thumb_std_maint_move_list = NULL;
1062 static GList *thumb_std_maint_move_tail = NULL;
1065 static void thumb_std_maint_move_step(TMaintMove *tm);
1066 static gboolean thumb_std_maint_move_idle(gpointer data);
1069 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
1071 TMaintMove *tm = data;
1074 /* get the original thumbnail pixbuf (unrotated, with original options)
1075 this is called from image_loader done callback, so tm->tl->il must exist*/
1076 pixbuf = image_loader_get_pixbuf(tm->tl->il);
1080 const gchar *mtime_str;
1082 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1083 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1085 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1089 /* The validation utility abuses ThumbLoader, and we
1090 * abuse the utility just to load the thumbnail,
1091 * but the loader needs to look sane for the save to complete.
1094 tm->tl->cache_enable = TRUE;
1095 tm->tl->cache_hit = FALSE;
1096 tm->tl->cache_local = FALSE;
1097 file_data_unref(tm->tl->fd);
1098 tm->tl->fd = file_data_new_group(tm->dest);
1099 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1101 pathl = path_from_utf8(tm->tl->fd->path);
1102 g_free(tm->tl->thumb_uri);
1103 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1104 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1107 g_free(tm->tl->thumb_path);
1108 tm->tl->thumb_path = NULL;
1109 tm->tl->thumb_path_local = FALSE;
1111 DEBUG_1("thumb move attempting save:");
1113 thumb_loader_std_save(tm->tl, pixbuf);
1116 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1117 unlink_file(tm->thumb_path);
1120 thumb_std_maint_move_step(tm);
1123 static void thumb_std_maint_move_step(TMaintMove *tm)
1125 const gchar *folder;
1132 g_free(tm->source_uri);
1133 g_free(tm->thumb_path);
1136 if (thumb_std_maint_move_list)
1138 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1144 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1146 g_free(tm->thumb_path);
1147 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1148 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1149 thumb_std_maint_move_validate_cb, tm);
1152 static gboolean thumb_std_maint_move_idle(gpointer data)
1157 if (!thumb_std_maint_move_list) return FALSE;
1159 tm = thumb_std_maint_move_list->data;
1161 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1162 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1164 pathl = path_from_utf8(tm->source);
1165 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1170 thumb_std_maint_move_step(tm);
1175 /* This will schedule a move of the thumbnail for source image to dest when idle.
1176 * We do this so that file renaming or moving speed is not sacrificed by
1177 * moving the thumbnails at the same time because:
1179 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1181 * The thumbnails are processed when the app is idle. If the app
1182 * exits early well too bad - they can simply be regenerated from scratch.
1184 * This does not manage local thumbnails (fixme ?)
1186 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1190 tm = g_new0(TMaintMove, 1);
1191 tm->source = g_strdup(source);
1192 tm->dest = g_strdup(dest);
1194 if (!thumb_std_maint_move_list)
1196 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1199 if (thumb_std_maint_move_tail)
1201 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1202 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1206 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1207 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1210 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */