[gnome-games] gnibbles: Use gsettings



commit 1e43c3aa2ce0c4127ebe6366f9b503803d6f4e0e
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Jan 3 16:23:14 2012 +1100

    gnibbles: Use gsettings

 gnibbles/data/org.gnome.gnibbles.gschema.xml.in |  116 +++++++++++------------
 gnibbles/src/main.c                             |   13 +++-
 gnibbles/src/preferences.c                      |   22 ++---
 gnibbles/src/properties.c                       |   91 ++++++------------
 gnibbles/src/properties.h                       |   19 ----
 5 files changed, 105 insertions(+), 156 deletions(-)
---
diff --git a/gnibbles/data/org.gnome.gnibbles.gschema.xml.in b/gnibbles/data/org.gnome.gnibbles.gschema.xml.in
index 2f581ff..44dd816 100644
--- a/gnibbles/data/org.gnome.gnibbles.gschema.xml.in
+++ b/gnibbles/data/org.gnome.gnibbles.gschema.xml.in
@@ -42,15 +42,7 @@
     </key>
     <child name="worm" schema="org.gnome.gnibbles.worm"/>
   </schema>
-  <schema id="org.gnome.gnibbles.worm" path="/org/gnome/gnibbles/worm/">
-    <child name="0" schema="org.gnome.gnibbles.worm.0"/>
-    <child name="1" schema="org.gnome.gnibbles.worm.1"/>
-    <child name="2" schema="org.gnome.gnibbles.worm.2"/>
-    <child name="3" schema="org.gnome.gnibbles.worm.3"/>
-    <child name="4" schema="org.gnome.gnibbles.worm.4"/>
-    <child name="5" schema="org.gnome.gnibbles.worm.5"/>
-  </schema>
-  <schema id="org.gnome.gnibbles.worm.0" path="/org/gnome/gnibbles/worm/0/">
+  <schema id="org.gnome.gnibbles.worm0" path="/org/gnome/gnibbles/worm0/">
     <key name="color" type="s">
       <default>'red'</default>
       <_summary>Color to use for worm</_summary>
@@ -61,28 +53,28 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
   </schema>
-  <schema id="org.gnome.gnibbles.worm.1" path="/org/gnome/gnibbles/worm/1/">
+  <schema id="org.gnome.gnibbles.worm1" path="/org/gnome/gnibbles/worm1/">
     <key name="color" type="s">
       <default>'green'</default>
       <_summary>Color to use for worm</_summary>
@@ -93,28 +85,28 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
   </schema>
-  <schema id="org.gnome.gnibbles.worm.2" path="/org/gnome/gnibbles/worm/2/">
+  <schema id="org.gnome.gnibbles.worm2" path="/org/gnome/gnibbles/worm2/">
     <key name="color" type="s">
       <default>'blue'</default>
       <_summary>Color to use for worm</_summary>
@@ -125,28 +117,28 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
   </schema>
-  <schema id="org.gnome.gnibbles.worm.3" path="/org/gnome/gnibbles/worm/3/">
+  <schema id="org.gnome.gnibbles.worm3" path="/org/gnome/gnibbles/worm3/">
     <key name="color" type="s">
       <default>'yellow'</default>
       <_summary>Color to use for worm</_summary>
@@ -157,28 +149,28 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
   </schema>
-  <schema id="org.gnome.gnibbles.worm.4" path="/org/gnome/gnibbles/worm/4/">
+  <schema id="org.gnome.gnibbles.worm4" path="/org/gnome/gnibbles/worm4/">
     <key name="color" type="s">
       <default>'cyan'</default>
       <_summary>Color to use for worm</_summary>
@@ -189,28 +181,28 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
   </schema>
-  <schema id="org.gnome.gnibbles.worm.5" path="/org/gnome/gnibbles/worm/5/">
+  <schema id="org.gnome.gnibbles.worm5" path="/org/gnome/gnibbles/worm5/">
     <key name="color" type="s">
       <default>'purple'</default>
       <_summary>Color to use for worm</_summary>
@@ -221,23 +213,23 @@
       <_summary>Use relative movement</_summary>
       <_description>Use relative movement (ie. left or right only).</_description>
     </key>
-    <key name="key-up" type="s">
-      <default>'Up'</default>
+    <key name="key-up" type="i">
+      <default>65362</default>
       <_summary>Move up</_summary>
       <_description>Key to use for motion up.</_description>
     </key>
-    <key name="key-down" type="s">
-      <default>'Down'</default>
+    <key name="key-down" type="i">
+      <default>65364</default>
       <_summary>Move down</_summary>
       <_description>Key to use for motion down.</_description>
     </key>
-    <key name="key-left" type="s">
-      <default>'Left'</default>
+    <key name="key-left" type="i">
+      <default>65361</default>
       <_summary>Move left</_summary>
       <_description>Key to use for motion left.</_description>
     </key>
-    <key name="key-right" type="s">
-      <default>'Right'</default>
+    <key name="key-right" type="i">
+      <default>65363</default>
       <_summary>Move right</_summary>
       <_description>Key to use for motion right.</_description>
     </key>
diff --git a/gnibbles/src/main.c b/gnibbles/src/main.c
index 11cc975..fc8e270 100644
--- a/gnibbles/src/main.c
+++ b/gnibbles/src/main.c
@@ -54,6 +54,8 @@
 #define DEFAULT_WIDTH 650
 #define DEFAULT_HEIGHT 520
 
+GSettings *settings;
+GSettings *worm_settings[NUMWORMS];
 GtkWidget *window;
 GtkWidget *statusbar;
 GtkWidget *notebook;
