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.
22 #include "thumb-standard.h"
30 #include <glib-object.h>
35 #include "color-man.h"
39 #include "image-load.h"
43 #include "pixbuf-util.h"
45 #include "ui-fileops.h"
52 * This thumbnail caching implementation attempts to conform
53 * to the Thumbnail Managing Standard proposed on freedesktop.org
54 * The standard is documented here: \n
55 * https://www.freedesktop.org/wiki/Specifications/thumbnails/ \n
57 * This code attempts to conform to version 0.7.0 of the standard.
60 * > Validation of the thumb's embedded uri is a simple strcmp between our
61 * > version of the escaped uri and the thumb's escaped uri. But not all uri
62 * > escape functions escape the same set of chars, comparing the unescaped
63 * > versions may be more accurate. \n
64 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
65 * Storing the Size, Width, Height should probably be implemented.
70 THUMB_SIZE_NORMAL = 128,
71 THUMB_SIZE_LARGE = 256
74 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
75 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
76 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
77 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
78 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
79 #define THUMB_MARKER_APP "tEXt::Software"
82 *-----------------------------------------------------------------------------
84 *-----------------------------------------------------------------------------
88 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
89 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
92 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
96 tl = g_new0(ThumbLoaderStd, 1);
98 tl->standard_loader = TRUE;
99 tl->requested_width = width;
100 tl->requested_height = height;
101 tl->cache_enable = options->thumbnails.enable_caching;
106 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
107 ThumbLoaderStd::Func func_done,
108 ThumbLoaderStd::Func func_error,
109 ThumbLoaderStd::Func func_progress,
114 tl->func_done = func_done;
115 tl->func_error = func_error;
116 tl->func_progress = func_progress;
120 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
122 image_loader_free(tl->il);
125 file_data_unref(tl->fd);
128 g_free(tl->thumb_path);
129 tl->thumb_path = nullptr;
131 g_free(tl->thumb_uri);
132 tl->thumb_uri = nullptr;
133 tl->local_uri = nullptr;
135 tl->thumb_path_local = FALSE;
137 tl->cache_hit = FALSE;
139 tl->source_mtime = 0;
146 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
147 const gchar *cache_subfolder)
149 gchar *result = nullptr;
153 if (!path || !uri || !cache_subfolder) return nullptr;
155 md5_text = md5_get_string(reinterpret_cast<const guchar *>(uri), strlen(uri));
157 if (!md5_text) return nullptr;
159 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
163 gchar *base = remove_level_from_path(path);
165 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
170 result = g_build_filename(get_thumbnails_standard_cache_dir(),
171 cache_subfolder, name, NULL);
180 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
186 if (!tl->fd || !tl->thumb_uri) return nullptr;
190 w = gdk_pixbuf_get_width(pixbuf);
191 h = gdk_pixbuf_get_height(pixbuf);
195 w = tl->requested_width;
196 h = tl->requested_height;
201 folder = THUMB_FOLDER_FAIL;
203 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
205 folder = THUMB_FOLDER_LARGE;
209 folder = THUMB_FOLDER_NORMAL;
212 return thumb_std_cache_path(tl->fd->path,
213 (local) ? tl->local_uri : tl->thumb_uri,
217 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
220 gboolean result = FALSE;
222 fail_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, TRUE);
223 if (isfile(fail_path))
235 pathl = path_from_utf8(fail_path);
236 pixbuf = gdk_pixbuf_new_from_file(pathl, nullptr);
242 const gchar *mtime_str;
244 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
245 if (mtime_str && strtol(mtime_str, nullptr, 10) == tl->source_mtime)
248 DEBUG_1("thumb fail valid: %s", tl->fd->path);
249 DEBUG_1(" thumb: %s", fail_path);
252 g_object_unref(G_OBJECT(pixbuf));
255 if (!result) unlink_file(fail_path);
262 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
264 const gchar *valid_uri;
266 const gchar *mtime_str;
271 if (!pixbuf) return FALSE;
273 w = gdk_pixbuf_get_width(pixbuf);
274 h = gdk_pixbuf_get_height(pixbuf);
276 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
277 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
279 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
281 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
282 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
284 if (!mtime_str || !uri || !valid_uri) return FALSE;
285 if (strcmp(uri, valid_uri) != 0) return FALSE;
287 mtime = strtol(mtime_str, nullptr, 10);
288 if (tl->source_mtime != mtime) return FALSE;
293 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
299 if (!tl->cache_enable || tl->cache_hit) return;
300 if (tl->thumb_path) return;
304 /* local failures are not stored */
305 if (tl->cache_local) return;
307 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
312 g_object_ref(G_OBJECT(pixbuf));
316 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
319 g_object_unref(G_OBJECT(pixbuf));
322 tl->thumb_path_local = tl->cache_local;
324 /* create thumbnail dir if needed */
325 base_path = remove_level_from_path(tl->thumb_path);
328 if (!isdir(base_path))
333 source_base = remove_level_from_path(tl->fd->path);
334 if (stat_utf8(source_base, &st))
336 recursive_mkdir_if_not_exists(base_path, st.st_mode);
343 recursive_mkdir_if_not_exists(base_path, S_IRWXU);
347 DEBUG_1("thumb saving: %s", tl->fd->path);
348 DEBUG_1(" saved: %s", tl->thumb_path);
350 /* save thumb, using a temp file then renaming into place */
351 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
354 const gchar *mark_uri;
360 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
362 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
363 mark_mtime = g_strdup_printf("%llu", static_cast<unsigned long long>(tl->source_mtime));
364 pathl = path_from_utf8(tmp_path);
365 success = gdk_pixbuf_save(pixbuf, pathl, "png", nullptr,
366 THUMB_MARKER_URI, mark_uri,
367 THUMB_MARKER_MTIME, mark_mtime,
368 THUMB_MARKER_APP, mark_app,
372 chmod(pathl, (tl->cache_local) ? tl->source_mode : S_IRUSR & S_IWUSR);
373 success = rename_file(tmp_path, tl->thumb_path);
384 DEBUG_1("thumb save failed: %s", tl->fd->path);
385 DEBUG_1(" thumb: %s", tl->thumb_path);
390 g_object_unref(G_OBJECT(pixbuf));
393 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
395 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
396 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
400 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
401 ColorMan *cm = nullptr;
402 ExifData *exif = nullptr;
403 gint color_profile_from_image = COLOR_PROFILE_NONE;
404 ColorManProfileType input_type = COLOR_PROFILE_MEM;
405 ColorManProfileType screen_type;
406 const gchar *input_file = nullptr;
407 guchar *profile = nullptr;
412 if (!options->thumbnails.use_color_management)
417 sw = gdk_pixbuf_get_width(pixbuf);
418 sh = gdk_pixbuf_get_height(pixbuf);
420 exif = exif_read_fd(fd);
424 if (g_strcmp0(fd->format_name, "heif") == 0)
426 profile = heif_color_profile(fd, &profile_len);
431 profile = exif_get_color_profile(exif, &profile_len);
436 DEBUG_1("Found embedded color profile");
437 color_profile_from_image = COLOR_PROFILE_MEM;
441 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
445 /* Exif 2.21 specification */
446 if (!strcmp(interop_index, "R98"))
448 color_profile_from_image = COLOR_PROFILE_SRGB;
449 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
451 else if (!strcmp(interop_index, "R03"))
453 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
454 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
456 g_free(interop_index);
462 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
463 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
466 color_profile_from_image = COLOR_PROFILE_SRGB;
467 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
471 /* non-standard way of specifying AdobeRGB (used by some software) */
472 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
473 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
478 if(color_profile_from_image != COLOR_PROFILE_NONE)
480 // transform image, we always use sRGB as target for thumbnails
481 screen_type = COLOR_PROFILE_SRGB;
485 cm = color_man_new_embedded(nullptr, pixbuf,
486 profile, profile_len,
487 screen_type, nullptr, nullptr, 0);
492 cm = color_man_new(nullptr, pixbuf,
493 input_type, input_file,
494 screen_type, nullptr, nullptr, 0);
498 color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
503 exif_free_fd(fd, exif);
507 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
509 GdkPixbuf *pixbuf_thumb = nullptr;
511 GdkPixbuf *rotated = nullptr;
516 if (!tl->cache_hit && options->image.exif_rotate_enable)
518 if (!tl->fd->exif_orientation)
520 if (g_strcmp0(tl->fd->format_name, "heif") != 0)
522 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
526 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
530 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
532 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
537 sw = gdk_pixbuf_get_width(pixbuf);
538 sh = gdk_pixbuf_get_height(pixbuf);
540 if (tl->cache_enable)
547 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
549 cache_w = cache_h = THUMB_SIZE_LARGE;
553 cache_w = cache_h = THUMB_SIZE_NORMAL;
556 if (sw > cache_w || sh > cache_h || shrunk)
562 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
565 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
566 static_cast<GdkInterpType>(options->thumbnails.quality));
570 pixbuf_thumb = pixbuf;
571 g_object_ref(G_OBJECT(pixbuf_thumb));
574 /* do not save the thumbnail if the source file has changed meanwhile -
575 the thumbnail is most probably broken */
576 if (stat_utf8(tl->fd->path, &st) &&
577 tl->source_mtime == st.st_mtime &&
578 tl->source_size == st.st_size)
580 thumb_loader_std_save(tl, pixbuf_thumb);
584 else if (tl->cache_hit &&
585 tl->cache_local && !tl->thumb_path_local)
587 /* A local cache save was requested, but a valid thumb is in $HOME,
588 * so specifically save as a local thumbnail.
590 g_free(tl->thumb_path);
591 tl->thumb_path = nullptr;
593 tl->cache_hit = FALSE;
595 DEBUG_1("thumb copied: %s", tl->fd->path);
597 thumb_loader_std_save(tl, pixbuf);
601 if (sw <= tl->requested_width && sh <= tl->requested_height)
604 g_object_ref(result);
613 pixbuf = pixbuf_thumb;
614 sw = gdk_pixbuf_get_width(pixbuf);
615 sh = gdk_pixbuf_get_height(pixbuf);
618 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
621 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
622 static_cast<GdkInterpType>(options->thumbnails.quality));
627 g_object_ref(result);
631 // apply color correction, if required
632 thumb_loader_std_calibrate_pixbuf(tl->fd, result);
634 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
635 if (rotated) g_object_unref(rotated);
640 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
642 image_loader_free(tl->il);
647 if (!tl->thumb_path_local && remove_broken)
649 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
650 unlink_file(tl->thumb_path);
653 g_free(tl->thumb_path);
654 tl->thumb_path = nullptr;
656 if (!tl->thumb_path_local)
658 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, nullptr, FALSE);
659 if (isfile(tl->thumb_path))
661 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
662 if (thumb_loader_std_setup(tl, fd))
665 tl->thumb_path_local = TRUE;
671 g_free(tl->thumb_path);
672 tl->thumb_path = nullptr;
675 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
678 thumb_loader_std_save(tl, nullptr);
682 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
684 auto tl = static_cast<ThumbLoaderStd *>(data);
687 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
688 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
690 pixbuf = image_loader_get_pixbuf(tl->il);
693 DEBUG_1("...but no pixbuf");
694 thumb_loader_std_error_cb(il, data);
698 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
700 if (thumb_loader_std_next_source(tl, TRUE)) return;
702 if (tl->func_error) tl->func_error(tl, tl->data);
706 tl->cache_hit = (tl->thumb_path != nullptr);
710 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
711 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
714 if (tl->func_done) tl->func_done(tl, tl->data);
717 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
719 auto tl = static_cast<ThumbLoaderStd *>(data);
721 /* if at least some of the image is available, go to done */
722 if (image_loader_get_pixbuf(tl->il) != nullptr)
724 thumb_loader_std_done_cb(il, data);
728 DEBUG_1("thumb image error: %s", tl->fd->path);
729 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
731 if (thumb_loader_std_next_source(tl, TRUE)) return;
733 thumb_loader_std_set_fallback(tl);
735 if (tl->func_error) tl->func_error(tl, tl->data);
738 static void thumb_loader_std_progress_cb(ImageLoader *, gdouble percent, gpointer data)
740 auto tl = static_cast<ThumbLoaderStd *>(data);
742 tl->progress = percent;
744 if (tl->func_progress) tl->func_progress(tl, tl->data);
747 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
749 tl->il = image_loader_new(fd);
750 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
752 /* this will speed up jpegs by up to 3x in some cases */
753 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
754 tl->requested_height <= THUMB_SIZE_NORMAL)
756 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
760 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
763 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
764 if (tl->func_progress)
766 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
768 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
770 if (image_loader_start(tl->il))
775 image_loader_free(tl->il);
781 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
782 * a valid existing thumb is found anywhere the local thumb will not be created.
784 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
788 tl->cache_enable = enable_cache;
789 tl->cache_local = local;
790 tl->cache_retry = retry_failed;
793 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
795 static gchar *thumb_cache = nullptr;
798 if (!tl || !fd) return FALSE;
800 thumb_loader_std_reset(tl);
803 tl->fd = file_data_ref(fd);
804 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))
806 thumb_loader_std_set_fallback(tl);
809 tl->source_mtime = st.st_mtime;
810 tl->source_size = st.st_size;
811 tl->source_mode = st.st_mode;
815 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
818 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
822 pathl = path_from_utf8(fd->path);
823 tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
824 tl->local_uri = filename_from_path(tl->thumb_uri);
828 if (tl->cache_enable)
832 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, FALSE);
833 tl->thumb_path_local = FALSE;
835 found = isfile(tl->thumb_path);
838 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
839 if (thumb_loader_std_setup(tl, fd))
847 if (thumb_loader_std_fail_check(tl) ||
848 !thumb_loader_std_next_source(tl, found))
850 thumb_loader_std_set_fallback(tl);
856 if (!thumb_loader_std_setup(tl, tl->fd))
858 thumb_loader_std_save(tl, nullptr);
859 thumb_loader_std_set_fallback(tl);
866 void thumb_loader_std_free(ThumbLoaderStd *tl)
870 thumb_loader_std_reset(tl);
874 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
878 if (tl && tl->fd && tl->fd->thumb_pixbuf)
880 pixbuf = tl->fd->thumb_pixbuf;
881 g_object_ref(pixbuf);
885 pixbuf = pixbuf_fallback(nullptr, tl->requested_width, tl->requested_height);
898 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
901 guint idle_id; /* event source id */
904 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
906 thumb_loader_std_free(tv->tl);
911 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
917 tv = static_cast<ThumbValidate *>(tl->data);
921 g_source_remove(tv->idle_id);
925 thumb_loader_std_thumb_file_validate_free(tv);
928 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
930 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
932 thumb_loader_std_thumb_file_validate_free(tv);
935 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *, gpointer data)
937 auto tv = static_cast<ThumbValidate *>(data);
939 gboolean valid = FALSE;
941 /* get the original thumbnail pixbuf (unrotated, with original options)
942 this is called from image_loader done callback, so tv->tl->il must exist*/
943 pixbuf = image_loader_get_pixbuf(tv->tl->il);
947 const gchar *mtime_str;
949 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
950 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
951 if (uri && mtime_str)
953 if (strncmp(uri, "file:", strlen("file:")) == 0)
958 target = g_filename_from_uri(uri, nullptr, nullptr);
959 if (stat(target, &st) == 0 &&
960 st.st_mtime == strtol(mtime_str, nullptr, 10))
970 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
972 if (stat_utf8(tv->path, &st))
977 if (st.st_atime >= now - static_cast<time_t>(tv->days) * 24 * 60 * 60)
986 DEBUG_1("invalid image found in std cache: %s", tv->path);
990 thumb_loader_std_thumb_file_validate_finish(tv, valid);
993 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *, gpointer data)
995 auto tv = static_cast<ThumbValidate *>(data);
997 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
1000 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
1002 auto tv = static_cast<ThumbValidate *>(data);
1005 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
1007 return G_SOURCE_REMOVE;
1010 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
1011 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
1016 tv = g_new0(ThumbValidate, 1);
1018 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
1019 thumb_loader_std_set_callbacks(tv->tl,
1020 thumb_loader_std_thumb_file_validate_done_cb,
1021 thumb_loader_std_thumb_file_validate_error_cb,
1024 thumb_loader_std_reset(tv->tl);
1026 tv->path = g_strdup(thumb_path);
1027 tv->days = allowed_days;
1028 tv->func_valid = func_valid;
1031 FileData *fd = file_data_new_no_grouping(thumb_path);
1032 if (!thumb_loader_std_setup(tv->tl, fd))
1034 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
1041 file_data_unref(fd);
1045 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1046 const gchar *subfolder)
1050 thumb_path = thumb_std_cache_path(source,
1051 (local) ? filename_from_path(uri) : uri,
1053 if (isfile(thumb_path))
1055 DEBUG_1("thumb removing: %s", thumb_path);
1056 unlink_file(thumb_path);
1061 /* this also removes local thumbnails (the source is gone so it makes sense) */
1062 void thumb_std_maint_removed(const gchar *source)
1067 sourcel = path_from_utf8(source);
1068 uri = g_filename_to_uri(sourcel, nullptr, nullptr);
1071 /* all this to remove a thumbnail? */
1073 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1074 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1075 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1076 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1077 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1094 static GList *thumb_std_maint_move_list = nullptr;
1095 static GList *thumb_std_maint_move_tail = nullptr;
1098 static void thumb_std_maint_move_step(TMaintMove *tm);
1099 static gboolean thumb_std_maint_move_idle(gpointer data);
1102 static void thumb_std_maint_move_validate_cb(const gchar *, gboolean, gpointer data)
1104 auto tm = static_cast<TMaintMove *>(data);
1107 /* get the original thumbnail pixbuf (unrotated, with original options)
1108 this is called from image_loader done callback, so tm->tl->il must exist*/
1109 pixbuf = image_loader_get_pixbuf(tm->tl->il);
1113 const gchar *mtime_str;
1115 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1116 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1118 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1122 /* The validation utility abuses ThumbLoader, and we
1123 * abuse the utility just to load the thumbnail,
1124 * but the loader needs to look sane for the save to complete.
1127 tm->tl->cache_enable = TRUE;
1128 tm->tl->cache_hit = FALSE;
1129 tm->tl->cache_local = FALSE;
1130 file_data_unref(tm->tl->fd);
1131 tm->tl->fd = file_data_new_group(tm->dest);
1132 tm->tl->source_mtime = strtol(mtime_str, nullptr, 10);
1134 pathl = path_from_utf8(tm->tl->fd->path);
1135 g_free(tm->tl->thumb_uri);
1136 tm->tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1137 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1140 g_free(tm->tl->thumb_path);
1141 tm->tl->thumb_path = nullptr;
1142 tm->tl->thumb_path_local = FALSE;
1144 DEBUG_1("thumb move attempting save:");
1146 thumb_loader_std_save(tm->tl, pixbuf);
1149 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1150 unlink_file(tm->thumb_path);
1153 thumb_std_maint_move_step(tm);
1156 static void thumb_std_maint_move_step(TMaintMove *tm)
1158 const gchar *folder;
1165 g_free(tm->source_uri);
1166 g_free(tm->thumb_path);
1169 if (thumb_std_maint_move_list)
1171 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1177 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1179 g_free(tm->thumb_path);
1180 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1181 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1182 thumb_std_maint_move_validate_cb, tm);
1185 static gboolean thumb_std_maint_move_idle(gpointer)
1190 if (!thumb_std_maint_move_list) return G_SOURCE_REMOVE;
1192 tm = static_cast<TMaintMove *>(thumb_std_maint_move_list->data);
1194 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1195 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = nullptr;
1197 pathl = path_from_utf8(tm->source);
1198 tm->source_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1203 thumb_std_maint_move_step(tm);
1205 return G_SOURCE_REMOVE;
1208 /* This will schedule a move of the thumbnail for source image to dest when idle.
1209 * We do this so that file renaming or moving speed is not sacrificed by
1210 * moving the thumbnails at the same time because:
1212 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1214 * The thumbnails are processed when the app is idle. If the app
1215 * exits early well too bad - they can simply be regenerated from scratch.
1217 /** @FIXME This does not manage local thumbnails (fixme ?)
1219 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1223 tm = g_new0(TMaintMove, 1);
1224 tm->source = g_strdup(source);
1225 tm->dest = g_strdup(dest);
1227 if (!thumb_std_maint_move_list)
1229 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1232 if (thumb_std_maint_move_tail)
1234 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1235 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1239 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1240 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1243 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */