[gedit/eggplugins] [libplugins] Compute the plugin's data dir in the engine.



commit 864572b5a3f1f34c3dd1b1699d5a678d06355329
Author: Steve Frécinaux <code istique net>
Date:   Fri Oct 9 20:32:32 2009 +0200

    [libplugins] Compute the plugin's data dir in the engine.
    
    This makes it easier to replace the directory logic afterwards. It will
    need to be replaced as it is now dependant on gedit functions anyway...

 libplugins/egg-plugins-engine.c                   |   45 +++++++++++++++-
 libplugins/egg-plugins-loader.c                   |    5 +-
 libplugins/egg-plugins-loader.h                   |   10 ++--
 libplugins/egg-plugins-plugin.c                   |   61 ++-------------------
 plugin-loaders/c/egg-plugins-loader-c.c           |   10 ++--
 plugin-loaders/python/egg-plugins-loader-python.c |   17 ++++---
 6 files changed, 73 insertions(+), 75 deletions(-)
---
diff --git a/libplugins/egg-plugins-engine.c b/libplugins/egg-plugins-engine.c
index 3da12c9..390f93a 100644
--- a/libplugins/egg-plugins-engine.c
+++ b/libplugins/egg-plugins-engine.c
@@ -521,12 +521,52 @@ save_active_plugin_list (EggPluginsEngine *engine)
 	g_slist_free (active_plugins);
 }
 
