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.
57 THUMB_SIZE_NORMAL = 128,
58 THUMB_SIZE_LARGE = 256
61 #define THUMB_MARKER_URI "tEXt::Thumb::URI"
62 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime"
63 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size"
64 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width"
65 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height"
66 #define THUMB_MARKER_APP "tEXt::Software"
69 *-----------------------------------------------------------------------------
71 *-----------------------------------------------------------------------------
75 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
76 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
79 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
83 tl = g_new0(ThumbLoaderStd, 1);
85 tl->standard_loader = TRUE;
86 tl->requested_width = width;
87 tl->requested_height = height;
88 tl->cache_enable = options->thumbnails.enable_caching;
93 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl,
94 ThumbLoaderStd::Func func_done,
95 ThumbLoaderStd::Func func_error,
96 ThumbLoaderStd::Func func_progress,
101 tl->func_done = func_done;
102 tl->func_error = func_error;
103 tl->func_progress = func_progress;
107 static void thumb_loader_std_reset(ThumbLoaderStd *tl)
109 image_loader_free(tl->il);
112 file_data_unref(tl->fd);
115 g_free(tl->thumb_path);
116 tl->thumb_path = nullptr;
118 g_free(tl->thumb_uri);
119 tl->thumb_uri = nullptr;
120 tl->local_uri = nullptr;
122 tl->thumb_path_local = FALSE;
124 tl->cache_hit = FALSE;
126 tl->source_mtime = 0;
133 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
134 const gchar *cache_subfolder)
136 gchar *result = nullptr;
140 if (!path || !uri || !cache_subfolder) return nullptr;
142 md5_text = md5_get_string(reinterpret_cast<const guchar *>(uri), strlen(uri));
144 if (!md5_text) return nullptr;
146 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL);
150 gchar *base = remove_level_from_path(path);
152 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL);
157 result = g_build_filename(get_thumbnails_standard_cache_dir(),
158 cache_subfolder, name, NULL);
167 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
172 if (!tl->fd || !tl->thumb_uri) return nullptr;
176 w = gdk_pixbuf_get_width(pixbuf);
177 h = gdk_pixbuf_get_height(pixbuf);
181 w = tl->requested_width;
182 h = tl->requested_height;
187 folder = THUMB_FOLDER_FAIL;
189 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL)
191 folder = THUMB_FOLDER_LARGE;
195 folder = THUMB_FOLDER_NORMAL;
198 return thumb_std_cache_path(tl->fd->path,
199 (local) ? tl->local_uri : tl->thumb_uri,
203 static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
206 gboolean result = FALSE;
208 fail_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, TRUE);
209 if (isfile(fail_path))
221 pathl = path_from_utf8(fail_path);
222 pixbuf = gdk_pixbuf_new_from_file(pathl, nullptr);
228 const gchar *mtime_str;
230 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
231 if (mtime_str && strtol(mtime_str, nullptr, 10) == tl->source_mtime)
234 DEBUG_1("thumb fail valid: %s", tl->fd->path);
235 DEBUG_1(" thumb: %s", fail_path);
238 g_object_unref(G_OBJECT(pixbuf));
241 if (!result) unlink_file(fail_path);
248 static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
250 const gchar *valid_uri;
252 const gchar *mtime_str;
256 if (!pixbuf) return FALSE;
258 w = gdk_pixbuf_get_width(pixbuf);
259 h = gdk_pixbuf_get_height(pixbuf);
261 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE &&
262 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE;
264 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri;
266 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
267 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
269 if (!mtime_str || !uri || !valid_uri) return FALSE;
270 if (strcmp(uri, valid_uri) != 0) return FALSE;
272 mtime = strtol(mtime_str, nullptr, 10);
273 if (tl->source_mtime != mtime) return FALSE;
278 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
284 if (!tl->cache_enable || tl->cache_hit) return;
285 if (tl->thumb_path) return;
289 /* local failures are not stored */
290 if (tl->cache_local) return;
292 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
297 g_object_ref(G_OBJECT(pixbuf));
301 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
304 g_object_unref(G_OBJECT(pixbuf));
307 tl->thumb_path_local = tl->cache_local;
309 /* create thumbnail dir if needed */
310 base_path = remove_level_from_path(tl->thumb_path);
313 if (!isdir(base_path))
318 source_base = remove_level_from_path(tl->fd->path);
319 if (stat_utf8(source_base, &st))
321 recursive_mkdir_if_not_exists(base_path, st.st_mode);
328 recursive_mkdir_if_not_exists(base_path, S_IRWXU);
332 DEBUG_1("thumb saving: %s", tl->fd->path);
333 DEBUG_1(" saved: %s", tl->thumb_path);
335 /* save thumb, using a temp file then renaming into place */
336 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
339 const gchar *mark_uri;
345 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
347 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
348 mark_mtime = g_strdup_printf("%llu", static_cast<unsigned long long>(tl->source_mtime));
349 pathl = path_from_utf8(tmp_path);
350 success = gdk_pixbuf_save(pixbuf, pathl, "png", nullptr,
351 THUMB_MARKER_URI, mark_uri,
352 THUMB_MARKER_MTIME, mark_mtime,
353 THUMB_MARKER_APP, mark_app,
357 chmod(pathl, (tl->cache_local) ? tl->source_mode : S_IRUSR & S_IWUSR);
358 success = rename_file(tmp_path, tl->thumb_path);
369 DEBUG_1("thumb save failed: %s", tl->fd->path);
370 DEBUG_1(" thumb: %s", tl->thumb_path);
375 g_object_unref(G_OBJECT(pixbuf));
378 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
380 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
381 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
385 void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
386 ColorMan *cm = nullptr;
387 ExifData *exif = nullptr;
388 gint color_profile_from_image = COLOR_PROFILE_NONE;
389 ColorManProfileType input_type = COLOR_PROFILE_MEM;
390 ColorManProfileType screen_type;
391 const gchar *input_file = nullptr;
392 guchar *profile = nullptr;
396 if (!options->thumbnails.use_color_management)
401 sw = gdk_pixbuf_get_width(pixbuf);
402 sh = gdk_pixbuf_get_height(pixbuf);
404 exif = exif_read_fd(fd);
408 if (g_strcmp0(fd->format_name, "heif") == 0)
410 profile = heif_color_profile(fd, &profile_len);
415 profile = exif_get_color_profile(exif, &profile_len);
420 DEBUG_1("Found embedded color profile");
421 color_profile_from_image = COLOR_PROFILE_MEM;
425 gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
429 /* Exif 2.21 specification */
430 if (!strcmp(interop_index, "R98"))
432 color_profile_from_image = COLOR_PROFILE_SRGB;
433 DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
435 else if (!strcmp(interop_index, "R03"))
437 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
438 DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
440 g_free(interop_index);
446 /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
447 if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
450 color_profile_from_image = COLOR_PROFILE_SRGB;
451 DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
455 /* non-standard way of specifying AdobeRGB (used by some software) */
456 color_profile_from_image = COLOR_PROFILE_ADOBERGB;
457 DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
462 if(color_profile_from_image != COLOR_PROFILE_NONE)
464 // transform image, we always use sRGB as target for thumbnails
465 screen_type = COLOR_PROFILE_SRGB;
469 cm = color_man_new_embedded(nullptr, pixbuf,
470 profile, profile_len,
471 screen_type, nullptr, nullptr, 0);
476 cm = color_man_new(nullptr, pixbuf,
477 input_type, input_file,
478 screen_type, nullptr, nullptr, 0);
482 color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
487 exif_free_fd(fd, exif);
491 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
493 GdkPixbuf *pixbuf_thumb = nullptr;
495 GdkPixbuf *rotated = nullptr;
499 if (!tl->cache_hit && options->image.exif_rotate_enable)
501 if (!tl->fd->exif_orientation)
503 if (g_strcmp0(tl->fd->format_name, "heif") != 0)
505 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
509 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
513 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
515 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
520 sw = gdk_pixbuf_get_width(pixbuf);
521 sh = gdk_pixbuf_get_height(pixbuf);
523 if (tl->cache_enable)
527 gint cache_w, cache_h;
529 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL)
531 cache_w = cache_h = THUMB_SIZE_LARGE;
535 cache_w = cache_h = THUMB_SIZE_NORMAL;
538 if (sw > cache_w || sh > cache_h || shrunk)
540 gint thumb_w, thumb_h;
543 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
546 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
547 static_cast<GdkInterpType>(options->thumbnails.quality));
551 pixbuf_thumb = pixbuf;
552 g_object_ref(G_OBJECT(pixbuf_thumb));
555 /* do not save the thumbnail if the source file has changed meanwhile -
556 the thumbnail is most probably broken */
557 if (stat_utf8(tl->fd->path, &st) &&
558 tl->source_mtime == st.st_mtime &&
559 tl->source_size == st.st_size)
561 thumb_loader_std_save(tl, pixbuf_thumb);
565 else if (tl->cache_hit &&
566 tl->cache_local && !tl->thumb_path_local)
568 /* A local cache save was requested, but a valid thumb is in $HOME,
569 * so specifically save as a local thumbnail.
571 g_free(tl->thumb_path);
572 tl->thumb_path = nullptr;
574 tl->cache_hit = FALSE;
576 DEBUG_1("thumb copied: %s", tl->fd->path);
578 thumb_loader_std_save(tl, pixbuf);
582 if (sw <= tl->requested_width && sh <= tl->requested_height)
585 g_object_ref(result);
589 gint thumb_w, thumb_h;
593 pixbuf = pixbuf_thumb;
594 sw = gdk_pixbuf_get_width(pixbuf);
595 sh = gdk_pixbuf_get_height(pixbuf);
598 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh,
601 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h,
602 static_cast<GdkInterpType>(options->thumbnails.quality));
607 g_object_ref(result);
611 // apply color correction, if required
612 thumb_loader_std_calibrate_pixbuf(tl->fd, result);
614 if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
615 if (rotated) g_object_unref(rotated);
620 static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
622 image_loader_free(tl->il);
627 if (!tl->thumb_path_local && remove_broken)
629 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path);
630 unlink_file(tl->thumb_path);
633 g_free(tl->thumb_path);
634 tl->thumb_path = nullptr;
636 if (!tl->thumb_path_local)
638 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, nullptr, FALSE);
639 if (isfile(tl->thumb_path))
641 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
642 if (thumb_loader_std_setup(tl, fd))
645 tl->thumb_path_local = TRUE;
651 g_free(tl->thumb_path);
652 tl->thumb_path = nullptr;
655 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
658 thumb_loader_std_save(tl, nullptr);
662 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
664 auto tl = static_cast<ThumbLoaderStd *>(data);
667 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
668 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
670 pixbuf = image_loader_get_pixbuf(tl->il);
673 DEBUG_1("...but no pixbuf");
674 thumb_loader_std_error_cb(il, data);
678 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf))
680 if (thumb_loader_std_next_source(tl, TRUE)) return;
682 if (tl->func_error) tl->func_error(tl, tl->data);
686 tl->cache_hit = (tl->thumb_path != nullptr);
690 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
691 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
694 if (tl->func_done) tl->func_done(tl, tl->data);
697 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
699 auto tl = static_cast<ThumbLoaderStd *>(data);
701 /* if at least some of the image is available, go to done */
702 if (image_loader_get_pixbuf(tl->il) != nullptr)
704 thumb_loader_std_done_cb(il, data);
708 DEBUG_1("thumb image error: %s", tl->fd->path);
709 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path);
711 if (thumb_loader_std_next_source(tl, TRUE)) return;
713 thumb_loader_std_set_fallback(tl);
715 if (tl->func_error) tl->func_error(tl, tl->data);
718 static void thumb_loader_std_progress_cb(ImageLoader *, gdouble percent, gpointer data)
720 auto tl = static_cast<ThumbLoaderStd *>(data);
722 tl->progress = percent;
724 if (tl->func_progress) tl->func_progress(tl, tl->data);
727 static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
729 tl->il = image_loader_new(fd);
730 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
732 /* this will speed up jpegs by up to 3x in some cases */
733 if (tl->requested_width <= THUMB_SIZE_NORMAL &&
734 tl->requested_height <= THUMB_SIZE_NORMAL)
736 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
740 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
743 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
744 if (tl->func_progress)
746 g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
748 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
750 if (image_loader_start(tl->il))
755 image_loader_free(tl->il);
761 * Note: Currently local_cache only specifies where to save a _new_ thumb, if
762 * a valid existing thumb is found anywhere the local thumb will not be created.
764 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
768 tl->cache_enable = enable_cache;
769 tl->cache_local = local;
770 tl->cache_retry = retry_failed;
773 gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
775 static gchar *thumb_cache = nullptr;
778 if (!tl || !fd) return FALSE;
780 thumb_loader_std_reset(tl);
783 tl->fd = file_data_ref(fd);
784 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))
786 thumb_loader_std_set_fallback(tl);
789 tl->source_mtime = st.st_mtime;
790 tl->source_size = st.st_size;
791 tl->source_mode = st.st_mode;
795 thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
798 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
802 pathl = path_from_utf8(fd->path);
803 tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
804 tl->local_uri = filename_from_path(tl->thumb_uri);
808 if (tl->cache_enable)
812 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, nullptr, FALSE);
813 tl->thumb_path_local = FALSE;
815 found = isfile(tl->thumb_path);
818 FileData *fd = file_data_new_no_grouping(tl->thumb_path);
819 if (thumb_loader_std_setup(tl, fd))
827 if (thumb_loader_std_fail_check(tl) ||
828 !thumb_loader_std_next_source(tl, found))
830 thumb_loader_std_set_fallback(tl);
836 if (!thumb_loader_std_setup(tl, tl->fd))
838 thumb_loader_std_save(tl, nullptr);
839 thumb_loader_std_set_fallback(tl);
846 void thumb_loader_std_free(ThumbLoaderStd *tl)
850 thumb_loader_std_reset(tl);
854 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl)
858 if (tl && tl->fd && tl->fd->thumb_pixbuf)
860 pixbuf = tl->fd->thumb_pixbuf;
861 g_object_ref(pixbuf);
865 pixbuf = pixbuf_fallback(nullptr, tl->requested_width, tl->requested_height);
878 void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
881 guint idle_id; /* event source id */
884 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
886 thumb_loader_std_free(tv->tl);
891 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
897 tv = static_cast<ThumbValidate *>(tl->data);
901 g_source_remove(tv->idle_id);
905 thumb_loader_std_thumb_file_validate_free(tv);
908 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
910 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
912 thumb_loader_std_thumb_file_validate_free(tv);
915 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *, gpointer data)
917 auto tv = static_cast<ThumbValidate *>(data);
919 gboolean valid = FALSE;
921 /* get the original thumbnail pixbuf (unrotated, with original options)
922 this is called from image_loader done callback, so tv->tl->il must exist*/
923 pixbuf = image_loader_get_pixbuf(tv->tl->il);
927 const gchar *mtime_str;
929 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
930 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
931 if (uri && mtime_str)
933 if (strncmp(uri, "file:", strlen("file:")) == 0)
938 target = g_filename_from_uri(uri, nullptr, nullptr);
939 if (stat(target, &st) == 0 &&
940 st.st_mtime == strtol(mtime_str, nullptr, 10))
950 DEBUG_1("thumb uri foreign, doing day check: %s", uri);
952 if (stat_utf8(tv->path, &st))
957 if (st.st_atime >= now - static_cast<time_t>(tv->days) * 24 * 60 * 60)
966 DEBUG_1("invalid image found in std cache: %s", tv->path);
970 thumb_loader_std_thumb_file_validate_finish(tv, valid);
973 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *, gpointer data)
975 auto tv = static_cast<ThumbValidate *>(data);
977 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
980 static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
982 auto tv = static_cast<ThumbValidate *>(data);
985 thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
987 return G_SOURCE_REMOVE;
990 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
991 void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
996 tv = g_new0(ThumbValidate, 1);
998 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
999 thumb_loader_std_set_callbacks(tv->tl,
1000 thumb_loader_std_thumb_file_validate_done_cb,
1001 thumb_loader_std_thumb_file_validate_error_cb,
1004 thumb_loader_std_reset(tv->tl);
1006 tv->path = g_strdup(thumb_path);
1007 tv->days = allowed_days;
1008 tv->func_valid = func_valid;
1011 FileData *fd = file_data_new_no_grouping(thumb_path);
1012 if (!thumb_loader_std_setup(tv->tl, fd))
1014 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
1021 file_data_unref(fd);
1025 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
1026 const gchar *subfolder)
1030 thumb_path = thumb_std_cache_path(source,
1031 (local) ? filename_from_path(uri) : uri,
1033 if (isfile(thumb_path))
1035 DEBUG_1("thumb removing: %s", thumb_path);
1036 unlink_file(thumb_path);
1041 /* this also removes local thumbnails (the source is gone so it makes sense) */
1042 void thumb_std_maint_removed(const gchar *source)
1047 sourcel = path_from_utf8(source);
1048 uri = g_filename_to_uri(sourcel, nullptr, nullptr);
1051 /* all this to remove a thumbnail? */
1053 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL);
1054 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE);
1055 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL);
1056 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL);
1057 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE);
1074 static GList *thumb_std_maint_move_list = nullptr;
1075 static GList *thumb_std_maint_move_tail = nullptr;
1078 static void thumb_std_maint_move_step(TMaintMove *tm);
1079 static gboolean thumb_std_maint_move_idle(gpointer data);
1082 static void thumb_std_maint_move_validate_cb(const gchar *, gboolean, gpointer data)
1084 auto tm = static_cast<TMaintMove *>(data);
1087 /* get the original thumbnail pixbuf (unrotated, with original options)
1088 this is called from image_loader done callback, so tm->tl->il must exist*/
1089 pixbuf = image_loader_get_pixbuf(tm->tl->il);
1093 const gchar *mtime_str;
1095 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI);
1096 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME);
1098 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0)
1102 /* The validation utility abuses ThumbLoader, and we
1103 * abuse the utility just to load the thumbnail,
1104 * but the loader needs to look sane for the save to complete.
1107 tm->tl->cache_enable = TRUE;
1108 tm->tl->cache_hit = FALSE;
1109 tm->tl->cache_local = FALSE;
1110 file_data_unref(tm->tl->fd);
1111 tm->tl->fd = file_data_new_group(tm->dest);
1112 tm->tl->source_mtime = strtol(mtime_str, nullptr, 10);
1114 pathl = path_from_utf8(tm->tl->fd->path);
1115 g_free(tm->tl->thumb_uri);
1116 tm->tl->thumb_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1117 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri);
1120 g_free(tm->tl->thumb_path);
1121 tm->tl->thumb_path = nullptr;
1122 tm->tl->thumb_path_local = FALSE;
1124 DEBUG_1("thumb move attempting save:");
1126 thumb_loader_std_save(tm->tl, pixbuf);
1129 DEBUG_1("thumb move unlink: %s", tm->thumb_path);
1130 unlink_file(tm->thumb_path);
1133 thumb_std_maint_move_step(tm);
1136 static void thumb_std_maint_move_step(TMaintMove *tm)
1138 const gchar *folder;
1145 g_free(tm->source_uri);
1146 g_free(tm->thumb_path);
1149 if (thumb_std_maint_move_list)
1151 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1157 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE;
1159 g_free(tm->thumb_path);
1160 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder);
1161 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0,
1162 thumb_std_maint_move_validate_cb, tm);
1165 static gboolean thumb_std_maint_move_idle(gpointer)
1170 if (!thumb_std_maint_move_list) return G_SOURCE_REMOVE;
1172 tm = static_cast<TMaintMove *>(thumb_std_maint_move_list->data);
1174 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm);
1175 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = nullptr;
1177 pathl = path_from_utf8(tm->source);
1178 tm->source_uri = g_filename_to_uri(pathl, nullptr, nullptr);
1183 thumb_std_maint_move_step(tm);
1185 return G_SOURCE_REMOVE;
1188 /* This will schedule a move of the thumbnail for source image to dest when idle.
1189 * We do this so that file renaming or moving speed is not sacrificed by
1190 * moving the thumbnails at the same time because:
1192 * This cache design requires the tedious task of loading the png thumbnails and saving them.
1194 * The thumbnails are processed when the app is idle. If the app
1195 * exits early well too bad - they can simply be regenerated from scratch.
1197 /** @FIXME This does not manage local thumbnails (fixme ?)
1199 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
1203 tm = g_new0(TMaintMove, 1);
1204 tm->source = g_strdup(source);
1205 tm->dest = g_strdup(dest);
1207 if (!thumb_std_maint_move_list)
1209 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, nullptr, nullptr);
1212 if (thumb_std_maint_move_tail)
1214 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm);
1215 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next;
1219 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm);
1220 thumb_std_maint_move_tail = thumb_std_maint_move_list;
1223 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */