Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / lib / ephy-spell-check.c
blob5d4d746a0cd553a1534e2500c138d8e800c769a8
1 /*
2 * Copyright © 2006 Christian Persch
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * $Id: ephy-spell-check.c 6952 2007-03-11 19:42:02Z chpe $
21 #include "config.h"
23 #include <string.h>
25 #include <glib/gi18n.h>
27 #include <enchant.h>
29 #include "ephy-debug.h"
31 #include "ephy-spell-check.h"
33 #define EPHY_SPELL_CHECK_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_SPELL_CHECK, EphySpellCheckPrivate))
35 struct _EphySpellCheckPrivate
37 EnchantBroker *broker;
38 EnchantDict *dict;
41 enum
43 LAST_SIGNAL
46 /* static guint signals[LAST_SIGNAL]; */
47 static GObjectClass *parent_class;
49 /* Helper functions */
51 /* Class implementation */
53 static void
54 ephy_spell_check_init (EphySpellCheck *speller)
56 EphySpellCheckPrivate *priv;
57 const gchar * const *locale;
59 priv = speller->priv = EPHY_SPELL_CHECK_GET_PRIVATE (speller);
61 priv->broker = enchant_broker_init ();
63 /* We don't want to check against C so we get a useful message
64 in case of no available dictionary */
65 for (locale = g_get_language_names ();
66 *locale != NULL && g_ascii_strcasecmp (*locale, "C") != 0;
67 ++locale)
69 priv->dict = enchant_broker_request_dict (priv->broker, *locale);
70 if (priv->dict != NULL) break;
72 if (priv->dict == NULL)
73 g_warning (enchant_broker_get_error (priv->broker));
76 static void
77 ephy_spell_check_finalize (GObject *object)
79 EphySpellCheck *speller = EPHY_SPELL_CHECK (object);
80 EphySpellCheckPrivate *priv = speller->priv;
83 LOG ("EphySpellCheck finalised");
85 if (priv->dict != NULL)
87 enchant_broker_free_dict (priv->broker, priv->dict);
88 priv->dict = NULL;
91 enchant_broker_free (priv->broker);
93 parent_class->finalize (object);
96 static void
97 ephy_spell_check_class_init (EphySpellCheckClass *klass)
99 GObjectClass *object_class = G_OBJECT_CLASS (klass);
101 parent_class = g_type_class_peek_parent (klass);
103 object_class->finalize = ephy_spell_check_finalize;
105 #if 0
106 signals[CANCEL] =
107 g_signal_new ("cancel",
108 G_OBJECT_CLASS_TYPE (klass),
109 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
110 G_STRUCT_OFFSET (EphySpellCheckClass, cancel),
111 g_signal_accumulator_true_handled, NULL,
112 ephy_marshal_BOOLEAN__VOID,
113 G_TYPE_BOOLEAN,
115 #endif
117 g_type_class_add_private (object_class, sizeof (EphySpellCheckPrivate));
120 GType
121 ephy_spell_check_get_type (void)
123 static GType type = 0;
125 if (G_UNLIKELY (type == 0))
127 const GTypeInfo our_info =
129 sizeof (EphySpellCheckClass),
130 NULL,
131 NULL,
132 (GClassInitFunc) ephy_spell_check_class_init,
133 NULL,
134 NULL,
135 sizeof (EphySpellCheck),
137 (GInstanceInitFunc) ephy_spell_check_init
140 type = g_type_register_static (G_TYPE_OBJECT,
141 "EphySpellCheck",
142 &our_info, 0);
145 return type;
148 /* Public API */
151 * ephy_spell_check_get_default:
153 * Returns: a reference to the default #EphySpellCheck object
155 EphySpellCheck *
156 ephy_spell_check_get_default (void)
158 static EphySpellCheck *instance = NULL;
160 if (instance == NULL)
162 EphySpellCheck **instanceptr = &instance;
164 instance = g_object_new (EPHY_TYPE_SPELL_CHECK, NULL);
165 g_object_add_weak_pointer (G_OBJECT (instance),
166 (gpointer) instanceptr);
168 return instance;
171 return g_object_ref (instance);
175 ephy_spell_check_check_word (EphySpellCheck *speller,
176 const char *word,
177 gssize len,
178 gboolean *correct)
180 EphySpellCheckPrivate *priv = speller->priv;
181 int result;
183 g_return_val_if_fail (word != NULL, -1);
185 if (priv->dict == NULL)
186 return FALSE;
188 if (len < 0)
189 len = strlen (word);
191 result = enchant_dict_check (priv->dict, word, len);
192 if (result < 0)
193 return FALSE;
195 *correct = result == 0;
197 return TRUE;
200 char **
201 ephy_spell_check_get_suggestions (EphySpellCheck *speller,
202 const char *word,
203 gssize len,
204 gsize *_count)
206 EphySpellCheckPrivate *priv = speller->priv;
207 char **suggestions;
208 size_t count;
210 g_return_val_if_fail (word != NULL, NULL);
212 if (priv->dict == NULL)
213 return FALSE;
215 if (len < 0)
216 len = strlen (word);
218 suggestions = enchant_dict_suggest (priv->dict, word, len, &count);
220 *_count = count;
221 return suggestions;
224 void
225 ephy_spell_check_free_suggestions (EphySpellCheck *speller,
226 char **suggestions)
228 EphySpellCheckPrivate *priv = speller->priv;
230 if (suggestions != NULL)
232 g_return_if_fail (priv->dict != NULL);
234 /* FIXME!! What if inbetween there has been a change of dict!? */
235 enchant_dict_free_suggestions (priv->dict, suggestions);
239 gboolean
240 ephy_spell_check_set_language (EphySpellCheck *speller,
241 const char *lang)
243 EphySpellCheckPrivate *priv = speller->priv;
244 char *code;
246 if (priv->dict != NULL)
248 enchant_broker_free_dict (priv->broker, priv->dict);
249 priv->dict = NULL;
252 /* Enchant expects ab_CD codes, not ab-CD */
253 code = g_strdup (lang);
254 g_strdelimit (code, "-", '_');
255 priv->dict = enchant_broker_request_dict (priv->broker, code);
256 g_free (code);
258 return priv->dict != NULL;
261 static void
262 describe_cb (const char * const lang_tag,
263 const char * const provider_name,
264 const char * const provider_desc,
265 const char * const provider_file,
266 char **_language)
268 *_language = g_strdup (lang_tag);
271 char *
272 ephy_spell_check_get_language (EphySpellCheck *speller)
274 EphySpellCheckPrivate *priv = speller->priv;
275 char *code = NULL;
277 if (priv->dict == NULL) return NULL;
279 enchant_dict_describe (priv->dict, (EnchantDictDescribeFn) describe_cb, &code);
280 return code;