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