diff --git a/thunar/thunar-chooser-dialog.c b/thunar/thunar-chooser-dialog.c index bb615a73d090c54bc015ff5caa0bc3a78fa4869d..05bdbb8c424dcf7ca7911e70a991d4792d83d465 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 f4cd32e32213d96fa1e8e67867d16ed4e34e692f..ec56b24ea212b15322e28ffee1936feb4a18ab10 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 1d34ef569df24f6805e5d76b004f4d9e689032de..8940ea1acef4b1fc28b9405cc53fc2c255017440 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;