Include a Other Software section in Help file
[geeqie.git] / src / thumb_standard.c
index f65eb63..3ac2b4b 100644 (file)
@@ -1,42 +1,54 @@
 /*
- * Geeqie
- * (C) 2006 John Ellis
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2006 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: John Ellis
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-
 #include "main.h"
 #include "thumb_standard.h"
 
-#include "cache.h"     /* for cache_ensure_dir_exists */
+#include "cache.h"
 #include "image-load.h"
 #include "md5-util.h"
 #include "pixbuf_util.h"
 #include "ui_fileops.h"
 #include "filedata.h"
 #include "exif.h"
+#include "metadata.h"
+#include "color-man.h"
 
 
-/*
+/**
+ * @file
+ * 
  * This thumbnail caching implementation attempts to conform
  * to the Thumbnail Managing Standard proposed on freedesktop.org
- * The standard is documented here:
- *   http://triq.net/~jens/thumbnail-spec/index.html
+ * The standard is documented here: \n
+ *   http://triq.net/~jens/thumbnail-spec/index.html \n
  *  (why isn't it actually hosted on freedesktop.org?)
  *
  * This code attempts to conform to version 0.7.0 of the standard.
  *
  * Notes:
  *   > Validation of the thumb's embedded uri is a simple strcmp between our
- *     version of the escaped uri and the thumb's escaped uri. But not all uri
- *     escape functions escape the same set of chars, comparing the unescaped
- *     versions may be more accurate.
+ *   > version of the escaped uri and the thumb's escaped uri. But not all uri
+ *   > escape functions escape the same set of chars, comparing the unescaped
+ *   > versions may be more accurate. \n
  *   > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time.
  *     Storing the Size, Width, Height should probably be implemented.
  */
@@ -65,7 +77,7 @@
 
 
 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
-static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path);
+static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
 
 
 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
@@ -75,16 +87,9 @@ ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
        tl = g_new0(ThumbLoaderStd, 1);
 
        tl->standard_loader = TRUE;
-
        tl->requested_width = width;
        tl->requested_height = height;
-
-       tl->il = NULL;
-       tl->fd = NULL;
-
        tl->cache_enable = options->thumbnails.enable_caching;
-       tl->cache_local = FALSE;
-       tl->cache_retry = FALSE;
 
        return tl;
 }
@@ -129,7 +134,7 @@ static void thumb_loader_std_reset(ThumbLoaderStd *tl)
        tl->progress = 0.0;
 }
 
-static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint local,
+static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gboolean local,
                                   const gchar *cache_subfolder)
 {
        gchar *result = NULL;
@@ -155,7 +160,8 @@ static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint loc
                }
        else
                {
-               result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL);
+               result = g_build_filename(get_thumbnails_standard_cache_dir(),
+                                                                                                       cache_subfolder, name, NULL);
                }
 
        g_free(name);
@@ -164,7 +170,7 @@ static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint loc
        return result;
 }
 
-static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPixbuf *pixbuf, gint fail)
+static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gboolean local, GdkPixbuf *pixbuf, gboolean fail)
 {
        const gchar *folder;
        gint w, h;
@@ -200,10 +206,10 @@ static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPix
                                    local, folder);
 }
 
-static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl)
+static gboolean thumb_loader_std_fail_check(ThumbLoaderStd *tl)
 {
        gchar *fail_path;
-       gint result = FALSE;
+       gboolean result = FALSE;
 
        fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE);
        if (isfile(fail_path))
@@ -245,7 +251,7 @@ static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl)
        return result;
 }
 
-static gint thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
+static gboolean thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
 {
        const gchar *valid_uri;
        const gchar *uri;
@@ -279,7 +285,7 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
 {
        gchar *base_path;
        gchar *tmp_path;
-       gint fail;
+       gboolean fail;
 
        if (!tl->cache_enable || tl->cache_hit) return;
        if (tl->thumb_path) return;
@@ -318,14 +324,14 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
                        source_base = remove_level_from_path(tl->fd->path);
                        if (stat_utf8(source_base, &st))
                                {
-                               cache_ensure_dir_exists(base_path, st.st_mode);
+                               recursive_mkdir_if_not_exists(base_path, st.st_mode);
                                }
                        g_free(source_base);
                        }
                }
        else
                {
-               cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER);
+               recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
                }
        g_free(base_path);
 
@@ -340,13 +346,12 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
                gchar *mark_app;
                gchar *mark_mtime;
                gchar *pathl;
-               gint success;
+               gboolean success;
 
                mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;
 
                mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION);
