[gedit] Remember if the autospell was enabled per document. Fixes bug #451042.



commit 41fb213bfcd4ffe4b7c3adb7ffa8d286a65040e3
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Dec 1 12:56:58 2009 +0100

    Remember if the autospell was enabled per document. Fixes bug #451042.

 plugins/spell/gedit-spell-plugin.c |  177 ++++++++++++++++++++++++++++++++----
 1 files changed, 159 insertions(+), 18 deletions(-)
---
diff --git a/plugins/spell/gedit-spell-plugin.c b/plugins/spell/gedit-spell-plugin.c
index 6cc9d67..9d7ad04 100644
--- a/plugins/spell/gedit-spell-plugin.c
+++ b/plugins/spell/gedit-spell-plugin.c
@@ -43,8 +43,10 @@
 #ifdef G_OS_WIN32
 #include <gedit/gedit-metadata-manager.h>
 #define GEDIT_METADATA_ATTRIBUTE_SPELL_LANGUAGE "spell-language"
+#define GEDIT_METADATA_ATTRIBUTE_SPELL_ENABLED  "spell-enabled"
 #else
 #define GEDIT_METADATA_ATTRIBUTE_SPELL_LANGUAGE "metadata::gedit-spell-language"
+#define GEDIT_METADATA_ATTRIBUTE_SPELL_ENABLED  "metadata::gedit-spell-enabled"
 #endif
 
 #define WINDOW_DATA_KEY "GeditSpellPluginWindowData"
@@ -61,6 +63,8 @@ typedef struct
 	GtkActionGroup *action_group;
 	guint           ui_id;
 	guint           message_cid;
+	gulong          tab_added_id;
+	gulong          tab_removed_id;
 } WindowData;
 
 typedef struct
@@ -806,23 +810,12 @@ spell_cb (GtkAction   *action,
 }
 
 static void
