]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-spell.c
Merge branch 'debugger'
[empathy.git] / libempathy-gtk / empathy-spell.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  *
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.
9  *
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.
14  *
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
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  *          Richard Hult <richard@imendio.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include <glib/gi18n-lib.h>
30
31 #ifdef HAVE_ENCHANT
32 #include <enchant.h>
33 #endif
34
35 #include "empathy-spell.h"
36 #include "empathy-conf.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
40
41 #ifdef HAVE_ENCHANT
42
43 typedef struct {
44         EnchantBroker *config;
45         EnchantDict   *speller;
46 } SpellLanguage;
47
48 #define ISO_CODES_DATADIR    ISO_CODES_PREFIX "/share/xml/iso-codes"
49 #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
50
51 static GHashTable  *iso_code_names = NULL;
52 static GList       *languages = NULL;
53 static gboolean     empathy_conf_notify_inited = FALSE;
54
55 static void
56 spell_iso_codes_parse_start_tag (GMarkupParseContext  *ctx,
57                                  const gchar          *element_name,
58                                  const gchar         **attr_names,
59                                  const gchar         **attr_values,
60                                  gpointer              data,
61                                  GError              **error)
62 {
63         const gchar *ccode_longB, *ccode_longT, *ccode;
64         const gchar *lang_name;
65
66         if (!g_str_equal (element_name, "iso_639_entry") ||
67             attr_names == NULL || attr_values == NULL) {
68                 return;
69         }
70
71         ccode = NULL;
72         ccode_longB = NULL;
73         ccode_longT = NULL;
74         lang_name = NULL;
75
76         while (*attr_names && *attr_values) {
77                 if (g_str_equal (*attr_names, "iso_639_1_code")) {
78                         if (**attr_values) {
79                                 ccode = *attr_values;
80                         }
81                 }
82                 else if (g_str_equal (*attr_names, "iso_639_2B_code")) {
83                         if (**attr_values) {
84                                 ccode_longB = *attr_values;
85                         }
86                 }
87                 else if (g_str_equal (*attr_names, "iso_639_2T_code")) {
88                         if (**attr_values) {
89                                 ccode_longT = *attr_values;
90                         }
91                 }
92                 else if (g_str_equal (*attr_names, "name")) {
93                         lang_name = *attr_values;
94                 }
95
96                 attr_names++;
97                 attr_values++;
98         }
99
100         if (!lang_name) {
101                 return;
102         }
103
104         if (ccode) {
105                 g_hash_table_insert (iso_code_names,
106                                      g_strdup (ccode),
107                                      g_strdup (lang_name));
108         }
109
110         if (ccode_longB) {
111                 g_hash_table_insert (iso_code_names,
112                                      g_strdup (ccode_longB),
113                                      g_strdup (lang_name));
114         }
115
116         if (ccode_longT) {
117                 g_hash_table_insert (iso_code_names,
118                                      g_strdup (ccode_longT),
119                                      g_strdup (lang_name));
120         }
121 }
122
123 static void
124 spell_iso_code_names_init (void)
125 {
126         GError *err = NULL;
127         gchar  *buf;
128         gsize   buf_len;
129
130         iso_code_names = g_hash_table_new_full (g_str_hash, g_str_equal,
131                                                 g_free, g_free);
132
133         bindtextdomain ("iso_639", ISO_CODES_LOCALESDIR);
134         bind_textdomain_codeset ("iso_639", "UTF-8");
135
136         /* FIXME: We should read this in chunks and pass to the parser. */
137         if (g_file_get_contents (ISO_CODES_DATADIR "/iso_639.xml", &buf, &buf_len, &err)) {
138                 GMarkupParseContext *ctx;
139                 GMarkupParser        parser = {
140                         spell_iso_codes_parse_start_tag,
141                         NULL, NULL, NULL, NULL
142                 };
143
144                 ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
145                 if (!g_markup_parse_context_parse (ctx, buf, buf_len, &err)) {
146                         g_warning ("Failed to parse '%s': %s",
147                                    ISO_CODES_DATADIR"/iso_639.xml",
148                                    err->message);
149                         g_error_free (err);
150                 }
151
152                 g_markup_parse_context_free (ctx);
153                 g_free (buf);
154         } else {
155                 g_warning ("Failed to load '%s': %s",
156                                 ISO_CODES_DATADIR"/iso_639.xml", err->message);
157                 g_error_free (err);
158         }
159 }
160
161 static void
162 spell_notify_languages_cb (EmpathyConf  *conf,
163                            const gchar *key,
164                            gpointer     user_data)
165 {
166         GList *l;
167
168         DEBUG ("Resetting languages due to config change");
169
170         /* We just reset the languages list. */
171         for (l = languages; l; l = l->next) {
172                 SpellLanguage *lang;
173
174                 lang = l->data;
175
176                 enchant_broker_free_dict (lang->config, lang->speller);
177                 enchant_broker_free (lang->config);
178
179                 g_slice_free (SpellLanguage, lang);
180         }
181
182         g_list_free (languages);
183         languages = NULL;
184 }
185
186 static void
187 spell_setup_languages (void)
188 {
189         gchar  *str;
190
191         if (!empathy_conf_notify_inited) {
192                 empathy_conf_notify_add (empathy_conf_get (),
193                                          EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
194                                          spell_notify_languages_cb, NULL);
195
196                 empathy_conf_notify_inited = TRUE;
197         }
198
199         if (languages) {
200                 return;
201         }
202
203         if (empathy_conf_get_string (empathy_conf_get (),
204                                      EMPATHY_PREFS_CHAT_SPELL_CHECKER_LANGUAGES,
205                                      &str) && str) {
206                 gchar **strv;
207                 gint    i;
208
209                 strv = g_strsplit (str, ",", -1);
210
211                 i = 0;
212                 while (strv && strv[i]) {
213                         SpellLanguage *lang;
214
215                         DEBUG ("Setting up language:'%s'", strv[i]);
216
217                         lang = g_slice_new0 (SpellLanguage);
218
219                         lang->config = enchant_broker_init ();
220                         lang->speller = enchant_broker_request_dict (lang->config, strv[i]);
221
222                         languages = g_list_append (languages, lang);
223                         i++;
224                 }
225
226                 if (strv) {
227                         g_strfreev (strv);
228                 }
229
230                 g_free (str);
231         }
232 }
233
234 const gchar *
235 empathy_spell_get_language_name (const gchar *code)
236 {
237         const gchar *name;
238
239         g_return_val_if_fail (code != NULL, NULL);
240
241         if (!iso_code_names) {
242                 spell_iso_code_names_init ();
243         }
244
245         name = g_hash_table_lookup (iso_code_names, code);
246         if (!name) {
247                 return NULL;
248         }
249
250         return dgettext ("iso_639", name);
251 }
252
253 static void
254 enumerate_dicts (const gchar * const lang_tag,
255                  const gchar * const provider_name,
256                  const gchar * const provider_desc,
257                  const gchar * const provider_file,
258                  gpointer            user_data)
259 {
260         GList **list = user_data;
261         gchar  *lang = g_strdup (lang_tag);
262
263         if (strchr (lang, '_')) {
264                 /* cut country part out of language */
265                 strchr (lang, '_')[0] = '\0';
266         }
267
268         if (g_list_find_custom (*list, lang, (GCompareFunc) strcmp)) {
269                 /* this language is already part of the list */
270                 g_free (lang);
271                 return;
272         }
273
274         *list = g_list_append (*list, g_strdup (lang));
275 }
276
277 GList *
278 empathy_spell_get_language_codes (void)
279 {
280         EnchantBroker *broker;
281         GList         *list_langs = NULL;
282
283         broker = enchant_broker_init ();
284         enchant_broker_list_dicts (broker, enumerate_dicts, &list_langs);
285         enchant_broker_free (broker);
286
287         return list_langs;
288 }
289
290 void
291 empathy_spell_free_language_codes (GList *codes)
292 {
293         g_list_foreach (codes, (GFunc) g_free, NULL);
294         g_list_free (codes);
295 }
296
297 gboolean
298 empathy_spell_check (const gchar *word)
299 {
300         gint         enchant_result = 1;
301         const gchar *p;
302         gboolean     digit;
303         gunichar     c;
304         gint         len;
305         GList       *l;
306
307         g_return_val_if_fail (word != NULL, FALSE);
308
309         spell_setup_languages ();
310
311         if (!languages) {
312                 DEBUG ("No languages to check against");
313                 return TRUE;
314         }
315
316         /* Ignore certain cases like numbers, etc. */
317         for (p = word, digit = TRUE; *p && digit; p = g_utf8_next_char (p)) {
318                 c = g_utf8_get_char (p);
319                 digit = g_unichar_isdigit (c);
320         }
321
322         if (digit) {
323                 /* We don't spell check digits. */
324                 DEBUG ("Not spell checking word:'%s', it is all digits", word);
325                 return TRUE;
326         }
327
328         len = strlen (word);
329         for (l = languages; l; l = l->next) {
330                 SpellLanguage  *lang;
331
332                 lang = l->data;
333
334                 enchant_result = enchant_dict_check (lang->speller, word, len);
335
336                 if (enchant_result == 0) {
337                         break;
338                 }
339         }
340
341         return (enchant_result == 0);
342 }
343
344 GList *
345 empathy_spell_get_suggestions (const gchar *word)
346 {
347         gint   len;
348         GList *l1;
349         GList *suggestion_list = NULL;
350
351         g_return_val_if_fail (word != NULL, NULL);
352
353         spell_setup_languages ();
354
355         len = strlen (word);
356
357         for (l1 = languages; l1; l1 = l1->next) {
358                 SpellLanguage *lang;
359                 gchar **suggestions;
360                 gsize   i, number_of_suggestions;
361
362                 lang = l1->data;
363
364                 suggestions = enchant_dict_suggest (lang->speller, word, len,
365                                                     &number_of_suggestions);
366
367                 for (i = 0; i < number_of_suggestions; i++) {
368                         suggestion_list = g_list_append (suggestion_list,
369                                                          g_strdup (suggestions[i]));
370                 }
371
372                 if (suggestions) {
373                         enchant_dict_free_string_list (lang->speller, suggestions);
374                 }
375         }
376
377         return suggestion_list;
378 }
379
380 gboolean
381 empathy_spell_supported (void)
382 {
383         if (g_getenv ("EMPATHY_SPELL_DISABLED")) {
384                 DEBUG ("EMPATHY_SPELL_DISABLE env variable defined");
385                 return FALSE;
386         }
387
388         return TRUE;
389 }
390
391 #else /* not HAVE_ENCHANT */
392
393 gboolean
394 empathy_spell_supported (void)
395 {
396         return FALSE;
397 }
398
399 GList *
400 empathy_spell_get_suggestions (const gchar *word)
401 {
402         DEBUG ("Support disabled, could not get suggestions");
403
404         return NULL;
405 }
406
407 gboolean
408 empathy_spell_check (const gchar *word)
409 {
410         DEBUG ("Support disabled, could not check spelling");
411
412         return TRUE;
413 }
414
415 const gchar *
416 empathy_spell_get_language_name (const gchar *lang)
417 {
418         DEBUG ("Support disabled, could not get language name");
419
420         return NULL;
421 }
422
423 GList *
424 empathy_spell_get_language_codes (void)
425 {
426         DEBUG ("Support disabled, could not get language codes");
427
428         return NULL;
429 }
430
431 void
432 empathy_spell_free_language_codes (GList *codes)
433 {
434 }
435
436 #endif /* HAVE_ENCHANT */
437
438
439 void
440 empathy_spell_free_suggestions (GList *suggestions)
441 {
442         g_list_foreach (suggestions, (GFunc) g_free, NULL);
443         g_list_free (suggestions);
444 }
445