mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-24 00:14:06 +00:00
194 lines
5.4 KiB
C++
194 lines
5.4 KiB
C++
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
||
|
* 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"
|
||
|
|
||
|
#include <QMessageBox>
|
||
|
#include <QDir>
|
||
|
|
||
|
#include <set>
|
||
|
|
||
|
using std::set;
|
||
|
|
||
|
using std::string;
|
||
|
using std::vector;
|
||
|
|
||
|
LoadDictionaries::LoadDictionaries( Config::Class const & cfg ):
|
||
|
paths( cfg.paths ), soundDirs( cfg.soundDirs ), hunspell( cfg.hunspell )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
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() );
|
||
|
}
|
||
|
|
||
|
}
|
||
|
catch( std::exception & e )
|
||
|
{
|
||
|
exceptionText = e.what();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void LoadDictionaries::handlePath( Config::Path const & path )
|
||
|
{
|
||
|
vector< string > allFiles;
|
||
|
|
||
|
QDir dir( path.path );
|
||
|
|
||
|
QFileInfoList entries = dir.entryInfoList( QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot );
|
||
|
|
||
|
for( QFileInfoList::const_iterator i = entries.constBegin();
|
||
|
i != entries.constEnd(); ++i )
|
||
|
{
|
||
|
QString fullName = i->canonicalFilePath();
|
||
|
|
||
|
if ( path.recursive && i->isDir() )
|
||
|
handlePath( Config::Path( fullName, true ) );
|
||
|
|
||
|
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,
|
||
|
QNetworkAccessManager & dictNetMgr )
|
||
|
{
|
||
|
dictionaries.clear();
|
||
|
|
||
|
::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();
|
||
|
|
||
|
///// 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() );
|
||
|
}
|
||
|
|
||
|
// 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 );
|
||
|
}
|
||
|
}
|
||
|
|