[grilo] [core] Use string keys to identify configuration parameters



commit 430c126f5a8498ef478fba8a0a902521b451a6ab
Author: Iago Toral Quiroga <itoral igalia com>
Date:   Tue Apr 20 17:54:17 2010 +0200

    [core] Use string keys to identify configuration parameters

 src/data/grl-config.c |  115 +++++++++++++++++++++++++++++++++++++++--
 src/data/grl-config.h |  136 ++++++++++++++++++++++++++-----------------------
 2 files changed, 182 insertions(+), 69 deletions(-)
---
diff --git a/src/data/grl-config.c b/src/data/grl-config.c
index 12a5117..3f20bb3 100644
--- a/src/data/grl-config.c
+++ b/src/data/grl-config.c
@@ -25,7 +25,6 @@
 /**
  * SECTION:grl-config
  * @short_description: Configuration data storage
- * @see_also: #GrlData
  *
  * This class is used to store configuration settings used by plugins.
  */
@@ -35,10 +34,26 @@
 #undef G_LOG_DOMAIN
 #define G_LOG_DOMAIN "grl-config"
 
+#define GRL_CONFIG_GET_PRIVATE(o)                                         \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GRL_TYPE_CONFIG, GrlConfigPrivate))
+
+struct _GrlConfigPrivate {
+  GHashTable *config;
+};
+
 static void grl_config_dispose (GObject *object);
 static void grl_config_finalize (GObject *object);
 
-G_DEFINE_TYPE (GrlConfig, grl_config, GRL_TYPE_DATA);
+G_DEFINE_TYPE (GrlConfig, grl_config, G_TYPE_OBJECT);
+
+static void
+free_val (GValue *val)
+{
+  if (val) {
+    g_value_unset (val);
+    g_free (val);
+  }
+}
 
 static void
 grl_config_class_init (GrlConfigClass *klass)
@@ -47,11 +62,18 @@ grl_config_class_init (GrlConfigClass *klass)
 
   gobject_class->dispose = grl_config_dispose;
   gobject_class->finalize = grl_config_finalize;
+
+  g_type_class_add_private (klass, sizeof (GrlConfigPrivate));
 }
 
 static void
 grl_config_init (GrlConfig *self)
 {
+  self->priv = GRL_CONFIG_GET_PRIVATE (self);
+  self->priv->config = g_hash_table_new_full (g_str_hash,
+					      g_str_equal,
+					      g_free,
+					      (GDestroyNotify) free_val);
 }
 
 static void
@@ -86,11 +108,94 @@ grl_config_new (const gchar *plugin, const gchar *source)
   g_return_val_if_fail (plugin != NULL, NULL);
   GrlConfig *config = g_object_new (GRL_TYPE_CONFIG, NULL);
   if (plugin) {
-    grl_config_set_plugin (config, plugin);
+    grl_config_set_string (config, GRL_CONFIG_KEY_PLUGIN, plugin);
   }
   if (source) {
-    grl_config_set_source (config, plugin);
+    grl_config_set_source (config, source);
   }
-
   return config;
 }
+
+void
+grl_config_set (GrlConfig *config, const gchar *param, const GValue *value)
+{
+  GValue *copy;
+  g_return_if_fail (GRL_IS_CONFIG (config));
+  copy = g_new0 (GValue, 1);
+  g_value_init (copy, G_VALUE_TYPE (value));
+  g_value_copy (value, copy);
+  g_hash_table_insert (config->priv->config, g_strdup (param), copy);
+}
+
+void
+grl_config_set_string (GrlConfig *config, const gchar *param, const gchar *value)
+{
+  GValue v = { 0 };
+  g_value_init (&v, G_TYPE_STRING);
+  g_value_set_string (&v, value);
+  grl_config_set (config, param, &v);
+  g_value_unset (&v);
+}
+
+void
+grl_config_set_int (GrlConfig *config, const gchar *param, gint value)
+{
+  GValue v = { 0 };
+  g_value_init (&v, G_TYPE_INT);
+  g_value_set_int (&v, value);
+  grl_config_set (config, param, &v);
+}
+
+
+void
+grl_config_set_float (GrlConfig *config, const gchar *param, gfloat value)
+{
+  GValue v = { 0 };
+  g_value_init (&v, G_TYPE_FLOAT);
+  g_value_set_float (&v, value);
+  grl_config_set (config, param, &v);
+}
+
+
+const GValue *
+grl_config_get (GrlConfig *config, const gchar *param)
+{
+  g_return_val_if_fail (GRL_IS_CONFIG (config), NULL);
+  return g_hash_table_lookup (config->priv->config, param);
+}
+
+const gchar *
+grl_config_get_string (GrlConfig *config, const gchar *param)
+{
+  g_return_val_if_fail (GRL_IS_CONFIG (config), NULL);
+  const GValue *value = grl_config_get (config, param);
+  if (!value || !G_VALUE_HOLDS_STRING (value)) {
+    return NULL;
+  } else {
+    return g_value_get_string (value);
+  }
+}
+
+gint
+grl_config_get_int (GrlConfig *config, const gchar *param)
+{
+  g_return_val_if_fail (GRL_IS_CONFIG (config), 0);
+  const GValue *value = grl_config_get (config, param);
+  if (!value || !G_VALUE_HOLDS_INT (value)) {
+    return 0;
+  } else {
+    return g_value_get_int (value);
+  }
+}
+
+gfloat
+grl_config_get_float (GrlConfig *config, const gchar *param)
+{
+  g_return_val_if_fail (GRL_IS_CONFIG (config), 0.0);
+  const GValue *value = grl_config_get (config, param);
+  if (!value || !G_VALUE_HOLDS_FLOAT (value)) {
+    return 0.0;
+  } else {
+    return g_value_get_float (value);
+  }
+}
diff --git a/src/data/grl-config.h b/src/data/grl-config.h
index ea77fd6..938e3ff 100644
--- a/src/data/grl-config.h
+++ b/src/data/grl-config.h
@@ -26,12 +26,12 @@
 #error "Only <grilo.h> can be included directly."
 #endif
 
