[gnome-desktop/wip/thumbnail-resources: 30/32] thumbnail: Refactor script running part out into a separate function



commit 1c805c8feeb581738f477984d9e163b1c52a6d8c
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Oct 1 17:52:30 2012 -0300

    thumbnail: Refactor script running part out into a separate function

 libgnome-desktop/gnome-desktop-thumbnail.c |   63 +++++++++++++++++-----------
 1 files changed, 38 insertions(+), 25 deletions(-)
---
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c
index 541ad4a..94ea0be 100644
--- a/libgnome-desktop/gnome-desktop-thumbnail.c
+++ b/libgnome-desktop/gnome-desktop-thumbnail.c
@@ -1154,6 +1154,42 @@ expand_thumbnailing_script (const char *script,
   return NULL;
 }
 
+static GdkPixbuf *
+run_script (char *script, const char *uri, int size)
+{
+  int fd;
+  int exit_status;
+  char *expanded_script;
+  char *tmpname;
+  GdkPixbuf *pixbuf = NULL;
+
+  fd = g_file_open_tmp (".gnome_desktop_thumbnail.XXXXXX", &tmpname, NULL);
+
+  if (fd == -1)
+    return NULL;
+
+  close (fd);
+  expanded_script = expand_thumbnailing_script (script, size, uri, tmpname);
+
+  if (expanded_script == NULL)
+    goto out;
+
+  if (!g_spawn_command_line_sync (expanded_script, NULL, NULL, &exit_status, NULL))
+    goto out;
+
+  if (exit_status != 0)
+    goto out;
+
+  pixbuf = gdk_pixbuf_new_from_file (tmpname, NULL);
+
+ out:
+  g_free (expanded_script);
+  g_unlink (tmpname);
+  g_free (tmpname);
+
+  return pixbuf;
+}
+
 /**
  * gnome_desktop_thumbnail_factory_generate_thumbnail:
  * @factory: a #GnomeDesktopThumbnailFactory
@@ -1175,14 +1211,12 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory
 						    const char            *mime_type)
 {
   GdkPixbuf *pixbuf, *scaled, *tmp_pixbuf;
-  char *script, *expanded_script;
+  char *script;
   int width, height, size;
   int original_width = 0;
   int original_height = 0;
   char dimension[12];
   double scale;
-  int exit_status;
-  char *tmpname;
 
   g_return_val_if_fail (uri != NULL, NULL);
   g_return_val_if_fail (mime_type != NULL, NULL);
@@ -1209,28 +1243,7 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory
   
   if (script)
     {
-      int fd;
-
-      fd = g_file_open_tmp (".gnome_desktop_thumbnail.XXXXXX", &tmpname, NULL);
-
-      if (fd != -1)
-	{
-	  close (fd);
-
-	  expanded_script = expand_thumbnailing_script (script, size, uri, tmpname);
-	  if (expanded_script != NULL &&
-	      g_spawn_command_line_sync (expanded_script,
-					 NULL, NULL, &exit_status, NULL) &&
-	      exit_status == 0)
-	    {
-	      pixbuf = gdk_pixbuf_new_from_file (tmpname, NULL);
-	    }
-
-	  g_free (expanded_script);
-	  g_unlink (tmpname);
-	  g_free (tmpname);
-	}
-
+      pixbuf = run_script (script, uri, size);
       g_free (script);
     }
 



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