[Xfce4-commits] <thunar:master> Change CWD before spawning applications in ThunarLauncher.

Jannis Pohlmann noreply at xfce.org
Thu Jan 28 21:18:01 CET 2010


Updating branch refs/heads/master
         to c70f431334ab8256087f43813240ce01cd028f34 (commit)
       from ad4d0cf0ac997e33e567e1b1553428dcbc08e660 (commit)

commit c70f431334ab8256087f43813240ce01cd028f34
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Thu Jan 28 19:48:18 2010 +0100

    Change CWD before spawning applications in ThunarLauncher.
    
    Before launching files with a GAppInfo, ThunarLauncher now changes the
    current working directory of the Thunar process to its own current
    directory, which usually also is the current directory of the window. It
    remembers the original directory Thunar was launched from and resets the
    CWD right after the files have been launched.
    
    This is done transparently by adding a function
    thunar_g_app_info_launch() that handles the CWD state internally. It
    calls another new function, thunar_util_change_working_directory() that
    is based on getcwd/_getcwd (the latter being used on Windows) and
    chdir().

 configure.in.in                |    4 +-
 thunar/thunar-gio-extensions.c |   55 +++++++++++++++++++++++++++++++++++
 thunar/thunar-gio-extensions.h |   62 ++++++++++++++++++++++------------------
 thunar/thunar-launcher.c       |    7 ++++-
 thunar/thunar-util.c           |   48 +++++++++++++++++++++++++++++++
 thunar/thunar-util.h           |   18 ++++++-----
 6 files changed, 155 insertions(+), 39 deletions(-)

diff --git a/configure.in.in b/configure.in.in
index d4cbc5d..b4fc97e 100644
--- a/configure.in.in
+++ b/configure.in.in
@@ -119,8 +119,8 @@ dnl *** Check for standard headers ***
 dnl **********************************
 AC_CHECK_HEADERS([ctype.h errno.h fcntl.h grp.h limits.h locale.h memory.h \
                   paths.h pwd.h sched.h signal.h stdarg.h stdlib.h string.h \
-                  sys/mman.h sys/stat.h sys/time.h sys/types.h sys/uio.h \
-                  sys/wait.h time.h])
+                  sys/mman.h sys/param.h sys/stat.h sys/time.h sys/types.h \
+                  sys/uio.h sys/wait.h time.h])
 
 dnl ************************************
 dnl *** Check for standard functions ***
diff --git a/thunar/thunar-gio-extensions.c b/thunar/thunar-gio-extensions.c
index 599ce87..f4b68e5 100644
--- a/thunar/thunar-gio-extensions.c
+++ b/thunar/thunar-gio-extensions.c
@@ -27,8 +27,10 @@
 #include <exo/exo.h>
 #include <libxfce4util/libxfce4util.h>
 
+#include <thunar/thunar-file.h>
 #include <thunar/thunar-gio-extensions.h>
 #include <thunar/thunar-private.h>
+#include <thunar/thunar-util.h>
 
 
 
@@ -467,3 +469,56 @@ thunar_g_volume_is_present (GVolume *volume)
 
   return has_media;
 }
+
+
+
+gboolean
+thunar_g_app_info_launch (GAppInfo          *info,
+                          GFile             *working_directory,
+                          GList             *path_list,
+                          GAppLaunchContext *context,
+                          GError           **error)
+{
+  gboolean result = FALSE;
+  gchar   *new_path = NULL;
+  gchar   *old_path = NULL;
+
+  _thunar_return_val_if_fail (G_IS_APP_INFO (info), FALSE);
+  _thunar_return_val_if_fail (working_directory == NULL || G_IS_FILE (working_directory), FALSE);
+  _thunar_return_val_if_fail (path_list != NULL, FALSE);
+  _thunar_return_val_if_fail (G_IS_APP_LAUNCH_CONTEXT (context), FALSE);
+  _thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  /* check if we want to set the working directory of the spawned app */
+  if (working_directory != NULL)
+    {
+      /* determine the working directory path */
+      new_path = g_file_get_path (working_directory);
+      if (new_path != NULL)
+        {
+          /* switch to the desired working directory, remember that of Thunar itself */
+          old_path = thunar_util_change_working_directory (new_path);
+
+          /* forget about the new working directory path */
+          g_free (new_path);
+        }
+    }
+
+  /* launch the paths with the specified app info */
+  result = g_app_info_launch (info, path_list, context, error);
+
+  /* check if we need to reset the working directory to the one Thunar was
+   * opened from */
+  if (old_path != NULL)
+    {
+      /* switch to Thunar's original working directory */
+      new_path = thunar_util_change_working_directory (old_path);
+
+      /* clean up */
+      g_free (new_path);
+      g_free (old_path);
+    }
+
+  return result;
+}
+
diff --git a/thunar/thunar-gio-extensions.h b/thunar/thunar-gio-extensions.h
index 69219cc..c5f3333 100644
--- a/thunar/thunar-gio-extensions.h
+++ b/thunar/thunar-gio-extensions.h
@@ -29,23 +29,23 @@ GFile    *thunar_g_file_new_for_home             (void);
 GFile    *thunar_g_file_new_for_root             (void);
 GFile    *thunar_g_file_new_for_trash            (void);
 GFile    *thunar_g_file_new_for_desktop          (void);
