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 * https://www.freedesktop.org/wiki/Specifications/thumbnails/ \n
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 ThumbLoaderStd::Func func_done,
98 ThumbLoaderStd::Func func_error,
99 ThumbLoaderStd::Func 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 = nullptr;
121 g_free(tl->thumb_uri);
122 tl->thumb_uri = nullptr;
123 tl->local_uri = nullptr;
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 = nullptr;
143 if (!path || !uri || !cache_subfolder) return nullptr;
145 md5_text = md5_get_string(reinterpret_cast<const guchar *>(uri), strlen(uri));
147 if (!md5_text) return nullptr;
149 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
153 gchar *base = remove_level_from_path(path);
155 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
160 result = g_build_filename(get_thumbnails_standard_cache_dir(),
161 cache_subfolder, name, NULL);
170 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
175 if (!tl->fd || !tl->thumb_uri) return nullptr;
179 w = gdk_pixbuf_get_width(pixbuf);
180 h = gdk_pixbuf_get_height(pixbuf);
184 w = tl->requested_width;
185 h = tl->requested_height;
190 folder = THUMB_FOLDER_FAIL;
192 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
194 folder = THUMB_FOLDER_LARGE;
198 folder = THUMB_FOLDER_NORMAL;
201 return thumb_std_cache_path(tl->fd->path,
202 (local) ? tl->local_uri : tl->thumb_uri,
206 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
209 gboolean result = FALSE;
211 fail_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, TRUE);
212 if (isfile(fail_path))
224 pathl = path_from_utf8(fail_path);
225 pixbuf = gdk_pixbuf_new_from_file(pathl, nullptr);
231 const gchar *mtime_str;
233 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
234 if (mtime_str && strtol(mtime_str, nullptr, 10) == tl->source_mtime)
237 DEBUG_1("thumb fail valid: %s", tl->fd->path);
238 DEBUG_1(" thumb: %s", fail_path);
241 g_object_unref(G_OBJECT(pixbuf));
244 if (!result) unlink_file(fail_path);
251 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
253 const gchar *valid_uri;
255 const gchar *mtime_str;
259 if (!pixbuf) return FALSE;
261 w = gdk_pixbuf_get_width(pixbuf);
262 h = gdk_pixbuf_get_height(pixbuf);
264 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
265 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
267 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
269 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
270 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
272 if (!mtime_str || !uri || !valid_uri) return FALSE;
273 if (strcmp(uri, valid_uri) != 0) return FALSE;
275 mtime = strtol(mtime_str, nullptr, 10);
276 if (tl->source_mtime != mtime) return FALSE;
281 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
287 if (!tl->cache_enable || tl->cache_hit) return;
288 if (tl->thumb_path) return;
292 /* local failures are not stored */
293 if (tl->cache_local) return;
295 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
300 g_object_ref(G_OBJECT(pixbuf));
304 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
307 g_object_unref(G_OBJECT(pixbuf));
310 tl->thumb_path_local = tl->cache_local;
312 /* create thumbnail dir if needed */
313 base_path = remove_level_from_path(tl->thumb_path);
316 if (!isdir(base_path))
321 source_base = remove_level_from_path(tl->fd->path);
322 if (stat_utf8(source_base, &st))
324 recursive_mkdir_if_not_exists(base_path, st.st_mode);
331 recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
335 DEBUG_1("thumb saving: %s", tl->fd->path);
336 DEBUG_1(" saved: %s", tl->thumb_path);
338 /* save thumb, using a temp file then renaming into place */
339 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
342 const gchar *mark_uri;
348 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
350 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
351 mark_mtime = g_strdup_printf("%llu", static_cast<unsigned long long>(tl->source_mtime));
352 pathl = path_from_utf8(tmp_path);
353 success = gdk_pixbuf_save(pixbuf, pathl, "png", nullptr,
354 THUMB_MARKER_URI, mark_uri,
355 THUMB_MARKER_MTIME, mark_mtime,
356 THUMB_MARKER_APP, mark_app,
360 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
361 success = rename_file(tmp_path, tl->thumb_path);
372 DEBUG_1("thumb save failed: %s", tl->fd->path);
373 DEBUG_1(" thumb: %s", tl->thumb_path);
378 g_object_unref(G_OBJECT(pixbuf));
381 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
383 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
384 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
388 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
389 ColorMan *cm = nullptr;
390 ExifData *exif = nullptr;
391 gint color_profile_from_image = COLOR_PROFILE_NONE;
392 ColorManProfileType input_type = COLOR_PROFILE_MEM;
393 ColorManProfileType screen_type;
394 const gchar *input_file = nullptr;
395 guchar *profile = nullptr;
399 if (!options->thumbnails.use_color_management)
404 sw = gdk_pixbuf_get_width(pixbuf);
405 sh = gdk_pixbuf_get_height(pixbuf);
407 exif = exif_read_fd(fd);
411 if (g_strcmp0(fd->format_name, "heif") == 0)
413 profile = heif_color_profile(fd, &profile_len);
418 profile = exif_get_color_profile(exif, &profile_len);
423 DEBUG_1("Found embedded color profile");
424 color_profile_from_image = COLOR_PROFILE_MEM;
428 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
432 /* Exif 2.21 specification */
433 if (!strcmp(interop_index, "R98"))
435 color_profile_from_image = COLOR_PROFILE_SRGB;
436 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
438 else if (!strcmp(interop_index, "R03"))
440 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
441 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
443 g_free(interop_index);
449 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
450 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
453 color_profile_from_image = COLOR_PROFILE_SRGB;
454 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
458 /* non-standard way of specifying AdobeRGB (used by some software) */
459 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
460 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
465 if(color_profile_from_image != COLOR_PROFILE_NONE)
467 // transform image, we always use sRGB as target for thumbnails
468 screen_type = COLOR_PROFILE_SRGB;
472 cm = color_man_new_embedded(nullptr, pixbuf,
473 profile, profile_len,
474 screen_type, nullptr, nullptr, 0);
479 cm = color_man_new(nullptr, pixbuf,
480 input_type, input_file,
481 screen_type, nullptr, nullptr, 0);
485 color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
490 exif_free_fd(fd, exif);
494 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
496 GdkPixbuf *pixbuf_thumb = nullptr;
498 GdkPixbuf *rotated = nullptr;
502 if (!tl->cache_hit && options->image.exif_rotate_enable)
504 if (!tl->fd->exif_orientation)
506 if (g_strcmp0(tl->fd->format_name, "heif") != 0)
508 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
512 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
516 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
518 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
523 sw = gdk_pixbuf_get_width(pixbuf);
524 sh = gdk_pixbuf_get_height(pixbuf);
526 if (tl->cache_enable)
530 gint cache_w, cache_h;
532 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
534 cache_w = cache_h = THUMB_SIZE_LARGE;
538 cache_w = cache_h = THUMB_SIZE_NORMAL;
541 if (sw > cache_w || sh > cache_h || shrunk)
543 gint thumb_w, thumb_h;
546 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
549 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
550 static_cast<GdkInterpType>(options->thumbnails.quality));
554 pixbuf_thumb = pixbuf;
555 g_object_ref(G_OBJECT(pixbuf_thumb));
558 /* do not save the thumbnail if the source file has changed meanwhile -
559 the thumbnail is most probably broken */
560 if (stat_utf8(tl->fd->path, &st) &&
561 tl->source_mtime == st.st_mtime &&
562 tl->source_size == st.st_size)
564 thumb_loader_std_save(tl, pixbuf_thumb);
568 else if (tl->cache_hit &&
569 tl->cache_local && !tl->thumb_path_local)
571 /* A local cache save was requested, but a valid thumb is in $HOME,
572 * so specifically save as a local thumbnail.
574 g_free(tl->thumb_path);
575 tl->thumb_path = nullptr;
577 tl->cache_hit = FALSE;
579 DEBUG_1("thumb copied: %s", tl->fd->path);
581 thumb_loader_std_save(tl, pixbuf);
585 if (sw <= tl->requested_width && sh <= tl->requested_height)
588 g_object_ref(result);
592 gint thumb_w, thumb_h;
596 pixbuf = pixbuf_thumb;
597 sw = gdk_pixbuf_get_width(pixbuf);
598 sh = gdk_pixbuf_get_height(pixbuf);
601 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
604 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
605 static_cast<GdkInterpType>(options->thumbnails.quality));
610 g_object_ref(result);
614 // apply color correction, if required
615 thumb_loader_std_calibrate_pixbuf(tl->fd, result);
617 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
618 if (rotated) g_object_unref(rotated);
623 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
625 image_loader_free(tl->il);
630 if (!tl->thumb_path_local && remove_broken)
632 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
633 unlink_file(tl->thumb_path);
636 g_free(tl->thumb_path);
637 tl->thumb_path = nullptr;
639 if (!tl->thumb_path_local)
641 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, nullptr, FALSE);
642 if (isfile(tl->thumb_path))
644 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
645 if (thumb_loader_std_setup(tl, fd))
648 tl->thumb_path_local = TRUE;
654 g_free(tl->thumb_path);
655 tl->thumb_path = nullptr;
658 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
661 thumb_loader_std_save(tl, nullptr);
665 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
667 auto tl = static_cast<ThumbLoaderStd *>(data);
670 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
671 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
673 pixbuf = image_loader_get_pixbuf(tl->il);
676 DEBUG_1("...but no pixbuf");
677 thumb_loader_std_error_cb(il, data);
681 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
683 if (thumb_loader_std_next_source(tl, TRUE)) return;
685 if (tl->func_error) tl->func_error(tl, tl->data);
689 tl->cache_hit = (tl->thumb_path != nullptr);
693 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
694 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
697 if (tl->func_done) tl->func_done(tl, tl->data);
700 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
702 auto tl = static_cast<ThumbLoaderStd *>(data);
704 /* if at least some of the image is available, go to done */
705 if (image_loader_get_pixbuf(tl->il) != nullptr)
707 thumb_loader_std_done_cb(il, data);
711 DEBUG_1("thumb image error: %s", tl->fd->path);
712 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
714 if (thumb_loader_std_next_source(tl, TRUE)) return;
716 thumb_loader_std_set_fallback(tl);
718 if (tl->func_error) tl->func_error(tl, tl->data);
721 static void thumb_loader_std_progress_cb(ImageLoader *, gdouble percent, gpointer data)
723 auto tl = static_cast<ThumbLoaderStd *>(data);
725 tl->progress = percent;
727 if (tl->func_progress) tl->func_progress(tl, tl->data);
730 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
732 tl->il = image_loader_new(fd);
733 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
735 /* this will speed up jpegs by up to 3x in some cases */
736 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
737 tl->requested_height <= THUMB_SIZE_NORMAL)
739 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
743 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
746 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
747 if (tl->func_progress)
749 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
751 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
753 if (image_loader_start(tl->il))
758 image_loader_free(tl->il);
764 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
765 * a valid existing thumb is found anywhere the local thumb will not be created.
767 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
771 tl->cache_enable = enable_cache;
772 tl->cache_local = local;
773 tl->cache_retry = retry_failed;
776 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
778 static gchar *thumb_cache = nullptr;
781 if (!tl || !fd) return FALSE;
783 thumb_loader_std_reset(tl);
786 tl->fd = file_data_ref(fd);
787 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))
789 thumb_loader_std_set_fallback(tl);
792 tl->source_mtime = st.st_mtime;
793 tl->source_size = st.st_size;
794 tl->source_mode = st.st_mode;
798 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
801 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
805 pathl = path_from_utf8(fd->path);
806 tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
807 tl->local_uri = filename_from_path(tl->thumb_uri);
811 if (tl->cache_enable)
815 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, FALSE);
816 tl->thumb_path_local = FALSE;
818 found = isfile(tl->thumb_path);
821 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
822 if (thumb_loader_std_setup(tl, fd))
830 if (thumb_loader_std_fail_check(tl) ||
831 !thumb_loader_std_next_source(tl, found))
833 thumb_loader_std_set_fallback(tl);
839 if (!thumb_loader_std_setup(tl, tl->fd))
841 thumb_loader_std_save(tl, nullptr);
842 thumb_loader_std_set_fallback(tl);
849 void thumb_loader_std_free(ThumbLoaderStd *tl)
853 thumb_loader_std_reset(tl);
857 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
861 if (tl && tl->fd && tl->fd->thumb_pixbuf)
863 pixbuf = tl->fd->thumb_pixbuf;
864 g_object_ref(pixbuf);
868 pixbuf = pixbuf_fallback(nullptr, tl->requested_width, tl->requested_height);
881 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
884 guint idle_id; /* event source id */
887 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
889 thumb_loader_std_free(tv->tl);
894 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
900 tv = static_cast<ThumbValidate *>(tl->data);
904 g_source_remove(tv->idle_id);
908 thumb_loader_std_thumb_file_validate_free(tv);
911 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
913 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
915 thumb_loader_std_thumb_file_validate_free(tv);
918 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *, gpointer data)
920 auto tv = static_cast<ThumbValidate *>(data);
922 gboolean valid = FALSE;
924 /* get the original thumbnail pixbuf (unrotated, with original options)
925 this is called from image_loader done callback, so tv->tl->il must exist*/
926 pixbuf = image_loader_get_pixbuf(tv->tl->il);
930 const gchar *mtime_str;
932 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
933 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
934 if (uri && mtime_str)
936 if (strncmp(uri, "file:", strlen("file:")) == 0)
941 target = g_filename_from_uri(uri, nullptr, nullptr);
942 if (stat(target, &st) == 0 &&
943 st.st_mtime == strtol(mtime_str, nullptr, 10))
953 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
955 if (stat_utf8(tv->path, &st))
960 if (st.st_atime >= now - static_cast<time_t>(tv->days) * 24 * 60 * 60)
969 DEBUG_1("invalid image found in std cache: %s", tv->path);
973 thumb_loader_std_thumb_file_validate_finish(tv, valid);
976 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *, gpointer data)
978 auto tv = static_cast<ThumbValidate *>(data);
980 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
983 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
985 auto tv = static_cast<ThumbValidate *>(data);
988 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
990 return G_SOURCE_REMOVE;
993 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
994 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
999 tv = g_new0(ThumbValidate, 1);
1001 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
1002 thumb_loader_std_set_callbacks(tv->tl,
1003 thumb_loader_std_thumb_file_validate_done_cb,
1004 thumb_loader_std_thumb_file_validate_error_cb,
1007 thumb_loader_std_reset(tv->tl);
1009 tv->path = g_strdup(thumb_path);
1010 tv->days = allowed_days;
1011 tv->func_valid = func_valid;
1014 FileData *fd = file_data_new_no_grouping(thumb_path);
1015 if (!thumb_loader_std_setup(tv->tl, fd))
1017 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
1024 file_data_unref(fd);
1028 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1029 const gchar *subfolder)
1033 thumb_path = thumb_std_cache_path(source,
1034 (local) ? filename_from_path(uri) : uri,
1036 if (isfile(thumb_path))
1038 DEBUG_1("thumb removing: %s", thumb_path);
1039 unlink_file(thumb_path);
1044 /* this also removes local thumbnails (the source is gone so it makes sense) */
1045 void thumb_std_maint_removed(const gchar *source)
1050 sourcel = path_from_utf8(source);
1051 uri = g_filename_to_uri(sourcel, nullptr, nullptr);
1054 /* all this to remove a thumbnail? */
1056 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1057 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1058 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1059 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1060 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1077 static GList *thumb_std_maint_move_list = nullptr;
1078 static GList *thumb_std_maint_move_tail = nullptr;
1081 static void thumb_std_maint_move_step(TMaintMove *tm);
1082 static gboolean thumb_std_maint_move_idle(gpointer data);
1085 static void thumb_std_maint_move_validate_cb(const gchar *, gboolean, gpointer data)
1087 auto tm = static_cast<TMaintMove *>(data);
1090 /* get the original thumbnail pixbuf (unrotated, with original options)
1091 this is called from image_loader done callback, so tm->tl->il must exist*/
1092 pixbuf = image_loader_get_pixbuf(tm->tl->il);
1096 const gchar *mtime_str;
1098 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1099 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1101 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1105 /* The validation utility abuses ThumbLoader, and we
1106 * abuse the utility just to load the thumbnail,
1107 * but the loader needs to look sane for the save to complete.
1110 tm->tl->cache_enable = TRUE;
1111 tm->tl->cache_hit = FALSE;
1112 tm->tl->cache_local = FALSE;
1113 file_data_unref(tm->tl->fd);
1114 tm->tl->fd = file_data_new_group(tm->dest);
1115 tm->tl->source_mtime = strtol(mtime_str, nullptr, 10);
1117 pathl = path_from_utf8(tm->tl->fd->path);
1118 g_free(tm->tl->thumb_uri);
1119 tm->tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1120 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1123 g_free(tm->tl->thumb_path);
1124 tm->tl->thumb_path = nullptr;
1125 tm->tl->thumb_path_local = FALSE;
1127 DEBUG_1("thumb move attempting save:");
1129 thumb_loader_std_save(tm->tl, pixbuf);
1132 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1133 unlink_file(tm->thumb_path);
1136 thumb_std_maint_move_step(tm);
1139 static void thumb_std_maint_move_step(TMaintMove *tm)
1141 const gchar *folder;
1148 g_free(tm->source_uri);
1149 g_free(tm->thumb_path);
1152 if (thumb_std_maint_move_list)
1154 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1160 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1162 g_free(tm->thumb_path);
1163 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1164 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1165 thumb_std_maint_move_validate_cb, tm);
1168 static gboolean thumb_std_maint_move_idle(gpointer)
1173 if (!thumb_std_maint_move_list) return G_SOURCE_REMOVE;
1175 tm = static_cast<TMaintMove *>(thumb_std_maint_move_list->data);
1177 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1178 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = nullptr;
1180 pathl = path_from_utf8(tm->source);
1181 tm->source_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1186 thumb_std_maint_move_step(tm);
1188 return G_SOURCE_REMOVE;
1191 /* This will schedule a move of the thumbnail for source image to dest when idle.
1192 * We do this so that file renaming or moving speed is not sacrificed by
1193 * moving the thumbnails at the same time because:
1195 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1197 * The thumbnails are processed when the app is idle. If the app
1198 * exits early well too bad - they can simply be regenerated from scratch.
1200 /** @FIXME This does not manage local thumbnails (fixme ?)
1202 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1206 tm = g_new0(TMaintMove, 1);
1207 tm->source = g_strdup(source);
1208 tm->dest = g_strdup(dest);
1210 if (!thumb_std_maint_move_list)
1212 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1215 if (thumb_std_maint_move_tail)
1217 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1218 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1222 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1223 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1226 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */