[evince] shell: Store and restore current folder when opening or saving file



commit f6137df22503d88a76cc69f622919ec7bd3582d7
Author: Christian Persch <chpe gnome org>
Date:   Fri Jul 6 17:39:31 2012 +0200

    shell: Store and restore current folder when opening or saving file
    
    Remember the folder last used to open or save a document, attachment
    or image.

 data/org.gnome.Evince.gschema.xml.in |    8 ++
 shell/ev-window.c                    |  144 ++++++++++++++++++++++++++--------
 2 files changed, 120 insertions(+), 32 deletions(-)
---
diff --git a/data/org.gnome.Evince.gschema.xml.in b/data/org.gnome.Evince.gschema.xml.in
index 06384d4..3de283a 100644
--- a/data/org.gnome.Evince.gschema.xml.in
+++ b/data/org.gnome.Evince.gschema.xml.in
@@ -17,6 +17,14 @@
       <_summary>Automatically reload the document</_summary>
       <_description>The document is automatically reloaded on file change.</_description>
     </key>
+    <key name="document-directory" type="ms">
+      <default>nothing</default>
+      <_summary>The URI of the directory last used to open or save a document</_summary>
+    </key>
+    <key name="pictures-directory" type="ms">
+      <default>nothing</default>
+      <_summary>The URI of the directory last used to save a picture</_summary>
+    </key>
     <child name="default" schema="org.gnome.Evince.Default"/>
   </schema>
 
