2 * Copyright (C) 2011 Lindsay Mathieson <lindsay.mathieson@gmail.com>
3 * Copyright (C) 2011 Dawit Alemayehu <adawit@kde.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include "TextCheckerClientQt.h"
32 #include "NotImplemented.h"
33 #include "QtPlatformPlugin.h"
35 #include <QStringList>
37 #include <wtf/text/WTFString.h>
40 static void convertToVectorList(const QStringList& list, Vector<String>& vList)
42 const int count = list.count();
44 for (int i = 0; i < count; ++i)
45 vList.append(list.at(i));
50 void TextCheckerClientQt::ignoreWordInSpellDocument(const String& word)
52 if (!loadSpellChecker())
55 m_spellChecker->ignoreWordInSpellDocument(word);
58 void TextCheckerClientQt::learnWord(const String& word)
60 if (!loadSpellChecker())
63 m_spellChecker->learnWord(word);
66 String TextCheckerClientQt::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
68 if (!loadSpellChecker())
71 return m_spellChecker->autoCorrectSuggestionForMisspelledWord(misspelledWord);
74 void TextCheckerClientQt::checkSpellingOfString(const UChar* buffer, int length, int* misspellingLocation, int* misspellingLength)
76 if (!loadSpellChecker())
79 const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
80 m_spellChecker->checkSpellingOfString(text, misspellingLocation, misspellingLength);
83 void TextCheckerClientQt::checkGrammarOfString(const UChar* buffer, int length, Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
85 if (!loadSpellChecker())
88 const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
91 QList<QWebSpellChecker::GrammarDetail> qGrammarDetails;
92 m_spellChecker->checkGrammarOfString(text, qGrammarDetails, badGrammarLocation, badGrammarLength);
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);
108 void TextCheckerClientQt::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
110 if (!loadSpellChecker())
113 QStringList guessesList;
114 m_spellChecker->guessesForWord(word, context, guessesList);
115 convertToVectorList(guessesList, guesses);
118 bool TextCheckerClientQt::isContinousSpellCheckingEnabled()
120 if (!loadSpellChecker())
123 return m_spellChecker->isContinousSpellCheckingEnabled();
126 void TextCheckerClientQt::toggleContinousSpellChecking()
128 if (!loadSpellChecker())
131 m_spellChecker->toggleContinousSpellChecking();
134 bool TextCheckerClientQt::isGrammarCheckingEnabled()
136 if (!loadSpellChecker())
139 return m_spellChecker->isGrammarCheckingEnabled();
142 void TextCheckerClientQt::toggleGrammarChecking()
144 if (!loadSpellChecker())
147 m_spellChecker->toggleGrammarChecking();
150 bool TextCheckerClientQt::loadSpellChecker()
155 if ((m_spellChecker = m_platformPlugin.createSpellChecker()))