No longer honours following status in JSON, instead relies solely on following list.
[larjonas-pumpa.git] / src / qaspell.cpp
blob8adaeca7610e361ed65b6435ab87c12ee5278af5
1 /*
2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 #include "qaspell.h"
22 #ifdef USE_ASPELL
24 //------------------------------------------------------------------------------
26 QString QASpell::s_locale = "en_US";
28 //------------------------------------------------------------------------------
30 QASpell::QASpell(QObject* parent) : QObject(parent) {
31 ok = false;
32 spell_config = new_aspell_config();
33 aspell_config_replace(spell_config, "lang",
34 s_locale.toLocal8Bit().constData());
35 aspell_config_replace(spell_config, "encoding", "ucs-2");
37 AspellCanHaveError* possible_err = new_aspell_speller(spell_config);
38 spell_checker = NULL;
39 if (aspell_error_number(possible_err) != 0) {
40 qDebug() << aspell_error_message(possible_err);
41 return;
43 spell_checker = to_aspell_speller(possible_err);
44 ok = true;
47 //------------------------------------------------------------------------------
49 QASpell::~QASpell() {
50 if (spell_checker != NULL)
51 delete_aspell_speller(spell_checker);
52 delete_aspell_config(spell_config);
55 //------------------------------------------------------------------------------
57 void QASpell::setLocale(QString locale) {
58 if (!locale.isEmpty())
59 s_locale = locale;
62 //------------------------------------------------------------------------------
64 bool QASpell::checksOK() const {
65 if (!ok)
66 return false;
68 if (spell_checker == NULL) {
69 qDebug() << "aspell was not initialised properly!";
70 return false;
72 return true;
75 //------------------------------------------------------------------------------
77 bool QASpell::checkWord(const QString& word) const {
78 if (!checksOK())
79 return true;
81 int correct =
82 aspell_speller_check(spell_checker,
83 reinterpret_cast<const char *>(word.utf16()), -1);
84 return correct;
87 //------------------------------------------------------------------------------
89 QStringList QASpell::suggestions(const QString& word) const {
90 QStringList list;
91 if (!checksOK())
92 return list;
94 const AspellWordList* wl =
95 aspell_speller_suggest(spell_checker,
96 reinterpret_cast<const char *>(word.utf16()), -1);
97 AspellStringEnumeration* w = aspell_word_list_elements(wl);
98 const char* cw;
99 while ((cw = aspell_string_enumeration_next(w)) != NULL) {
100 list << QString::fromUtf16(reinterpret_cast<const ushort *>(cw));
102 delete_aspell_string_enumeration(w);
104 return list;
107 #endif // USE_ASPELL