Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_text_checker.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  * Copyright (C) 2012 Intel Corporation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 #include "config.h"
28 #include "ewk_text_checker.h"
29
30 #if ENABLE(SPELLCHECK)
31
32 #include "TextCheckerEnchant.h"
33 #include "WKAPICast.h"
34 #include "WKMutableArray.h"
35 #include "WKRetainPtr.h"
36 #include "WKString.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"
42 #include <Eina.h>
43 #include <wtf/OwnPtr.h>
44 #include <wtf/text/CString.h>
45
46 using namespace WebCore;
47 using namespace WebKit;
48
49 /**
50  * @brief Structure to store client callback functions.
51  *
52  * @internal
53  */
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;
61 };
62
63 static inline TextCheckerEnchant* textCheckerEnchant()
64 {
65     static OwnPtr<TextCheckerEnchant> textCheckerEnchant = TextCheckerEnchant::create();
66     return textCheckerEnchant.get();
67 }
68
69 static inline ClientCallbacks& clientCallbacks()
70 {
71     DEFINE_STATIC_LOCAL(ClientCallbacks, clientCallbacks, ());
72     return clientCallbacks;
73 }
74
75 static bool isContinuousSpellCheckingEnabled(const void*)
76 {
77     return ewk_settings_continuous_spell_checking_enabled_get();
78 }
79
80 static void setContinuousSpellCheckingEnabled(bool enabled, const void*)
81 {
82     ewk_settings_continuous_spell_checking_enabled_set(enabled);
83 }
84
85 static uint64_t uniqueSpellDocumentTag(WKPageRef page, const void*)
86 {
87     if (clientCallbacks().unique_spell_document_tag_get)
88         return clientCallbacks().unique_spell_document_tag_get(toImpl(page)->viewWidget());
89
90     return 0;
91 }
92
93 static void closeSpellDocumentWithTag(uint64_t tag, const void*)
94 {
95     if (clientCallbacks().unique_spell_document_tag_close)
96         clientCallbacks().unique_spell_document_tag_close(tag);
97 }
98
99 static void checkSpellingOfString(uint64_t tag, WKStringRef text, int32_t* misspellingLocation, int32_t* misspellingLength, const void*)
100 {
101     if (clientCallbacks().string_spelling_check)
102         clientCallbacks().string_spelling_check(tag, toImpl(text)->string().utf8().data(), misspellingLocation, misspellingLength);
103     else
104         textCheckerEnchant()->checkSpellingOfString(toImpl(text)->string(), *misspellingLocation, *misspellingLength);
105 }
106
107 static WKArrayRef guessesForWord(uint64_t tag, WKStringRef word, const void*)
108 {
109     WKMutableArrayRef suggestionsForWord = WKMutableArrayCreate();
110
111     if (clientCallbacks().word_guesses_get) {
112         Eina_List* list = clientCallbacks().word_guesses_get(tag, toImpl(word)->string().utf8().data());
113         void* item;
114
115         EINA_LIST_FREE(list, item) {
116             WKRetainPtr<WKStringRef> suggestion(AdoptWK, WKStringCreateWithUTF8CString(static_cast<const char*>(item)));
117             WKArrayAppendItem(suggestionsForWord, suggestion.get());
118             free(item);
119         }
120     } else {
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());
126         }
127     }
128
129     return suggestionsForWord;
130 }
131
132 static void learnWord(uint64_t tag, WKStringRef word, const void*)
133 {
134     if (clientCallbacks().word_learn)
135         clientCallbacks().word_learn(tag, toImpl(word)->string().utf8().data());
136     else
137         textCheckerEnchant()->learnWord(toImpl(word)->string());
138 }
139
140 static void ignoreWord(uint64_t tag, WKStringRef word, const void*)
141 {
142     if (clientCallbacks().word_ignore)
143         clientCallbacks().word_ignore(tag, toImpl(word)->string().utf8().data());
144     else
145         textCheckerEnchant()->ignoreWord(toImpl(word)->string());
146 }
147
148 namespace Ewk_Text_Checker {
149
150 Vector<String> availableSpellCheckingLanguages()
151 {
152     return textCheckerEnchant()->availableSpellCheckingLanguages();
153 }
154
155 void updateSpellCheckingLanguages(const Vector<String>& languages)
156 {
157     textCheckerEnchant()->updateSpellCheckingLanguages(languages);
158 }
159
160 Vector<String> loadedSpellCheckingLanguages()
161 {
162     return textCheckerEnchant()->loadedSpellCheckingLanguages();
163 }
164
165 bool hasDictionary()
166 {
167     return textCheckerEnchant()->hasDictionary();
168 }
169
170 /**
171  * Initializes spellcheck feature.
172  *
173  * @internal
174  *
175  * The default spellcheck feature is based on Enchant library.
176  * Client may use own spellcheck implementation previously set
177  * through the callback functions.
178  */
179 void initialize()
180 {
181     static bool didInitializeTextCheckerClient = false;
182     if (didInitializeTextCheckerClient)
183         return;
184
185     WKTextCheckerClient textCheckerClient = {
186         kWKTextCheckerClientCurrentVersion,
187         0, // clientInfo
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
201         guessesForWord,
202         learnWord,
203         ignoreWord
204     };
205     WKTextCheckerSetClient(&textCheckerClient);
206
207     didInitializeTextCheckerClient = true;
208 }
209
210 } // namespace Ewk_Text_Checker
211
212 #define EWK_TEXT_CHECKER_CALLBACK_SET(TYPE_NAME, NAME)  \
213 void ewk_text_checker_##NAME##_cb_set(TYPE_NAME cb)     \
214 {                                                       \
215     clientCallbacks().NAME = cb;      \
216 }
217
218 #else
219
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)        \
223 {                                                       \
224 }
225 #endif // ENABLE(SPELLCHECK)
226
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)