commit 4c902ce157f23f013d26bb3c02a676e0c2fa34eb
Author: vrishab <gnome.vrb@gmail.com>
Date:   Tue Nov 15 03:02:52 2016 +0530

    playlist: Remember last used location for loading / saving playlists

diff --git a/data/org.gnome.rhythmbox.gschema.xml b/data/org.gnome.rhythmbox.gschema.xml
index d5dbb9f..d07c79e 100644
--- a/data/org.gnome.rhythmbox.gschema.xml
+++ b/data/org.gnome.rhythmbox.gschema.xml
@@ -172,6 +172,15 @@
     <child name='encoding' schema='org.gnome.rhythmbox.encoding-settings'/>
     <child name='source' schema='org.gnome.rhythmbox.source'/>
   </schema>
+
+  <schema id="org.gnome.rhythmbox.playlist" path="/org/gnome/rhythmbox/playlist/">
+    <key name="last-used-location" type="s">
+      <default>''</default>
+      <summary>URI of the directory where Rhythmbox last saved playlists to (or) loaded playlists from</summary>
+      <description>URI of the directory where Rhythmbox last saved playlists to (or) loaded playlists from</description>
+    </key>
+  </schema>
+
   <enum id="org.gnome.rhythmbox.podcast-interval">
     <value nick="hourly" value="0"/>
     <value nick="daily" value="1"/>
diff --git a/shell/rb-playlist-manager.c b/shell/rb-playlist-manager.c
index c4809a6..1e156f2 100644
--- a/shell/rb-playlist-manager.c
+++ b/shell/rb-playlist-manager.c
@@ -113,6 +113,7 @@ static void shuffle_playlist_action_cb (GSimpleAction *action, GVariant *paramet
 static void save_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer data);
 static void add_to_new_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer data);
 static void add_to_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer data);
