[gnome-maps/wip/settings] Settings: add a Gio.Settings wrapper.



commit a45496ccb0778a4bf01b35ed007c14aa9b53fb97
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Mon Sep 30 16:17:22 2013 +0200

    Settings: add a Gio.Settings wrapper.
    
    The Settings class wraps a Gio.Settings object, but takes care of all
    GVariant (un)boxing. This should make working with settings a much
    less verbose experience.

 src/Makefile-js.am |    1 +
 src/application.js |    3 +-
 src/geoclue.js     |   30 +++++++++++--------------
 src/mainWindow.js  |   31 ++++++++++----------------
 src/mapView.js     |    3 +-
 src/settings.js    |   61 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 90 insertions(+), 39 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 233db98..134fbd1 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -7,6 +7,7 @@ dist_js_DATA = \
     mapView.js \
     path.js \
     sidebar.js \
+    settings.js \
     utils.js \
     userLocation.js \
     geoclue.js \
diff --git a/src/application.js b/src/application.js
index 156f516..3a67dc6 100644
--- a/src/application.js
+++ b/src/application.js
@@ -37,6 +37,7 @@ const Format = imports.format;
 const MainWindow = imports.mainWindow;
 const Utils = imports.utils;
 const Path = imports.path;
+const Settings = imports.settings;
 
 // used globally
 let application = null;