-               mark_mtime = g_strdup_printf("%lu", tl->source_mtime);
-
+               mark_mtime = g_strdup_printf("%llu", (unsigned long long)tl->source_mtime);
                pathl = path_from_utf8(tmp_path);
                success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
                                          THUMB_MARKER_URI, mark_uri,
@@ -376,7 +381,111 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
        g_object_unref(G_OBJECT(pixbuf));
 }
 
-static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk)
+static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl)
+{
+       if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
+       tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height);
+}
+
+
+void thumb_loader_std_calibrate_pixbuf(FileData *fd, GdkPixbuf *pixbuf) {
+       ColorMan *cm = NULL;
+       ExifData *exif = NULL;
+       gint color_profile_from_image = COLOR_PROFILE_NONE;
+       ColorManProfileType input_type = COLOR_PROFILE_MEM;
+       ColorManProfileType screen_type;
+       const gchar *input_file = NULL;
+       guchar *profile = NULL;
+       guint profile_len;
+       gint sw, sh;
+
+       if (!options->thumbnails.use_color_management)
+               {
+               return;
+               }
+
+       sw = gdk_pixbuf_get_width(pixbuf);
+       sh = gdk_pixbuf_get_height(pixbuf);
+
+       exif = exif_read_fd(fd);
+
+       if (exif)
+               {
+               profile = exif_get_color_profile(exif, &profile_len);
+               if (profile)
+                       {
+                       DEBUG_1("Found embedded color profile");
+                       color_profile_from_image = COLOR_PROFILE_MEM;
+                       }
+               else
+                       {
+                       gchar *interop_index = exif_get_data_as_text(exif, "Exif.Iop.InteroperabilityIndex");
+
+                       if (interop_index)
+                               {
+                               /* Exif 2.21 specification */
+                               if (!strcmp(interop_index, "R98"))
+                                       {
+                                       color_profile_from_image = COLOR_PROFILE_SRGB;
+                                       DEBUG_1("Found EXIF 2.21 ColorSpace of sRGB");
+                                       }
+                               else if (!strcmp(interop_index, "R03"))
+                                       {
+                                       color_profile_from_image = COLOR_PROFILE_ADOBERGB;
+                                       DEBUG_1("Found EXIF 2.21 ColorSpace of AdobeRGB");
+                                       }
+                               g_free(interop_index);
+                               }
+                       else
+                               {
+                               gint cs;
+
+                               /* ColorSpace == 1 specifies sRGB per EXIF 2.2 */
+                               if (!exif_get_integer(exif, "Exif.Photo.ColorSpace", &cs)) cs = 0;
+                               if (cs == 1)
+                                       {
+                                       color_profile_from_image = COLOR_PROFILE_SRGB;
+                                       DEBUG_1("Found EXIF 2.2 ColorSpace of sRGB");
+                                       }
+                               else if (cs == 2)
+                                       {
+                                       /* non-standard way of specifying AdobeRGB (used by some software) */
+                                       color_profile_from_image = COLOR_PROFILE_ADOBERGB;
+                                       DEBUG_1("Found EXIF 2.2 ColorSpace of AdobeRGB");
+                                       }
+                               }
+                       }
+
+               if(color_profile_from_image != COLOR_PROFILE_NONE)
+                       {
+                               // transform image, we always use sRGB as target for thumbnails
+                               screen_type = COLOR_PROFILE_SRGB;
+
+                               if (profile)
+                                       {
+                                       cm = color_man_new_embedded(NULL, pixbuf,
+                                                                       profile, profile_len,
+                                                                       screen_type, NULL, NULL, 0);
+                                       g_free(profile);
+                                       }
+                               else
+                                       {
+                                       cm = color_man_new(NULL, pixbuf,
+                                                       input_type, input_file,
+                                                       screen_type, NULL, NULL, 0);
+                                       }
+
+                               if(cm) {
+                                       color_man_correct_region(cm, cm->pixbuf, 0, 0, sw, sh);
+                                       g_free(cm);
+                               }
+
+                       }
+               exif_free_fd(fd, exif);
+               }
+}
+
+static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gboolean shrunk)
 {
        GdkPixbuf *pixbuf_thumb = NULL;
        GdkPixbuf *result;
@@ -388,16 +497,9 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                {
                if (!tl->fd->exif_orientation)
                        {
-                       ExifData *exif = exif_read_fd(tl->fd);
-                       gint orientation;
-
-                       if (exif && exif_get_integer(exif, "Exif.Image.Orientation", &orientation))
-                               tl->fd->exif_orientation = orientation;
-                       else
-                               tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
-                       exif_free_fd(tl->fd, exif);
+                       tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
                        }
-               
+
                if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
                        {
                        rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
@@ -426,6 +528,7 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                        if (sw > cache_w || sh > cache_h || shrunk)
                                {
                                gint thumb_w, thumb_h;
+                               struct stat st;
 
                                if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh,
                                                                  &thumb_w, &thumb_h))
@@ -439,7 +542,14 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                                        g_object_ref(G_OBJECT(pixbuf_thumb));
                                        }
 
-                               thumb_loader_std_save(tl, pixbuf_thumb);
+                               /* do not save the thumbnail if the source file has changed meanwhile -
+                                  the thumbnail is most probably broken */
+                               if (stat_utf8(tl->fd->path, &st) &&
+                                   tl->source_mtime == st.st_mtime &&
+                                   tl->source_size == st.st_size)
+                                       {
+                                       thumb_loader_std_save(tl, pixbuf_thumb);
+                                       }
                                }
                        }
                else if (tl->cache_hit &&
@@ -488,13 +598,16 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                        }
                }
 
