From d903e89ece448f92a133adfc918eca8de5a15f05 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Wed, 7 Apr 2004 17:52:03 +0000 Subject: [PATCH] Fix #132500. 2004-04-07 Federico Mena Quintero Fix #132500. * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the passed-in 'str' is not NULL. * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; expands "~/" or "~foo/" at the beginning of a filename. (gtk_file_system_unix_parse): Use expand_tilde() before doing anything else. * gtk/gtkfilechooserentry.c (gtk_file_chooser_entry_maybe_update_directory): Take in a force_reload argument. (gtk_file_chooser_entry_changed): If gtk_file_system_parse() returns an error, set the file_part_pos to -1. (load_directory_callback): Only populate the model if the file_part_pos is not -1. --- ChangeLog | 20 ++++++++++++ ChangeLog.pre-2-10 | 20 ++++++++++++ ChangeLog.pre-2-4 | 20 ++++++++++++ ChangeLog.pre-2-6 | 20 ++++++++++++ ChangeLog.pre-2-8 | 20 ++++++++++++ gtk/gtkfilechooserentry.c | 43 +++++++++++++++----------- gtk/gtkfilesystem.c | 2 +- gtk/gtkfilesystemunix.c | 64 ++++++++++++++++++++++++++++++++++++--- 8 files changed, 186 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9ab4a010b..856430f62 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2004-04-07 Federico Mena Quintero + + Fix #132500. + + * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the + passed-in 'str' is not NULL. + + * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; + expands "~/" or "~foo/" at the beginning of a filename. + (gtk_file_system_unix_parse): Use expand_tilde() before doing + anything else. + + * gtk/gtkfilechooserentry.c + (gtk_file_chooser_entry_maybe_update_directory): Take in a + force_reload argument. + (gtk_file_chooser_entry_changed): If gtk_file_system_parse() + returns an error, set the file_part_pos to -1. + (load_directory_callback): Only populate the model if the + file_part_pos is not -1. + 2004-04-06 Pablo Saratxaga * configure.in: Added Icelandic (is) to ALL_LINGUAS diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 9ab4a010b..856430f62 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,23 @@ +2004-04-07 Federico Mena Quintero + + Fix #132500. + + * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the + passed-in 'str' is not NULL. + + * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; + expands "~/" or "~foo/" at the beginning of a filename. + (gtk_file_system_unix_parse): Use expand_tilde() before doing + anything else. + + * gtk/gtkfilechooserentry.c + (gtk_file_chooser_entry_maybe_update_directory): Take in a + force_reload argument. + (gtk_file_chooser_entry_changed): If gtk_file_system_parse() + returns an error, set the file_part_pos to -1. + (load_directory_callback): Only populate the model if the + file_part_pos is not -1. + 2004-04-06 Pablo Saratxaga * configure.in: Added Icelandic (is) to ALL_LINGUAS diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 9ab4a010b..856430f62 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,23 @@ +2004-04-07 Federico Mena Quintero + + Fix #132500. + + * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the + passed-in 'str' is not NULL. + + * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; + expands "~/" or "~foo/" at the beginning of a filename. + (gtk_file_system_unix_parse): Use expand_tilde() before doing + anything else. + + * gtk/gtkfilechooserentry.c + (gtk_file_chooser_entry_maybe_update_directory): Take in a + force_reload argument. + (gtk_file_chooser_entry_changed): If gtk_file_system_parse() + returns an error, set the file_part_pos to -1. + (load_directory_callback): Only populate the model if the + file_part_pos is not -1. + 2004-04-06 Pablo Saratxaga * configure.in: Added Icelandic (is) to ALL_LINGUAS diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 9ab4a010b..856430f62 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,23 @@ +2004-04-07 Federico Mena Quintero + + Fix #132500. + + * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the + passed-in 'str' is not NULL. + + * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; + expands "~/" or "~foo/" at the beginning of a filename. + (gtk_file_system_unix_parse): Use expand_tilde() before doing + anything else. + + * gtk/gtkfilechooserentry.c + (gtk_file_chooser_entry_maybe_update_directory): Take in a + force_reload argument. + (gtk_file_chooser_entry_changed): If gtk_file_system_parse() + returns an error, set the file_part_pos to -1. + (load_directory_callback): Only populate the model if the + file_part_pos is not -1. + 2004-04-06 Pablo Saratxaga * configure.in: Added Icelandic (is) to ALL_LINGUAS diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 9ab4a010b..856430f62 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,23 @@ +2004-04-07 Federico Mena Quintero + + Fix #132500. + + * gtk/gtkfilesystem.c (gtk_file_system_parse): Ensure that the + passed-in 'str' is not NULL. + + * gtk/gtkfilesystemunix.c (expand_tilde): New helper function; + expands "~/" or "~foo/" at the beginning of a filename. + (gtk_file_system_unix_parse): Use expand_tilde() before doing + anything else. + + * gtk/gtkfilechooserentry.c + (gtk_file_chooser_entry_maybe_update_directory): Take in a + force_reload argument. + (gtk_file_chooser_entry_changed): If gtk_file_system_parse() + returns an error, set the file_part_pos to -1. + (load_directory_callback): Only populate the model if the + file_part_pos is not -1. + 2004-04-06 Pablo Saratxaga * configure.in: Added Icelandic (is) to ALL_LINGUAS diff --git a/gtk/gtkfilechooserentry.c b/gtk/gtkfilechooserentry.c index b967ea011..9c7380d5e 100644 --- a/gtk/gtkfilechooserentry.c +++ b/gtk/gtkfilechooserentry.c @@ -574,19 +574,22 @@ load_directory_callback (GtkFileChooserEntry *chooser_entry) g_signal_connect (chooser_entry->current_folder, "files-removed", G_CALLBACK (files_deleted_cb), chooser_entry); - gtk_file_folder_list_children (chooser_entry->current_folder, - &child_paths, - NULL); /* NULL-GError */ chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, GTK_TYPE_FILE_PATH); - if (child_paths) + if (chooser_entry->file_part_pos != -1) { - update_current_folder_files (chooser_entry, child_paths); - add_completion_idle (chooser_entry); - gtk_file_paths_free (child_paths); - } + gtk_file_folder_list_children (chooser_entry->current_folder, + &child_paths, + NULL); /* NULL-GError */ + if (child_paths) + { + update_current_folder_files (chooser_entry, child_paths); + add_completion_idle (chooser_entry); + gtk_file_paths_free (child_paths); + } + } gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), GTK_TREE_MODEL (chooser_entry->completion_store)); @@ -657,13 +660,14 @@ gtk_file_chooser_entry_activate (GtkEntry *entry) */ static void gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry, - GtkFilePath *folder_path) + GtkFilePath *folder_path, + gboolean force_reload) { gboolean queue_idle = FALSE; if (chooser_entry->current_folder_path) { - if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0) + if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0 || force_reload) { /* We changed our current directory. We need to clear out the old * directory information. @@ -736,22 +740,25 @@ gtk_file_chooser_entry_changed (GtkEditable *editable) { folder_path = gtk_file_path_copy (chooser_entry->base_folder); file_part = g_strdup (""); + file_part_pos = -1; } - - file_part_len = strlen (file_part); - total_len = strlen (text); - if (total_len > file_part_len) - file_part_pos = g_utf8_strlen (text, total_len - file_part_len); else - file_part_pos = 0; - - gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path); + { + file_part_len = strlen (file_part); + total_len = strlen (text); + if (total_len > file_part_len) + file_part_pos = g_utf8_strlen (text, total_len - file_part_len); + else + file_part_pos = 0; + } if (chooser_entry->file_part) g_free (chooser_entry->file_part); chooser_entry->file_part = file_part; chooser_entry->file_part_pos = file_part_pos; + + gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path, file_part_pos == -1); } static void diff --git a/gtk/gtkfilesystem.c b/gtk/gtkfilesystem.c index af5f537e6..52a8b13a5 100644 --- a/gtk/gtkfilesystem.c +++ b/gtk/gtkfilesystem.c @@ -592,9 +592,9 @@ gtk_file_system_parse (GtkFileSystem *file_system, g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE); g_return_val_if_fail (base_path != NULL, FALSE); + g_return_val_if_fail (str != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - result = GTK_FILE_SYSTEM_GET_IFACE (file_system)->parse (file_system, base_path, str, &tmp_folder, &tmp_file_part, error); diff --git a/gtk/gtkfilesystemunix.c b/gtk/gtkfilesystemunix.c index 9d4f63eb8..2357bafcf 100644 --- a/gtk/gtkfilesystemunix.c +++ b/gtk/gtkfilesystemunix.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -876,6 +877,48 @@ canonicalize_filename (gchar *filename) *q = '\0'; } +/* Takes a user-typed filename and expands a tilde at the beginning of the string */ +static char * +expand_tilde (const char *filename) +{ + const char *notilde; + const char *slash; + const char *home; + + if (filename[0] != '~') + return g_strdup (filename); + + notilde = filename + 1; + + slash = strchr (notilde, G_DIR_SEPARATOR); + if (!slash) + return NULL; + + if (slash == notilde) + { + home = g_get_home_dir (); + + if (!home) + return g_strdup (filename); + } + else + { + char *username; + struct passwd *passwd; + + username = g_strndup (notilde, slash - notilde); + passwd = getpwnam (username); + g_free (username); + + if (!passwd) + return g_strdup (filename); + + home = passwd->pw_dir; + } + + return g_build_filename (home, G_DIR_SEPARATOR_S, slash + 1, NULL); +} + static gboolean gtk_file_system_unix_parse (GtkFileSystem *file_system, const GtkFilePath *base_path, @@ -885,6 +928,7 @@ gtk_file_system_unix_parse (GtkFileSystem *file_system, GError **error) { const char *base_filename; + gchar *filename; gchar *last_slash; gboolean result = FALSE; @@ -892,11 +936,21 @@ gtk_file_system_unix_parse (GtkFileSystem *file_system, g_return_val_if_fail (base_filename != NULL, FALSE); g_return_val_if_fail (g_path_is_absolute (base_filename), FALSE); - last_slash = strrchr (str, G_DIR_SEPARATOR); + filename = expand_tilde (str); + if (!filename) + { + g_set_error (error, + GTK_FILE_SYSTEM_ERROR, + GTK_FILE_SYSTEM_ERROR_BAD_FILENAME, + "%s", ""); /* nothing for now, as we are string-frozen */ + return FALSE; + } + + last_slash = strrchr (filename, G_DIR_SEPARATOR); if (!last_slash) { *folder = gtk_file_path_copy (base_path); - *file_part = g_strdup (str); + *file_part = g_strdup (filename); result = TRUE; } else @@ -905,10 +959,10 @@ gtk_file_system_unix_parse (GtkFileSystem *file_system, gchar *folder_path; GError *tmp_error = NULL; - if (last_slash == str) + if (last_slash == filename) folder_part = g_strdup ("/"); else - folder_part = g_filename_from_utf8 (str, last_slash - str, + folder_part = g_filename_from_utf8 (filename, last_slash - filename, NULL, NULL, &tmp_error); if (!folder_part) @@ -941,6 +995,8 @@ gtk_file_system_unix_parse (GtkFileSystem *file_system, } } + g_free (filename); + return result; } -- 2.43.2