goldendict-ng/src/resourceschemehandler.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.8 KiB
C++
Raw Normal View History

#include "resourceschemehandler.hh"
2021-08-28 02:35:23 +00:00
ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & articleNetMgr, QObject * parent ):
QWebEngineUrlSchemeHandler( parent ),
mManager( articleNetMgr )
{
2021-08-28 02:35:23 +00:00
}
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
2021-08-28 02:35:23 +00:00
{
const QUrl url = requestJob->requestUrl();
QString content_type;
const QMimeType mineType = db.mimeTypeForUrl( url );
const sptr< Dictionary::DataRequest > reply = this->mManager.getResource( url, content_type );
content_type = mineType.name();
if ( reply == nullptr ) {
qDebug() << "Resource failed to load: " << url.toString();
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
}
else if ( reply->isFinished() ) {
replyJob( reply, requestJob, content_type );
}
else {
connect( reply.get(), &Dictionary::DataRequest::finished, requestJob, [ = ]() {
replyJob( reply, requestJob, content_type );
} );
}
}
void ResourceSchemeHandler::replyJob( sptr< Dictionary::DataRequest > reply,
QWebEngineUrlRequestJob * requestJob,
QString content_type )
{
if ( !reply.get() ) {
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
return;
}
const auto & data = reply->getFullData();
if ( data.empty() ) {
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
return;
}
QByteArray * ba = new QByteArray( data.data(), data.size() );
QBuffer * buffer = new QBuffer( ba );
buffer->open( QBuffer::ReadOnly );
buffer->seek( 0 );
// Reply segment
requestJob->reply( content_type.toLatin1(), buffer );
2021-08-28 02:35:23 +00:00
connect( requestJob, &QObject::destroyed, buffer, [ = ]() {
buffer->close();
ba->clear();
delete ba;
buffer->deleteLater();
2022-01-08 04:22:41 +00:00
} );
2021-08-28 02:35:23 +00:00
}