Blob Blame History Raw
From 099aa09ff5eabf24005fcf94ca6a71a1a7138228 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Wilmet?= <swilmet@gnome.org>
Date: Sun, 7 Jun 2015 14:51:43 +0200
Subject: [PATCH 09/26] spell: fix bug with document metadata

See the comment in the code.

With one document, the bug was easy to reproduce:
1. open document <file> in gedit
2. activate the auto spell -> metadata set to 1
3. close gedit
4. $ gedit <file> -> spell checking disabled, but should be enabled

https://bugzilla.gnome.org/show_bug.cgi?id=741853
---
 plugins/spell/gedit-spell-plugin.c | 56 +++++++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 10 deletions(-)

--- a/plugins/spell/gedit-spell-plugin.c
+++ b/plugins/spell/gedit-spell-plugin.c
@@ -89,15 +89,16 @@ G_DEFINE_DYNAMIC_TYPE_EXTENDED (GeditSpe
 							       gedit_window_activatable_iface_init)
 				G_ADD_PRIVATE_DYNAMIC (GeditSpellPlugin))
 
-static void	spell_cb	(GSimpleAction *action, GVariant *parameter, gpointer data);
-static void	set_language_cb	(GSimpleAction *action, GVariant *parameter, gpointer data);
-static void	auto_spell_cb	(GSimpleAction *action, GVariant *state, gpointer data);
+static void	spell_cb			(GSimpleAction *action, GVariant *parameter, gpointer data);
+static void	set_language_cb			(GSimpleAction *action, GVariant *parameter, gpointer data);
+static void	auto_spell_activate_cb		(GSimpleAction *action, GVariant *parameter, gpointer data);
+static void	auto_spell_change_state_cb	(GSimpleAction *action, GVariant *state, gpointer data);
 
 static GActionEntry action_entries[] =
 {
 	{ "check-spell", spell_cb },
 	{ "config-spell", set_language_cb },
-	{ "auto-spell", NULL, NULL, "false", auto_spell_cb }
+	{ "auto-spell", auto_spell_activate_cb, NULL, "false", auto_spell_change_state_cb }
 };
 
 static GQuark spell_checker_id = 0;
@@ -874,20 +875,27 @@ set_auto_spell (GeditWindow   *window,
 }
 
 static void
-auto_spell_cb (GSimpleAction  *action,
-               GVariant       *state,
-               gpointer        data)
+auto_spell_activate_cb (GSimpleAction *action,
+			GVariant      *parameter,
+			gpointer       data)
 {
 	GeditSpellPlugin *plugin = GEDIT_SPELL_PLUGIN (data);
 	GeditSpellPluginPrivate *priv = plugin->priv;
-	GeditView *view;
+	GVariant *state;
 	gboolean active;
+	GeditView *view;
 
 	gedit_debug (DEBUG_PLUGINS);
 
+	state = g_action_get_state (G_ACTION (action));
+	g_return_if_fail (state != NULL);
+
 	active = g_variant_get_boolean (state);
+	g_variant_unref (state);
 
-	gedit_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated");
+	/* We must toggle ourself the value. */
+	active = !active;
+	g_action_change_state (G_ACTION (action), g_variant_new_boolean (active));
 
 	view = gedit_window_get_active_view (priv->window);
 	if (view != NULL)
@@ -896,10 +904,38 @@ auto_spell_cb (GSimpleAction  *action,
 
 		doc = GEDIT_DOCUMENT (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
 
+		/* Set metadata in the "activate" handler, not in "change-state"
+		 * because "change-state" is called every time the state
+		 * changes, not specifically when the user has changed the state
+		 * herself. For example "change-state" is called to initialize
+		 * the sate to the default value specified in the GActionEntry.
+		 */
 		gedit_document_set_metadata (doc,
 					     GEDIT_METADATA_ATTRIBUTE_SPELL_ENABLED,
-					     active ? "1" : NULL, NULL);
+					     active ? "1" : NULL,
+					     NULL);
+	}
+}
+
+static void
+auto_spell_change_state_cb (GSimpleAction *action,
+			    GVariant      *state,
+			    gpointer       data)
+{
+	GeditSpellPlugin *plugin = GEDIT_SPELL_PLUGIN (data);
+	GeditSpellPluginPrivate *priv = plugin->priv;
+	GeditView *view;
+	gboolean active;
+
+	gedit_debug (DEBUG_PLUGINS);
+
+	active = g_variant_get_boolean (state);
 
+	gedit_debug_message (DEBUG_PLUGINS, active ? "Auto Spell activated" : "Auto Spell deactivated");
+
+	view = gedit_window_get_active_view (priv->window);
+	if (view != NULL)
+	{
 		set_auto_spell (priv->window, view, active);
 		g_simple_action_set_state (action, g_variant_new_boolean (active));
 	}