[gnome-text-editor] filechooser: try to select a reasonable directory



commit aa97e54fb164be42ac2e93f6a489af80a349e1ff
Author: Christian Hergert <chergert redhat com>
Date:   Tue Nov 30 00:21:49 2021 -0800

    filechooser: try to select a reasonable directory
    
    Instead of always landing on Recent, it's nice to try to be a bit smarter
    here. If we have a document open, we can use the directory of that file
    first (as it's more likely to be closest).
    
    Otherwise, we can use the directory we last saved to.
    
    Additionally, when saving a new file, we can try to reuse the directory
    we last saved within.
    
    Fixes #232

 data/org.gnome.TextEditor.gschema.xml |  5 +++++
 src/editor-page.c                     | 20 ++++++++++++++++++++
 src/editor-window-actions.c           | 11 +++++++++++
 3 files changed, 36 insertions(+)
---
diff --git a/data/org.gnome.TextEditor.gschema.xml b/data/org.gnome.TextEditor.gschema.xml
index 75d05ec..a15b3cd 100644
--- a/data/org.gnome.TextEditor.gschema.xml
+++ b/data/org.gnome.TextEditor.gschema.xml
@@ -118,5 +118,10 @@
       <summary>Keybindings</summary>
       <description>The keybindings to use within Text Editor.</description>
     </key>
+    <key name="last-save-directory" type="s">
+      <default>''</default>
+      <summary>Last Save Directory</summary>
+      <description>The directory last used in a save or save-as dialog.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/editor-page.c b/src/editor-page.c
index 5e8caf8..64fb1e6 100644
--- a/src/editor-page.c
+++ b/src/editor-page.c
@@ -931,6 +931,14 @@ editor_page_save_as_cb (EditorPage           *self,
       if ((dest = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (native))))
         {
           GtkSourceNewlineType crlf = _editor_file_chooser_get_line_ending (GTK_FILE_CHOOSER (native));
+          g_autoptr(GFile) directory = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (native));
+
+          if (directory != NULL)
+            {
+              g_autoptr(GSettings) settings = g_settings_new ("org.gnome.TextEditor");
+              g_autofree char *uri = g_file_get_uri (directory);
+              g_settings_set_string (settings, "last-save-directory", uri);
+            }
 
           _editor_document_set_newline_type (self->document, crlf);
           _editor_document_save_async (self->document,
@@ -948,11 +956,19 @@ void
 _editor_page_save_as (EditorPage *self,
                       const char *filename)
 {
+  g_autoptr(GSettings) settings = NULL;
+  g_autoptr(GFile) directory = NULL;
   GtkFileChooserNative *native;
   EditorWindow *window;
+  g_autofree char *uri = NULL;
 
   g_return_if_fail (EDITOR_IS_PAGE (self));
 
+  settings = g_settings_new ("org.gnome.TextEditor");
+  uri = g_settings_get_string (settings, "last-save-directory");
+  if (uri && uri[0])
+    directory = g_file_new_for_uri (uri);
+
   _editor_page_raise (self);
 
   window = _editor_page_get_window (self);
@@ -983,6 +999,10 @@ _editor_page_save_as (EditorPage *self,
 
       gtk_file_chooser_set_file (GTK_FILE_CHOOSER (native), selected, NULL);
     }
+  else if (directory != NULL)
+    {
+      gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (native), directory, NULL);
+    }
 
   g_signal_connect_object (native,
                            "response",
diff --git a/src/editor-window-actions.c b/src/editor-window-actions.c
index ea37067..8900cf6 100644
--- a/src/editor-window-actions.c
+++ b/src/editor-window-actions.c
@@ -358,6 +358,17 @@ editor_window_actions_open_cb (GtkWidget  *widget,
       if (dir != NULL)
         gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (native), dir, NULL);
     }
+  else
+    {
+      g_autoptr(GSettings) settings = g_settings_new ("org.gnome.TextEditor");
+      g_autofree char *uri = g_settings_get_string (settings, "last-save-directory");
+
+      if (uri && uri[0])
+        {
+          g_autoptr(GFile) dir = g_file_new_for_uri (uri);
+          gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (native), dir, NULL);
+        }
+    }
 
   all_files = gtk_file_filter_new ();
   gtk_file_filter_set_name (all_files, _("All Files"));


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