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