]> git.lyx.org Git - lyx.git/blob - src/EnchantChecker.cpp
prepare Qt 5.6 builds
[lyx.git] / src / EnchantChecker.cpp
1 /**
2  * \file EnchantChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Caolán McNamara
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include <enchant++.h>
15
16 #include "EnchantChecker.h"
17 #include "LyXRC.h"
18 #include "WordLangTuple.h"
19
20 #include "support/lassert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
23
24 #include <map>
25 #include <string>
26
27 using namespace std;
28
29 namespace lyx {
30
31 namespace {
32
33 struct Speller {
34         enchant::Dict * speller;
35 };
36
37 typedef map<string, Speller> Spellers;
38   
39 } // anon namespace
40
41 struct EnchantChecker::Private
42 {
43         Private()
44         {}
45
46         ~Private();
47
48         /// add a speller of the given language
49         enchant::Dict * addSpeller(string const & lang);
50
51         ///
52         enchant::Dict * speller(string const & lang);
53
54         /// the spellers
55         Spellers spellers_;
56 };
57
58
59 EnchantChecker::Private::~Private()
60 {
61         Spellers::iterator it = spellers_.begin();
62         Spellers::iterator end = spellers_.end();
63
64         for (; it != end; ++it)
65                 delete it->second.speller;
66 }
67
68
69 enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
70 {
71         enchant::Broker * instance = enchant::Broker::instance();
72         Speller m;
73
74         try {
75                 LYXERR(Debug::FILES, "request enchant speller for language " << lang);
76                 m.speller = instance->request_dict(lang);
77         }
78         catch (enchant::Exception & e) {
79                 // FIXME error handling?
80                 const char * what = e.what();
81                 LYXERR(Debug::FILES, "cannot add enchant speller: " <<
82                            ((what && *what) ? what : "unspecified enchant exception in request_dict()"));
83                 m.speller = 0;
84         }
85         spellers_[lang] = m;
86         return m.speller;
87 }
88
89
90 enchant::Dict * EnchantChecker::Private::speller(string const & lang)
91 {
92         Spellers::iterator it = spellers_.find(lang);
93         if (it != spellers_.end())
94                 return it->second.speller;
95         
96         return addSpeller(lang);
97 }
98
99
100 EnchantChecker::EnchantChecker()
101         : d(new Private)
102 {}
103
104
105 EnchantChecker::~EnchantChecker()
106 {
107         delete d;
108 }
109
110
111 SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
112 {
113         enchant::Dict * m = d->speller(word.lang()->code());
114
115         if (!m)
116                 return NO_DICTIONARY;
117
118         if (word.word().empty())
119                 return WORD_OK;
120
121         string utf8word = to_utf8(word.word());
122
123         if (m->check(utf8word))
124                 return WORD_OK;
125
126         return UNKNOWN_WORD;
127 }
128
129
130 void EnchantChecker::advanceChangeNumber()
131 {
132         nextChangeNumber();
133 }
134
135
136 void EnchantChecker::insert(WordLangTuple const & word)
137 {
138         enchant::Dict * m = d->speller(word.lang()->code());
139         if (m) {
140                 m->add(to_utf8(word.word()));
141                 advanceChangeNumber();
142         }
143 }
144         
145         
146 void EnchantChecker::remove(WordLangTuple const & word)
147 {
148         enchant::Dict * m = d->speller(word.lang()->code());
149         if (m) {
150                 m->remove(to_utf8(word.word()));
151                 advanceChangeNumber();
152         }
153 }
154
155
156 void EnchantChecker::accept(WordLangTuple const & word)
157 {
158         enchant::Dict * m = d->speller(word.lang()->code());
159         if (m) {
160                 m->add_to_session(to_utf8(word.word()));
161                 advanceChangeNumber();
162         }
163 }
164
165
166 void EnchantChecker::suggest(WordLangTuple const & wl,
167         docstring_list & suggestions)
168 {
169         suggestions.clear();
170         enchant::Dict * m = d->speller(wl.lang()->code());
171
172         if (!m)
173                 return;
174
175         string utf8word = to_utf8(wl.word());
176
177         vector<string> suggs = m->suggest(utf8word);
178         vector<string>::const_iterator it = suggs.begin();
179         
180         for (; it != suggs.end(); ++it)
181                 suggestions.push_back(from_utf8(*it));
182 }
183
184
185 bool EnchantChecker::hasDictionary(Language const * lang) const
186 {
187         if (!lang)
188                 return false;
189         enchant::Broker * instance = enchant::Broker::instance();
190         return (instance->dict_exists(lang->code()));
191 }
192
193
194 int EnchantChecker::numDictionaries() const
195 {
196         return d->spellers_.size();
197 }
198         
199
200 docstring const EnchantChecker::error()
201 {
202         return docstring();
203 }
204
205
206 } // namespace lyx