[gnome-games] libgames-support: Add games_conf_save()



commit 0dc0b05300bb5088d10d5072cdc1e967b91403b7
Author: Christian Persch <chpe gnome org>
Date:   Fri Nov 27 20:00:24 2009 +0100

    libgames-support: Add games_conf_save()
    
    To explicitly flush the settings to disk. Tracks "dirty" status, so it
    only writes when necessary.

 libgames-support/games-conf.c |  114 +++++++++++++++++++++++++++-------------
 libgames-support/games-conf.h |    2 +
 2 files changed, 79 insertions(+), 37 deletions(-)
---
diff --git a/libgames-support/games-conf.c b/libgames-support/games-conf.c
index 153fc2c..19ee713 100644
--- a/libgames-support/games-conf.c
+++ b/libgames-support/games-conf.c
@@ -50,6 +50,7 @@ struct _GamesConfPrivate {
   char *main_group;
 #endif
   guint need_init : 1;
+  guint dirty : 1;
 };
 
 enum
@@ -351,6 +352,19 @@ get_gconf_value_type_from_schema (const char *key_name)
 
 #endif /* HAVE_GNOME */
 
+#ifndef HAVE_GNOME
+
+static void
+mark_dirty_cb (GamesConf *conf)
+{
+  GamesConfPrivate *priv = conf->priv;
+
+  g_print ("marking dirty!\n");
+  priv->dirty = TRUE;
+}
+
+#endif /* !HAVE_GNOME */
+
 /* Class implementation */
 
 static void
@@ -361,6 +375,11 @@ games_conf_init (GamesConf *conf)
   priv = conf->priv = GAMES_CONF_GET_PRIVATE (conf);
 
   priv->need_init = FALSE;
+  priv->dirty = FALSE;
+
+#ifndef HAVE_GNOME
+  g_signal_connect (conf, "value-changed", G_CALLBACK (mark_dirty_cb), NULL);
+#endif
 }
 
 static GObject *
@@ -456,44 +475,8 @@ games_conf_finalize (GObject *object)
   priv->gconf_client = NULL;
 
 #else /* !HAVE_GNOME */
-  char *game_name, *conf_file, *conf_dir, *data = NULL;
-  gsize len = 0;
-  GError *error = NULL;
-
-  game_name = g_ascii_strdown (priv->game_name, -1);
-  conf_file = g_build_filename (g_get_user_config_dir (), "gnome-games", game_name, NULL);
 
-  /* Ensure the directory exists */
-  conf_dir = g_build_filename (g_get_user_config_dir (), "gnome-games", NULL);
-  /* Mode 0700 per the XDG basedir spec */
-  if (g_mkdir_with_parents (conf_dir, 0700) < 0) {
-    int err = errno;
-
-    if (err != EEXIST) {
-      g_warning ("Failed to create config directory \"%s\": %s\n", conf_dir, g_strerror (err));
-      goto loser;
-    }
-  }
-
-  data = g_key_file_to_data (priv->key_file, &len, &error);
-  if (!data) {
-    g_warning ("Failed to save settings to \"%s\": %s",
-               conf_file, error->message);
-    g_error_free (error);
-    goto loser;
-  }
-
-  if (!g_file_set_contents (conf_file, data, len, &error)) {
-    g_warning ("Failed to save settings to \"%s\": %s",
-              conf_file, error->message);
-    g_error_free (error);
-  }
-
-loser:
-  g_free (data);
-  g_free (conf_file);
-  g_free (conf_dir);
-  g_free (game_name);
+  games_conf_save ();
 
   g_free (priv->main_group);
   g_key_file_free (priv->key_file);
@@ -619,6 +602,63 @@ games_conf_get_default (void)
 }
 
 /**
+ * games_conf_save:
+ *
+ * Ensures settings are written to disk.
+ */
+void
+games_conf_save (void)
+{
+#ifndef HAVE_GNOME
+  GamesConfPrivate *priv = instance->priv;
+  char *game_name, *conf_file, *conf_dir, *data = NULL;
+  gsize len = 0;
+  GError *error = NULL;
+
+  if (!priv->dirty)
+    return;
+
+  game_name = g_ascii_strdown (priv->game_name, -1);
+  conf_dir = g_build_filename (g_get_user_config_dir (), "gnome-games", NULL);
+  conf_file = g_build_filename (conf_dir, game_name, NULL);
+
+  /* Ensure the directory exists; mode 0700 per the XDG basedir spec. */
+  if (g_mkdir_with_parents (conf_dir, 0700) < 0) {
+    int err = errno;
+
+    if (err != EEXIST) {
+      g_warning ("Failed to create config directory \"%s\": %s\n", conf_dir, g_strerror (err));
+      goto loser;
+    }
+  }
+
+  data = g_key_file_to_data (priv->key_file, &len, &error);
+  if (!data) {
+    g_warning ("Failed to save settings to \"%s\": %s",
+               conf_file, error->message);
+    g_error_free (error);
+    goto loser;
+  }
+
+  if (!g_file_set_contents (conf_file, data, len, &error)) {
+    g_warning ("Failed to save settings to \"%s\": %s",
+              conf_file, error->message);
+    g_error_free (error);
+    goto loser;
+  }
+
+  /* Sucessfully saved */
+  priv->dirty = FALSE;
+
+loser:
+  g_free (data);
+  g_free (conf_file);
+  g_free (conf_dir);
+  g_free (game_name);
+#endif /* !HAVE_GNOME */
+}
+
+/**
  * games_conf_get_string:
  * @group: the group name, or %NULL to use the default group
  * @key: the key name
diff --git a/libgames-support/games-conf.h b/libgames-support/games-conf.h
index 9d31a1c..5bef9d7 100644
--- a/libgames-support/games-conf.h
+++ b/libgames-support/games-conf.h
@@ -50,6 +50,8 @@ void games_conf_shutdown (void);
 
 GamesConf *games_conf_get_default (void);
 
+void games_conf_save (void);
+
 char *games_conf_get_string (const char *group, const char *key,
                              GError ** error);
 



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