diff --git a/shell/ev-window.c b/shell/ev-window.c
index c8bd717..7f068af 100644
--- a/shell/ev-window.c
+++ b/shell/ev-window.c
@@ -246,6 +246,8 @@ struct _EvWindowPrivate {
 #define GS_SCHEMA_NAME           "org.gnome.Evince"
 #define GS_OVERRIDE_RESTRICTIONS "override-restrictions"
 #define GS_AUTO_RELOAD           "auto-reload"
+#define GS_LAST_DOCUMENT_DIRECTORY "document-directory"
+#define GS_LAST_PICTURES_DIRECTORY "pictures-directory"
 
 #define SIDEBAR_DEFAULT_SIZE    132
 #define LINKS_SIDEBAR_ID "links"
@@ -1438,6 +1440,22 @@ lockdown_changed (GSettings   *lockdown,
 }
 #endif
 
+static GSettings *
+ev_window_ensure_settings (EvWindow *ev_window)
+{
+        EvWindowPrivate *priv = ev_window->priv;
+
+        if (priv->settings != NULL)
+                return priv->settings;
+
+        priv->settings = g_settings_new (GS_SCHEMA_NAME);
+        g_signal_connect (priv->settings,
+                          "changed::"GS_OVERRIDE_RESTRICTIONS,
+                          G_CALLBACK (override_restrictions_changed),
+                          ev_window);
+        return priv->settings;
+}
+
 static gboolean
 ev_window_setup_document (EvWindow *ev_window)
 {
@@ -1453,13 +1471,7 @@ ev_window_setup_document (EvWindow *ev_window)
 	ev_window_title_set_document (ev_window->priv->title, document);
 	ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
 
-	if (!ev_window->priv->settings) {
-		ev_window->priv->settings = g_settings_new (GS_SCHEMA_NAME);
-		g_signal_connect (ev_window->priv->settings,
-				  "changed::"GS_OVERRIDE_RESTRICTIONS,
-				  G_CALLBACK (override_restrictions_changed),
-				  ev_window);
-	}
+        ev_window_ensure_settings (ev_window);
 
 #ifdef HAVE_DESKTOP_SCHEMAS
 	if (!ev_window->priv->lockdown_settings) {
@@ -2404,6 +2416,75 @@ ev_window_reload_document (EvWindow *ev_window,
 	}
 }
 
+static const gchar *
+get_settings_key_for_directory (GUserDirectory directory)
+{
+        switch (directory) {
+                case G_USER_DIRECTORY_PICTURES:
+                        return GS_LAST_PICTURES_DIRECTORY;
+                case G_USER_DIRECTORY_DOCUMENTS:
+                default:
+                        return GS_LAST_DOCUMENT_DIRECTORY;
+        }
+}
+
+static void
+ev_window_file_chooser_restore_folder (EvWindow       *window,
+                                       GtkFileChooser *file_chooser,
+                                       const gchar    *uri,
+                                       GUserDirectory  directory)
+{
+        const gchar *folder_uri, *dir;
+        gchar *parent_uri = NULL;
+
+        g_settings_get (ev_window_ensure_settings (window),
+                        get_settings_key_for_directory (directory),
+                        "m&s", &folder_uri);
+        if (folder_uri == NULL && uri != NULL) {
+                GFile *file, *parent;
+
+                file = g_file_new_for_uri (uri);
+                parent = g_file_get_parent (file);
+                g_object_unref (file);
+                if (parent) {
+                        folder_uri = parent_uri = g_file_get_uri (parent);
+                        g_object_unref (parent);
+                }
+        }
+
+        if (folder_uri) {
+                gtk_file_chooser_set_current_folder_uri (file_chooser, folder_uri);
+        } else {
+                dir = g_get_user_special_dir (directory);
+                gtk_file_chooser_set_current_folder (file_chooser,
+                                                     dir ? dir : g_get_home_dir ());
+        }
+
+        g_free (parent_uri);
+}
+
+static void
+ev_window_file_chooser_save_folder (EvWindow       *window,
+                                    GtkFileChooser *file_chooser,
+                                    GUserDirectory  directory)
+{
+        gchar *uri, *folder;
+
+        folder = gtk_file_chooser_get_current_folder (file_chooser);
+        if (g_strcmp0 (folder, g_get_user_special_dir (directory)) == 0) {
+                /* Store 'nothing' if the folder is the default one */
+                uri = NULL;
+        } else {
+                uri = gtk_file_chooser_get_current_folder_uri (file_chooser);
+        }
+        g_free (folder);
+
+        g_settings_set (ev_window_ensure_settings (window),
+                        get_settings_key_for_directory (directory),
+                        "ms", uri);
+        g_free (uri);
+}
+
 static void
 file_open_dialog_response_cb (GtkWidget *chooser,
 			      gint       response_id,
@@ -2412,6 +2493,9 @@ file_open_dialog_response_cb (GtkWidget *chooser,
 	if (response_id == GTK_RESPONSE_OK) {
 		GSList *uris;
 
+                ev_window_file_chooser_save_folder (ev_window, GTK_FILE_CHOOSER (chooser),
+                                                    G_USER_DIRECTORY_DOCUMENTS);
+
 		uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (chooser));
 
 		ev_application_open_uri_list (EV_APP, uris,
@@ -2429,8 +2513,6 @@ static void
 ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
 {
 	GtkWidget   *chooser;
-	const gchar *default_uri = NULL;
-	gchar       *parent_uri = NULL;
 
 	chooser = gtk_file_chooser_dialog_new (_("Open Document"),
 					       GTK_WINDOW (window),
@@ -2444,29 +2526,8 @@ ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
 	gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
 	gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
 
-	if (window->priv->uri) {
-		GFile *file, *parent;
-
-		file = g_file_new_for_uri (window->priv->uri);
-		parent = g_file_get_parent (file);
-		if (parent) {
-			parent_uri = g_file_get_uri (parent);
-			default_uri = parent_uri;
-			g_object_unref (parent);
-		}
-		g_object_unref (file);
-	}
-
-	if (default_uri) {
-		gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (chooser), default_uri);
-	} else {
-		const gchar *folder;
-
-		folder = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
-		gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
-						     folder ? folder : g_get_home_dir ());
-	}
-	g_free (parent_uri);
+        ev_window_file_chooser_restore_folder (window, GTK_FILE_CHOOSER (chooser),
+                                               NULL, G_USER_DIRECTORY_DOCUMENTS);
 
 	g_signal_connect (chooser, "response",
 			  G_CALLBACK (file_open_dialog_response_cb),
@@ -2899,6 +2960,9 @@ file_save_dialog_response_cb (GtkWidget *fc,
 		return;
 	}
 
+        ev_window_file_chooser_save_folder (ev_window, GTK_FILE_CHOOSER (fc),
+                                            G_USER_DIRECTORY_DOCUMENTS);
+
 	uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
 
 	/* FIXME: remote copy should be done here rather than in the save job, 
@@ -2947,6 +3011,10 @@ ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
 	g_object_unref (file);
 	g_free (base_name);
 
+        ev_window_file_chooser_restore_folder (ev_window, GTK_FILE_CHOOSER (fc),
+                                               ev_window->priv->uri,
+                                               G_USER_DIRECTORY_DOCUMENTS);
+
 	g_signal_connect (fc, "response",
 			  G_CALLBACK (file_save_dialog_response_cb),
 			  ev_window);
@@ -6570,6 +6638,9 @@ image_save_dialog_response_cb (GtkWidget *fc,
 		return;
 	}
 
+	ev_window_file_chooser_save_folder (ev_window, GTK_FILE_CHOOSER (fc),
+                                            G_USER_DIRECTORY_PICTURES);
+
 	uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
 	filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (fc));
 	format = g_object_get_data (G_OBJECT (filter), "pixbuf-format");
@@ -6673,6 +6744,9 @@ ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
 	
 	file_chooser_dialog_add_writable_pixbuf_formats	(GTK_FILE_CHOOSER (fc));
 	
+        ev_window_file_chooser_restore_folder (window, GTK_FILE_CHOOSER (fc), NULL,
+                                               G_USER_DIRECTORY_PICTURES);
+
 	g_signal_connect (fc, "response",
 			  G_CALLBACK (image_save_dialog_response_cb),
 			  window);
@@ -6805,6 +6879,9 @@ attachment_save_dialog_response_cb (GtkWidget *fc,
 		return;
 	}
 
+	ev_window_file_chooser_save_folder (ev_window, GTK_FILE_CHOOSER (fc),
+                                            G_USER_DIRECTORY_DOCUMENTS);
+
 	uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
 	target_file = g_file_new_for_uri (uri);
 	g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
@@ -6901,6 +6978,9 @@ ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
 		gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
 						   ev_attachment_get_name (attachment));
 
+        ev_window_file_chooser_restore_folder (window, GTK_FILE_CHOOSER (fc), NULL,
+                                               G_USER_DIRECTORY_DOCUMENTS);
+
 	g_signal_connect (fc, "response",
 			  G_CALLBACK (attachment_save_dialog_response_cb),
 			  window);



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