onWebpageDownloadComplete A callback when a webpage download completes.
53 const QByteArray data{
self->getDownloadedData()};
54 const QUrl url{
self->getUrl()};
57 if(error != QNetworkReply::NoError) {
59 qDebug() <<
"FINISHED DOWNLOADING WEBPAGE WITH ERROR" << error;
63 const QString document(data);
65 QRegularExpression imageTagRegex(
"\\<img[^\\>]*src\\s*=\\s*\"([^\"]*)\"[^\\>]*\\>", QRegularExpression::CaseInsensitiveOption | QRegularExpression::InvertedGreedinessOption);
67 QStringList imageMatches;
68 QStringList urlMatches;
70 QRegularExpressionMatchIterator it = imageTagRegex.globalMatch(document);
72 QRegularExpressionMatch match = it.next();
73 imageMatches.append(match.captured(0));
74 urlMatches.append(match.captured(1));
77 QList<QUrl> imageUrls;
78 for(
const QString& url : urlMatches) {
79 imageUrls.push_back(QUrl(url));
82 const QString currentPathUrl{url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash | QUrl::RemoveFragment).toString()};
83 for(QUrl& imageUrl : imageUrls) {
86 if(imageUrl.scheme().isEmpty()) {
87 const QString imageUrlString{imageUrl.toString(QUrl::PrettyDecoded)};
88 if(imageUrlString.startsWith(
"//")) {
90 if(!url.scheme().isEmpty()) {
91 imageUrl.setScheme(url.scheme());
93 imageUrl.setScheme(
"http");
95 }
else if(imageUrlString.startsWith(
"/")) {
96 imageUrl = imageUrl.resolved(currentPathUrl);
98 imageUrl = QUrl(currentPathUrl + imageUrl.toString());
void onImageDownloadComplete(network::Downloader *self, const QNetworkReply::NetworkError error)
onImageDownloadComplete A callback when an image download completes.
Definition: completionhandlers.cpp:24
void downloadImage(const QUrl &url, const std::function< void(network::Downloader *self, QNetworkReply::NetworkError error)> &onComplete)
downloadImage Downloads an image over the network.
Definition: networkactions.cpp:13