[gspell/wip/checker-null-lang] checker: accept a NULL language



commit 02aafdf31fc7d850862251c04c4f575e0d41acd9
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jan 16 14:56:25 2016 +0100

    checker: accept a NULL language
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760619

 gspell/gspell-checker.c                    |  146 +++++++++++-----------------
 gspell/gspell-checker.h                    |    4 -
 gspell/gspell-inline-checker-text-buffer.c |    4 -
 3 files changed, 57 insertions(+), 97 deletions(-)
---
diff --git a/gspell/gspell-checker.c b/gspell/gspell-checker.c
index 30d371c..915ca09 100644
--- a/gspell/gspell-checker.c
+++ b/gspell/gspell-checker.c
@@ -2,7 +2,7 @@
  * This file is part of gspell, a spell-checking library.
  *
  * Copyright 2002-2006 - Paolo Maggi
- * Copyright 2015 - Sébastien Wilmet
+ * Copyright 2015, 2016 - Sébastien Wilmet
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -37,10 +37,6 @@
  *
  * #GspellChecker is a spell checker. It is a wrapper around the Enchant
  * library, to have an API based on #GObject.
- *
- * If the #GspellChecker:language is not set correctly, the spell checker
- * should not be used. It can happen when no dictionaries are available, in
- * which case gspell_checker_get_language() returns %NULL.
  */
 
 typedef struct _GspellCheckerPrivate GspellCheckerPrivate;
@@ -83,29 +79,6 @@ gspell_checker_error_quark (void)
        return quark;
 }
 
