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"
37 * This thumbnail caching implementation attempts to conform
38 * to the Thumbnail Managing Standard proposed on freedesktop.org
39 * The standard is documented here:
40 * http://triq.net/~jens/thumbnail-spec/index.html
41 * (why isn't it actually hosted on freedesktop.org?)
43 * This code attempts to conform to version 0.7.0 of the standard.
46 * > Validation of the thumb's embedded uri is a simple strcmp between our
47 * version of the escaped uri and the thumb's escaped uri. But not all uri
48 * escape functions escape the same set of chars, comparing the unescaped
49 * versions may be more accurate.
50 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
51 * Storing the Size, Width, Height should probably be implemented.
55 #define THUMB_SIZE_NORMAL 128
56 #define THUMB_SIZE_LARGE 256
58 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
59 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
60 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
61 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
62 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
63 #define THUMB_MARKER_APP "tEXt::Software"
65 #define THUMB_PERMS_FOLDER 0700
66 #define THUMB_PERMS_THUMB 0600
71 *-----------------------------------------------------------------------------
73 *-----------------------------------------------------------------------------
77 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
78 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
81 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
85 tl = g_new0(ThumbLoaderStd, 1);
87 tl->standard_loader = TRUE;
88 tl->requested_width = width;
89 tl->requested_height = height;
90 tl->cache_enable = options->thumbnails.enable_caching;
95 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
96 ThumbLoaderStdFunc func_done,
97 ThumbLoaderStdFunc func_error,
98 ThumbLoaderStdFunc func_progress,
103 tl->func_done = func_done;
104 tl->func_error = func_error;
105 tl->func_progress = func_progress;
109 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
111 image_loader_free(tl->il);
114 file_data_unref(tl->fd);
117 g_free(tl->thumb_path);
118 tl->thumb_path = NULL;
120 g_free(tl->thumb_uri);
121 tl->thumb_uri = NULL;
122 tl->local_uri = NULL;
124 tl->thumb_path_local = FALSE;
126 tl->cache_hit = FALSE;
128 tl->source_mtime = 0;
135 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
136 const gchar *cache_subfolder)
138 gchar *result = NULL;
143 if (!path || !uri || !cache_subfolder) return NULL;
145 md5_get_digest((guchar *)uri, strlen(uri), digest);
146 md5_text = md5_digest_to_text(digest);
148 if (!md5_text) return NULL;
150 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
154 gchar *base = remove_level_from_path(path);
156 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
161 result = g_build_filename(get_thumbnails_standard_cache_dir(),
162 cache_subfolder, name, NULL);
171 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
176 if (!tl->fd || !tl->thumb_uri) return NULL;
180 w = gdk_pixbuf_get_width(pixbuf);
181 h = gdk_pixbuf_get_height(pixbuf);
185 w = tl->requested_width;
186 h = tl->requested_height;
191 folder = THUMB_FOLDER_FAIL;
193 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
195 folder = THUMB_FOLDER_LARGE;
199 folder = THUMB_FOLDER_NORMAL;
202 return thumb_std_cache_path(tl->fd->path,
203 (local) ? tl->local_uri : tl->thumb_uri,
207 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
210 gboolean result = FALSE;
212 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
213 if (isfile(fail_path))
225 pathl = path_from_utf8(fail_path);
226 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL);
232 const gchar *mtime_str;
234 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
235 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime)
238 DEBUG_1("thumb fail valid: %s", tl->fd->path);
239 DEBUG_1(" thumb: %s", fail_path);
242 g_object_unref(G_OBJECT(pixbuf));
245 if (!result) unlink_file(fail_path);
252 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
254 const gchar *valid_uri;
256 const gchar *mtime_str;
260 if (!pixbuf) return FALSE;
262 w = gdk_pixbuf_get_width(pixbuf);
263 h = gdk_pixbuf_get_height(pixbuf);
265 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
266 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
268 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
270 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
271 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
273 if (!mtime_str || !uri || !valid_uri) return FALSE;
274 if (strcmp(uri, valid_uri) != 0) return FALSE;
276 mtime = strtol(mtime_str, NULL, 10);
277 if (tl->source_mtime != mtime) return FALSE;
282 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
288 if (!tl->cache_enable || tl->cache_hit) return;
289 if (tl->thumb_path) return;
293 /* local failures are not stored */
294 if (tl->cache_local) return;
296 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
301 g_object_ref(G_OBJECT(pixbuf));
305 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
308 g_object_unref(G_OBJECT(pixbuf));
311 tl->thumb_path_local = tl->cache_local;
313 /* create thumbnail dir if needed */
314 base_path = remove_level_from_path(tl->thumb_path);
317 if (!isdir(base_path))
322 source_base = remove_level_from_path(tl->fd->path);
323 if (stat_utf8(source_base, &st))
325 recursive_mkdir_if_not_exists(base_path, st.st_mode);
332 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
336 DEBUG_1("thumb saving: %s", tl->fd->path);
337 DEBUG_1(" saved: %s", tl->thumb_path);
339 /* save thumb, using a temp file then renaming into place */
340 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
343 const gchar *mark_uri;
349 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
351 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
352 mark_mtime = g_strdup_printf("%llu", (unsigned long long)tl->source_mtime);
353 pathl = path_from_utf8(tmp_path);
354 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
355 THUMB_MARKER_URI, mark_uri,
356 THUMB_MARKER_MTIME, mark_mtime,
357 THUMB_MARKER_APP, mark_app,
361 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
362 success = rename_file(tmp_path, tl->thumb_path);
373 DEBUG_1("thumb save failed: %s", tl->fd->path);
374 DEBUG_1(" thumb: %s", tl->thumb_path);
379 g_object_unref(G_OBJECT(pixbuf));
382 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
384 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
385 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
389 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
391 ExifData *exif = NULL;
392 gint color_profile_from_image = COLOR_PROFILE_NONE;
393 ColorManProfileType input_type = COLOR_PROFILE_MEM;
394 ColorManProfileType screen_type;
395 const gchar *input_file = NULL;
396 guchar *profile = NULL;
400 sw = gdk_pixbuf_get_width(pixbuf);
401 sh = gdk_pixbuf_get_height(pixbuf);
403 exif = exif_read_fd(fd);
407 profile = exif_get_color_profile(exif, &profile_len);
410 DEBUG_1("Found embedded color profile");
411 color_profile_from_image = COLOR_PROFILE_MEM;
415 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
419 /* Exif 2.21 specification */
420 if (!strcmp(interop_index, "R98"))
422 color_profile_from_image = COLOR_PROFILE_SRGB;
423 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
425 else if (!strcmp(interop_index, "R03"))
427 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
428 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
430 g_free(interop_index);
436 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
437 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
440 color_profile_from_image = COLOR_PROFILE_SRGB;
441 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
445 /* non-standard way of specifying AdobeRGB (used by some software) */
446 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
447 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
452 if(color_profile_from_image != COLOR_PROFILE_NONE)
454 // transform image, we always use sRGB as target for thumbnails
455 screen_type = COLOR_PROFILE_SRGB;
459 cm = color_man_new_embedded(NULL, pixbuf,
460 profile, profile_len,
461 screen_type, NULL, NULL, 0);
466 cm = color_man_new(NULL, pixbuf,
467 input_type, input_file,
468 screen_type, NULL, NULL, 0);
472 color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
477 exif_free_fd(fd, exif);
481 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
483 GdkPixbuf *pixbuf_thumb = NULL;
485 GdkPixbuf *rotated = NULL;
489 if (!tl->cache_hit && options->image.exif_rotate_enable)
491 if (!tl->fd->exif_orientation)
493 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
496 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
498 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
503 sw = gdk_pixbuf_get_width(pixbuf);
504 sh = gdk_pixbuf_get_height(pixbuf);
506 if (tl->cache_enable)
510 gint cache_w, cache_h;
512 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
514 cache_w = cache_h = THUMB_SIZE_LARGE;
518 cache_w = cache_h = THUMB_SIZE_NORMAL;
521 if (sw > cache_w || sh > cache_h || shrunk)
523 gint thumb_w, thumb_h;
526 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
529 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
530 (GdkInterpType)options->thumbnails.quality);
534 pixbuf_thumb = pixbuf;
535 g_object_ref(G_OBJECT(pixbuf_thumb));
538 /* do not save the thumbnail if the source file has changed meanwhile -
539 the thumbnail is most probably broken */
540 if (stat_utf8(tl->fd->path, &st) &&
541 tl->source_mtime == st.st_mtime &&
542 tl->source_size == st.st_size)
544 thumb_loader_std_save(tl, pixbuf_thumb);
548 else if (tl->cache_hit &&
549 tl->cache_local && !tl->thumb_path_local)
551 /* A local cache save was requested, but a valid thumb is in $HOME,
552 * so specifically save as a local thumbnail.
554 g_free(tl->thumb_path);
555 tl->thumb_path = NULL;
557 tl->cache_hit = FALSE;
559 DEBUG_1("thumb copied: %s", tl->fd->path);
561 thumb_loader_std_save(tl, pixbuf);
565 if (sw <= tl->requested_width && sh <= tl->requested_height)
568 g_object_ref(result);
572 gint thumb_w, thumb_h;
576 pixbuf = pixbuf_thumb;
577 sw = gdk_pixbuf_get_width(pixbuf);
578 sh = gdk_pixbuf_get_height(pixbuf);
581 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
584 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
585 (GdkInterpType)options->thumbnails.quality);
590 g_object_ref(result);
594 // apply color correction, if required
595 thumb_loader_std_calibrate_pixbuf(tl->fd, result);
597 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
598 if (rotated) g_object_unref(rotated);
603 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
605 image_loader_free(tl->il);
610 if (!tl->thumb_path_local && remove_broken)
612 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
613 unlink_file(tl->thumb_path);
616 g_free(tl->thumb_path);
617 tl->thumb_path = NULL;
619 if (!tl->thumb_path_local)
621 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
622 if (isfile(tl->thumb_path))
624 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
625 if (thumb_loader_std_setup(tl, fd))
628 tl->thumb_path_local = TRUE;
634 g_free(tl->thumb_path);
635 tl->thumb_path = NULL;
638 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
641 thumb_loader_std_save(tl, NULL);
645 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
647 ThumbLoaderStd *tl = data;
650 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
651 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
653 pixbuf = image_loader_get_pixbuf(tl->il);
656 DEBUG_1("...but no pixbuf");
657 thumb_loader_std_error_cb(il, data);
661 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
663 if (thumb_loader_std_next_source(tl, TRUE)) return;
665 if (tl->func_error) tl->func_error(tl, tl->data);
669 tl->cache_hit = (tl->thumb_path != NULL);
673 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
674 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
677 if (tl->func_done) tl->func_done(tl, tl->data);
680 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
682 ThumbLoaderStd *tl = data;
684 /* if at least some of the image is available, go to done */
685 if (image_loader_get_pixbuf(tl->il) != NULL)
687 thumb_loader_std_done_cb(il, data);
691 DEBUG_1("thumb image error: %s", tl->fd->path);
692 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
694 if (thumb_loader_std_next_source(tl, TRUE)) return;
696 thumb_loader_std_set_fallback(tl);
698 if (tl->func_error) tl->func_error(tl, tl->data);
701 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data)
703 ThumbLoaderStd *tl = data;
705 tl->progress = (gdouble)percent;
707 if (tl->func_progress) tl->func_progress(tl, tl->data);
710 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
712 tl->il = image_loader_new(fd);
713 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
715 /* this will speed up jpegs by up to 3x in some cases */
716 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
717 tl->requested_height <= THUMB_SIZE_NORMAL)
719 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
723 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
726 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
727 if (tl->func_progress)
729 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
731 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
733 if (image_loader_start(tl->il))
738 image_loader_free(tl->il);
744 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
745 * a valid existing thumb is found anywhere the local thumb will not be created.
747 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
751 tl->cache_enable = enable_cache;
752 tl->cache_local = local;
753 tl->cache_retry = retry_failed;
756 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
758 static gchar *thumb_cache = NULL;
761 if (!tl || !fd) return FALSE;
763 thumb_loader_std_reset(tl);
766 tl->fd = file_data_ref(fd);
767 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))
769 thumb_loader_std_set_fallback(tl);
772 tl->source_mtime = st.st_mtime;
773 tl->source_size = st.st_size;
774 tl->source_mode = st.st_mode;
778 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
781 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
785 pathl = path_from_utf8(fd->path);
786 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
787 tl->local_uri = filename_from_path(tl->thumb_uri);
791 if (tl->cache_enable)
795 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE);
796 tl->thumb_path_local = FALSE;
798 found = isfile(tl->thumb_path);
801 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
802 if (thumb_loader_std_setup(tl, fd))
810 if (thumb_loader_std_fail_check(tl) ||
811 !thumb_loader_std_next_source(tl, found))
813 thumb_loader_std_set_fallback(tl);
819 if (!thumb_loader_std_setup(tl, tl->fd))
821 thumb_loader_std_save(tl, NULL);
822 thumb_loader_std_set_fallback(tl);
829 void thumb_loader_std_free(ThumbLoaderStd *tl)
833 thumb_loader_std_reset(tl);
837 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
841 if (tl && tl->fd && tl->fd->thumb_pixbuf)
843 pixbuf = tl->fd->thumb_pixbuf;
844 g_object_ref(pixbuf);
848 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height);
855 typedef struct _ThumbValidate ThumbValidate;
856 struct _ThumbValidate
862 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
865 guint idle_id; /* event source id */
868 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
870 thumb_loader_std_free(tv->tl);
875 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
885 g_source_remove(tv->idle_id);
889 thumb_loader_std_thumb_file_validate_free(tv);
892 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
894 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
896 thumb_loader_std_thumb_file_validate_free(tv);
899 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data)
901 ThumbValidate *tv = data;
903 gboolean valid = FALSE;
905 /* get the original thumbnail pixbuf (unrotated, with original options)
906 this is called from image_loader done callback, so tv->tl->il must exist*/
907 pixbuf = image_loader_get_pixbuf(tv->tl->il);
911 const gchar *mtime_str;
913 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
914 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
915 if (uri && mtime_str)
917 if (strncmp(uri, "file:", strlen("file:")) == 0)
922 target = g_filename_from_uri(uri, NULL, NULL);
923 if (stat(target, &st) == 0 &&
924 st.st_mtime == strtol(mtime_str, NULL, 10))
934 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
936 if (stat_utf8(tv->path, &st))
941 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60)
950 DEBUG_1("invalid image found in std cache: %s", tv->path);
954 thumb_loader_std_thumb_file_validate_finish(tv, valid);
957 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data)
959 ThumbValidate *tv = data;
961 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
964 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
966 ThumbValidate *tv = data;
969 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
974 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
975 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
980 tv = g_new0(ThumbValidate, 1);
982 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
983 thumb_loader_std_set_callbacks(tv->tl,
984 thumb_loader_std_thumb_file_validate_done_cb,
985 thumb_loader_std_thumb_file_validate_error_cb,
988 thumb_loader_std_reset(tv->tl);
990 tv->path = g_strdup(thumb_path);
991 tv->days = allowed_days;
992 tv->func_valid = func_valid;
995 FileData *fd = file_data_new_no_grouping(thumb_path);
996 if (!thumb_loader_std_setup(tv->tl, fd))
998 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
1005 file_data_unref(fd);
1009 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1010 const gchar *subfolder)
1014 thumb_path = thumb_std_cache_path(source,
1015 (local) ? filename_from_path(uri) : uri,
1017 if (isfile(thumb_path))
1019 DEBUG_1("thumb removing: %s", thumb_path);
1020 unlink_file(thumb_path);
1025 /* this also removes local thumbnails (the source is gone so it makes sense) */
1026 void thumb_std_maint_removed(const gchar *source)
1031 sourcel = path_from_utf8(source);
1032 uri = g_filename_to_uri(sourcel, NULL, NULL);
1035 /* all this to remove a thumbnail? */
1037 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1038 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1039 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1040 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1041 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1046 typedef struct _TMaintMove TMaintMove;
1059 static GList *thumb_std_maint_move_list = NULL;
1060 static GList *thumb_std_maint_move_tail = NULL;
1063 static void thumb_std_maint_move_step(TMaintMove *tm);
1064 static gboolean thumb_std_maint_move_idle(gpointer data);
1067 static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
1069 TMaintMove *tm = data;
1072 /* get the original thumbnail pixbuf (unrotated, with original options)
1073 this is called from image_loader done callback, so tm->tl->il must exist*/
1074 pixbuf = image_loader_get_pixbuf(tm->tl->il);
1078 const gchar *mtime_str;
1080 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1081 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1083 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1087 /* The validation utility abuses ThumbLoader, and we
1088 * abuse the utility just to load the thumbnail,
1089 * but the loader needs to look sane for the save to complete.
1092 tm->tl->cache_enable = TRUE;
1093 tm->tl->cache_hit = FALSE;
1094 tm->tl->cache_local = FALSE;
1095 file_data_unref(tm->tl->fd);
1096 tm->tl->fd = file_data_new_group(tm->dest);
1097 tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
1099 pathl = path_from_utf8(tm->tl->fd->path);
1100 g_free(tm->tl->thumb_uri);
1101 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL);
1102 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1105 g_free(tm->tl->thumb_path);
1106 tm->tl->thumb_path = NULL;
1107 tm->tl->thumb_path_local = FALSE;
1109 DEBUG_1("thumb move attempting save:");
1111 thumb_loader_std_save(tm->tl, pixbuf);
1114 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1115 unlink_file(tm->thumb_path);
1118 thumb_std_maint_move_step(tm);
1121 static void thumb_std_maint_move_step(TMaintMove *tm)
1123 const gchar *folder;
1130 g_free(tm->source_uri);
1131 g_free(tm->thumb_path);
1134 if (thumb_std_maint_move_list)
1136 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1142 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1144 g_free(tm->thumb_path);
1145 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1146 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1147 thumb_std_maint_move_validate_cb, tm);
1150 static gboolean thumb_std_maint_move_idle(gpointer data)
1155 if (!thumb_std_maint_move_list) return FALSE;
1157 tm = thumb_std_maint_move_list->data;
1159 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1160 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL;
1162 pathl = path_from_utf8(tm->source);
1163 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL);
1168 thumb_std_maint_move_step(tm);
1173 /* This will schedule a move of the thumbnail for source image to dest when idle.
1174 * We do this so that file renaming or moving speed is not sacrificed by
1175 * moving the thumbnails at the same time because:
1177 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1179 * The thumbnails are processed when the app is idle. If the app
1180 * exits early well too bad - they can simply be regenerated from scratch.
1182 * This does not manage local thumbnails (fixme ?)
1184 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1188 tm = g_new0(TMaintMove, 1);
1189 tm->source = g_strdup(source);
1190 tm->dest = g_strdup(dest);
1192 if (!thumb_std_maint_move_list)
1194 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL);
1197 if (thumb_std_maint_move_tail)
1199 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1200 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1204 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1205 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1208 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */