forked from AuroraMiddleware/gtk
Make GtkFileChoser remember the last directory opened
Introduces a 'LastFolderUri' settings key, where we remember the last-opened folder from the previous instance of the file chooser. The idea is that this works globally, across all applications, so it will be easy to do things like 1. Save an attachment from a mail (or some other file) 2. Open another program 3. Do File/Open and automatically get sent to the folder where (1) happened. Signed-off-by: Federico Mena Quintero <federico@gnome.org> https://bugzilla.gnome.org/show_bug.cgi?id=644426
This commit is contained in:
parent
dd0ce5dd1e
commit
76c24a4a81
@ -5575,9 +5575,24 @@ static void
|
||||
settings_save (GtkFileChooserDefault *impl)
|
||||
{
|
||||
GtkFileChooserSettings *settings;
|
||||
char *current_folder_uri;
|
||||
|
||||
settings = _gtk_file_chooser_settings_new ();
|
||||
|
||||
/* Current folder */
|
||||
|
||||
if (impl->current_folder)
|
||||
current_folder_uri = g_file_get_uri (impl->current_folder);
|
||||
else
|
||||
current_folder_uri = "";
|
||||
|
||||
_gtk_file_chooser_settings_set_last_folder_uri (settings, current_folder_uri);
|
||||
|
||||
if (impl->current_folder)
|
||||
g_free (current_folder_uri);
|
||||
|
||||
/* All the other state */
|
||||
|
||||
_gtk_file_chooser_settings_set_location_mode (settings, impl->location_mode);
|
||||
_gtk_file_chooser_settings_set_show_hidden (settings, gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
|
||||
_gtk_file_chooser_settings_set_show_size_column (settings, impl->show_size_column);
|
||||
@ -5605,12 +5620,35 @@ gtk_file_chooser_default_realize (GtkWidget *widget)
|
||||
emit_default_size_changed (impl);
|
||||
}
|
||||
|
||||
static GFile *
|
||||
get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
|
||||
{
|
||||
char *last_folder_uri;
|
||||
GFile *file;
|
||||
GtkFileChooserSettings *settings;
|
||||
|
||||
settings = _gtk_file_chooser_settings_new ();
|
||||
last_folder_uri = _gtk_file_chooser_settings_get_last_folder_uri (settings);
|
||||
g_object_unref (settings);
|
||||
|
||||
/* If no last folder is set, we use the user's home directory, since
|
||||
* this is the starting point for most documents.
|
||||
*/
|
||||
if (last_folder_uri[0] == '\0')
|
||||
file = g_file_new_for_path (g_get_home_dir ());
|
||||
else
|
||||
file = g_file_new_for_uri (last_folder_uri);
|
||||
|
||||
g_free (last_folder_uri);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
/* GtkWidget::map method */
|
||||
static void
|
||||
gtk_file_chooser_default_map (GtkWidget *widget)
|
||||
{
|
||||
GtkFileChooserDefault *impl;
|
||||
char *current_working_dir;
|
||||
|
||||
profile_start ("start", NULL);
|
||||
|
||||
@ -5620,16 +5658,17 @@ gtk_file_chooser_default_map (GtkWidget *widget)
|
||||
|
||||
if (impl->operation_mode == OPERATION_MODE_BROWSE)
|
||||
{
|
||||
GFile *folder;
|
||||
|
||||
switch (impl->reload_state)
|
||||
{
|
||||
case RELOAD_EMPTY:
|
||||
/* The user didn't explicitly give us a folder to
|
||||
* display, so we'll use the cwd
|
||||
/* The user didn't explicitly give us a folder to display, so we'll
|
||||
* use the saved one from the last invocation of the file chooser
|
||||
*/
|
||||
current_working_dir = g_get_current_dir ();
|
||||
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl),
|
||||
current_working_dir);
|
||||
g_free (current_working_dir);
|
||||
folder = get_file_for_last_folder_opened (impl);
|
||||
gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), folder, NULL);
|
||||
g_object_unref (folder);
|
||||
break;
|
||||
|
||||
case RELOAD_HAS_FOLDER:
|
||||
@ -6877,17 +6916,12 @@ gtk_file_chooser_default_get_current_folder (GtkFileChooser *chooser)
|
||||
|
||||
if (impl->reload_state == RELOAD_EMPTY)
|
||||
{
|
||||
char *current_working_dir;
|
||||
GFile *file;
|
||||
|
||||
/* We are unmapped, or we had an error while loading the last folder. We'll return
|
||||
* the $cwd since once we get (re)mapped, we'll load $cwd anyway unless the caller
|
||||
* explicitly calls set_current_folder() on us.
|
||||
/* We are unmapped, or we had an error while loading the last folder.
|
||||
* We'll return the folder used by the last invocation of the file chooser
|
||||
* since once we get (re)mapped, we'll load *that* folder anyway unless
|
||||
* the caller explicitly calls set_current_folder() on us.
|
||||
*/
|
||||
current_working_dir = g_get_current_dir ();
|
||||
file = g_file_new_for_path (current_working_dir);
|
||||
g_free (current_working_dir);
|
||||
return file;
|
||||
return get_file_for_last_folder_opened (impl);
|
||||
}
|
||||
|
||||
if (impl->current_folder)
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "gtkalias.h"
|
||||
|
||||
#define SETTINGS_GROUP "Filechooser Settings"
|
||||
#define LAST_FOLDER_URI_KEY "LastFolderUri"
|
||||
#define LOCATION_MODE_KEY "LocationMode"
|
||||
#define SHOW_HIDDEN_KEY "ShowHidden"
|
||||
#define SHOW_SIZE_COLUMN_KEY "ShowSizeColumn"
|
||||
@ -137,6 +138,10 @@ ensure_settings_read (GtkFileChooserSettings *settings)
|
||||
if (!g_key_file_has_group (key_file, SETTINGS_GROUP))
|
||||
goto out;
|
||||
|
||||
/* Last folder URI */
|
||||
|
||||
settings->last_folder_uri = g_key_file_get_string (key_file, SETTINGS_GROUP, LAST_FOLDER_URI_KEY, NULL);
|
||||
|
||||
/* Location mode */
|
||||
|
||||
location_mode_str = g_key_file_get_string (key_file, SETTINGS_GROUP,
|
||||
@ -233,6 +238,7 @@ _gtk_file_chooser_settings_class_init (GtkFileChooserSettingsClass *class)
|
||||
static void
|
||||
_gtk_file_chooser_settings_init (GtkFileChooserSettings *settings)
|
||||
{
|
||||
settings->last_folder_uri = NULL;
|
||||
settings->location_mode = LOCATION_MODE_PATH_BAR;
|
||||
settings->sort_order = GTK_SORT_ASCENDING;
|
||||
settings->sort_column = FILE_LIST_COL_NAME;
|
||||
@ -250,6 +256,19 @@ _gtk_file_chooser_settings_new (void)
|
||||
return g_object_new (GTK_FILE_CHOOSER_SETTINGS_TYPE, NULL);
|
||||
}
|
||||
|
||||
char *
|
||||
_gtk_file_chooser_settings_get_last_folder_uri (GtkFileChooserSettings *settings)
|
||||
{
|
||||
return g_strdup (settings->last_folder_uri);
|
||||
}
|
||||
|
||||
void
|
||||
_gtk_file_chooser_settings_set_last_folder_uri (GtkFileChooserSettings *settings, const char *uri)
|
||||
{
|
||||
g_free (settings->last_folder_uri);
|
||||
settings->last_folder_uri = g_strdup (uri);
|
||||
}
|
||||
|
||||
LocationMode
|
||||
_gtk_file_chooser_settings_get_location_mode (GtkFileChooserSettings *settings)
|
||||
{
|
||||
@ -418,6 +437,8 @@ _gtk_file_chooser_settings_save (GtkFileChooserSettings *settings,
|
||||
/* Initialise with the on-disk keyfile, so we keep unknown options */
|
||||
g_key_file_load_from_file (key_file, filename, 0, NULL);
|
||||
|
||||
g_key_file_set_string (key_file, SETTINGS_GROUP,
|
||||
LAST_FOLDER_URI_KEY, settings->last_folder_uri);
|
||||
g_key_file_set_string (key_file, SETTINGS_GROUP,
|
||||
LOCATION_MODE_KEY, location_mode_str);
|
||||
g_key_file_set_boolean (key_file, SETTINGS_GROUP,
|
||||
|
@ -44,6 +44,8 @@ struct _GtkFileChooserSettings
|
||||
{
|
||||
GObject object;
|
||||
|
||||
char *last_folder_uri;
|
||||
|
||||
LocationMode location_mode;
|
||||
|
||||
GtkSortType sort_order;
|
||||
@ -68,6 +70,9 @@ GType _gtk_file_chooser_settings_get_type (void) G_GNUC_CONST;
|
||||
|
||||
GtkFileChooserSettings *_gtk_file_chooser_settings_new (void);
|
||||
|
||||
char *_gtk_file_chooser_settings_get_last_folder_uri (GtkFileChooserSettings *settings);
|
||||
void _gtk_file_chooser_settings_set_last_folder_uri (GtkFileChooserSettings *settings, const char *uri);
|
||||
|
||||
LocationMode _gtk_file_chooser_settings_get_location_mode (GtkFileChooserSettings *settings);
|
||||
void _gtk_file_chooser_settings_set_location_mode (GtkFileChooserSettings *settings,
|
||||
LocationMode location_mode);
|
||||
|
Loading…
Reference in New Issue
Block a user