@@ -80,7 +81,7 @@ const Application = new Lang.Class({
         Utils.loadStyleSheet(Gio.file_new_for_uri('resource:///org/gnome/maps/application.css'));
 
         application = this;
-        settings = new Gio.Settings({ schema: 'org.gnome.maps' });
+        settings = new Settings.Settings('org.gnome.maps');
 
         Utils.initActions(this, [{
             properties: { name: 'quit' },
diff --git a/src/geoclue.js b/src/geoclue.js
index 4c0eee0..5767bda 100644
--- a/src/geoclue.js
+++ b/src/geoclue.js
@@ -91,18 +91,15 @@ const Geoclue = new Lang.Class({
     },
 
     _init: function() {
-        let lastLocation = Application.settings.get_value('last-location');
-        if (lastLocation.n_children() >= 3) {
-            let lat = lastLocation.get_child_value(0);
-            let lng = lastLocation.get_child_value(1);
-            let accuracy = lastLocation.get_child_value(2);
-
-            this.location = new Geocode.Location({ latitude: lat.get_double(),
-                                                   longitude: lng.get_double(),
-                                                   accuracy: accuracy.get_double() });
-            let lastLocationDescription = Application.settings.get_string('last-location-description');
+        let lastLocation = Application.settings.get('last-location');
+        if (lastLocation.length >= 3) {
+            let [lat, lng, accuracy] = lastLocation;
+            this.location = new Geocode.Location({ latitude: lat,
+                                                   longitude: lng,
+                                                   accuracy: accuracy });
+            let lastLocationDescription = Application.settings.get('last-location-description');
             this.location.set_description(lastLocationDescription);
-            this.userSetLocation = Application.settings.get_boolean('last-location-user-set');
+            this.userSetLocation = Application.settings.get('last-location-user-set');
         }
 
         this._managerProxy = new ManagerProxy(Gio.DBus.system,
@@ -142,12 +139,11 @@ const Geoclue = new Lang.Class({
     _updateLocation: function(location, userSet) {
         this.location = location;
 
-        let variant = GLib.Variant.new('ad', [location.latitude,
-                                              location.longitude,
-                                              location.accuracy]);
-        Application.settings.set_value('last-location', variant);
-        Application.settings.set_string('last-location-description', location.description);
-        Application.settings.set_boolean('last-location-user-set', userSet);
+        Application.settings.set('last-location', [location.latitude,
+                                                   location.longitude,
+                                                   location.accuracy]);
+        Application.settings.set('last-location-description', location.description);
+        Application.settings.set('last-location-user-set', userSet);
         this.userSetLocation = userSet;
 
         this.emit('location-changed');
diff --git a/src/mainWindow.js b/src/mainWindow.js
index d2abb80..034a57d 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -152,34 +152,27 @@ const MainWindow = new Lang.Class({
 
         // GLib.Variant.new() can handle arrays just fine
         let size = this.window.get_size();
-        let variant = GLib.Variant.new ('ai', size);
-        Application.settings.set_value('window-size', variant);
+        Application.settings.set('window-size', size);
 
         let position = this.window.get_position();
-        variant = GLib.Variant.new ('ai', position);
-        Application.settings.set_value('window-position', variant);
+        Application.settings.set('window-position', position);
     },
 
     _restoreWindowGeometry: function() {
-        let size = Application.settings.get_value('window-size');
-        if (size.n_children() === 2) {
-            let width = size.get_child_value(0);
-            let height = size.get_child_value(1);
-
-            this.window.set_default_size(width.get_int32(),
-                                         height.get_int32());
+        let size = Application.settings.get('window-size');
+        if (size.length === 2) {
+            let [width, height] = size;
+            this.window.set_default_size(width, height);
         }
 
-        let position = Application.settings.get_value('window-position');
-        if (position.n_children() === 2) {
-            let x = position.get_child_value(0);
-            let y = position.get_child_value(1);
+        let position = Application.settings.get('window-position');
+        if (position.length === 2) {
+            let [x, y] = position;
 
-            this.window.move(x.get_int32(),
-                             y.get_int32());
+            this.window.move(x, y);
         }
 
-        if (Application.settings.get_boolean('window-maximized'))
+        if (Application.settings.get('window-maximized'))
             this.window.maximize();
     },
 
@@ -203,7 +196,7 @@ const MainWindow = new Lang.Class({
             return;
 
         let maximized = (state & Gdk.WindowState.MAXIMIZED);
-        Application.settings.set_boolean('window-maximized', maximized);
+        Application.settings.set('window-maximized', maximized);
     },
 
     _onKeyPressEvent: function(widget, event) {
diff --git a/src/mapView.js b/src/mapView.js
index 4e33358..3292c44 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -98,8 +98,7 @@ const MapView = new Lang.Class({
     geocodeSearch: function(searchString, searchCompleteCallback) {
         let forward = Geocode.Forward.new_for_string(searchString);
         let places = [];
-        let answerCount =
-            Application.settings.get_value('max-search-results').get_int32();
+        let answerCount = Application.settings.get('max-search-results');
 
         forward.set_answer_count(answerCount);
         forward.search_async (null, (function(forward, res) {
diff --git a/src/settings.js b/src/settings.js
new file mode 100644
index 0000000..69e781d
--- /dev/null
+++ b/src/settings.js
@@ -0,0 +1,61 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2013 Mattias Bengtsson
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
+ */
+
+const Lang = imports.lang;
+
+const Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
+
+const Settings = new Lang.Class({
+    Name: "Settings",
+    Extends: Gio.Settings,
+
+    _types: {},
+
+    _init: function(schema) {
+        this.parent({ schema: schema });
+        this.list_keys().forEach((function(key) {
+            this._types[key] = this.get_range(key)
+                                   .get_child_value(1)
+                                   .get_variant()
+                                   .get_type()
+                                   .dup_string()
+                                   .slice(1);
+        }).bind(this));
+    },
+
+    get: function(name) {
+        return this.get_value(name).deep_unpack();
+    }, 
+    set: function(name, value) {
+        this.set_value(name, GLib.Variant.new (this._types[name], value));
+    }
+});
+
+// let s = new Settings('org.gnome.maps');
+// log(s.get('window-position'));
+// s.set('window-position', [800,900]);
+// log(s.get('window-position'));
+
+// OUTPUT:
+// Gjs-Message: JS LOG: 183,150
+// Gjs-Message: JS LOG: 800,900


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