[gtranslator/dl-info: 4/6] Do not override files on DL load
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtranslator/dl-info: 4/6] Do not override files on DL load
- Date: Mon, 4 Jan 2021 08:06:04 +0000 (UTC)
commit 722d9a3114965914bcae447ed3b7dee9b67fbd45
Author: Daniel GarcĂa Moreno <dani danigm net>
Date: Mon Dec 28 12:21:18 2020 +0100
Do not override files on DL load
Instead of override the files when downloading from DL it's better to
just add a number to the filename and create a new one so we do not
override the existing files on the user Downloads folder.
This patch adds a number to the filename so we'll have something like
"es_ES (1).po".
https://gitlab.gnome.org/GNOME/gtranslator/-/issues/124
src/gtr-dl-teams.c | 34 +++++++++++++++++++++++-----------
src/gtr-utils.c | 22 ++++++++++++++++++++++
src/gtr-utils.h | 2 ++
3 files changed, 47 insertions(+), 11 deletions(-)
---
diff --git a/src/gtr-dl-teams.c b/src/gtr-dl-teams.c
index 094bc628..22ee8a3b 100644
--- a/src/gtr-dl-teams.c
+++ b/src/gtr-dl-teams.c
@@ -436,8 +436,11 @@ gtr_dl_teams_load_po_file (GtkButton *button, GtrDlTeams *self)
GOutputStream *output = NULL;
gsize bytes = 0;
GtkWidget *dialog;
- char *basename = NULL;
+ gboolean ret = FALSE;
+ int file_index = 0;
const char *dest_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
+ g_autofree char *basename = NULL;
+ g_autofree char *filename = NULL;
g_autofree char *file_path = NULL;
g_autoptr(GFile) dest_file = NULL;
@@ -496,11 +499,25 @@ gtr_dl_teams_load_po_file (GtkButton *button, GtrDlTeams *self)
}
/* Save file to Downloads; file basename is the part from last / character on */
- basename = strrchr (priv->file_path, '/');
+ basename = g_path_get_basename (priv->file_path);
+ // Remove the extension
+ filename = gtr_utils_get_filename (basename);
file_path = g_strconcat ("file://", dest_dir, "/", basename, NULL);
dest_file = g_file_new_for_uri (file_path);
- g_file_copy (tmp_file, dest_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
+ ret = g_file_copy (tmp_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
+ while (!ret && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+ {
+ g_free (basename);
+ g_free (file_path);
+ g_object_unref (dest_file);
+ g_clear_error (&error);
+
+ basename = g_strdup_printf ("%s (%d).po", filename, ++file_index);
+ file_path = g_strconcat ("file://", dest_dir, "/", basename, NULL);
+ dest_file = g_file_new_for_uri (file_path);
+ ret = g_file_copy (tmp_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
+ }
if (error != NULL)
{
@@ -517,14 +534,9 @@ gtr_dl_teams_load_po_file (GtkButton *button, GtrDlTeams *self)
if (gtr_open (dest_file, priv->main_window, &error)) {
GtrTab *tab = gtr_window_get_active_tab (priv->main_window);
g_autofree char *info_msg = NULL;
- if (basename)
- {
- info_msg = g_strdup_printf (_("The file '%s' has been stored on %s"),
- basename + 1, dest_dir);
- }
- else
- info_msg = g_strdup_printf (_("The file has been stored on %s"), dest_dir);
-
+ g_autofree char *filename = g_file_get_basename (dest_file);
+ info_msg = g_strdup_printf (_("The file '%s' has been stored on %s"),
+ filename, dest_dir);
gtr_tab_set_info (tab, info_msg, NULL);
}
diff --git a/src/gtr-utils.c b/src/gtr-utils.c
index 36356b2e..e961de2f 100644
--- a/src/gtr-utils.c
+++ b/src/gtr-utils.c
@@ -562,3 +562,25 @@ gtr_utils_escape_underscores (const gchar * text, gssize length)
return g_string_free (str, FALSE);
}
+
+// Removes the extension from a filename
+gchar *
+gtr_utils_get_filename (const gchar * filename)
+{
+ gchar *new_str;
+ gchar **array;
+ int l = 0;
+
+ array = g_strsplit (filename, ".", -1);
+ l = g_strv_length (array);
+
+ // Remove the extension
+ g_free (array[l]);
+ array[l] = NULL;
+
+ new_str = g_strjoinv (".", array);
+
+ g_strfreev (array);
+
+ return new_str;
+}
diff --git a/src/gtr-utils.h b/src/gtr-utils.h
index 86e84d49..bfa26636 100644
--- a/src/gtr-utils.h
+++ b/src/gtr-utils.h
@@ -76,4 +76,6 @@ void gtr_utils_menu_position_under_tree_view (GtkMenu * menu,
gchar *gtr_utils_get_datadir (void);
gchar *gtr_utils_get_win32_plugindir (void);
+
+ gchar * gtr_utils_get_filename (const gchar * filename);
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]