+static gchar *rb_playlist_manager_get_playlist_dir (RBPlaylistManager *mgr);
 
 struct RBPlaylistManagerPrivate
 {
@@ -124,6 +125,7 @@ struct RBPlaylistManagerPrivate
 
 	RBStaticPlaylistSource *loading_playlist;
 	RBSource *new_playlist;
+	GSettings *settings;
 
 	gint dirty;
 	gint saving;
@@ -1037,6 +1039,7 @@ load_playlist_response_cb (GtkDialog *dialog,
 			   RBPlaylistManager *mgr)
 {
 	char *escaped_file = NULL;
+	gchar *load_dir;
 	GError *error = NULL;
 
 	if (response_id != GTK_RESPONSE_ACCEPT) {
@@ -1051,6 +1054,13 @@ load_playlist_response_cb (GtkDialog *dialog,
 	if (escaped_file == NULL)
 		return;
 
+	load_dir = g_path_get_dirname (escaped_file);
+
+	/* Remember last used playlist location */
+	if (rb_uri_is_directory (load_dir)) {
+		g_settings_set_string (mgr->priv->settings, PLAYLIST_LAST_USED_DIR_KEY, load_dir);
+	}
+
 	if (!rb_playlist_manager_parse_file (mgr, escaped_file, &error)) {
 		rb_error_dialog (NULL, _("Couldn't read playlist"),
 				 "%s", error->message);
@@ -1058,9 +1068,26 @@ load_playlist_response_cb (GtkDialog *dialog,
 	}
 
 	g_free (escaped_file);
+	g_free (load_dir);
 	rb_playlist_manager_set_dirty (mgr, TRUE);
 }
 
+static gchar *
+rb_playlist_manager_get_playlist_dir (RBPlaylistManager *mgr)
+{
+	gchar *uri;
+
+	uri = g_settings_get_string (mgr->priv->settings, PLAYLIST_LAST_USED_DIR_KEY);
+
+	/* If we don't have a load / save directory saved, use the home dir as fallback */
+	if (uri == NULL || (strcmp (uri, "") == 0)) {
+		uri = g_filename_to_uri (g_get_home_dir (), NULL, NULL);
+	}
+
+	return uri;
+}
+
+
 static void
 load_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer data)
 {
@@ -1069,6 +1096,7 @@ load_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer da
 	GtkWidget *dialog;
 	GtkFileFilter *filter;
 	GtkFileFilter *filter_all;
+	gchar *load_dir;
 	int i;
 
 	filter = gtk_file_filter_new ();
@@ -1091,10 +1119,14 @@ load_playlist_action_cb (GSimpleAction *action, GVariant *parameter, gpointer da
 	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter_all);
 	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
 
+	load_dir = rb_playlist_manager_get_playlist_dir (mgr);
+	gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog), load_dir);
+
 	g_signal_connect_object (dialog, "response",
 				 G_CALLBACK (load_playlist_response_cb), mgr, 0);
 
 	g_object_unref (window);
+	g_free (load_dir);
 }
 
 static void
@@ -1103,10 +1135,14 @@ save_playlist_response_cb (GtkDialog *dialog,
 			   RBSource *source)
 {
 	char *file = NULL;
+	gchar *save_dir;
 	GtkWidget *menu;
 	gint index;
 	RBPlaylistExportType export_type = RB_PLAYLIST_EXPORT_TYPE_UNKNOWN;
 
+	RBShell *shell;
+	RBPlaylistManager *mgr;
+
 	if (response_id != GTK_RESPONSE_OK) {
 		gtk_widget_destroy (GTK_WIDGET (dialog));
 		return;
@@ -1116,6 +1152,16 @@ save_playlist_response_cb (GtkDialog *dialog,
 	if (file == NULL || file[0] == '\0')
 		return;
 
+	g_object_get (source, "shell", &shell, NULL);
+	g_object_get (shell, "playlist-manager", &mgr, NULL);
+
+	save_dir = g_path_get_dirname (file);
+
+	/* Remember last used playlist location */
+	if (rb_uri_is_directory (save_dir)) {
+		g_settings_set_string (mgr->priv->settings, PLAYLIST_LAST_USED_DIR_KEY, save_dir);
+	}
+
 	menu = g_object_get_data (G_OBJECT(dialog), "export-menu");
 	index = gtk_combo_box_get_active (GTK_COMBO_BOX (menu));
 
@@ -1145,6 +1191,10 @@ save_playlist_response_cb (GtkDialog *dialog,
 		gtk_widget_destroy (GTK_WIDGET (dialog));
 	}
 
+	g_object_unref (mgr);
+	g_object_unref (shell);
+
+	g_free (save_dir);
 	g_free (file);
 }
 
@@ -1240,6 +1290,7 @@ rb_playlist_manager_save_playlist_file (RBPlaylistManager *mgr, RBSource *source
 	GtkWidget *menu;
 	char *name;
 	char *tmp;
+	gchar *save_dir;
 
 	g_return_if_fail (RB_IS_PLAYLIST_SOURCE (source));
 
@@ -1256,6 +1307,9 @@ rb_playlist_manager_save_playlist_file (RBPlaylistManager *mgr, RBSource *source
 	g_free (tmp);
 	g_free (name);
 
+	save_dir = rb_playlist_manager_get_playlist_dir (mgr);
+	gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog), save_dir);
+
 	/* FIXME: always has "by extension" as default (it should probably remember the last selection) */
 	gtk_combo_box_set_active (GTK_COMBO_BOX (menu), 0);
 	g_signal_connect_object (dialog, "response",
@@ -1263,6 +1317,7 @@ rb_playlist_manager_save_playlist_file (RBPlaylistManager *mgr, RBSource *source
 				 source, 0);
 
 	g_object_unref (builder);
+	g_free (save_dir);
 }
 
 static void
@@ -1862,6 +1917,7 @@ rb_playlist_manager_constructed (GObject *object)
 
 	RB_CHAIN_GOBJECT_METHOD(rb_playlist_manager_parent_class, constructed, G_OBJECT (mgr));
 
+	mgr->priv->settings = g_settings_new (PLAYLIST_SETTINGS_SCHEMA);
 	app = g_application_get_default ();
 	g_action_map_add_action_entries (G_ACTION_MAP (app), actions, G_N_ELEMENTS (actions), mgr);
 
@@ -1922,6 +1978,7 @@ rb_playlist_manager_dispose (GObject *object)
 
 	g_return_if_fail (mgr->priv != NULL);
 
+	g_clear_object (&mgr->priv->settings);
 	g_clear_object (&mgr->priv->db);
 	g_clear_object (&mgr->priv->selected_source);
 
diff --git a/shell/rb-playlist-manager.h b/shell/rb-playlist-manager.h
index 1225e4e..0b8ee94 100644
--- a/shell/rb-playlist-manager.h
+++ b/shell/rb-playlist-manager.h
@@ -28,6 +28,10 @@
 #ifndef __RB_PLAYLIST_MANAGER_H
 #define __RB_PLAYLIST_MANAGER_H
 
+#define PLAYLIST_SETTINGS_SCHEMA	"org.gnome.rhythmbox.playlist"
+
+#define PLAYLIST_LAST_USED_DIR_KEY	"last-used-location"
+
 #include <sources/rb-source.h>
 #include <sources/rb-display-page-model.h>
 #include <sources/rb-display-page-tree.h>