+static gchar *
+find_plugin_data_dir (const gchar *install_dir,
+                      EggPluginsInfo *info)
+{
+	gchar *gedit_lib_dir;
+	gchar *data_dir;
+
+	/* If it's a "user" plugin the data dir is $install_dir/$module_name.
+	 * if instead it's a "system" plugin the data dir is under
+	 * $gedit_data_dir, so the data is located it's under
+	 * $prefix/share/gedit-2/plugins/$module_name.
+	 */
+	gedit_lib_dir = gedit_dirs_get_gedit_lib_dir ();
+
+	/* CHECK: is checking the prefix enough or should we be more
+	 * careful about normalizing paths etc? */
+	if (g_str_has_prefix (install_dir, gedit_lib_dir))
+	{
+		gchar *gedit_data_dir = gedit_dirs_get_gedit_data_dir ();
+
+		data_dir = g_build_filename (gedit_data_dir,
+					     "plugins",
+					     info->module_name,
+					     NULL);
+
+		g_free (gedit_data_dir);
+	}
+	else
+	{
+		data_dir = g_build_filename (install_dir,
+					     info->module_name,
+					     NULL);
+	}
+
+	g_free (gedit_lib_dir);
+
+	return data_dir;
+}
+
 static gboolean
 load_plugin (EggPluginsEngine *engine,
 	     EggPluginsInfo   *info)
 {
 	EggPluginsLoader *loader;
 	gchar *path;
+	gchar *data_dir;
 
 	if (egg_plugins_info_is_active (info))
 		return TRUE;
@@ -546,8 +586,11 @@ load_plugin (EggPluginsEngine *engine,
 	path = g_path_get_dirname (info->file);
 	g_return_val_if_fail (path != NULL, FALSE);
 
-	info->plugin = egg_plugins_loader_load (loader, info, path);
+	data_dir = find_plugin_data_dir (path, info);
+
+	info->plugin = egg_plugins_loader_load (loader, info, path, data_dir);
 
+	g_free (data_dir);
 	g_free (path);
 
 	if (info->plugin == NULL)
diff --git a/libplugins/egg-plugins-loader.c b/libplugins/egg-plugins-loader.c
index d853571..1fd7a93 100644
--- a/libplugins/egg-plugins-loader.c
+++ b/libplugins/egg-plugins-loader.c
@@ -91,7 +91,8 @@ egg_plugins_loader_type_get_id (GType type)
 EggPluginsPlugin *
 egg_plugins_loader_load (EggPluginsLoader *loader,
 			 EggPluginsInfo   *info,
-			 const gchar      *path)
+			 const gchar      *path,
+			 const gchar      *datadir)
 {
 	EggPluginsLoaderInterface *iface;
 
@@ -100,7 +101,7 @@ egg_plugins_loader_load (EggPluginsLoader *loader,
 	iface = EGG_PLUGINS_LOADER_GET_INTERFACE (loader);
 	g_return_val_if_fail (iface->load != NULL, NULL);
 
-	return iface->load (loader, info, path);
+	return iface->load (loader, info, path, datadir);
 }
 
 void
diff --git a/libplugins/egg-plugins-loader.h b/libplugins/egg-plugins-loader.h
index cad429f..724fe73 100644
--- a/libplugins/egg-plugins-loader.h
+++ b/libplugins/egg-plugins-loader.h
@@ -42,9 +42,10 @@ struct _EggPluginsLoaderInterface {
 
 	const gchar *(*get_id)		(void);
 
-	EggPluginsPlugin *(*load) 	(EggPluginsLoader 	*loader,
-			     		 EggPluginsInfo	*info,
-			      		 const gchar       	*path);
+	EggPluginsPlugin *(*load)	(EggPluginsLoader 	*loader,
+					 EggPluginsInfo	        *info,
+					 const gchar       	*path,
+					 const gchar		*datadir);
 
 	void 	     (*unload)		(EggPluginsLoader 	*loader,
 					 EggPluginsInfo       	*info);
@@ -57,7 +58,8 @@ GType egg_plugins_loader_get_type (void);
 const gchar *egg_plugins_loader_type_get_id	(GType 			 type);
 EggPluginsPlugin *egg_plugins_loader_load	(EggPluginsLoader 	*loader,
 						 EggPluginsInfo 	*info,
-						 const gchar		*path);
+						 const gchar		*path,
+						 const gchar		*datadir);
 void egg_plugins_loader_unload			(EggPluginsLoader 	*loader,
 						 EggPluginsInfo		*info);
 void egg_plugins_loader_garbage_collect		(EggPluginsLoader 	*loader);
diff --git a/libplugins/egg-plugins-plugin.c b/libplugins/egg-plugins-plugin.c
index 605333e..a8979e3 100644
--- a/libplugins/egg-plugins-plugin.c
+++ b/libplugins/egg-plugins-plugin.c
@@ -39,7 +39,6 @@
 enum {
 	PROP_0,
 	PROP_INSTALL_DIR,
-	PROP_DATA_DIR_NAME,
 	PROP_DATA_DIR
 };
 
@@ -48,7 +47,7 @@ typedef struct _EggPluginsPluginPrivate EggPluginsPluginPrivate;
 struct _EggPluginsPluginPrivate
 {
 	gchar *install_dir;
-	gchar *data_dir_name;
+	gchar *data_dir;
 };
 
 #define EGG_PLUGINS_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_PLUGINS_TYPE_PLUGIN, EggPluginsPluginPrivate))
@@ -106,8 +105,8 @@ egg_plugins_plugin_set_property (GObject      *object,
 		case PROP_INSTALL_DIR:
 			priv->install_dir = g_value_dup_string (value);
 			break;
-		case PROP_DATA_DIR_NAME:
-			priv->data_dir_name = g_value_dup_string (value);
+		case PROP_DATA_DIR:
+			priv->data_dir = g_value_dup_string (value);
 			break;
 		default:
 			g_return_if_reached ();
@@ -120,7 +119,7 @@ egg_plugins_plugin_finalize (GObject *object)
 	EggPluginsPluginPrivate *priv = EGG_PLUGINS_PLUGIN_GET_PRIVATE (object);
 
 	g_free (priv->install_dir);
-	g_free (priv->data_dir_name);
+	g_free (priv->data_dir);
 
 	G_OBJECT_CLASS (egg_plugins_plugin_parent_class)->finalize (object);
 }
@@ -149,23 +148,13 @@ egg_plugins_plugin_class_init (EggPluginsPluginClass *klass)
 							      NULL,
 							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
-	/* the basename of the data dir is set at construction time by the plugin loader
-	 * while the full path is constructed on the fly to take into account relocability
-	 * that's why we have a writeonly prop and a readonly prop */
-	g_object_class_install_property (object_class,
-					 PROP_DATA_DIR_NAME,
-					 g_param_spec_string ("data-dir-name",
-							      "Basename of the data directory",
-							      "The basename of the directory where the plugin should look for its data files",
-							      NULL,
-							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 	g_object_class_install_property (object_class,
 					 PROP_DATA_DIR,
 					 g_param_spec_string ("data-dir",
 							      "Data Directory",
 							      "The full path of the directory where the plugin should look for its data files",
 							      NULL,
-							      G_PARAM_READABLE));
+							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_type_class_add_private (klass, sizeof (EggPluginsPluginPrivate));
 }
@@ -206,47 +195,9 @@ egg_plugins_plugin_get_install_dir (EggPluginsPlugin *plugin)
 gchar *
 egg_plugins_plugin_get_data_dir (EggPluginsPlugin *plugin)
 {
-	EggPluginsPluginPrivate *priv;
-	gchar *gedit_lib_dir;
-	gchar *data_dir;
-
 	g_return_val_if_fail (EGG_PLUGINS_IS_PLUGIN (plugin), NULL);
 
-	priv = EGG_PLUGINS_PLUGIN_GET_PRIVATE (plugin);
-
-	/* If it's a "user" plugin the data dir is
-	 * install_dir/data_dir_name if instead it's a
-	 * "system" plugin the data dir is under gedit_data_dir,
-	 * so it's under $prefix/share/gedit-2/plugins/data_dir_name
-	 * where data_dir_name usually it's the name of the plugin
-	 */
-	gedit_lib_dir = gedit_dirs_get_gedit_lib_dir ();
-
-	/* CHECK: is checking the prefix enough or should we be more
-	 * careful about normalizing paths etc? */
-	if (g_str_has_prefix (priv->install_dir, gedit_lib_dir))
-	{
-		gchar *gedit_data_dir;
-
-		gedit_data_dir = gedit_dirs_get_gedit_data_dir ();
-
-		data_dir = g_build_filename (gedit_data_dir,
-					     "plugins",
-					     priv->data_dir_name,
-					     NULL);
-
-		g_free (gedit_data_dir);
-	}
-	else
-	{
-		data_dir = g_build_filename (priv->install_dir,
-					     priv->data_dir_name,
-					     NULL);
-	}
-
-	g_free (gedit_lib_dir);
-
-	return data_dir;
+	return g_strdup (EGG_PLUGINS_PLUGIN_GET_PRIVATE (plugin)->data_dir);
 }
 
 /**
diff --git a/plugin-loaders/c/egg-plugins-loader-c.c b/plugin-loaders/c/egg-plugins-loader-c.c
index eab15df..88f445c 100644
--- a/plugin-loaders/c/egg-plugins-loader-c.c
+++ b/plugin-loaders/c/egg-plugins-loader-c.c
@@ -43,8 +43,9 @@ egg_plugins_loader_iface_get_id (void)
 
 static EggPluginsPlugin *
 egg_plugins_loader_iface_load (EggPluginsLoader *loader,
-				EggPluginsInfo    *info,
-				const gchar       *path)
+				EggPluginsInfo  *info,
+				const gchar     *path,
+				const gchar     *datadir)
 {
 	EggPluginsLoaderC *cloader = EGG_PLUGINS_LOADER_C (loader);
 	EggPluginsObjectModule *module;
@@ -75,12 +76,9 @@ egg_plugins_loader_iface_load (EggPluginsLoader *loader,
 		return NULL;
 	}
 
-	/* TODO: for now we force data-dir-name = module-name... if needed we can
-	 * add a datadir field to the plugin descriptor file.
-	 */
 	result = (EggPluginsPlugin *) egg_plugins_object_module_new_object (module,
 								"install-dir", path,
-								"data-dir-name", module_name,
+								"data-dir", datadir,
 								NULL);
 
 	if (!result)
diff --git a/plugin-loaders/python/egg-plugins-loader-python.c b/plugin-loaders/python/egg-plugins-loader-python.c
index f6a82ae..9b85f48 100644
--- a/plugin-loaders/python/egg-plugins-loader-python.c
+++ b/plugin-loaders/python/egg-plugins-loader-python.c
@@ -100,7 +100,8 @@ find_python_plugin_type (EggPluginsInfo *info,
 
 static EggPluginsPlugin *
 new_plugin_from_info (EggPluginsLoaderPython *loader,
-		      EggPluginsInfo          *info)
+		      EggPluginsInfo         *info,
+		      const gchar            *datadir)
 {
 	PythonInfo *pyinfo;
 	PyTypeObject *pytype;
@@ -140,7 +141,7 @@ new_plugin_from_info (EggPluginsLoaderPython *loader,
 
 	pygobject_construct (pygobject,
 			     "install-dir", pyinfo->path,
-			     "data-dir-name", egg_plugins_info_get_module_name (info),
+			     "data-dir", datadir,
 			     NULL);
 
 	if (pygobject->obj == NULL)
@@ -176,6 +177,7 @@ add_python_info (EggPluginsLoaderPython *loader,
 		 EggPluginsInfo          *info,
 		 PyObject		 *module,
 		 const gchar             *path,
+		 const gchar             *datadir,
 		 PyObject                *type)
 {
 	PythonInfo *pyinfo;
@@ -188,7 +190,7 @@ add_python_info (EggPluginsLoaderPython *loader,
 
 	g_hash_table_insert (loader->priv->loaded_plugins, info, pyinfo);
 
-	return new_plugin_from_info (loader, info);
+	return new_plugin_from_info (loader, info, datadir);
 }
 
 static const gchar *
@@ -199,8 +201,9 @@ egg_plugins_loader_iface_get_id (void)
 
 static EggPluginsPlugin *
 egg_plugins_loader_iface_load (EggPluginsLoader *loader,
-				EggPluginsInfo   *info,
-				const gchar       *path)
+			       EggPluginsInfo   *info,
+			       const gchar      *path,
+			       const gchar      *datadir)
 {
 	EggPluginsLoaderPython *pyloader = EGG_PLUGINS_LOADER_PYTHON (loader);
 	PyObject *main_module, *main_locals, *pytype;
@@ -217,7 +220,7 @@ egg_plugins_loader_iface_load (EggPluginsLoader *loader,
 	}
 
 	/* see if py definition for the plugin is already loaded */
-	result = new_plugin_from_info (pyloader, info);
+	result = new_plugin_from_info (pyloader, info, datadir);
 
 	if (result != NULL)
 		return result;
@@ -268,7 +271,7 @@ egg_plugins_loader_iface_load (EggPluginsLoader *loader,
 	pytype = find_python_plugin_type (info, pymodule);
 
 	if (pytype)
-		return add_python_info (pyloader, info, pymodule, path, pytype);
+		return add_python_info (pyloader, info, pymodule, path, datadir, pytype);
 
 	return NULL;
 }



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