mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-12-12 19:04:05 +00:00
b5349478cf
The next commit will add `.git-blame-ignore-revs` https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view
33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
#include "resourceschemehandler.hh"
|
|
|
|
ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & articleNetMgr, QObject * parent ):
|
|
QWebEngineUrlSchemeHandler( parent ),
|
|
mManager( articleNetMgr )
|
|
{
|
|
}
|
|
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
|
{
|
|
QUrl url = requestJob->requestUrl();
|
|
|
|
QNetworkRequest request;
|
|
request.setUrl( url );
|
|
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
|
|
QNetworkReply * reply = this->mManager.getArticleReply( request );
|
|
connect( reply, &QNetworkReply::finished, requestJob, [ = ]() {
|
|
if ( reply->error() == QNetworkReply::ContentNotFoundError ) {
|
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
|
return;
|
|
}
|
|
if ( reply->error() != QNetworkReply::NoError ) {
|
|
qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url();
|
|
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
|
|
return;
|
|
}
|
|
QMimeType mineType = db.mimeTypeForUrl( url );
|
|
QString contentType = mineType.name();
|
|
// Reply segment
|
|
requestJob->reply( contentType.toLatin1(), reply );
|
|
} );
|
|
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
|
}
|