]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-spell.c
Merge commit 'staz/dnd'
[empathy.git] / libempathy-gtk / empathy-spell.c
index db32332943b5b2211b5f27f377d17030984d8974..17c7f100e303bff989e9c1e7338b22665107e00c 100644 (file)
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU General Public
  * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Martyn Russell <martyn@imendio.com>
  *          Richard Hult <richard@imendio.com>
 #include <string.h>
 #include <stdlib.h>
 
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
 
-#ifdef HAVE_ASPELL
-#include <aspell.h>
+#ifdef HAVE_ENCHANT
+#include <enchant.h>
 #endif
 
-#include <libempathy/empathy-debug.h>
-
 #include "empathy-spell.h"
 #include "empathy-conf.h"
 
-#define DEBUG_DOMAIN "Spell"
-
-#ifdef HAVE_ASPELL
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
 
-/* Note: We could use aspell_reset_cache (NULL); periodically if we wanted
- * to...
- */
+#ifdef HAVE_ENCHANT
 
 typedef struct {
-       AspellConfig       *spell_config;
-       AspellCanHaveError *spell_possible_err;
-       AspellSpeller      *spell_checker;
+       EnchantBroker *config;
+       EnchantDict   *speller;
 } SpellLanguage;
 
 #define ISO_CODES_DATADIR    ISO_CODES_PREFIX "/share/xml/iso-codes"