@@ -886,10 +888,11 @@ main (int argc, char **argv)
 {
   GOptionContext *context;
   gboolean retval;
+  int i;
   GError *error = NULL;
 
   g_set_application_name (_("Nibbles"));
-
+  
   if (gtk_clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) {
     GtkWidget *dialog = gtk_message_dialog_new (NULL,
                                                 GTK_DIALOG_MODAL,
@@ -902,6 +905,14 @@ main (int argc, char **argv)
     exit (1);
   }
 
+  settings = g_settings_new ("org.gnome.gnibbles");
+  for (i = 0; i < NUMWORMS; i++)
+  {
+    gchar *name = g_strdup_printf ("org.gnome.gnibbles.worm%d", i);
+    worm_settings[i] = g_settings_new (name);
+    g_free (name);
+  }
+
   context = g_option_context_new (NULL);
   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
   g_option_context_add_group (context, gtk_get_option_group (TRUE));
diff --git a/gnibbles/src/preferences.c b/gnibbles/src/preferences.c
index bad546f..c55a177 100644
--- a/gnibbles/src/preferences.c
+++ b/gnibbles/src/preferences.c
@@ -37,6 +37,8 @@
 #define KB_TEXT_HEIGHT 32
 #define KB_TEXT_NCHARS 8
 
+extern GSettings *settings;
+extern GSettings *worm_settings[NUMWORMS];
 static GtkWidget *pref_dialog = NULL;
 static gint unpause = 0;
 extern GtkWidget *window;
@@ -402,11 +404,6 @@ gnibbles_preferences_cb (GtkWidget * widget, gpointer data)
                     G_CALLBACK (num_worms_cb), num_ai);
 
   for (i = 0; i < NUMWORMS; i++) {
-    char up_key[64];
-    char down_key[64];
-    char left_key[64];
-    char right_key[64];
-
     buffer = g_strdup_printf ("%s %d", _("Worm"), i + 1);
     label = gtk_label_new (buffer);
     g_free (buffer);
@@ -418,18 +415,13 @@ gnibbles_preferences_cb (GtkWidget * widget, gpointer data)
 
     frame = games_frame_new (_("Keyboard Controls"));
 
-    controls = games_controls_list_new (KEY_PREFERENCES_GROUP);
-
-    g_snprintf (left_key, sizeof (left_key), KEY_WORM_LEFT, i);
-    g_snprintf (right_key, sizeof (right_key), KEY_WORM_RIGHT, i);
-    g_snprintf (up_key, sizeof (up_key), KEY_WORM_UP, i);
-    g_snprintf (down_key, sizeof (down_key), KEY_WORM_DOWN, i);
+    controls = games_controls_list_new_settings (worm_settings[i]);
 
     games_controls_list_add_controls (GAMES_CONTROLS_LIST (controls),
-                                      left_key, _("Move left"), GDK_KEY_Left,
-                                      right_key, _("Move right"), GDK_KEY_Right,
-                                      up_key, _("Move up"), GDK_KEY_Up,
-                                      down_key, _("Move down"), GDK_KEY_Down,
+                                      "key-left", _("Move left"), GDK_KEY_Left,
+                                      "key-right", _("Move right"), GDK_KEY_Right,
+                                      "key-up", _("Move up"), GDK_KEY_Up,
+                                      "key-down", _("Move down"), GDK_KEY_Down,
                                       NULL);
     gtk_container_add (GTK_CONTAINER (frame), controls);
 
diff --git a/gnibbles/src/properties.c b/gnibbles/src/properties.c
index 3e9507d..c8bb30a 100644
--- a/gnibbles/src/properties.c
+++ b/gnibbles/src/properties.c
@@ -34,6 +34,9 @@
 
 #define MAX_SPEED 4
 
+extern GSettings *settings;
+extern GSettings *worm_settings[NUMWORMS];
+
 typedef struct _ColorLookup ColorLookup;
 
 struct _ColorLookup {
@@ -79,17 +82,16 @@ void
 gnibbles_properties_update (GnibblesProperties * tmp)
 {
   gint i;
-  gchar buffer[256];
   gchar *category;
   gchar *color_name;
 
-  tmp->human = g_settings_get_integer (settings, KEY_NUM_WORMS, NULL);
+  tmp->human = g_settings_get_int (settings, "players");
   if (tmp->human < 0)
     tmp->human = 0;
   else if (tmp->human > NUMWORMS)
     tmp->human = NUMWORMS;
 
-  tmp->ai = g_settings_get_integer (settings, KEY_NUM_AI, NULL);
+  tmp->ai = g_settings_get_int (settings, "ai");
   if (tmp->ai < 0)
     tmp->ai = 0;
   else if (tmp->ai > NUMWORMS)
@@ -97,31 +99,26 @@ gnibbles_properties_update (GnibblesProperties * tmp)
 
   tmp->numworms = tmp->human + tmp->ai;
 
-  tmp->gamespeed = g_settings_get_integer (settings,
-                                           KEY_SPEED, NULL);
+  tmp->gamespeed = g_settings_get_int (settings, "speed");
   if (tmp->gamespeed < 1)
     tmp->gamespeed = 2;
   else if (tmp->gamespeed > MAX_SPEED)
     tmp->gamespeed = MAX_SPEED;
 
-  tmp->fakes = g_settings_get_boolean (settings,
-                                       KEY_FAKES, NULL);
+  tmp->fakes = g_settings_get_boolean (settings, "fakes");
 
-  tmp->random = g_settings_get_boolean (settings,
-                                        KEY_RANDOM, NULL);
+  tmp->random = g_settings_get_boolean (settings, "random");
 
-  tmp->startlevel = g_settings_get_integer (settings,
-                                            KEY_START_LEVEL, NULL);
+  tmp->startlevel = g_settings_get_int (settings, "start-level");
   if (tmp->startlevel < 1)
     tmp->startlevel = 1;
   if (tmp->startlevel > MAXLEVEL)
     tmp->startlevel = MAXLEVEL;
 
-  tmp->sound = g_settings_get_boolean (settings, KEY_SOUND, NULL);
+  tmp->sound = g_settings_get_boolean (settings, "sound");
   sound_enable (tmp->sound);
 
-  tmp->tilesize = g_settings_get_integer (settings,
-                                          KEY_TILE_SIZE, NULL);
+  tmp->tilesize = g_settings_get_int (settings, "tile-size");
   if (tmp->tilesize < 1)
     tmp->tilesize = 5;
   if (tmp->tilesize > 30)
@@ -130,9 +127,7 @@ gnibbles_properties_update (GnibblesProperties * tmp)
   for (i = 0; i < NUMWORMS; i++) {
     tmp->wormprops[i] = g_slice_new0 (GnibblesWormProps);
 
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_COLOR, i);
-    color_name = g_settings_get_string_with_default (settings,
-                                                     buffer, "red");
+    color_name = g_settings_get_string (worm_settings[i], "color");
     tmp->wormprops[i]->color = colorval_from_name (color_name);
     g_free (color_name);
 
@@ -141,20 +136,11 @@ gnibbles_properties_update (GnibblesProperties * tmp)
     if (tmp->wormprops[i]->color > WORMRED + NUM_COLORS)
       tmp->wormprops[i]->color = (i % NUM_COLORS) + WORMRED;
 
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_REL_MOVE, i);
-    tmp->wormprops[i]->relmove = g_settings_get_boolean (settings, buffer);
-
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_UP, i);
-    tmp->wormprops[i]->up = g_settings_get_int (settings, buffer);
-
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_DOWN, i);
-    tmp->wormprops[i]->down = g_settings_get_int (settings, buffer);
-
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_LEFT, i);
-    tmp->wormprops[i]->left = g_settings_get_int (settings, buffer);
-
-    g_snprintf (buffer, sizeof (buffer), KEY_WORM_RIGHT, i);
-    tmp->wormprops[i]->right = g_settings_get_int (settings, buffer);
+    tmp->wormprops[i]->relmove = g_settings_get_boolean (worm_settings[i], "move-relative");
+    tmp->wormprops[i]->up = g_settings_get_int (worm_settings[i], "key-up");
+    tmp->wormprops[i]->down = g_settings_get_int (worm_settings[i], "key-down");
+    tmp->wormprops[i]->left = g_settings_get_int (worm_settings[i], "key-left");
+    tmp->wormprops[i]->right = g_settings_get_int (worm_settings[i], "key-right");
   }
 
   category = g_strdup_printf ("%d.%d", tmp->gamespeed, tmp->fakes);
