[geary] Bind to GSettings rather than read/write imperatively



commit 229f4dc4b06dfb8aafa02df605eaf552a7a8802e
Author: Jim Nelson <jim yorba org>
Date:   Fri Dec 20 16:48:53 2013 -0800

    Bind to GSettings rather than read/write imperatively
    
    This work is the result of the prior commit (updating the Preferences
    box, specifically).  Instead of initialzing Widgets with GSettings
    values and writing values out as Widget state changes, bind GSettings
    values directly to the Widgets.  This logic is also moved out to the
    rest of the application where monitoring changes is necessary.
    
    This has the pleasant side-effect of allowing changes to come in from
    GSettings backends (i.e. dconf) and having them updated in Geary
    automatically.

 src/client/application/geary-config.vala           |   86 ++++++++------------
 src/client/components/main-window.vala             |   10 +-
 src/client/composer/composer-window.vala           |    3 +-
 .../conversation-list/conversation-list-store.vala |    3 +-
 .../conversation-list/conversation-list-view.vala  |    3 +-
 src/client/dialogs/preferences-dialog.vala         |   54 ++-----------
 6 files changed, 51 insertions(+), 108 deletions(-)
---
diff --git a/src/client/application/geary-config.vala b/src/client/application/geary-config.vala
index b7dc253..16d98ce 100644
--- a/src/client/application/geary-config.vala
+++ b/src/client/application/geary-config.vala
@@ -6,86 +6,70 @@
 
 // Wrapper class for GSettings.
 public class Configuration {
-    // TODO: These signals can be removed; anyone needing to know when a configuration value has changed
-    // can use the notify["property-name"] syntax
-    public signal void display_preview_changed();
-    public signal void spell_check_changed();
+    public const string WINDOW_WIDTH_KEY = "window-width";
+    public const string WINDOW_HEIGHT_KEY = "window-height";
+    public const string WINDOW_MAXIMIZE_KEY = "window-maximize";
+    public const string FOLDER_LIST_PANE_POSITION_KEY = "folder-list-pane-position";
+    public const string MESSAGES_PANE_POSITION_KEY = "messages-pane-position";
+    public const string AUTOSELECT_KEY = "autoselect";
+    public const string DISPLAY_PREVIEW_KEY = "display-preview";
+    public const string SPELL_CHECK_KEY = "spell-check";
+    public const string PLAY_SOUNDS_KEY = "play-sounds";
+    public const string SHOW_NOTIFICATIONS_KEY = "show-notifications";
+    public const string ASK_OPEN_ATTACHMENT_KEY = "ask-open-attachment";
+    public const string COMPOSE_AS_HTML_KEY = "compose-as-html";
+    
+    public Settings settings { get; private set; }
     
-    private Settings settings;
     private Settings gnome_interface;
     private Settings? indicator_datetime;
     
-    public const string WINDOW_WIDTH_NAME = "window-width";
     public int window_width {
-        get { return settings.get_int(WINDOW_WIDTH_NAME); }
+        get { return settings.get_int(WINDOW_WIDTH_KEY); }
     }
     
-    public const string WINDOW_HEIGHT_NAME = "window-height";
     public int window_height {
-        get { return settings.get_int(WINDOW_HEIGHT_NAME); }
+        get { return settings.get_int(WINDOW_HEIGHT_KEY); }
     }
     
-    public const string WINDOW_MAXIMIZE_NAME = "window-maximize";
     public bool window_maximize {
-        get { return settings.get_boolean(WINDOW_MAXIMIZE_NAME); }
+        get { return settings.get_boolean(WINDOW_MAXIMIZE_KEY); }
     }
     
-    public const string FOLDER_LIST_PANE_POSITION_NAME = "folder-list-pane-position";
     public int folder_list_pane_position {
-        get { return settings.get_int(FOLDER_LIST_PANE_POSITION_NAME); }
+        get { return settings.get_int(FOLDER_LIST_PANE_POSITION_KEY); }
     }
     
-    public const string MESSAGES_PANE_POSITION_NAME = "messages-pane-position";
     public int messages_pane_position {
-        get { return settings.get_int(MESSAGES_PANE_POSITION_NAME); }
+        get { return settings.get_int(MESSAGES_PANE_POSITION_KEY); }
     }
     
-    private const string AUTOSELECT_NAME = "autoselect";
     public bool autoselect {
-        get { return settings.get_boolean(AUTOSELECT_NAME); }
-        set { set_boolean(AUTOSELECT_NAME, value); }
+        get { return settings.get_boolean(AUTOSELECT_KEY); }
     }
     
-    private const string DISPLAY_PREVIEW_NAME = "display-preview";
     public bool display_preview {
-        get { return settings.get_boolean(DISPLAY_PREVIEW_NAME); }
-        set {
-            set_boolean(DISPLAY_PREVIEW_NAME, value);
-            display_preview_changed(); 
-        }
+        get { return settings.get_boolean(DISPLAY_PREVIEW_KEY); }
     }
     
-    private const string SPELL_CHECK_NAME = "spell-check";
     public bool spell_check {
-        get { return settings.get_boolean(SPELL_CHECK_NAME); }
-        set {
-            set_boolean(SPELL_CHECK_NAME, value);
-            spell_check_changed();
-        }
+        get { return settings.get_boolean(SPELL_CHECK_KEY); }
     }
 
-    private const string PLAY_SOUNDS_NAME = "play-sounds";
     public bool play_sounds {
-        get { return settings.get_boolean(PLAY_SOUNDS_NAME); }
-        set {
-            set_boolean(PLAY_SOUNDS_NAME, value);
-        }
+        get { return settings.get_boolean(PLAY_SOUNDS_KEY); }
     }
 
-    private const string SHOW_NOTIFICATIONS_NAME = "show-notifications";
     public bool show_notifications {
-        get { return settings.get_boolean(SHOW_NOTIFICATIONS_NAME); }
-        set {
-            set_boolean(SHOW_NOTIFICATIONS_NAME, value);
-        }
+        get { return settings.get_boolean(SHOW_NOTIFICATIONS_KEY); }
     }
     
-    private const string CLOCK_FORMAT_NAME = "clock-format";
-    private const string TIME_FORMAT_NAME = "time-format";
+    private const string CLOCK_FORMAT_KEY = "clock-format";
+    private const string TIME_FORMAT_KEY = "time-format";
     public Date.ClockFormat clock_format {
         get {
             if (indicator_datetime != null) {
-                string format = indicator_datetime.get_string(TIME_FORMAT_NAME);
+                string format = indicator_datetime.get_string(TIME_FORMAT_KEY);
                 if (format == "12-hour")
                     return Date.ClockFormat.TWELVE_HOURS;
                 else if (format == "24-hour")
@@ -95,23 +79,21 @@ public class Configuration {
                     return Date.ClockFormat.LOCALE_DEFAULT;
                 }
             }
-            if (gnome_interface.get_string(CLOCK_FORMAT_NAME) == "12h")
+            if (gnome_interface.get_string(CLOCK_FORMAT_KEY) == "12h")
                 return Date.ClockFormat.TWELVE_HOURS;
             else
                 return Date.ClockFormat.TWENTY_FOUR_HOURS;
         }
     }
     
-    private const string ASK_OPEN_ATTACHMENT = "ask-open-attachment";
     public bool ask_open_attachment {
-        get { return settings.get_boolean(ASK_OPEN_ATTACHMENT); }
-        set { set_boolean(ASK_OPEN_ATTACHMENT, value); }
+        get { return settings.get_boolean(ASK_OPEN_ATTACHMENT_KEY); }
+        set { set_boolean(ASK_OPEN_ATTACHMENT_KEY, value); }
     }
     
-    private const string COMPOSE_AS_HTML = "compose-as-html";
     public bool compose_as_html {
-        get { return settings.get_boolean(COMPOSE_AS_HTML); }
-        set { set_boolean(COMPOSE_AS_HTML, value); }
+        get { return settings.get_boolean(COMPOSE_AS_HTML_KEY); }
+        set { set_boolean(COMPOSE_AS_HTML_KEY, value); }
     }
     
     // Creates a configuration object.
@@ -119,7 +101,7 @@ public class Configuration {
         // Start GSettings.
         settings = new Settings(schema_id);
         gnome_interface = new Settings("org.gnome.desktop.interface");
-        foreach(string schema in GLib.Settings.list_schemas()) {
+        foreach(unowned string schema in GLib.Settings.list_schemas()) {
             if (schema == "com.canonical.indicator.datetime") {
                 indicator_datetime = new Settings("com.canonical.indicator.datetime");
                 break;
@@ -138,7 +120,7 @@ public class Configuration {
         }
     }
     
-    public void bind (string key, Object object, string property,
+    public void bind(string key, Object object, string property,
         SettingsBindFlags flags = SettingsBindFlags.DEFAULT) {
         settings.bind(key, object, property, flags);
     }
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index a5af2cd..bc93d71 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -40,11 +40,11 @@ public class MainWindow : Gtk.ApplicationWindow {
         // the value in dconf changes *immediately*, and stays saved
         // in the event of a crash.
         Configuration config = GearyApplication.instance.config;
-        config.bind(Configuration.FOLDER_LIST_PANE_POSITION_NAME, folder_paned, "position");
-        config.bind(Configuration.MESSAGES_PANE_POSITION_NAME, conversations_paned, "position");
-        config.bind(Configuration.WINDOW_WIDTH_NAME, this, "window-width");
-        config.bind(Configuration.WINDOW_HEIGHT_NAME, this, "window-height");
-        config.bind(Configuration.WINDOW_MAXIMIZE_NAME, this, "window-maximized");
+        config.bind(Configuration.FOLDER_LIST_PANE_POSITION_KEY, folder_paned, "position");
+        config.bind(Configuration.MESSAGES_PANE_POSITION_KEY, conversations_paned, "position");
+        config.bind(Configuration.WINDOW_WIDTH_KEY, this, "window-width");
+        config.bind(Configuration.WINDOW_HEIGHT_KEY, this, "window-height");
+        config.bind(Configuration.WINDOW_MAXIMIZE_KEY, this, "window-maximized");
         
         add_accel_group(GearyApplication.instance.ui_manager.get_accel_group());
         
diff --git a/src/client/composer/composer-window.vala b/src/client/composer/composer-window.vala
index e6a75ea..f1532a6 100644
--- a/src/client/composer/composer-window.vala
+++ b/src/client/composer/composer-window.vala
@@ -399,7 +399,8 @@ public class ComposerWindow : Gtk.Window {
         editor.navigation_policy_decision_requested.connect(on_navigation_policy_decision_requested);
         editor.new_window_policy_decision_requested.connect(on_navigation_policy_decision_requested);
         
-        GearyApplication.instance.config.spell_check_changed.connect(on_spell_check_changed);
+        GearyApplication.instance.config.settings.changed[Configuration.SPELL_CHECK_KEY].connect(
+            on_spell_check_changed);
         
         // Font family menu items.
         font_sans = new Gtk.RadioMenuItem(new SList<Gtk.RadioMenuItem>());
diff --git a/src/client/conversation-list/conversation-list-store.vala 
b/src/client/conversation-list/conversation-list-store.vala
index 2ab8cd9..9865cb3 100644
--- a/src/client/conversation-list/conversation-list-store.vala
+++ b/src/client/conversation-list/conversation-list-store.vala
@@ -57,7 +57,8 @@ public class ConversationListStore : Gtk.ListStore {
         set_default_sort_func(sort_by_date);
         set_sort_column_id(Gtk.SortColumn.DEFAULT, Gtk.SortType.DESCENDING);
         
-        GearyApplication.instance.config.display_preview_changed.connect(on_display_preview_changed);
+        GearyApplication.instance.config.settings.changed[Configuration.DISPLAY_PREVIEW_KEY].connect(
+            on_display_preview_changed);
         update_id = Timeout.add_seconds_full(Priority.LOW, 60, update_date_strings);
         
         GearyApplication.instance.controller.notify[GearyController.PROP_CURRENT_CONVERSATION].
diff --git a/src/client/conversation-list/conversation-list-view.vala 
b/src/client/conversation-list/conversation-list-view.vala
index 3871bbb..0350216 100644
--- a/src/client/conversation-list/conversation-list-view.vala
+++ b/src/client/conversation-list/conversation-list-view.vala
@@ -66,7 +66,8 @@ public class ConversationListView : Gtk.TreeView {
         Gtk.drag_source_set(this, Gdk.ModifierType.BUTTON1_MASK, FolderList.Tree.TARGET_ENTRY_LIST,
             Gdk.DragAction.COPY | Gdk.DragAction.MOVE);
         
-        GearyApplication.instance.config.display_preview_changed.connect(on_display_preview_changed);
+        GearyApplication.instance.config.settings.changed[Configuration.DISPLAY_PREVIEW_KEY].connect(
+            on_display_preview_changed);
         GearyApplication.instance.controller.notify[GearyController.PROP_CURRENT_CONVERSATION].
             connect(on_conversation_monitor_changed);
         
diff --git a/src/client/dialogs/preferences-dialog.vala b/src/client/dialogs/preferences-dialog.vala
index 8b7c592..d9a878a 100644
--- a/src/client/dialogs/preferences-dialog.vala
+++ b/src/client/dialogs/preferences-dialog.vala
@@ -6,17 +6,8 @@
 
 public class PreferencesDialog : Object {
     private Gtk.Dialog dialog;
-    private Gtk.CheckButton autoselect;
-    private Gtk.CheckButton display_preview;
-    private Gtk.CheckButton spell_check;
-    private Gtk.CheckButton play_sounds;
-    private Gtk.CheckButton show_notifications;
-    private Gtk.Button close_button;
-    private Configuration config;
     
     public PreferencesDialog(Gtk.Window parent) {
-        this.config = GearyApplication.instance.config;
-        
         Gtk.Builder builder = GearyApplication.instance.create_builder("preferences.glade");
         
         // Get all of the dialog elements.
@@ -24,25 +15,12 @@ public class PreferencesDialog : Object {
         dialog.set_transient_for(parent);
         dialog.set_modal(true);
         
-        autoselect = builder.get_object("autoselect") as Gtk.CheckButton;
-        display_preview = builder.get_object("display_preview") as Gtk.CheckButton;
-        spell_check = builder.get_object("spell_check") as Gtk.CheckButton;
-        play_sounds = builder.get_object("play_sounds") as Gtk.CheckButton;
-        show_notifications = builder.get_object("show_notifications") as Gtk.CheckButton;
-        close_button = builder.get_object("close_button") as Gtk.Button;
-        
-        autoselect.active = config.autoselect;
-        display_preview.active = config.display_preview;
-        spell_check.active = config.spell_check;
-        play_sounds.active = config.play_sounds;
-        show_notifications.active = config.show_notifications;
-        
-        // Connect to element signals.
-        autoselect.toggled.connect(on_autoselect_toggled);
-        display_preview.toggled.connect(on_display_preview_toggled);
-        spell_check.toggled.connect(on_spell_check_toggled);
-        play_sounds.toggled.connect(on_play_sounds_toggled);
-        show_notifications.toggled.connect(on_show_notifications_toggled);
+        Configuration config = GearyApplication.instance.config;
+        config.bind(Configuration.AUTOSELECT_KEY, builder.get_object("autoselect"), "active");
+        config.bind(Configuration.DISPLAY_PREVIEW_KEY, builder.get_object("display_preview"), "active");
+        config.bind(Configuration.SPELL_CHECK_KEY, builder.get_object("spell_check"), "active");
+        config.bind(Configuration.PLAY_SOUNDS_KEY, builder.get_object("play_sounds"), "active");
+        config.bind(Configuration.SHOW_NOTIFICATIONS_KEY, builder.get_object("show_notifications"), 
"active");
     }
     
     public void run() {
@@ -50,25 +28,5 @@ public class PreferencesDialog : Object {
         dialog.run();
         dialog.destroy();
     }
-    
-    private void on_autoselect_toggled() {
-        config.autoselect = autoselect.active;
-    }
-    
-    private void on_display_preview_toggled() {
-        config.display_preview = display_preview.active;
-    }
-    
-    private void on_spell_check_toggled() {
-        config.spell_check = spell_check.active;
-    }
-
-    private void on_play_sounds_toggled() {
-        config.play_sounds = play_sounds.active;
-    }
-
-    private void on_show_notifications_toggled() {
-        config.show_notifications = show_notifications.active;
-    }
 }
 


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