@@ -171,7 +165,7 @@ spell_notify_languages_cb (EmpathyConf  *conf,
 {
        GList *l;
 
-       empathy_debug (DEBUG_DOMAIN, "Resetting languages due to config change");
+       DEBUG ("Resetting languages due to config change");
 
        /* We just reset the languages list. */
        for (l = languages; l; l = l->next) {
@@ -179,8 +173,8 @@ spell_notify_languages_cb (EmpathyConf  *conf,
 
                lang = l->data;
 
-               delete_aspell_config (lang->spell_config);
-               delete_aspell_speller (lang->spell_checker);
+               enchant_broker_free_dict (lang->config, lang->speller);
+               enchant_broker_free (lang->config);
 
                g_slice_free (SpellLanguage, lang);
        }
@@ -218,24 +212,20 @@ spell_setup_languages (void)
                while (strv && strv[i]) {
                        SpellLanguage *lang;
 
-                       empathy_debug (DEBUG_DOMAIN, "Setting up language:'%s'", strv[i]);
+                       DEBUG ("Setting up language:'%s'", strv[i]);
 
                        lang = g_slice_new0 (SpellLanguage);
 
-                       lang->spell_config = new_aspell_config();
+                       lang->config = enchant_broker_init ();
+                       lang->speller = enchant_broker_request_dict (lang->config, strv[i]);
 
-                       aspell_config_replace (lang->spell_config, "encoding", "utf-8");
-                       aspell_config_replace (lang->spell_config, "lang", strv[i++]);
-
-                       lang->spell_possible_err = new_aspell_speller (lang->spell_config);
-
-                       if (aspell_error_number (lang->spell_possible_err) == 0) {
-                               lang->spell_checker = to_aspell_speller (lang->spell_possible_err);
-                               languages = g_list_append (languages, lang);
+                       if (lang->speller == NULL) {
+                               DEBUG ("language '%s' has no valid dict", strv[i]);
                        } else {
-                               delete_aspell_config (lang->spell_config);
-                               g_slice_free (SpellLanguage, lang);
+                               languages = g_list_append (languages, lang);
                        }
+
+                       i++;
                }
 
                if (strv) {
@@ -246,8 +236,8 @@ spell_setup_languages (void)
        }
 }
 
-const char *
-empathy_spell_get_language_name (const char *code)
+const gchar *
+empathy_spell_get_language_name (const gchar *code)
 {
        const gchar *name;
 
@@ -265,31 +255,41 @@ empathy_spell_get_language_name (const char *code)
        return dgettext ("iso_639", name);
 }
 
-GList *
-empathy_spell_get_language_codes (void)
+static void
+enumerate_dicts (const gchar * const lang_tag,
+                 const gchar * const provider_name,
+                 const gchar * const provider_desc,
+                 const gchar * const provider_file,
+                gpointer            user_data)
 {
-       AspellConfig              *config;
-       AspellDictInfoList        *dlist;
-       AspellDictInfoEnumeration *dels;
-       const AspellDictInfo      *entry;
-       GList                     *codes = NULL;
-
-       config = new_aspell_config ();
-       dlist = get_aspell_dict_info_list (config);
-       dels = aspell_dict_info_list_elements (dlist);
-
-       while ((entry = aspell_dict_info_enumeration_next (dels)) != 0) {
-               if (g_list_find_custom (codes, entry->code, (GCompareFunc) strcmp)) {
-                       continue;
-               }
+       GList **list = user_data;
+       gchar  *lang = g_strdup (lang_tag);
 
-               codes = g_list_append (codes, g_strdup (entry->code));
+       if (strchr (lang, '_')) {
+               /* cut country part out of language */
+               strchr (lang, '_')[0] = '\0';
        }
 
-       delete_aspell_dict_info_enumeration (dels);
-       delete_aspell_config (config);
+       if (g_list_find_custom (*list, lang, (GCompareFunc) strcmp)) {
+               /* this language is already part of the list */
+               g_free (lang);
+               return;
+       }
 
-       return codes;
+       *list = g_list_append (*list, g_strdup (lang));
+}
+
+GList *
+empathy_spell_get_language_codes (void)
+{
+       EnchantBroker *broker;
+       GList         *list_langs = NULL;
+
+       broker = enchant_broker_init ();
+       enchant_broker_list_dicts (broker, enumerate_dicts, &list_langs);
+       enchant_broker_free (broker);
+
+       return list_langs;
 }
 
 void
@@ -302,20 +302,18 @@ empathy_spell_free_language_codes (GList *codes)
 gboolean
 empathy_spell_check (const gchar *word)
 {
-       GList       *l;
-       gint         n_langs;
-       gboolean     correct = FALSE;
-       gint         len;
+       gint         enchant_result = 1;
        const gchar *p;
-       gunichar     c;
        gboolean     digit;
+       gunichar     c;
+       gint         len;
+       GList       *l;
 
        g_return_val_if_fail (word != NULL, FALSE);
 
        spell_setup_languages ();
 
        if (!languages) {
-               empathy_debug (DEBUG_DOMAIN, "No languages to check against");
                return TRUE;
        }
 
@@ -327,35 +325,32 @@ empathy_spell_check (const gchar *word)
 
        if (digit) {
                /* We don't spell check digits. */
-               empathy_debug (DEBUG_DOMAIN, "Not spell checking word:'%s', it is all digits", word);
+               DEBUG ("Not spell checking word:'%s', it is all digits", word);
                return TRUE;
        }
 
        len = strlen (word);
-       n_langs = g_list_length (languages);
        for (l = languages; l; l = l->next) {
-               SpellLanguage *lang;
+               SpellLanguage  *lang;
 
                lang = l->data;
 
-               correct = aspell_speller_check (lang->spell_checker, word, len);
-               if (n_langs > 1 && correct) {
+               enchant_result = enchant_dict_check (lang->speller, word, len);
+
+               if (enchant_result == 0) {
                        break;
                }
        }
 
-       return correct;
+       return (enchant_result == 0);
 }
 
 GList *
 empathy_spell_get_suggestions (const gchar *word)
 {
-       GList                   *l1;
-       GList                   *l2 = NULL;
-       const AspellWordList    *suggestions;
-       AspellStringEnumeration *elements;
-       const char              *next;
-       gint                     len;
+       gint   len;
+       GList *l1;
+       GList *suggestion_list = NULL;
 
        g_return_val_if_fail (word != NULL, NULL);
 
@@ -365,36 +360,39 @@ empathy_spell_get_suggestions (const gchar *word)
 
        for (l1 = languages; l1; l1 = l1->next) {
                SpellLanguage *lang;
+               gchar **suggestions;
+               gsize   i, number_of_suggestions;
 
                lang = l1->data;
 
-               suggestions = aspell_speller_suggest (lang->spell_checker,
-                                                     word, len);
+               suggestions = enchant_dict_suggest (lang->speller, word, len,
+                                                   &number_of_suggestions);
 
-               elements = aspell_word_list_elements (suggestions);
-
-               while ((next = aspell_string_enumeration_next (elements))) {
-                       l2 = g_list_append (l2, g_strdup (next));
+               for (i = 0; i < number_of_suggestions; i++) {
+                       suggestion_list = g_list_append (suggestion_list,
+                                                        g_strdup (suggestions[i]));
                }
 
-               delete_aspell_string_enumeration (elements);
+               if (suggestions) {
+                       enchant_dict_free_string_list (lang->speller, suggestions);
+               }
        }
 
-       return l2;
+       return suggestion_list;
 }
 
 gboolean
 empathy_spell_supported (void)
 {
        if (g_getenv ("EMPATHY_SPELL_DISABLED")) {
-               empathy_debug (DEBUG_DOMAIN, "EMPATHY_SPELL_DISABLE env variable defined");
+               DEBUG ("EMPATHY_SPELL_DISABLE env variable defined");
                return FALSE;
        }
 
        return TRUE;
 }
 
-#else /* not HAVE_ASPELL */
+#else /* not HAVE_ENCHANT */
 
 gboolean
 empathy_spell_supported (void)
@@ -405,7 +403,7 @@ empathy_spell_supported (void)
 GList *
 empathy_spell_get_suggestions (const gchar *word)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get suggestions");
+       DEBUG ("Support disabled, could not get suggestions");
 
        return NULL;
 }
@@ -413,15 +411,15 @@ empathy_spell_get_suggestions (const gchar *word)
 gboolean
 empathy_spell_check (const gchar *word)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not check spelling");
+       DEBUG ("Support disabled, could not check spelling");
 
        return TRUE;
 }
 
-const char *
-empathy_spell_get_language_name (const char *lang)
+const gchar *
+empathy_spell_get_language_name (const gchar *lang)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language name");
+       DEBUG ("Support disabled, could not get language name");
 
        return NULL;
 }
@@ -429,7 +427,7 @@ empathy_spell_get_language_name (const char *lang)
 GList *
 empathy_spell_get_language_codes (void)
 {
-       empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language codes");
+       DEBUG ("Support disabled, could not get language codes");
 
        return NULL;
 }
@@ -439,7 +437,7 @@ empathy_spell_free_language_codes (GList *codes)
 {
 }
 
-#endif /* HAVE_ASPELL */
+#endif /* HAVE_ENCHANT */
 
 
 void