forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoiceengines.cc
144 lines (112 loc) · 3.96 KB
/
voiceengines.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This file is (c) 2013 Timon Wong <[email protected]>
* 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"
#include <string>
#include <map>
#include <QDir>
#include <QFileInfo>
#include <QCryptographicHash>
#include "qt4x5.hh"
namespace VoiceEngines
{
using namespace Dictionary;
using std::string;
using std::map;
inline string toMd5( QByteArray const & b )
{
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.id.toUtf8() ),
vector< string >() ),
voiceEngine( voiceEngine )
{
}
virtual string getName() throw()
{ return voiceEngine.name.toUtf8().data(); }
virtual map< Property, string > getProperties() throw()
{ return map< Property, string >(); }
virtual unsigned long getArticleCount() throw()
{ return 0; }
virtual unsigned long getWordCount() throw()
{ return 0; }
virtual sptr< WordSearchRequest > prefixMatch( wstring const & word,
unsigned long maxResults )
throw( std::exception );
virtual sptr< DataRequest > getArticle( wstring const &,
vector< wstring > const & alts,
wstring const & )
throw( std::exception );
protected:
virtual void loadIcon() throw();
};
sptr< WordSearchRequest > VoiceEnginesDictionary::prefixMatch( wstring const & /*word*/,
unsigned long /*maxResults*/ )
throw( std::exception )
{
WordSearchRequestInstant * sr = new WordSearchRequestInstant();
sr->setUncertain( true );
return sr;
}
sptr< Dictionary::DataRequest > VoiceEnginesDictionary::getArticle(
wstring const & word, vector< wstring > const &, wstring const & )
throw( std::exception )
{
string result;
string wordUtf8( Utf8::encode( word ) );
result += "<table class=\"voiceengines_play\"><tr>";
QUrl url;
url.setScheme( "gdtts" );
url.setHost( "localhost" );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( wordUtf8.c_str() ) ) );
QList< QPair<QString, QString> > query;
query.push_back( QPair<QString, QString>( "engine", QString::fromStdString( getId() ) ) );
Qt4x5::Url::setQueryItems( url, query );
string encodedUrl = url.toEncoded().data();
string ref = string( "\"" ) + encodedUrl + "\"";
result += addAudioLink( ref, getId() );
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( wordUtf8 ) + "</a></td>";
result += "</tr></table>";
sptr< DataRequestInstant > ret = new DataRequestInstant( true );
ret->getData().resize( result.size() );
memcpy( &( ret->getData().front() ), result.data(), result.size() );
return ret;
}
void VoiceEnginesDictionary::loadIcon() throw()
{
if ( dictionaryIconLoaded )
return;
if ( !voiceEngine.iconFilename.isEmpty() )
{
QFileInfo fInfo( QDir( Config::getConfigDir() ), voiceEngine.iconFilename );
if ( fInfo.isFile() )
loadIconFromFile( fInfo.absoluteFilePath(), true );
}
if ( dictionaryIcon.isNull() )
dictionaryIcon = dictionaryNativeIcon = QIcon( ":/icons/playsound.png" );
dictionaryIconLoaded = true;
}
vector< sptr< Dictionary::Class > > makeDictionaries(
Config::VoiceEngines const & voiceEngines )
throw( std::exception )
{
vector< sptr< Dictionary::Class > > result;
for ( Config::VoiceEngines::const_iterator i = voiceEngines.begin(); i != voiceEngines.end(); ++i )
{
if ( i->enabled )
result.push_back( new VoiceEnginesDictionary( *i ) );
}
return result;
}
}