2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-05-16 18:04:21 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "website.hh"
|
|
|
|
#include "wstring_qt.hh"
|
2009-05-29 19:48:50 +00:00
|
|
|
#include "utf8.hh"
|
2009-05-16 18:04:21 +00:00
|
|
|
#include <QUrl>
|
|
|
|
#include <QTextCodec>
|
|
|
|
|
|
|
|
namespace WebSite {
|
|
|
|
|
|
|
|
using namespace Dictionary;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WebSiteDictionary: public Dictionary::Class
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
QByteArray urlTemplate;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
WebSiteDictionary( string const & id, string const & name_,
|
|
|
|
QString const & urlTemplate_ ):
|
|
|
|
Dictionary::Class( id, vector< string >() ),
|
|
|
|
name( name_ ),
|
|
|
|
urlTemplate( QUrl( urlTemplate_ ).toEncoded() )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual string getName() throw()
|
|
|
|
{ return name; }
|
|
|
|
|
|
|
|
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 QIcon getIcon() throw()
|
|
|
|
{ return QIcon(":/icons/internet.png"); }
|
|
|
|
|
|
|
|
virtual sptr< WordSearchRequest > prefixMatch( wstring const & word,
|
|
|
|
unsigned long ) throw( std::exception );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
virtual sptr< DataRequest > getArticle( wstring const &,
|
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & context )
|
2009-05-16 18:04:21 +00:00
|
|
|
throw( std::exception );
|
|
|
|
};
|
|
|
|
|
2010-06-22 12:43:11 +00:00
|
|
|
sptr< WordSearchRequest > WebSiteDictionary::prefixMatch( wstring const & /*word*/,
|
2009-05-16 18:04:21 +00:00
|
|
|
unsigned long ) throw( std::exception )
|
|
|
|
{
|
|
|
|
sptr< WordSearchRequestInstant > sr = new WordSearchRequestInstant;
|
|
|
|
|
2010-06-22 12:43:11 +00:00
|
|
|
sr->setUncertain( true );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
|
|
|
|
vector< wstring > const &,
|
|
|
|
wstring const & context )
|
2009-05-16 18:04:21 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
sptr< DataRequestInstant > dr = new DataRequestInstant( true );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
QByteArray urlString;
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
// Context contains the right url to go to
|
|
|
|
if ( context.size() )
|
|
|
|
urlString = Utf8::encode( context ).c_str();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
urlString = urlTemplate;
|
|
|
|
|
|
|
|
QString inputWord = gd::toQString( str );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
urlString.replace( "%25GDWORD%25", inputWord.toUtf8().toPercentEncoding() );
|
2011-07-28 13:32:34 +00:00
|
|
|
urlString.replace( "%25GD1251%25",
|
|
|
|
QTextCodec::codecForName( "Windows-1251" )->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
urlString.replace( "%25GDBIG5%25",
|
|
|
|
QTextCodec::codecForName( "Big-5" )->fromUnicode( inputWord ).toPercentEncoding() );
|
2009-08-20 11:37:07 +00:00
|
|
|
|
|
|
|
// Handle all ISO-8859 encodings
|
|
|
|
for( int x = 1; x <= 16; ++x )
|
|
|
|
{
|
|
|
|
urlString.replace( QString( "%25GDISO%1%25" ).arg( x ),
|
|
|
|
QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toAscii() )->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
if ( x == 10 )
|
|
|
|
x = 12; // Skip encodings 11..12, they don't exist
|
|
|
|
}
|
2009-05-29 19:48:50 +00:00
|
|
|
}
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
string result = "<div class=\"website_padding\"></div>";
|
|
|
|
|
|
|
|
result += string( "<iframe id=\"gdexpandframe-" ) + getId() +
|
|
|
|
"\" src=\"" + urlString.data() +
|
2011-07-30 20:28:24 +00:00
|
|
|
"\" onmouseover=\"processIframeMouseOver('gdexpandframe-" + getId() + "');\" "
|
|
|
|
"onmouseout=\"processIframeMouseOut();\" "
|
|
|
|
"scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" "
|
2009-05-16 18:04:21 +00:00
|
|
|
"frameborder=\"0\" vspace=\"0\" hspace=\"0\" "
|
|
|
|
"style=\"overflow:visible; width:100%; display:none;\">"
|
|
|
|
"</iframe>";
|
|
|
|
|
|
|
|
dr->getData().resize( result.size() );
|
|
|
|
|
|
|
|
memcpy( &( dr->getData().front() ), result.data(), result.size() );
|
|
|
|
|
|
|
|
return dr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ws )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > result;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < ws.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( ws[ x ].enabled )
|
|
|
|
result.push_back( new WebSiteDictionary( ws[ x ].id.toUtf8().data(),
|
|
|
|
ws[ x ].name.toUtf8().data(),
|
|
|
|
ws[ x ].url ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|