goldendict-ng/resourceschemehandler.cpp
Xiao YiFang d73956b90d opt: change ArticleNetworkAccessManager's method createRequest to getArticleReply
createRequest was the parent's protected method ,give the impression that the derived class has override it.

rename it to clacify the misunderstand.
2022-05-19 07:14:36 +08:00

31 lines
1.2 KiB
C++

#include "resourceschemehandler.h"
ResourceSchemeHandler::ResourceSchemeHandler(ArticleNetworkAccessManager& articleNetMgr):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);
}