Commit 3a4e595d authored by Cosimo Cecchi's avatar Cosimo Cecchi
Browse files

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
parent dcad22bf
......@@ -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 *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment