[gnome-terminal/wip/rishi/preserve-working-directory: 2/2] profile: editor: Add setting to preserve the working directory



commit a5871f6d00aa95fad3b8dafdae6ea752ac73b93b
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri May 24 17:14:19 2019 +0200

    profile: editor: Add setting to preserve the working directory
    
    The default value of the setting is 'nothing', which retains the
    current behaviour of preserving the working directory only when the
    command is a shell.
    
    Therefore, unless explicitly changed, the 'preserve working directory'
    check-button is kept synchronized with the 'use custom command'
    check-button without altering the value of the underlying setting. To
    make this work, an explicit signal handler is used to track changes to
    the 'preserve working directory' check-button, as opposed to binding
    it directly to the setting. This ensures that changes to the button
    not initiated by user action don't modify the setting.
    
    https://gitlab.gnome.org/GNOME/gnome-terminal/issues/126

 src/org.gnome.Terminal.gschema.xml |  5 ++++
 src/preferences.ui                 | 27 +++++++++++++----
 src/profile-editor.c               | 60 ++++++++++++++++++++++++++++++++++++++
 src/terminal-schemas.h             |  1 +
 src/terminal-screen.c              |  2 +-
 src/terminal-util.c                | 35 ++++++++++++++++++++++
 src/terminal-util.h                |  2 ++
 7 files changed, 125 insertions(+), 7 deletions(-)
---
diff --git a/src/org.gnome.Terminal.gschema.xml b/src/org.gnome.Terminal.gschema.xml
index e4e0c003..39ac8827 100644
--- a/src/org.gnome.Terminal.gschema.xml
+++ b/src/org.gnome.Terminal.gschema.xml
@@ -247,6 +247,11 @@
       <summary>Whether to launch the command in the terminal as a login shell</summary>
       <description>If true, the command inside the terminal will be launched as a login shell (argv[0] will 
have a hyphen in front of it).</description>
     </key>
+    <key name="preserve-working-directory" type="mb">
+      <default>nothing</default>
+      <summary>Whether to preserve the working directory when opening a new terminal</summary>
+      <description>If true, when opening a new terminal from a previous one the working directory of the 
older terminal is carried over to the new one.</description>
+    </key>
     <key name="use-custom-command" type="b">
       <default>false</default>
       <summary>Whether to run a custom command instead of the shell</summary>
diff --git a/src/preferences.ui b/src/preferences.ui
index f42b652c..a7bf9d36 100644
--- a/src/preferences.ui
+++ b/src/preferences.ui
@@ -1817,6 +1817,21 @@
                             <property name="border_width">12</property>
                             <property name="column_spacing">12</property>
                             <property name="row_spacing">6</property>
+                            <child>
+                              <object class="GtkCheckButton" id="preserve-working-directory-checkbutton">
+                                <property name="label" translatable="yes">_Preserve working 
directory</property>
+                                <property name="visible">True</property>
+                                <property name="can_focus">True</property>
+                                <property name="receives_default">False</property>
+                                <property name="use_underline">True</property>
+                                <property name="draw_indicator">True</property>
+                              </object>
+                              <packing>
+                                <property name="top_attach">0</property>
+                                <property name="left_attach">0</property>
+                                <property name="width">2</property>
+                              </packing>
+                            </child>
                             <child>
                               <object class="GtkCheckButton" id="login-shell-checkbutton">
                                 <property name="label" translatable="yes">_Run command as a login 
shell</property>
@@ -1827,7 +1842,7 @@
                                 <property name="draw_indicator">True</property>
                               </object>
                               <packing>
-                                <property name="top_attach">0</property>
+                                <property name="top_attach">1</property>
                                 <property name="left_attach">0</property>
                                 <property name="width">2</property>
                               </packing>
@@ -1842,7 +1857,7 @@
                                 <property name="draw_indicator">True</property>
                               </object>
                               <packing>
-                                <property name="top_attach">1</property>
+                                <property name="top_attach">2</property>
                                 <property name="left_attach">0</property>
                                 <property name="width">2</property>
                               </packing>
@@ -1857,7 +1872,7 @@
                                 <property name="xalign">0</property>
                               </object>
                               <packing>
-                                <property name="top_attach">2</property>
+                                <property name="top_attach">3</property>
                                 <property name="left_attach">0</property>
                               </packing>
                             </child>
@@ -1868,7 +1883,7 @@
                                 <property name="hexpand">True</property>
                               </object>
                               <packing>
-                                <property name="top_attach">2</property>
+                                <property name="top_attach">3</property>
                                 <property name="left_attach">1</property>
                               </packing>
                             </child>
@@ -1882,7 +1897,7 @@
                                 <property name="xalign">0</property>
                               </object>
                               <packing>
-                                <property name="top_attach">3</property>
+                                <property name="top_attach">4</property>
                                 <property name="left_attach">0</property>
                               </packing>
                             </child>
@@ -1901,7 +1916,7 @@
                                 </child>
                               </object>
                               <packing>
-                                <property name="top_attach">3</property>
+                                <property name="top_attach">4</property>
                                 <property name="left_attach">1</property>
                               </packing>
                             </child>
diff --git a/src/profile-editor.c b/src/profile-editor.c
index 1912c8cf..56a8b116 100644
--- a/src/profile-editor.c
+++ b/src/profile-editor.c
@@ -639,6 +639,47 @@ init_encodings_combo (GtkWidget *widget)
                                   "text", ENCODINGS_COLUMN_TEXT, NULL);
 }
 
+static void
+profile_preserve_working_directory_checkbutton_cb (GtkToggleButton *checkbutton, gpointer user_data)
+{
+  GSettings *profile = G_SETTINGS (user_data);
+  gboolean preserve_working_directory;
+
+  preserve_working_directory = gtk_toggle_button_get_active (checkbutton);
+  g_settings_set (profile, TERMINAL_PROFILE_PRESERVE_WORKING_DIRECTORY_KEY, "mb", TRUE, 
preserve_working_directory);
+}
+
+static void
+profile_update_preserve_working_directory_checkbutton (GtkWidget *checkbutton, GSettings *profile)
+{
+  gboolean preserve_working_directory;
+
+  preserve_working_directory = terminal_util_get_preserve_working_directory (profile);
+
+  g_signal_handlers_block_by_func (checkbutton, profile_preserve_working_directory_checkbutton_cb, profile);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbutton), preserve_working_directory);
+  g_signal_handlers_unblock_by_func (checkbutton, profile_preserve_working_directory_checkbutton_cb, 
profile);
+}
+
+static void
+profile_use_custom_command_setting_cb (GSettings *profile, const gchar *key, gpointer user_data)
+{
+
+  GtkWidget *checkbutton = GTK_WIDGET (user_data);
+  profile_update_preserve_working_directory_checkbutton (checkbutton, profile);
+}
+
+static gboolean
+preserve_working_directory_to_bool (GValue *value, GVariant *variant, gpointer user_data)
+{
+  GSettings *profile = G_SETTINGS (user_data);
+  gboolean preserve_working_directory;
+
+  preserve_working_directory = terminal_util_get_preserve_working_directory (profile);
+  g_value_set_boolean (value, preserve_working_directory);
+  return TRUE;
+}
+
 static gboolean
 s_to_rgba (GValue *value,
            GVariant *variant,
@@ -1201,10 +1242,29 @@ profile_prefs_load (const char *uuid, GSettings *profile)
                                "active",
                                G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET |
                                G_SETTINGS_BIND_INVERT_BOOLEAN);
+
+  w = (GtkWidget *) gtk_builder_get_object (builder, "preserve-working-directory-checkbutton");
+  profile_update_preserve_working_directory_checkbutton (w, profile);
+  profile_prefs_signal_connect (w, "toggled",
+                                G_CALLBACK (profile_preserve_working_directory_checkbutton_cb),
+                                profile);
+  profile_prefs_signal_connect (profile, "changed::" TERMINAL_PROFILE_USE_CUSTOM_COMMAND_KEY,
+                                G_CALLBACK (profile_use_custom_command_setting_cb),
+                                w);
+  profile_prefs_settings_bind_with_mapping (profile,
+                                            TERMINAL_PROFILE_PRESERVE_WORKING_DIRECTORY_KEY,
+                                            w,
+                                            "active",
+                                            G_SETTINGS_BIND_GET,
+                                            (GSettingsBindGetMapping) preserve_working_directory_to_bool,
+                                            NULL,
+                                            profile,
+                                            NULL);
   profile_prefs_settings_bind (profile, TERMINAL_PROFILE_USE_CUSTOM_COMMAND_KEY,
                                gtk_builder_get_object (builder,
                                                        "use-custom-command-checkbutton"),
                                "active", G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);
+
   profile_prefs_settings_bind (profile, TERMINAL_PROFILE_USE_THEME_COLORS_KEY,
                                gtk_builder_get_object (builder,
                                                        "use-theme-colors-checkbutton"),
diff --git a/src/terminal-schemas.h b/src/terminal-schemas.h
index ef30bd59..bd091051 100644
--- a/src/terminal-schemas.h
+++ b/src/terminal-schemas.h
@@ -59,6 +59,7 @@ G_BEGIN_DECLS
 #define TERMINAL_PROFILE_LOGIN_SHELL_KEY                "login-shell"
 #define TERMINAL_PROFILE_NAME_KEY                       "name"
 #define TERMINAL_PROFILE_PALETTE_KEY                    "palette"
+#define TERMINAL_PROFILE_PRESERVE_WORKING_DIRECTORY_KEY "preserve-working-directory"
 #define TERMINAL_PROFILE_REWRAP_ON_RESIZE_KEY           "rewrap-on-resize"
 #define TERMINAL_PROFILE_SCROLLBACK_LINES_KEY           "scrollback-lines"
 #define TERMINAL_PROFILE_SCROLLBACK_UNLIMITED_KEY       "scrollback-unlimited"
diff --git a/src/terminal-screen.c b/src/terminal-screen.c
index 23134e80..8249c732 100644
--- a/src/terminal-screen.c
+++ b/src/terminal-screen.c
@@ -1403,7 +1403,7 @@ terminal_screen_do_exec (TerminalScreen *screen,
   profile = priv->profile;
 
   if (priv->initial_working_directory &&
-      !g_settings_get_boolean (profile, TERMINAL_PROFILE_USE_CUSTOM_COMMAND_KEY))
+      terminal_util_get_preserve_working_directory (profile))
     working_dir = priv->initial_working_directory;
   else
     working_dir = g_get_home_dir ();
diff --git a/src/terminal-util.c b/src/terminal-util.c
index 72a0d401..aa2ee0fc 100644
--- a/src/terminal-util.c
+++ b/src/terminal-util.c
@@ -40,6 +40,7 @@
 #include "terminal-accels.h"
 #include "terminal-app.h"
 #include "terminal-intl.h"
+#include "terminal-schemas.h"
 #include "terminal-util.h"
 #include "terminal-libgsystem.h"
 
@@ -1429,3 +1430,37 @@ terminal_util_save_print_settings (GtkPrintSettings *settings,
 
   save_cache_keyfile (keyfile, TERMINAL_PRINT_SETTINGS_FILENAME);
 }
+
+/**
+ * terminal_util_get_preserve_working_directory:
+ * @settings: a #GSettings
+ *
+ * Looks at the "custom-command" profile setting to resolve the value
+ * of the "preserve-working-directory" setting, if it hasn't been
+ * explicitly set.
+ *
+ * Returns: Whether the working directory of the current terminal
+ * should be used when creating a new terminal tab or window.
+ */
+gboolean
+terminal_util_get_preserve_working_directory (GSettings *profile)
+{
+  gboolean preserve_working_directory;
+  gboolean preserve_working_directory_set;
+
+  g_settings_get (profile,
+                  TERMINAL_PROFILE_PRESERVE_WORKING_DIRECTORY_KEY,
+                  "mb",
+                  &preserve_working_directory_set,
+                  &preserve_working_directory);
+
+  if (!preserve_working_directory_set)
+    {
+      gboolean use_custom_command;
+
+      use_custom_command = g_settings_get_boolean (profile, TERMINAL_PROFILE_USE_CUSTOM_COMMAND_KEY);
+      preserve_working_directory = !use_custom_command;
+    }
+
+  return preserve_working_directory;
+}
diff --git a/src/terminal-util.h b/src/terminal-util.h
index abd34fd7..aabf55ae 100644
--- a/src/terminal-util.h
+++ b/src/terminal-util.h
@@ -108,6 +108,8 @@ void terminal_util_load_print_settings (GtkPrintSettings **settings,
 void terminal_util_save_print_settings (GtkPrintSettings *settings,
                                         GtkPageSetup *page_setup);
 
+gboolean terminal_util_get_preserve_working_directory (GSettings *profile);
+
 G_END_DECLS
 
 #endif /* TERMINAL_UTIL_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]