+#include <glib.h>
+#include <glib-object.h>
+
 #ifndef _GRL_CONFIG_H_
 #define _GRL_CONFIG_H_
 
-#include <grl-data.h>
-
-
 G_BEGIN_DECLS
 
 #define GRL_TYPE_CONFIG                         \
@@ -60,29 +60,15 @@ G_BEGIN_DECLS
                               GRL_TYPE_CONFIG,  \
                               GrlConfigClass))
 
+#define GRL_CONFIG_KEY_PLUGIN      "target-plugin"
+#define GRL_CONFIG_KEY_SOURCE      "target-source"
+#define GRL_CONFIG_KEY_APIKEY      "api-key"
+#define GRL_CONFIG_KEY_APITOKEN    "api-token"
+#define GRL_CONFIG_KEY_APISECRET   "api-secret"
 
-#define GRL_CONFIG_KEY_PLUGIN               1
-#define GRL_CONFIG_KEY_PLUGIN_NAME          "plugin"
-#define GRL_CONFIG_KEY_PLUGIN_DESC          "Plugin ID to which the configuration applies"
-
-#define GRL_CONFIG_KEY_SOURCE               2
-#define GRL_CONFIG_KEY_SOURCE_NAME          "source"
-#define GRL_CONFIG_KEY_SOURCE_DESC          "Source ID to which the configuration applies"
-
-#define GRL_CONFIG_KEY_APIKEY               3
-#define GRL_CONFIG_KEY_APIKEY_NAME          "api-key"
-#define GRL_CONFIG_KEY_APIKEY_DESC          "API Key"
-
-#define GRL_CONFIG_KEY_APITOKEN             4
-#define GRL_CONFIG_KEY_APITOKEN_NAME        "api-token"
-#define GRL_CONFIG_KEY_APITOKEN_DESC        "API token"
-
-#define GRL_CONFIG_KEY_APISECRET            5
-#define GRL_CONFIG_KEY_APISECRET_NAME       "api-secret"
-#define GRL_CONFIG_KEY_APISECRET_DESC       "API secret"
-
-typedef struct _GrlConfig      GrlConfig;
-typedef struct _GrlConfigClass GrlConfigClass;
+typedef struct _GrlConfig        GrlConfig;
+typedef struct _GrlConfigPrivate GrlConfigPrivate;
+typedef struct _GrlConfigClass   GrlConfigClass;
 
 /**
  * GrlConfigClass:
@@ -92,113 +78,135 @@ typedef struct _GrlConfigClass GrlConfigClass;
  */
 struct _GrlConfigClass
 {
-  GrlDataClass parent_class;
+  GObjectClass parent_class;
 };
 
 struct _GrlConfig
 {
-  GrlData parent;
+  GObject parent;
+
+  /*< private >*/
+  GrlConfigPrivate *priv;
 };
 
 /**
  * grl_config_set_plugin:
- * @data: the config instance
+ * @config: the config instance
  * @plugin: the plugin id
  *
  * Set the plugin key in the configuration
  */
-#define grl_config_set_plugin(data, plugin)     \
-  grl_data_set_string(GRL_DATA((data)),         \
-                      GRL_CONFIG_KEY_PLUGIN,    \
-                      (plugin))                 \
+#define grl_config_set_plugin(config, plugin)     \
+  grl_config_set_string(GRL_CONFIG((config)),         \
+			GRL_CONFIG_KEY_PLUGIN,    \
+			(plugin))                 \
 
 /**
  * grl_config_set_source:
- * @data: the config instance
+ * @config: the config instance
  * @source: the source id
  *
  * Set the plugin key in the configuration
  */
