2013-04-24 14:52:04 +00:00
|
|
|
/* 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>
|
|
|
|
|
2013-04-24 14:52:04 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
2013-04-24 16:01:44 +00:00
|
|
|
#include <QCryptographicHash>
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2013-05-30 13:24:21 +00:00
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
namespace VoiceEngines
|
|
|
|
{
|
2013-04-24 14:52:04 +00:00
|
|
|
|
|
|
|
using namespace Dictionary;
|
2013-04-24 16:01:44 +00:00
|
|
|
using std::string;
|
|
|
|
using std::map;
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2013-04-24 16:01:44 +00:00
|
|
|
inline string toMd5( QByteArray const & b )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
2013-04-24 16:01:44 +00:00
|
|
|
return string( QCryptographicHash::hash( b, QCryptographicHash::Md5 ).toHex().constData() );
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class VoiceEnginesDictionary: public Dictionary::Class
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Config::VoiceEngine voiceEngine;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
VoiceEnginesDictionary( Config::VoiceEngine const & voiceEngine ):
|
|
|
|
Dictionary::Class(
|
2022-07-31 01:19:50 +00:00
|
|
|
toMd5( voiceEngine.name.toUtf8() ),
|
2013-04-24 14:52:04 +00:00
|
|
|
vector< string >() ),
|
|
|
|
voiceEngine( voiceEngine )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
string getName() noexcept override
|
2013-04-24 14:52:04 +00:00
|
|
|
{ return voiceEngine.name.toUtf8().data(); }
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
map< Property, string > getProperties() noexcept override
|
2013-04-24 14:52:04 +00:00
|
|
|
{ return map< Property, string >(); }
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getArticleCount() noexcept override
|
2013-04-24 14:52:04 +00:00
|
|
|
{ return 0; }
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getWordCount() noexcept override
|
2013-04-24 14:52:04 +00:00
|
|
|
{ return 0; }
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< WordSearchRequest > prefixMatch( wstring const & word,
|
|
|
|
unsigned long maxResults ) override
|
2022-01-09 08:35:07 +00:00
|
|
|
;
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< DataRequest > getArticle( wstring const &,
|
2013-04-24 14:52:04 +00:00
|
|
|
vector< wstring > const & alts,
|
2022-12-29 07:07:40 +00:00
|
|
|
wstring const &, bool ) override
|
2022-01-09 08:35:07 +00:00
|
|
|
;
|
2013-04-24 14:52:04 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void loadIcon() noexcept override;
|
2013-04-24 14:52:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 14:52:04 +00:00
|
|
|
{
|
2013-04-24 16:01:44 +00:00
|
|
|
WordSearchRequestInstant * sr = new WordSearchRequestInstant();
|
2013-04-24 14:52:04 +00:00
|
|
|
sr->setUncertain( true );
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::shared_ptr<WordSearchRequestInstant>(sr);
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< Dictionary::DataRequest > VoiceEnginesDictionary::getArticle(
|
2018-06-13 16:00:42 +00:00
|
|
|
wstring const & word, vector< wstring > const &, wstring const &, bool )
|
2022-01-09 08:35:07 +00:00
|
|
|
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
string result;
|
|
|
|
string wordUtf8( Utf8::encode( word ) );
|
|
|
|
|
|
|
|
result += "<table class=\"voiceengines_play\"><tr>";
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl url;
|
2013-04-24 14:52:04 +00:00
|
|
|
url.setScheme( "gdtts" );
|
|
|
|
url.setHost( "localhost" );
|
2021-11-27 07:17:33 +00:00
|
|
|
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( wordUtf8.c_str() ) ) );
|
2013-04-24 14:52:04 +00:00
|
|
|
QList< QPair<QString, QString> > query;
|
|
|
|
query.push_back( QPair<QString, QString>( "engine", QString::fromStdString( getId() ) ) );
|
2021-11-27 07:17:33 +00:00
|
|
|
Utils::Url::setQueryItems( url, query );
|
2013-04-24 14:52:04 +00:00
|
|
|
|
|
|
|
string encodedUrl = url.toEncoded().data();
|
|
|
|
string ref = string( "\"" ) + encodedUrl + "\"";
|
|
|
|
result += addAudioLink( ref, getId() );
|
|
|
|
|
2023-03-05 20:20:05 +00:00
|
|
|
result += "<td><a href=" + ref + R"(><img src="qrc:///icons/playsound.png" border="0" alt="Play"/></a></td>)";
|
2013-04-24 14:52:04 +00:00
|
|
|
result += "<td><a href=" + ref + ">" + Html::escape( wordUtf8 ) + "</a></td>";
|
|
|
|
result += "</tr></table>";
|
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
sptr< DataRequestInstant > ret = std::make_shared<DataRequestInstant>( true );
|
2013-04-24 14:52:04 +00:00
|
|
|
ret->getData().resize( result.size() );
|
2013-04-24 16:01:44 +00:00
|
|
|
memcpy( &( ret->getData().front() ), result.data(), result.size() );
|
2013-04-24 14:52:04 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
void VoiceEnginesDictionary::loadIcon() noexcept
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
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" );
|
2013-04-24 14:52:04 +00:00
|
|
|
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
|
|
|
|
2013-04-24 14:52:04 +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 )
|
2013-04-24 14:52:04 +00:00
|
|
|
{
|
|
|
|
if ( i->enabled )
|
2022-11-29 03:54:31 +00:00
|
|
|
result.push_back( std::make_shared<VoiceEnginesDictionary>( *i ) );
|
2013-04-24 14:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|