Tizen 2.1 base
[external/enchant.git] / src / uspell / uspell_provider.cpp
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* enchant
3  * Copyright (C) 2003 Dom Lachowicz, Raphael Finkel
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, Dom Lachowicz
21  * gives permission to link the code of this program with
22  * non-LGPL Spelling Provider libraries (eg: a MSFT Office
23  * spell checker backend) and distribute linked combinations including
24  * the two.  You must obey the GNU Lesser General Public License in all
25  * respects for all of the code used other than said providers.  If you modify
26  * this file, you may extend this exception to your version of the
27  * file, but you are not obligated to do so.  If you do not wish to
28  * do so, delete this exception statement from your version.
29  */
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <string>
37 #include <vector>
38
39 #include <glib.h>
40
41 #include "enchant.h"
42 #include "enchant-provider.h"
43
44 #include <uspell/utf8convert.h>
45 #include <uspell/uniprops.h>
46 #include <uspell/uspell.h>
47
48 ENCHANT_PLUGIN_DECLARE("Uspell")
49
50 static const size_t MAXALTERNATIVE = 20; // we won't return more than this number of suggestions
51 static const size_t MAXCHARS = 100; // maximum number of bytes of utf8 or chars of UCS4 in a word
52
53 static GSList *
54 uspell_checker_get_dictionary_dirs (EnchantBroker * broker)
55 {
56         GSList *dirs = NULL;
57
58         {
59                 GSList *config_dirs, *iter;
60
61                 config_dirs = enchant_get_user_config_dirs ();
62                 
63                 for (iter = config_dirs; iter; iter = iter->next)
64                         {
65                                 dirs = g_slist_append (dirs, g_build_filename ((const gchar *)iter->data, 
66                                                                                "uspell", NULL));
67                         }
68
69                 g_slist_foreach (config_dirs, (GFunc)g_free, NULL);
70                 g_slist_free (config_dirs);
71         }
72
73         {
74                 const gchar* const * system_data_dirs = g_get_system_data_dirs ();
75                 const gchar* const * iter;
76
77                 for (iter = system_data_dirs; *iter; iter++)
78                         {
79                                 dirs = g_slist_append (dirs, g_build_filename (*iter, "uspell", "dicts", NULL));
80                         }
81         }
82
83         /* until I work out how to link the modules against enchant in MacOSX - fjf
84          */
85 #ifndef XP_TARGET_COCOA
86         char * uspell_prefix = NULL;
87
88         /* Look for explicitly set registry values */
89         uspell_prefix = enchant_get_registry_value ("Uspell", "Data_Dir");
90         if (uspell_prefix)
91                 dirs = g_slist_append (dirs, uspell_prefix);
92
93         /* Dynamically locate library and search for modules relative to it. */
94         char * enchant_prefix = enchant_get_prefix_dir();
95         if(enchant_prefix)
96                 {
97                         uspell_prefix = g_build_filename(enchant_prefix, "share", "enchant", "uspell", NULL);
98                         g_free(enchant_prefix);
99                         dirs = g_slist_append (dirs, uspell_prefix);
100                 }
101 #endif
102
103 #ifdef ENCHANT_USPELL_DICT_DIR
104         dirs = g_slist_append (dirs, g_strdup (ENCHANT_USPELL_DICT_DIR));
105 #endif
106
107         {
108                 GSList *config_dirs, *iter;
109
110                 config_dirs = enchant_get_dirs_from_param (broker, "enchant.uspell.dictionary.path");
111                 
112                 for (iter = config_dirs; iter; iter = iter->next)
113                         {
114                                 dirs = g_slist_append (dirs, g_strdup ((const gchar *)iter->data));
115                         }
116
117                 g_slist_foreach (config_dirs, (GFunc)g_free, NULL);
118                 g_slist_free (config_dirs);
119         }
120
121         return dirs;
122 }
123
124 static int
125 uspell_dict_check (EnchantDict * me, const char *const word, size_t len)
126 {
127         uSpell *manager;
128         wide_t buf1[MAXCHARS], buf2[MAXCHARS], *curBuf, *otherBuf, *tmpBuf;
129         utf8_t myWord[MAXCHARS];
130         int length;
131         
132         if (len >= MAXCHARS)
133                 return 1; // too long; can't be right
134         memcpy(reinterpret_cast<char *>(myWord), word, len);
135         myWord[len] = 0;
136         curBuf = buf1;
137         otherBuf = buf2;
138         manager = reinterpret_cast<uSpell *>(me->user_data);
139
140         length = utf8_wide(curBuf, myWord, MAXCHARS);
141         if (manager->isSpelledRight(curBuf, length)) {
142                 return 0; // correct the first time
143         }
144         if (manager->theFlags & uSpell::upperLower) {
145                 toUpper(otherBuf, curBuf, length);
146                 if (manager->isSpelledRight(otherBuf, length)) {
147                         manager->acceptWord(myWord);
148                         return 0; // correct if converted to all upper case
149                 }
150                 tmpBuf = curBuf;
151                 curBuf = otherBuf;
152                 otherBuf = tmpBuf;
153         }
154         if (manager->theFlags & uSpell::hasComposition) {
155                 unPrecompose(otherBuf, &length, curBuf, length);
156                 if (manager->isSpelledRight(otherBuf, length)) {
157                         manager->acceptWord(myWord);
158                         return 0; // correct if precomposed characters expanded, all upper
159                 }
160                 tmpBuf = curBuf;
161                 curBuf = otherBuf;
162                 otherBuf = tmpBuf;
163         }
164         if (manager->theFlags & uSpell::hasCompounds) {
165                 if (manager->isSpelledRightMultiple(curBuf, length)) {
166                         manager->acceptWord(myWord);
167                         return 0; // correct as two words.  Not right for all languages.
168                 }
169         }
170         return 1;
171 }
172
173 static char **
174 uspell_dict_suggest (EnchantDict * me, const char *const word,
175                      size_t len, size_t * out_n_suggs)
176 {
177         uSpell *manager;
178         utf8_t myWord[MAXCHARS];
179         
180         char **sugg_arr = NULL;
181         const utf8_t *sugg;
182         wide_t buf[MAXCHARS];
183         int length;
184   unsigned int i;
185         utf8_t **list;
186         
187         if (len >= MAXCHARS) // no suggestions; the word is outlandish
188                 return NULL;
189         memcpy(reinterpret_cast<char *>(myWord), word, len);
190         myWord[len] = 0;
191         manager = reinterpret_cast<uSpell *>(me->user_data);
192         
193         list = reinterpret_cast<utf8_t **>(
194                                            calloc(sizeof(char *), MAXALTERNATIVE));
195         length = utf8_wide(buf, myWord, MAXCHARS);
196         *out_n_suggs = manager->showAlternatives(buf, length,
197                                                  list, MAXALTERNATIVE);
198         
199         if (*out_n_suggs)
200                 {
201                         sugg_arr = g_new0 (char *, *out_n_suggs + 1);
202                         for (i = 0; i < *out_n_suggs; i++)
203                                 {
204                                         sugg = list[i];
205                                         if (sugg)
206                                                 sugg_arr[i] =
207                                                         g_strdup (reinterpret_cast<const gchar *>(sugg));
208                                         free(list[i]);
209                                 }
210                 }
211         free(list);
212         return sugg_arr;
213 } // uspell_dict_suggest
214
215 static void
216 uspell_dict_add_to_session (EnchantDict * me, const char *const word,
217                      size_t len)
218 {
219         uSpell *manager;
220         wide_t buf[MAXCHARS];
221         utf8_t myWord[MAXCHARS];
222         int length, index;
223         
224         manager = reinterpret_cast<uSpell *>(me->user_data);
225
226         manager->acceptWord((const utf8_t *)word);
227         if (len >= MAXCHARS)
228                 return; // too long; can't reasonably convert
229         // see if we want to acceptWord(uppercase(myWord))
230         if (!(manager->theFlags & uSpell::upperLower)) return; // non-case language
231         length = utf8_wide(buf, (const utf8_t *)word, MAXCHARS);
232         for (index = 0; index < length; index++) {
233                 if (g_unichar_isupper(buf[index])) return; // case-sensitive word
234                 buf[index] = g_unichar_toupper(buf[index]);
235         }
236         wide_utf8(myWord, MAXCHARS, buf, length);
237         manager->acceptWord(myWord);
238 } // uspell_dict_add_to_session
239
240 typedef struct {
241         const char * language_tag;
242         const char * corresponding_uspell_file_name;
243         int language_flags;
244 } Mapping;
245
246 static const Mapping mapping [] = {
247         {"he",    "hebrew",  0},
248         {"he_IL", "hebrew",  0},
249         {"yi",    "yiddish", uSpell::hasComposition},
250         {"en_US", "american", uSpell::upperLower},
251 };
252
253 static const size_t n_mappings = (sizeof(mapping)/sizeof(mapping[0]));
254
255 static void
256 s_buildHashNames (std::vector<std::string> & names, EnchantBroker * broker, const char * tag)
257 {
258         names.clear ();
259
260         size_t mapIndex;
261
262         for (mapIndex = 0; mapIndex < n_mappings; mapIndex++) {
263                 if (!strcmp(tag, mapping[mapIndex].language_tag)) 
264                         break;
265         }
266
267         if (mapIndex < n_mappings) {
268
269                 GSList *dirs, *iter;
270                 char * dict = g_strdup_printf ("%s.uspell.dat",
271                                                mapping[mapIndex].corresponding_uspell_file_name);
272
273                 dirs = uspell_checker_get_dictionary_dirs (broker);
274
275                 for (iter = dirs; iter; iter = iter->next)
276                         {
277                                 char *tmp;
278
279                                 tmp = g_build_filename ((const char *)iter->data, dict, NULL);
280                                 names.push_back (tmp);
281                                 g_free (tmp);
282                         }
283
284                 g_slist_foreach (dirs, (GFunc)g_free, NULL);
285                 g_slist_free (dirs);
286         }
287 }
288
289 static uSpell *
290 uspell_request_dict (const char * base, const char * mapping, const int flags)
291 {
292         char *fileName, *transName, *filePart, *transPart;
293
294         uSpell *manager;
295
296         if (!base)
297                 return NULL;
298
299         filePart =  g_strconcat(mapping, ".uspell.dat", NULL);
300         transPart =  g_strconcat(mapping, ".uspell.trans", NULL);
301         fileName = g_build_filename (base, filePart, NULL);
302         transName = g_build_filename (base, transPart, NULL);
303         g_free(filePart);       
304         g_free(transPart);      
305
306         try {
307                 manager = new uSpell(fileName, transName, flags);
308         } 
309         catch (...) {
310                 manager = NULL;
311         }
312
313         g_free (fileName);
314         g_free (transName);
315
316         return manager;
317 }
318
319 static uSpell *
320 uspell_request_manager (const char * dir, size_t mapIndex)
321 {
322         uSpell * manager = NULL;
323
324         manager = uspell_request_dict (dir,
325                                        mapping[mapIndex].corresponding_uspell_file_name,
326                                        mapping[mapIndex].language_flags);
327
328         if (!manager) return NULL;
329
330         // look for a supplementary private dictionary
331         const char *config_dir = g_get_user_config_dir();
332         if (config_dir) {
333                 gchar * auxFileName, * transPart;
334                 transPart = g_strconcat (mapping[mapIndex].language_tag, ".dic", NULL);
335                 auxFileName = g_build_filename (config_dir, transPart, NULL);
336                 g_free (transPart);
337
338                 (void) manager->assimilateFile (auxFileName);
339                 g_free (auxFileName);
340         }
341
342         return manager;
343 }
344
345 extern "C" {
346
347 ENCHANT_MODULE_EXPORT (EnchantProvider *) 
348              init_enchant_provider (void);
349
350 static EnchantDict *
351 uspell_provider_request_dict (EnchantProvider * me, const char *const tag)
352 {
353         EnchantDict *dict = NULL;
354         uSpell *manager = NULL;
355         size_t mapIndex;
356         bool found = false;
357
358         GSList *dirs, *iter;
359
360         for (mapIndex = 0; mapIndex < n_mappings && !found; mapIndex++) {
361                 if (!strcmp(tag, mapping[mapIndex].language_tag)) 
362                         break;
363         }
364
365         if (!found)
366                 return NULL;
367
368         dirs = uspell_checker_get_dictionary_dirs (me->owner);
369
370         for (iter = dirs; iter && !manager; iter = iter->next)
371                 {
372                         manager = uspell_request_manager ((const char *)iter->data, mapIndex);
373                 }
374         
375         g_slist_foreach (dirs, (GFunc)g_free, NULL);
376         g_slist_free (dirs);
377
378         if (!manager) 
379                 return NULL;
380
381         dict = g_new0 (EnchantDict, 1);
382         dict->user_data = manager;
383         dict->check = uspell_dict_check;
384         dict->suggest = uspell_dict_suggest;
385         dict->add_to_session = uspell_dict_add_to_session;
386         // don't use personal, session - let higher level implement that
387         
388         return dict;
389 }
390
391 static int
392 uspell_provider_dictionary_exists (struct str_enchant_provider * me, 
393                                    const char *const tag)
394 {
395         std::vector <std::string> names;
396
397         s_buildHashNames (names, me->owner, tag);
398         for (size_t i = 0; i < names.size(); i++) {
399                 if (g_file_test (names[i].c_str(), G_FILE_TEST_EXISTS))
400                         return 1;
401         }
402
403         return 0;
404 }
405
406 static void
407 uspell_provider_dispose_dict (EnchantProvider * me, EnchantDict * dict)
408 {
409         uSpell *manager = reinterpret_cast<uSpell *>(dict->user_data);
410         delete manager;
411
412         g_free (dict);
413 }
414
415 static char **
416 uspell_provider_list_dictionaries (EnchantProvider * me,
417                                    size_t * out_n_dicts)
418 {
419         size_t i, nb;
420
421         nb = 0;
422         for (i = 0; i < n_mappings; i++)
423                 if (uspell_provider_dictionary_exists (me, mapping[i].language_tag))
424                         nb++;
425         
426         *out_n_dicts = nb;
427         if (nb == 0)
428                 return NULL;
429
430         char ** out_dicts = g_new0 (char *, nb + 1);
431         for (i = 0; i < n_mappings; i++)
432                 if (uspell_provider_dictionary_exists (me, mapping[i].language_tag))
433                         out_dicts[i] = g_strdup (mapping[i].language_tag);
434
435         return out_dicts;
436 }
437
438 static void
439 uspell_provider_free_string_list (EnchantProvider * me, char **str_list)
440 {
441         g_strfreev (str_list);
442 }
443
444 static void
445 uspell_provider_dispose (EnchantProvider * me)
446 {
447         g_free (me);
448 }
449
450 static const char *
451 uspell_provider_identify (EnchantProvider * me)
452 {
453         return "uspell";
454 }
455
456 static const char *
457 uspell_provider_describe (EnchantProvider * me)
458 {
459         return "Uspell Provider";
460 }
461
462 EnchantProvider *
463 init_enchant_provider (void)
464 {
465         EnchantProvider *provider;
466         
467         provider = g_new0 (EnchantProvider, 1);
468         provider->dispose = uspell_provider_dispose;
469         provider->request_dict = uspell_provider_request_dict;
470         provider->dispose_dict = uspell_provider_dispose_dict;
471         provider->dictionary_exists = uspell_provider_dictionary_exists;
472         provider->identify = uspell_provider_identify;
473         provider->describe = uspell_provider_describe;
474         provider->list_dicts = uspell_provider_list_dictionaries;
475         provider->free_string_list = uspell_provider_free_string_list;
476
477         return provider;
478 }
479
480 }
481