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 std::map<std::string, Speller> Spellers;
41 struct EnchantChecker::Private
47 /// add a speller of the given language
48 enchant::Dict * addSpeller(string const & lang);
51 enchant::Dict * speller(string const & lang);
58 EnchantChecker::Private::~Private()
60 Spellers::iterator it = spellers_.begin();
61 Spellers::iterator end = spellers_.end();
63 for (; it != end; ++it) {
64 delete it->second.speller;
69 enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
71 enchant::Broker * instance = enchant::Broker::instance();
72 enchant::Dict * dict = instance->request_dict(lang);
80 // FIXME error handling?
85 enchant::Dict * EnchantChecker::Private::speller(string const & lang)
87 Spellers::iterator it = spellers_.find(lang);
88 if (it != spellers_.end())
89 return it->second.speller;
91 return addSpeller(lang);
95 EnchantChecker::EnchantChecker(): d(new Private)
100 EnchantChecker::~EnchantChecker()
106 SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
108 enchant::Dict * m = d->speller(word.lang()->code());
113 std::string utf8word(to_utf8(word.word()));
115 if (m->check(utf8word))
122 void EnchantChecker::insert(WordLangTuple const & word)
124 Spellers::iterator it = d->spellers_.find(word.lang()->code());
125 if (it != d->spellers_.end())
126 it->second.speller->add(to_utf8(word.word()));
130 void EnchantChecker::accept(WordLangTuple const & word)
132 Spellers::iterator it = d->spellers_.find(word.lang()->code());
133 if (it != d->spellers_.end())
134 it->second.speller->add_to_session(to_utf8(word.word()));
138 void EnchantChecker::suggest(WordLangTuple const & wl,
139 docstring_list & suggestions)
142 enchant::Dict * m = d->speller(wl.lang()->code());
147 string utf8word = to_utf8(wl.word());
149 vector<string> suggs = m->suggest(utf8word);
150 vector<string>::const_iterator it = suggs.begin();
152 for (; it != suggs.end(); ++it)
153 suggestions.push_back(from_utf8(*it));
157 docstring const EnchantChecker::error()