@@ -163,16 +149,12 @@ gnibbles_properties_update (GnibblesProperties * tmp)
 }
 
 static void
-conf_value_changed_cb (GamesConf *conf,
-                       const char *group,
-                       const char *key,
-                       gpointer data)
+settings_changed_cb (GSettings *settings,
+                     const char *key,
+                     gpointer data)
 {
   GnibblesProperties *props = (GnibblesProperties *) data;
 
-  if (!group || strcmp (group, settings) != 0)
-    return;
-
   gnibbles_properties_update (props);
 }
 
@@ -183,10 +165,7 @@ gnibbles_properties_new (void)
 
   props = g_slice_new0 (GnibblesProperties);
 
-  props->conf_notify_id = g_signal_connect (g_settings_get_default (),
-                                            "value-changed",
-                                            G_CALLBACK (conf_value_changed_cb),
-                                            props);
+  props->conf_notify_id = g_signal_connect (settings, "changed", G_CALLBACK (settings_changed_cb), props);
 
   gnibbles_properties_update (props);
 
@@ -201,8 +180,7 @@ gnibbles_properties_destroy (GnibblesProperties * props)
   for (i = 0; i < NUMWORMS; i++)
     g_slice_free (GnibblesWormProps, props->wormprops[i]);
 
-  g_signal_handler_disconnect (g_settings_get_default (),
-                               props->conf_notify_id);
+  g_signal_handler_disconnect (settings, props->conf_notify_id);
 
   g_slice_free (GnibblesProperties, props);
 }
