[PATCH 1/3] registry: add grl_plugin_registry_load_default() method



From: Lionel Landwerlin <lionel g landwerlin linux intel com>

The purpose of this new method is to provide an helper to load a given
set of plugins using the command line or an environment variable. It
is useful for debugging.

Signed-off-by: Lionel Landwerlin <lionel g landwerlin linux intel com>
---
 src/grilo.c               |   27 +++++++++++++----
 src/grl-plugin-registry.c |   70 ++++++++++++++++++++++++++++++++++----------
 src/grl-plugin-registry.h |    4 ++
 3 files changed, 79 insertions(+), 22 deletions(-)

diff --git a/src/grilo.c b/src/grilo.c
index 8b3f80b..d74c116 100644
--- a/src/grilo.c
+++ b/src/grilo.c
@@ -43,6 +43,7 @@
 
 static gboolean grl_initialized = FALSE;
 static const gchar *plugin_path = NULL;
+static const gchar *default_plugin_list = NULL;
 
 /**
  * grl_init:
@@ -60,8 +61,8 @@ grl_init (gint *argc,
   GOptionContext *ctx;
   GOptionGroup *group;
   GrlPluginRegistry *registry;
-  gchar **plugin_dir;
-  gchar **plugin_dirs_split;
+  gchar **split_element;
+  gchar **split_list;
 
   if (grl_initialized) {
     GRL_DEBUG ("already initialized grl");
@@ -108,11 +109,23 @@ grl_init (gint *argc,
     plugin_path = GRL_PLUGIN_PATH_DEFAULT;
   }
 
-  plugin_dirs_split = g_strsplit (plugin_path, ":", 0);
-  for (plugin_dir = plugin_dirs_split; *plugin_dir; plugin_dir++) {
-    grl_plugin_registry_add_directory (registry, *plugin_dir);
+  split_list = g_strsplit (plugin_path, ":", 0);
+  for (split_element = split_list; *split_element; split_element++) {
+    grl_plugin_registry_add_directory (registry, *split_element);
+  }
+  g_strfreev (split_list);
+
+  if (!default_plugin_list) {
+    default_plugin_list = g_getenv (GRL_DEFAULT_PLUGINS_VAR);
+  }
+
+  if (default_plugin_list) {
+    split_list = g_strsplit (default_plugin_list, ",", 0);
+    for (split_element = split_list; *split_element; split_element++) {
+      grl_plugin_registry_add_default_plugin (registry, *split_element);
+    }
+    g_strfreev (split_list);
   }
-  g_strfreev (plugin_dirs_split);
 
   grl_initialized = TRUE;
 }
@@ -138,6 +151,8 @@ grl_init_get_option_group (void)
   static const GOptionEntry grl_args[] = {
     { "grl-plugin-path", 0, 0, G_OPTION_ARG_STRING, &plugin_path,
       "Colon-separated paths containing plugins", NULL },
+    { "grl-default-plugins", 0, 0, G_OPTION_ARG_STRING, &default_plugin_list,
+      "Colon-separated ids of plugins to load in default case", NULL },
     { NULL }
   };
 
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index f713cd4..c35215b 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -70,6 +70,7 @@ struct _GrlPluginRegistryPrivate {
   GParamSpecPool *system_keys;
   GHashTable *ranks;
   GSList *plugins_dir;
+  GSList *default_plugins;
 };
 
 static void grl_plugin_registry_setup_ranks (GrlPluginRegistry *registry);
@@ -450,6 +451,28 @@ grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
 }
 
 /**
+ * grl_plugin_registry_add_default_plugin:
+ * @registry: the registry instance
+ * @plugin_id: a plugin id
+ *
+ * Set this plugin id as part of default set of plugins to load.
+ *
+ * Since: 0.1.16
+ **/
+void
+grl_plugin_registry_add_default_plugin (GrlPluginRegistry *registry,
+                                        const gchar *plugin_id)
+{
+  g_return_if_fail (GRL_IS_PLUGIN_REGISTRY (registry));
+  g_return_if_fail (plugin_id);
+
+  /* Use append instead of prepend so plugins are loaded in the same order as
+     they were added */
+  registry->priv->default_plugins = g_slist_append (registry->priv->default_plugins,
+                                                    g_strdup (plugin_id));
+}
+
+/**
  * grl_plugin_registry_load:
  * @registry: the registry instance
  * @path: the path to the so file
@@ -638,39 +661,54 @@ grl_plugin_registry_load_directory (GrlPluginRegistry *registry,
  * @registry: the registry instance
  * @error: error return location or @NULL to ignore
  *
- * Load all the modules available in the default directory path.
+ * Load all the modules available in the default directory path or a
+ * list of default plugins given either using
+ * --grl-default-plugins=plugin1,plugin2,... or through the
+ * environment variable %GRL_DEFAULT_PLUGINS.
  *
  * The default directory path can be changed through the environment
  * variable %GRL_PLUGIN_PATH and it can contain several paths separated
  * by ":"
  *
- * Returns: %FALSE% is all the configured plugin paths are invalid,
- * %TRUE% otherwise.
+ * Returns: %FALSE% is all the configured plugin paths are invalid or
+ * couldn't load one of the default plugin, %TRUE% otherwise.
  *
  * Since: 0.1.1
  */
 gboolean
 grl_plugin_registry_load_all (GrlPluginRegistry *registry, GError **error)
 {
-  GSList *plugin_dir;
+  GSList *element;
   gboolean loaded_one = FALSE;
 
   g_return_val_if_fail (GRL_IS_PLUGIN_REGISTRY (registry), TRUE);
 
-  for (plugin_dir = registry->priv->plugins_dir;
-       plugin_dir;
-       plugin_dir = g_slist_next (plugin_dir)) {
-    if (grl_plugin_registry_load_directory (registry, plugin_dir->data, NULL)) {
-      loaded_one = TRUE;
+  if (registry->priv->default_plugins) {
+    for (element = registry->priv->default_plugins;
+         element;
+         element = g_slist_next (element)) {
+      if (!grl_plugin_registry_load_by_id (registry,
+                                           (const gchar *) element->data,
+                                           error))
+        return FALSE;
+    }
+    loaded_one = TRUE;
+  } else {
+    for (element = registry->priv->plugins_dir;
+         element;
+         element = g_slist_next (element)) {
+      if (grl_plugin_registry_load_directory (registry, element->data, NULL)) {
+        loaded_one = TRUE;
+      }
     }
-  }
 
-  if (!loaded_one) {
-    g_set_error (error,
-                 GRL_CORE_ERROR,
-                 GRL_CORE_ERROR_LOAD_PLUGIN_FAILED,
-                 "All configured plugin paths are invalid. "    \
-                 "Failed to load plugins.");
+    if (!loaded_one) {
+      g_set_error (error,
+                   GRL_CORE_ERROR,
+                   GRL_CORE_ERROR_LOAD_PLUGIN_FAILED,
+                   "All configured plugin paths are invalid. "  \
+                   "Failed to load plugins.");
+    }
   }
 
   return loaded_one;
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index acbbc8b..db0ea20 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -38,6 +38,7 @@
 
 #define GRL_PLUGIN_PATH_VAR "GRL_PLUGIN_PATH"
 #define GRL_PLUGIN_RANKS_VAR "GRL_PLUGIN_RANKS"
+#define GRL_DEFAULT_PLUGINS_VAR "GRL_DEFAULT_PLUGINS"
 
 /* Macros */
 
@@ -205,6 +206,9 @@ GrlPluginRegistry *grl_plugin_registry_get_default (void);
 void grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
                                         const gchar *path);
 
+void grl_plugin_registry_add_default_plugin (GrlPluginRegistry *registry,
+                                             const gchar *plugin_id);
+
 gboolean grl_plugin_registry_load (GrlPluginRegistry *registry,
                                    const gchar *path,
                                    GError **error);
-- 
1.7.5.4



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