File indexing completed on 2024-05-05 04:01:24

0001 /*
0002     SPDX-FileCopyrightText: 2019 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ispellcheckerdict.h"
0008 #include "ispellcheckerdebug.h"
0009 
0010 using namespace Sonnet;
0011 
0012 ISpellCheckerDict::ISpellCheckerDict(ISpellChecker *spellChecker, const QString &language)
0013     : SpellerPlugin(language)
0014     , m_spellChecker(spellChecker)
0015 {
0016     Q_ASSERT(m_spellChecker);
0017 }
0018 
0019 ISpellCheckerDict::~ISpellCheckerDict()
0020 {
0021     // we don't own m_spellChecker!
0022 }
0023 
0024 bool ISpellCheckerDict::isCorrect(const QString &word) const
0025 {
0026     // check if we are incorrect, we only need to check one enum entry for that, only empty enum means OK
0027     bool ok = true;
0028     IEnumSpellingError *enumSpellingError = nullptr;
0029     if (SUCCEEDED(m_spellChecker->Check(word.toStdWString().c_str(), &enumSpellingError))) {
0030         ISpellingError *spellingError = nullptr;
0031         if (S_OK == enumSpellingError->Next(&spellingError)) {
0032             ok = false;
0033             spellingError->Release();
0034         }
0035         enumSpellingError->Release();
0036     }
0037     return ok;
0038 }
0039 
0040 QStringList ISpellCheckerDict::suggest(const QString &word) const
0041 {
0042     // query suggestions
0043     QStringList replacements;
0044     IEnumString *words = nullptr;
0045     if (SUCCEEDED(m_spellChecker->Suggest(word.toStdWString().c_str(), &words))) {
0046         HRESULT hr = S_OK;
0047         while (S_OK == hr) {
0048             LPOLESTR string = nullptr;
0049             hr = words->Next(1, &string, nullptr);
0050             if (S_OK == hr) {
0051                 replacements.push_back(QString::fromWCharArray(string));
0052                 CoTaskMemFree(string);
0053             }
0054         }
0055         words->Release();
0056     }
0057     return replacements;
0058 }
0059 
0060 bool ISpellCheckerDict::storeReplacement(const QString &bad, const QString &good)
0061 {
0062     Q_UNUSED(bad);
0063     Q_UNUSED(good);
0064     qCDebug(SONNET_ISPELLCHECKER) << "ISpellCheckerDict::storeReplacement not implemented";
0065     return false;
0066 }
0067 
0068 bool ISpellCheckerDict::addToPersonal(const QString &word)
0069 {
0070     // add word "permanently" to the dictionary
0071     return SUCCEEDED(m_spellChecker->Add(word.toStdWString().c_str()));
0072 }
0073 
0074 bool ISpellCheckerDict::addToSession(const QString &word)
0075 {
0076     // ignore word for this session
0077     return SUCCEEDED(m_spellChecker->Ignore(word.toStdWString().c_str()));
0078 }