-GFile    *thunar_g_file_new_for_user_special_dir (GUserDirectory  dir);
-
-gboolean  thunar_g_file_is_root                  (GFile          *file);
-gboolean  thunar_g_file_is_trashed               (GFile          *file);
-gboolean  thunar_g_file_is_desktop               (GFile          *file);
-
-GKeyFile *thunar_g_file_query_key_file           (GFile          *file,
-                                                  GCancellable   *cancellable,
-                                                  GError        **error);
-gboolean  thunar_g_file_write_key_file           (GFile          *file,
-                                                  GKeyFile       *key_file,
-                                                  GCancellable   *cancellable,
-                                                  GError        **error);
-
-gchar    *thunar_g_file_get_location             (GFile          *file);
-
-gboolean  thunar_g_vfs_is_uri_scheme_supported   (const gchar    *scheme);
+GFile    *thunar_g_file_new_for_user_special_dir (GUserDirectory        dir);
+                                                                      
+gboolean  thunar_g_file_is_root                  (GFile                *file);
+gboolean  thunar_g_file_is_trashed               (GFile                *file);
+gboolean  thunar_g_file_is_desktop               (GFile                *file);
+                                                                      
+GKeyFile *thunar_g_file_query_key_file           (GFile                *file,
+                                                  GCancellable         *cancellable,
+                                                  GError              **error);
+gboolean  thunar_g_file_write_key_file           (GFile                *file,
+                                                  GKeyFile             *key_file,
+                                                  GCancellable         *cancellable,
+                                                  GError              **error);
+                                                                      
+gchar    *thunar_g_file_get_location             (GFile                *file);
+                                                                      
+gboolean  thunar_g_vfs_is_uri_scheme_supported   (const gchar          *scheme);
 
 /**
  * THUNAR_TYPE_G_FILE_LIST:
@@ -57,18 +57,24 @@ gboolean  thunar_g_vfs_is_uri_scheme_supported   (const gchar    *scheme);
 
 GType     thunar_g_file_list_get_type               (void);
 
-GList    *thunar_g_file_list_new_from_string        (const gchar *string);
-gchar    *thunar_g_file_list_to_string              (GList       *list);
-GList    *thunar_g_file_list_append                 (GList       *list,
-                                                     GFile       *file);
-GList    *thunar_g_file_list_prepend                (GList       *list,
-                                                     GFile       *file);
-GList    *thunar_g_file_list_copy                   (GList       *list);
-void      thunar_g_file_list_free                   (GList       *list);
+GList    *thunar_g_file_list_new_from_string        (const gchar       *string);
+gchar    *thunar_g_file_list_to_string              (GList             *list);
+GList    *thunar_g_file_list_append                 (GList             *list,
+                                                     GFile             *file);
+GList    *thunar_g_file_list_prepend                (GList             *list,
+                                                     GFile             *file);
+GList    *thunar_g_file_list_copy                   (GList             *list);
+void      thunar_g_file_list_free                   (GList             *list);
+
+gboolean  thunar_g_volume_is_removable              (GVolume           *volume);
+gboolean  thunar_g_volume_is_mounted                (GVolume           *volume);
+gboolean  thunar_g_volume_is_present                (GVolume           *volume);
 
-gboolean  thunar_g_volume_is_removable              (GVolume     *volume);
-gboolean  thunar_g_volume_is_mounted                (GVolume     *volume);
-gboolean  thunar_g_volume_is_present                (GVolume     *volume);
+gboolean  thunar_g_app_info_launch                  (GAppInfo          *info,
+                                                     GFile             *working_directory,
+                                                     GList             *path_list,
+                                                     GAppLaunchContext *context,
+                                                     GError           **error);
 
 G_END_DECLS
 
diff --git a/thunar/thunar-launcher.c b/thunar/thunar-launcher.c
index c34faaf..6b3a8f5 100644
--- a/thunar/thunar-launcher.c
+++ b/thunar/thunar-launcher.c
@@ -605,6 +605,7 @@ thunar_launcher_open_paths (GAppInfo       *app_info,
   GdkAppLaunchContext *context;
   GdkScreen           *screen;
   GError              *error = NULL;
+  GFile               *working_directory = NULL;
   gchar               *message;
   gchar               *name;
   guint                n;
@@ -616,8 +617,12 @@ thunar_launcher_open_paths (GAppInfo       *app_info,
   context = gdk_app_launch_context_new ();
   gdk_app_launch_context_set_screen (context, screen);
 
+  /* determine the working directory */
+  if (launcher->current_directory != NULL)
+    working_directory = thunar_file_get_file (launcher->current_directory);
+
   /* try to execute the application with the given URIs */
