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>
|
2012-12-03 19:30:42 +00:00
|
|
|
#include <QDir>
|
2012-12-03 12:48:31 +00:00
|
|
|
#include <QFileInfo>
|
2017-03-04 12:02:13 +00:00
|
|
|
#include "gddebug.hh"
|
2022-05-13 16:00:23 +00:00
|
|
|
#include "globalbroadcaster.h"
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
namespace WebSite {
|
|
|
|
|
|
|
|
using namespace Dictionary;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class WebSiteDictionary: public Dictionary::Class
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
QByteArray urlTemplate;
|
2022-05-17 12:44:48 +00:00
|
|
|
bool experimentalIframe;
|
2012-12-03 12:48:31 +00:00
|
|
|
QString iconFilename;
|
2016-06-17 14:27:48 +00:00
|
|
|
bool inside_iframe;
|
|
|
|
QNetworkAccessManager & netMgr;
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
WebSiteDictionary( string const & id, string const & name_,
|
2012-12-03 12:48:31 +00:00
|
|
|
QString const & urlTemplate_,
|
2016-06-17 14:27:48 +00:00
|
|
|
QString const & iconFilename_,
|
|
|
|
bool inside_iframe_,
|
|
|
|
QNetworkAccessManager & netMgr_ ):
|
2009-05-16 18:04:21 +00:00
|
|
|
Dictionary::Class( id, vector< string >() ),
|
|
|
|
name( name_ ),
|
2016-06-17 14:27:48 +00:00
|
|
|
iconFilename( iconFilename_ ),
|
|
|
|
inside_iframe( inside_iframe_ ),
|
2022-05-17 12:44:48 +00:00
|
|
|
netMgr( netMgr_ ),
|
|
|
|
experimentalIframe(false)
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
2022-05-17 12:44:48 +00:00
|
|
|
QString temp=urlTemplate_;
|
|
|
|
if(temp.endsWith("##")){
|
|
|
|
temp.chop(2);
|
|
|
|
}
|
2022-08-04 12:37:31 +00:00
|
|
|
//make this default.
|
|
|
|
experimentalIframe = true;
|
2022-05-17 12:44:48 +00:00
|
|
|
|
|
|
|
urlTemplate = QUrl( temp ).toEncoded() ;
|
|
|
|
|
|
|
|
dictionaryDescription = temp;
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
virtual string getName() noexcept
|
2009-05-16 18:04:21 +00:00
|
|
|
{ return name; }
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
virtual map< Property, string > getProperties() noexcept
|
2009-05-16 18:04:21 +00:00
|
|
|
{ return map< Property, string >(); }
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
virtual unsigned long getArticleCount() noexcept
|
2009-05-16 18:04:21 +00:00
|
|
|
{ return 0; }
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
virtual unsigned long getWordCount() noexcept
|
2009-05-16 18:04:21 +00:00
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
virtual sptr< WordSearchRequest > prefixMatch( wstring const & word,
|
2022-01-09 08:35:07 +00:00
|
|
|
unsigned long ) ;
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
virtual sptr< DataRequest > getArticle( wstring const &,
|
|
|
|
vector< wstring > const & alts,
|
2018-06-13 16:00:42 +00:00
|
|
|
wstring const & context, bool )
|
2022-01-09 08:35:07 +00:00
|
|
|
;
|
2012-12-03 12:47:43 +00:00
|
|
|
|
2022-01-09 08:35:07 +00:00
|
|
|
virtual sptr< Dictionary::DataRequest > getResource( string const & name ) ;
|
2016-06-18 11:56:13 +00:00
|
|
|
|
|
|
|
void isolateWebCSS( QString & css );
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
protected:
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
virtual void loadIcon() noexcept;
|
2009-05-16 18:04:21 +00:00
|
|
|
};
|
|
|
|
|
2010-06-22 12:43:11 +00:00
|
|
|
sptr< WordSearchRequest > WebSiteDictionary::prefixMatch( wstring const & /*word*/,
|
2022-01-09 08:35:07 +00:00
|
|
|
unsigned long )
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-18 11:56:13 +00:00
|
|
|
void WebSiteDictionary::isolateWebCSS( QString & css )
|
|
|
|
{
|
|
|
|
isolateCSS( css, ".website" );
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
class WebSiteArticleRequest: public WebSiteDataRequestSlots
|
|
|
|
{
|
|
|
|
QNetworkReply * netReply;
|
|
|
|
QString url;
|
|
|
|
Class * dictPtr;
|
|
|
|
QNetworkAccessManager & mgr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
WebSiteArticleRequest( QString const & url, QNetworkAccessManager & _mgr,
|
|
|
|
Class * dictPtr_ );
|
|
|
|
~WebSiteArticleRequest()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual void cancel();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
virtual void requestFinished( QNetworkReply * );
|
2016-07-12 14:51:44 +00:00
|
|
|
static QTextCodec * codecForHtml( QByteArray const & ba );
|
2016-06-17 14:27:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void WebSiteArticleRequest::cancel()
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSiteArticleRequest::WebSiteArticleRequest( QString const & url_,
|
|
|
|
QNetworkAccessManager & _mgr,
|
|
|
|
Class * dictPtr_ ):
|
|
|
|
url( url_ ), dictPtr( dictPtr_ ), mgr( _mgr )
|
|
|
|
{
|
|
|
|
connect( &mgr, SIGNAL( finished( QNetworkReply * ) ),
|
|
|
|
this, SLOT( requestFinished( QNetworkReply * ) ),
|
|
|
|
Qt::QueuedConnection );
|
|
|
|
|
|
|
|
QUrl reqUrl( url );
|
|
|
|
|
2022-04-14 14:45:34 +00:00
|
|
|
auto request = QNetworkRequest( reqUrl );
|
|
|
|
request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy );
|
|
|
|
netReply = mgr.get( request );
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL
|
|
|
|
connect( netReply, SIGNAL( sslErrors( QList< QSslError > ) ),
|
|
|
|
netReply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-12 14:51:44 +00:00
|
|
|
QTextCodec * WebSiteArticleRequest::codecForHtml( QByteArray const & ba )
|
|
|
|
{
|
|
|
|
return QTextCodec::codecForHtml( ba, 0 );
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
void WebSiteArticleRequest::requestFinished( QNetworkReply * r )
|
|
|
|
{
|
|
|
|
if ( isFinished() ) // Was cancelled
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( r != netReply )
|
|
|
|
{
|
|
|
|
// Well, that's not our reply, don't do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( netReply->error() == QNetworkReply::NoError )
|
|
|
|
{
|
|
|
|
// Check for redirect reply
|
|
|
|
|
|
|
|
QVariant possibleRedirectUrl = netReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
|
|
|
|
QUrl redirectUrl = possibleRedirectUrl.toUrl();
|
|
|
|
if( !redirectUrl.isEmpty() )
|
|
|
|
{
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( netReply, 0, 0, 0 );
|
2016-06-17 14:27:48 +00:00
|
|
|
netReply->deleteLater();
|
|
|
|
netReply = mgr.get( QNetworkRequest( redirectUrl ) );
|
|
|
|
#ifndef QT_NO_OPENSSL
|
|
|
|
connect( netReply, SIGNAL( sslErrors( QList< QSslError > ) ),
|
|
|
|
netReply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle reply data
|
|
|
|
|
|
|
|
QByteArray replyData = netReply->readAll();
|
|
|
|
QString articleString;
|
|
|
|
|
2016-07-12 14:51:44 +00:00
|
|
|
QTextCodec * codec = WebSiteArticleRequest::codecForHtml( replyData );
|
2016-06-17 14:27:48 +00:00
|
|
|
if( codec )
|
|
|
|
articleString = codec->toUnicode( replyData );
|
|
|
|
else
|
|
|
|
articleString = QString::fromUtf8( replyData );
|
|
|
|
|
|
|
|
// Change links from relative to absolute
|
|
|
|
|
|
|
|
QString root = netReply->url().scheme() + "://" + netReply->url().host();
|
|
|
|
QString base = root + netReply->url().path();
|
|
|
|
while( !base.isEmpty() && !base.endsWith( "/" ) )
|
|
|
|
base.chop( 1 );
|
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression tags( "<\\s*(a|link|img|script)\\s+[^>]*(src|href)\\s*=\\s*['\"][^>]+>",
|
2018-02-28 14:15:27 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption );
|
2018-02-27 16:42:21 +00:00
|
|
|
QRegularExpression links( "\\b(src|href)\\s*=\\s*(['\"])([^'\"]+['\"])",
|
2018-02-28 14:15:27 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption );
|
2018-02-27 16:42:21 +00:00
|
|
|
int pos = 0;
|
|
|
|
QString articleNewString;
|
|
|
|
QRegularExpressionMatchIterator it = tags.globalMatch( articleString );
|
|
|
|
while( it.hasNext() )
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch match = it.next();
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = match.capturedEnd();
|
|
|
|
|
|
|
|
QString tag = match.captured();
|
|
|
|
|
|
|
|
QRegularExpressionMatch match_links = links.match( tag );
|
|
|
|
if( !match_links.hasMatch() )
|
|
|
|
{
|
|
|
|
articleNewString += tag;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString url = match_links.captured( 3 );
|
|
|
|
|
|
|
|
if( url.indexOf( ":/" ) >= 0 || url.indexOf( "data:" ) >= 0
|
|
|
|
|| url.indexOf( "mailto:" ) >= 0 || url.startsWith( "#" )
|
|
|
|
|| url.startsWith( "javascript:" ) )
|
|
|
|
{
|
|
|
|
// External link, anchor or base64-encoded data
|
|
|
|
articleNewString += tag;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString newUrl = match_links.captured( 1 ) + "=" + match_links.captured( 2 );
|
|
|
|
if( url.startsWith( "//" ) )
|
|
|
|
newUrl += netReply->url().scheme() + ":";
|
|
|
|
else
|
|
|
|
if( url.startsWith( "/" ) )
|
|
|
|
newUrl += root;
|
|
|
|
else
|
|
|
|
newUrl += base;
|
|
|
|
newUrl += match_links.captured( 3 );
|
|
|
|
|
|
|
|
tag.replace( match_links.capturedStart(), match_links.capturedLength(), newUrl );
|
|
|
|
articleNewString += tag;
|
|
|
|
}
|
|
|
|
if( pos )
|
|
|
|
{
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewString += articleString.mid( pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
articleString = articleNewString;
|
|
|
|
articleNewString.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect CSS links to own handler
|
|
|
|
|
|
|
|
QString prefix = QString( "bres://" ) + dictPtr->getId().c_str() + "/";
|
|
|
|
QRegularExpression linkTags( "(<\\s*link\\s[^>]*rel\\s*=\\s*['\"]stylesheet['\"]\\s+[^>]*href\\s*=\\s*['\"])([^'\"]+)://([^'\"]+['\"][^>]+>)",
|
2018-02-28 14:15:27 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = 0;
|
|
|
|
it = linkTags.globalMatch( articleString );
|
2022-03-12 11:41:24 +00:00
|
|
|
|
2018-02-27 16:42:21 +00:00
|
|
|
while( it.hasNext() )
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch match = it.next();
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
pos = match.capturedEnd();
|
|
|
|
|
|
|
|
QString newTag = match.captured( 1 ) + prefix + match.captured( 2 )
|
|
|
|
+ "/" + match.captured( 3 );
|
|
|
|
articleNewString += newTag;
|
|
|
|
}
|
|
|
|
if( pos )
|
|
|
|
{
|
2022-02-27 05:17:37 +00:00
|
|
|
articleNewString += articleString.mid( pos );
|
2018-02-27 16:42:21 +00:00
|
|
|
articleString = articleNewString;
|
|
|
|
articleNewString.clear();
|
|
|
|
}
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
// See Issue #271: A mechanism to clean-up invalid HTML cards.
|
2022-04-10 12:07:00 +00:00
|
|
|
articleString += "</font>""</font>""</font>""</font>""</font>""</font>"
|
|
|
|
"</font>""</font>""</font>""</font>""</font>""</font>"
|
|
|
|
"</b></b></b></b></b></b></b></b>"
|
|
|
|
"</i></i></i></i></i></i></i></i>"
|
|
|
|
"</a></a></a></a></a></a></a></a>";
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
QByteArray articleBody = articleString.toUtf8();
|
|
|
|
|
2016-06-18 11:56:13 +00:00
|
|
|
QString divStr = QString( "<div class=\"website\"" );
|
2016-06-17 14:27:48 +00:00
|
|
|
divStr += dictPtr->isToLanguageRTL() ? " dir=\"rtl\">" : ">";
|
|
|
|
|
|
|
|
articleBody.prepend( divStr.toUtf8() );
|
|
|
|
articleBody.append( "</div>" );
|
|
|
|
|
2016-06-17 19:21:59 +00:00
|
|
|
articleBody.prepend( "<div class=\"website_padding\"></div>" );
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t prevSize = data.size();
|
|
|
|
|
|
|
|
data.resize( prevSize + articleBody.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + prevSize, articleBody.data(), articleBody.size() );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2017-03-04 12:02:13 +00:00
|
|
|
{
|
|
|
|
if( netReply->url().scheme() == "file" )
|
|
|
|
{
|
|
|
|
gdWarning( "WebSites: Failed loading article from \"%s\", reason: %s\n", dictPtr->getName().c_str(),
|
|
|
|
netReply->errorString().toUtf8().data() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setErrorString( netReply->errorString() );
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 14:27:48 +00:00
|
|
|
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( netReply, 0, 0, 0 );
|
2016-06-17 14:27:48 +00:00
|
|
|
netReply->deleteLater();
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str,
|
|
|
|
vector< wstring > const &,
|
2018-06-13 16:00:42 +00:00
|
|
|
wstring const & context, bool )
|
2022-01-09 08:35:07 +00:00
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
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
|
|
|
|
2013-03-30 10:00:28 +00:00
|
|
|
QTextCodec *codec = QTextCodec::codecForName( "Windows-1251" );
|
|
|
|
if( codec )
|
|
|
|
urlString.replace( "%25GD1251%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
codec = QTextCodec::codecForName( "Big-5" );
|
|
|
|
if( codec )
|
|
|
|
urlString.replace( "%25GDBIG5%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
codec = QTextCodec::codecForName( "Big5-HKSCS" );
|
|
|
|
if( codec )
|
|
|
|
urlString.replace( "%25GDBIG5HKSCS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
codec = QTextCodec::codecForName( "Shift-JIS" );
|
|
|
|
if( codec )
|
|
|
|
urlString.replace( "%25GDSHIFTJIS%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
|
|
|
codec = QTextCodec::codecForName( "GB18030" );
|
|
|
|
if( codec )
|
|
|
|
urlString.replace( "%25GDGBK%25", codec->fromUnicode( inputWord ).toPercentEncoding() );
|
|
|
|
|
2009-08-20 11:37:07 +00:00
|
|
|
|
|
|
|
// Handle all ISO-8859 encodings
|
|
|
|
for( int x = 1; x <= 16; ++x )
|
|
|
|
{
|
2013-03-30 10:00:28 +00:00
|
|
|
codec = QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toLatin1() );
|
|
|
|
if( codec )
|
2022-01-08 13:16:22 +00:00
|
|
|
urlString.replace( QString( "%25GDISO%1%25" ).arg( x ).toUtf8(), codec->fromUnicode( inputWord ).toPercentEncoding() );
|
2009-08-20 11:37:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
if( inside_iframe )
|
|
|
|
{
|
|
|
|
// Just insert link in <iframe> tag
|
|
|
|
|
2016-06-18 11:56:13 +00:00
|
|
|
sptr< DataRequestInstant > dr = new DataRequestInstant( true );
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
string result = "<div class=\"website_padding\"></div>";
|
|
|
|
|
2022-05-17 12:44:48 +00:00
|
|
|
//heuristic add url to global whitelist.
|
2022-05-13 16:00:23 +00:00
|
|
|
QUrl url(urlString);
|
|
|
|
GlobalBroadcaster::instance()->addWhitelist(url.host());
|
|
|
|
|
2022-05-17 12:44:48 +00:00
|
|
|
QString encodeUrl;
|
|
|
|
if(experimentalIframe){
|
|
|
|
encodeUrl="ifr://localhost?url="+QUrl::toPercentEncoding( urlString);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
encodeUrl = urlString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
result += string( "<iframe id=\"gdexpandframe-" ) + getId() +
|
2022-05-17 12:44:48 +00:00
|
|
|
"\" src=\""+encodeUrl.toStdString() +
|
2016-06-17 14:27:48 +00:00
|
|
|
"\" onmouseover=\"processIframeMouseOver('gdexpandframe-" + getId() + "');\" "
|
|
|
|
"onmouseout=\"processIframeMouseOut();\" "
|
|
|
|
"scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" "
|
2022-04-17 14:28:14 +00:00
|
|
|
"frameborder=\"0\" vspace=\"0\" hspace=\"0\""
|
2022-08-13 10:24:29 +00:00
|
|
|
"style=\"overflow:visible; width:100%; display:block;\" sandbox=\"allow-scripts allow-popups\">"
|
2016-06-17 14:27:48 +00:00
|
|
|
"</iframe>";
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
dr->getData().resize( result.size() );
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
memcpy( &( dr->getData().front() ), result.data(), result.size() );
|
|
|
|
|
|
|
|
return dr;
|
|
|
|
}
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
// To load data from site
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
return new WebSiteArticleRequest( urlString, netMgr, this );
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 11:56:13 +00:00
|
|
|
class WebSiteResourceRequest: public WebSiteDataRequestSlots
|
|
|
|
{
|
|
|
|
QNetworkReply * netReply;
|
|
|
|
QString url;
|
|
|
|
WebSiteDictionary * dictPtr;
|
|
|
|
QNetworkAccessManager & mgr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
WebSiteResourceRequest( QString const & url_, QNetworkAccessManager & _mgr,
|
|
|
|
WebSiteDictionary * dictPtr_ );
|
|
|
|
~WebSiteResourceRequest()
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual void cancel();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
virtual void requestFinished( QNetworkReply * );
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSiteResourceRequest::WebSiteResourceRequest( QString const & url_,
|
|
|
|
QNetworkAccessManager & _mgr,
|
|
|
|
WebSiteDictionary * dictPtr_ ):
|
|
|
|
url( url_ ), dictPtr( dictPtr_ ), mgr( _mgr )
|
|
|
|
{
|
|
|
|
connect( &mgr, SIGNAL( finished( QNetworkReply * ) ),
|
|
|
|
this, SLOT( requestFinished( QNetworkReply * ) ),
|
|
|
|
Qt::QueuedConnection );
|
|
|
|
|
|
|
|
QUrl reqUrl( url );
|
|
|
|
|
|
|
|
netReply = mgr.get( QNetworkRequest( reqUrl ) );
|
|
|
|
|
|
|
|
#ifndef QT_NO_OPENSSL
|
|
|
|
connect( netReply, SIGNAL( sslErrors( QList< QSslError > ) ),
|
|
|
|
netReply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSiteResourceRequest::cancel()
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSiteResourceRequest::requestFinished( QNetworkReply * r )
|
|
|
|
{
|
|
|
|
if ( isFinished() ) // Was cancelled
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( r != netReply )
|
|
|
|
{
|
|
|
|
// Well, that's not our reply, don't do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( netReply->error() == QNetworkReply::NoError )
|
|
|
|
{
|
|
|
|
// Check for redirect reply
|
|
|
|
|
|
|
|
QVariant possibleRedirectUrl = netReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
|
|
|
|
QUrl redirectUrl = possibleRedirectUrl.toUrl();
|
|
|
|
if( !redirectUrl.isEmpty() )
|
|
|
|
{
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( netReply, 0, 0, 0 );
|
2016-06-18 11:56:13 +00:00
|
|
|
netReply->deleteLater();
|
|
|
|
netReply = mgr.get( QNetworkRequest( redirectUrl ) );
|
|
|
|
#ifndef QT_NO_OPENSSL
|
|
|
|
connect( netReply, SIGNAL( sslErrors( QList< QSslError > ) ),
|
|
|
|
netReply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle reply data
|
|
|
|
|
|
|
|
QByteArray replyData = netReply->readAll();
|
|
|
|
QString cssString = QString::fromUtf8( replyData );
|
|
|
|
|
|
|
|
dictPtr->isolateWebCSS( cssString );
|
|
|
|
|
|
|
|
QByteArray cssData = cssString.toUtf8();
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t prevSize = data.size();
|
|
|
|
|
|
|
|
data.resize( prevSize + cssData.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + prevSize, cssData.data(), cssData.size() );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setErrorString( netReply->errorString() );
|
|
|
|
|
2017-03-20 14:31:02 +00:00
|
|
|
disconnect( netReply, 0, 0, 0 );
|
2016-06-18 11:56:13 +00:00
|
|
|
netReply->deleteLater();
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2022-01-09 08:35:07 +00:00
|
|
|
sptr< Dictionary::DataRequest > WebSiteDictionary::getResource( string const & name )
|
2016-06-18 11:56:13 +00:00
|
|
|
{
|
|
|
|
QString link = QString::fromUtf8( name.c_str() );
|
|
|
|
int pos = link.indexOf( '/' );
|
|
|
|
if( pos > 0 )
|
|
|
|
link.replace( pos, 1, "://" );
|
|
|
|
return new WebSiteResourceRequest( link, netMgr, this );
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:28:41 +00:00
|
|
|
void WebSiteDictionary::loadIcon() noexcept
|
2012-12-03 12:48:31 +00:00
|
|
|
{
|
2013-01-31 22:30:11 +00:00
|
|
|
if ( dictionaryIconLoaded )
|
|
|
|
return;
|
|
|
|
|
2012-12-03 12:48:31 +00:00
|
|
|
if( !iconFilename.isEmpty() )
|
|
|
|
{
|
|
|
|
QFileInfo fInfo( QDir( Config::getConfigDir() ), iconFilename );
|
|
|
|
if( fInfo.isFile() )
|
|
|
|
loadIconFromFile( fInfo.absoluteFilePath(), true );
|
|
|
|
}
|
|
|
|
if( dictionaryIcon.isNull() )
|
2022-01-25 14:54:17 +00:00
|
|
|
dictionaryIcon = dictionaryNativeIcon = QIcon(":/icons/webdict.svg");
|
2012-12-03 12:48:31 +00:00
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ws,
|
|
|
|
QNetworkAccessManager & mgr )
|
2022-01-09 08:35:07 +00:00
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > result;
|
|
|
|
|
2012-12-10 12:49:45 +00:00
|
|
|
for( int x = 0; x < ws.size(); ++x )
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
if ( ws[ x ].enabled )
|
|
|
|
result.push_back( new WebSiteDictionary( ws[ x ].id.toUtf8().data(),
|
|
|
|
ws[ x ].name.toUtf8().data(),
|
2012-12-03 12:48:31 +00:00
|
|
|
ws[ x ].url,
|
2016-06-17 14:27:48 +00:00
|
|
|
ws[ x ].iconFilename,
|
|
|
|
ws[ x ].inside_iframe,
|
|
|
|
mgr )
|
|
|
|
);
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|