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)
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 $
25 #include <glib/gi18n.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
;
46 /* static guint signals[LAST_SIGNAL]; */
47 static GObjectClass
*parent_class
;
49 /* Helper functions */
51 /* Class implementation */
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;
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
));
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
);
91 enchant_broker_free (priv
->broker
);
93 parent_class
->finalize (object
);
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
;
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
,
117 g_type_class_add_private (object_class
, sizeof (EphySpellCheckPrivate
));
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
),
132 (GClassInitFunc
) ephy_spell_check_class_init
,
135 sizeof (EphySpellCheck
),
137 (GInstanceInitFunc
) ephy_spell_check_init
140 type
= g_type_register_static (G_TYPE_OBJECT
,
151 * ephy_spell_check_get_default:
153 * Returns: a reference to the default #EphySpellCheck object
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
);
171 return g_object_ref (instance
);
175 ephy_spell_check_check_word (EphySpellCheck
*speller
,
180 EphySpellCheckPrivate
*priv
= speller
->priv
;
183 g_return_val_if_fail (word
!= NULL
, -1);
185 if (priv
->dict
== NULL
)
191 result
= enchant_dict_check (priv
->dict
, word
, len
);
195 *correct
= result
== 0;
201 ephy_spell_check_get_suggestions (EphySpellCheck
*speller
,
206 EphySpellCheckPrivate
*priv
= speller
->priv
;
210 g_return_val_if_fail (word
!= NULL
, NULL
);
212 if (priv
->dict
== NULL
)
218 suggestions
= enchant_dict_suggest (priv
->dict
, word
, len
, &count
);
225 ephy_spell_check_free_suggestions (EphySpellCheck
*speller
,
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
);
240 ephy_spell_check_set_language (EphySpellCheck
*speller
,
243 EphySpellCheckPrivate
*priv
= speller
->priv
;
246 if (priv
->dict
!= NULL
)
248 enchant_broker_free_dict (priv
->broker
, priv
->dict
);
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
);
258 return priv
->dict
!= NULL
;
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
,
268 *_language
= g_strdup (lang_tag
);
272 ephy_spell_check_get_language (EphySpellCheck
*speller
)
274 EphySpellCheckPrivate
*priv
= speller
->priv
;
277 if (priv
->dict
== NULL
) return NULL
;
279 enchant_dict_describe (priv
->dict
, (EnchantDictDescribeFn
) describe_cb
, &code
);