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 | |
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 | |
25 | auto maybeAddPath = [&dirList](const QString &path) { |
26 | if (QFileInfo::exists(file: path)) { |
27 | dirList.append(t: path); |
28 | |
29 | QDir dir(path); |
30 | for (const QString &subDir : dir.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot)) { |
31 | dirList.append(t: dir.absoluteFilePath(fileName: subDir)); |
32 | } |
33 | } |
34 | }; |
35 | |
36 | const auto genericPaths = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("hunspell" ), options: QStandardPaths::LocateDirectory); |
37 | |
38 | for (const auto &p : genericPaths) { |
39 | maybeAddPath(p); |
40 | } |
41 | |
42 | const auto appLocalPaths = QStandardPaths::locateAll(type: QStandardPaths::AppLocalDataLocation, QStringLiteral("hunspell" ), options: 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(nameFilters: {QStringLiteral("*.aff" )}, filters: 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(key: language, value: alias); |
67 | continue; |
68 | } |
69 | } |
70 | m_languagePaths.insert(key: language, value: dict.canonicalPath()); |
71 | } |
72 | } |
73 | } |
74 | |
75 | HunspellClient::~HunspellClient() |
76 | { |
77 | } |
78 | |
79 | SpellerPlugin *HunspellClient::createSpeller(const QString &inputLang) |
80 | { |
81 | QString language = inputLang; |
82 | if (m_languageAliases.contains(key: language)) { |
83 | qCDebug(SONNET_HUNSPELL) << "Using alias" << m_languageAliases.value(key: language) << "for" << language; |
84 | language = m_languageAliases.value(key: language); |
85 | } |
86 | std::shared_ptr<Hunspell> hunspell = m_hunspellCache.value(key: language).lock(); |
87 | if (!hunspell) { |
88 | hunspell = HunspellDict::createHunspell(lang: language, path: m_languagePaths.value(key: language)); |
89 | m_hunspellCache.insert(key: language, value: hunspell); |
90 | } |
91 | qCDebug(SONNET_HUNSPELL) << " SpellerPlugin *HunspellClient::createSpeller(const QString &language) ;" << language; |
92 | HunspellDict *ad = new HunspellDict(inputLang, hunspell); |
93 | return ad; |
94 | } |
95 | |
96 | QStringList HunspellClient::languages() const |
97 | { |
98 | return m_languagePaths.keys() + m_languageAliases.keys(); |
99 | } |
100 | |
101 | #include "moc_hunspellclient.cpp" |
102 | |