-auto_spell_cb (GtkAction   *action,
-	       GeditWindow *window)
+set_auto_spell (GeditWindow   *window,
+		GeditDocument *doc,
+		gboolean       active)
 {
 	GeditAutomaticSpellChecker *autospell;
-	GeditDocument *doc;
 	GeditSpellChecker *spell;
-	gboolean active;
-
-	gedit_debug (DEBUG_PLUGINS);
-
-	active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
-
-	gedit_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated");
-
-	doc = gedit_window_get_active_document (window);
-	if (doc == NULL)
-		return;
 
 	spell = get_spell_checker_from_document (doc);
 	g_return_if_fail (spell != NULL);
@@ -851,6 +844,31 @@ auto_spell_cb (GtkAction   *action,
 }
 
 static void
+auto_spell_cb (GtkAction   *action,
+	       GeditWindow *window)
+{
+	
+	GeditDocument *doc;
+	gboolean active;
+
+	gedit_debug (DEBUG_PLUGINS);
+
+	active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+
+	gedit_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated");
+
+	doc = gedit_window_get_active_document (window);
+	if (doc == NULL)
+		return;
+
+	gedit_document_set_metadata (doc,
+				     GEDIT_METADATA_ATTRIBUTE_SPELL_ENABLED,
+				     active ? "1" : NULL, NULL);
+
+	set_auto_spell (window, doc, active);
+}
+
+static void
 free_window_data (WindowData *data)
 {
 	g_return_if_fail (data != NULL);
@@ -883,8 +901,31 @@ update_ui_real (GeditWindow *window,
 
 	autospell = (doc != NULL &&
 	             gedit_automatic_spell_checker_get_from_document (doc) != NULL);
-	action = gtk_action_group_get_action (data->action_group, "AutoSpell");
-	gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), autospell);
+
+	if (doc != NULL)
+	{
+		GeditTab *tab;
+		GeditTabState state;
+
+		tab = gedit_window_get_active_tab (window);
+		state = gedit_tab_get_state (tab);
+
+		/* If the document is loading we can't get the metadata so we
+		   endup with an useless speller */
+		if (state == GEDIT_TAB_STATE_NORMAL)
+		{
+			action = gtk_action_group_get_action (data->action_group,
+							      "AutoSpell");
+	
+			g_signal_handlers_block_by_func (action, auto_spell_cb,
+							 window);
+			set_auto_spell (window, doc, autospell);
+			gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
+						      autospell);
+			g_signal_handlers_unblock_by_func (action, auto_spell_cb,
+							   window);
+		}
+	}
 
 	gtk_action_group_set_sensitive (data->action_group,
 					(view != NULL) &&
@@ -892,12 +933,97 @@ update_ui_real (GeditWindow *window,
 }
 
 static void
+set_auto_spell_from_metadata (GeditWindow    *window,
+			      GeditDocument  *doc,
+			      GtkActionGroup *action_group)
+{
+	gboolean active = FALSE;
+	gchar *active_str;
+	GeditDocument *active_doc;
+
+	active_str = gedit_document_get_metadata (doc,
+						  GEDIT_METADATA_ATTRIBUTE_SPELL_ENABLED);
+
+	if (active_str)
+	{
+		active = *active_str == '1';
+	
+		g_free (active_str);
+	}
+
+	set_auto_spell (window, doc, active);
+
+	/* In case that the doc is the active one we mark the spell action */
+	active_doc = gedit_window_get_active_document (window);
+
+	if (active_doc == doc && action_group != NULL)
+	{
+		GtkAction *action;
+		
+		action = gtk_action_group_get_action (action_group,
+						      "AutoSpell");
+
+		g_signal_handlers_block_by_func (action, auto_spell_cb,
+						 window);
+		gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
+					      active);
+		g_signal_handlers_unblock_by_func (action, auto_spell_cb,
+						   window);
+	}
+}
+
+static void
+on_document_loaded (GeditDocument *doc,
+		    const GError  *error,
+		    GeditWindow   *window)
+{
+	if (error == NULL)
+	{
+		WindowData *data = g_object_get_data (G_OBJECT (window),
+						      WINDOW_DATA_KEY);
+	
+		set_auto_spell_from_metadata (window, doc, data->action_group);
+	}
+}
+
+static void
+tab_added_cb (GeditWindow *window,
+	      GeditTab    *tab,
+	      gpointer     useless)
+{
+	GeditDocument *doc;
+	GeditView *view;
+
+	doc = gedit_tab_get_document (tab);
+	view = gedit_tab_get_view (tab);
+
+	g_signal_connect (doc, "loaded",
+			  G_CALLBACK (on_document_loaded),
+			  window);
+}
+
+static void
+tab_removed_cb (GeditWindow *window,
+		GeditTab    *tab,
+		gpointer     useless)
+{
+	GeditDocument *doc;
+	GeditView *view;
+
+	doc = gedit_tab_get_document (tab);
+	view = gedit_tab_get_view (tab);
+	
+	g_signal_handlers_disconnect_by_func (doc, on_document_loaded, window);
+}
+
+static void
 impl_activate (GeditPlugin *plugin,
 	       GeditWindow *window)
 {
 	GtkUIManager *manager;
 	WindowData *data;
 	ActionData *action_data;
+	GList *docs, *l;
 
 	gedit_debug (DEBUG_PLUGINS);
 
@@ -958,9 +1084,21 @@ impl_activate (GeditPlugin *plugin,
 			       GTK_UI_MANAGER_MENUITEM, 
 			       FALSE);
 
-	
-
 	update_ui_real (window, data);
+
+	docs = gedit_window_get_documents (window);
+	for (l = docs; l != NULL; l = g_list_next (l))
+	{
+		set_auto_spell_from_metadata (window, GEDIT_DOCUMENT (l->data),
+					      data->action_group);
+	}
+
+	data->tab_added_id =
+		g_signal_connect (window, "tab-added",
+				  G_CALLBACK (tab_added_cb), NULL);
+	data->tab_removed_id =
+		g_signal_connect (window, "tab-removed",
+				  G_CALLBACK (tab_removed_cb), NULL);
 }
 
 static void
@@ -980,6 +1118,9 @@ impl_deactivate	(GeditPlugin *plugin,
 	gtk_ui_manager_remove_ui (manager, data->ui_id);
 	gtk_ui_manager_remove_action_group (manager, data->action_group);
 
+	g_signal_handler_disconnect (window, data->tab_added_id);
+	g_signal_handler_disconnect (window, data->tab_removed_id);
+
 	g_object_set_data (G_OBJECT (window), WINDOW_DATA_KEY, NULL);
 }
 



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