Sonnet

hunspellclient.cpp
1/*
2 * kspell_hunspellclient.cpp
3 *
4 * SPDX-FileCopyrightText: 2009 Montel Laurent <montel@kde.org>
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
16using namespace Sonnet;
17
18HunspellClient::HunspellClient(QObject *parent)
19 : Client(parent)
20{
21 qCDebug(SONNET_HUNSPELL) << " HunspellClient::HunspellClient";
22
23 QStringList dirList;
24
25 auto maybeAddPath = [&dirList](const QString &path) {
26 if (QFileInfo::exists(path)) {
27 dirList.append(path);
28
29 QDir dir(path);
30 for (const QString &subDir : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
31 dirList.append(dir.absoluteFilePath(subDir));
32 }
33 }
34 };
35
36 const auto genericPaths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("hunspell"), QStandardPaths::LocateDirectory);
37
38 for (const auto &p : genericPaths) {
39 maybeAddPath(p);
40 }
41
42 const auto appLocalPaths = QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, QStringLiteral("hunspell"), QStandardPaths::LocateDirectory);
43
44 for (const auto &p : appLocalPaths) {
45 maybeAddPath(p);
46 }
47
48#ifdef Q_OS_WIN
49 maybeAddPath(QStringLiteral(SONNET_INSTALL_PREFIX "/bin/data/hunspell/"));
50#else
51 maybeAddPath(QStringLiteral("/System/Library/Spelling"));
52 maybeAddPath(QStringLiteral("/usr/share/hunspell/"));
53 maybeAddPath(QStringLiteral("/usr/share/myspell/"));
54#endif
55
56 for (const QString &dirString : dirList) {
57 QDir dir(dirString);
58 const QList<QFileInfo> dicts = dir.entryInfoList({QStringLiteral("*.aff")}, QDir::Files);
59 for (const QFileInfo &dict : dicts) {
60 const QString language = dict.baseName();
61 if (dict.isSymbolicLink()) {
62 const QFileInfo actualDict(dict.canonicalFilePath());
63 const QString alias = actualDict.baseName();
64 if (language != alias) {
65 qCDebug(SONNET_HUNSPELL) << "Found alias" << language << "->" << alias;
66 m_languageAliases.insert(language, alias);
67 continue;
68 }
69 } else {
70 m_languagePaths.insert(language, dict.canonicalPath());
71 }
72 }
73 }
74}
75
76HunspellClient::~HunspellClient()
77{
78}
79
80SpellerPlugin *HunspellClient::createSpeller(const QString &inputLang)
81{
82 QString language = inputLang;
83 if (m_languageAliases.contains(language)) {
84 qCDebug(SONNET_HUNSPELL) << "Using alias" << m_languageAliases.value(language) << "for" << language;
85 language = m_languageAliases.value(language);
86 }
87 std::shared_ptr<Hunspell> hunspell = m_hunspellCache.value(language).lock();
88 if (!hunspell) {
89 hunspell = HunspellDict::createHunspell(language, m_languagePaths.value(language));
90 m_hunspellCache.insert(language, hunspell);
91 }
92 qCDebug(SONNET_HUNSPELL) << " SpellerPlugin *HunspellClient::createSpeller(const QString &language) ;" << language;
93 HunspellDict *ad = new HunspellDict(inputLang, hunspell);
94 return ad;
95}
96
97QStringList HunspellClient::languages() const
98{
99 return m_languagePaths.keys() + m_languageAliases.keys();
100}
101
102#include "moc_hunspellclient.cpp"
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
The sonnet namespace.
bool exists(const QString &path)
void append(QList< T > &&value)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
QString & insert(qsizetype position, QChar ch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:22 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.