Tizen 2.1 base
[external/enchant.git] / src / voikko / voikko_provider.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* enchant
3  * Copyright (C) 2003,2004 Dom Lachowicz
4  *               2006-2007 Harri Pitkänen <hatapitk@iki.fi>
5  *               2006 Anssi Hannula <anssi.hannula@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02110-1301, USA.
21  *
22  * In addition, as a special exception, Dom Lachowicz
23  * gives permission to link the code of this program with
24  * non-LGPL Spelling Provider libraries (eg: a MSFT Office
25  * spell checker backend) and distribute linked combinations including
26  * the two.  You must obey the GNU Lesser General Public License in all
27  * respects for all of the code used other than said providers.  If you modify
28  * this file, you may extend this exception to your version of the
29  * file, but you are not obligated to do so.  If you do not wish to
30  * do so, delete this exception statement from your version.
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38 #include <libvoikko/voikko.h>
39
40 #include "enchant.h"
41 #include "enchant-provider.h"
42
43 /**
44  * Voikko is a Finnish spell checker. More information is available from:
45  *
46  * http://voikko.sourceforge.net/
47  */
48 ENCHANT_PLUGIN_DECLARE("Voikko")
49
50 static int
51 voikko_dict_check (EnchantDict * me, const char *const word, size_t len)
52 {
53         int result;
54         int voikko_handle;
55
56         voikko_handle = (long) me->user_data;
57         result = voikko_spell_cstr(voikko_handle, word);
58         if (result == VOIKKO_SPELL_FAILED)
59                 return 1;
60         else if (result == VOIKKO_SPELL_OK)
61                 return 0;
62         else
63                 return -1;
64 }
65
66 static char **
67 voikko_dict_suggest (EnchantDict * me, const char *const word,
68                      size_t len, size_t * out_n_suggs)
69 {
70         char **sugg_arr;
71         int voikko_handle;
72
73         voikko_handle = (long) me->user_data;
74         sugg_arr = voikko_suggest_cstr(voikko_handle, word);
75         if (sugg_arr == NULL)
76                 return NULL;
77         for (*out_n_suggs = 0; sugg_arr[*out_n_suggs] != NULL; (*out_n_suggs)++);
78         return sugg_arr;
79 }
80
81 static EnchantDict *
82 voikko_provider_request_dict (EnchantProvider * me, const char *const tag)
83 {
84         EnchantDict *dict;
85         const char * voikko_error;
86         int voikko_handle;
87
88         /* Only Finnish is supported at the moment */
89         if (strncmp(tag, "fi_FI", 6) != 0 && strncmp(tag, "fi", 3) != 0)
90                 return NULL;
91         
92         voikko_error = voikko_init(&voikko_handle, "fi_FI", 0);
93         if (voikko_error) {
94                 enchant_provider_set_error(me, voikko_error);
95                 return NULL;
96         }
97
98         dict = g_new0 (EnchantDict, 1);
99         dict->user_data = (void *)(long) voikko_handle;
100         dict->check = voikko_dict_check;
101         dict->suggest = voikko_dict_suggest;
102
103         return dict;
104 }
105
106 static void
107 voikko_provider_dispose_dict (EnchantProvider * me, EnchantDict * dict)
108 {
109         voikko_terminate((long) dict->user_data);
110         g_free (dict);
111 }
112
113 static int
114 voikko_provider_dictionary_exists (struct str_enchant_provider * me,
115                                    const char *const tag)
116 {
117         int voikko_handle;
118
119         /* Only Finnish is supported */
120         if (strncmp(tag, "fi", 3) != 0)
121                 return 0;
122
123         /* Check that a dictionary is actually available */
124         if (voikko_init(&voikko_handle, "fi_FI", 0) == NULL) {
125                 voikko_terminate(voikko_handle);
126                 return 1;
127         }
128         else return 0;
129 }
130
131
132 static char **
133 voikko_provider_list_dicts (EnchantProvider * me, 
134                             size_t * out_n_dicts)
135 {
136         char ** out_list = NULL;
137         int voikko_handle;
138         *out_n_dicts = 0;
139
140         if (voikko_init(&voikko_handle, "fi_FI", 0) == NULL) {
141                 voikko_terminate(voikko_handle);
142                 *out_n_dicts = 1;
143                 out_list = g_new0 (char *, *out_n_dicts + 1);
144                 out_list[0] = g_strdup("fi");
145         }
146
147         return out_list;
148 }
149
150 static void
151 voikko_provider_free_string_list (EnchantProvider * me, char **str_list)
152 {
153         g_strfreev (str_list);
154 }
155
156 static void
157 voikko_provider_dispose (EnchantProvider * me)
158 {
159         g_free (me);
160 }
161
162 static const char *
163 voikko_provider_identify (EnchantProvider * me)
164 {
165         return "voikko";
166 }
167
168 static const char *
169 voikko_provider_describe (EnchantProvider * me)
170 {
171         return "Voikko Provider";
172 }
173
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177
178 ENCHANT_MODULE_EXPORT (EnchantProvider *) 
179              init_enchant_provider (void);
180
181 EnchantProvider *
182 init_enchant_provider (void)
183 {
184         EnchantProvider *provider;
185
186         provider = g_new0 (EnchantProvider, 1);
187         provider->dispose = voikko_provider_dispose;
188         provider->request_dict = voikko_provider_request_dict;
189         provider->dispose_dict = voikko_provider_dispose_dict;
190         provider->dictionary_exists = voikko_provider_dictionary_exists;
191         provider->identify = voikko_provider_identify;
192         provider->describe = voikko_provider_describe;
193         provider->list_dicts = voikko_provider_list_dicts;
194         provider->free_string_list = voikko_provider_free_string_list;
195
196         return provider;
197 }
198
199 #ifdef __cplusplus
200 }
201 #endif