Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 301 lines (227 sloc) 9.046 kb
535fe0d @Abs62 Update year in copyright notices.
Abs62 authored
1 /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
2 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
3
4 #include "loaddictionaries.hh"
5 #include "initializing.hh"
6 #include "bgl.hh"
7 #include "stardict.hh"
8 #include "lsa.hh"
9 #include "dsl.hh"
10 #include "mediawiki.hh"
11 #include "sounddir.hh"
12 #include "hunspell.hh"
13 #include "dictdfiles.hh"
197ccf3 + Support for transliterations added. For now basic Russian translit and
Konstantin Isakov authored
14 #include "romaji.hh"
15 #include "russiantranslit.hh"
5253d27 added German transliteration rules
ars_goldendict authored
16 #include "german.hh"
2b557e7 Greek transliteration by Jennie Petoumenou.
Konstantin Isakov authored
17 #include "greektranslit.hh"
93fe5e5 + 'WebSites' implemented.
Konstantin Isakov authored
18 #include "website.hh"
9e43f86 Support for Forvo online pronunciations added.
Konstantin Isakov authored
19 #include "forvo.hh"
9960efc @dragonroot Add support for launching arbitrary executables (tts, manpages etc).
dragonroot authored
20 #include "programs.hh"
36e509a @Abs62 Replace printf in code by a macro DPRINTF
Abs62 authored
21 #include "dprintf.hh"
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
22 #include "fsencoding.hh"
af53018 @Abs62 XDXF dictionaries format support
Abs62 authored
23 #include "xdxf.hh"
79d24b8 @Abs62 Add support for Sdict dictionaries
Abs62 authored
24 #include "sdict.hh"
81ee55a @Abs62 Add support for Aard dictionaries (aar-html)
Abs62 authored
25 #include "aard.hh"
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
26
27 #include <QMessageBox>
28 #include <QDir>
29
30 #include <set>
31
32 using std::set;
33
34 using std::string;
35 using std::vector;
36
37 LoadDictionaries::LoadDictionaries( Config::Class const & cfg ):
197ccf3 + Support for transliterations added. For now basic Russian translit and
Konstantin Isakov authored
38 paths( cfg.paths ), soundDirs( cfg.soundDirs ), hunspell( cfg.hunspell ),
8f23e83 +! Prevent accidential index purging when the program is terminated
Konstantin Isakov authored
39 transliteration( cfg.transliteration ),
40 exceptionText( "Load did not finish" ) // Will be cleared upon success
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
41 {
3621020 + Apply extension filters early when searching for dictionary files.
Konstantin Isakov authored
42 // Populate name filters
43
44 nameFilters << "*.bgl" << "*.ifo" << "*.lsa" << "*.dat"
79d24b8 @Abs62 Add support for Sdict dictionaries
Abs62 authored
45 << "*.dsl" << "*.dsl.dz" << "*.index" << "*.xdxf"
81ee55a @Abs62 Add support for Aard dictionaries (aar-html)
Abs62 authored
46 << "*.xdxf.dz" << "*.dct" << "*.aar";
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
47 }
48
49 void LoadDictionaries::run()
50 {
51 try
52 {
53 for( Config::Paths::const_iterator i = paths.begin(); i != paths.end(); ++i )
54 handlePath( *i );
55
56 // Make soundDirs
57 {
58 vector< sptr< Dictionary::Class > > soundDirDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
59 SoundDir::makeDictionaries( soundDirs, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
60
61 dictionaries.insert( dictionaries.end(), soundDirDictionaries.begin(),
62 soundDirDictionaries.end() );
63 }
64
65 // Make hunspells
66 {
67 vector< sptr< Dictionary::Class > > hunspellDictionaries =
68 HunspellMorpho::makeDictionaries( hunspell );
69
70 dictionaries.insert( dictionaries.end(), hunspellDictionaries.begin(),
71 hunspellDictionaries.end() );
72 }
8f23e83 +! Prevent accidential index purging when the program is terminated
Konstantin Isakov authored
73
74 exceptionText.clear();
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
75 }
76 catch( std::exception & e )
77 {
78 exceptionText = e.what();
79 }
80 }
81
82 void LoadDictionaries::handlePath( Config::Path const & path )
83 {
84 vector< string > allFiles;
85
86 QDir dir( path.path );
87
3621020 + Apply extension filters early when searching for dictionary files.
Konstantin Isakov authored
88 QFileInfoList entries = dir.entryInfoList( nameFilters, QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
89
90 for( QFileInfoList::const_iterator i = entries.constBegin();
91 i != entries.constEnd(); ++i )
92 {
93 QString fullName = i->canonicalFilePath();
94
95 if ( path.recursive && i->isDir() )
cfd6e1d + Don't recurse into .dsl.files or .dsl.dz.files directories when sea…
Konstantin Isakov authored
96 {
97 // Make sure the path doesn't look like with dsl resources
98 if ( !fullName.endsWith( ".dsl.files", Qt::CaseInsensitive ) &&
99 !fullName.endsWith( ".dsl.dz.files", Qt::CaseInsensitive ) )
100 handlePath( Config::Path( fullName, true ) );
101 }
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
102
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
103 allFiles.push_back( FsEncoding::encode( QDir::toNativeSeparators( fullName ) ) );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
104 }
105
106 {
107 vector< sptr< Dictionary::Class > > bglDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
108 Bgl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
109
110 dictionaries.insert( dictionaries.end(), bglDictionaries.begin(),
111 bglDictionaries.end() );
112 }
113
114 {
115 vector< sptr< Dictionary::Class > > stardictDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
116 Stardict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
117
118 dictionaries.insert( dictionaries.end(), stardictDictionaries.begin(),
119 stardictDictionaries.end() );
120 }
121
122 {
123 vector< sptr< Dictionary::Class > > lsaDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
124 Lsa::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
125
126 dictionaries.insert( dictionaries.end(), lsaDictionaries.begin(),
127 lsaDictionaries.end() );
128 }
129
130 {
131 vector< sptr< Dictionary::Class > > dslDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
132 Dsl::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
133
134 dictionaries.insert( dictionaries.end(), dslDictionaries.begin(),
135 dslDictionaries.end() );
136 }
137
138 {
139 vector< sptr< Dictionary::Class > > dictdDictionaries =
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
140 DictdFiles::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
141
142 dictionaries.insert( dictionaries.end(), dictdDictionaries.begin(),
143 dictdDictionaries.end() );
144 }
af53018 @Abs62 XDXF dictionaries format support
Abs62 authored
145 {
146 vector< sptr< Dictionary::Class > > xdxfDictionaries =
147 Xdxf::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
148
149 dictionaries.insert( dictionaries.end(), xdxfDictionaries.begin(),
150 xdxfDictionaries.end() );
151 }
79d24b8 @Abs62 Add support for Sdict dictionaries
Abs62 authored
152 {
153 vector< sptr< Dictionary::Class > > sdictDictionaries =
154 Sdict::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
155
156 dictionaries.insert( dictionaries.end(), sdictDictionaries.begin(),
157 sdictDictionaries.end() );
158 }
81ee55a @Abs62 Add support for Aard dictionaries (aar-html)
Abs62 authored
159 {
160 vector< sptr< Dictionary::Class > > aardDictionaries =
161 Aard::makeDictionaries( allFiles, FsEncoding::encode( Config::getIndexDir() ), *this );
162
163 dictionaries.insert( dictionaries.end(), aardDictionaries.begin(),
164 aardDictionaries.end() );
165 }
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
166 }
167
168 void LoadDictionaries::indexingDictionary( string const & dictionaryName ) throw()
169 {
170 emit indexingDictionarySignal( QString::fromUtf8( dictionaryName.c_str() ) );
171 }
172
173
174 void loadDictionaries( QWidget * parent, bool showInitially,
175 Config::Class const & cfg,
176 std::vector< sptr< Dictionary::Class > > & dictionaries,
ba9b13d * On initial load do deferred dictionary init at the very end.
Konstantin Isakov authored
177 QNetworkAccessManager & dictNetMgr,
178 bool doDeferredInit_ )
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
179 {
180 dictionaries.clear();
765ba9d added language recognition for bgl files
ars_goldendict authored
181
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
182 ::Initializing init( parent, showInitially );
183
184 // Start a thread to load all the dictionaries
185
186 LoadDictionaries loadDicts( cfg );
187
188 QObject::connect( &loadDicts, SIGNAL( indexingDictionarySignal( QString const & ) ),
189 &init, SLOT( indexing( QString const & ) ) );
190
191 QEventLoop localLoop;
192
193 QObject::connect( &loadDicts, SIGNAL( finished() ),
194 &localLoop, SLOT( quit() ) );
195
196 loadDicts.start();
197
198 localLoop.exec();
199
200 loadDicts.wait();
201
202 if ( loadDicts.getExceptionText().size() )
203 {
204 QMessageBox::critical( parent, QCoreApplication::translate( "LoadDictionaries", "Error loading dictionaries" ),
205 QString::fromUtf8( loadDicts.getExceptionText().c_str() ) );
206
207 return;
208 }
209
210 dictionaries = loadDicts.getDictionaries();
211
1e4a643 + Create transliterations syncronously.
Konstantin Isakov authored
212 ///// We create transliterations syncronously since they are very simple
213
214 // Make Romaji
215 {
216 vector< sptr< Dictionary::Class > > romajiDictionaries =
217 Romaji::makeDictionaries( cfg.transliteration.romaji );
218
219 dictionaries.insert( dictionaries.end(), romajiDictionaries.begin(),
220 romajiDictionaries.end() );
221 }
222
223 // Make Russian transliteration
224 if ( cfg.transliteration.enableRussianTransliteration )
225 dictionaries.push_back( RussianTranslit::makeDictionary() );
226
227 // Make German transliteration
228 if ( cfg.transliteration.enableGermanTransliteration )
229 dictionaries.push_back( GermanTranslit::makeDictionary() );
230
2b557e7 Greek transliteration by Jennie Petoumenou.
Konstantin Isakov authored
231 // Make Greek transliteration
232 if ( cfg.transliteration.enableGreekTransliteration )
233 dictionaries.push_back( GreekTranslit::makeDictionary() );
234
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
235 ///// We create MediaWiki dicts syncronously, since they use netmgr
236
237 {
238 vector< sptr< Dictionary::Class > > dicts =
239 MediaWiki::makeDictionaries( loadDicts, cfg.mediawikis, dictNetMgr );
240
241 dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
242 }
243
93fe5e5 + 'WebSites' implemented.
Konstantin Isakov authored
244 ///// WebSites are very simple, no need to create them asyncronously
245 {
246 vector< sptr< Dictionary::Class > > dicts =
247 WebSite::makeDictionaries( cfg.webSites );
248
249 dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
250 }
251
9e43f86 Support for Forvo online pronunciations added.
Konstantin Isakov authored
252 //// Forvo dictionaries
253
254 {
255 vector< sptr< Dictionary::Class > > dicts =
256 Forvo::makeDictionaries( loadDicts, cfg.forvo, dictNetMgr );
257
258 dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
259 }
260
9960efc @dragonroot Add support for launching arbitrary executables (tts, manpages etc).
dragonroot authored
261 //// Programs
262 {
263 vector< sptr< Dictionary::Class > > dicts =
264 Programs::makeDictionaries( cfg.programs );
265
266 dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
267 }
268
36e509a @Abs62 Replace printf in code by a macro DPRINTF
Abs62 authored
269 DPRINTF( "Load done\n" );
15e7af9 + Add a mechanism to defer dictionaries' initialization.
Konstantin Isakov authored
270
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
271 // Remove any stale index files
272
273 set< string > ids;
274
275 for( unsigned x = dictionaries.size(); x--; )
276 ids.insert( dictionaries[ x ]->getId() );
277
278 QDir indexDir( Config::getIndexDir() );
279
280 QStringList allIdxFiles = indexDir.entryList( QDir::Files );
281
282 for( QStringList::const_iterator i = allIdxFiles.constBegin();
283 i != allIdxFiles.constEnd(); ++i )
284 {
2763b74 @Abs62 Use file names in UTF-8. Fix issue #30.
Abs62 authored
285 if ( ids.find( FsEncoding::encode( *i ) ) == ids.end() &&
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
286 i->size() == 32 )
287 indexDir.remove( *i );
288 }
15e7af9 + Add a mechanism to defer dictionaries' initialization.
Konstantin Isakov authored
289
290 // Run deferred inits
291
ba9b13d * On initial load do deferred dictionary init at the very end.
Konstantin Isakov authored
292 if ( doDeferredInit_ )
293 doDeferredInit( dictionaries );
294 }
295
296 void doDeferredInit( std::vector< sptr< Dictionary::Class > > & dictionaries )
297 {
15e7af9 + Add a mechanism to defer dictionaries' initialization.
Konstantin Isakov authored
298 for( unsigned x = 0; x < dictionaries.size(); ++x )
299 dictionaries[ x ]->deferredInit();
e94ed0f * Both Sources and Groups were consolidated into a single Dictionarie…
Konstantin Isakov authored
300 }
Something went wrong with that request. Please try again.