+       // apply color correction, if required
+       thumb_loader_std_calibrate_pixbuf(tl->fd, result);
+
        if (pixbuf_thumb) g_object_unref(pixbuf_thumb);
        if (rotated) g_object_unref(rotated);
 
        return result;
 }
 
-static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken)
+static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove_broken)
 {
        image_loader_free(tl->il);
        tl->il = NULL;
@@ -513,17 +626,23 @@ static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken)
                if (!tl->thumb_path_local)
                        {
                        tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE);
-                       if (isfile(tl->thumb_path) && thumb_loader_std_setup(tl, tl->thumb_path))
+                       if (isfile(tl->thumb_path))
                                {
-                               tl->thumb_path_local = TRUE;
-                               return TRUE;
+                               FileData *fd = file_data_new_no_grouping(tl->thumb_path);
+                               if (thumb_loader_std_setup(tl, fd))
+                                       {
+                                       file_data_unref(fd);
+                                       tl->thumb_path_local = TRUE;
+                                       return TRUE;
+                                       }
+                               file_data_unref(fd);
                                }
 
                        g_free(tl->thumb_path);
                        tl->thumb_path = NULL;
                        }
 
-               if (thumb_loader_std_setup(tl, tl->fd->path)) return TRUE;
+               if (thumb_loader_std_setup(tl, tl->fd)) return TRUE;
                }
 
        thumb_loader_std_save(tl, NULL);
@@ -535,8 +654,8 @@ static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
        ThumbLoaderStd *tl = data;
        GdkPixbuf *pixbuf;
 
-       DEBUG_1("thumb image done: %s", tl->fd->path);
-       DEBUG_1("            from: %s", tl->il->path);
+       DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???");
+       DEBUG_1("            from: %s", image_loader_get_fd(tl->il)->path);
 
        pixbuf = image_loader_get_pixbuf(tl->il);
        if (!pixbuf)
@@ -559,7 +678,7 @@ static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data)
        if (tl->fd)
                {
                if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
-               tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, il->shrunk);
+               tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il));
                }
 
        if (tl->func_done) tl->func_done(tl, tl->data);
@@ -575,12 +694,14 @@ static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data)
                thumb_loader_std_done_cb(il, data);
                return;
                }
-       
+
        DEBUG_1("thumb image error: %s", tl->fd->path);
-       DEBUG_1("             from: %s", tl->il->fd->path);
+       DEBUG_1("             from: %s", image_loader_get_fd(tl->il)->path);
 
        if (thumb_loader_std_next_source(tl, TRUE)) return;
 
+       thumb_loader_std_set_fallback(tl);
+
        if (tl->func_error) tl->func_error(tl, tl->data);
 }
 
@@ -593,31 +714,30 @@ static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpoin
        if (tl->func_progress) tl->func_progress(tl, tl->data);
 }
 
-static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path)
+static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
 {
-       tl->il = image_loader_new_from_path(path);
+       tl->il = image_loader_new(fd);
+       image_loader_set_priority(tl->il, G_PRIORITY_LOW);
 
-       if (options->thumbnails.fast)
+       /* this will speed up jpegs by up to 3x in some cases */
+       if (tl->requested_width <= THUMB_SIZE_NORMAL &&
+           tl->requested_height <= THUMB_SIZE_NORMAL)
                {
-               /* this will speed up jpegs by up to 3x in some cases */
-               if (tl->requested_width <= THUMB_SIZE_NORMAL &&
-                   tl->requested_height <= THUMB_SIZE_NORMAL)
-                       {
-                       image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
-                       }
-               else
-                       {
-                       image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
-                       }
+               image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL);
+               }
+       else
+               {
+               image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE);
                }
 
