[gnome-desktop/wip/thumbnail-resources: 3/7] thumbnail: Move thumbnail generation/saving to a few helper functions



commit a01c6b5c24159559b5d0eb29fe692f88fa38bb98
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Sep 5 18:49:21 2012 -0300

    thumbnail: Move thumbnail generation/saving to a few helper functions
    
    Another cleanup in preparation for a new "simple" thumbnail API.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684026

 libgnome-desktop/gnome-desktop-thumbnail.c |  305 +++++++++------------------
 1 files changed, 102 insertions(+), 203 deletions(-)
---
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c
index fcbdf44..7a78895 100644
--- a/libgnome-desktop/gnome-desktop-thumbnail.c
+++ b/libgnome-desktop/gnome-desktop-thumbnail.c
@@ -887,6 +887,28 @@ thumbnail_path (const char                *uri,
   return path;
 }
 
+static char *
+lookup_thumbnail_path (const char                *uri,
+                       time_t                     mtime,
+                       GnomeDesktopThumbnailSize  size,
+                       gboolean                   is_failed)
+{
+  GdkPixbuf *pixbuf;
+  char *path = thumbnail_path (uri, size, is_failed);
+
+  pixbuf = gdk_pixbuf_new_from_file (path, NULL);
+  if (pixbuf == NULL)
+    goto out;
+
+  if (!gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime))
+    g_clear_pointer (&path, g_free);
+
+  g_clear_object (&pixbuf);
+
+ out:
+  return path;
+}
+
 /**
  * gnome_desktop_thumbnail_factory_lookup:
  * @factory: a #GnomeDesktopThumbnailFactory
@@ -907,30 +929,10 @@ gnome_desktop_thumbnail_factory_lookup (GnomeDesktopThumbnailFactory *factory,
 					time_t                 mtime)
 {
   GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv;
-  GdkPixbuf *pixbuf;
-  gboolean res;
-  char *path;
 
   g_return_val_if_fail (uri != NULL, NULL);
 
-  res = FALSE;
-
-  path = thumbnail_path (uri, priv->size, FALSE);
-
-  pixbuf = gdk_pixbuf_new_from_file (path, NULL);
-  if (pixbuf != NULL)
-    {
-      res = gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime);
-      g_object_unref (pixbuf);
-    }
-
-  g_checksum_free (checksum);
-
-  if (res)
-    return path;
-
-  g_free (path);
-  return NULL;
+  return lookup_thumbnail_path (uri, mtime, priv->size, FALSE);
 }
 
 /**
@@ -954,26 +956,17 @@ gnome_desktop_thumbnail_factory_has_valid_failed_thumbnail (GnomeDesktopThumbnai
 							    const char            *uri,
 							    time_t                 mtime)
 {
+  GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv;
   char *path;
-  GdkPixbuf *pixbuf;
-  gboolean res;
 
-  res = FALSE;
+  g_return_val_if_fail (uri != NULL, FALSE);
 
-  path = thumbnail_path (uri, priv->size, TRUE);
+  path = lookup_thumbnail_path (uri, mtime, priv->size, TRUE);
+  if (path == NULL)
+    return FALSE;
 
-  pixbuf = gdk_pixbuf_new_from_file (path, NULL);
   g_free (path);
-
-  if (pixbuf)
-    {
-      res = gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime);
-      g_object_unref (pixbuf);
-    }
-
-  g_checksum_free (checksum);
-
-  return res;
+  return TRUE;
 }
 
 static gboolean
@@ -1289,89 +1282,89 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory
 }
 
 static gboolean
-make_thumbnail_dirs (GnomeDesktopThumbnailFactory *factory)
+save_thumbnail (GdkPixbuf  *pixbuf,
+                char       *path,
+                const char *uri,
+                time_t      mtime)
 {
-  char *thumbnail_dir;
-  char *image_dir;
-  gboolean res;
+  char *dirname;
+  char *tmp_path = NULL;
+  int tmp_fd;
+  char mtime_str[21];
+  gboolean saved_ok;
+  gboolean ret = FALSE;
+  GError *error = NULL;
+  const char *width, *height;
 
-  res = FALSE;
+  if (pixbuf == NULL)
+    return FALSE;
 
-  thumbnail_dir = g_build_filename (g_get_user_cache_dir (),
-				    "thumbnails",
-				    NULL);
-  if (!g_file_test (thumbnail_dir, G_FILE_TEST_IS_DIR))
-    {
-      g_mkdir (thumbnail_dir, 0700);
-      res = TRUE;
-    }
+  dirname = g_path_get_dirname (path);
 
-  image_dir = g_build_filename (thumbnail_dir,
-				(factory->priv->size == GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL)?"normal":"large",
-				NULL);
-  if (!g_file_test (image_dir, G_FILE_TEST_IS_DIR))
-    {
-      g_mkdir (image_dir, 0700);
-      res = TRUE;
-    }
+  if (!g_mkdir_with_parents (dirname, 0700))
+    goto out;
 
-  g_free (thumbnail_dir);
-  g_free (image_dir);
-  
-  return res;
-}
+  tmp_path = g_strconcat (path, ".XXXXXX", NULL);
+  tmp_fd = g_mkstemp (tmp_path);
 
-static gboolean
-make_thumbnail_fail_dirs (GnomeDesktopThumbnailFactory *factory)
-{
-  char *thumbnail_dir;
-  char *fail_dir;
-  char *app_dir;
-  gboolean res;
+  if (tmp_fd == -1)
+    goto out;
+  close (tmp_fd);
 
-  res = FALSE;
+  g_snprintf (mtime_str, 21, "%ld",  mtime);
+  width = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Width");
+  height = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Height");
 
-  thumbnail_dir = g_build_filename (g_get_user_cache_dir (),
-				    "thumbnails",
-				    NULL);
-  if (!g_file_test (thumbnail_dir, G_FILE_TEST_IS_DIR))
-    {
-      g_mkdir (thumbnail_dir, 0700);
-      res = TRUE;
-    }
+  error = NULL;
+  if (width != NULL && height != NULL)
+    saved_ok  = gdk_pixbuf_save (pixbuf,
+				 tmp_path,
+				 "png", &error,
+				 "tEXt::Thumb::Image::Width", width,
+				 "tEXt::Thumb::Image::Height", height,
+				 "tEXt::Thumb::URI", uri,
+				 "tEXt::Thumb::MTime", mtime_str,
+				 "tEXt::Software", "GNOME::ThumbnailFactory",
+				 NULL);
+  else
+    saved_ok  = gdk_pixbuf_save (pixbuf,
+				 tmp_path,
+				 "png", &error,
+				 "tEXt::Thumb::URI", uri,
+				 "tEXt::Thumb::MTime", mtime_str,
+				 "tEXt::Software", "GNOME::ThumbnailFactory",
+				 NULL);
 
-  fail_dir = g_build_filename (thumbnail_dir,
-			       "fail",
-			       NULL);
-  if (!g_file_test (fail_dir, G_FILE_TEST_IS_DIR))
-    {
-      g_mkdir (fail_dir, 0700);
-      res = TRUE;
-    }
+  if (!saved_ok)
+    goto out;
 
-  app_dir = g_build_filename (fail_dir,
-			      appname,
-			      NULL);
-  if (!g_file_test (app_dir, G_FILE_TEST_IS_DIR))
+  ret = TRUE;
+  g_chmod (tmp_path, 0600);
+  g_rename (tmp_path, path);
+
+ out:
+  if (error != NULL)
     {
-      g_mkdir (app_dir, 0700);
-      res = TRUE;
+      g_warning ("Failed to create thumbnail %s: %s", tmp_path, error->message);
+      g_error_free (error);
     }
-
-  g_free (thumbnail_dir);
-  g_free (fail_dir);
-  g_free (app_dir);
-  
-  return res;
+  g_unlink (tmp_path);
+  g_free (tmp_path);
+  return ret;
 }
 
+static GdkPixbuf *
+make_failed_thumbnail (void)
+{
+  return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
+}
 
 /**
  * gnome_desktop_thumbnail_factory_save_thumbnail:
  * @factory: a #GnomeDesktopThumbnailFactory
- * @thumbnail: the thumbnail as a pixbuf 
+ * @thumbnail: the thumbnail as a pixbuf
  * @uri: the uri of a file
- * @original_mtime: the modification time of the original file 
+ * @original_mtime: the modification time of the original file
  *
  * Saves @thumbnail at the right place. If the save fails a
  * failed thumbnail is written.
@@ -1388,74 +1381,17 @@ gnome_desktop_thumbnail_factory_save_thumbnail (GnomeDesktopThumbnailFactory *fa
 {
   GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv;
   char *path;
-  char *tmp_path;
-  const char *width, *height;
-  int tmp_fd;
-  char mtime_str[21];
-  gboolean saved_ok;
 
   path = thumbnail_path (uri, priv->size, FALSE);
-
-  tmp_path = g_strconcat (path, ".XXXXXX", NULL);
-
-  tmp_fd = g_mkstemp (tmp_path);
-  if (tmp_fd == -1 &&
-      make_thumbnail_dirs (factory))
-    {
-      g_free (tmp_path);
-      tmp_path = g_strconcat (path, ".XXXXXX", NULL);
-      tmp_fd = g_mkstemp (tmp_path);
-    }
-
-  if (tmp_fd == -1)
+  if (!save_thumbnail (thumbnail, path, uri, original_mtime))
     {
-      gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, uri, original_mtime);
-      g_free (tmp_path);
+      thumbnail = make_failed_thumbnail ();
       g_free (path);
-      return;
-    }
-  close (tmp_fd);
-  
-  g_snprintf (mtime_str, 21, "%ld",  original_mtime);
-  width = gdk_pixbuf_get_option (thumbnail, "tEXt::Thumb::Image::Width");
-  height = gdk_pixbuf_get_option (thumbnail, "tEXt::Thumb::Image::Height");
-
-  error = NULL;
-  if (width != NULL && height != NULL) 
-    saved_ok  = gdk_pixbuf_save (thumbnail,
-				 tmp_path,
-				 "png", &error,
-				 "tEXt::Thumb::Image::Width", width,
-				 "tEXt::Thumb::Image::Height", height,
-				 "tEXt::Thumb::URI", uri,
-				 "tEXt::Thumb::MTime", mtime_str,
-				 "tEXt::Software", "GNOME::ThumbnailFactory",
-				 NULL);
-  else
-    saved_ok  = gdk_pixbuf_save (thumbnail,
-				 tmp_path,
-				 "png", &error,
-				 "tEXt::Thumb::URI", uri,
-				 "tEXt::Thumb::MTime", mtime_str,
-				 "tEXt::Software", "GNOME::ThumbnailFactory",
-				 NULL);
-    
-
-  if (saved_ok)
-    {
-      g_chmod (tmp_path, 0600);
-      g_rename (tmp_path, path);
+      path = thumbnail_path (uri, priv->size, TRUE);
+      save_thumbnail (thumbnail, path, uri, original_mtime);
+      g_object_unref (thumbnail);
     }
-  else
-    {
-      g_warning ("Failed to create thumbnail %s: %s", tmp_path, error->message);
-      gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, uri, original_mtime);
-      g_unlink (tmp_path);
-      g_clear_error (&error);
-    }
-
   g_free (path);
-  g_free (tmp_path);
 }
 
 /**
@@ -1476,52 +1412,15 @@ gnome_desktop_thumbnail_factory_create_failed_thumbnail (GnomeDesktopThumbnailFa
 							 const char            *uri,
 							 time_t                 mtime)
 {
+  GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv;
   char *path;
-  char *tmp_path;
-  int tmp_fd;
-  char mtime_str[21];
-  gboolean saved_ok;
   GdkPixbuf *pixbuf;
 
   path = thumbnail_path (uri, priv->size, TRUE);
-
-  tmp_path = g_strconcat (path, ".XXXXXX", NULL);
-
-  tmp_fd = g_mkstemp (tmp_path);
-  if (tmp_fd == -1 &&
-      make_thumbnail_fail_dirs (factory))
-    {
-      g_free (tmp_path);
-      tmp_path = g_strconcat (path, ".XXXXXX", NULL);
-      tmp_fd = g_mkstemp (tmp_path);
-    }
-
-  if (tmp_fd == -1)
-    {
-      g_free (tmp_path);
-      g_free (path);
-      return;
-    }
-  close (tmp_fd);
-  
-  g_snprintf (mtime_str, 21, "%ld",  mtime);
-  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
-  saved_ok  = gdk_pixbuf_save (pixbuf,
-			       tmp_path,
-			       "png", NULL, 
-			       "tEXt::Thumb::URI", uri,
-			       "tEXt::Thumb::MTime", mtime_str,
-			       "tEXt::Software", "GNOME::ThumbnailFactory",
-			       NULL);
-  g_object_unref (pixbuf);
-  if (saved_ok)
-    {
-      g_chmod (tmp_path, 0600);
-      g_rename(tmp_path, path);
-    }
-
+  pixbuf = make_failed_thumbnail ();
+  save_thumbnail (pixbuf, path, uri, mtime);
   g_free (path);
-  g_free (tmp_path);
+  g_object_unref (pixbuf);
 }
 
 /**



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]