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
This commit is contained in:
Cosimo Cecchi 2013-01-02 18:58:07 +01:00
parent dcad22bfa8
commit 3a4e595d32

View File

@ -77,13 +77,14 @@ on_screenshot_written (GObject *source,
/* called in an I/O thread */ /* called in an I/O thread */
static GOutputStream * static GOutputStream *
get_stream_for_path (const gchar *path, get_stream_for_unique_path (const gchar *path,
const gchar *filename, const gchar *filename,
gchar **filename_used) gchar **filename_used)
{ {
GOutputStream *stream; GOutputStream *stream;
GFile *file; GFile *file;
gchar *real_path, *real_filename, *name, *ptr; gchar *real_path, *real_filename, *name, *ptr;
gint idx;
ptr = g_strrstr (filename, ".png"); ptr = g_strrstr (filename, ".png");
@ -92,17 +93,34 @@ get_stream_for_path (const gchar *path,
else else
real_filename = g_strdup (filename); real_filename = g_strdup (filename);
name = g_strdup_printf ("%s.png", real_filename); idx = 0;
real_path = g_build_filename (path, name, NULL); real_path = NULL;
g_free (name);
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);
if (stream != NULL)
*filename_used = real_path;
else
g_free (real_path);
idx++;
}
while (stream == NULL);
g_free (real_filename); g_free (real_filename);
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);
*filename_used = real_path;
return stream; return stream;
} }
@ -121,7 +139,7 @@ get_stream_for_filename (const gchar *filename,
return NULL; return NULL;
} }
return get_stream_for_path (path, filename, filename_used); return get_stream_for_unique_path (path, filename, filename_used);
} }
static GOutputStream * static GOutputStream *