filechooser: Attach GSettings object to GtkSettings

... instead of keeping one per GtkFileChooserDefault. This allows using
it in other places, too.
This commit is contained in:
Benjamin Otte 2013-01-23 03:23:33 +01:00
parent b112a85395
commit 78d91aaf9b
4 changed files with 59 additions and 38 deletions

View File

@ -6014,16 +6014,6 @@ set_sort_column (GtkFileChooserDefault *impl)
impl->sort_order); impl->sort_order);
} }
static void
settings_ensure (GtkFileChooserDefault *impl)
{
if (impl->settings != NULL)
return;
impl->settings = g_settings_new ("org.gtk.Settings.FileChooser");
g_settings_delay (impl->settings);
}
static void static void
settings_load (GtkFileChooserDefault *impl) settings_load (GtkFileChooserDefault *impl)
{ {
@ -6033,15 +6023,16 @@ settings_load (GtkFileChooserDefault *impl)
gint sort_column; gint sort_column;
GtkSortType sort_order; GtkSortType sort_order;
gint sidebar_width; gint sidebar_width;
GSettings *settings;
settings_ensure (impl); settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
location_mode = g_settings_get_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE); location_mode = g_settings_get_enum (settings, SETTINGS_KEY_LOCATION_MODE);
show_hidden = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN); show_hidden = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN);
show_size_column = g_settings_get_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN); show_size_column = g_settings_get_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN);
sort_column = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN); sort_column = g_settings_get_enum (settings, SETTINGS_KEY_SORT_COLUMN);
sort_order = g_settings_get_enum (impl->settings, SETTINGS_KEY_SORT_ORDER); sort_order = g_settings_get_enum (settings, SETTINGS_KEY_SORT_ORDER);
sidebar_width = g_settings_get_int (impl->settings, SETTINGS_KEY_SIDEBAR_WIDTH); sidebar_width = g_settings_get_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH);
location_mode_set (impl, location_mode, TRUE); location_mode_set (impl, location_mode, TRUE);
@ -6065,25 +6056,29 @@ save_dialog_geometry (GtkFileChooserDefault *impl)
{ {
GtkWindow *toplevel; GtkWindow *toplevel;
int x, y, width, height; int x, y, width, height;
GSettings *settings;
toplevel = get_toplevel (GTK_WIDGET (impl)); toplevel = get_toplevel (GTK_WIDGET (impl));
if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel))) if (!(toplevel && GTK_IS_FILE_CHOOSER_DIALOG (toplevel)))
return; return;
settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (toplevel));
gtk_window_get_position (toplevel, &x, &y); gtk_window_get_position (toplevel, &x, &y);
gtk_window_get_size (toplevel, &width, &height); gtk_window_get_size (toplevel, &width, &height);
g_settings_set (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", x, y); g_settings_set (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", x, y);
g_settings_set (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", width, height); g_settings_set (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", width, height);
} }
static void static void
settings_save (GtkFileChooserDefault *impl) settings_save (GtkFileChooserDefault *impl)
{ {
char *current_folder_uri; char *current_folder_uri;
GSettings *settings;
settings_ensure (impl); settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
/* Current folder */ /* Current folder */
@ -6092,29 +6087,26 @@ settings_save (GtkFileChooserDefault *impl)
else else
current_folder_uri = ""; current_folder_uri = "";
g_settings_set_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri); g_settings_set_string (settings, SETTINGS_KEY_LAST_FOLDER_URI, current_folder_uri);
if (impl->current_folder) if (impl->current_folder)
g_free (current_folder_uri); g_free (current_folder_uri);
/* All the other state */ /* All the other state */
g_settings_set_enum (impl->settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode); g_settings_set_enum (settings, SETTINGS_KEY_LOCATION_MODE, impl->location_mode);
g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_HIDDEN, g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_HIDDEN,
gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl))); gtk_file_chooser_get_show_hidden (GTK_FILE_CHOOSER (impl)));
g_settings_set_boolean (impl->settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column); g_settings_set_boolean (settings, SETTINGS_KEY_SHOW_SIZE_COLUMN, impl->show_size_column);
g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column); g_settings_set_enum (settings, SETTINGS_KEY_SORT_COLUMN, impl->sort_column);
g_settings_set_enum (impl->settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order); g_settings_set_enum (settings, SETTINGS_KEY_SORT_ORDER, impl->sort_order);
g_settings_set_int (impl->settings, SETTINGS_KEY_SIDEBAR_WIDTH, g_settings_set_int (settings, SETTINGS_KEY_SIDEBAR_WIDTH,
gtk_paned_get_position (GTK_PANED (impl->browse_widgets_hpaned))); gtk_paned_get_position (GTK_PANED (impl->browse_widgets_hpaned)));
save_dialog_geometry (impl); save_dialog_geometry (impl);
/* Now apply the settings */ /* Now apply the settings */
g_settings_apply (impl->settings); g_settings_apply (settings);
g_object_unref (impl->settings);
impl->settings = NULL;
} }
/* GtkWidget::realize method */ /* GtkWidget::realize method */
@ -6134,11 +6126,12 @@ static GFile *
get_file_for_last_folder_opened (GtkFileChooserDefault *impl) get_file_for_last_folder_opened (GtkFileChooserDefault *impl)
{ {
char *last_folder_uri; char *last_folder_uri;
GSettings *settings;
GFile *file; GFile *file;
settings_ensure (impl); settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
last_folder_uri = g_settings_get_string (impl->settings, SETTINGS_KEY_LAST_FOLDER_URI); last_folder_uri = g_settings_get_string (settings, SETTINGS_KEY_LAST_FOLDER_URI);
/* If no last folder is set, we use the user's home directory, since /* If no last folder is set, we use the user's home directory, since
* this is the starting point for most documents. * this is the starting point for most documents.
@ -8214,13 +8207,14 @@ gtk_file_chooser_default_get_default_size (GtkFileChooserEmbed *chooser_embed,
GtkFileChooserDefault *impl; GtkFileChooserDefault *impl;
GtkRequisition req; GtkRequisition req;
int x, y, width, height; int x, y, width, height;
GSettings *settings;
impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed); impl = GTK_FILE_CHOOSER_DEFAULT (chooser_embed);
settings_ensure (impl); settings = _gtk_file_chooser_get_settings_for_widget (GTK_WIDGET (impl));
g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y); g_settings_get (settings, SETTINGS_KEY_WINDOW_POSITION, "(ii)", &x, &y);
g_settings_get (impl->settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height); g_settings_get (settings, SETTINGS_KEY_WINDOW_SIZE, "(ii)", &width, &height);
if (x >= 0 && y >= 0 && width > 0 && height > 0) if (x >= 0 && y >= 0 && width > 0 && height > 0)
{ {

View File

@ -280,8 +280,6 @@ struct _GtkFileChooserDefault
gint sort_column; gint sort_column;
GtkSortType sort_order; GtkSortType sort_order;
GSettings *settings;
#if 0 #if 0
GdkDragContext *shortcuts_drag_context; GdkDragContext *shortcuts_drag_context;
GSource *shortcuts_drag_outside_idle; GSource *shortcuts_drag_outside_idle;

View File

@ -414,3 +414,30 @@ _gtk_file_chooser_extract_recent_folders (GList *infos)
return result; return result;
} }
GSettings *
_gtk_file_chooser_get_settings_for_widget (GtkWidget *widget)
{
static GQuark file_chooser_settings_quark = 0;
GtkSettings *gtksettings;
GSettings *settings;
if (G_UNLIKELY (file_chooser_settings_quark == 0))
file_chooser_settings_quark = g_quark_from_static_string ("-gtk-file-chooser-settings");
gtksettings = gtk_widget_get_settings (widget);
settings = g_object_get_qdata (G_OBJECT (gtksettings), file_chooser_settings_quark);
if (G_UNLIKELY (settings == NULL))
{
settings = g_settings_new ("org.gtk.Settings.FileChooser");
g_settings_delay (settings);
g_object_set_qdata_full (G_OBJECT (gtksettings),
file_chooser_settings_quark,
settings,
g_object_unref);
}
return settings;
}

View File

@ -52,6 +52,8 @@ GQuark _gtk_file_chooser_delegate_get_quark (void) G_GNUC_CONST;
GList *_gtk_file_chooser_extract_recent_folders (GList *infos); GList *_gtk_file_chooser_extract_recent_folders (GList *infos);
GSettings *_gtk_file_chooser_get_settings_for_widget (GtkWidget *widget);
G_END_DECLS G_END_DECLS
#endif /* __GTK_FILE_CHOOSER_UTILS_H__ */ #endif /* __GTK_FILE_CHOOSER_UTILS_H__ */