Websites: Fix some encoding recognition errors when compiled with Qt 4.x

This commit is contained in:
Abs62 2016-07-12 17:51:44 +03:00
parent b2366124ef
commit 39455250e6

View file

@ -104,6 +104,7 @@ public:
private:
virtual void requestFinished( QNetworkReply * );
static QTextCodec * codecForHtml( QByteArray const & ba );
};
void WebSiteArticleRequest::cancel()
@ -130,6 +131,50 @@ WebSiteArticleRequest::WebSiteArticleRequest( QString const & url_,
#endif
}
QTextCodec * WebSiteArticleRequest::codecForHtml( QByteArray const & ba )
{
#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
return QTextCodec::codecForHtml( ba, 0 );
#else
// Implementation taken from Qt 5 sources
// Function from Qt 4 can't recognize charset name inside single quotes
QByteArray header = ba.left( 1024 ).toLower();
int pos = header.indexOf( "meta " );
if (pos != -1) {
pos = header.indexOf( "charset=", pos );
if (pos != -1) {
pos += qstrlen( "charset=" );
int pos2 = pos;
while ( ++pos2 < header.size() )
{
char ch = header.at( pos2 );
if( ch != '\"' && ch != '\'' && ch != ' ' )
break;
}
// The attribute can be closed with either """, "'", ">" or "/",
// none of which are valid charset characters.
while ( pos2++ < header.size() )
{
char ch = header.at( pos2 );
if( ch == '\"' || ch == '\'' || ch == '>' || ch == '/' )
{
QByteArray name = header.mid( pos, pos2 - pos );
if ( name == "unicode" )
name = QByteArray( "UTF-8" );
return QTextCodec::codecForName(name);
}
}
}
}
return 0;
#endif
}
void WebSiteArticleRequest::requestFinished( QNetworkReply * r )
{
if ( isFinished() ) // Was cancelled
@ -163,7 +208,7 @@ void WebSiteArticleRequest::requestFinished( QNetworkReply * r )
QByteArray replyData = netReply->readAll();
QString articleString;
QTextCodec * codec = QTextCodec::codecForHtml( replyData, 0 );
QTextCodec * codec = WebSiteArticleRequest::codecForHtml( replyData );
if( codec )
articleString = codec->toUnicode( replyData );
else