[PATCH 2/4] Set default plugin directories in grl_init()



Added a function to registry, grl_plugin_registry_add_directory(), to add a
directory to default list of directoies where grl_plugin_registry_load_all()
will look when loading plugins.

In this patch, grl_plugin_load_all() does not have any "default" plugins
directory. Instead, grl_init() will use grl_plugin_registry_add_directory() to
specify the list of directories.

These are obtained first from GRL_PLUGIN_PATH envvar if defined, and if not
then using the default plugin directory.

Developers can add more directories just invoking
grl_plugin_registry_load_all() after initialized Grilo.
---
 src/grilo.c               |   18 ++++++++++++++++++
 src/grl-plugin-registry.c |   45 +++++++++++++++++++++++++++------------------
 src/grl-plugin-registry.h |    3 +++
 3 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/src/grilo.c b/src/grilo.c
index 9881f5a..83ac5ac 100644
--- a/src/grilo.c
+++ b/src/grilo.c
@@ -22,6 +22,9 @@
 
 #include "grilo.h"
 #include "grl-metadata-key-priv.h"
+#include "config.h"
+
+#define GRL_PLUGIN_PATH_DEFAULT GRL_PLUGINS_DIR
 
 static gboolean grl_initialized = FALSE;
 
@@ -30,6 +33,9 @@ grl_init (gint *argc,
           gchar **argv[])
 {
   GrlPluginRegistry *registry;
+  const gchar *plugin_dirs;
+  gchar **plugin_dir;
+  gchar **plugin_dirs_split;
 
   if (grl_initialized) {
     g_debug ("already initialized grl");
@@ -46,5 +52,17 @@ grl_init (gint *argc,
   g_type_class_ref (GRL_TYPE_MEDIA_VIDEO);
   g_type_class_ref (GRL_TYPE_MEDIA_IMAGE);
 
+  /* Set default plugin directories */
+  plugin_dirs = g_getenv (GRL_PLUGIN_PATH_VAR);
+  if (!plugin_dirs) {
+    plugin_dirs = GRL_PLUGIN_PATH_DEFAULT;
+  }
+
+  plugin_dirs_split = g_strsplit (plugin_dirs, ":", 0);
+  for (plugin_dir = plugin_dirs_split; *plugin_dir; plugin_dir++) {
+    grl_plugin_registry_add_directory (registry, *plugin_dir);
+  }
+  g_strfreev (plugin_dirs_split);
+
   grl_initialized = TRUE;
 }
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index 3e1a60b..5e91fd8 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -39,13 +39,10 @@
 
 #include "grl-plugin-registry.h"
 #include "grl-media-plugin-priv.h"
-#include "config.h"
 
 #include <string.h>
 #include <gmodule.h>
 
-#define GRL_PLUGIN_PATH_DEFAULT GRL_PLUGINS_DIR
-
 #undef G_LOG_DOMAIN
 #define G_LOG_DOMAIN "grl-plugin-registry"
 
@@ -60,6 +57,7 @@ struct _GrlPluginRegistryPrivate {
   GHashTable *sources;
   GParamSpecPool *system_keys;
   GHashTable *ranks;
+  GSList *plugins_dir;
 };
 
 static void grl_plugin_registry_setup_ranks (GrlPluginRegistry *registry);
@@ -316,6 +314,26 @@ grl_plugin_registry_unregister_source (GrlPluginRegistry *registry,
 }
 
 /**
+ * grl_plugin_registry_add_directory:
+ * @registry: the registry intance
+ * @path: a path with plugins
+ *
+ * Set this path as part of default paths to load plugins.
+ **/
+void
+grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
+                                   const gchar *path)
+{
+  g_return_if_fail (GRL_IS_PLUGIN_REGISTRY (registry));
+  g_return_if_fail (path);
+
+  /* Use append instead of prepend so plugins are loaded in the same order as
+     they were added */
+  registry->priv->plugins_dir = g_slist_append (registry->priv->plugins_dir,
+                                                g_strdup (path));
+}
+
+/**
  * grl_plugin_registry_load:
  * @registry: the registry instance
  * @path: the path to the so file
@@ -420,25 +438,16 @@ grl_plugin_registry_load_directory (GrlPluginRegistry *registry,
 gboolean
 grl_plugin_registry_load_all (GrlPluginRegistry *registry)
 {
-  const gchar *plugin_dirs_env;
-  gchar **plugin_dirs;
-  gchar **dirs_iter;
+  GSList *plugin_dir;
 
-  plugin_dirs_env = g_getenv (GRL_PLUGIN_PATH_VAR);
-  if (!plugin_dirs_env) {
-    plugin_dirs_env = GRL_PLUGIN_PATH_DEFAULT;
-  }
-
-  plugin_dirs = g_strsplit (plugin_dirs_env, ":", 0);
-  dirs_iter = plugin_dirs;
+  g_return_val_if_fail (GRL_IS_PLUGIN_REGISTRY (registry), TRUE);
 
-  while (*dirs_iter) {
-    grl_plugin_registry_load_directory (registry, *dirs_iter);
-    dirs_iter++;
+  for (plugin_dir = registry->priv->plugins_dir;
+       plugin_dir;
+       plugin_dir = g_slist_next (plugin_dir)) {
+    grl_plugin_registry_load_directory (registry, plugin_dir->data);
   }
 
-  g_strfreev (plugin_dirs);
-
   return TRUE;
 }
 
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index d0b6dbb..3a01149 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -212,6 +212,9 @@ GType grl_plugin_registry_get_type (void);
 
 GrlPluginRegistry *grl_plugin_registry_get_instance (void);
 
+void grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
+                                        const gchar *path);
+
 gboolean grl_plugin_registry_load (GrlPluginRegistry *registry,
                                    const gchar *path);
 
-- 
1.7.0.4



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