[gedit/wip/spell-checking] spell-checker: add a GError parameter to check_word()



commit 196aa1b41da7c2db3038f7944549a6779b61920a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jul 24 12:24:04 2015 +0200

    spell-checker: add a GError parameter to check_word()

 plugins/spell/gedit-automatic-spell-checker.c |   14 ++++++-
 plugins/spell/gedit-spell-checker-dialog.c    |   15 ++++++-
 plugins/spell/gedit-spell-checker.c           |   56 +++++++++++++-----------
 plugins/spell/gedit-spell-checker.h           |   29 ++++++++++++-
 plugins/spell/gedit-spell-plugin.c            |   10 ++++-
 5 files changed, 92 insertions(+), 32 deletions(-)
---
diff --git a/plugins/spell/gedit-automatic-spell-checker.c b/plugins/spell/gedit-automatic-spell-checker.c
index e2c0317..91a450d 100644
--- a/plugins/spell/gedit-automatic-spell-checker.c
+++ b/plugins/spell/gedit-automatic-spell-checker.c
@@ -74,6 +74,8 @@ check_word (GeditAutomaticSpellChecker *spell,
            const GtkTextIter          *end)
 {
        gchar *word;
+       GError *error = NULL;
+       gboolean correctly_spelled;
 
        if (!gtk_text_iter_starts_word (start) ||
            !gtk_text_iter_ends_word (end))
@@ -84,7 +86,17 @@ check_word (GeditAutomaticSpellChecker *spell,
 
        word = gtk_text_buffer_get_text (spell->buffer, start, end, FALSE);
 
-       if (!gedit_spell_checker_check_word (spell->spell_checker, word))
+       correctly_spelled = gedit_spell_checker_check_word (spell->spell_checker,
+                                                           word,
+                                                           &error);
+
+       if (error != NULL)
+       {
+               g_warning ("Automatic spell checker: %s", error->message);
+               g_error_free (error);
+       }
+
+       if (!correctly_spelled)
        {
                gtk_text_buffer_apply_tag (spell->buffer,
                                           spell->tag_highlight,
diff --git a/plugins/spell/gedit-spell-checker-dialog.c b/plugins/spell/gedit-spell-checker-dialog.c
index b209c9b..2685dd8 100644
--- a/plugins/spell/gedit-spell-checker-dialog.c
+++ b/plugins/spell/gedit-spell-checker-dialog.c
@@ -385,7 +385,7 @@ gedit_spell_checker_dialog_set_misspelled_word (GeditSpellCheckerDialog *dlg,
        g_return_if_fail (word != NULL);
 
        g_return_if_fail (dlg->spell_checker != NULL);
-       g_return_if_fail (!gedit_spell_checker_check_word (dlg->spell_checker, word));
+       g_return_if_fail (!gedit_spell_checker_check_word (dlg->spell_checker, word, NULL));
 
        g_free (dlg->misspelled_word);
        dlg->misspelled_word = g_strdup (word);
@@ -507,13 +507,24 @@ static void
 check_word_button_clicked_handler (GtkButton *button, GeditSpellCheckerDialog *dlg)
 {
        const gchar *word;
+       GError *error = NULL;
+       gboolean correctly_spelled;
 
        g_return_if_fail (GEDIT_IS_SPELL_CHECKER_DIALOG (dlg));
 
        word = gtk_entry_get_text (GTK_ENTRY (dlg->word_entry));
        g_return_if_fail (gtk_entry_get_text_length (GTK_ENTRY (dlg->word_entry)) > 0);
 
-       if (gedit_spell_checker_check_word (dlg->spell_checker, word))
+       correctly_spelled = gedit_spell_checker_check_word (dlg->spell_checker, word, &error);
+
+       if (error != NULL)
+       {
+               g_warning ("Spell checker dialog: %s", error->message);
+               g_error_free (error);
+               return;
+       }
+
+       if (correctly_spelled)
        {
                GtkListStore *store;
                GtkTreeIter iter;
diff --git a/plugins/spell/gedit-spell-checker.c b/plugins/spell/gedit-spell-checker.c
index 61dc6ff..772e061 100644
--- a/plugins/spell/gedit-spell-checker.c
+++ b/plugins/spell/gedit-spell-checker.c
@@ -24,6 +24,7 @@
 
 #include "gedit-spell-checker.h"
 #include <enchant.h>
+#include <glib/gi18n.h>
 #include "gedit-spell-utils.h"
 
 #ifdef OS_OSX
@@ -57,6 +58,19 @@ static guint signals[LAST_SIGNAL] = { 0 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GeditSpellChecker, gedit_spell_checker, G_TYPE_OBJECT)
 
+GQuark
+gedit_spell_checker_error_quark (void)
+{
+       static GQuark quark = 0;
+
+       if (G_UNLIKELY (quark == 0))
+       {
+               quark = g_quark_from_static_string ("gedit-spell-checker-error-quark");
+       }
+
+       return quark;
+}
+
 static void
 gedit_spell_checker_set_property (GObject      *object,
                                  guint         prop_id,
@@ -326,16 +340,19 @@ gedit_spell_checker_get_language (GeditSpellChecker *checker)
        return priv->active_lang;
 }
 
+/* Returns whether @word is correctly spelled. */
 gboolean
-gedit_spell_checker_check_word (GeditSpellChecker *checker,
-                               const gchar       *word)
+gedit_spell_checker_check_word (GeditSpellChecker  *checker,
+                               const gchar        *word,
+                               GError            **error)
 {
        GeditSpellCheckerPrivate *priv;
        gint enchant_result;
-       gboolean res = FALSE;
+       gboolean correctly_spelled;
 
        g_return_val_if_fail (GEDIT_IS_SPELL_CHECKER (checker), FALSE);
        g_return_val_if_fail (word != NULL, FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
        priv = gedit_spell_checker_get_instance_private (checker);
 
@@ -351,32 +368,19 @@ gedit_spell_checker_check_word (GeditSpellChecker *checker,
 
        enchant_result = enchant_dict_check (priv->dict, word, -1);
 
-       switch (enchant_result)
-       {
-               case -1:
-                       /* error */
-                       res = FALSE;
-
-                       g_warning ("Spell checker plugin: error checking word '%s' (%s).",
-                                  word, enchant_dict_get_error (priv->dict));
+       correctly_spelled = enchant_result == 0;
 
-                       break;
-
-               case 1:
-                       /* it is not in the directory */
-                       res = FALSE;
-                       break;
-
-               case 0:
-                       /* is is in the directory */
-                       res = TRUE;
-                       break;
-
-               default:
-                       g_return_val_if_reached (FALSE);
+       if (enchant_result < 0)
+       {
+               g_set_error (error,
+                            GEDIT_SPELL_CHECKER_ERROR,
+                            GEDIT_SPELL_CHECKER_ERROR_DICTIONARY,
+                            _("Error when checking the spelling of word “%s”: %s"),
+                            word,
+                            enchant_dict_get_error (priv->dict));
        }
 
-       return res;
+       return correctly_spelled;
 }
 
 /* return NULL on error or if no suggestions are found */
diff --git a/plugins/spell/gedit-spell-checker.h b/plugins/spell/gedit-spell-checker.h
index c96289f..b0d78d5 100644
--- a/plugins/spell/gedit-spell-checker.h
+++ b/plugins/spell/gedit-spell-checker.h
@@ -31,6 +31,28 @@ G_DECLARE_DERIVABLE_TYPE (GeditSpellChecker, gedit_spell_checker,
                          GEDIT, SPELL_CHECKER,
                          GObject)
 
+/**
+ * GEDIT_SPELL_CHECKER_ERROR:
+ *
+ * Error domain for the spell checker. Errors in this domain will be from the
+ * #GeditSpellCheckerError enumeration. See #GError for more information on
+ * error domains.
+ */
+#define GEDIT_SPELL_CHECKER_ERROR (gedit_spell_checker_error_quark ())
+
+/**
+ * GeditSpellCheckerError:
+ * @GEDIT_SPELL_CHECKER_ERROR_DICTIONARY: dictionary error.
+ *
+ * An error code used with %GEDIT_SPELL_CHECKER_ERROR in a #GError returned
+ * from a spell-checker-related function.
+ */
+typedef enum _GeditSpellCheckerError GeditSpellCheckerError;
+enum _GeditSpellCheckerError
+{
+       GEDIT_SPELL_CHECKER_ERROR_DICTIONARY,
+};
+
 struct _GeditSpellCheckerClass
 {
        GObjectClass parent_class;
@@ -45,6 +67,8 @@ struct _GeditSpellCheckerClass
        void (* clear_session)          (GeditSpellChecker *checker);
 };
 
+GQuark         gedit_spell_checker_error_quark                 (void);
+
 GeditSpellChecker *
                gedit_spell_checker_new                         (const GeditSpellCheckerLanguage *language);
 
@@ -55,8 +79,9 @@ gboolean
 const GeditSpellCheckerLanguage *
                gedit_spell_checker_get_language                (GeditSpellChecker *checker);
 
-gboolean       gedit_spell_checker_check_word                  (GeditSpellChecker *checker,
-                                                                const gchar       *word);
+gboolean       gedit_spell_checker_check_word                  (GeditSpellChecker  *checker,
+                                                                const gchar        *word,
+                                                                GError            **error);
 
 GSList *       gedit_spell_checker_get_suggestions             (GeditSpellChecker *checker,
                                                                 const gchar       *word);
diff --git a/plugins/spell/gedit-spell-plugin.c b/plugins/spell/gedit-spell-plugin.c
index 9a572bb..e81c395 100644
--- a/plugins/spell/gedit-spell-plugin.c
+++ b/plugins/spell/gedit-spell-plugin.c
@@ -552,6 +552,7 @@ get_next_misspelled_word (GeditView *view,
        gint start, end;
        gchar *word;
        GeditSpellChecker *checker;
+       GError *error = NULL;
 
        g_return_val_if_fail (view != NULL, NULL);
 
@@ -572,7 +573,7 @@ get_next_misspelled_word (GeditView *view,
 
        gedit_debug_message (DEBUG_PLUGINS, "Word to check: %s", word);
 
-       while (gedit_spell_checker_check_word (checker, word))
+       while (gedit_spell_checker_check_word (checker, word, &error))
        {
                g_free (word);
 
@@ -591,6 +592,13 @@ get_next_misspelled_word (GeditView *view,
                gedit_debug_message (DEBUG_PLUGINS, "Word to check: %s", word);
        }
 
+       if (error != NULL)
+       {
+               g_warning ("Spell checking plugin: %s", error->message);
+               g_error_free (error);
+               return NULL;
+       }
+
        if (!goto_next_word (doc))
        {
                update_current (doc, gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER (doc)));


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