[gedit/libgpe: 21/21] Handle loading/saving of the active plugins list in gconf.



commit 704d940304b0ed9de0ccab2b1368f06daeffba90
Author: Steve Frécinaux <code istique net>
Date:   Thu Nov 5 00:42:46 2009 +0100

    Handle loading/saving of the active plugins list in gconf.

 gedit/gedit-plugins-engine.c |   90 +++++++++++++++++++++++++++++-------------
 1 files changed, 62 insertions(+), 28 deletions(-)
---
diff --git a/gedit/gedit-plugins-engine.c b/gedit/gedit-plugins-engine.c
index fbde7f4..9f92708 100644
--- a/gedit/gedit-plugins-engine.c
+++ b/gedit/gedit-plugins-engine.c
@@ -50,7 +50,9 @@ G_DEFINE_TYPE(GeditPluginsEngine, gedit_plugins_engine, GPE_TYPE_ENGINE)
 
 struct _GeditPluginsEnginePrivate
 {
-	gboolean activate_from_prefs;
+	/* We use this flag to avoid saving the plugin list when (de)activating
+	 * a plugin on active_plugins_changed(), avoiding a notification loop. */
+	gboolean setting_active_plugins;
 };
 
 GPEEngine *default_engine = NULL;
@@ -64,9 +66,58 @@ gedit_plugins_engine_init (GeditPluginsEngine *engine)
 						    GEDIT_TYPE_PLUGINS_ENGINE,
 						    GeditPluginsEnginePrivate);
 
-	/* make sure that the first reactivation will read active plugins
-	   from the prefs */
-	engine->priv->activate_from_prefs = TRUE;
+	engine->priv->setting_active_plugins = FALSE;
+}
+
+static void
+save_active_plugin_list (GPEEngine *engine)
+{
+	GSList *list = NULL;
+	gchar **active_plugins;
+	guint i;
+
+	g_debug ("Saving the plugin list.");
+
+	active_plugins = gpe_engine_get_active_plugins (engine);
+
+	for (i = 0; active_plugins[i] != NULL; i++)
+		list = g_slist_prepend (list, active_plugins[i]);
+
+	gedit_prefs_manager_set_active_plugins (list);
+
+	g_slist_free (list);
+	g_boxed_free (G_TYPE_STRV, active_plugins);
+}
+
+static void
+gedit_plugins_engine_activate_plugin (GPEEngine     *engine,
+				      GPEPluginInfo *info)
+{
+	GPE_ENGINE_CLASS (gedit_plugins_engine_parent_class)->activate_plugin (engine, info);
+
+	/* We won't save the plugin list if we are currently activating the
+	   plugins from the saved list */
+	if (GEDIT_PLUGINS_ENGINE (engine)->priv->setting_active_plugins)
+		return;
+
+	if (gpe_plugin_info_is_active (info))
+		save_active_plugin_list (engine);
+}
+
+
+static void
+gedit_plugins_engine_deactivate_plugin (GPEEngine     *engine,
+					GPEPluginInfo *info)
+{
+	GPE_ENGINE_CLASS (gedit_plugins_engine_parent_class)->deactivate_plugin (engine, info);
+
+	/* We won't save the plugin list if we are currently deactivating
+	   plugins from the saved list */
+	if (GEDIT_PLUGINS_ENGINE (engine)->priv->setting_active_plugins)
+		return;
+
+	if (!gpe_plugin_info_is_active (info))
+		save_active_plugin_list (engine);
 }
 
 static void
@@ -98,6 +149,8 @@ gedit_plugins_engine_class_init (GeditPluginsEngineClass *klass)
 	GPEEngineClass *engine_class = GPE_ENGINE_CLASS (klass);
 
 	object_class->finalize = gedit_plugins_engine_finalize;
+	engine_class->activate_plugin = gedit_plugins_engine_activate_plugin;
+	engine_class->deactivate_plugin = gedit_plugins_engine_deactivate_plugin;
 	engine_class->deactivate_plugin_on_object = gedit_plugins_engine_deactivate_plugin_on_object;
 
 	g_type_class_add_private (klass, sizeof (GeditPluginsEnginePrivate));
@@ -132,35 +185,14 @@ gedit_plugins_engine_get_default (void)
 	g_free (module_dir);
 	g_free (data_dir);
 
+	/* Load the plugin list from gconf */
+	gedit_plugins_engine_active_plugins_changed (GEDIT_PLUGINS_ENGINE (default_engine));
+
 	g_object_add_weak_pointer (G_OBJECT (default_engine),
 				   (gpointer) &default_engine);
 	return default_engine;
 }
 
-#if 0
-static void
-save_active_plugin_list (GeditPluginsEngine *engine)
-{
-	GSList *active_plugins = NULL;
-	GList *l;
-
-	for (l = engine->priv->plugin_list; l != NULL; l = l->next)
-	{
-		GeditPluginInfo *info = (GeditPluginInfo *) l->data;
-
-		if (gedit_plugin_info_is_active (info))
-		{
-			active_plugins = g_slist_prepend (active_plugins,
-							  (gpointer)gedit_plugin_info_get_module_name (info));
-		}
-	}
-
-	gedit_prefs_manager_set_active_plugins (active_plugins);
-
-	g_slist_free (active_plugins);
-}
-#endif
-
 void 
 gedit_plugins_engine_active_plugins_changed (GeditPluginsEngine *engine)
 {
@@ -178,7 +210,9 @@ gedit_plugins_engine_active_plugins_changed (GeditPluginsEngine *engine)
 		*s = (const gchar *) p->data;
 	*s = NULL;
 
+	engine->priv->setting_active_plugins = TRUE;
 	gpe_engine_set_active_plugins (GPE_ENGINE (engine), strv);
+	engine->priv->setting_active_plugins = FALSE;
 
 	g_free (strv);
 	g_slist_foreach (active_plugins, (GFunc) g_free, NULL);



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