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(); contentType = mineType.name();
string id = url.host().toStdString(); string id = url.host().toStdString();
bool search = ( id == "search" ); for ( const auto & dictionary : dictionaries ) {
if ( dictionary->getId() == id ) {
if ( !search ) { if ( url.scheme() == "gico" ) {
for ( const auto & dictionary : dictionaries ) { QByteArray bytes;
if ( dictionary->getId() == id ) { QBuffer buffer( &bytes );
if ( url.scheme() == "gico" ) { buffer.open( QIODevice::WriteOnly );
QByteArray bytes; dictionary->getIcon().pixmap( 64 ).save( &buffer, "PNG" );
QBuffer buffer( &bytes ); buffer.close();
buffer.open( QIODevice::WriteOnly ); sptr< Dictionary::DataRequestInstant > ico = std::make_shared< Dictionary::DataRequestInstant >( true );
dictionary->getIcon().pixmap( 64 ).save( &buffer, "PNG" ); ico->getData().resize( bytes.size() );
buffer.close(); memcpy( &( ico->getData().front() ), bytes.data(), bytes.size() );
sptr< Dictionary::DataRequestInstant > ico = std::make_shared< Dictionary::DataRequestInstant >( true ); return ico;
ico->getData().resize( bytes.size() ); }
memcpy( &( ico->getData().front() ), bytes.data(), bytes.size() ); try {
return ico; return dictionary->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
} }
try { catch ( std::exception & e ) {
return dictionary->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() ); gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() );
} return {};
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; string n = resourceDir1 + filename;
if ( Filetype::isNameOfSound( 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; QUrl url;
url.setScheme( "gdau" ); 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() ) ) ); 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() ) ); 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" ); QDomElement el_script = dd.createElement( "script" );
QDomNode parent = el.parentNode(); QDomNode parent = el.parentNode();
if ( !parent.isNull() ) { 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; QUrl url;
url.setScheme( "gdau" ); 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() ) ) ); url.setPath( Utils::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
el_script.setAttribute( "type", "text/javascript" ); 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 ); 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 { else {
// Normal resource download // Normal resource download
QString contentType; QString contentType;
@ -1224,75 +1151,12 @@ ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QUrl
sptr< Dictionary::DataRequest > req; sptr< Dictionary::DataRequest > req;
if ( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau" || url.scheme() == "gdvideo" ) { if ( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau" || url.scheme() == "gdvideo" ) {
if ( url.host() == "search" ) { // Normal resource download
// Since searches should be limited to current group, we just do them QString contentType;
// here ourselves since otherwise we'd need to pass group id to netmgr req = articleNetMgr.getResource( url, contentType );
// and it should've been having knowledge of the current groups, too.
unsigned currentGroup = getGroup( ref ); if ( req.get() ) {
handler->addRequest( req );
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 );
}
} }
} }
else { else {