Trim trailing white spaces.
[geeqie.git] / src / thumb_standard.c
index 892674b..f3cd252 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -66,7 +66,6 @@
 
 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data);
 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd);
-static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path);
 
 
 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height)
@@ -388,7 +387,7 @@ static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf,
                {
                if (!tl->fd->exif_orientation)
                        {
-                       tl->fd->exif_orientation = metadata_read_int(tl->fd, "Exif.Image.Orientation", EXIF_ORIENTATION_TOP_LEFT);
+                       tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
                        }
                
                if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
@@ -419,6 +418,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))
@@ -432,7 +432,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 &&
@@ -506,10 +513,16 @@ static gboolean thumb_loader_std_next_source(ThumbLoaderStd *tl, gboolean remove
                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_path(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);
@@ -621,14 +634,6 @@ static gboolean thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd)
        return FALSE;
 }
 
-static gboolean thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path)
-{
-       FileData *fd = file_data_new_simple(path);
-       gboolean ret = thumb_loader_std_setup(tl, fd);
-       file_data_unref(fd);
-       return ret;
-}
-
 /*
  * 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.
@@ -681,7 +686,16 @@ gboolean thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd)
                tl->thumb_path_local = FALSE;
 
                found = isfile(tl->thumb_path);
-               if (found && thumb_loader_std_setup_path(tl, tl->thumb_path)) return TRUE;
+               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);
+                       }
 
                if (thumb_loader_std_fail_check(tl) ||
                    !thumb_loader_std_next_source(tl, found))
@@ -738,7 +752,7 @@ struct _ThumbValidate
        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)
@@ -756,8 +770,11 @@ 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);
 }
@@ -775,8 +792,9 @@ static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpo
        GdkPixbuf *pixbuf;
        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;
@@ -817,8 +835,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);
@@ -835,7 +855,7 @@ 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;
@@ -862,15 +882,17 @@ 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_path(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;
 }
 
@@ -937,8 +959,9 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid,
        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;
@@ -959,9 +982,8 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid,
                        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);
@@ -981,7 +1003,6 @@ static void thumb_std_maint_move_validate_cb(const gchar *path, gboolean valid,
 
                DEBUG_1("thumb move unlink: %s", tm->thumb_path);
                unlink_file(tm->thumb_path);
-               g_object_unref(pixbuf);
                }
 
        thumb_std_maint_move_step(tm);