summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Schwinn <alexxcons@xfce.org>2020-11-12 22:18:01 +0100
committerAlexander Schwinn <alexxcons@xfce.org>2020-12-07 23:49:38 +0100
commit7316aabab07444e546ab640f5582ef3e8757ecbc (patch)
treef35da10d743a9f4319360731c2c1180431cc3933
parent8514580934e47f79b0f70f60d75b70c171c6838d (diff)
downloadthunar-7316aabab07444e546ab640f5582ef3e8757ecbc.tar.gz
Escape all whitespace characters in custom command path
for open with --> custom command dialog. When the file-chooser dialog was used to pick a file, whitespace characters will be escaped. This will fix this old bug: https://bugzilla.xfce.org/show_bug.cgi?id=10883 withouth causing trouble for direct command input (Issue #425)
-rw-r--r--thunar/thunar-chooser-dialog.c5
-rw-r--r--thunar/thunar-gobject-extensions.c52
-rw-r--r--thunar/thunar-gobject-extensions.h3
3 files changed, 58 insertions, 2 deletions
diff --git a/thunar/thunar-chooser-dialog.c b/thunar/thunar-chooser-dialog.c
index bb615a73..05bdbb8c 100644
--- a/thunar/thunar-chooser-dialog.c
+++ b/thunar/thunar-chooser-dialog.c
@@ -743,6 +743,7 @@ thunar_chooser_dialog_browse_clicked (GtkWidget *button,
GtkFileFilter *filter;
GtkWidget *chooser;
gchar *filename;
+ gchar *filename_escaped;
gchar *s;
chooser = gtk_file_chooser_dialog_new (_("Select an Application"),
@@ -840,7 +841,9 @@ thunar_chooser_dialog_browse_clicked (GtkWidget *button,
if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
- gtk_entry_set_text (GTK_ENTRY (dialog->custom_entry), filename);
+ filename_escaped = thunar_g_strescape (filename);
+ gtk_entry_set_text (GTK_ENTRY (dialog->custom_entry), filename_escaped);
+ g_free (filename_escaped);
g_free (filename);
}
diff --git a/thunar/thunar-gobject-extensions.c b/thunar/thunar-gobject-extensions.c
index f4cd32e3..ec56b24e 100644
--- a/thunar/thunar-gobject-extensions.c
+++ b/thunar/thunar-gobject-extensions.c
@@ -117,3 +117,55 @@ thunar_g_initialize_transformations (void)
/* register a transformation function string->enum unconditionally */
g_value_register_transform_func (G_TYPE_STRING, G_TYPE_ENUM, transform_string_to_enum);
}
+
+
+
+/**
+ * thunar_g_strescape
+ * @source : The string to escape
+ *
+ * Similar to g_strescape, but as well escapes SPACE
+ *
+ * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\' ' ' and '"' in the string source by inserting a '\' before them.
+ * Additionally all characters in the range 0x01-0x1F (SPACE and everything below)
+ * and in the range 0x7F-0xFF (all non-ASCII chars) are replaced with a '\' followed by their octal representation.
+ *
+ * Return value: (transfer full): The new string. Has to be freed with g_free after usage.
+ **/
+gchar*
+thunar_g_strescape (const gchar *source)
+{
+ gchar* g_escaped;
+ gchar* result;
+ unsigned int j = 0;
+ unsigned int new_size = 0;
+
+ /* First apply the default escaping .. will escape everything, expect SPACE */
+ g_escaped = g_strescape (source, NULL);
+
+ /* calc required new size */
+ for (unsigned int i = 0; i < strlen (g_escaped); i++)
+ {
+ if (g_escaped[i] == ' ')
+ new_size++;
+ new_size++;
+ }
+
+ /* strlen() does not include the \0 character, add an extra slot for it */
+ new_size++;
+ result = malloc (new_size * sizeof (gchar));
+
+ for (unsigned int i = 0; i < strlen (g_escaped); i++)
+ {
+ if (g_escaped[i] == ' ')
+ {
+ result[j] = '\\';
+ j++;
+ }
+ result[j] = g_escaped[i];
+ j++;
+ }
+ result[j] = '\0';
+ g_free (g_escaped);
+ return result;
+}
diff --git a/thunar/thunar-gobject-extensions.h b/thunar/thunar-gobject-extensions.h
index 1d34ef56..8940ea1a 100644
--- a/thunar/thunar-gobject-extensions.h
+++ b/thunar/thunar-gobject-extensions.h
@@ -37,7 +37,8 @@ G_BEGIN_DECLS;
#define G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec) G_STMT_START{ (void)0; }G_STMT_END
#endif
-void thunar_g_initialize_transformations (void);
+void thunar_g_initialize_transformations (void);
+gchar* thunar_g_strescape (const gchar *source);
G_END_DECLS;