File indexing completed on 2024-05-05 04:01:24

0001 /*
0002  * kspell_hunspellclient.cpp
0003  *
0004  * SPDX-FileCopyrightText: 2009 Montel Laurent <montel@kde.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 #include "hunspellclient.h"
0009 #include "hunspelldebug.h"
0010 #include "hunspelldict.h"
0011 
0012 #include <QDir>
0013 #include <QStandardPaths>
0014 #include <QString>
0015 
0016 using namespace Sonnet;
0017 
0018 HunspellClient::HunspellClient(QObject *parent)
0019     : Client(parent)
0020 {
0021     qCDebug(SONNET_HUNSPELL) << " HunspellClient::HunspellClient";
0022 
0023     QStringList dirList;
0024     // search QStandardPaths
0025     dirList.append(QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("hunspell"), QStandardPaths::LocateDirectory));
0026 
0027     auto maybeAddPath = [&dirList](const QString &path) {
0028         if (QFileInfo::exists(path)) {
0029             dirList.append(path);
0030 
0031             QDir dir(path);
0032             for (const QString &subDir : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
0033                 dirList.append(dir.absoluteFilePath(subDir));
0034             }
0035         }
0036     };
0037 #ifdef Q_OS_WIN
0038     maybeAddPath(QStringLiteral(SONNET_INSTALL_PREFIX "/bin/data/hunspell/"));
0039 #else
0040     maybeAddPath(QStringLiteral("/System/Library/Spelling"));
0041     maybeAddPath(QStringLiteral("/usr/share/hunspell/"));
0042     maybeAddPath(QStringLiteral("/usr/share/myspell/"));
0043 #endif
0044 
0045     for (const QString &dirString : dirList) {
0046         QDir dir(dirString);
0047         const QList<QFileInfo> dicts = dir.entryInfoList({QStringLiteral("*.aff")}, QDir::Files);
0048         for (const QFileInfo &dict : dicts) {
0049             const QString language = dict.baseName();
0050             if (dict.isSymbolicLink()) {
0051                 const QFileInfo actualDict(dict.canonicalFilePath());
0052                 const QString alias = actualDict.baseName();
0053                 if (language != alias) {
0054                     qCDebug(SONNET_HUNSPELL) << "Found alias" << language << "->" << alias;
0055                     m_languageAliases.insert(language, alias);
0056                     continue;
0057                 }
0058             } else {
0059                 m_languagePaths.insert(language, dict.canonicalPath());
0060             }
0061         }
0062     }
0063 }
0064 
0065 HunspellClient::~HunspellClient()
0066 {
0067 }
0068 
0069 SpellerPlugin *HunspellClient::createSpeller(const QString &inputLang)
0070 {
0071     QString language = inputLang;
0072     if (m_languageAliases.contains(language)) {
0073         qCDebug(SONNET_HUNSPELL) << "Using alias" << m_languageAliases.value(language) << "for" << language;
0074         language = m_languageAliases.value(language);
0075     }
0076     std::shared_ptr<Hunspell> hunspell = m_hunspellCache.value(language).lock();
0077     if (!hunspell) {
0078         hunspell = HunspellDict::createHunspell(language, m_languagePaths.value(language));
0079         m_hunspellCache.insert(language, hunspell);
0080     }
0081     qCDebug(SONNET_HUNSPELL) << " SpellerPlugin *HunspellClient::createSpeller(const QString &language) ;" << language;
0082     HunspellDict *ad = new HunspellDict(inputLang, hunspell);
0083     return ad;
0084 }
0085 
0086 QStringList HunspellClient::languages() const
0087 {
0088     return m_languagePaths.keys() + m_languageAliases.keys();
0089 }
0090 
0091 #include "moc_hunspellclient.cpp"