diff --git a/src/common/utils.hh b/src/common/utils.hh index 7de3d8e9..baa3b854 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -252,12 +252,24 @@ inline std::pair< bool, QString > getQueryWord( QUrl const & url ) inline bool isAudioUrl( QUrl const & url ) { + if ( !url.isValid() ) + return false; // Note: we check for forvo sound links explicitly, as they don't have extensions return ( url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gdau" ) && ( Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" ); } +inline bool isWebAudioUrl( QUrl const & url ) +{ + if ( !url.isValid() ) + return false; + // Note: we check for forvo sound links explicitly, as they don't have extensions + + return ( url.scheme() == "http" || url.scheme() == "https" ) + && ( Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" ); +} + /// Uses some heuristics to chop off the first domain name from the host name, /// but only if it's not too base. Returns the resulting host name. inline QString getHostBase( QString const & host ) diff --git a/src/dict/mediawiki.cc b/src/dict/mediawiki.cc index e38a102b..69c7b5d2 100644 --- a/src/dict/mediawiki.cc +++ b/src/dict/mediawiki.cc @@ -571,7 +571,7 @@ void MediaWikiArticleRequest::requestFinished( QNetworkReply * r ) articleNewString.clear(); } - // audio url + // audio url like this +#include "common/utils.hh" +#include PronounceEngine::PronounceEngine( QObject * parent ): QObject{ parent } @@ -21,6 +23,9 @@ void PronounceEngine::sendAudio( std::string dictId, QString audioLink ) if ( state == PronounceState::OCCUPIED ) return; + if ( !Utils::Url::isAudioUrl( QUrl( audioLink ) ) ) + return; + QMutexLocker _( &mutex ); dictAudioMap.operator[]( dictId ).push_back( audioLink ); diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index 2172444f..f516b63d 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -586,8 +586,9 @@ void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts ) { // Don't try mangling audio urls, even if they are from the framed websites - if ( ( url.scheme() == "http" || url.scheme() == "https" ) - && !Dictionary::WebMultimediaDownload::isAudioUrl( url ) ) { + if ( !url.isValid() ) + return; + if ( ( url.scheme() == "http" || url.scheme() == "https" ) && !Utils::Url::isWebAudioUrl( url ) ) { // Maybe a link inside a website was clicked? QString ca = getCurrentArticle(); @@ -849,7 +850,7 @@ void ArticleView::linkHovered( const QString & link ) if ( url.scheme() == "bres" ) { msg = tr( "Resource" ); } - else if ( url.scheme() == "gdau" || Dictionary::WebMultimediaDownload::isAudioUrl( url ) ) { + else if ( url.scheme() == "gdau" || Utils::Url::isAudioUrl( url ) ) { msg = tr( "Audio" ); } else if ( url.scheme() == "gdtts" ) { @@ -932,7 +933,7 @@ void ArticleView::linkClicked( QUrl const & url_ ) void ArticleView::linkClickedInHtml( QUrl const & url_ ) { - emit webview->linkClickedInHtml( url_ ); + webview->linkClickedInHtml( url_ ); if ( !url_.isEmpty() ) { linkClicked( url_ ); } @@ -1021,7 +1022,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & } } else if ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" - || Dictionary::WebMultimediaDownload::isAudioUrl( url ) ) { + || Utils::Url::isAudioUrl( url ) ) { // Download it // Clear any pending ones @@ -1030,7 +1031,7 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & resourceDownloadUrl = url; - if ( Dictionary::WebMultimediaDownload::isAudioUrl( url ) ) { + if ( Utils::Url::isWebAudioUrl( url ) ) { sptr< Dictionary::DataRequest > req = std::make_shared< Dictionary::WebMultimediaDownload >( url, articleNetMgr ); resourceDownloadRequests.push_back( req ); @@ -1775,8 +1776,7 @@ void ArticleView::resourceDownloadFinished() vector< char > const & data = ( *i )->getFullData(); - if ( resourceDownloadUrl.scheme() == "gdau" - || Dictionary::WebMultimediaDownload::isAudioUrl( resourceDownloadUrl ) ) { + if ( resourceDownloadUrl.scheme() == "gdau" || Utils::Url::isWebAudioUrl( resourceDownloadUrl ) ) { // Audio data connect( audioPlayer.data(), &AudioPlayerInterface::error, diff --git a/src/ui/articleview.hh b/src/ui/articleview.hh index 708a6715..abe07c60 100644 --- a/src/ui/articleview.hh +++ b/src/ui/articleview.hh @@ -340,7 +340,7 @@ private slots: bool isAudioLink( QUrl & targetUrl ) { - return ( targetUrl.scheme() == "gdau" || Dictionary::WebMultimediaDownload::isAudioUrl( targetUrl ) ); + return ( targetUrl.scheme() == "gdau" || Utils::Url::isAudioUrl( targetUrl ) ); } void resourceDownloadFinished(); diff --git a/src/webmultimediadownload.cc b/src/webmultimediadownload.cc index 9fd2e8b0..69964883 100644 --- a/src/webmultimediadownload.cc +++ b/src/webmultimediadownload.cc @@ -72,12 +72,4 @@ void WebMultimediaDownload::replyFinished( QNetworkReply * r ) finish(); } -bool WebMultimediaDownload::isAudioUrl( QUrl const & url ) -{ - // Note: we check for forvo sound links explicitly, as they don't have extensions - - return ( url.scheme() == "http" || url.scheme() == "https" ) - && ( Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" ); -} - } // namespace Dictionary diff --git a/src/webmultimediadownload.hh b/src/webmultimediadownload.hh index aa1486cb..db9ccd7e 100644 --- a/src/webmultimediadownload.hh +++ b/src/webmultimediadownload.hh @@ -20,9 +20,6 @@ public: WebMultimediaDownload( QUrl const &, QNetworkAccessManager & ); - /// Checks if the given url is an http request for an audio file. - static bool isAudioUrl( QUrl const & ); - virtual void cancel(); private slots: