2 * Copyright (C) 2012 Samsung Electronics
3 * Copyright (C) 2012 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "ewk_text_checker.h"
30 #if ENABLE(SPELLCHECK)
32 #include "TextCheckerEnchant.h"
33 #include "WKAPICast.h"
34 #include "WKMutableArray.h"
35 #include "WKRetainPtr.h"
37 #include "WKTextChecker.h"
38 #include "WebPageProxy.h"
39 #include "WebString.h"
40 #include "ewk_settings.h"
41 #include "ewk_text_checker_private.h"
43 #include <wtf/OwnPtr.h>
44 #include <wtf/text/CString.h>
46 using namespace WebCore;
47 using namespace WebKit;
50 * @brief Structure to store client callback functions.
54 struct ClientCallbacks {
55 Ewk_Text_Checker_Unique_Spell_Document_Tag_Get_Cb unique_spell_document_tag_get;
56 Ewk_Text_Checker_Unique_Spell_Document_Tag_Close_Cb unique_spell_document_tag_close;
57 Ewk_Text_Checker_String_Spelling_Check_Cb string_spelling_check;
58 Ewk_Text_Checker_Word_Guesses_Get_Cb word_guesses_get;
59 Ewk_Text_Checker_Word_Learn_Cb word_learn;
60 Ewk_Text_Checker_Word_Ignore_Cb word_ignore;
63 static inline TextCheckerEnchant* textCheckerEnchant()
65 static OwnPtr<TextCheckerEnchant> textCheckerEnchant = TextCheckerEnchant::create();
66 return textCheckerEnchant.get();
69 static inline ClientCallbacks& clientCallbacks()
71 DEFINE_STATIC_LOCAL(ClientCallbacks, clientCallbacks, ());
72 return clientCallbacks;
75 static bool isContinuousSpellCheckingEnabled(const void*)
77 return ewk_settings_continuous_spell_checking_enabled_get();
80 static void setContinuousSpellCheckingEnabled(bool enabled, const void*)
82 ewk_settings_continuous_spell_checking_enabled_set(enabled);
85 static uint64_t uniqueSpellDocumentTag(WKPageRef page, const void*)
87 if (clientCallbacks().unique_spell_document_tag_get)
88 return clientCallbacks().unique_spell_document_tag_get(toImpl(page)->viewWidget());
93 static void closeSpellDocumentWithTag(uint64_t tag, const void*)
95 if (clientCallbacks().unique_spell_document_tag_close)
96 clientCallbacks().unique_spell_document_tag_close(tag);
99 static void checkSpellingOfString(uint64_t tag, WKStringRef text, int32_t* misspellingLocation, int32_t* misspellingLength, const void*)
101 if (clientCallbacks().string_spelling_check)
102 clientCallbacks().string_spelling_check(tag, toImpl(text)->string().utf8().data(), misspellingLocation, misspellingLength);
104 textCheckerEnchant()->checkSpellingOfString(toImpl(text)->string(), *misspellingLocation, *misspellingLength);
107 static WKArrayRef guessesForWord(uint64_t tag, WKStringRef word, const void*)
109 WKMutableArrayRef suggestionsForWord = WKMutableArrayCreate();
111 if (clientCallbacks().word_guesses_get) {
112 Eina_List* list = clientCallbacks().word_guesses_get(tag, toImpl(word)->string().utf8().data());
115 EINA_LIST_FREE(list, item) {
116 WKRetainPtr<WKStringRef> suggestion(AdoptWK, WKStringCreateWithUTF8CString(static_cast<const char*>(item)));
117 WKArrayAppendItem(suggestionsForWord, suggestion.get());
121 const Vector<String>& guesses = textCheckerEnchant()->getGuessesForWord(toImpl(word)->string());
122 size_t numberOfGuesses = guesses.size();
123 for (size_t i = 0; i < numberOfGuesses; ++i) {
124 WKRetainPtr<WKStringRef> suggestion(AdoptWK, WKStringCreateWithUTF8CString(guesses[i].utf8().data()));
125 WKArrayAppendItem(suggestionsForWord, suggestion.get());
129 return suggestionsForWord;
132 static void learnWord(uint64_t tag, WKStringRef word, const void*)
134 if (clientCallbacks().word_learn)
135 clientCallbacks().word_learn(tag, toImpl(word)->string().utf8().data());
137 textCheckerEnchant()->learnWord(toImpl(word)->string());
140 static void ignoreWord(uint64_t tag, WKStringRef word, const void*)
142 if (clientCallbacks().word_ignore)
143 clientCallbacks().word_ignore(tag, toImpl(word)->string().utf8().data());
145 textCheckerEnchant()->ignoreWord(toImpl(word)->string());
148 namespace Ewk_Text_Checker {
150 Vector<String> availableSpellCheckingLanguages()
152 return textCheckerEnchant()->availableSpellCheckingLanguages();
155 void updateSpellCheckingLanguages(const Vector<String>& languages)
157 textCheckerEnchant()->updateSpellCheckingLanguages(languages);
160 Vector<String> loadedSpellCheckingLanguages()
162 return textCheckerEnchant()->loadedSpellCheckingLanguages();
167 return textCheckerEnchant()->hasDictionary();
171 * Initializes spellcheck feature.
175 * The default spellcheck feature is based on Enchant library.
176 * Client may use own spellcheck implementation previously set
177 * through the callback functions.
181 static bool didInitializeTextCheckerClient = false;
182 if (didInitializeTextCheckerClient)
185 WKTextCheckerClient textCheckerClient = {
186 kWKTextCheckerClientCurrentVersion,
188 0, // isContinuousSpellCheckingAllowed
189 isContinuousSpellCheckingEnabled,
190 setContinuousSpellCheckingEnabled,
191 0, // isGrammarCheckingEnabled
192 0, // setGrammarCheckingEnabled
193 uniqueSpellDocumentTag,
194 closeSpellDocumentWithTag,
195 checkSpellingOfString,
196 0, // checkGrammarOfString
197 0, // spellingUIIsShowing
198 0, // toggleSpellingUIIsShowing
199 0, // updateSpellingUIWithMisspelledWord
200 0, // updateSpellingUIWithGrammarString
205 WKTextCheckerSetClient(&textCheckerClient);
207 didInitializeTextCheckerClient = true;
210 } // namespace Ewk_Text_Checker
212 #define EWK_TEXT_CHECKER_CALLBACK_SET(TYPE_NAME, NAME) \
213 void ewk_text_checker_##NAME##_cb_set(TYPE_NAME cb) \
215 clientCallbacks().NAME = cb; \
220 // Defines an empty API to do not break build.
221 #define EWK_TEXT_CHECKER_CALLBACK_SET(TYPE_NAME, NAME) \
222 void ewk_text_checker_##NAME##_cb_set(TYPE_NAME) \
225 #endif // ENABLE(SPELLCHECK)
227 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_Unique_Spell_Document_Tag_Get_Cb, unique_spell_document_tag_get)
228 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_Unique_Spell_Document_Tag_Close_Cb, unique_spell_document_tag_close)
229 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_String_Spelling_Check_Cb, string_spelling_check)
230 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_Word_Guesses_Get_Cb, word_guesses_get)
231 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_Word_Learn_Cb, word_learn)
232 EWK_TEXT_CHECKER_CALLBACK_SET(Ewk_Text_Checker_Word_Ignore_Cb, word_ignore)