refactor getqueryword

This commit is contained in:
Xiao YiFang 2022-06-07 08:05:49 +08:00
parent ccd8ace0ec
commit 5f856bf2ae
3 changed files with 22 additions and 7 deletions

View file

@ -513,11 +513,10 @@ void LocalSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
request.setUrl( url ); request.setUrl( url );
//all the url reached here must be either gdlookup or bword scheme. //all the url reached here must be either gdlookup or bword scheme.
auto queryWord = Utils::Url::getQueryWord( url ); auto [valid, word] = Utils::Url::getQueryWord( url );
auto word = queryWord.second;
// or the condition can be (!queryWord.first || word.isEmpty()) // or the condition can be (!queryWord.first || word.isEmpty())
// ( queryWord.first && word.isEmpty() ) is only part of the above condition. // ( queryWord.first && word.isEmpty() ) is only part of the above condition.
if( queryWord.first && word.isEmpty() ) if( valid && word.isEmpty() )
{ {
// invalid gdlookup url. // invalid gdlookup url.
return; return;

View file

@ -1113,9 +1113,8 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref,
audioPlayer->stop(); audioPlayer->stop();
qDebug() << "open link url:" << url; qDebug() << "open link url:" << url;
auto queryWord = Utils::Url::getQueryWord( url ); auto [valid, word] = Utils::Url::getQueryWord( url );
auto word = queryWord.second; if( valid && word.isEmpty() )
if( queryWord.first && word.isEmpty() )
{ {
// invalid gdlookup url. // invalid gdlookup url.
return; return;

View file

@ -224,8 +224,25 @@ inline std::pair< bool, QString > getQueryWord( QUrl const & url )
if( url.scheme().compare( "bword" ) == 0 ) if( url.scheme().compare( "bword" ) == 0 )
{ {
validScheme = true; validScheme = true;
auto path = url.path();
// url like this , bword:word or bword://localhost/word
if( !path.isEmpty() )
{
//url,bword://localhost/word
if( path.startsWith( "/" ) )
word = url.path().mid( 1 ); word = url.path().mid( 1 );
} }
else
{
// url looks like this, bword://word,or bword://localhost
auto host = url.host();
if( host != "localhost" )
{
word = host;
}
}
}
return std::make_pair( validScheme, word ); return std::make_pair( validScheme, word );
} }
} }