diff --git a/src/common/utils.cc b/src/common/utils.cc index 9e3e004d..ca119af0 100644 --- a/src/common/utils.cc +++ b/src/common/utils.cc @@ -8,6 +8,7 @@ #include #endif #include +#include using std::string; namespace Utils { @@ -119,3 +120,55 @@ void removeDirectory( string const & directory ) } } // namespace Utils::Fs + +namespace Utils::WebSite { +QString urlReplaceWord( const QString url, QString inputWord ) +{ + //copy temp url + auto urlString = url; + + urlString.replace( "%25GDWORD%25", inputWord.toUtf8().toPercentEncoding() ); + + 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() ); + } + + + // Handle all ISO-8859 encodings + for ( int x = 1; x <= 16; ++x ) { + codec = QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toLatin1() ); + if ( codec ) { + urlString.replace( QString( "%25GDISO%1%25" ).arg( x ).toUtf8(), + codec->fromUnicode( inputWord ).toPercentEncoding() ); + } + + // Skip encodings 11..12, they don't exist + if ( x == 10 ) { + x = 12; + } + } + + return urlString; +} +} // namespace Utils::WebSite diff --git a/src/common/utils.hh b/src/common/utils.hh index c794e60f..76807341 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -341,6 +341,10 @@ void removeDirectory( QString const & directory ); void removeDirectory( string const & directory ); } // namespace Fs +namespace WebSite { +QString urlReplaceWord( const QString url, QString word ); +} + QString escapeAmps( QString const & str ); QString unescapeAmps( QString const & str ); diff --git a/src/dict/website.cc b/src/dict/website.cc index 74f8fa4b..ca03d6b4 100644 --- a/src/dict/website.cc +++ b/src/dict/website.cc @@ -10,6 +10,7 @@ #include #include "gddebug.hh" #include "globalbroadcaster.hh" +#include "fmt/compile.h" #include @@ -312,73 +313,23 @@ void WebSiteArticleRequest::requestFinished( QNetworkReply * r ) } } - disconnect( netReply, 0, 0, 0 ); + disconnect( netReply, nullptr, 0, 0 ); netReply->deleteLater(); finish(); } -sptr< DataRequest > -WebSiteDictionary::getArticle( wstring const & str, vector< wstring > const &, wstring const & context, bool ) - +sptr< DataRequest > WebSiteDictionary::getArticle( wstring const & str, + vector< wstring > const & /*alts*/, + wstring const & context, + bool /*ignoreDiacritics*/ ) { - QByteArray urlString; - - // Context contains the right url to go to - if ( context.size() ) { - urlString = Utf8::encode( context ).c_str(); - } - else { - urlString = urlTemplate; - - QString inputWord = QString::fromStdU32String( str ); - - urlString.replace( "%25GDWORD%25", inputWord.toUtf8().toPercentEncoding() ); - - 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() ); - } - - - // Handle all ISO-8859 encodings - for ( int x = 1; x <= 16; ++x ) { - codec = QTextCodec::codecForName( QString( "ISO 8859-%1" ).arg( x ).toLatin1() ); - if ( codec ) { - urlString.replace( QString( "%25GDISO%1%25" ).arg( x ).toUtf8(), - codec->fromUnicode( inputWord ).toPercentEncoding() ); - } - - if ( x == 10 ) { - x = 12; // Skip encodings 11..12, they don't exist - } - } - } + QString urlString = Utils::WebSite::urlReplaceWord( QString( urlTemplate ), QString::fromStdU32String( str ) ); if ( inside_iframe ) { // Just insert link in "; + fmt::format_to( std::back_inserter( result ), + R"()", + getId(), + encodeUrl.toStdString(), + getId() ); auto dr = std::make_shared< DataRequestInstant >( true ); dr->appendString( result );