-#define grl_config_set_source(data, source)     \
-  grl_data_set_string(GRL_DATA((data)),         \
-                      GRL_CONFIG_KEY_SOURCE,    \
-                      (source))                 \
+#define grl_config_set_source(config, source)     \
+  grl_config_set_string(GRL_CONFIG((config)),	\
+			GRL_CONFIG_KEY_SOURCE,	\
+			(source))		\
 
 /**
  * grl_config_set_api_key:
- * @data: the config instance
+ * @config: the config instance
  * @key: the API key
  *
  * Set the webservice API key in the configuration
  */
-#define grl_config_set_api_key(data, key)       \
-  grl_data_set_string(GRL_DATA((data)),         \
-                      GRL_CONFIG_KEY_APIKEY,    \
-                      (key))
+#define grl_config_set_api_key(config, key)       \
+  grl_config_set_string(GRL_CONFIG((config)),	\
+			GRL_CONFIG_KEY_APIKEY,	\
+			(key))
 
 /**
  * grl_config_set_api_token:
- * @data: the config instance
+ * @config: the config instance
  * @token: the API token
  *
  * Set the webservice API token in the configuration
  */
-#define grl_config_set_api_token(data, token)   \
-  grl_data_set_string(GRL_DATA((data)),         \
-                      GRL_CONFIG_KEY_APITOKEN,  \
-                      (token))
+#define grl_config_set_api_token(config, token)   \
+  grl_config_set_string(GRL_CONFIG((config)),         \
+			GRL_CONFIG_KEY_APITOKEN,  \
+			(token))
 
 /**
  * grl_config_set_api_secret:
- * @data: the config instance
+ * @config: the config instance
  * @secret: the webservice passphrase
  *
  * Set the webservice passphrase in the configuration
  */
-#define grl_config_set_api_secret(data, secret) \
-  grl_data_set_string(GRL_DATA((data)),         \
-                      GRL_CONFIG_KEY_APISECRET, \
-                      (secret))
+#define grl_config_set_api_secret(config, secret) \
+  grl_config_set_string(GRL_CONFIG((config)),         \
+			GRL_CONFIG_KEY_APISECRET, \
+			(secret))
 
 /**
  * grl_config_get_plugin:
- * @data: the config instance
+ * @config: the config instance
  *
  * Returns: (type utf8) (transfer none): the plugin id
  */
-#define grl_config_get_plugin(data)                               \
-  grl_data_get_string(GRL_DATA((data)), GRL_CONFIG_KEY_PLUGIN)
+#define grl_config_get_plugin(config)                               \
+  grl_config_get_string(GRL_CONFIG((config)), GRL_CONFIG_KEY_PLUGIN)
 
 /**
  * grl_config_get_api_key:
- * @data: the config instance
+ * @config: the config instance
  *
  * Returns: (type utf8) (transfer none): the webservice API key
  */
-#define grl_config_get_api_key(data)                              \
-  grl_data_get_string(GRL_DATA((data)), GRL_CONFIG_KEY_APIKEY)
+#define grl_config_get_api_key(config)                              \
+  grl_config_get_string(GRL_CONFIG((config)), GRL_CONFIG_KEY_APIKEY)
 
 /**
  * grl_config_get_api_token:
- * @data: the config instance
+ * @config: the config instance
  *
  * Returns: (type utf8) (transfer none): the webservice API token
  */
-#define grl_config_get_api_token(data)                            \
-  grl_data_get_string(GRL_DATA((data)), GRL_CONFIG_KEY_APITOKEN)
+#define grl_config_get_api_token(config)					\
+  grl_config_get_string(GRL_CONFIG((config)), GRL_CONFIG_KEY_APITOKEN)
 
 /**
  * grl_config_get_api_secret:
- * @data: the config instance
+ * @config: the config instance
  *
  * Returns: (type utf8) (transfer none): the webservice API passphrase
  */
-#define grl_config_get_api_secret(data)                           \
-grl_data_get_string(GRL_DATA((data)), GRL_CONFIG_KEY_APISECRET)
+#define grl_config_get_api_secret(config)                           \
+grl_config_get_string(GRL_CONFIG((config)), GRL_CONFIG_KEY_APISECRET)
 
 GType grl_config_get_type (void) G_GNUC_CONST;
 GrlConfig *grl_config_new (const gchar *plugin, const gchar *source);
 
+void grl_config_set (GrlConfig *config, const gchar *param, const GValue *value);
+
+void grl_config_set_string (GrlConfig *config,
+			    const gchar *param,
+			    const gchar *value);
+
+void grl_config_set_int (GrlConfig *config, const gchar *param, gint value);
+
+void grl_config_set_float (GrlConfig *config, const gchar *param, gfloat value);
+
+const GValue *grl_config_get (GrlConfig *config, const gchar *param);
+
+const gchar *grl_config_get_string (GrlConfig *config, const gchar *param);
+
+gint grl_config_get_int (GrlConfig *config, const gchar *param);
+
+gfloat grl_config_get_float (GrlConfig *config, const gchar *param);
+
+
 G_END_DECLS
 
 #endif /* _GRL_CONFIG_H_ */



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