opt: remove audio search logic in the current group (#1851)
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run

* opt: remove audio search logic in the current group
This commit is contained in:
xiaoyifang 2024-10-22 14:15:17 +08:00 committed by GitHub
parent 0146c72d99
commit 60adc6ef75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 189 deletions

View file

@ -258,29 +258,25 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c
contentType = mineType.name();
string id = url.host().toStdString();
bool search = ( id == "search" );
if ( !search ) {
for ( const auto & dictionary : dictionaries ) {
if ( dictionary->getId() == id ) {
if ( url.scheme() == "gico" ) {
QByteArray bytes;
QBuffer buffer( &bytes );
buffer.open( QIODevice::WriteOnly );
dictionary->getIcon().pixmap( 64 ).save( &buffer, "PNG" );
buffer.close();
sptr< Dictionary::DataRequestInstant > ico = std::make_shared< Dictionary::DataRequestInstant >( true );
ico->getData().resize( bytes.size() );
memcpy( &( ico->getData().front() ), bytes.data(), bytes.size() );
return ico;
}
try {
return dictionary->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() );
return {};
}
for ( const auto & dictionary : dictionaries ) {
if ( dictionary->getId() == id ) {
if ( url.scheme() == "gico" ) {
QByteArray bytes;
QBuffer buffer( &bytes );
buffer.open( QIODevice::WriteOnly );
dictionary->getIcon().pixmap( 64 ).save( &buffer, "PNG" );
buffer.close();
sptr< Dictionary::DataRequestInstant > ico = std::make_shared< Dictionary::DataRequestInstant >( true );
ico->getData().resize( bytes.size() );
memcpy( &( ico->getData().front() ), bytes.data(), bytes.size() );
return ico;
}
try {
return dictionary->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() );
return {};
}
}
}

View file

@ -851,18 +851,11 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
string n = resourceDir1 + filename;
if ( Filetype::isNameOfSound( filename ) ) {
// If we have the file here, do the exact reference to this dictionary.
// Otherwise, make a global 'search' one.
bool search = !File::exists( n ) && !File::exists( resourceDir2 + filename )
&& !File::exists( getContainingFolder().toStdString() + Utils::Fs::separator() + filename )
&& ( !resourceZip.isOpen() || !resourceZip.hasFile( Utf8::decode( filename ) ) );
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( search ? "search" : getId().c_str() ) );
url.setHost( QString::fromUtf8( getId().c_str() ) );
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
if ( search && idxHeader.hasSoundDictionaryName ) {
if ( idxHeader.hasSoundDictionaryName ) {
Utils::Url::setFragment( url, QString::fromUtf8( preferredSoundDictionary.c_str() ) );
}

View file

@ -654,24 +654,9 @@ string convert( string const & in,
QDomElement el_script = dd.createElement( "script" );
QDomNode parent = el.parentNode();
if ( !parent.isNull() ) {
bool search = false;
if ( type == STARDICT ) {
string n = dictPtr->getContainingFolder().toStdString() + Utils::Fs::separator() + string( "res" )
+ Utils::Fs::separator() + filename;
search = !File::exists( n )
&& ( !resourceZip || !resourceZip->isOpen() || !resourceZip->hasFile( Utf8::decode( filename ) ) );
}
else {
string n = dictPtr->getDictionaryFilenames()[ 0 ] + ".files" + Utils::Fs::separator() + filename;
search = !File::exists( n )
&& !File::exists( dictPtr->getContainingFolder().toStdString() + Utils::Fs::separator() + filename )
&& ( !resourceZip || !resourceZip->isOpen() || !resourceZip->hasFile( Utf8::decode( filename ) ) );
}
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( search ? "search" : dictPtr->getId().c_str() ) );
url.setHost( QString::fromUtf8( dictPtr->getId().c_str() ) );
url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
el_script.setAttribute( "type", "text/javascript" );

View file

@ -1050,79 +1050,6 @@ void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const &
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else if ( url.scheme() == "gdau" && url.host() == "search" ) {
// Since searches should be limited to current group, we just do them
// here ourselves since otherwise we'd need to pass group id to netmgr
// and it should've been having knowledge of the current groups, too.
unsigned currentGroup = getGroup( ref );
std::vector< sptr< Dictionary::Class > > const * activeDicts =
dictionaryGroup->getActiveDictionaries( currentGroup );
if ( activeDicts ) {
unsigned preferred = UINT_MAX;
if ( url.hasFragment() ) {
// Find sound in the preferred dictionary
QString preferredName = Utils::Url::fragment( url );
try {
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
if ( preferredName.compare( QString::fromUtf8( ( *activeDicts )[ x ]->getName().c_str() ) ) == 0 ) {
preferred = x;
sptr< Dictionary::DataRequest > req =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
resourceDownloadRequests.push_back( req );
if ( !req->isFinished() ) {
// Queued loading
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else {
// Immediate loading
if ( req->dataSize() > 0 ) {
// Resource already found, stop next search
resourceDownloadFinished();
return;
}
}
break;
}
}
}
catch ( std::exception & e ) {
emit statusBarMessage( tr( "ERROR: %1" ).arg( e.what() ), 10000, QPixmap( ":/icons/error.svg" ) );
}
}
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( x == preferred ) {
continue;
}
sptr< Dictionary::DataRequest > req =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
resourceDownloadRequests.push_back( req );
if ( !req->isFinished() ) {
// Queued loading
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else {
// Immediate loading
if ( req->dataSize() > 0 ) {
// Resource already found, stop next search
break;
}
}
}
catch ( std::exception & e ) {
emit statusBarMessage( tr( "ERROR: %1" ).arg( e.what() ), 10000, QPixmap( ":/icons/error.svg" ) );
}
}
}
}
else {
// Normal resource download
QString contentType;
@ -1224,75 +1151,12 @@ ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QUrl
sptr< Dictionary::DataRequest > req;
if ( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau" || url.scheme() == "gdvideo" ) {
if ( url.host() == "search" ) {
// Since searches should be limited to current group, we just do them
// here ourselves since otherwise we'd need to pass group id to netmgr
// and it should've been having knowledge of the current groups, too.
// Normal resource download
QString contentType;
req = articleNetMgr.getResource( url, contentType );
unsigned currentGroup = getGroup( ref );
std::vector< sptr< Dictionary::Class > > const * activeDicts =
dictionaryGroup->getActiveDictionaries( currentGroup );
if ( activeDicts ) {
unsigned preferred = UINT_MAX;
if ( url.hasFragment() && url.scheme() == "gdau" ) {
// Find sound in the preferred dictionary
QString preferredName = Utils::Url::fragment( url );
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( preferredName.compare( QString::fromUtf8( ( *activeDicts )[ x ]->getName().c_str() ) ) == 0 ) {
preferred = x;
sptr< Dictionary::DataRequest > data_request =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
handler->addRequest( data_request );
if ( data_request->isFinished() && data_request->dataSize() > 0 ) {
handler->downloadFinished();
return handler;
}
break;
}
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n",
e.what(),
( *activeDicts )[ x ]->getName().c_str() );
}
}
}
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( x == preferred ) {
continue;
}
req = ( *activeDicts )[ x ]->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
handler->addRequest( req );
if ( req->isFinished() && req->dataSize() > 0 ) {
// Resource already found, stop next search
break;
}
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n",
e.what(),
( *activeDicts )[ x ]->getName().c_str() );
}
}
}
}
else {
// Normal resource download
QString contentType;
req = articleNetMgr.getResource( url, contentType );
if ( req.get() ) {
handler->addRequest( req );
}
if ( req.get() ) {
handler->addRequest( req );
}
}
else {