2 * \file EnchantChecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Caolán McNamara
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include <enchant++.h>
16 #include "EnchantChecker.h"
18 #include "WordLangTuple.h"
20 #include "support/lassert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
34 enchant::Dict * speller;
37 typedef map<string, Speller> Spellers;
41 struct EnchantChecker::Private
48 /// add a speller of the given language
49 enchant::Dict * addSpeller(string const & lang);
52 enchant::Dict * speller(string const & lang);
59 EnchantChecker::Private::~Private()
61 Spellers::iterator it = spellers_.begin();
62 Spellers::iterator end = spellers_.end();
64 for (; it != end; ++it)
65 delete it->second.speller;
69 enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
71 enchant::Broker * instance = enchant::Broker::instance();
75 m.speller = instance->request_dict(lang);
77 catch (const enchant::Exception & e) {
78 // FIXME error handling?
86 enchant::Dict * EnchantChecker::Private::speller(string const & lang)
88 Spellers::iterator it = spellers_.find(lang);
89 if (it != spellers_.end())
90 return it->second.speller;
92 return addSpeller(lang);
96 EnchantChecker::EnchantChecker()
101 EnchantChecker::~EnchantChecker()
107 SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
109 enchant::Dict * m = d->speller(word.lang()->code());
112 return NO_DICTIONARY;
114 if (word.word().empty())
117 string utf8word = to_utf8(word.word());
119 if (m->check(utf8word))
126 void EnchantChecker::advanceChangeNumber()
132 void EnchantChecker::insert(WordLangTuple const & word)
134 enchant::Dict * m = d->speller(word.lang()->code());
136 m->add(to_utf8(word.word()));
137 advanceChangeNumber();
142 void EnchantChecker::remove(WordLangTuple const & word)
144 enchant::Dict * m = d->speller(word.lang()->code());
146 m->remove(to_utf8(word.word()));
147 advanceChangeNumber();
152 void EnchantChecker::accept(WordLangTuple const & word)
154 enchant::Dict * m = d->speller(word.lang()->code());
156 m->add_to_session(to_utf8(word.word()));
157 advanceChangeNumber();
162 void EnchantChecker::suggest(WordLangTuple const & wl,
163 docstring_list & suggestions)
166 enchant::Dict * m = d->speller(wl.lang()->code());
171 string utf8word = to_utf8(wl.word());
173 vector<string> suggs = m->suggest(utf8word);
174 vector<string>::const_iterator it = suggs.begin();
176 for (; it != suggs.end(); ++it)
177 suggestions.push_back(from_utf8(*it));
181 bool EnchantChecker::hasDictionary(Language const * lang) const
185 enchant::Broker * instance = enchant::Broker::instance();
186 return (instance->dict_exists(lang->code()));
190 int EnchantChecker::numDictionaries() const
192 return d->spellers_.size();
196 docstring const EnchantChecker::error()