1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Portions Copyright (c) 2010 Motorola Mobility, Inc.  All rights reserved.
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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * 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 APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "TextChecker.h"
29
30#include "TextCheckerState.h"
31#include "WebTextChecker.h"
32#include <WebCore/NotImplemented.h>
33
34using namespace WebCore;
35
36namespace WebKit {
37
38static TextCheckerState textCheckerState;
39
40const TextCheckerState& TextChecker::state()
41{
42    static bool didInitializeState = false;
43    if (didInitializeState)
44        return textCheckerState;
45
46    WebTextCheckerClient& client = WebTextChecker::shared()->client();
47    textCheckerState.isContinuousSpellCheckingEnabled = client.continuousSpellCheckingEnabled();
48    textCheckerState.isGrammarCheckingEnabled =  client.grammarCheckingEnabled();
49
50    didInitializeState = true;
51
52    return textCheckerState;
53}
54
55bool TextChecker::isContinuousSpellCheckingAllowed()
56{
57    return WebTextChecker::shared()->client().continuousSpellCheckingAllowed();
58}
59
60void TextChecker::setContinuousSpellCheckingEnabled(bool isContinuousSpellCheckingEnabled)
61{
62    if (state().isContinuousSpellCheckingEnabled == isContinuousSpellCheckingEnabled)
63        return;
64    textCheckerState.isContinuousSpellCheckingEnabled = isContinuousSpellCheckingEnabled;
65    WebTextChecker::shared()->client().setContinuousSpellCheckingEnabled(isContinuousSpellCheckingEnabled);
66}
67
68void TextChecker::setGrammarCheckingEnabled(bool isGrammarCheckingEnabled)
69{
70    if (state().isGrammarCheckingEnabled == isGrammarCheckingEnabled)
71        return;
72    textCheckerState.isGrammarCheckingEnabled = isGrammarCheckingEnabled;
73    WebTextChecker::shared()->client().setGrammarCheckingEnabled(isGrammarCheckingEnabled);
74}
75
76void TextChecker::continuousSpellCheckingEnabledStateChanged(bool enabled)
77{
78    textCheckerState.isContinuousSpellCheckingEnabled = enabled;
79}
80
81void TextChecker::grammarCheckingEnabledStateChanged(bool enabled)
82{
83    textCheckerState.isGrammarCheckingEnabled = enabled;
84}
85
86int64_t TextChecker::uniqueSpellDocumentTag(WebPageProxy* page)
87{
88    return WebTextChecker::shared()->client().uniqueSpellDocumentTag(page);
89}
90
91void TextChecker::closeSpellDocumentWithTag(int64_t tag)
92{
93    WebTextChecker::shared()->client().closeSpellDocumentWithTag(tag);
94}
95
96void TextChecker::checkSpellingOfString(int64_t spellDocumentTag, StringView text, int32_t& misspellingLocation, int32_t& misspellingLength)
97{
98    WebTextChecker::shared()->client().checkSpellingOfString(spellDocumentTag, text.toStringWithoutCopying(), misspellingLocation, misspellingLength);
99}
100
101void TextChecker::checkGrammarOfString(int64_t spellDocumentTag, StringView text, Vector<WebCore::GrammarDetail>& grammarDetails, int32_t& badGrammarLocation, int32_t& badGrammarLength)
102{
103    WebTextChecker::shared()->client().checkGrammarOfString(spellDocumentTag, text.toStringWithoutCopying(), grammarDetails, badGrammarLocation, badGrammarLength);
104}
105
106bool TextChecker::spellingUIIsShowing()
107{
108    return WebTextChecker::shared()->client().spellingUIIsShowing();
109}
110
111void TextChecker::toggleSpellingUIIsShowing()
112{
113    WebTextChecker::shared()->client().toggleSpellingUIIsShowing();
114}
115
116void TextChecker::updateSpellingUIWithMisspelledWord(int64_t spellDocumentTag, const String& misspelledWord)
117{
118    WebTextChecker::shared()->client().updateSpellingUIWithMisspelledWord(spellDocumentTag, misspelledWord);
119}
120
121void TextChecker::updateSpellingUIWithGrammarString(int64_t spellDocumentTag, const String& badGrammarPhrase, const GrammarDetail& grammarDetail)
122{
123    WebTextChecker::shared()->client().updateSpellingUIWithGrammarString(spellDocumentTag, badGrammarPhrase, grammarDetail);
124}
125
126void TextChecker::getGuessesForWord(int64_t spellDocumentTag, const String& word, const String& /* context */, Vector<String>& guesses)
127{
128    WebTextChecker::shared()->client().guessesForWord(spellDocumentTag, word, guesses);
129}
130
131void TextChecker::learnWord(int64_t spellDocumentTag, const String& word)
132{
133    WebTextChecker::shared()->client().learnWord(spellDocumentTag, word);
134}
135
136void TextChecker::ignoreWord(int64_t spellDocumentTag, const String& word)
137{
138    WebTextChecker::shared()->client().ignoreWord(spellDocumentTag, word);
139}
140
141void TextChecker::requestCheckingOfString(PassRefPtr<TextCheckerCompletion>)
142{
143    notImplemented();
144}
145
146} // namespace WebKit
147