-       image_loader_set_error_func(tl->il, thumb_loader_std_error_cb, tl);
+       g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl);
        if (tl->func_progress)
                {
-               image_loader_set_percent_func(tl->il, thumb_loader_std_progress_cb, tl);
+               g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl);
                }
+       g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl);
 
-       if (image_loader_start(tl->il, thumb_loader_std_done_cb, tl))
+       if (image_loader_start(tl->il))
                {
                return TRUE;
                }
@@ -631,7 +751,7 @@ static gint thumb_loader_std_setup(ThumbLoaderStd *tl, const gchar *path)
  * Note: Currently local_cache only specifies where to save a _new_ thumb, if
  *       a valid existing thumb is found anywhere the local thumb will not be created.
  */
-void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint local, gint retry_failed)
+void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
 {
        if (!tl) return;
 
@@ -640,7 +760,7 @@ void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint loca
        tl->cache_retry = retry_failed;
 }
 
-gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
+gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
 {
        static gchar *thumb_cache = NULL;
        struct stat st;
@@ -648,19 +768,23 @@ gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
        if (!tl || !fd) return FALSE;
 
        thumb_loader_std_reset(tl);
-       
-       if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf);
-       fd->thumb_pixbuf = pixbuf_fallback(fd, tl->requested_width, tl->requested_height);
-
 
-       if (!stat_utf8(fd->path, &st)) return FALSE;
 
        tl->fd = file_data_ref(fd);
+       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))
+               {
+               thumb_loader_std_set_fallback(tl);
+               return FALSE;
+               }
        tl->source_mtime = st.st_mtime;
        tl->source_size = st.st_size;
        tl->source_mode = st.st_mode;
 
-       if (!thumb_cache) thumb_cache = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, NULL);
+       if (!thumb_cache)
+               {
+               thumb_cache = g_strdup(get_thumbnails_standard_cache_dir());
+               }
+
        if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0)
                {
                gchar *pathl;
@@ -679,16 +803,30 @@ gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
                tl->thumb_path_local = FALSE;
 
                found = isfile(tl->thumb_path);
-               if (found && thumb_loader_std_setup(tl, tl->thumb_path)) return TRUE;
-
-               if (thumb_loader_std_fail_check(tl)) return FALSE;
+               if (found)
+                       {
+                       FileData *fd = file_data_new_no_grouping(tl->thumb_path);
+                       if (thumb_loader_std_setup(tl, fd))
+                               {
+                               file_data_unref(fd);
+                               return TRUE;
+                               }
+                       file_data_unref(fd);
+                       }
 
-               return thumb_loader_std_next_source(tl, found);
+               if (thumb_loader_std_fail_check(tl) ||
+                   !thumb_loader_std_next_source(tl, found))
+                       {
+                       thumb_loader_std_set_fallback(tl);
+                       return FALSE;
+                       }
+               return TRUE;
                }
 
-       if (!thumb_loader_std_setup(tl, tl->fd->path))
+       if (!thumb_loader_std_setup(tl, tl->fd))
                {
                thumb_loader_std_save(tl, NULL);
+               thumb_loader_std_set_fallback(tl);
                return FALSE;
                }
 
@@ -728,10 +866,10 @@ struct _ThumbValidate
        gchar *path;
        gint days;
 
-       void (*func_valid)(const gchar *path, gint valid, gpointer data);
+       void (*func_valid)(const gchar *path, gboolean valid, gpointer data);
        gpointer data;
 
-       gint idle_id;
+       guint idle_id; /* event source id */
 };
 
 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv)
@@ -749,13 +887,16 @@ void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl)
 
        tv = tl->data;
 
-       if (tv->idle_id != -1) g_source_remove(tv->idle_id);
-       tv->idle_id = -1;
+       if (tv->idle_id)
+               {
+               g_source_remove(tv->idle_id);
+               tv->idle_id = 0;
+               }
 
        thumb_loader_std_thumb_file_validate_free(tv);
 }
 
-static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gint valid)
+static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gboolean valid)
 {
        if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data);
 
@@ -766,10 +907,11 @@ static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpo
 {
        ThumbValidate *tv = data;
        GdkPixbuf *pixbuf;
-       gint valid = FALSE;
+       gboolean valid = FALSE;
 
-       /* this function is called on success, so the pixbuf should not be a fallback*/
-       pixbuf = thumb_loader_std_get_pixbuf(tv->tl);
+       /* get the original thumbnail pixbuf (unrotated, with original options)
+          this is called from image_loader done callback, so tv->tl->il must exist*/
+       pixbuf = image_loader_get_pixbuf(tv->tl->il);
        if (pixbuf)
                {
                const gchar *uri;
@@ -810,8 +952,10 @@ static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpo
                                        }
                                }
                        }
