Sonnet

hunspellclient.cpp
1 /*
2  * kspell_hunspellclient.cpp
3  *
4  * SPDX-FileCopyrightText: 2009 Montel Laurent <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.1-or-later
7  */
8 #include "hunspellclient.h"
9 #include "hunspelldebug.h"
10 #include "hunspelldict.h"
11 
12 #include <QDir>
13 #include <QStandardPaths>
14 #include <QString>
15 
16 using namespace Sonnet;
17 
18 HunspellClient::HunspellClient(QObject *parent)
19  : Client(parent)
20 {
21  qCDebug(SONNET_HUNSPELL) << " HunspellClient::HunspellClient";
22 
23  QStringList dirList;
24  // search QStandardPaths
26 
27  auto maybeAddPath = [&dirList](const QString &path) {
28  if (QFileInfo::exists(path)) {
29  dirList.append(path);
30 
31  QDir dir(path);
32  for (const QString &subDir : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
33  dirList.append(dir.absoluteFilePath(subDir));
34  }
35  }
36  };
37 #ifdef Q_OS_WIN
38  maybeAddPath(QStringLiteral(SONNET_INSTALL_PREFIX "/bin/data/hunspell/"));
39 #else
40  maybeAddPath(QStringLiteral("/System/Library/Spelling"));
41  maybeAddPath(QStringLiteral("/usr/share/hunspell/"));
42  maybeAddPath(QStringLiteral("/usr/share/myspell/"));
43 #endif
44 
45  for (const QString &dirString : dirList) {
46  QDir dir(dirString);
47  const QList<QFileInfo> dicts = dir.entryInfoList({QStringLiteral("*.aff")}, QDir::Files);
48  for (const QFileInfo &dict : dicts) {
49  const QString language = dict.baseName();
50  if (dict.isSymbolicLink()) {
51  const QFileInfo actualDict(dict.canonicalFilePath());
52  const QString alias = actualDict.baseName();
53  if (language != alias) {
54  qCDebug(SONNET_HUNSPELL) << "Found alias" << language << "->" << alias;
55  m_languageAliases.insert(language, alias);
56  continue;
57  }
58  } else {
59  m_languagePaths.insert(language, dict.canonicalPath());
60  }
61  }
62  }
63 }
64 
65 HunspellClient::~HunspellClient()
66 {
67 }
68 
69 SpellerPlugin *HunspellClient::createSpeller(const QString &inputLang)
70 {
71  QString language = inputLang;
72  if (m_languageAliases.contains(language)) {
73  qCDebug(SONNET_HUNSPELL) << "Using alias" << m_languageAliases.value(language) << "for" << language;
74  language = m_languageAliases.value(language);
75  }
76  std::shared_ptr<Hunspell> hunspell = m_hunspellCache.value(language).lock();
77  if (!hunspell) {
78  hunspell = HunspellDict::createHunspell(language, m_languagePaths.value(language));
79  m_hunspellCache.insert(language, hunspell);
80  }
81  qCDebug(SONNET_HUNSPELL) << " SpellerPlugin *HunspellClient::createSpeller(const QString &language) ;" << language;
82  HunspellDict *ad = new HunspellDict(inputLang, hunspell);
83  return ad;
84 }
85 
86 QStringList HunspellClient::languages() const
87 {
88  return m_languagePaths.keys() + m_languageAliases.keys();
89 }
void append(const T &value)
bool exists() const const
The sonnet namespace.
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
QString & insert(int position, QChar ch)
QStringList locateAll(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
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.