[gedit/eggplugins] [libplugins] Add engine.add/remove_object API.



commit 7345964eda62fa1a53a4072eb6de0889f1b1d67f
Author: Steve Frécinaux <code istique net>
Date:   Sun Oct 11 20:22:53 2009 +0200

    [libplugins] Add engine.add/remove_object API.
    
    This allows the plugin engine to keep track of the objects the plugins
    are activated on, allowing to activate/deactivate plugins on all those
    objects when needed.
    
    This also gets rid of the GeditApp usage in EggPluginsEngine.

 gedit/gedit-window.c            |    8 ++--
 libplugins/egg-plugins-engine.c |   69 ++++++++++++++++++++++++++------------
 libplugins/egg-plugins-engine.h |   10 +++--
 3 files changed, 57 insertions(+), 30 deletions(-)
---
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index a3f44ea..9a69cd0 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -181,8 +181,8 @@ gedit_window_dispose (GObject *object)
 	{
 		save_panes_state (window);
 
-		egg_plugins_engine_deactivate_plugins (gedit_plugins_engine_get_default (),
-					                  window);
+		egg_plugins_engine_remove_object (gedit_plugins_engine_get_default (),
+					          G_OBJECT (window));
 		window->priv->dispose_has_run = TRUE;
 	}
 
@@ -3906,8 +3906,8 @@ gedit_window_init (GeditWindow *window)
 
 	gedit_debug_message (DEBUG_WINDOW, "Update plugins ui");
 	
-	egg_plugins_engine_activate_plugins (gedit_plugins_engine_get_default (),
-					        window);
+	egg_plugins_engine_add_object (gedit_plugins_engine_get_default (),
+				       G_OBJECT (window));
 
 	/* set visibility of panes.
 	 * This needs to be done after plugins activatation */
diff --git a/libplugins/egg-plugins-engine.c b/libplugins/egg-plugins-engine.c
index 912cbd0..8089163 100644
--- a/libplugins/egg-plugins-engine.c
+++ b/libplugins/egg-plugins-engine.c
@@ -1,8 +1,9 @@
 /*
- * gedit-plugins-engine.c
+ * egg-plugins-engine.c
  * This file is part of gedit
  *
  * Copyright (C) 2002-2005 Paolo Maggi
+ * Copyright (C) 2009 Steve Frécinaux
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,11 +22,9 @@
  */
 
 /*
- * Modified by the gedit Team, 2002-2005. See the AUTHORS file for a
+ * Modified by the gedit Team, 2002-2009. See the AUTHORS file for a
  * list of people on the gedit Team.
  * See the ChangeLog files for a list of changes.
- *
- * $Id$
  */
 
 #ifdef HAVE_CONFIG_H
@@ -41,7 +40,6 @@
 #include "egg-plugins-loader.h"
 #include "egg-plugins-object-module.h"
 #include "egg-plugins-plugin.h"
-#include <gedit/gedit-app.h>
 #include <gedit/gedit-prefs-manager.h>
 #include <gedit/gedit-dirs.h>
 
@@ -76,6 +74,8 @@ struct _EggPluginsEnginePrivate
 	GList *plugin_list;
 	GHashTable *loaders;
 
+	GList *object_list;
+
 	gboolean activate_from_prefs;
 };
 
@@ -276,6 +276,8 @@ egg_plugins_engine_init (EggPluginsEngine *engine)
 						    EGG_PLUGINS_TYPE_ENGINE,
 						    EggPluginsEnginePrivate);
 
+	engine->priv->object_list = NULL;
+
 	load_all_plugins (engine);
 
 	/* make sure that the first reactivation will read active plugins
@@ -310,7 +312,7 @@ egg_plugins_engine_finalize (GObject *object)
 	EggPluginsEngine *engine = EGG_PLUGINS_ENGINE (object);
 	GList *item;
 
-	/* Firs deactivate all plugins */
+	/* First deactivate all plugins */
 	for (item = engine->priv->plugin_list; item; item = item->next)
 	{
 		EggPluginsInfo *info = EGG_PLUGINS_INFO (item->data);
@@ -331,6 +333,7 @@ egg_plugins_engine_finalize (GObject *object)
 	}
 
 	g_list_free (engine->priv->plugin_list);
+	g_list_free (engine->priv->object_list);
 
 	G_OBJECT_CLASS (egg_plugins_engine_parent_class)->finalize (object);
 }
@@ -613,17 +616,13 @@ static void
 egg_plugins_engine_activate_plugin_real (EggPluginsEngine *engine,
 					 EggPluginsInfo   *info)
 {
-	const GList *wins;
+	const GList *item;
 
 	if (!load_plugin (engine, info))
 		return;
 
-	for (wins = gedit_app_get_windows (gedit_app_get_default ());
-	     wins != NULL;
-	     wins = wins->next)
-	{
-		egg_plugins_plugin_activate (info->plugin, G_OBJECT (wins->data));
-	}
+	for (item = engine->priv->object_list; item != NULL; item = item->next);
+		egg_plugins_plugin_activate (info->plugin, G_OBJECT (item->data));
 }
 
 gboolean
