Sonnet

aspelldict.cpp
1/*
2 * kspell_aspelldict.cpp
3 *
4 * SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8#include "aspelldict.h"
9
10#include "aspell_debug.h"
11
12#ifdef Q_OS_WIN
13#include <QCoreApplication>
14#endif
15
16using namespace Sonnet;
17
18ASpellDict::ASpellDict(const QString &lang)
19 : SpellerPlugin(lang)
20{
21 m_config = new_aspell_config();
22 aspell_config_replace(m_config, "lang", lang.toLatin1().constData());
23 /* All communication with Aspell is done in UTF-8 */
24 /* For reference, please look at BR#87250 */
25 aspell_config_replace(m_config, "encoding", "utf-8");
26
27#ifdef Q_OS_WIN
28 aspell_config_replace(m_config, "data-dir", QString::fromLatin1("%1/data/aspell").arg(QCoreApplication::applicationDirPath()).toLatin1().constData());
29 aspell_config_replace(m_config, "dict-dir", QString::fromLatin1("%1/data/aspell").arg(QCoreApplication::applicationDirPath()).toLatin1().constData());
30#endif
31
32 AspellCanHaveError *possible_err = new_aspell_speller(m_config);
33
34 if (aspell_error_number(possible_err) != 0) {
35 qCWarning(SONNET_LOG_ASPELL) << "aspell error: " << aspell_error_message(possible_err);
36 } else {
37 m_speller = to_aspell_speller(possible_err);
38 }
39}
40
41ASpellDict::~ASpellDict()
42{
43 delete_aspell_speller(m_speller);
44 delete_aspell_config(m_config);
45}
46
47bool ASpellDict::isCorrect(const QString &word) const
48{
49 /* ASpell is expecting length of a string in char representation */
50 /* word.length() != word.toUtf8().length() for nonlatin strings */
51 if (!m_speller) {
52 return false;
53 }
54 int correct = aspell_speller_check(m_speller, word.toUtf8().constData(), word.toUtf8().length());
55 return correct;
56}
57
58QStringList ASpellDict::suggest(const QString &word) const
59{
60 if (!m_speller) {
61 return QStringList();
62 }
63
64 /* ASpell is expecting length of a string in char representation */
65 /* word.length() != word.toUtf8().length() for nonlatin strings */
66 const AspellWordList *suggestions = aspell_speller_suggest(m_speller, word.toUtf8().constData(), word.toUtf8().length());
67
68 AspellStringEnumeration *elements = aspell_word_list_elements(suggestions);
69
70 QStringList qsug;
71 const char *cword;
72
73 while ((cword = aspell_string_enumeration_next(elements))) {
74 /* Since while creating the class ASpellDict the encoding is set */
75 /* to utf-8, one has to convert output from Aspell to QString's UTF-16 */
76 qsug.append(QString::fromUtf8(cword));
77 }
78
79 delete_aspell_string_enumeration(elements);
80 return qsug;
81}
82
83bool ASpellDict::storeReplacement(const QString &bad, const QString &good)
84{
85 if (!m_speller) {
86 return false;
87 }
88 /* ASpell is expecting length of a string in char representation */
89 /* word.length() != word.toUtf8().length() for nonlatin strings */
90 return aspell_speller_store_replacement(m_speller, bad.toUtf8().constData(), bad.toUtf8().length(), good.toUtf8().constData(), good.toUtf8().length());
91}
92
93bool ASpellDict::addToPersonal(const QString &word)
94{
95 if (!m_speller) {
96 return false;
97 }
98 qCDebug(SONNET_LOG_ASPELL) << "Adding" << word << "to aspell personal dictionary";
99 /* ASpell is expecting length of a string in char representation */
100 /* word.length() != word.toUtf8().length() for nonlatin strings */
101 aspell_speller_add_to_personal(m_speller, word.toUtf8().constData(), word.toUtf8().length());
102 /* Add is not enough, one has to save it. This is not documented */
103 /* in ASpell's API manual. I found it in */
104 /* aspell-0.60.2/example/example-c.c */
105 return aspell_speller_save_all_word_lists(m_speller);
106}
107
108bool ASpellDict::addToSession(const QString &word)
109{
110 if (!m_speller) {
111 return false;
112 }
113 /* ASpell is expecting length of a string in char representation */
114 /* word.length() != word.toUtf8().length() for nonlatin strings */
115 return aspell_speller_add_to_session(m_speller, word.toUtf8().constData(), word.toUtf8().length());
116}
The sonnet namespace.
const char * constData() const const
qsizetype length() const const
QString applicationDirPath()
void append(QList< T > &&value)
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
QByteArray toLatin1() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:10 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.