1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Richard Hult <richard@imendio.com>
29 #include <glib/gi18n.h>
35 #include <libempathy/empathy-debug.h>
37 #include "empathy-spell.h"
38 #include "empathy-conf.h"
40 #define DEBUG_DOMAIN "Spell"
44 /* Note: We could use aspell_reset_cache (NULL); periodically if we wanted
49 AspellConfig *spell_config;
50 AspellCanHaveError *spell_possible_err;
51 AspellSpeller *spell_checker;
54 #define ISO_CODES_DATADIR ISO_CODES_PREFIX "/share/xml/iso-codes"
55 #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
57 static GHashTable *iso_code_names = NULL;
58 static GList *languages = NULL;
59 static gboolean empathy_conf_notify_inited = FALSE;
62 spell_iso_codes_parse_start_tag (GMarkupParseContext *ctx,
63 const gchar *element_name,
64 const gchar **attr_names,
65 const gchar **attr_values,
69 const gchar *ccode_longB, *ccode_longT, *ccode;
70 const gchar *lang_name;
72 if (!g_str_equal (element_name, "iso_639_entry") ||
73 attr_names == NULL || attr_values == NULL) {
82 while (*attr_names && *attr_values) {
83 if (g_str_equal (*attr_names, "iso_639_1_code")) {
88 else if (g_str_equal (*attr_names, "iso_639_2B_code")) {
90 ccode_longB = *attr_values;
93 else if (g_str_equal (*attr_names, "iso_639_2T_code")) {
95 ccode_longT = *attr_values;
98 else if (g_str_equal (*attr_names, "name")) {
99 lang_name = *attr_values;
111 g_hash_table_insert (iso_code_names,
113 g_strdup (lang_name));
117 g_hash_table_insert (iso_code_names,
118 g_strdup (ccode_longB),
119 g_strdup (lang_name));
123 g_hash_table_insert (iso_code_names,
124 g_strdup (ccode_longT),
125 g_strdup (lang_name));
130 spell_iso_code_names_init (void)
136 iso_code_names = g_hash_table_new_full (g_str_hash, g_str_equal,
139 bindtextdomain ("iso_639", ISO_CODES_LOCALESDIR);
140 bind_textdomain_codeset ("iso_639", "UTF-8");
142 /* FIXME: We should read this in chunks and pass to the parser. */
143 if (g_file_get_contents (ISO_CODES_DATADIR "/iso_639.xml", &buf, &buf_len, &err)) {
144 GMarkupParseContext *ctx;
145 GMarkupParser parser = {
146 spell_iso_codes_parse_start_tag,
147 NULL, NULL, NULL, NULL
150 ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
151 if (!g_markup_parse_context_parse (ctx, buf, buf_len, &err)) {
152 g_warning ("Failed to parse '%s': %s",
153 ISO_CODES_DATADIR"/iso_639.xml",
158 g_markup_parse_context_free (ctx);
161 g_warning ("Failed to load '%s': %s",
162 ISO_CODES_DATADIR"/iso_639.xml", err->message);
168 spell_notify_languages_cb (EmpathyConf *conf,
174 empathy_debug (DEBUG_DOMAIN, "Resetting languages due to config change");
176 /* We just reset the languages list. */
177 for (l = languages; l; l = l->next) {
182 delete_aspell_config (lang->spell_config);
183 delete_aspell_speller (lang->spell_checker);
185 g_slice_free (SpellLanguage, lang);
188 g_list_free (languages);
193 spell_setup_languages (void)
197 if (!empathy_conf_notify_inited) {
198 empathy_conf_notify_add (empathy_conf_get (),
199 EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
200 spell_notify_languages_cb, NULL);
202 empathy_conf_notify_inited = TRUE;
209 if (empathy_conf_get_string (empathy_conf_get (),
210 EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
215 strv = g_strsplit (str, ",", -1);
218 while (strv && strv[i]) {
221 empathy_debug (DEBUG_DOMAIN, "Setting up language:'%s'", strv[i]);
223 lang = g_slice_new0 (SpellLanguage);
225 lang->spell_config = new_aspell_config();
227 aspell_config_replace (lang->spell_config, "encoding", "utf-8");
228 aspell_config_replace (lang->spell_config, "lang", strv[i++]);
230 lang->spell_possible_err = new_aspell_speller (lang->spell_config);
232 if (aspell_error_number (lang->spell_possible_err) == 0) {
233 lang->spell_checker = to_aspell_speller (lang->spell_possible_err);
234 languages = g_list_append (languages, lang);
236 delete_aspell_config (lang->spell_config);
237 g_slice_free (SpellLanguage, lang);
250 empathy_spell_get_language_name (const char *code)
254 g_return_val_if_fail (code != NULL, NULL);
256 if (!iso_code_names) {
257 spell_iso_code_names_init ();
260 name = g_hash_table_lookup (iso_code_names, code);
265 return dgettext ("iso_639", name);
269 empathy_spell_get_language_codes (void)
271 AspellConfig *config;
272 AspellDictInfoList *dlist;
273 AspellDictInfoEnumeration *dels;
274 const AspellDictInfo *entry;
277 config = new_aspell_config ();
278 dlist = get_aspell_dict_info_list (config);
279 dels = aspell_dict_info_list_elements (dlist);
281 while ((entry = aspell_dict_info_enumeration_next (dels)) != 0) {
282 if (g_list_find_custom (codes, entry->code, (GCompareFunc) strcmp)) {
286 codes = g_list_append (codes, g_strdup (entry->code));
289 delete_aspell_dict_info_enumeration (dels);
290 delete_aspell_config (config);
296 empathy_spell_free_language_codes (GList *codes)
298 g_list_foreach (codes, (GFunc) g_free, NULL);
303 empathy_spell_check (const gchar *word)
307 gboolean correct = FALSE;
313 g_return_val_if_fail (word != NULL, FALSE);
315 spell_setup_languages ();
318 empathy_debug (DEBUG_DOMAIN, "No languages to check against");
322 /* Ignore certain cases like numbers, etc. */
323 for (p = word, digit = TRUE; *p && digit; p = g_utf8_next_char (p)) {
324 c = g_utf8_get_char (p);
325 digit = g_unichar_isdigit (c);
329 /* We don't spell check digits. */
330 empathy_debug (DEBUG_DOMAIN, "Not spell checking word:'%s', it is all digits", word);
335 n_langs = g_list_length (languages);
336 for (l = languages; l; l = l->next) {
341 correct = aspell_speller_check (lang->spell_checker, word, len);
342 if (n_langs > 1 && correct) {
351 empathy_spell_get_suggestions (const gchar *word)
355 const AspellWordList *suggestions;
356 AspellStringEnumeration *elements;
360 g_return_val_if_fail (word != NULL, NULL);
362 spell_setup_languages ();
366 for (l1 = languages; l1; l1 = l1->next) {
371 suggestions = aspell_speller_suggest (lang->spell_checker,
374 elements = aspell_word_list_elements (suggestions);
376 while ((next = aspell_string_enumeration_next (elements))) {
377 l2 = g_list_append (l2, g_strdup (next));
380 delete_aspell_string_enumeration (elements);
387 empathy_spell_supported (void)
389 if (g_getenv ("EMPATHY_SPELL_DISABLED")) {
390 empathy_debug (DEBUG_DOMAIN, "EMPATHY_SPELL_DISABLE env variable defined");
397 #else /* not HAVE_ASPELL */
400 empathy_spell_supported (void)
406 empathy_spell_get_suggestions (const gchar *word)
408 empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get suggestions");
414 empathy_spell_check (const gchar *word)
416 empathy_debug (DEBUG_DOMAIN, "Support disabled, could not check spelling");
422 empathy_spell_get_language_name (const char *lang)
424 empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language name");
430 empathy_spell_get_language_codes (void)
432 empathy_debug (DEBUG_DOMAIN, "Support disabled, could not get language codes");
438 empathy_spell_free_language_codes (GList *codes)
442 #endif /* HAVE_ASPELL */
446 empathy_spell_free_suggestions (GList *suggestions)
448 g_list_foreach (suggestions, (GFunc) g_free, NULL);
449 g_list_free (suggestions);