Sonnet

ispellcheckerclient.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Christoph Cullmann <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "ispellcheckerclient.h"
8 #include "ispellcheckerdebug.h"
9 #include "ispellcheckerdict.h"
10 
11 using namespace Sonnet;
12 
13 ISpellCheckerClient::ISpellCheckerClient(QObject *parent)
14  : Client(parent)
15 {
16  qCDebug(SONNET_ISPELLCHECKER) << " ISpellCheckerClient::ISpellCheckerClient";
17 
18  // init com if needed, use same variant as e.g. Qt in qtbase/src/corelib/io/qfilesystemengine_win.cpp
19  CoInitialize(nullptr);
20 
21  // get factory & collect all known languages + instantiate the spell checkers for them
22  ISpellCheckerFactory *spellCheckerFactory = nullptr;
23  if (SUCCEEDED(CoCreateInstance(__uuidof(SpellCheckerFactory), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&spellCheckerFactory))) && spellCheckerFactory) {
24  // if we have a factory, cache the language names
25  IEnumString *enumLanguages = nullptr;
26  if (SUCCEEDED(spellCheckerFactory->get_SupportedLanguages(&enumLanguages))) {
27  HRESULT hr = S_OK;
28  while (S_OK == hr) {
29  LPOLESTR string = nullptr;
30  hr = enumLanguages->Next(1, &string, nullptr);
31  if (S_OK == hr) {
32  ISpellChecker *spellChecker = nullptr;
33  if (SUCCEEDED(spellCheckerFactory->CreateSpellChecker(string, &spellChecker)) && spellChecker) {
34  m_languages.insert(QString::fromWCharArray(string), spellChecker);
35  }
36  CoTaskMemFree(string);
37  }
38  }
39  enumLanguages->Release();
40  }
41  spellCheckerFactory->Release();
42  }
43 }
44 
45 ISpellCheckerClient::~ISpellCheckerClient()
46 {
47  // FIXME: we at the moment leak all checkers as sonnet does the cleanup to late for proper com cleanup :/
48 }
49 
50 SpellerPlugin *ISpellCheckerClient::createSpeller(const QString &language)
51 {
52  // create requested spellchecker if we know the language
53  qCDebug(SONNET_ISPELLCHECKER) << " SpellerPlugin *ISpellCheckerClient::createSpeller(const QString &language) ;" << language;
54  const auto it = m_languages.find(language);
55  if (it != m_languages.end()) {
56  return new ISpellCheckerDict(it.value(), language);
57  }
58  return nullptr;
59 }
60 
61 QStringList ISpellCheckerClient::languages() const
62 {
63  return m_languages.keys();
64 }
QString fromWCharArray(const wchar_t *string, int size)
The sonnet namespace.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.