1/*
2 * kspell_hunspelldict.cpp
3 *
4 * SPDX-FileCopyrightText: 2009 Montel Laurent <montel@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8
9#include "hunspelldict.h"
10
11#include "config-hunspell.h"
12#include "hunspelldebug.h"
13
14#include <QDir>
15#include <QFile>
16#include <QFileInfo>
17#include <QStandardPaths>
18#include <QTextStream>
19
20using namespace Sonnet;
21
22HunspellDict::HunspellDict(const QString &lang, const std::shared_ptr<Hunspell> &speller)
23 : SpellerPlugin(lang)
24{
25 if (!speller) {
26 qCWarning(SONNET_HUNSPELL) << "Can't create a client without a speller";
27 return;
28 }
29 m_decoder = QStringDecoder(speller->get_dic_encoding());
30 if (!m_decoder.isValid()) {
31 qCWarning(SONNET_HUNSPELL) << "Failed to find a text codec for name" << speller->get_dic_encoding() << "defaulting to locale text codec";
32 m_decoder = QStringDecoder(QStringDecoder::System);
33 Q_ASSERT(m_decoder.isValid());
34 }
35 m_encoder = QStringEncoder(speller->get_dic_encoding());
36 if (!m_encoder.isValid()) {
37 qCWarning(SONNET_HUNSPELL) << "Failed to find a text codec for name" << speller->get_dic_encoding() << "defaulting to locale text codec";
38 m_encoder = QStringEncoder(QStringEncoder::System);
39 Q_ASSERT(m_encoder.isValid());
40 }
41 m_speller = speller;
42
43 const QString userDic = QDir::home().filePath(fileName: QLatin1String(".hunspell_") % lang);
44 QFile userDicFile(userDic);
45 if (userDicFile.open(flags: QIODevice::ReadOnly | QIODevice::Text)) {
46 qCDebug(SONNET_HUNSPELL) << "Load a user dictionary" << userDic;
47 QTextStream userDicIn(&userDicFile);
48 while (!userDicIn.atEnd()) {
49 const QString word = userDicIn.readLine();
50 if (word.isEmpty()) {
51 continue;
52 }
53
54 if (word.contains(c: QLatin1Char('/'))) {
55 QStringList wordParts = word.split(sep: QLatin1Char('/'));
56 speller->add_with_affix(word: toDictEncoding(word: wordParts.at(i: 0)).constData(), example: toDictEncoding(word: wordParts.at(i: 1)).constData());
57 }
58 if (word.at(i: 0) == QLatin1Char('*')) {
59 speller->remove(word: toDictEncoding(word: word.mid(position: 1)).constData());
60 } else {
61 speller->add(word: toDictEncoding(word).constData());
62 }
63 }
64 userDicFile.close();
65 }
66}
67
68std::shared_ptr<Hunspell> HunspellDict::createHunspell(const QString &lang, QString path)
69{
70 qCDebug(SONNET_HUNSPELL) << "Loading dictionary for" << lang << "from" << path;
71
72 if (!path.endsWith(c: QLatin1Char('/'))) {
73 path += QLatin1Char('/');
74 }
75 path += lang;
76 QString dictionary = path + QStringLiteral(".dic");
77 QString aff = path + QStringLiteral(".aff");
78
79 if (!QFileInfo::exists(file: dictionary) || !QFileInfo::exists(file: aff)) {
80 qCWarning(SONNET_HUNSPELL) << "Unable to find dictionary for" << lang << "in path" << path;
81 return nullptr;
82 }
83
84 std::shared_ptr<Hunspell> speller = std::make_shared<Hunspell>(args: aff.toLocal8Bit().constData(), args: dictionary.toLocal8Bit().constData());
85 qCDebug(SONNET_HUNSPELL) << "Created " << speller.get();
86
87 return speller;
88}
89
90HunspellDict::~HunspellDict()
91{
92}
93
94QByteArray HunspellDict::toDictEncoding(const QString &word) const
95{
96 if (m_encoder.isValid()) {
97 return m_encoder.encode(str: word);
98 }
99 return {};
100}
101
102bool HunspellDict::isCorrect(const QString &word) const
103{
104 qCDebug(SONNET_HUNSPELL) << " isCorrect :" << word;
105 if (!m_speller) {
106 return false;
107 }
108
109#if USE_OLD_HUNSPELL_API
110 int result = m_speller->spell(toDictEncoding(word).constData());
111 qCDebug(SONNET_HUNSPELL) << " result :" << result;
112 return result != 0;
113#else
114 bool result = m_speller->spell(word: toDictEncoding(word).toStdString());
115 qCDebug(SONNET_HUNSPELL) << " result :" << result;
116 return result;
117#endif
118}
119
120QStringList HunspellDict::suggest(const QString &word) const
121{
122 if (!m_speller) {
123 return QStringList();
124 }
125
126 QStringList lst;
127#if USE_OLD_HUNSPELL_API
128 char **selection;
129 int nbWord = m_speller->suggest(&selection, toDictEncoding(word).constData());
130 for (int i = 0; i < nbWord; ++i) {
131 lst << m_decoder.decode(selection[i]);
132 }
133 m_speller->free_list(&selection, nbWord);
134#else
135 const auto suggestions = m_speller->suggest(word: toDictEncoding(word).toStdString());
136 for_each(first: suggestions.begin(), last: suggestions.end(), f: [this, &lst](const std::string &suggestion) {
137 lst << m_decoder.decode(ba: suggestion.c_str());
138 });
139#endif
140
141 return lst;
142}
143
144bool HunspellDict::storeReplacement(const QString &bad, const QString &good)
145{
146 Q_UNUSED(bad);
147 Q_UNUSED(good);
148 if (!m_speller) {
149 return false;
150 }
151 qCDebug(SONNET_HUNSPELL) << "HunspellDict::storeReplacement not implemented";
152 return false;
153}
154
155bool HunspellDict::addToPersonal(const QString &word)
156{
157 if (!m_speller) {
158 return false;
159 }
160 m_speller->add(word: toDictEncoding(word).constData());
161 const QString userDic = QDir::home().filePath(fileName: QLatin1String(".hunspell_") % language());
162 QFile userDicFile(userDic);
163 if (userDicFile.open(flags: QIODevice::Append | QIODevice::Text)) {
164 QTextStream out(&userDicFile);
165 out << word << '\n';
166 userDicFile.close();
167 return true;
168 }
169
170 return false;
171}
172
173bool HunspellDict::addToSession(const QString &word)
174{
175 if (!m_speller) {
176 return false;
177 }
178 int r = m_speller->add(word: toDictEncoding(word).constData());
179 return r == 0;
180}
181

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