@@ -212,69 +190,64 @@ gnibbles_properties_destroy (GnibblesProperties * props)
 void
 gnibbles_properties_set_worms_number (gint value)
 {
-  g_settings_set_integer (settings, KEY_NUM_WORMS, value);
+  g_settings_set_int (settings, "players", value);
 }
 
 void
 gnibbles_properties_set_ai_number (gint value)
 {
-  g_settings_set_integer (settings, KEY_NUM_AI, value);
+  g_settings_set_int (settings, "ai", value);
 }
 
 void
 gnibbles_properties_set_speed (gint value)
 {
-  g_settings_set_integer (settings, KEY_SPEED, value);
+  g_settings_set_int (settings, "speed", value);
 }
 
 void
 gnibbles_properties_set_fakes (gboolean value)
 {
-  g_settings_set_boolean (settings, KEY_FAKES, value);
+  g_settings_set_boolean (settings, "fakes", value);
 }
 
 void
 gnibbles_properties_set_random (gboolean value)
 {
-  g_settings_set_boolean (settings, KEY_RANDOM, value);
+  g_settings_set_boolean (settings, "random", value);
 }
 
 void
 gnibbles_properties_set_start_level (gint value)
 {
-  g_settings_set_integer (settings, KEY_START_LEVEL, value);
+  g_settings_set_int (settings, "start-level", value);
 }
 
 void
 gnibbles_properties_set_sound (gboolean value)
 {
-  g_settings_set_boolean (settings, KEY_SOUND, value);
+  g_settings_set_boolean (settings, "sound", value);
 }
 
 void
 gnibbles_properties_set_tile_size (gint value)
 {
-  g_settings_set_integer (settings, KEY_TILE_SIZE, value);
+  g_settings_set_int (settings, "tile-size", value);
 }
 
 void
 gnibbles_properties_set_worm_relative_movement (gint i, gboolean value)
 {
-  char key[64];
-  g_snprintf (key, sizeof (key), KEY_WORM_REL_MOVE, i);
-  g_settings_set_boolean (settings, key, value);
+  g_settings_set_boolean (worm_settings[i], "move-relative", value);
 }
 
 void
 gnibbles_properties_set_worm_color (gint i, gint value)
 {
-  char key[64];
   char *color_name;
 
-  g_snprintf (key, sizeof (key), KEY_WORM_COLOR, i);
-
   color_name = colorval_name (value);
-  g_settings_set_string (settings, key, color_name);
+  g_settings_set_string (worm_settings[i], "color", color_name);
 }
 
 void
diff --git a/gnibbles/src/properties.h b/gnibbles/src/properties.h
index 2d41a97..ef1630d 100644
--- a/gnibbles/src/properties.h
+++ b/gnibbles/src/properties.h
@@ -26,25 +26,6 @@
 
 #include "gnibbles.h"
 
-#define KEY_PREFERENCES_GROUP "preferences"
-
-#define KEY_NUM_WORMS "players"
-#define KEY_NUM_AI "ai"
-#define KEY_SPEED "speed"
-#define KEY_FAKES "fakes"
-#define KEY_RANDOM "random"
-#define KEY_START_LEVEL "start_level"
-#define KEY_SOUND "sound"
-#define KEY_TILE_SIZE "tile_size"
-
-#define KEY_WORM_DIR "worm/%d"
-#define KEY_WORM_COLOR "worm/%d/color"
-#define KEY_WORM_REL_MOVE "worm/%d/move_relative"
-#define KEY_WORM_UP "worm/%d/key_up"
-#define KEY_WORM_DOWN "worm/%d/key_down"
-#define KEY_WORM_LEFT "worm/%d/key_left"
-#define KEY_WORM_RIGHT "worm/%d/key_right"
-
 typedef struct {
   gint color;
   gboolean relmove;



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