Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/ispellchecker/ispellcheckerdict.cpp
- This file was added.
1 | /* SPDX-License-Identifier: LGPL-2.0-or-later | ||||
---|---|---|---|---|---|
2 | | ||||
3 | Copyright (C) 2019 Christoph Cullmann <cullmann@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Library General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "ispellcheckerdict.h" | ||||
22 | #include "ispellcheckerdebug.h" | ||||
23 | | ||||
24 | using namespace Sonnet; | ||||
25 | | ||||
26 | ISpellCheckerDict::ISpellCheckerDict(ISpellCheckerFactory *spellCheckerFactory, const QString &language) | ||||
27 | : SpellerPlugin(language) | ||||
28 | { | ||||
29 | // try to init checker | ||||
30 | if (!SUCCEEDED(spellCheckerFactory->CreateSpellChecker(language.toStdWString().c_str(), &m_spellChecker))) { | ||||
31 | m_spellChecker = nullptr; | ||||
32 | } | ||||
33 | } | ||||
34 | | ||||
35 | ISpellCheckerDict::~ISpellCheckerDict() | ||||
36 | { | ||||
37 | // release com if needed | ||||
38 | if (m_spellChecker) { | ||||
39 | m_spellChecker->Release(); | ||||
40 | } | ||||
41 | } | ||||
42 | | ||||
43 | bool ISpellCheckerDict::isCorrect(const QString &word) const | ||||
44 | { | ||||
45 | // check if we are incorrect, we only need to check one enum entry for that, only empty enum means OK | ||||
46 | bool ok = true; | ||||
47 | IEnumSpellingError* enumSpellingError = nullptr; | ||||
48 | if (m_spellChecker && SUCCEEDED(m_spellChecker->Check(word.toStdWString().c_str(), &enumSpellingError))) { | ||||
49 | ISpellingError *spellingError = nullptr; | ||||
50 | if (S_OK == enumSpellingError->Next(&spellingError)) { | ||||
51 | ok = false; | ||||
52 | spellingError->Release(); | ||||
53 | } | ||||
54 | enumSpellingError->Release(); | ||||
55 | } | ||||
56 | return ok; | ||||
57 | } | ||||
58 | | ||||
59 | QStringList ISpellCheckerDict::suggest(const QString &word) const | ||||
60 | { | ||||
61 | // query suggestions | ||||
62 | QStringList replacements; | ||||
63 | IEnumString* words = nullptr; | ||||
64 | if (m_spellChecker && SUCCEEDED(m_spellChecker->Suggest(word.toStdWString().c_str(), &words))) { | ||||
65 | HRESULT hr = S_OK; | ||||
66 | while (S_OK == hr) { | ||||
67 | LPOLESTR string = nullptr; | ||||
68 | hr = words->Next(1, &string, nullptr); | ||||
69 | if (S_OK == hr) { | ||||
70 | replacements.push_back(QString::fromWCharArray(string)); | ||||
71 | CoTaskMemFree(string); | ||||
72 | } | ||||
73 | } | ||||
74 | words->Release(); | ||||
75 | } | ||||
76 | return replacements; | ||||
77 | } | ||||
78 | | ||||
79 | bool ISpellCheckerDict::storeReplacement(const QString &bad, const QString &good) | ||||
80 | { | ||||
81 | Q_UNUSED(bad); | ||||
82 | Q_UNUSED(good); | ||||
83 | qCDebug(SONNET_ISPELLCHECKER) << "ISpellCheckerDict::storeReplacement not implemented"; | ||||
84 | return false; | ||||
85 | } | ||||
86 | | ||||
87 | bool ISpellCheckerDict::addToPersonal(const QString &word) | ||||
88 | { | ||||
89 | // add word "permanently" to the dictionary | ||||
90 | return m_spellChecker && SUCCEEDED(m_spellChecker->Add(word.toStdWString().c_str())); | ||||
91 | } | ||||
92 | | ||||
93 | bool ISpellCheckerDict::addToSession(const QString &word) | ||||
94 | { | ||||
95 | // ignore word for this session | ||||
96 | return m_spellChecker && SUCCEEDED(m_spellChecker->Ignore(word.toStdWString().c_str())); | ||||
97 | } |