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"
|
2023-04-17 21:56:55 +00:00
|
|
|
#include "globalbroadcaster.hh"
|
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
|
|
|
{
|
2023-02-16 07:58:44 +00:00
|
|
|
if ( urlTemplate_.startsWith( "http://" ) || urlTemplate_.startsWith( "https://" ) ) {
|
|
|
|
experimentalIframe = true;
|
2022-05-17 12:44:48 +00:00
|
|
|
}
|
2023-02-16 07:58:44 +00:00
|
|
|
//else file:/// local dictionary file path
|
2022-05-17 12:44:48 +00:00
|
|
|
|
2023-02-16 08:11:27 +00:00
|
|
|
urlTemplate = QUrl( urlTemplate_ ).toEncoded();
|
2023-02-16 07:58:44 +00:00
|
|
|
dictionaryDescription = urlTemplate_;
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
string getName() noexcept override
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
map< Property, string > getProperties() noexcept override
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
return map< Property, string >();
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getArticleCount() noexcept override
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
unsigned long getWordCount() noexcept override
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< WordSearchRequest > prefixMatch( wstring const & word, unsigned long ) override;
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< DataRequest >
|
|
|
|
getArticle( wstring const &, vector< wstring > const & alts, wstring const & context, bool ) override;
|
2012-12-03 12:47:43 +00:00
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
sptr< Dictionary::DataRequest > getResource( string const & name ) override;
|
2016-06-18 11:56:13 +00:00
|
|
|
|
|
|
|
void isolateWebCSS( QString & css );
|
|
|
|
|
2012-12-03 12:47:43 +00:00
|
|
|
protected:
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void loadIcon() noexcept override;
|
2009-05-16 18:04:21 +00:00
|
|
|
};
|
|
|
|
|
2023-12-12 02:20:34 +00:00
|
|
|
class WebSiteDataRequestSlots: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
protected slots:
|
|
|
|
|
|
|
|
virtual void requestFinished( QNetworkReply * ) {}
|
|
|
|
};
|
|
|
|
|
2010-06-22 12:43:11 +00:00
|
|
|
sptr< WordSearchRequest > WebSiteDictionary::prefixMatch( wstring const & /*word*/, unsigned long )
|
2009-05-16 18:04:21 +00:00
|
|
|
{
|
2022-11-29 03:54:31 +00:00
|
|
|
sptr< WordSearchRequestInstant > sr = std::make_shared< WordSearchRequestInstant >();
|
2009-05-16 18:04:21 +00:00
|
|
|
|
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() {}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void cancel() override;
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void requestFinished( QNetworkReply * ) override;
|
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
|
|
|
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2016-06-17 14:27:48 +00:00
|
|
|
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 ) );
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2016-06-17 14:27:48 +00:00
|
|
|
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 );
|
|
|
|
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression tags( R"(<\s*(a|link|img|script)\s+[^>]*(src|href)\s*=\s*['"][^>]+>)",
|
2018-02-28 14:15:27 +00:00
|
|
|
QRegularExpression::CaseInsensitiveOption );
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression links( R"(\b(src|href)\s*=\s*(['"])([^'"]+['"]))", 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() + "/";
|
2022-12-24 22:01:50 +00:00
|
|
|
QRegularExpression linkTags(
|
|
|
|
R"((<\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.
|
2023-04-15 07:39:49 +00:00
|
|
|
articleString += QString::fromStdString( Utils::Html::getHtmlCleaner() );
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
|
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\">" : ">";
|
|
|
|
|
2023-06-23 15:09:31 +00:00
|
|
|
articleString.prepend( divStr.toUtf8() );
|
|
|
|
articleString.append( "</div>" );
|
2016-06-17 14:27:48 +00:00
|
|
|
|
2023-06-23 15:09:31 +00:00
|
|
|
articleString.prepend( "<div class=\"website_padding\"></div>" );
|
2016-06-17 19:21:59 +00:00
|
|
|
|
2023-06-23 15:09:31 +00:00
|
|
|
appendString( articleString.toStdString() );
|
2016-06-17 14:27:48 +00:00
|
|
|
|
|
|
|
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 &, wstring const & context, bool )
|
2023-07-20 08:02:22 +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;
|
|
|
|
|
2023-04-16 09:07:07 +00:00
|
|
|
QString inputWord = QString::fromStdU32String( 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
|
|
|
|
|
|
|
|
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-09-22 12:04:01 +00:00
|
|
|
"\" src=\""+encodeUrl.toStdString() +
|
|
|
|
"\" onmouseover=\"processIframeMouseOver('gdexpandframe-" + getId() + "');\" "
|
|
|
|
"onmouseout=\"processIframeMouseOut();\" "
|
2022-09-03 06:17:21 +00:00
|
|
|
"scrolling=\"no\" "
|
|
|
|
"style=\"overflow:visible; width:100%; display:block; border:none;\" sandbox=\"allow-same-origin allow-scripts allow-popups\">"
|
2016-06-17 14:27:48 +00:00
|
|
|
"</iframe>";
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2023-06-24 21:36:10 +00:00
|
|
|
auto dr = std::make_shared< DataRequestInstant >( true );
|
|
|
|
dr->appendString( result );
|
2016-06-17 14:27:48 +00:00
|
|
|
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
|
|
|
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< 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() {}
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void cancel() override;
|
2016-06-18 11:56:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-12-29 07:07:40 +00:00
|
|
|
void requestFinished( QNetworkReply * ) override;
|
2016-06-18 11:56:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2016-06-18 11:56:13 +00:00
|
|
|
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 ) );
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2016-06-18 11:56:13 +00:00
|
|
|
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 );
|
|
|
|
|
2023-06-23 15:09:31 +00:00
|
|
|
appendString( cssString.toStdString() );
|
2016-06-18 11:56:13 +00:00
|
|
|
|
|
|
|
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, "://" );
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< WebSiteResourceRequest >( link, netMgr, this );
|
2016-06-18 11:56:13 +00:00
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
2022-09-07 13:53:19 +00:00
|
|
|
if ( dictionaryIcon.isNull() && !loadIconFromText( ":/icons/webdict.svg", QString::fromStdString( name ) ) )
|
2023-06-19 02:34:08 +00:00
|
|
|
dictionaryIcon = QIcon( ":/icons/webdict.svg" );
|
2012-12-03 12:48:31 +00:00
|
|
|
dictionaryIconLoaded = true;
|
|
|
|
}
|
|
|
|
|
2009-05-16 18:04:21 +00:00
|
|
|
} // namespace
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2016-06-17 14:27:48 +00:00
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries( Config::WebSites const & ws, QNetworkAccessManager & mgr )
|
2009-05-16 18:04:21 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > result;
|
|
|
|
|
2023-07-29 16:50:03 +00:00
|
|
|
for ( const auto & w : ws ) {
|
|
|
|
if ( w.enabled )
|
|
|
|
result.push_back( std::make_shared< WebSiteDictionary >( w.id.toUtf8().data(),
|
|
|
|
w.name.toUtf8().data(),
|
|
|
|
w.url,
|
|
|
|
w.iconFilename,
|
|
|
|
w.inside_iframe,
|
2016-06-17 14:27:48 +00:00
|
|
|
mgr ) );
|
2009-05-16 18:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-12 02:20:34 +00:00
|
|
|
#include "website.moc"
|
2009-05-16 18:04:21 +00:00
|
|
|
} // namespace WebSite
|