[tracker/gsettings-rebased: 6/8] store: Use GSettings for configuration storage.



commit 4f0a7e73af0e13065c85d59bf0caedc5ca8fe3f1
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Feb 17 15:53:21 2011 +0100

    store: Use GSettings for configuration storage.

 src/tracker-store/tracker-config.c    |  238 ++++-----------------------------
 src/tracker-store/tracker-config.h    |    8 +-
 src/tracker-store/tracker-config.vapi |    1 -
 3 files changed, 32 insertions(+), 215 deletions(-)
---
diff --git a/src/tracker-store/tracker-config.c b/src/tracker-store/tracker-config.c
index adc1bb8..64e34ec 100644
--- a/src/tracker-store/tracker-config.c
+++ b/src/tracker-store/tracker-config.c
@@ -25,32 +25,12 @@
 #include <glib.h>
 #include <gio/gio.h>
 
+#include <libtracker-common/tracker-enum-types.h>
+#include <libtracker-common/tracker-config-file.h>
 #include <libtracker-common/tracker-keyfile-object.h>
 
 #include "tracker-config.h"
 
-#define TRACKER_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TRACKER_TYPE_CONFIG, TrackerConfigPrivate))
-
-/* GKeyFile defines */
-#define GROUP_GENERAL     "General"
-
-/* Default values (only critical logs by default) */
-#define DEFAULT_VERBOSITY 0
-
-/* typedef struct TrackerConfigPrivate TrackerConfigPrivate; */
-
-typedef struct {
-	/* General */
-	gint verbosity;
-} TrackerConfigPrivate;
-
-typedef struct {
-	GType type;
-	const gchar *property;
-	const gchar *group;
-	const gchar *key;
-} ObjectToKeyFile;
-
 static void config_set_property         (GObject       *object,
                                          guint          param_id,
                                          const GValue  *value,
@@ -61,23 +41,18 @@ static void config_get_property         (GObject       *object,
                                          GParamSpec    *pspec);
 static void config_finalize             (GObject       *object);
 static void config_constructed          (GObject       *object);
-static void config_create_with_defaults (TrackerConfig *config,
-                                         GKeyFile      *key_file,
-                                         gboolean       overwrite);
-static void config_load                 (TrackerConfig *config);
 
 enum {
 	PROP_0,
-
-	/* General */
 	PROP_VERBOSITY,
 };
 
-static ObjectToKeyFile conversions[] = {
-	{ G_TYPE_INT, "verbosity", GROUP_GENERAL,  "Verbosity" },
+static TrackerConfigMigrationEntry migration[] = {
+	{ G_TYPE_ENUM, "General", "Verbosity", "verbosity" },
+	{ 0 }
 };
 
-G_DEFINE_TYPE (TrackerConfig, tracker_config, TRACKER_TYPE_CONFIG_FILE);
+G_DEFINE_TYPE (TrackerConfig, tracker_config, G_TYPE_SETTINGS);
 
 static void
 tracker_config_class_init (TrackerConfigClass *klass)
@@ -89,18 +64,14 @@ tracker_config_class_init (TrackerConfigClass *klass)
 	object_class->finalize     = config_finalize;
 	object_class->constructed  = config_constructed;
 
-	/* General */
 	g_object_class_install_property (object_class,
 	                                 PROP_VERBOSITY,
-	                                 g_param_spec_int ("verbosity",
-	                                                   "Log verbosity",
-	                                                   " Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
-	                                                   0,
-	                                                   3,
-	                                                   DEFAULT_VERBOSITY,
-	                                                   G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
-
-	g_type_class_add_private (object_class, sizeof (TrackerConfigPrivate));
+	                                 g_param_spec_enum ("verbosity",
+	                                                    "Log verbosity",
+	                                                    "Log verbosity (0=errors, 1=minimal, 2=detailed, 3=debug)",
+	                                                    TRACKER_TYPE_VERBOSITY,
+	                                                    TRACKER_VERBOSITY_ERRORS,
+	                                                    G_PARAM_READWRITE));
 }
 
 static void
@@ -115,10 +86,9 @@ config_set_property (GObject      *object,
                      GParamSpec           *pspec)
 {
 	switch (param_id) {
-		/* General */
 	case PROP_VERBOSITY:
 		tracker_config_set_verbosity (TRACKER_CONFIG (object),
-		                              g_value_get_int (value));
+		                              g_value_get_enum (value));
 		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -132,14 +102,10 @@ config_get_property (GObject    *object,
                      GValue     *value,
                      GParamSpec *pspec)
 {
-	TrackerConfigPrivate *priv;
-
-	priv = TRACKER_CONFIG_GET_PRIVATE (object);
-
 	switch (param_id) {
 		/* General */
 	case PROP_VERBOSITY:
-		g_value_set_int (value, priv->verbosity);
+		g_value_set_enum (value, tracker_config_get_verbosity (TRACKER_CONFIG (object)));
 		break;
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
@@ -160,191 +126,45 @@ config_finalize (GObject *object)
 static void
 config_constructed (GObject *object)
 {
-	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
-
-	config_load (TRACKER_CONFIG (object));
-}
-
-static void
-config_create_with_defaults (TrackerConfig *config,
-                             GKeyFile      *key_file,
-                             gboolean       overwrite)
-{
-	gint i;
-
-	g_message ("Loading defaults into GKeyFile...");
-
-	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
-		gboolean has_key;
-
-		has_key = g_key_file_has_key (key_file,
-		                              conversions[i].group,
-		                              conversions[i].key,
-		                              NULL);
-		if (!overwrite && has_key) {
-			continue;
-		}
-
-		switch (conversions[i].type) {
-		case G_TYPE_INT:
-			g_key_file_set_integer (key_file,
-			                        conversions[i].group,
-			                        conversions[i].key,
-			                        tracker_keyfile_object_default_int (config,
-			                                                            conversions[i].property));
-			break;
-
-		case G_TYPE_BOOLEAN:
-			g_key_file_set_boolean (key_file,
-			                        conversions[i].group,
-			                        conversions[i].key,
-			                        tracker_keyfile_object_default_boolean (config,
-			                                                                conversions[i].property));
-			break;
-
-		default:
-			g_assert_not_reached ();
-			break;
-		}
-
-		g_key_file_set_comment (key_file,
-		                        conversions[i].group,
-		                        conversions[i].key,
-		                        tracker_keyfile_object_blurb (config,
-		                                                      conversions[i].property),
-		                        NULL);
-	}
-}
-
-static void
-config_load (TrackerConfig *config)
-{
-	TrackerConfigFile *file;
-	gint i;
+	TrackerConfigFile *config_file;
 
-	file = TRACKER_CONFIG_FILE (config);
-	config_create_with_defaults (config, file->key_file, FALSE);
-
-	if (!file->file_exists) {
-		tracker_config_file_save (file);
-	}
-
-	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
-		gboolean has_key;
-
-		has_key = g_key_file_has_key (file->key_file,
-		                              conversions[i].group,
-		                              conversions[i].key,
-		                              NULL);
-
-		switch (conversions[i].type) {
-		case G_TYPE_INT:
-			tracker_keyfile_object_load_int (G_OBJECT (file),
-			                                 conversions[i].property,
-			                                 file->key_file,
-			                                 conversions[i].group,
-			                                 conversions[i].key);
-			break;
-
-		case G_TYPE_BOOLEAN:
-			tracker_keyfile_object_load_boolean (G_OBJECT (file),
-			                                     conversions[i].property,
-			                                     file->key_file,
-			                                     conversions[i].group,
-			                                     conversions[i].key);
-			break;
-
-		default:
-			g_assert_not_reached ();
-			break;
-		}
-	}
-}
-
-static gboolean
-config_save (TrackerConfig *config)
-{
-	TrackerConfigFile *file;
-	gint i;
-
-	file = TRACKER_CONFIG_FILE (config);
-
-	if (!file->key_file) {
-		g_critical ("Could not save config, GKeyFile was NULL, has the config been loaded?");
-
-		return FALSE;
-	}
-
-	g_message ("Setting details to GKeyFile object...");
+	(G_OBJECT_CLASS (tracker_config_parent_class)->constructed) (object);
 
-	for (i = 0; i < G_N_ELEMENTS (conversions); i++) {
-		switch (conversions[i].type) {
-		case G_TYPE_INT:
-			tracker_keyfile_object_save_int (file,
-			                                 conversions[i].property,
-			                                 file->key_file,
-			                                 conversions[i].group,
-			                                 conversions[i].key);
-			break;
+	g_settings_delay (G_SETTINGS (object));
 
-		case G_TYPE_BOOLEAN:
-			tracker_keyfile_object_save_boolean (file,
-			                                     conversions[i].property,
-			                                     file->key_file,
-			                                     conversions[i].group,
-			                                     conversions[i].key);
-			break;
+	/* Migrate keyfile-based configuration */
+	config_file = tracker_config_file_new ();
 
-		default:
-			g_assert_not_reached ();
-			break;
-		}
+	if (config_file) {
+		tracker_config_file_migrate (config_file, G_SETTINGS (object), migration);
+		g_object_unref (config_file);
 	}
-
-	return tracker_config_file_save (TRACKER_CONFIG_FILE (config));
 }
 
 TrackerConfig *
 tracker_config_new (void)
 {
-	return g_object_new (TRACKER_TYPE_CONFIG, NULL);
-}
-
-gboolean
-tracker_config_save (TrackerConfig *config)
-{
-	g_return_val_if_fail (TRACKER_IS_CONFIG (config), FALSE);
-
-	return config_save (config);
+	return g_object_new (TRACKER_TYPE_CONFIG,
+			     "schema", "org.freedesktop.Tracker.Store",
+			     "path", "/org/freedesktop/tracker/store/",
+			     NULL);
 }
 
 gint
 tracker_config_get_verbosity (TrackerConfig *config)
 {
-	TrackerConfigPrivate *priv;
-
-	g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_VERBOSITY);
+	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
 
-	priv = TRACKER_CONFIG_GET_PRIVATE (config);
-
-	return priv->verbosity;
+	return g_settings_get_enum (G_SETTINGS (config), "verbosity");
 }
 
 void
 tracker_config_set_verbosity (TrackerConfig *config,
                               gint           value)
 {
-	TrackerConfigPrivate *priv;
-
 	g_return_if_fail (TRACKER_IS_CONFIG (config));
 
-	if (!tracker_keyfile_object_validate_int (config, "verbosity", value)) {
-		return;
-	}
-
-	priv = TRACKER_CONFIG_GET_PRIVATE (config);
-
-	priv->verbosity = value;
+	g_settings_set_enum (G_SETTINGS (config), "verbosity", value);
 	g_object_notify (G_OBJECT (config), "verbosity");
 }
 
diff --git a/src/tracker-store/tracker-config.h b/src/tracker-store/tracker-config.h
index a7dbe6b..17fd48c 100644
--- a/src/tracker-store/tracker-config.h
+++ b/src/tracker-store/tracker-config.h
@@ -21,8 +21,7 @@
 #define __TRACKER_STORE_CONFIG_H__
 
 #include <glib-object.h>
-
-#include <libtracker-common/tracker-config-file.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -37,17 +36,16 @@ typedef struct TrackerConfig TrackerConfig;
 typedef struct TrackerConfigClass TrackerConfigClass;
 
 struct TrackerConfig {
-	TrackerConfigFile parent;
+	GSettings parent;
 };
 
 struct TrackerConfigClass {
-	TrackerConfigFileClass parent_class;
+	GSettingsClass parent_class;
 };
 
 GType          tracker_config_get_type                             (void) G_GNUC_CONST;
 
 TrackerConfig *tracker_config_new                                  (void);
-gboolean       tracker_config_save                                 (TrackerConfig *config);
 gint           tracker_config_get_verbosity                        (TrackerConfig *config);
 
 void           tracker_config_set_verbosity                        (TrackerConfig *config,
diff --git a/src/tracker-store/tracker-config.vapi b/src/tracker-store/tracker-config.vapi
index 72888da..e8d50b5 100644
--- a/src/tracker-store/tracker-config.vapi
+++ b/src/tracker-store/tracker-config.vapi
@@ -21,7 +21,6 @@ namespace Tracker {
 	[CCode (cheader_filename = "tracker-store/tracker-config.h")]
 	public class Config : ConfigFile {
 		public Config ();
-		public bool save ();
 		public int verbosity { get; set; }
 	}
 }



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