-  if (!g_app_info_launch (app_info, path_list, G_APP_LAUNCH_CONTEXT (context), &error))
+  if (!thunar_g_app_info_launch (app_info, working_directory, path_list, G_APP_LAUNCH_CONTEXT (context), &error))
     {
       /* figure out the appropriate error message */
       n = g_list_length (path_list);
diff --git a/thunar/thunar-util.c b/thunar/thunar-util.c
index 8cd2523..20d7a89 100644
--- a/thunar/thunar-util.c
+++ b/thunar/thunar-util.c
@@ -25,6 +25,9 @@
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
 
 #ifdef HAVE_MEMORY_H
 #include <memory.h>
@@ -41,6 +44,14 @@
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef G_PLATFORM_WIN32
+#include <direct.h>
+#include <glib/gwin32.h>
+#endif
 
 #include <thunar/thunar-private.h>
 #include <thunar/thunar-util.h>
@@ -352,3 +363,40 @@ thunar_util_time_from_rfc3339 (const gchar *date_string)
 }
 
 
+
+gchar *
+thunar_util_change_working_directory (const gchar *new_directory)
+{
+  gchar *old_directory;
+
+  _thunar_return_val_if_fail (new_directory != NULL && *new_directory != '\0', NULL);
+
+  /* allocate a path buffer for the old working directory */
+  old_directory = g_malloc0 (sizeof (gchar) * MAXPATHLEN);
+
+  /* try to determine the current working directory */
+#ifdef G_PLATFORM_WIN32
+  if (_getcwd (old_directory, MAXPATHLEN) == NULL)
+#else
+  if (getcwd (old_directory, MAXPATHLEN) == NULL)
+#endif
+    {
+      /* working directory couldn't be determined, reset the buffer */
+      g_free (old_directory);
+      old_directory = NULL;
+    }
+
+  /* try switching to the new working directory */
+#ifdef G_PLATFORM_WIN32
+  if (_chdir (new_directory) != 0)
+#else
+  if (chdir (new_directory) != 0)
+#endif
+    {
+      /* switching failed, we don't need to return the old directory */
+      g_free (old_directory);
+      old_directory = NULL;
+    }
+
+  return old_directory;
+}
diff --git a/thunar/thunar-util.h b/thunar/thunar-util.h
index e512b1e..a28a1b4 100644
--- a/thunar/thunar-util.h
+++ b/thunar/thunar-util.h
@@ -25,18 +25,20 @@
 
 G_BEGIN_DECLS;
 
-gboolean   thunar_util_looks_like_an_uri  (const gchar      *string) G_GNUC_WARN_UNUSED_RESULT;
+gboolean   thunar_util_looks_like_an_uri        (const gchar      *string) G_GNUC_WARN_UNUSED_RESULT;
 
-gchar     *thunar_util_expand_filename    (const gchar    *filename,
-                                           GError        **error);
+gchar     *thunar_util_expand_filename          (const gchar    *filename,
+                                                 GError        **error);
 
-gchar     *thunar_util_humanize_file_time (guint64         file_time,
-                                           ThunarDateStyle date_style) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+gchar     *thunar_util_humanize_file_time       (guint64         file_time,
+                                                 ThunarDateStyle date_style) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
-GdkScreen *thunar_util_parse_parent       (gpointer        parent,
-                                           GtkWindow     **window_return) G_GNUC_WARN_UNUSED_RESULT;
+GdkScreen *thunar_util_parse_parent             (gpointer        parent,
+                                                 GtkWindow     **window_return) G_GNUC_WARN_UNUSED_RESULT;
 
-time_t     thunar_util_time_from_rfc3339  (const gchar    *date_string) G_GNUC_WARN_UNUSED_RESULT;
+time_t     thunar_util_time_from_rfc3339        (const gchar    *date_string) G_GNUC_WARN_UNUSED_RESULT;
+
+gchar     *thunar_util_change_working_directory (const gchar    *new_directory) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
 
 G_END_DECLS;
 



More information about the Xfce4-commits mailing list