@@ -650,19 +649,15 @@ static void
 egg_plugins_engine_deactivate_plugin_real (EggPluginsEngine *engine,
 					   EggPluginsInfo   *info)
 {
-	const GList *wins;
+	const GList *item;
 	EggPluginsLoader *loader;
 
 	if (!egg_plugins_info_is_active (info) ||
 	    !egg_plugins_info_is_available (info))
 		return;
 
-	for (wins = gedit_app_get_windows (gedit_app_get_default ());
-	     wins != NULL;
-	     wins = wins->next)
-	{
-		egg_plugins_plugin_deactivate (info->plugin, G_OBJECT (wins->data));
-	}
+	for (item = engine->priv->object_list; item != NULL; item = item->next);
+		egg_plugins_plugin_deactivate (info->plugin, G_OBJECT (item->data));
 
 	/* let engine subclasses perform required cleanups. */
 	EGG_PLUGINS_ENGINE_GET_CLASS (engine)->plugin_deactivated (engine, info);
@@ -695,7 +690,7 @@ egg_plugins_engine_deactivate_plugin (EggPluginsEngine *engine,
 	return !egg_plugins_info_is_active (info);
 }
 
-void
+static void
 egg_plugins_engine_activate_plugins (EggPluginsEngine *engine,
 				     GObject          *target_object)
 {
@@ -741,7 +736,7 @@ egg_plugins_engine_activate_plugins (EggPluginsEngine *engine,
 	egg_plugins_engine_update_plugins_ui (engine, target_object);
 }
 
-void
+static void
 egg_plugins_engine_deactivate_plugins (EggPluginsEngine *engine,
 				       GObject          *target_object)
 {
@@ -849,3 +844,33 @@ egg_plugins_engine_rescan_plugins (EggPluginsEngine *engine)
 {
 	load_all_plugins (engine);
 }
+
+void
+egg_plugins_engine_add_object (EggPluginsEngine *engine, GObject *object)
+{
+	g_return_if_fail (EGG_PLUGINS_IS_ENGINE (engine));
+	g_return_if_fail (G_IS_OBJECT (object));
+
+	/* Ensure we don't insert the same object twice... */
+	if (g_list_find (engine->priv->object_list, object))
+		return;
+
+	/* Activate the plugin on object, and add it to the list of managed objects */
+	egg_plugins_engine_activate_plugins (engine, object);
+	engine->priv->object_list = g_list_prepend (engine->priv->object_list, object);
+}
+
+void
+egg_plugins_engine_remove_object (EggPluginsEngine *engine, GObject *object)
+{
+	g_return_if_fail (EGG_PLUGINS_IS_ENGINE (engine));
+	g_return_if_fail (G_IS_OBJECT (object));
+
+	GList *item = g_list_find (engine->priv->object_list, object);
+	if (item == NULL)
+		return;
+
+	/* Remove the object to the list of managed objects, and deactivate the plugin on it */
+	engine->priv->object_list = g_list_delete_link (engine->priv->object_list, item);
+	egg_plugins_engine_deactivate_plugins (engine, object);
+}
diff --git a/libplugins/egg-plugins-engine.h b/libplugins/egg-plugins-engine.h
index 35cdff0..3a4f495 100644
--- a/libplugins/egg-plugins-engine.h
+++ b/libplugins/egg-plugins-engine.h
@@ -92,10 +92,6 @@ void	 	 egg_plugins_engine_configure_plugin	(EggPluginsEngine *engine,
 							 GtkWindow        *parent);
 
 /* plugin activation/deactivation per target_object */
-void 		 egg_plugins_engine_activate_plugins   (EggPluginsEngine *engine,
-							GObject          *target_object);
-void 		 egg_plugins_engine_deactivate_plugins (EggPluginsEngine *engine,
-							GObject          *target_object);
 void		 egg_plugins_engine_update_plugins_ui  (EggPluginsEngine *engine,
 							GObject          *target_object);
 
@@ -105,6 +101,12 @@ void		 egg_plugins_engine_active_plugins_changed
 
 void		 egg_plugins_engine_rescan_plugins	(EggPluginsEngine *engine);
 
+/* object management */
+void		 egg_plugins_engine_add_object		(EggPluginsEngine *engine,
+							 GObject *object);
+void		 egg_plugins_engine_remove_object	(EggPluginsEngine *engine,
+							 GObject *object);
+
 G_END_DECLS
 
 #endif  /* __EGG_PLUGINS_ENGINE_H__ */



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