2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-04-04 16:06:06 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "sounddir.hh"
|
|
|
|
#include "file.hh"
|
|
|
|
#include "folding.hh"
|
|
|
|
#include "utf8.hh"
|
|
|
|
#include "btreeidx.hh"
|
|
|
|
#include "chunkedstorage.hh"
|
|
|
|
#include "filetype.hh"
|
|
|
|
#include "htmlescape.hh"
|
2009-04-10 21:07:03 +00:00
|
|
|
#include "audiolink.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring_qt.hh"
|
2011-09-09 12:05:28 +00:00
|
|
|
#include "fsencoding.hh"
|
2009-04-04 16:06:06 +00:00
|
|
|
#include <set>
|
|
|
|
#include <QDir>
|
2013-02-02 18:25:24 +00:00
|
|
|
#include <QFileInfo>
|
2009-04-12 11:27:43 +00:00
|
|
|
#include <QUrl>
|
2013-09-20 14:25:44 +00:00
|
|
|
#include <QDebug>
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
namespace SoundDir {
|
|
|
|
|
|
|
|
using std::string;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
2009-04-04 16:06:06 +00:00
|
|
|
using std::map;
|
|
|
|
using std::multimap;
|
|
|
|
using std::set;
|
|
|
|
using BtreeIndexing::WordArticleLink;
|
|
|
|
using BtreeIndexing::IndexedWords;
|
2009-04-14 16:35:47 +00:00
|
|
|
using BtreeIndexing::IndexInfo;
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
Signature = 0x58524453, // SDRX on little-endian, XRDS on big-endian
|
|
|
|
CurrentFormatVersion = 1+ BtreeIndexing::FormatVersion + Folding::Version
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IdxHeader
|
|
|
|
{
|
|
|
|
uint32_t signature; // First comes the signature, SDRX
|
|
|
|
uint32_t formatVersion; // File format version, is to be CurrentFormatVersion
|
|
|
|
uint32_t soundsCount; // Total number of sounds, for informative purposes only
|
|
|
|
uint32_t chunksOffset; // The offset to chunks' storage
|
2009-04-14 16:35:47 +00:00
|
|
|
uint32_t indexBtreeMaxElements; // Two fields from IndexInfo
|
|
|
|
uint32_t indexRootOffset;
|
2009-04-29 23:18:26 +00:00
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
__attribute__((packed))
|
|
|
|
#endif
|
|
|
|
;
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
bool indexIsOldOrBad( string const & indexFile )
|
|
|
|
{
|
|
|
|
File::Class idx( indexFile, "rb" );
|
|
|
|
|
|
|
|
IdxHeader header;
|
|
|
|
|
|
|
|
return idx.readRecords( &header, sizeof( header ), 1 ) != 1 ||
|
|
|
|
header.signature != Signature ||
|
|
|
|
header.formatVersion != CurrentFormatVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SoundDirDictionary: public BtreeIndexing::BtreeDictionary
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
Mutex idxMutex;
|
|
|
|
File::Class idx;
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
ChunkedStorage::Reader chunks;
|
2013-02-03 10:08:41 +00:00
|
|
|
QString iconFilename;
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
SoundDirDictionary( string const & id, string const & name,
|
|
|
|
string const & indexFile,
|
2013-02-02 18:25:24 +00:00
|
|
|
vector< string > const & dictionaryFiles,
|
|
|
|
QString const & iconFilename_ );
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return name; }
|
|
|
|
|
|
|
|
virtual map< Dictionary::Property, string > getProperties() throw()
|
|
|
|
{ return map< Dictionary::Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
|
|
|
{ return idxHeader.soundsCount; }
|
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
|
|
|
{ return getArticleCount(); }
|
|
|
|
|
|
|
|
virtual sptr< Dictionary::DataRequest > getArticle( wstring const &,
|
2009-05-29 19:48:50 +00:00
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-04-04 16:06:06 +00:00
|
|
|
throw( std::exception );
|
|
|
|
|
|
|
|
virtual sptr< Dictionary::DataRequest > getResource( string const & name )
|
|
|
|
throw( std::exception );
|
2012-12-03 12:47:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2013-02-02 18:25:24 +00:00
|
|
|
virtual void loadIcon() throw();
|
2009-04-04 16:06:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SoundDirDictionary::SoundDirDictionary( string const & id,
|
|
|
|
string const & name_,
|
|
|
|
string const & indexFile,
|
2013-02-02 18:25:24 +00:00
|
|
|
vector< string > const & dictionaryFiles,
|
|
|
|
QString const & iconFilename_ ):
|
2009-04-04 16:06:06 +00:00
|
|
|
BtreeDictionary( id, dictionaryFiles ),
|
|
|
|
name( name_ ),
|
|
|
|
idx( indexFile, "rb" ),
|
|
|
|
idxHeader( idx.read< IdxHeader >() ),
|
2013-02-02 18:25:24 +00:00
|
|
|
chunks( idx, idxHeader.chunksOffset ),
|
|
|
|
iconFilename( iconFilename_ )
|
2009-04-04 16:06:06 +00:00
|
|
|
{
|
|
|
|
// Initialize the index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
openIndex( IndexInfo( idxHeader.indexBtreeMaxElements,
|
|
|
|
idxHeader.indexRootOffset ),
|
|
|
|
idx, idxMutex );
|
2009-04-04 16:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > SoundDirDictionary::getArticle( wstring const & word,
|
2009-05-29 19:48:50 +00:00
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-04-04 16:06:06 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< WordArticleLink > chain = findArticles( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < alts.size(); ++x )
|
|
|
|
{
|
|
|
|
/// Make an additional query for each alt
|
|
|
|
|
|
|
|
vector< WordArticleLink > altChain = findArticles( alts[ x ] );
|
|
|
|
|
|
|
|
chain.insert( chain.end(), altChain.begin(), altChain.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps to the chain number
|
|
|
|
multimap< wstring, unsigned > mainArticles, alternateArticles;
|
|
|
|
|
|
|
|
set< uint32_t > articlesIncluded; // Some synonims make it that the articles
|
|
|
|
// appear several times. We combat this
|
|
|
|
// by only allowing them to appear once.
|
|
|
|
|
|
|
|
wstring wordCaseFolded = Folding::applySimpleCaseOnly( word );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < chain.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( articlesIncluded.find( chain[ x ].articleOffset ) != articlesIncluded.end() )
|
|
|
|
continue; // We already have this article in the body.
|
|
|
|
|
|
|
|
// Ok. Now, does it go to main articles, or to alternate ones? We list
|
|
|
|
// main ones first, and alternates after.
|
|
|
|
|
|
|
|
// We do the case-folded comparison here.
|
|
|
|
|
|
|
|
wstring headwordStripped =
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( chain[ x ].word ) );
|
|
|
|
|
2009-04-21 20:09:02 +00:00
|
|
|
multimap< wstring, unsigned > & mapToUse =
|
2009-04-04 16:06:06 +00:00
|
|
|
( wordCaseFolded == headwordStripped ) ?
|
|
|
|
mainArticles : alternateArticles;
|
|
|
|
|
|
|
|
mapToUse.insert( std::pair< wstring, uint32_t >(
|
|
|
|
Folding::applySimpleCaseOnly( Utf8::decode( chain[ x ].word ) ), x ) );
|
|
|
|
|
|
|
|
articlesIncluded.insert( chain[ x ].articleOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( mainArticles.empty() && alternateArticles.empty() )
|
|
|
|
return new Dictionary::DataRequestInstant( false ); // No such word
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
|
|
|
multimap< wstring, uint32_t >::const_iterator i;
|
|
|
|
|
|
|
|
result += "<table class=\"lsa_play\">";
|
|
|
|
for( i = mainArticles.begin(); i != mainArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<tr>";
|
|
|
|
|
2009-04-12 11:27:43 +00:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
|
|
|
url.setPath( QString::number( chain[ i->second ].articleOffset ) );
|
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
2009-04-04 16:06:06 +00:00
|
|
|
|
2009-05-14 20:38:17 +00:00
|
|
|
result += addAudioLink( ref, getId() );
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
result += "<td><a href=" + ref + "><img src=\"qrcx://localhost/icons/playsound.png\" border=\"0\" alt=\"Play\"/></a></td>";
|
|
|
|
result += "<td><a href=" + ref + ">" + Html::escape( chain[ i->second ].word ) + "</a></td>";
|
|
|
|
result += "</tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = alternateArticles.begin(); i != alternateArticles.end(); ++i )
|
|
|
|
{
|
|
|
|
result += "<tr>";
|
|
|
|
|
2009-04-12 11:27:43 +00:00
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "gdau" );
|
|
|
|
url.setHost( QString::fromUtf8( getId().c_str() ) );
|
|
|
|
url.setPath( QString::number( chain[ i->second ].articleOffset ) );
|
|
|
|
|
|
|
|
string ref = string( "\"" ) + url.toEncoded().data() + "\"";
|
2009-04-04 16:06:06 +00:00
|
|
|
|
2009-05-14 20:38:17 +00:00
|
|
|
result += addAudioLink( ref, getId() );
|
2009-04-10 21:07:03 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
result += "<td><a href=" + ref + "><img src=\"qrcx://localhost/icons/playsound.png\" border=\"0\" alt=\"Play\"/></a></td>";
|
|
|
|
result += "<td><a href=" + ref + ">" + Html::escape( chain[ i->second ].word ) + "</a></td>";
|
|
|
|
result += "</tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += "</table>";
|
|
|
|
|
|
|
|
Dictionary::DataRequestInstant * ret =
|
|
|
|
new Dictionary::DataRequestInstant( true );
|
|
|
|
|
|
|
|
ret->getData().resize( result.size() );
|
|
|
|
|
|
|
|
memcpy( &(ret->getData().front()), result.data(), result.size() );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-02-02 18:25:24 +00:00
|
|
|
void SoundDirDictionary::loadIcon() throw()
|
|
|
|
{
|
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( !iconFilename.isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo fInfo( QDir( Config::getConfigDir() ), iconFilename );
|
|
|
|
if( fInfo.isFile() )
|
|
|
|
loadIconFromFile( fInfo.absoluteFilePath(), true );
|
|
|
|
}
|
|
|
|
if( dictionaryIcon.isNull() )
|
|
|
|
dictionaryIcon = dictionaryNativeIcon = QIcon(":/icons/playsound.png");
|
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
sptr< Dictionary::DataRequest > SoundDirDictionary::getResource( string const & name )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
bool isNumber = false;
|
|
|
|
|
|
|
|
uint32_t articleOffset = QString::fromUtf8( name.c_str() ).toULong( &isNumber );
|
|
|
|
|
|
|
|
if ( !isNumber )
|
|
|
|
return new Dictionary::DataRequestInstant( false ); // No such resource
|
|
|
|
|
|
|
|
vector< char > chunk;
|
|
|
|
char * articleData;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Mutex::Lock _( idxMutex );
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
articleData = chunks.getBlock( articleOffset, chunk );
|
|
|
|
|
|
|
|
if ( articleData >= &chunk.front() + chunk.size() )
|
|
|
|
{
|
|
|
|
// chunks reader thinks it's okay since zero-sized records can exist,
|
|
|
|
// but we don't allow that.
|
|
|
|
throw ChunkedStorage::exAddressOutOfRange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( ChunkedStorage::exAddressOutOfRange & )
|
|
|
|
{
|
|
|
|
// Bad address
|
|
|
|
return new Dictionary::DataRequestInstant( false ); // No such resource
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk.back() = 0; // It must end with 0 anyway, but just in case
|
|
|
|
|
2011-09-09 12:05:28 +00:00
|
|
|
QDir dir( QDir::fromNativeSeparators( FsEncoding::decode( getDictionaryFilenames()[ 0 ].c_str() ) ) );
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
QString fileName = QDir::toNativeSeparators( dir.filePath( QString::fromUtf8( articleData ) ) );
|
|
|
|
|
|
|
|
// Now try loading that file
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
try
|
|
|
|
{
|
2011-09-09 12:05:28 +00:00
|
|
|
File::Class f( FsEncoding::encode( fileName ), "rb" );
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
sptr< Dictionary::DataRequestInstant > dr = new
|
|
|
|
Dictionary::DataRequestInstant( true );
|
|
|
|
|
|
|
|
vector< char > & data = dr->getData();
|
|
|
|
|
|
|
|
f.seekEnd();
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
data.resize( f.tell() );
|
|
|
|
|
|
|
|
f.rewind();
|
|
|
|
f.read( &data.front(), data.size() );
|
|
|
|
|
|
|
|
return dr;
|
|
|
|
}
|
|
|
|
catch( File::Ex )
|
|
|
|
{
|
|
|
|
return new Dictionary::DataRequestInstant( false ); // No such resource
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDir( QDir const & baseDir, QDir const & dir, IndexedWords & indexedWords,
|
|
|
|
uint32_t & soundsCount, ChunkedStorage::Writer & chunks )
|
|
|
|
{
|
|
|
|
QFileInfoList entries = dir.entryInfoList( QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot );
|
|
|
|
|
|
|
|
for( QFileInfoList::const_iterator i = entries.constBegin();
|
|
|
|
i != entries.constEnd(); ++i )
|
|
|
|
{
|
|
|
|
if ( i->isDir() )
|
|
|
|
addDir( baseDir, QDir( i->absoluteFilePath() ), indexedWords, soundsCount, chunks );
|
|
|
|
else
|
|
|
|
if ( Filetype::isNameOfSound( i->fileName().toUtf8().data() ) )
|
|
|
|
{
|
|
|
|
// Add this sound to index
|
|
|
|
|
|
|
|
string fileName = baseDir.relativeFilePath( i->filePath() ).toUtf8().data();
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
uint32_t articleOffset = chunks.startNewBlock();
|
|
|
|
chunks.addToBlock( fileName.c_str(), fileName.size() + 1 );
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
wstring name = gd::toWString( i->fileName() );
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
wstring::size_type pos = name.rfind( L'.' );
|
|
|
|
|
|
|
|
if ( pos != wstring::npos )
|
|
|
|
name.erase( pos );
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
indexedWords.addWord( name, articleOffset );
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
++soundsCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( Config::SoundDirs const & soundDirs,
|
|
|
|
string const & indicesDir,
|
|
|
|
Dictionary::Initializing & initializing )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > dictionaries;
|
|
|
|
|
|
|
|
for( Config::SoundDirs::const_iterator i = soundDirs.begin(); i != soundDirs.end();
|
|
|
|
++i )
|
|
|
|
{
|
|
|
|
QDir dir( i->path );
|
|
|
|
|
|
|
|
if ( !dir.exists() )
|
|
|
|
continue; // No such dir, no dictionary then
|
|
|
|
|
|
|
|
vector< string > dictFiles( 1,
|
|
|
|
QDir::toNativeSeparators( dir.canonicalPath() ).toLocal8Bit().data() );
|
|
|
|
|
|
|
|
dictFiles.push_back( "SoundDir" ); // A mixin
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
string dictId = Dictionary::makeDictionaryId( dictFiles );
|
|
|
|
|
|
|
|
dictFiles.pop_back(); // Remove mixin
|
|
|
|
|
|
|
|
string indexFile = indicesDir + dictId;
|
|
|
|
|
|
|
|
if ( Dictionary::needToRebuildIndex( dictFiles, indexFile ) || indexIsOldOrBad( indexFile ) )
|
|
|
|
{
|
|
|
|
// Building the index
|
|
|
|
|
2013-09-20 14:25:44 +00:00
|
|
|
qDebug() << "Sounds: Building the index for directory: " << i->path;
|
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
initializing.indexingDictionary( i->name.toUtf8().data() );
|
|
|
|
|
|
|
|
File::Class idx( indexFile, "wb" );
|
|
|
|
|
|
|
|
IdxHeader idxHeader;
|
|
|
|
|
|
|
|
memset( &idxHeader, 0, sizeof( idxHeader ) );
|
|
|
|
|
|
|
|
// We write a dummy header first. At the end of the process the header
|
|
|
|
// will be rewritten with the right values.
|
|
|
|
|
|
|
|
idx.write( idxHeader );
|
|
|
|
|
|
|
|
IndexedWords indexedWords;
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
ChunkedStorage::Writer chunks( idx );
|
|
|
|
|
|
|
|
uint32_t soundsCount = 0; // Header's one is packed, we can't ref it
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
addDir( dir, dir, indexedWords, soundsCount, chunks );
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
idxHeader.soundsCount = soundsCount;
|
|
|
|
|
|
|
|
// Finish with the chunks
|
|
|
|
|
|
|
|
idxHeader.chunksOffset = chunks.finish();
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-04-04 16:06:06 +00:00
|
|
|
// Build the index
|
|
|
|
|
2009-04-14 16:35:47 +00:00
|
|
|
IndexInfo idxInfo = BtreeIndexing::buildIndex( indexedWords, idx );
|
|
|
|
|
|
|
|
idxHeader.indexBtreeMaxElements = idxInfo.btreeMaxElements;
|
|
|
|
idxHeader.indexRootOffset = idxInfo.rootOffset;
|
2009-04-04 16:06:06 +00:00
|
|
|
|
|
|
|
// That concludes it. Update the header.
|
|
|
|
|
|
|
|
idxHeader.signature = Signature;
|
|
|
|
idxHeader.formatVersion = CurrentFormatVersion;
|
|
|
|
|
|
|
|
idx.rewind();
|
|
|
|
|
|
|
|
idx.write( &idxHeader, sizeof( idxHeader ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
dictionaries.push_back( new SoundDirDictionary( dictId,
|
|
|
|
i->name.toUtf8().data(),
|
|
|
|
indexFile,
|
2013-02-02 18:25:24 +00:00
|
|
|
dictFiles,
|
|
|
|
i->iconFilename ) );
|
2009-04-04 16:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|