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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
21 * Richard Hult <richard@imendio.com>
29 #include <glib/gi18n-lib.h>
35 #include "empathy-spell.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39 #include <libempathy/empathy-gsettings.h>
44 EnchantBroker
*config
;
48 #define ISO_CODES_DATADIR ISO_CODES_PREFIX "/share/xml/iso-codes"
49 #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
51 /* Language code (gchar *) -> language name (gchar *) */
52 static GHashTable
*iso_code_names
= NULL
;
53 /* Contains only _enabled_ languages
54 * Language code (gchar *) -> language (SpellLanguage *) */
55 static GHashTable
*languages
= NULL
;
58 spell_iso_codes_parse_start_tag (GMarkupParseContext
*ctx
,
59 const gchar
*element_name
,
60 const gchar
**attr_names
,
61 const gchar
**attr_values
,
65 const gchar
*ccode_longB
, *ccode_longT
, *ccode
;
66 const gchar
*lang_name
;
68 if (!g_str_equal (element_name
, "iso_639_entry") ||
69 attr_names
== NULL
|| attr_values
== NULL
) {
78 while (*attr_names
&& *attr_values
) {
79 if (g_str_equal (*attr_names
, "iso_639_1_code")) {
84 else if (g_str_equal (*attr_names
, "iso_639_2B_code")) {
86 ccode_longB
= *attr_values
;
89 else if (g_str_equal (*attr_names
, "iso_639_2T_code")) {
91 ccode_longT
= *attr_values
;
94 else if (g_str_equal (*attr_names
, "name")) {
95 lang_name
= *attr_values
;
107 g_hash_table_insert (iso_code_names
,
109 g_strdup (lang_name
));
113 g_hash_table_insert (iso_code_names
,
114 g_strdup (ccode_longB
),
115 g_strdup (lang_name
));
119 g_hash_table_insert (iso_code_names
,
120 g_strdup (ccode_longT
),
121 g_strdup (lang_name
));
126 spell_iso_code_names_init (void)
132 iso_code_names
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
135 bindtextdomain ("iso_639", ISO_CODES_LOCALESDIR
);
136 bind_textdomain_codeset ("iso_639", "UTF-8");
138 /* FIXME: We should read this in chunks and pass to the parser. */
139 if (g_file_get_contents (ISO_CODES_DATADIR
"/iso_639.xml", &buf
, &buf_len
, &err
)) {
140 GMarkupParseContext
*ctx
;
141 GMarkupParser parser
= {
142 spell_iso_codes_parse_start_tag
,
143 NULL
, NULL
, NULL
, NULL
146 ctx
= g_markup_parse_context_new (&parser
, 0, NULL
, NULL
);
147 if (!g_markup_parse_context_parse (ctx
, buf
, buf_len
, &err
)) {
148 g_warning ("Failed to parse '%s': %s",
149 ISO_CODES_DATADIR
"/iso_639.xml",
154 g_markup_parse_context_free (ctx
);
157 g_warning ("Failed to load '%s': %s",
158 ISO_CODES_DATADIR
"/iso_639.xml", err
->message
);
164 spell_notify_languages_cb (GSettings
*gsettings
,
168 DEBUG ("Resetting languages due to config change");
170 /* We just reset the languages list. */
171 if (languages
!= NULL
) {
172 g_hash_table_destroy (languages
);
178 empathy_spell_free_language (SpellLanguage
*lang
)
180 enchant_broker_free_dict (lang
->config
, lang
->speller
);
181 enchant_broker_free (lang
->config
);
183 g_slice_free (SpellLanguage
, lang
);
187 spell_setup_languages (void)
189 static GSettings
*gsettings
= NULL
;
192 if (gsettings
== NULL
) {
193 /* FIXME: this is never uninitialised */
194 gsettings
= g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA
);
196 g_signal_connect (gsettings
,
197 "changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES
,
198 G_CALLBACK (spell_notify_languages_cb
), NULL
);
205 languages
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
206 g_free
, (GDestroyNotify
) empathy_spell_free_language
);
208 str
= g_settings_get_string (gsettings
,
209 EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES
);
215 strv
= g_strsplit (str
, ",", -1);
218 while (strv
&& strv
[i
]) {
221 DEBUG ("Setting up language:'%s'", strv
[i
]);
223 lang
= g_slice_new0 (SpellLanguage
);
225 lang
->config
= enchant_broker_init ();
226 lang
->speller
= enchant_broker_request_dict (lang
->config
, strv
[i
]);
228 if (lang
->speller
== NULL
) {
229 DEBUG ("language '%s' has no valid dict", strv
[i
]);
231 g_hash_table_insert (languages
,
248 empathy_spell_get_language_name (const gchar
*code
)
252 g_return_val_if_fail (code
!= NULL
, NULL
);
254 if (!iso_code_names
) {
255 spell_iso_code_names_init ();
258 name
= g_hash_table_lookup (iso_code_names
, code
);
263 return dgettext ("iso_639", name
);
267 enumerate_dicts (const gchar
* const lang_tag
,
268 const gchar
* const provider_name
,
269 const gchar
* const provider_desc
,
270 const gchar
* const provider_file
,
273 GList
**list
= user_data
;
274 gchar
*lang
= g_strdup (lang_tag
);
276 if (strchr (lang
, '_')) {
277 /* cut country part out of language */
278 strchr (lang
, '_')[0] = '\0';
281 if (g_list_find_custom (*list
, lang
, (GCompareFunc
) strcmp
)) {
282 /* this language is already part of the list */
287 *list
= g_list_append (*list
, g_strdup (lang
));
291 empathy_spell_get_language_codes (void)
293 EnchantBroker
*broker
;
294 GList
*list_langs
= NULL
;
296 broker
= enchant_broker_init ();
297 enchant_broker_list_dicts (broker
, enumerate_dicts
, &list_langs
);
298 enchant_broker_free (broker
);
304 empathy_spell_get_enabled_language_codes (void)
306 spell_setup_languages ();
307 return g_hash_table_get_keys (languages
);
311 empathy_spell_free_language_codes (GList
*codes
)
313 g_list_foreach (codes
, (GFunc
) g_free
, NULL
);
318 empathy_spell_check (const gchar
*word
)
320 gint enchant_result
= 1;
328 g_return_val_if_fail (word
!= NULL
, FALSE
);
330 spell_setup_languages ();
336 /* Ignore certain cases like numbers, etc. */
337 for (p
= word
, digit
= TRUE
; *p
&& digit
; p
= g_utf8_next_char (p
)) {
338 c
= g_utf8_get_char (p
);
339 digit
= g_unichar_isdigit (c
);
343 /* We don't spell check digits. */
344 DEBUG ("Not spell checking word:'%s', it is all digits", word
);
349 g_hash_table_iter_init (&iter
, languages
);
350 while (g_hash_table_iter_next (&iter
, NULL
, (gpointer
*) &lang
)) {
351 enchant_result
= enchant_dict_check (lang
->speller
, word
, len
);
353 if (enchant_result
== 0) {
358 return (enchant_result
== 0);
362 empathy_spell_get_suggestions (const gchar
*code
,
366 GList
*suggestion_list
= NULL
;
369 gsize i
, number_of_suggestions
;
371 g_return_val_if_fail (code
!= NULL
, NULL
);
372 g_return_val_if_fail (word
!= NULL
, NULL
);
374 spell_setup_languages ();
382 lang
= g_hash_table_lookup (languages
, code
);
387 suggestions
= enchant_dict_suggest (lang
->speller
, word
, len
,
388 &number_of_suggestions
);
390 for (i
= 0; i
< number_of_suggestions
; i
++) {
391 suggestion_list
= g_list_append (suggestion_list
,
392 g_strdup (suggestions
[i
]));
396 enchant_dict_free_string_list (lang
->speller
, suggestions
);
399 return suggestion_list
;
403 empathy_spell_supported (void)
405 if (g_getenv ("EMPATHY_SPELL_DISABLED")) {
406 DEBUG ("EMPATHY_SPELL_DISABLE env variable defined");
414 empathy_spell_add_to_dictionary (const gchar
*code
,
419 g_return_if_fail (code
!= NULL
);
420 g_return_if_fail (word
!= NULL
);
422 spell_setup_languages ();
423 if (languages
== NULL
)
426 lang
= g_hash_table_lookup (languages
, code
);
430 enchant_dict_add_to_pwl (lang
->speller
, word
, strlen (word
));
433 #else /* not HAVE_ENCHANT */
436 empathy_spell_supported (void)
442 empathy_spell_get_suggestions (const gchar
*code
,
445 DEBUG ("Support disabled, could not get suggestions");
451 empathy_spell_check (const gchar
*word
)
453 DEBUG ("Support disabled, could not check spelling");
459 empathy_spell_get_language_name (const gchar
*lang
)
461 DEBUG ("Support disabled, could not get language name");
467 empathy_spell_get_language_codes (void)
469 DEBUG ("Support disabled, could not get language codes");
475 empathy_spell_free_language_codes (GList
*codes
)
480 empathy_spell_add_to_dictionary (const gchar
*code
,
483 DEBUG ("Support disabled, could not expand the dictionary");
487 empathy_spell_get_enabled_language_codes (void)
489 DEBUG ("Support disabled, could not get enabled language codes");
494 #endif /* HAVE_ENCHANT */
498 empathy_spell_free_suggestions (GList
*suggestions
)
500 g_list_foreach (suggestions
, (GFunc
) g_free
, NULL
);
501 g_list_free (suggestions
);