1/*
2 * kspell_hunspellclient.cpp
3 *
4 * SPDX-FileCopyrightText: 2009 Montel Laurent <montel@kde.org>
5 * SPDX-FileCopyrightText: 2025 Harald Sitter <sitter@kde.org>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 */
9#include "hunspellclient.h"
10#include "hunspelldebug.h"
11#include "hunspelldict.h"
12
13#include <QDir>
14#include <QStandardPaths>
15#include <QString>
16#include <QVersionNumber>
17
18using namespace Sonnet;
19using namespace Qt::StringLiterals;
20
21namespace
22{
23
24[[nodiscard]] QStringList flatpakSources()
25{
26 QMap<QVersionNumber, QString> versionedPaths;
27 // TODO: could load all paths from libflatpak. For now we hardcode the known ones.
28 for (const auto &flatpakPath :
29 {u"/var/lib/flatpak/runtime/org.kde.Platform.Locale/"_s,
30 QStandardPaths::locate(type: QStandardPaths::GenericDataLocation, fileName: u"flatpak/runtime/org.kde.Platform.Locale"_s, options: QStandardPaths::LocateDirectory)}) {
31 for (const auto &architectureEntry : QDirListing(flatpakPath, QDirListing::IteratorFlag::DirsOnly)) {
32 const auto &architecturePath = architectureEntry.filePath();
33 for (const auto &versionEntry : QDirListing(architecturePath, QDirListing::IteratorFlag::DirsOnly)) {
34 const auto filesPath = versionEntry.filePath() + "/active/files/"_L1;
35 if (!QFileInfo::exists(file: filesPath)) {
36 continue;
37 }
38
39 versionedPaths.insert(key: QVersionNumber::fromString(string: versionEntry.fileName()), value: filesPath);
40 }
41 }
42 }
43
44 if (versionedPaths.isEmpty()) {
45 qCDebug(SONNET_HUNSPELL) << "No flatpak hunspell location found";
46 return {};
47 }
48
49 const QString latestActiveVersionPath = versionedPaths.values().last();
50 qCDebug(SONNET_HUNSPELL) << "Found flatpak location" << latestActiveVersionPath;
51
52 QStringList sources;
53 for (const auto &parentLanguageEntry : QDirListing(latestActiveVersionPath, QDirListing::IteratorFlag::DirsOnly)) {
54 const auto &parentLanguagePath = parentLanguageEntry.filePath() + "/share/"_L1;
55 for (const auto &languageEntry : QDirListing(parentLanguagePath, QDirListing::IteratorFlag::DirsOnly)) {
56 sources.append(t: languageEntry.filePath() + "/hunspell/"_L1);
57 }
58 }
59 return sources;
60}
61
62} // namespace
63
64HunspellClient::HunspellClient(QObject *parent)
65 : Client(parent)
66{
67 qCDebug(SONNET_HUNSPELL) << " HunspellClient::HunspellClient";
68
69 QStringList dirList;
70
71 auto maybeAddPath = [&dirList](const QString &path) {
72 if (QFileInfo::exists(file: path)) {
73 dirList.append(t: path);
74
75 QDir dir(path);
76 for (const QString &subDir : dir.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot)) {
77 dirList.append(t: dir.absoluteFilePath(fileName: subDir));
78 }
79 }
80 };
81
82 const auto genericPaths = QStandardPaths::locateAll(type: QStandardPaths::GenericDataLocation, QStringLiteral("hunspell"), options: QStandardPaths::LocateDirectory);
83
84 for (const auto &p : genericPaths) {
85 maybeAddPath(p);
86 }
87
88 const auto appLocalPaths = QStandardPaths::locateAll(type: QStandardPaths::AppLocalDataLocation, QStringLiteral("hunspell"), options: QStandardPaths::LocateDirectory);
89
90 for (const auto &p : appLocalPaths) {
91 maybeAddPath(p);
92 }
93
94#ifdef Q_OS_WIN
95 maybeAddPath(QStringLiteral(SONNET_INSTALL_PREFIX "/bin/data/hunspell/"));
96#else
97 maybeAddPath(QStringLiteral("/System/Library/Spelling"));
98 maybeAddPath(QStringLiteral("/usr/share/hunspell/"));
99 maybeAddPath(QStringLiteral("/usr/share/myspell/"));
100 // Load supplementary dictionaries from our flatpak platform if available
101 for (const auto &flatpakSource : flatpakSources()) {
102 maybeAddPath(flatpakSource);
103 }
104#endif
105
106 for (const QString &dirString : dirList) {
107 QDir dir(dirString);
108 const QList<QFileInfo> dicts = dir.entryInfoList(nameFilters: {QStringLiteral("*.aff")}, filters: QDir::Files);
109 for (const QFileInfo &dict : dicts) {
110 const QString language = dict.baseName();
111 if (dict.isSymbolicLink()) {
112 const QFileInfo actualDict(dict.canonicalFilePath());
113 const QString alias = actualDict.baseName();
114 if (language != alias) {
115 qCDebug(SONNET_HUNSPELL) << "Found alias" << language << "->" << alias;
116 m_languageAliases.insert(key: language, value: alias);
117
118 if (!m_languagePaths.contains(key: language)) {
119 m_languagePaths.insert(key: alias, value: actualDict.canonicalPath());
120 }
121
122 continue;
123 }
124 }
125 m_languagePaths.insert(key: language, value: dict.canonicalPath());
126 }
127 }
128}
129
130HunspellClient::~HunspellClient()
131{
132}
133
134SpellerPlugin *HunspellClient::createSpeller(const QString &inputLang)
135{
136 QString language = inputLang;
137 if (m_languageAliases.contains(key: language)) {
138 qCDebug(SONNET_HUNSPELL) << "Using alias" << m_languageAliases.value(key: language) << "for" << language;
139 language = m_languageAliases.value(key: language);
140 }
141 std::shared_ptr<Hunspell> hunspell = m_hunspellCache.value(key: language).lock();
142 if (!hunspell) {
143 hunspell = HunspellDict::createHunspell(lang: language, path: m_languagePaths.value(key: language));
144 m_hunspellCache.insert(key: language, value: hunspell);
145 }
146 qCDebug(SONNET_HUNSPELL) << " SpellerPlugin *HunspellClient::createSpeller(const QString &language) ;" << language;
147 HunspellDict *ad = new HunspellDict(inputLang, hunspell);
148 return ad;
149}
150
151QStringList HunspellClient::languages() const
152{
153 return m_languagePaths.keys() + m_languageAliases.keys();
154}
155
156#include "moc_hunspellclient.cpp"
157

source code of sonnet/src/plugins/hunspell/hunspellclient.cpp