diff --git a/docs/reference/thunar-vfs/tmpl/thunar-vfs-util.sgml b/docs/reference/thunar-vfs/tmpl/thunar-vfs-util.sgml index db19b15..43c2f34 100644 --- a/docs/reference/thunar-vfs/tmpl/thunar-vfs-util.sgml +++ b/docs/reference/thunar-vfs/tmpl/thunar-vfs-util.sgml @@ -32,6 +32,7 @@ Utility Functions @filename: +@working_directory: @error: @Returns: diff --git a/thunar-vfs/thunar-vfs-util.c b/thunar-vfs/thunar-vfs-util.c index 83177dd..24b9768 100644 --- a/thunar-vfs/thunar-vfs-util.c +++ b/thunar-vfs/thunar-vfs-util.c @@ -222,11 +222,13 @@ thunar_vfs_canonicalize_filename (const gchar *filename) /** * thunar_vfs_expand_filename: - * @filename : a local filename. - * @error : return location for errors or %NULL. + * @filename : a local filename. + * @working_directory : #ThunarVfsPath of the current directory. + * @error : return location for errors or %NULL. * * Takes a user-typed @filename and expands a tilde at the - * beginning of the @filename. + * beginning of the @filename. It also takes care of paths with + * . at the beginning. * * The caller is responsible to free the returned string using * g_free() when no longer needed. @@ -234,13 +236,16 @@ thunar_vfs_canonicalize_filename (const gchar *filename) * Return value: the expanded @filename or %NULL on error. **/ gchar* -thunar_vfs_expand_filename (const gchar *filename, - GError **error) +thunar_vfs_expand_filename (const gchar *filename, + ThunarVfsPath *working_directory, + GError **error) { struct passwd *passwd; const gchar *replacement; const gchar *remainder; const gchar *slash; + gchar *pwd; + gchar *result; gchar *username; g_return_val_if_fail (filename != NULL, NULL); @@ -253,48 +258,76 @@ thunar_vfs_expand_filename (const gchar *filename, } /* check if we start with a '~' */ - if (G_LIKELY (*filename != '~')) - return g_strdup (filename); + if (G_LIKELY (*filename == '~')) + { + /* examine the remainder of the filename */ + remainder = filename + 1; - /* examine the remainder of the filename */ - remainder = filename + 1; + /* if we have only the slash, then we want the home dir */ + if (G_UNLIKELY (*remainder == '\0')) + return g_strdup (xfce_get_homedir ()); - /* if we have only the slash, then we want the home dir */ - if (G_UNLIKELY (*remainder == '\0')) - return g_strdup (xfce_get_homedir ()); + /* lookup the slash */ + for (slash = remainder; *slash != '\0' && *slash != G_DIR_SEPARATOR; ++slash) + ; - /* lookup the slash */ - for (slash = remainder; *slash != '\0' && *slash != G_DIR_SEPARATOR; ++slash) - ; + /* check if a username was given after the '~' */ + if (G_LIKELY (slash == remainder)) + { + /* replace the tilde with the home dir */ + replacement = xfce_get_homedir (); + } + else + { + /* lookup the pwd entry for the username */ + username = g_strndup (remainder, slash - remainder); + passwd = getpwnam (username); + g_free (username); - /* check if a username was given after the '~' */ - if (G_LIKELY (slash == remainder)) - { - /* replace the tilde with the home dir */ - replacement = xfce_get_homedir (); + /* check if we have a valid entry */ + if (G_UNLIKELY (passwd == NULL)) + { + username = g_strndup (remainder, slash - remainder); + g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, _("Unknown user \"%s\""), username); + g_free (username); + return NULL; + } + + /* use the homedir of the specified user */ + replacement = passwd->pw_dir; + } + + /* generate the filename */ + return g_build_filename (replacement, slash, G_DIR_SEPARATOR_S, NULL); } - else + else if (G_UNLIKELY (*filename == '.')) { - /* lookup the pwd entry for the username */ - username = g_strndup (remainder, slash - remainder); - passwd = getpwnam (username); - g_free (username); - - /* check if we have a valid entry */ - if (G_UNLIKELY (passwd == NULL)) + /* examine the remainder of the filename */ + remainder = filename + 1; + + /* transform working directory into a filename string */ + if (G_LIKELY (working_directory != NULL)) { - username = g_strndup (remainder, slash - remainder); - g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, _("Unknown user \"%s\""), username); - g_free (username); - return NULL; + pwd = thunar_vfs_path_dup_string (working_directory); + + /* if we only have the slash then we want the working directory only */ + if (G_UNLIKELY (*remainder == '\0')) + return pwd; + + /* concatenate working directory and remainder */ + result = g_build_filename (pwd, remainder, G_DIR_SEPARATOR_S, NULL); + + /* free the working directory string */ + g_free (pwd); } + else + result = g_strdup (filename); - /* use the homedir of the specified user */ - replacement = passwd->pw_dir; + /* return the resulting path string */ + return result; } - /* generate the filename */ - return g_build_filename (replacement, slash, NULL); + return g_strdup (filename); } diff --git a/thunar-vfs/thunar-vfs-util.h b/thunar-vfs/thunar-vfs-util.h index d2cd0ce..d65e498 100644 --- a/thunar-vfs/thunar-vfs-util.h +++ b/thunar-vfs/thunar-vfs-util.h @@ -23,12 +23,14 @@ #include #include +#include G_BEGIN_DECLS; gchar *thunar_vfs_canonicalize_filename (const gchar *filename) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; gchar *thunar_vfs_expand_filename (const gchar *filename, + ThunarVfsPath *working_directory, GError **error) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; gchar *thunar_vfs_humanize_size (ThunarVfsFileSize size, diff --git a/thunar/thunar-location-dialog.c b/thunar/thunar-location-dialog.c index 240ca91..67ace0e 100644 --- a/thunar/thunar-location-dialog.c +++ b/thunar/thunar-location-dialog.c @@ -167,3 +167,21 @@ thunar_location_dialog_set_selected_file (ThunarLocationDialog *location_dialog, } + +/** + * thunar_location_dialog_set_working_directory: + * @location_dialog : a #ThunarLocationDialog. + * @directory : a #ThunarFile or %NULL. + * + * Sets the working directory of @location_dialog to @directory. + **/ +void +thunar_location_dialog_set_working_directory (ThunarLocationDialog *location_dialog, + ThunarFile *directory) +{ + _thunar_return_if_fail (THUNAR_IS_LOCATION_DIALOG (location_dialog)); + _thunar_return_if_fail (directory == NULL || THUNAR_IS_FILE (directory)); + thunar_path_entry_set_working_directory (THUNAR_PATH_ENTRY (location_dialog->entry), directory); +} + + diff --git a/thunar/thunar-location-dialog.h b/thunar/thunar-location-dialog.h index bb8d17f..2e43f57 100644 --- a/thunar/thunar-location-dialog.h +++ b/thunar/thunar-location-dialog.h @@ -46,13 +46,15 @@ struct _ThunarLocationDialog GtkWidget *entry; }; -GType thunar_location_dialog_get_type (void) G_GNUC_CONST; +GType thunar_location_dialog_get_type (void) G_GNUC_CONST; -GtkWidget *thunar_location_dialog_new (void) G_GNUC_MALLOC; +GtkWidget *thunar_location_dialog_new (void) G_GNUC_MALLOC; -ThunarFile *thunar_location_dialog_get_selected_file (ThunarLocationDialog *location_dialog); -void thunar_location_dialog_set_selected_file (ThunarLocationDialog *location_dialog, - ThunarFile *selected_file); +ThunarFile *thunar_location_dialog_get_selected_file (ThunarLocationDialog *location_dialog); +void thunar_location_dialog_set_selected_file (ThunarLocationDialog *location_dialog, + ThunarFile *selected_file); +void thunar_location_dialog_set_working_directory (ThunarLocationDialog *location_dialog, + ThunarFile *directory); G_END_DECLS; diff --git a/thunar/thunar-location-entry.c b/thunar/thunar-location-entry.c index 3158bab..e5373a9 100644 --- a/thunar/thunar-location-entry.c +++ b/thunar/thunar-location-entry.c @@ -320,6 +320,8 @@ thunar_location_entry_set_property (GObject *object, { case PROP_CURRENT_DIRECTORY: thunar_navigator_set_current_directory (THUNAR_NAVIGATOR (object), g_value_get_object (value)); + thunar_path_entry_set_working_directory (THUNAR_PATH_ENTRY (THUNAR_LOCATION_ENTRY (object)->path_entry), + THUNAR_LOCATION_ENTRY (object)->current_directory); break; case PROP_SELECTED_FILES: diff --git a/thunar/thunar-path-entry.c b/thunar/thunar-path-entry.c index 8bc31e6..fead0f7 100644 --- a/thunar/thunar-path-entry.c +++ b/thunar/thunar-path-entry.c @@ -143,6 +143,7 @@ struct _ThunarPathEntry ThunarIconFactory *icon_factory; ThunarFile *current_folder; ThunarFile *current_file; + ThunarVfsPath *working_directory; GdkWindow *icon_area; gint drag_button; @@ -282,6 +283,7 @@ thunar_path_entry_init (ThunarPathEntry *path_entry) ThunarListModel *store; path_entry->check_completion_idle_id = -1; + path_entry->working_directory = NULL; /* allocate a new entry completion for the given model */ completion = gtk_entry_completion_new (); @@ -343,6 +345,9 @@ thunar_path_entry_finalize (GObject *object) g_object_unref (G_OBJECT (path_entry->current_file)); } + if (G_LIKELY (path_entry->working_directory != NULL)) + thunar_vfs_path_unref (path_entry->working_directory); + /* drop the check_completion_idle source */ if (G_UNLIKELY (path_entry->check_completion_idle_id >= 0)) g_source_remove (path_entry->check_completion_idle_id); @@ -1253,7 +1258,9 @@ thunar_path_entry_parse (ThunarPathEntry *path_entry, _thunar_return_val_if_fail (file_part != NULL, FALSE); /* expand the filename */ - filename = thunar_vfs_expand_filename (gtk_entry_get_text (GTK_ENTRY (path_entry)), error); + filename = thunar_vfs_expand_filename (gtk_entry_get_text (GTK_ENTRY (path_entry)), + path_entry->working_directory, error); + g_debug ("filename = %s", filename); if (G_UNLIKELY (filename == NULL)) return FALSE; @@ -1431,3 +1438,29 @@ uri: /* display the URI for the path */ gtk_widget_queue_draw (GTK_WIDGET (path_entry)); } + + +/** + * thunar_path_entry_set_working_directory: + * @path_entry : a #ThunarPathEntry. + * @working_directory : a #ThunarFile or %NULL. + * + * Sets the #ThunarFile that should be used as the + * working directory for @path_entry. + **/ +void +thunar_path_entry_set_working_directory (ThunarPathEntry *path_entry, + ThunarFile *working_directory) +{ + _thunar_return_if_fail (THUNAR_IS_PATH_ENTRY (path_entry)); + _thunar_return_if_fail (working_directory == NULL || THUNAR_IS_FILE (working_directory)); + + if (G_LIKELY (path_entry->working_directory != NULL)) + thunar_vfs_path_unref (path_entry->working_directory); + + path_entry->working_directory = NULL; + + if (THUNAR_IS_FILE (working_directory)) + path_entry->working_directory = thunar_vfs_path_ref (thunar_file_get_path (working_directory)); +} + diff --git a/thunar/thunar-path-entry.h b/thunar/thunar-path-entry.h index d3ce081..404f08c 100644 --- a/thunar/thunar-path-entry.h +++ b/thunar/thunar-path-entry.h @@ -34,13 +34,15 @@ typedef struct _ThunarPathEntry ThunarPathEntry; #define THUNAR_IS_PATH_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), THUNAR_TYPE_PATH_ENTRY)) #define THUNAR_PATH_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THUNAR_TYPE_PATH_ENTRY, ThunarPathEntryClass)) -GType thunar_path_entry_get_type (void) G_GNUC_CONST; +GType thunar_path_entry_get_type (void) G_GNUC_CONST; -GtkWidget *thunar_path_entry_new (void); +GtkWidget *thunar_path_entry_new (void); -ThunarFile *thunar_path_entry_get_current_file (ThunarPathEntry *path_entry); -void thunar_path_entry_set_current_file (ThunarPathEntry *path_entry, - ThunarFile *current_file); +ThunarFile *thunar_path_entry_get_current_file (ThunarPathEntry *path_entry); +void thunar_path_entry_set_current_file (ThunarPathEntry *path_entry, + ThunarFile *current_file); +void thunar_path_entry_set_working_directory (ThunarPathEntry *path_entry, + ThunarFile *directory); G_END_DECLS; diff --git a/thunar/thunar-window.c b/thunar/thunar-window.c index b45c0b6..060b862 100644 --- a/thunar/thunar-window.c +++ b/thunar/thunar-window.c @@ -1437,6 +1437,7 @@ thunar_window_start_open_location (ThunarWindow *window, gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE); gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window)); + thunar_location_dialog_set_working_directory (THUNAR_LOCATION_DIALOG (dialog), thunar_window_get_current_directory (window)); thunar_location_dialog_set_selected_file (THUNAR_LOCATION_DIALOG (dialog), thunar_window_get_current_directory (window)); /* setup the initial text (if any) */