[GLIB] Use GUniquePtr instead of GOwnPtr
[WebKit-https.git] / Source / WebKit / gtk / webkit / webkitspellcheckerenchant.cpp
1 /*
2  *  Copyright (C) 2011 Igalia S.L.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library 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 GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "webkitspellcheckerenchant.h"
21
22 #if ENABLE(SPELLCHECK)
23
24 #include "TextCheckerEnchant.h"
25 #include "webkitspellchecker.h"
26 #include <gtk/gtk.h>
27 #include <wtf/OwnPtr.h>
28 #include <wtf/text/CString.h>
29
30 using namespace WebCore;
31
32 /**
33  * SECTION:webkitspellcheckerenchant
34  * @short_description: the default spell checking implementation for WebKitGTK+.
35  *
36  * #WebKitSpellCheckerEnchant is the default spell checking implementation for
37  * WebKitGTK+. It uses the Enchant dictionaries installed on the system to
38  * correct spelling.
39  */
40
41 struct _WebKitSpellCheckerEnchantPrivate {
42     OwnPtr<TextCheckerEnchant> textCheckerEnchant;
43 };
44
45 static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* checkerInterface);
46
47 G_DEFINE_TYPE_WITH_CODE(WebKitSpellCheckerEnchant, webkit_spell_checker_enchant, G_TYPE_OBJECT,
48                         G_IMPLEMENT_INTERFACE(WEBKIT_TYPE_SPELL_CHECKER,
49                                               webkit_spell_checker_enchant_spell_checker_interface_init))
50
51 static void webkit_spell_checker_enchant_finalize(GObject* object)
52 {
53     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(object)->priv;
54     priv->~WebKitSpellCheckerEnchantPrivate();
55 }
56
57 static void webkit_spell_checker_enchant_class_init(WebKitSpellCheckerEnchantClass* klass)
58 {
59     GObjectClass* objectClass = G_OBJECT_CLASS(klass);
60     objectClass->finalize = webkit_spell_checker_enchant_finalize;
61     g_type_class_add_private(klass, sizeof(WebKitSpellCheckerEnchantPrivate));
62 }
63
64 static void webkit_spell_checker_enchant_init(WebKitSpellCheckerEnchant* checker)
65 {
66     WebKitSpellCheckerEnchantPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(checker, WEBKIT_TYPE_SPELL_CHECKER_ENCHANT, WebKitSpellCheckerEnchantPrivate);
67     checker->priv = priv;
68     new (priv) WebKitSpellCheckerEnchantPrivate();
69
70     priv->textCheckerEnchant = TextCheckerEnchant::create();
71 }
72
73 static void checkSpellingOfString(WebKitSpellChecker* checker, const char* string, int* misspellingLocation, int* misspellingLength)
74 {
75     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
76     priv->textCheckerEnchant->checkSpellingOfString(String::fromUTF8(string), *misspellingLocation, *misspellingLength);
77 }
78
79 static char** getGuessesForWord(WebKitSpellChecker* checker, const char* word, const char* context)
80 {
81     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
82
83     Vector<String> guesses = priv->textCheckerEnchant->getGuessesForWord(String::fromUTF8(word));
84
85     if (guesses.isEmpty())
86         return 0;
87
88     int i = 0;
89     int numberOfGuesses = guesses.size();
90     char** guessesArray = static_cast<char**>(g_malloc0((numberOfGuesses + 1) * sizeof(char*)));
91     for (Vector<String>::const_iterator iter = guesses.begin(); iter != guesses.end(); ++iter)
92         guessesArray[i++] = g_strdup(iter->utf8().data());
93     guessesArray[i] = 0;
94
95     return guessesArray;
96 }
97
98 static void updateSpellCheckingLanguages(WebKitSpellChecker* checker, const char* languages)
99 {
100     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
101
102     Vector<String> languagesVector;
103     String::fromUTF8(languages).split(static_cast<UChar>(','), languagesVector);
104     priv->textCheckerEnchant->updateSpellCheckingLanguages(languagesVector);
105 }
106
107 static char* getAutocorrectSuggestionsForMisspelledWord(WebKitSpellChecker* checker, const char* word)
108 {
109     return 0;
110 }
111
112 static void learnWord(WebKitSpellChecker* checker, const char* word)
113 {
114     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
115     priv->textCheckerEnchant->learnWord(String::fromUTF8(word));
116 }
117
118 static void ignoreWord(WebKitSpellChecker* checker, const char* word)
119 {
120     WebKitSpellCheckerEnchantPrivate* priv = WEBKIT_SPELL_CHECKER_ENCHANT(checker)->priv;
121     priv->textCheckerEnchant->ignoreWord(String::fromUTF8(word));
122 }
123
124 static void webkit_spell_checker_enchant_spell_checker_interface_init(WebKitSpellCheckerInterface* checkerInterface)
125 {
126     checkerInterface->check_spelling_of_string = checkSpellingOfString;
127     checkerInterface->get_guesses_for_word = getGuessesForWord;
128     checkerInterface->update_spell_checking_languages = updateSpellCheckingLanguages;
129     checkerInterface->get_autocorrect_suggestions_for_misspelled_word = getAutocorrectSuggestionsForMisspelledWord;
130     checkerInterface->learn_word = learnWord;
131     checkerInterface->ignore_word = ignoreWord;
132 }
133
134 #endif /* ENABLE(SPELLCHECK) */
135