initial import
[vuplus_webkit] / Source / WebKit / qt / WebCoreSupport / TextCheckerClientQt.cpp
1 /*
2  * Copyright (C) 2011 Lindsay Mathieson <lindsay.mathieson@gmail.com>
3  * Copyright (C) 2011 Dawit Alemayehu  <adawit@kde.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "TextCheckerClientQt.h"
31
32 #include "NotImplemented.h"
33 #include "QtPlatformPlugin.h"
34
35 #include <QStringList>
36 #include <QVector>
37 #include <wtf/text/WTFString.h>
38
39
40 static void convertToVectorList(const QStringList& list, Vector<String>& vList)
41 {
42     const int count = list.count();
43     vList.resize(count);
44     for (int i = 0; i < count; ++i)
45         vList.append(list.at(i));
46 }
47
48 namespace WebCore {
49
50 void TextCheckerClientQt::ignoreWordInSpellDocument(const String& word)
51 {
52     if (!loadSpellChecker())
53         return;
54
55     m_spellChecker->ignoreWordInSpellDocument(word);
56 }
57
58 void TextCheckerClientQt::learnWord(const String& word)
59 {
60     if (!loadSpellChecker())
61         return;
62
63     m_spellChecker->learnWord(word);
64 }
65
66 String TextCheckerClientQt::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
67 {
68     if (!loadSpellChecker())
69         return String();
70
71     return m_spellChecker->autoCorrectSuggestionForMisspelledWord(misspelledWord);
72 }
73
74 void TextCheckerClientQt::checkSpellingOfString(const UChar* buffer, int length, int* misspellingLocation, int* misspellingLength)
75 {
76     if (!loadSpellChecker())
77         return;
78
79     const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
80     m_spellChecker->checkSpellingOfString(text, misspellingLocation, misspellingLength);
81 }
82
83 void TextCheckerClientQt::checkGrammarOfString(const UChar* buffer, int length, Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
84 {
85     if (!loadSpellChecker())
86         return;
87
88     const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
89
90     // Do Grammer check.
91     QList<QWebSpellChecker::GrammarDetail> qGrammarDetails;
92     m_spellChecker->checkGrammarOfString(text, qGrammarDetails, badGrammarLocation, badGrammarLength);
93
94     // Copy the grammar detail from the Qt plugin to the webkit structure.
95     const int count = qGrammarDetails.count();
96     for (int i = 0; i < count; ++i) {
97         const QWebSpellChecker::GrammarDetail qGrammarDetail = qGrammarDetails.at(i);
98         GrammarDetail webkitGrammarDetail;
99         webkitGrammarDetail.location = qGrammarDetail.location;
100         webkitGrammarDetail.length = qGrammarDetail.length;
101         // Copy guesses strings.
102         convertToVectorList(qGrammarDetail.guesses, webkitGrammarDetail.guesses);
103         webkitGrammarDetail.userDescription = qGrammarDetail.userDescription;
104         details.append(webkitGrammarDetail);
105     }
106 }
107
108 void TextCheckerClientQt::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
109 {
110     if (!loadSpellChecker())
111         return;
112
113     QStringList guessesList;
114     m_spellChecker->guessesForWord(word, context, guessesList);
115     convertToVectorList(guessesList, guesses);
116 }
117
118 bool TextCheckerClientQt::isContinousSpellCheckingEnabled()
119 {
120     if (!loadSpellChecker())
121         return false;
122
123     return m_spellChecker->isContinousSpellCheckingEnabled();
124 }
125
126 void TextCheckerClientQt::toggleContinousSpellChecking()
127 {
128     if (!loadSpellChecker())
129         return;
130
131     m_spellChecker->toggleContinousSpellChecking();
132 }
133
134 bool TextCheckerClientQt::isGrammarCheckingEnabled()
135 {
136     if (!loadSpellChecker())
137         return false;
138
139     return m_spellChecker->isGrammarCheckingEnabled();
140 }
141
142 void TextCheckerClientQt::toggleGrammarChecking()
143 {
144     if (!loadSpellChecker())
145         return;
146
147     m_spellChecker->toggleGrammarChecking();
148 }
149
150 bool TextCheckerClientQt::loadSpellChecker()
151 {
152     if (m_spellChecker)
153         return true;
154
155     if ((m_spellChecker = m_platformPlugin.createSpellChecker()))
156         return true;
157
158     return false;
159 }
160
161 }