[grilo] core: Add function to load plugin configuration from file



commit 396a0153c8b53738a88ab778bca35655195b13d5
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Wed Dec 1 13:11:44 2010 +0100

    core: Add function to load plugin configuration from file
    
    grl_plugin_registry_add_config_from_file() loads a .ini-like file containing
    configurations for one or more plugins.
    
    See GKeyFile to know more about this syntax.
    
    An example is:
    
    [plugin1]
    key1 = value1
    key2 = value2
    
    [plugin2]
    key1 = value1
    key3 = value3
    
    It fixes GB#636064.

 src/grl-error.h           |    2 +
 src/grl-plugin-registry.c |   68 +++++++++++++++++++++++++++++++++++++++++++++
 src/grl-plugin-registry.h |    4 ++
 3 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/src/grl-error.h b/src/grl-error.h
index 1f677c6..04cfc51 100644
--- a/src/grl-error.h
+++ b/src/grl-error.h
@@ -48,6 +48,7 @@
  * @GRL_CORE_ERROR_REMOVE_FAILED: The removal operation failed
  * @GRL_CORE_ERROR_SET_METADATA_FAILED: The set metadata operation failed
  * @GRL_CORE_ERROR_MEDIA_FROM_URI_FAILED: The media from_uri operation failed
+ * @GRL_CORE_ERROR_CONFIG_LOAD_FAILED: Failed to load plugin configuration from a file
  *
  * These constants identify all the available core errors
  */
@@ -62,6 +63,7 @@ typedef enum {
   GRL_CORE_ERROR_REMOVE_FAILED,
   GRL_CORE_ERROR_SET_METADATA_FAILED,
   GRL_CORE_ERROR_MEDIA_FROM_URI_FAILED,
+  GRL_CORE_ERROR_CONFIG_LOAD_FAILED,
 } GrlCoreError;
 
 #endif /* _GRL_ERROR_H_ */
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index 4be7b94..84337a6 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -40,6 +40,7 @@
 #include "grl-plugin-registry.h"
 #include "grl-media-plugin-priv.h"
 #include "grl-log.h"
+#include "grl-error.h"
 
 #include <string.h>
 #include <gmodule.h>
@@ -789,3 +790,70 @@ grl_plugin_registry_add_config (GrlPluginRegistry *registry,
 			 configs);
   }
 }
+
+/**
+ * grl_plugin_registry_add_config_from_file:
+ * @registry: the registry instance
+ * @config_file: a key-value file containing the configuration
+ * @error: error return location or @NULL to ignore
+ *
+ * Load plugin configurations from a .ini-like config file.
+ *
+ * Returns: %TRUE on success
+ **/
+gboolean
+grl_plugin_registry_add_config_from_file (GrlPluginRegistry *registry,
+                                          const gchar *config_file,
+                                          GError **error)
+{
+  GError *load_error = NULL;
+  GKeyFile *keyfile;
+  GrlConfig *config;
+  gchar **key;
+  gchar **keys;
+  gchar **plugin;
+  gchar **plugins;
+  gchar *value;
+
+  g_return_val_if_fail (GRL_IS_PLUGIN_REGISTRY (registry), FALSE);
+  g_return_val_if_fail (config_file, FALSE);
+
+  keyfile = g_key_file_new ();
+
+  if (g_key_file_load_from_file (keyfile,
+                                 config_file,
+                                 G_KEY_FILE_NONE,
+                                 &load_error)) {
+
+    /* Look up for defined plugins */
+    plugins = g_key_file_get_groups (keyfile, NULL);
+    for (plugin = plugins; *plugin; plugin++) {
+      config = grl_config_new (*plugin, NULL);
+
+      /* Look up configuration keys for this plugin */
+      keys = g_key_file_get_keys (keyfile, *plugin, NULL, NULL);
+      for (key = keys; *key; key++) {
+        value = g_key_file_get_string (keyfile, *plugin, *key, NULL);
+        if (value) {
+          grl_config_set_string (config, *key, value);
+          g_free (value);
+        }
+      }
+      grl_plugin_registry_add_config (registry, config);
+      g_strfreev (keys);
+    }
+    g_strfreev (plugins);
+    g_key_file_free (keyfile);
+    return TRUE;
+  } else {
+    GRL_WARNING ("Unable to load configuration. %s", load_error->message);
+    if (error) {
+      *error = g_error_new_literal (GRL_CORE_ERROR,
+                                    GRL_CORE_ERROR_CONFIG_LOAD_FAILED,
+                                    load_error->message);
+    }
+    g_error_free (load_error);
+    g_key_file_free (keyfile);
+    return FALSE;
+  }
+}
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index 68674d4..5d53fad 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -232,6 +232,10 @@ GList *grl_plugin_registry_get_metadata_keys (GrlPluginRegistry *registry);
 void grl_plugin_registry_add_config (GrlPluginRegistry *registry,
                                      GrlConfig *config);
 
+gboolean grl_plugin_registry_add_config_from_file (GrlPluginRegistry *registry,
+                                                   const gchar *config_file,
+                                                   GError **error);
+
 G_END_DECLS
 
 #endif /* _GRL_PLUGIN_REGISTRY_H_ */



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