This repository has been archived on 2023-11-19. You can view files and clone it, but cannot push or open issues or pull requests.
goober-bb10/src/WebImageView.cpp
Morgan McMillian f8f3149d94 Initial commit
2016-09-22 16:21:20 -07:00

71 lines
1.8 KiB
C++

#include "WebImageView.h"
#include <bb/cascades/Image>
QNetworkAccessManager * WebImageView::mNetManager = new QNetworkAccessManager;
QNetworkDiskCache * WebImageView::mNetworkDiskCache = new QNetworkDiskCache();
WebImageView::WebImageView() {
mNetworkDiskCache->setCacheDirectory(QDesktopServices::storageLocation(QDesktopServices::CacheLocation));
mNetManager->setCache(mNetworkDiskCache);
}
const QUrl& WebImageView::url() const {
return mUrl;
}
void WebImageView::setUrl(const QUrl& url) {
mUrl = url;
mLoading = 0;
resetImage();
QNetworkRequest request;
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
request.setUrl(url);
QNetworkReply * reply = mNetManager->get(QNetworkRequest(request));
connect(reply,SIGNAL(finished()), this, SLOT(imageLoaded()));
connect(reply,SIGNAL(downloadProgress ( qint64 , qint64 )), this, SLOT(dowloadProgressed(qint64,qint64)));
emit urlChanged();
}
double WebImageView::loading() const {
return mLoading;
}
void WebImageView::imageLoaded() {
QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
QVariant possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
mRedirectUrl = this->redirectUrl(possibleRedirectUrl.toUrl(),mRedirectUrl);
if (!mRedirectUrl.isEmpty()) {
this->setUrl(mRedirectUrl);
} else {
setImage(Image(reply->readAll()));
}
reply->deleteLater();
}
void WebImageView::dowloadProgressed(qint64 bytes,qint64 total) {
mLoading = double(bytes)/double(total);
emit loadingChanged();
}
QUrl WebImageView::redirectUrl(const QUrl& possibleRedirectUrl, const QUrl& oldRedirectUrl) const {
QUrl redirectUrl;
if (!possibleRedirectUrl.isEmpty() && possibleRedirectUrl != oldRedirectUrl) {
redirectUrl = possibleRedirectUrl;
}
return redirectUrl;
}