-
-               g_object_unref(pixbuf);
+               else
+                       {
+                       DEBUG_1("invalid image found in std cache: %s", tv->path);
+                       }
                }
 
        thumb_loader_std_thumb_file_validate_finish(tv, valid);
@@ -824,18 +968,18 @@ static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gp
        thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
 }
 
-static gint thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
+static gboolean thumb_loader_std_thumb_file_validate_idle_cb(gpointer data)
 {
        ThumbValidate *tv = data;
 
-       tv->idle_id = -1;
+       tv->idle_id = 0;
        thumb_loader_std_thumb_file_validate_finish(tv, FALSE);
 
        return FALSE;
 }
 
 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days,
-                                                    void (*func_valid)(const gchar *path, gint valid, gpointer data),
+                                                    void (*func_valid)(const gchar *path, gboolean valid, gpointer data),
                                                     gpointer data)
 {
        ThumbValidate *tv;
@@ -855,19 +999,21 @@ ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gi
        tv->func_valid = func_valid;
        tv->data = data;
 
-       if (!thumb_loader_std_setup(tv->tl, thumb_path))
+       FileData *fd = file_data_new_no_grouping(thumb_path);
+       if (!thumb_loader_std_setup(tv->tl, fd))
                {
                tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv);
                }
        else
                {
-               tv->idle_id = -1;
+               tv->idle_id = 0;
                }
 
+       file_data_unref(fd);
        return tv->tl;
 }
 
-static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gint local,
+static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gboolean local,
                                       const gchar *subfolder)
 {
        gchar *thumb_path;
@@ -922,16 +1068,17 @@ static GList *thumb_std_maint_move_tail = NULL;
 
 
 static void thumb_std_maint_move_step(TMaintMove *tm);
-static gint thumb_std_maint_move_idle(gpointer data);
+static gboolean thumb_std_maint_move_idle(gpointer data);
 
 
-static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data)
+static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid, gpointer data)
 {
        TMaintMove *tm = data;
        GdkPixbuf *pixbuf;
 
-       /* this function is called on success, so the pixbuf should not be a fallback*/
-       pixbuf = thumb_loader_std_get_pixbuf(tm->tl);
+       /* get the original thumbnail pixbuf (unrotated, with original options)
+          this is called from image_loader done callback, so tm->tl->il must exist*/
+       pixbuf = image_loader_get_pixbuf(tm->tl->il);
        if (pixbuf)
                {
                const gchar *uri;
@@ -952,9 +1099,8 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpoi
                        tm->tl->cache_enable = TRUE;
                        tm->tl->cache_hit = FALSE;
                        tm->tl->cache_local = FALSE;
-
                        file_data_unref(tm->tl->fd);
-                       tm->tl->fd = file_data_new_simple(tm->dest);
+                       tm->tl->fd = file_data_new_group(tm->dest);
                        tm->tl->source_mtime = strtol(mtime_str, NULL, 10);
 
                        pathl = path_from_utf8(tm->tl->fd->path);
@@ -974,7 +1120,6 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpoi
 
                DEBUG_1("thumb move unlink: %s", tm->thumb_path);
                unlink_file(tm->thumb_path);
-               g_object_unref(pixbuf);
                }
 
        thumb_std_maint_move_step(tm);
@@ -1009,7 +1154,7 @@ static void thumb_std_maint_move_step(TMaintMove *tm)
                                                      thumb_std_maint_move_validate_cb, tm);
 }
 
-static gint thumb_std_maint_move_idle(gpointer data)
+static gboolean thumb_std_maint_move_idle(gpointer data)
 {
        TMaintMove *tm;
        gchar *pathl;
@@ -1040,8 +1185,8 @@ static gint thumb_std_maint_move_idle(gpointer data)
  *
  * The thumbnails are processed when the app is idle. If the app
  * exits early well too bad - they can simply be regenerated from scratch.
- *
- * This does not manage local thumbnails (fixme ?)
+ */
+/** @FIXME This does not manage local thumbnails (fixme ?)
  */
 void thumb_std_maint_moved(const gchar *source, const gchar *dest)
 {
@@ -1067,3 +1212,4 @@ void thumb_std_maint_moved(const gchar *source, const gchar *dest)
                thumb_std_maint_move_tail = thumb_std_maint_move_list;
                }
 }
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */