[gnome-shell] screenshot: save to an unique path when using a basename



commit 3a4e595d326bbd169b3fa0ae7ec63ee4453db2a8
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Jan 2 18:58:07 2013 +0100

    screenshot: save to an unique path when using a basename
    
    When a basename is passed, avoid filename conflicts. The unique path is
    returned by the API.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=688004

 src/shell-screenshot.c |   42 ++++++++++++++++++++++++++++++------------
 1 files changed, 30 insertions(+), 12 deletions(-)
---
diff --git a/src/shell-screenshot.c b/src/shell-screenshot.c
index b05550e..4078ccd 100644
--- a/src/shell-screenshot.c
+++ b/src/shell-screenshot.c
@@ -77,13 +77,14 @@ on_screenshot_written (GObject *source,
 
 /* called in an I/O thread */
 static GOutputStream *
-get_stream_for_path (const gchar *path,
-                     const gchar *filename,
-                     gchar **filename_used)
+get_stream_for_unique_path (const gchar *path,
+                            const gchar *filename,
+                            gchar **filename_used)
 {
   GOutputStream *stream;
   GFile *file;
   gchar *real_path, *real_filename, *name, *ptr;
+  gint idx;
 
   ptr = g_strrstr (filename, ".png");
 
@@ -92,16 +93,33 @@ get_stream_for_path (const gchar *path,
   else
     real_filename = g_strdup (filename);
 
-  name = g_strdup_printf ("%s.png", real_filename);
-  real_path = g_build_filename (path, name, NULL);
-  g_free (name);
-  g_free (real_filename);
+  idx = 0;
+  real_path = NULL;
+
+  do
+    {
+      if (idx == 0)
+        name = g_strdup_printf ("%s.png", real_filename);
+      else
+        name = g_strdup_printf ("%s - %d.png", real_filename, idx);
+
+      real_path = g_build_filename (path, name, NULL);
+      g_free (name);
+
+      file = g_file_new_for_path (real_path);
+      stream = G_OUTPUT_STREAM (g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL));
+      g_object_unref (file);
 
-  file = g_file_new_for_path (real_path);
-  stream = G_OUTPUT_STREAM (g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL));
-  g_object_unref (file);
+      if (stream != NULL)
+        *filename_used = real_path;
+      else
+        g_free (real_path);
 
-  *filename_used = real_path;
+      idx++;
+    }
+  while (stream == NULL);
+
+  g_free (real_filename);
 
   return stream;
 }
@@ -121,7 +139,7 @@ get_stream_for_filename (const gchar *filename,
         return NULL;
     }
 
-  return get_stream_for_path (path, filename, filename_used);
+  return get_stream_for_unique_path (path, filename, filename_used);
 }
 
 static GOutputStream *



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