]> git.lyx.org Git - lyx.git/blob - src/EnchantChecker.cpp
Combine OutlineIn and OutlineOut blocks
[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                 m.speller = instance->request_dict(lang);
76         }
77         catch (const enchant::Exception & e) {
78                 // FIXME error handling?
79                 m.speller = 0;
80         }
81         spellers_[lang] = m;
82         return m.speller;
83 }
84
85
86 enchant::Dict * EnchantChecker::Private::speller(string const & lang)
87 {
88         Spellers::iterator it = spellers_.find(lang);
89         if (it != spellers_.end())
90                 return it->second.speller;
91         
92         return addSpeller(lang);
93 }
94
95
96 EnchantChecker::EnchantChecker()
97         : d(new Private)
98 {}
99
100
101 EnchantChecker::~EnchantChecker()
102 {
103         delete d;
104 }
105
106
107 SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
108 {
109         enchant::Dict * m = d->speller(word.lang()->code());
110
111         if (!m)
112                 return NO_DICTIONARY;
113
114         if (word.word().empty())
115                 return WORD_OK;
116
117         string utf8word = to_utf8(word.word());
118
119         if (m->check(utf8word))
120                 return WORD_OK;
121
122         return UNKNOWN_WORD;
123 }
124
125
126 void EnchantChecker::advanceChangeNumber()
127 {
128         nextChangeNumber();
129 }
130
131
132 void EnchantChecker::insert(WordLangTuple const & word)
133 {
134         enchant::Dict * m = d->speller(word.lang()->code());
135         if (m) {
136                 m->add(to_utf8(word.word()));
137                 advanceChangeNumber();
138         }
139 }
140         
141         
142 void EnchantChecker::remove(WordLangTuple const & word)
143 {
144         enchant::Dict * m = d->speller(word.lang()->code());
145         if (m) {
146                 m->remove(to_utf8(word.word()));
147                 advanceChangeNumber();
148         }
149 }
150
151
152 void EnchantChecker::accept(WordLangTuple const & word)
153 {
154         enchant::Dict * m = d->speller(word.lang()->code());
155         if (m) {
156                 m->add_to_session(to_utf8(word.word()));
157                 advanceChangeNumber();
158         }
159 }
160
161
162 void EnchantChecker::suggest(WordLangTuple const & wl,
163         docstring_list & suggestions)
164 {
165         suggestions.clear();
166         enchant::Dict * m = d->speller(wl.lang()->code());
167
168         if (!m)
169                 return;
170
171         string utf8word = to_utf8(wl.word());
172
173         vector<string> suggs = m->suggest(utf8word);
174         vector<string>::const_iterator it = suggs.begin();
175         
176         for (; it != suggs.end(); ++it)
177                 suggestions.push_back(from_utf8(*it));
178 }
179
180
181 bool EnchantChecker::hasDictionary(Language const * lang) const
182 {
183         if (!lang)
184                 return false;
185         enchant::Broker * instance = enchant::Broker::instance();
186         return (instance->dict_exists(lang->code()));
187 }
188
189
190 int EnchantChecker::numDictionaries() const
191 {
192         return d->spellers_.size();
193 }
194         
195
196 docstring const EnchantChecker::error()
197 {
198         return docstring();
199 }
200
201
202 } // namespace lyx