Introduce preferences file
authorPhilip Chimento <philip.chimento@gmail.com>
Sat, 26 Feb 2011 16:15:35 +0000 (17:15 +0100)
committerPhilip Chimento <philip.chimento@gmail.com>
Sat, 26 Feb 2011 16:15:35 +0000 (17:15 +0100)
We now use a GSettings object with a custom backend that writes to a
configuration file, ~/.chimara. Right now there is only one example
setting, "flep". It's easy to bind the setting to a property of another
GObject, like the "active" property of a checkbox.

Still need to check how other GLib applications install the settings
file properly in their Makefile.am, and how to work with a default
configuration file such as /etc/chimara if the one in the home directory
is not present.

player/Makefile.am
player/chimara.ui
player/main.c
player/org.chimara-if.gschema.xml [new file with mode: 0644]
player/preferences.c

index ebc4c930dd870823a8f73472757ca06de0bda7d8..b548e64df45788a8a6942ffbab3ab15d952fa867 100644 (file)
@@ -24,4 +24,10 @@ chimara_CPPFLAGS = $(AM_CPPFLAGS) \
 chimara_CFLAGS = @TEST_CFLAGS@ $(AM_CFLAGS)
 chimara_LDADD = @TEST_LIBS@ $(top_builddir)/libchimara/libchimara.la
 
+schemasdir = $(datadir)/glib-2.0/schemas
+dist_schemas_DATA = org.chimara-if.gschema.xml
+
+install-data-hook:
+       glib-compile-schemas $(datadir)/glib-2.0/schemas
+
 endif
index d9d219c440463c9b15241ce4770a1a1369358b76..be9651b336c4a9a9398629522d9d3ccd5e53a005 100644 (file)
@@ -381,9 +381,16 @@ Philip Chimento</property>
             <property name="visible">True</property>
             <property name="can_focus">True</property>
             <child>
-              <object class="GtkFixed" id="fixed1">
+              <object class="GtkCheckButton" id="flep">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
+                <property name="can_focus">True</property>
+                <child>
+                  <object class="GtkLabel" id="flep-label">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label">Turn on flepping</property>
+                  </object>
+                </child>
               </object>
             </child>
             <child type="tab">
index ff5c681cdf6588e5e182b8cffd38c1988b0542d9..01de3982e1b8199815c8483dd43f776e1612f25d 100644 (file)
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
+/* Use a custom GSettings backend for our preferences file */
+#define G_SETTINGS_ENABLE_BACKEND
+#include <gio/gsettingsbackend.h>
+
 #include "error.h"
 #include <libchimara/chimara-glk.h>
 #include <libchimara/chimara-if.h>
@@ -56,6 +60,7 @@ GtkBuilder *builder = NULL;
 GtkWidget *aboutwindow = NULL;
 GtkWidget *prefswindow = NULL;
 GtkWidget *toolbar = NULL;
+GSettings *settings = NULL;
 
 GObject *
 load_object(const gchar *name)
@@ -206,7 +211,7 @@ create_window(void)
        g_signal_connect(glk, "notify::story-name", G_CALLBACK(change_window_title), window);
        
        /* Create preferences window */
-       //preferences_create(CHIMARA_GLK(glk));
+       preferences_create(CHIMARA_GLK(glk));
 }
 
 int
@@ -225,6 +230,11 @@ main(int argc, char *argv[])
        gdk_threads_init();
        gtk_init(&argc, &argv);
 
+       /* Initialize settings file */
+       gchar *keyfile = g_build_filename(g_get_home_dir(), ".chimara", NULL);
+       GSettingsBackend *backend = g_keyfile_settings_backend_new(keyfile, "/", "player");
+       settings = g_settings_new_with_backend("org.chimara-if.chimara-player", backend);
+
        create_window();
        gtk_widget_show_all(window);
 
diff --git a/player/org.chimara-if.gschema.xml b/player/org.chimara-if.gschema.xml
new file mode 100644 (file)
index 0000000..be81e95
--- /dev/null
@@ -0,0 +1,17 @@
+<schemalist>
+  <schema id="org.chimara-if.chimara-player" path="/">
+    
+    <!-- Keys and key names are subject to change without notice, until we have
+     a spec-->
+    <!-- "b" = boolean; see documentation of GVariantType -->
+    
+    <key name="flep" type="b"> 
+      <default>false</default>
+      <summary>Flepping</summary>
+      <description>
+        Whether to flep or not while gronking a bloop.
+      </description>
+    </key>
+  
+  </schema>
+</schemalist>
\ No newline at end of file
index b7cc3adf678e6600f5d344bdd756b183f0031f0f..504cbd6227e31fb5ad70089c35f82fd49beb76ee 100644 (file)
@@ -86,6 +86,11 @@ preferences_create(ChimaraGlk *glk)
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
 
        g_signal_connect(selection, "changed", G_CALLBACK(style_tree_select_callback), glk);
+
+       /* Bind the preferences to the entries in the preferences file */
+       extern GSettings *settings;
+       GObject *flep = G_OBJECT( load_object("flep") );
+       g_settings_bind(settings, "flep", flep, "active", G_SETTINGS_BIND_DEFAULT);
 }
 
 static void