From 764cc68ff6b55a91ad7b43250e465c60dc9cee65 Mon Sep 17 00:00:00 2001 From: Jeff Shipley Date: Sat, 27 Jul 2013 04:30:50 -0600 Subject: [PATCH] Use filename-friendly date format for saved files (bug 8445) --- lib/screenshooter-dialogs.c | 9 +++--- lib/screenshooter-utils.c | 66 +++++++++---------------------------------- lib/screenshooter-utils.h | 3 +- lib/screenshooter-zimagez.c | 4 +-- 4 files changed, 20 insertions(+), 62 deletions(-) diff --git a/lib/screenshooter-dialogs.c b/lib/screenshooter-dialogs.c index 104190a..5e4ce91 100644 --- a/lib/screenshooter-dialogs.c +++ b/lib/screenshooter-dialogs.c @@ -220,8 +220,7 @@ static gchar *generate_filename_for_uri (const gchar *uri, GFile *directory; GFile *file; gchar *base_name; - const gchar *date = screenshooter_get_date (TRUE); - const gchar *current_time = screenshooter_get_time (); + gchar *datetime; gint i; if (G_UNLIKELY (uri == NULL)) @@ -232,11 +231,12 @@ static gchar *generate_filename_for_uri (const gchar *uri, } TRACE ("Get the folder corresponding to the URI"); + datetime = screenshooter_get_datetime("%Y-%m-%d_%H%M%S"); directory = g_file_new_for_uri (uri); if (!timestamp) base_name = g_strconcat (title, ".png", NULL); else - base_name = g_strconcat (title, " - ", date, " - ", current_time, ".png", NULL); + base_name = g_strconcat (title, "_", datetime, ".png", NULL); file = g_file_get_child (directory, base_name); @@ -259,7 +259,7 @@ static gchar *generate_filename_for_uri (const gchar *uri, if (!timestamp) base_name = g_strconcat (title, extension, NULL); else - base_name = g_strconcat (title, " - ", date, " - ", current_time, extension, NULL); + base_name = g_strconcat (title, "_", datetime, extension, NULL); file = g_file_get_child (directory, base_name); @@ -272,6 +272,7 @@ static gchar *generate_filename_for_uri (const gchar *uri, g_object_unref (file); } + g_free(datetime); g_object_unref (directory); return base_name; diff --git a/lib/screenshooter-utils.c b/lib/screenshooter-utils.c index 4e64b47..8159342 100644 --- a/lib/screenshooter-utils.c +++ b/lib/screenshooter-utils.c @@ -279,65 +279,25 @@ void screenshooter_error (const gchar *format, ...) g_free (message); } -gchar *screenshooter_get_time (void) -{ - time_t now = time (0); - const struct tm *tm; - gchar *result, *converted; - gsize length; - gchar buffer[512]; - - tm = localtime (&now); - - converted = g_locale_from_utf8 ("%X", -1, NULL, NULL, NULL); - if (G_UNLIKELY (converted == NULL)) - converted = g_strdup (""); - length = strftime (buffer, sizeof (buffer), converted, tm); - - if (G_UNLIKELY (length == 0)) - { - TRACE ("Buffer is NULL"); - buffer[0] = '\0'; - } - - result = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL); - - g_free (converted); - - return result; -} -gchar *screenshooter_get_date (gboolean strip_slashes) +/** + * screenshooter_get_timestamp + * @format - a date format string + * + * Builds a timestamp using local time. + * Returned string should be released with g_free() +**/ +gchar *screenshooter_get_datetime (const gchar *format) { - GDate *date = g_date_new (); - gchar *result; - gchar **split = NULL; - gchar buffer[512]; - gsize length; - - g_date_set_time_t (date, time (NULL)); - - length = g_date_strftime (buffer, sizeof (buffer), "%x", date); - - if (G_UNLIKELY (length == 0)) - { - TRACE ("Buffer is NULL"); - buffer[0] = '\0'; - } + gchar *timestamp; - if (strip_slashes) - { - split = g_strsplit (buffer, "/", 0); - result = g_strjoinv (NULL, split); - } - else - result = g_strdup (buffer); + GDateTime *now = g_date_time_new_now_local(); + timestamp = g_date_time_format (now, format); - g_strfreev (split); - g_free (date); + g_date_time_unref (now); - return result; + return timestamp; } diff --git a/lib/screenshooter-utils.h b/lib/screenshooter-utils.h index 1646a8e..4453706 100644 --- a/lib/screenshooter-utils.h +++ b/lib/screenshooter-utils.h @@ -49,8 +49,7 @@ gboolean screenshooter_is_remote_uri (const gchar *uri); gchar *rot13 (gchar *string); void screenshooter_error (const gchar *format, ...); -gchar *screenshooter_get_date (gboolean strip_slashes); -gchar *screenshooter_get_time (void); +gchar *screenshooter_get_datetime (const gchar *format); void screenshooter_open_help (GtkWindow *parent); gboolean screenshooter_f1_key (GtkWidget *widget, GdkEventKey *event, diff --git a/lib/screenshooter-zimagez.c b/lib/screenshooter-zimagez.c index f607b1b..69e7810 100644 --- a/lib/screenshooter-zimagez.c +++ b/lib/screenshooter-zimagez.c @@ -202,12 +202,10 @@ zimagez_upload_job (ScreenshooterJob *job, GValueArray *param_values, GError **e const gchar *encoded_data; const gchar *image_path; const gchar *last_user; - const gchar *date = screenshooter_get_date (FALSE); - const gchar *current_time = screenshooter_get_time (); const gchar *proxy_uri; /* For translators: the first wildcard is the date, the second one the time, * e.g. "Taken on 12/31/99, at 23:13:48". */ - gchar *comment = g_strdup_printf (_("Taken on %s, at %s"), date, current_time); + gchar *comment = screenshooter_get_datetime (_("Taken on %x, at %X")); gchar *data = NULL; gchar *encoded_password = NULL; gchar *file_name = NULL; -- 1.7.9.5