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 // 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
65HunspellClient::~HunspellClient()
66{
67}
68
69SpellerPlugin *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
86QStringList HunspellClient::languages() const
87{
88 return m_languagePaths.keys() + m_languageAliases.keys();
89}
90
91#include "moc_hunspellclient.cpp"
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
The sonnet namespace.
bool exists() const const
void append(QList< T > &&value)
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
QList< Key > keys() const const
T value(const Key &key, const T &defaultValue) const const
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-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.