[tracker/gsettings: 5/7] writeback: Use GSettings for configuration storage



commit ed97a0398c6b9569b21b301afedd7a767e2c6957
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Feb 17 14:54:46 2011 +0100

    writeback: Use GSettings for configuration storage

 src/tracker-writeback/tracker-config.c |  252 ++++----------------------------
 src/tracker-writeback/tracker-config.h |   10 +-
 2 files changed, 32 insertions(+), 230 deletions(-)
---
diff --git a/src/tracker-writeback/tracker-config.c b/src/tracker-writeback/tracker-config.c
index ac9748e..9e1cfb3 100644
--- a/src/tracker-writeback/tracker-config.c
+++ b/src/tracker-writeback/tracker-config.c
@@ -25,32 +25,14 @@
 #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 <libtracker-common/tracker-file-utils.h>
 #include <libtracker-common/tracker-type-utils.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 */
-#define DEFAULT_VERBOSITY                        0
-
-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,52 +43,37 @@ static void     config_get_property         (GObject           *object,
                                              GParamSpec        *pspec);
 static void     config_finalize             (GObject           *object);
 static void     config_constructed          (GObject           *object);
-static void     config_changed              (TrackerConfigFile *file);
-static void     config_load                 (TrackerConfig     *config);
-static gboolean config_save                 (TrackerConfig     *config);
-static void     config_create_with_defaults (TrackerConfig     *config,
-                                             GKeyFile          *key_file,
-                                             gboolean           overwrite);
 
 enum {
 	PROP_0,
-
-	/* General */
 	PROP_VERBOSITY,
 };
 
-static ObjectToKeyFile conversions[] = {
-	/* General */
-	{ 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)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
-	TrackerConfigFileClass *config_file_class = TRACKER_CONFIG_FILE_CLASS (klass);
 
 	object_class->set_property = config_set_property;
 	object_class->get_property = config_get_property;
 	object_class->finalize     = config_finalize;
 	object_class->constructed  = config_constructed;
 
-	config_file_class->changed = config_changed;
-
-	/* 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
@@ -121,10 +88,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);
@@ -138,14 +104,9 @@ 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);
@@ -162,199 +123,44 @@ 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_changed (TrackerConfigFile *file)
-{
-	/* Reload config */
-	config_load (TRACKER_CONFIG (file));
-}
-
-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 ();
-		}
-
-		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;
-		}
-	}
-}
-
-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 (file);
 }
 
 TrackerConfig *
 tracker_config_new (void)
 {
-	return g_object_new (TRACKER_TYPE_CONFIG, NULL);
-}
-
-
-TrackerConfig *
-tracker_config_new_with_domain (const gchar *domain)
-{
-	return g_object_new (TRACKER_TYPE_CONFIG,  "domain", domain, 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.Writeback",
+			     "path", "/org/freedesktop/tracker/writeback/",
+			     NULL);
 }
 
 gint
 tracker_config_get_verbosity (TrackerConfig *config)
 {
-	TrackerConfigPrivate *priv;
-
-	g_return_val_if_fail (TRACKER_IS_CONFIG (config), DEFAULT_VERBOSITY);
-
-	priv = TRACKER_CONFIG_GET_PRIVATE (config);
+	g_return_val_if_fail (TRACKER_IS_CONFIG (config), 0);
 
-	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-writeback/tracker-config.h b/src/tracker-writeback/tracker-config.h
index d885128..9392682 100644
--- a/src/tracker-writeback/tracker-config.h
+++ b/src/tracker-writeback/tracker-config.h
@@ -21,8 +21,7 @@
 #define __TRACKER_WRITEBACK_CONFIG_H__
 
 #include <glib-object.h>
-
-#include <libtracker-common/tracker-config-file.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -37,19 +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);
-TrackerConfig *tracker_config_new_with_domain                      (const gchar *domain);
-
-gboolean       tracker_config_save                                 (TrackerConfig *config);
 
 gint           tracker_config_get_verbosity                        (TrackerConfig *config);
 void           tracker_config_set_verbosity                        (TrackerConfig *config,



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