goldendict-ng/voiceengines.cc

145 lines
3.9 KiB
C++
Raw Normal View History

/* This file is (c) 2013 Timon Wong <timon86.wang@gmail.com>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "voiceengines.hh"
#include "audiolink.hh"
#include "htmlescape.hh"
#include "utf8.hh"
#include "wstring_qt.hh"
2013-04-24 16:01:44 +00:00
#include <string>
#include <map>
#include <QDir>
#include <QFileInfo>
2013-04-24 16:01:44 +00:00
#include <QCryptographicHash>
#include "utils.hh"
2013-04-24 16:01:44 +00:00
namespace VoiceEngines
{
using namespace Dictionary;
2013-04-24 16:01:44 +00:00
using std::string;
using std::map;
2013-04-24 16:01:44 +00:00
inline string toMd5( QByteArray const & b )
{
2013-04-24 16:01:44 +00:00
return string( QCryptographicHash::hash( b, QCryptographicHash::Md5 ).toHex().constData() );
}
class VoiceEnginesDictionary: public Dictionary::Class
{
private:
Config::VoiceEngine voiceEngine;
public:
VoiceEnginesDictionary( Config::VoiceEngine const & voiceEngine ):
Dictionary::Class(
toMd5( voiceEngine.name.toUtf8() ),
vector< string >() ),
voiceEngine( voiceEngine )
{
}
string getName() noexcept override
{ return voiceEngine.name.toUtf8().data(); }
map< Property, string > getProperties() noexcept override
{ return map< Property, string >(); }
unsigned long getArticleCount() noexcept override
{ return 0; }
unsigned long getWordCount() noexcept override
{ return 0; }
sptr< WordSearchRequest > prefixMatch( wstring const & word,
unsigned long maxResults ) override
2022-01-09 08:35:07 +00:00
;
sptr< DataRequest > getArticle( wstring const &,
vector< wstring > const & alts,
wstring const &, bool ) override
2022-01-09 08:35:07 +00:00
;
protected:
void loadIcon() noexcept override;
};
sptr< WordSearchRequest > VoiceEnginesDictionary::prefixMatch( wstring const & /*word*/,
2013-04-24 16:01:44 +00:00
unsigned long /*maxResults*/ )
2022-01-09 08:35:07 +00:00
{
2013-04-24 16:01:44 +00:00
WordSearchRequestInstant * sr = new WordSearchRequestInstant();
sr->setUncertain( true );
return std::shared_ptr<WordSearchRequestInstant>(sr);
}
sptr< Dictionary::DataRequest > VoiceEnginesDictionary::getArticle(
wstring const & word, vector< wstring > const &, wstring const &, bool )
2022-01-09 08:35:07 +00:00
{
string result;
string wordUtf8( Utf8::encode( word ) );
result += "<table class=\"voiceengines_play\"><tr>";
QUrl url;
url.setScheme( "gdtts" );
url.setHost( "localhost" );
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( wordUtf8.c_str() ) ) );
QList< QPair<QString, QString> > query;
query.push_back( QPair<QString, QString>( "engine", QString::fromStdString( getId() ) ) );
Utils::Url::setQueryItems( url, query );
string encodedUrl = url.toEncoded().data();
string ref = string( "\"" ) + encodedUrl + "\"";
result += addAudioLink( ref, getId() );
result += "<td><a href=" + ref + R"(><img src="qrc:///icons/playsound.png" border="0" alt="Play"/></a></td>)";
result += "<td><a href=" + ref + ">" + Html::escape( wordUtf8 ) + "</a></td>";
result += "</tr></table>";
sptr< DataRequestInstant > ret = std::make_shared<DataRequestInstant>( true );
ret->getData().resize( result.size() );
2013-04-24 16:01:44 +00:00
memcpy( &( ret->getData().front() ), result.data(), result.size() );
return ret;
}
void VoiceEnginesDictionary::loadIcon() noexcept
{
if ( dictionaryIconLoaded )
return;
if ( !voiceEngine.iconFilename.isEmpty() )
{
QFileInfo fInfo( QDir( Config::getConfigDir() ), voiceEngine.iconFilename );
if ( fInfo.isFile() )
loadIconFromFile( fInfo.absoluteFilePath(), true );
}
if ( dictionaryIcon.isNull() )
2022-04-21 16:24:50 +00:00
dictionaryIcon = dictionaryNativeIcon = QIcon( ":/icons/text2speech.svg" );
dictionaryIconLoaded = true;
}
vector< sptr< Dictionary::Class > > makeDictionaries(
2013-04-24 16:01:44 +00:00
Config::VoiceEngines const & voiceEngines )
2022-01-09 08:35:07 +00:00
{
vector< sptr< Dictionary::Class > > result;
2013-04-24 16:01:44 +00:00
for ( Config::VoiceEngines::const_iterator i = voiceEngines.begin(); i != voiceEngines.end(); ++i )
{
if ( i->enabled )
result.push_back( std::make_shared<VoiceEnginesDictionary>( *i ) );
}
return result;
}
}