2010-05-08 18:59:59 +00:00
|
|
|
/* This file is (c) 2008-2010 Konstantin Isakov <ikm@users.berlios.de>
|
2009-04-30 15:29:03 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "loaddictionaries.hh"
|
|
|
|
#include "initializing.hh"
|
|
|
|
#include "bgl.hh"
|
|
|
|
#include "stardict.hh"
|
|
|
|
#include "lsa.hh"
|
|
|
|
#include "dsl.hh"
|
|
|
|
#include "mediawiki.hh"
|
|
|
|
#include "sounddir.hh"
|
|
|
|
#include "hunspell.hh"
|
|
|
|
#include "dictdfiles.hh"
|
2009-05-06 14:39:08 +00:00
|
|
|
#include "romaji.hh"
|
|
|
|
#include "russiantranslit.hh"
|
2009-05-11 23:37:18 +00:00
|
|
|
#include "german.hh"
|
2010-05-29 09:22:08 +00:00
|
|
|
#include "greektranslit.hh"
|
2009-05-16 18:04:21 +00:00
|
|
|
#include "website.hh"
|
2009-04-30 15:29:03 +00:00
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using std::set;
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
LoadDictionaries::LoadDictionaries( Config::Class const & cfg ):
|
2009-05-06 14:39:08 +00:00
|
|
|
paths( cfg.paths ), soundDirs( cfg.soundDirs ), hunspell( cfg.hunspell ),
|
2009-08-31 12:58:29 +00:00
|
|
|
transliteration( cfg.transliteration ),
|
|
|
|
exceptionText( "Load did not finish" ) // Will be cleared upon success
|
2009-04-30 15:29:03 +00:00
|
|
|
{
|
2009-05-17 15:30:43 +00:00
|
|
|
// Populate name filters
|
|
|
|
|
|
|
|
nameFilters << "*.bgl" << "*.ifo" << "*.lsa" << "*.dat"
|
|
|
|
<< "*.dsl" << "*.dsl.dz" << "*.index";
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoadDictionaries::run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for( Config::Paths::const_iterator i = paths.begin(); i != paths.end(); ++i )
|
|
|
|
handlePath( *i );
|
|
|
|
|
|
|
|
// Make soundDirs
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > soundDirDictionaries =
|
|
|
|
SoundDir::makeDictionaries( soundDirs, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), soundDirDictionaries.begin(),
|
|
|
|
soundDirDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make hunspells
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > hunspellDictionaries =
|
|
|
|
HunspellMorpho::makeDictionaries( hunspell );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), hunspellDictionaries.begin(),
|
|
|
|
hunspellDictionaries.end() );
|
|
|
|
}
|
2009-08-31 12:58:29 +00:00
|
|
|
|
|
|
|
exceptionText.clear();
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
|
|
|
catch( std::exception & e )
|
|
|
|
{
|
|
|
|
exceptionText = e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadDictionaries::handlePath( Config::Path const & path )
|
|
|
|
{
|
|
|
|
vector< string > allFiles;
|
|
|
|
|
|
|
|
QDir dir( path.path );
|
|
|
|
|
2009-05-17 15:30:43 +00:00
|
|
|
QFileInfoList entries = dir.entryInfoList( nameFilters, QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot );
|
2009-04-30 15:29:03 +00:00
|
|
|
|
|
|
|
for( QFileInfoList::const_iterator i = entries.constBegin();
|
|
|
|
i != entries.constEnd(); ++i )
|
|
|
|
{
|
|
|
|
QString fullName = i->canonicalFilePath();
|
|
|
|
|
|
|
|
if ( path.recursive && i->isDir() )
|
2009-05-17 15:37:36 +00:00
|
|
|
{
|
|
|
|
// Make sure the path doesn't look like with dsl resources
|
|
|
|
if ( !fullName.endsWith( ".dsl.files", Qt::CaseInsensitive ) &&
|
|
|
|
!fullName.endsWith( ".dsl.dz.files", Qt::CaseInsensitive ) )
|
|
|
|
handlePath( Config::Path( fullName, true ) );
|
|
|
|
}
|
2009-04-30 15:29:03 +00:00
|
|
|
|
|
|
|
allFiles.push_back( QDir::toNativeSeparators( fullName ).toLocal8Bit().data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > bglDictionaries =
|
|
|
|
Bgl::makeDictionaries( allFiles, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), bglDictionaries.begin(),
|
|
|
|
bglDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > stardictDictionaries =
|
|
|
|
Stardict::makeDictionaries( allFiles, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), stardictDictionaries.begin(),
|
|
|
|
stardictDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > lsaDictionaries =
|
|
|
|
Lsa::makeDictionaries( allFiles, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), lsaDictionaries.begin(),
|
|
|
|
lsaDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dslDictionaries =
|
|
|
|
Dsl::makeDictionaries( allFiles, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), dslDictionaries.begin(),
|
|
|
|
dslDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictdDictionaries =
|
|
|
|
DictdFiles::makeDictionaries( allFiles, Config::getIndexDir().toLocal8Bit().data(), *this );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), dictdDictionaries.begin(),
|
|
|
|
dictdDictionaries.end() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadDictionaries::indexingDictionary( string const & dictionaryName ) throw()
|
|
|
|
{
|
|
|
|
emit indexingDictionarySignal( QString::fromUtf8( dictionaryName.c_str() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loadDictionaries( QWidget * parent, bool showInitially,
|
|
|
|
Config::Class const & cfg,
|
|
|
|
std::vector< sptr< Dictionary::Class > > & dictionaries,
|
2009-05-17 22:02:54 +00:00
|
|
|
QNetworkAccessManager & dictNetMgr,
|
|
|
|
bool doDeferredInit_ )
|
2009-04-30 15:29:03 +00:00
|
|
|
{
|
|
|
|
dictionaries.clear();
|
2009-05-06 18:17:13 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
::Initializing init( parent, showInitially );
|
|
|
|
|
|
|
|
// Start a thread to load all the dictionaries
|
|
|
|
|
|
|
|
LoadDictionaries loadDicts( cfg );
|
|
|
|
|
|
|
|
QObject::connect( &loadDicts, SIGNAL( indexingDictionarySignal( QString const & ) ),
|
|
|
|
&init, SLOT( indexing( QString const & ) ) );
|
|
|
|
|
|
|
|
QEventLoop localLoop;
|
|
|
|
|
|
|
|
QObject::connect( &loadDicts, SIGNAL( finished() ),
|
|
|
|
&localLoop, SLOT( quit() ) );
|
|
|
|
|
|
|
|
loadDicts.start();
|
|
|
|
|
|
|
|
localLoop.exec();
|
|
|
|
|
|
|
|
loadDicts.wait();
|
|
|
|
|
|
|
|
if ( loadDicts.getExceptionText().size() )
|
|
|
|
{
|
|
|
|
QMessageBox::critical( parent, QCoreApplication::translate( "LoadDictionaries", "Error loading dictionaries" ),
|
|
|
|
QString::fromUtf8( loadDicts.getExceptionText().c_str() ) );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dictionaries = loadDicts.getDictionaries();
|
|
|
|
|
2009-05-18 10:46:53 +00:00
|
|
|
///// We create transliterations syncronously since they are very simple
|
|
|
|
|
|
|
|
// Make Romaji
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > romajiDictionaries =
|
|
|
|
Romaji::makeDictionaries( cfg.transliteration.romaji );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), romajiDictionaries.begin(),
|
|
|
|
romajiDictionaries.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make Russian transliteration
|
|
|
|
if ( cfg.transliteration.enableRussianTransliteration )
|
|
|
|
dictionaries.push_back( RussianTranslit::makeDictionary() );
|
|
|
|
|
|
|
|
// Make German transliteration
|
|
|
|
if ( cfg.transliteration.enableGermanTransliteration )
|
|
|
|
dictionaries.push_back( GermanTranslit::makeDictionary() );
|
|
|
|
|
2010-05-29 09:22:08 +00:00
|
|
|
// Make Greek transliteration
|
|
|
|
if ( cfg.transliteration.enableGreekTransliteration )
|
|
|
|
dictionaries.push_back( GreekTranslit::makeDictionary() );
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
///// We create MediaWiki dicts syncronously, since they use netmgr
|
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dicts =
|
|
|
|
MediaWiki::makeDictionaries( loadDicts, cfg.mediawikis, dictNetMgr );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
|
|
|
|
}
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
///// WebSites are very simple, no need to create them asyncronously
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dicts =
|
|
|
|
WebSite::makeDictionaries( cfg.webSites );
|
|
|
|
|
|
|
|
dictionaries.insert( dictionaries.end(), dicts.begin(), dicts.end() );
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:35:19 +00:00
|
|
|
printf( "Load done\n" );
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
// Remove any stale index files
|
|
|
|
|
|
|
|
set< string > ids;
|
|
|
|
|
|
|
|
for( unsigned x = dictionaries.size(); x--; )
|
|
|
|
ids.insert( dictionaries[ x ]->getId() );
|
|
|
|
|
|
|
|
QDir indexDir( Config::getIndexDir() );
|
|
|
|
|
|
|
|
QStringList allIdxFiles = indexDir.entryList( QDir::Files );
|
|
|
|
|
|
|
|
for( QStringList::const_iterator i = allIdxFiles.constBegin();
|
|
|
|
i != allIdxFiles.constEnd(); ++i )
|
|
|
|
{
|
|
|
|
if ( ids.find( i->toLocal8Bit().data() ) == ids.end() &&
|
|
|
|
i->size() == 32 )
|
|
|
|
indexDir.remove( *i );
|
|
|
|
}
|
2009-05-17 13:35:19 +00:00
|
|
|
|
|
|
|
// Run deferred inits
|
|
|
|
|
2009-05-17 22:02:54 +00:00
|
|
|
if ( doDeferredInit_ )
|
|
|
|
doDeferredInit( dictionaries );
|
|
|
|
}
|
|
|
|
|
|
|
|
void doDeferredInit( std::vector< sptr< Dictionary::Class > > & dictionaries )
|
|
|
|
{
|
2009-05-17 13:35:19 +00:00
|
|
|
for( unsigned x = 0; x < dictionaries.size(); ++x )
|
|
|
|
dictionaries[ x ]->deferredInit();
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|