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
40namespace WebCore {
41
42void TextCheckerClientQt::ignoreWordInSpellDocument(const String& word)
43{
44 if (!loadSpellChecker())
45 return;
46
47 m_spellChecker->ignoreWordInSpellDocument(word);
48}
49
50void TextCheckerClientQt::learnWord(const String& word)
51{
52 if (!loadSpellChecker())
53 return;
54
55 m_spellChecker->learnWord(word);
56}
57
58String TextCheckerClientQt::getAutoCorrectSuggestionForMisspelledWord(const String& misspelledWord)
59{
60 if (!loadSpellChecker())
61 return String();
62
63 return m_spellChecker->autoCorrectSuggestionForMisspelledWord(misspelledWord);
64}
65
66void TextCheckerClientQt::checkSpellingOfString(const UChar* buffer, int length, int* misspellingLocation, int* misspellingLength)
67{
68 if (!loadSpellChecker())
69 return;
70
71 const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
72 m_spellChecker->checkSpellingOfString(text, misspellingLocation, misspellingLength);
73}
74
75void TextCheckerClientQt::checkGrammarOfString(const UChar* buffer, int length, Vector<GrammarDetail>& details, int* badGrammarLocation, int* badGrammarLength)
76{
77 if (!loadSpellChecker())
78 return;
79
80 const QString text = QString::fromRawData(reinterpret_cast<const QChar*>(buffer), length);
81
82 // Do Grammer check.
83 QVector<QWebSpellChecker::GrammarDetail> qWebDetails;
84 m_spellChecker->checkGrammarOfString(text, qWebDetails, badGrammarLocation, badGrammarLength);
85
86 // Copy qWebDetails to details.
87 details.clear();
88 QVector<QWebSpellChecker::GrammarDetail>::iterator qWebIt = qWebDetails.begin();
89 QVector<QWebSpellChecker::GrammarDetail>::iterator qWebEnd = qWebDetails.end();
90 for (; qWebIt < qWebEnd; qWebIt++) {
91 GrammarDetail grammarDetail;
92 grammarDetail.location = qWebIt->location;
93 grammarDetail.length = qWebIt->length;
94 // Copy guesses strings.
95 QStringList::iterator guessIt = qWebIt->guesses.begin();
96 QStringList::iterator guessEnd = qWebIt->guesses.end();
97 for (;guessIt < guessEnd; guessIt++)
98 grammarDetail.guesses.append(*guessIt);
99 grammarDetail.userDescription = qWebIt->userDescription;
100 details.append(grammarDetail);
101 }
102}
103
104void TextCheckerClientQt::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
105{
106 if (!loadSpellChecker())
107 return;
108
109 QStringList guessesList;
110 m_spellChecker->guessesForWord(word, context, guessesList);
111 const int count = guessesList.count();
112 guesses.resize(count);
113 for (int i = 0; i < count; ++i)
114 guesses.append(guessesList.at(i));
115}
116
117bool TextCheckerClientQt::isContinousSpellCheckingEnabled()
118{
119 if (!loadSpellChecker())
120 return false;
121
122 return m_spellChecker->isContinousSpellCheckingEnabled();
123}
124
125void TextCheckerClientQt::toggleContinousSpellChecking()
126{
127 if (!loadSpellChecker())
128 return;
129
130 m_spellChecker->toggleContinousSpellChecking();
131}
132
133bool TextCheckerClientQt::isGrammarCheckingEnabled()
134{
135 if (!loadSpellChecker())
136 return false;
137
138 return m_spellChecker->isGrammarCheckingEnabled();
139}
140
141void TextCheckerClientQt::toggleGrammarChecking()
142{
143 if (!loadSpellChecker())
144 return;
145
146 m_spellChecker->toggleGrammarChecking();
147}
148
149bool TextCheckerClientQt::loadSpellChecker()
150{
151 if (m_spellChecker)
152 return true;
153
154 QtPlatformPlugin platformPlugin;
155 if ((m_spellChecker = platformPlugin.createSpellChecker()))
156 return true;
157
158 return false;
159}
160
161}