Changeset View
Changeset View
Standalone View
Standalone View
src/plugins/ispellchecker/ispellcheckerdict.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | SPDX-FileCopyrightText: 2019 Christoph Cullmann <cullmann@kde.org> | 2 | SPDX-FileCopyrightText: 2019 Christoph Cullmann <cullmann@kde.org> | ||
3 | 3 | | |||
4 | SPDX-License-Identifier: LGPL-2.0-or-later | 4 | SPDX-License-Identifier: LGPL-2.0-or-later | ||
5 | */ | 5 | */ | ||
6 | 6 | | |||
7 | #include "ispellcheckerdict.h" | 7 | #include "ispellcheckerdict.h" | ||
8 | #include "ispellcheckerdebug.h" | 8 | #include "ispellcheckerdebug.h" | ||
9 | 9 | | |||
10 | using namespace Sonnet; | 10 | using namespace Sonnet; | ||
11 | 11 | | |||
12 | ISpellCheckerDict::ISpellCheckerDict(ISpellCheckerFactory *spellCheckerFactory, const QString &language) | 12 | ISpellCheckerDict::ISpellCheckerDict(ISpellChecker *spellChecker, const QString &language) | ||
13 | : SpellerPlugin(language) | 13 | : SpellerPlugin(language) | ||
14 | , m_spellChecker(spellChecker) | ||||
14 | { | 15 | { | ||
15 | // try to init checker | 16 | Q_ASSERT(m_spellChecker); | ||
16 | if (!SUCCEEDED(spellCheckerFactory->CreateSpellChecker(language.toStdWString().c_str(), &m_spellChecker))) { | | |||
17 | m_spellChecker = nullptr; | | |||
18 | } | | |||
19 | } | 17 | } | ||
20 | 18 | | |||
21 | ISpellCheckerDict::~ISpellCheckerDict() | 19 | ISpellCheckerDict::~ISpellCheckerDict() | ||
22 | { | 20 | { | ||
23 | // release com if needed | 21 | // we don't own m_spellChecker! | ||
24 | if (m_spellChecker) { | | |||
25 | m_spellChecker->Release(); | | |||
26 | } | | |||
27 | } | 22 | } | ||
28 | 23 | | |||
29 | bool ISpellCheckerDict::isCorrect(const QString &word) const | 24 | bool ISpellCheckerDict::isCorrect(const QString &word) const | ||
30 | { | 25 | { | ||
31 | // check if we are incorrect, we only need to check one enum entry for that, only empty enum means OK | 26 | // check if we are incorrect, we only need to check one enum entry for that, only empty enum means OK | ||
32 | bool ok = true; | 27 | bool ok = true; | ||
33 | IEnumSpellingError* enumSpellingError = nullptr; | 28 | IEnumSpellingError* enumSpellingError = nullptr; | ||
34 | if (m_spellChecker && SUCCEEDED(m_spellChecker->Check(word.toStdWString().c_str(), &enumSpellingError))) { | 29 | if (SUCCEEDED(m_spellChecker->Check(word.toStdWString().c_str(), &enumSpellingError))) { | ||
35 | ISpellingError *spellingError = nullptr; | 30 | ISpellingError *spellingError = nullptr; | ||
36 | if (S_OK == enumSpellingError->Next(&spellingError)) { | 31 | if (S_OK == enumSpellingError->Next(&spellingError)) { | ||
37 | ok = false; | 32 | ok = false; | ||
38 | spellingError->Release(); | 33 | spellingError->Release(); | ||
39 | } | 34 | } | ||
40 | enumSpellingError->Release(); | 35 | enumSpellingError->Release(); | ||
41 | } | 36 | } | ||
42 | return ok; | 37 | return ok; | ||
43 | } | 38 | } | ||
44 | 39 | | |||
45 | QStringList ISpellCheckerDict::suggest(const QString &word) const | 40 | QStringList ISpellCheckerDict::suggest(const QString &word) const | ||
46 | { | 41 | { | ||
47 | // query suggestions | 42 | // query suggestions | ||
48 | QStringList replacements; | 43 | QStringList replacements; | ||
49 | IEnumString* words = nullptr; | 44 | IEnumString* words = nullptr; | ||
50 | if (m_spellChecker && SUCCEEDED(m_spellChecker->Suggest(word.toStdWString().c_str(), &words))) { | 45 | if (SUCCEEDED(m_spellChecker->Suggest(word.toStdWString().c_str(), &words))) { | ||
51 | HRESULT hr = S_OK; | 46 | HRESULT hr = S_OK; | ||
52 | while (S_OK == hr) { | 47 | while (S_OK == hr) { | ||
53 | LPOLESTR string = nullptr; | 48 | LPOLESTR string = nullptr; | ||
54 | hr = words->Next(1, &string, nullptr); | 49 | hr = words->Next(1, &string, nullptr); | ||
55 | if (S_OK == hr) { | 50 | if (S_OK == hr) { | ||
56 | replacements.push_back(QString::fromWCharArray(string)); | 51 | replacements.push_back(QString::fromWCharArray(string)); | ||
57 | CoTaskMemFree(string); | 52 | CoTaskMemFree(string); | ||
58 | } | 53 | } | ||
Show All 9 Lines | 61 | { | |||
68 | Q_UNUSED(good); | 63 | Q_UNUSED(good); | ||
69 | qCDebug(SONNET_ISPELLCHECKER) << "ISpellCheckerDict::storeReplacement not implemented"; | 64 | qCDebug(SONNET_ISPELLCHECKER) << "ISpellCheckerDict::storeReplacement not implemented"; | ||
70 | return false; | 65 | return false; | ||
71 | } | 66 | } | ||
72 | 67 | | |||
73 | bool ISpellCheckerDict::addToPersonal(const QString &word) | 68 | bool ISpellCheckerDict::addToPersonal(const QString &word) | ||
74 | { | 69 | { | ||
75 | // add word "permanently" to the dictionary | 70 | // add word "permanently" to the dictionary | ||
76 | return m_spellChecker && SUCCEEDED(m_spellChecker->Add(word.toStdWString().c_str())); | 71 | return SUCCEEDED(m_spellChecker->Add(word.toStdWString().c_str())); | ||
77 | } | 72 | } | ||
78 | 73 | | |||
79 | bool ISpellCheckerDict::addToSession(const QString &word) | 74 | bool ISpellCheckerDict::addToSession(const QString &word) | ||
80 | { | 75 | { | ||
81 | // ignore word for this session | 76 | // ignore word for this session | ||
82 | return m_spellChecker && SUCCEEDED(m_spellChecker->Ignore(word.toStdWString().c_str())); | 77 | return SUCCEEDED(m_spellChecker->Ignore(word.toStdWString().c_str())); | ||
83 | } | 78 | } |