-gboolean
-_gspell_checker_check_language_set (GspellChecker *checker)
-{
-       GspellCheckerPrivate *priv;
-
-       g_return_val_if_fail (GSPELL_IS_CHECKER (checker), FALSE);
-
-       priv = gspell_checker_get_instance_private (checker);
-
-       g_assert ((priv->active_lang == NULL && priv->dict == NULL) ||
-                 (priv->active_lang != NULL && priv->dict != NULL));
-
-       if (priv->active_lang == NULL)
-       {
-               g_warning ("Spell checker: the language is not correctly set.\n"
-                          "There is maybe no dictionaries available.\n"
-                          "Check the return value of gspell_checker_get_language().");
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
 static void
 gspell_checker_set_property (GObject      *object,
                             guint         prop_id,
@@ -132,14 +105,12 @@ gspell_checker_get_property (GObject    *object,
                             GValue     *value,
                             GParamSpec *pspec)
 {
-       GspellCheckerPrivate *priv;
-
-       priv = gspell_checker_get_instance_private (GSPELL_CHECKER (object));
+       GspellChecker *checker = GSPELL_CHECKER (object);
 
        switch (prop_id)
        {
                case PROP_LANGUAGE:
-                       g_value_set_boxed (value, priv->active_lang);
+                       g_value_set_boxed (value, gspell_checker_get_language (checker));
                        break;
 
                default:
@@ -259,8 +230,8 @@ gspell_checker_init (GspellChecker *checker)
  * gspell_checker_new:
  * @language: (nullable): the #GspellLanguage to use, or %NULL.
  *
- * Creates a new #GspellChecker. If @language is %NULL, the default language is
- * picked with gspell_language_get_default().
+ * Creates a new #GspellChecker. If @language is %NULL, the spell checker will
+ * be disabled.
  *
  * Returns: a new #GspellChecker object.
  */
@@ -272,45 +243,40 @@ gspell_checker_new (const GspellLanguage *language)
                             NULL);
 }
 
-static gboolean
-init_dictionary (GspellChecker *checker)
+static void
+create_new_dictionary (GspellChecker *checker)
 {
        GspellCheckerPrivate *priv;
+       const gchar *language_code;
        const gchar *app_name;
 
        priv = gspell_checker_get_instance_private (checker);
 
-       g_return_val_if_fail (priv->broker != NULL, FALSE);
-
        if (priv->dict != NULL)
        {
-               return TRUE;
+               enchant_broker_free_dict (priv->broker, priv->dict);
+               priv->dict = NULL;
        }
 
        if (priv->active_lang == NULL)
        {
-               priv->active_lang = gspell_language_get_default ();
+               return;
        }
 
-       if (priv->active_lang != NULL)
-       {
-               const gchar *code;
-
-               code = gspell_language_get_code (priv->active_lang);
-
-               priv->dict = enchant_broker_request_dict (priv->broker, code);
-       }
+       language_code = gspell_language_get_code (priv->active_lang);
+       priv->dict = enchant_broker_request_dict (priv->broker, language_code);
 
        if (priv->dict == NULL)
        {
+               g_warning ("Impossible to create an Enchant dictionary for the language code '%s'.",
+                          language_code);
+
                priv->active_lang = NULL;
-               return FALSE;
+               return;
        }
 
        app_name = g_get_application_name ();
        gspell_checker_add_word_to_session (checker, app_name, -1);
-
-       return TRUE;
 }
 
 /**
@@ -319,46 +285,32 @@ init_dictionary (GspellChecker *checker)
  * @language: (nullable): the #GspellLanguage to use, or %NULL.
  *
  * Sets the language to use for the spell checking. If @language is %NULL, the
- * default language is picked with gspell_language_get_default().
- *
- * Returns: whether the operation was successful.
+ * spell checker will be disabled.
  */
-gboolean
+void
 gspell_checker_set_language (GspellChecker        *checker,
                             const GspellLanguage *language)
 {
        GspellCheckerPrivate *priv;
-       gboolean success;
 
-       g_return_val_if_fail (GSPELL_IS_CHECKER (checker), FALSE);
+       g_return_if_fail (GSPELL_IS_CHECKER (checker));
 
        priv = gspell_checker_get_instance_private (checker);
 
-       if (language != NULL && priv->active_lang == language)
+       if (priv->active_lang != language)
        {
-               return TRUE;
+               priv->active_lang = language;
+               create_new_dictionary (checker);
+               g_object_notify (G_OBJECT (checker), "language");
        }
-
-       if (priv->dict != NULL)
-       {
-               enchant_broker_free_dict (priv->broker, priv->dict);
-               priv->dict = NULL;
-       }
-
-       priv->active_lang = language;
-       success = init_dictionary (checker);
-
-       g_object_notify (G_OBJECT (checker), "language");
-
-       return success;
 }
 
 /**
  * gspell_checker_get_language:
  * @checker: a #GspellChecker.
  *
- * Returns: (nullable): the #GspellLanguage currently used, or %NULL
- * if no dictionaries are available.
+ * Returns: (nullable): the #GspellLanguage currently used, or %NULL if the
+ * spell checker is disabled.
  */
 const GspellLanguage *
 gspell_checker_get_language (GspellChecker *checker)
@@ -379,6 +331,8 @@ gspell_checker_get_language (GspellChecker *checker)
  * @word_length: the byte length of @word, or -1 if @word is nul-terminated.
  * @error: (out) (optional): a location to a %NULL #GError, or %NULL.
  *
+ * If the #GspellChecker:language is %NULL, this function returns %TRUE.
+ *
  * Returns: whether @word is correctly spelled.
  */
 gboolean
@@ -396,11 +350,14 @@ gspell_checker_check_word (GspellChecker  *checker,
        g_return_val_if_fail (word_length >= -1, FALSE);
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-       /* If no dictionaries are available, limit the damage by returning TRUE. */
-       g_return_val_if_fail (_gspell_checker_check_language_set (checker), TRUE);
-
        priv = gspell_checker_get_instance_private (checker);
 
+       if (priv->dict == NULL)
+       {
+               /* Limit the damage. */
+               return TRUE;
+       }
+
        if (_gspell_utils_is_digit (word, word_length))
        {
                return TRUE;
@@ -458,10 +415,14 @@ gspell_checker_get_suggestions (GspellChecker *checker,
        g_return_val_if_fail (GSPELL_IS_CHECKER (checker), NULL);
        g_return_val_if_fail (word != NULL, NULL);
        g_return_val_if_fail (word_length >= -1, NULL);
-       g_return_val_if_fail (_gspell_checker_check_language_set (checker), NULL);
 
        priv = gspell_checker_get_instance_private (checker);
 
+       if (priv->dict == NULL)
+       {
+               return NULL;
+       }
+
        suggestions = enchant_dict_suggest (priv->dict, word, word_length, NULL);
 
        if (suggestions == NULL)
@@ -474,7 +435,7 @@ gspell_checker_get_suggestions (GspellChecker *checker,
                suggestions_list = g_slist_prepend (suggestions_list, suggestions[i]);
        }
 
-       /* The single suggestions will be freed by the caller */
+       /* The array/list elements will be freed by the caller. */
        g_free (suggestions);
 
        return g_slist_reverse (suggestions_list);
@@ -499,10 +460,14 @@ gspell_checker_add_word_to_personal (GspellChecker *checker,
        g_return_if_fail (GSPELL_IS_CHECKER (checker));
        g_return_if_fail (word != NULL);
        g_return_if_fail (word_length >= -1);
-       g_return_if_fail (_gspell_checker_check_language_set (checker));
 
        priv = gspell_checker_get_instance_private (checker);
 
+       if (priv->dict == NULL)
+       {
+               return;
+       }
+
        enchant_dict_add (priv->dict, word, word_length);
 
        if (word_length == -1)
@@ -546,10 +511,14 @@ gspell_checker_add_word_to_session (GspellChecker *checker,
        g_return_if_fail (GSPELL_IS_CHECKER (checker));
        g_return_if_fail (word != NULL);
        g_return_if_fail (word_length >= -1);
-       g_return_if_fail (_gspell_checker_check_language_set (checker));
 
        priv = gspell_checker_get_instance_private (checker);
 
+       if (priv->dict == NULL)
+       {
+               return;
+       }
+
        enchant_dict_add_to_session (priv->dict, word, word_length);
 
        if (word_length == -1)
@@ -582,16 +551,11 @@ gspell_checker_clear_session (GspellChecker *checker)
        GspellCheckerPrivate *priv;
 
        g_return_if_fail (GSPELL_IS_CHECKER (checker));
-       g_return_if_fail (_gspell_checker_check_language_set (checker));
 
        priv = gspell_checker_get_instance_private (checker);
 
-       /* free and re-request dictionary */
-       g_assert (priv->dict != NULL);
-       enchant_broker_free_dict (priv->broker, priv->dict);
-       priv->dict = NULL;
-
-       init_dictionary (checker);
+       /* Free and re-request dictionary. */
+       create_new_dictionary (checker);
 
        g_signal_emit (G_OBJECT (checker), signals[SIGNAL_SESSION_CLEARED], 0);
 }
@@ -621,10 +585,14 @@ gspell_checker_set_correction (GspellChecker *checker,
        g_return_if_fail (word_length >= -1);
        g_return_if_fail (replacement != NULL);
        g_return_if_fail (replacement_length >= -1);
-       g_return_if_fail (_gspell_checker_check_language_set (checker));
 
        priv = gspell_checker_get_instance_private (checker);
 
+       if (priv->dict == NULL)
+       {
+               return;
+       }
+
        enchant_dict_store_replacement (priv->dict,
                                        word, word_length,
                                        replacement, replacement_length);
diff --git a/gspell/gspell-checker.h b/gspell/gspell-checker.h
index e993128..65b59db 100644
--- a/gspell/gspell-checker.h
+++ b/gspell/gspell-checker.h
@@ -110,10 +110,6 @@ void               gspell_checker_set_correction           (GspellChecker *checker,
                                                         const gchar   *replacement,
                                                         gssize         replacement_length);
 
-/* Non-exported function */
-G_GNUC_INTERNAL
-gboolean       _gspell_checker_check_language_set      (GspellChecker *checker);
-
 G_END_DECLS
 
 #endif  /* __GSPELL_CHECKER_H__ */
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index 77c8a72..d990e53 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -753,8 +753,6 @@ language_notify_cb (GspellChecker                 *checker,
                    GParamSpec                    *pspec,
                    GspellInlineCheckerTextBuffer *spell)
 {
-       _gspell_checker_check_language_set (checker);
-
        recheck_all (spell);
 }
 
@@ -876,8 +874,6 @@ set_spell_checker (GspellInlineCheckerTextBuffer *spell,
        {
                g_object_ref (spell->spell_checker);
 
-               _gspell_checker_check_language_set (spell->spell_checker);
-
                g_signal_connect (spell->spell_checker,
                                  "word-added-to-session",
                                  G_CALLBACK (word_added_cb),


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