summaryrefslogtreecommitdiff
path: root/gtk
diff options
context:
space:
mode:
authorFederico Mena Quintero <federico@ximian.com>2004-04-07 17:52:03 +0000
committerFederico Mena Quintero <federico@src.gnome.org>2004-04-07 17:52:03 +0000
commitd903e89ece448f92a133adfc918eca8de5a15f05 (patch)
treeb019f95f90507d768f7dcf7b44b574581ae5d566 /gtk
parentac5b1b3193dfff36ecc90f2e614166aabbbeda91 (diff)
downloadgtk+-d903e89ece448f92a133adfc918eca8de5a15f05.tar.gz
Fix #132500.
2004-04-07 Federico Mena Quintero <federico@ximian.com> 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.
Diffstat (limited to 'gtk')
-rw-r--r--gtk/gtkfilechooserentry.c43
-rw-r--r--gtk/gtkfilesystem.c2
-rw-r--r--gtk/gtkfilesystemunix.c64
3 files changed, 86 insertions, 23 deletions
diff --git a/gtk/gtkfilechooserentry.c b/gtk/gtkfilechooserentry.c
index b967ea011c..9c7380d5e2 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 af5f537e6a..52a8b13a50 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 9d4f63eb88..2357bafcfa 100644
--- a/gtk/gtkfilesystemunix.c
+++ b/gtk/gtkfilesystemunix.c
@@ -32,6 +32,7 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
@@ -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;
}