|
|
@ -13,16 +13,16 @@ |
|
|
|
#include "fs_utils.h" |
|
|
|
#include "fs_utils.h" |
|
|
|
|
|
|
|
|
|
|
|
HtmlBrowser::HtmlBrowser(QWidget* parent) |
|
|
|
HtmlBrowser::HtmlBrowser(QWidget* parent) |
|
|
|
: QTextBrowser(parent) |
|
|
|
: QTextBrowser(parent) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_netManager = new QNetworkAccessManager(this); |
|
|
|
m_netManager = new QNetworkAccessManager(this); |
|
|
|
m_diskCache = new QNetworkDiskCache(this); |
|
|
|
m_diskCache = new QNetworkDiskCache(this); |
|
|
|
m_diskCache->setCacheDirectory(QDir::cleanPath(fsutils::cacheLocation() + "/rss")); |
|
|
|
m_diskCache->setCacheDirectory(QDir::cleanPath(fsutils::cacheLocation() + "/rss")); |
|
|
|
m_diskCache->setMaximumCacheSize(50 * 1024 * 1024); |
|
|
|
m_diskCache->setMaximumCacheSize(50 * 1024 * 1024); |
|
|
|
qDebug() << "HtmlBrowser cache path:" << m_diskCache->cacheDirectory() << " max size:" << m_diskCache->maximumCacheSize() / 1024 / 1024 << "MB"; |
|
|
|
qDebug() << "HtmlBrowser cache path:" << m_diskCache->cacheDirectory() << " max size:" << m_diskCache->maximumCacheSize() / 1024 / 1024 << "MB"; |
|
|
|
m_netManager->setCache(m_diskCache); |
|
|
|
m_netManager->setCache(m_diskCache); |
|
|
|
|
|
|
|
|
|
|
|
connect(m_netManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(resourceLoaded(QNetworkReply*))); |
|
|
|
connect(m_netManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(resourceLoaded(QNetworkReply*))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
HtmlBrowser::~HtmlBrowser() |
|
|
|
HtmlBrowser::~HtmlBrowser() |
|
|
@ -31,62 +31,63 @@ HtmlBrowser::~HtmlBrowser() |
|
|
|
|
|
|
|
|
|
|
|
QVariant HtmlBrowser::loadResource(int type, const QUrl &name) |
|
|
|
QVariant HtmlBrowser::loadResource(int type, const QUrl &name) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(type == QTextDocument::ImageResource) { |
|
|
|
if(type == QTextDocument::ImageResource) { |
|
|
|
QUrl url(name); |
|
|
|
QUrl url(name); |
|
|
|
if(url.scheme().isEmpty()) |
|
|
|
if(url.scheme().isEmpty()) |
|
|
|
url.setScheme("http"); |
|
|
|
url.setScheme("http"); |
|
|
|
|
|
|
|
|
|
|
|
QIODevice *dev = m_diskCache->data(url); |
|
|
|
QIODevice *dev = m_diskCache->data(url); |
|
|
|
if(dev != 0) { |
|
|
|
if(dev != 0) { |
|
|
|
qDebug() << "HtmlBrowser::loadResource() cache " << url.toString(); |
|
|
|
qDebug() << "HtmlBrowser::loadResource() cache " << url.toString(); |
|
|
|
QByteArray res = dev->readAll(); |
|
|
|
QByteArray res = dev->readAll(); |
|
|
|
delete dev; |
|
|
|
delete dev; |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!m_activeRequests.contains(url)) { |
|
|
|
if(!m_activeRequests.contains(url)) { |
|
|
|
m_activeRequests.insert(url, true); |
|
|
|
m_activeRequests.insert(url, true); |
|
|
|
qDebug() << "HtmlBrowser::loadResource() get " << url.toString(); |
|
|
|
qDebug() << "HtmlBrowser::loadResource() get " << url.toString(); |
|
|
|
QNetworkRequest req(url); |
|
|
|
QNetworkRequest req(url); |
|
|
|
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); |
|
|
|
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); |
|
|
|
m_netManager->get(req); |
|
|
|
m_netManager->get(req); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return QVariant(); |
|
|
|
return QVariant(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return QTextBrowser::loadResource(type, name); |
|
|
|
return QTextBrowser::loadResource(type, name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void HtmlBrowser::resourceLoaded(QNetworkReply *reply) |
|
|
|
void HtmlBrowser::resourceLoaded(QNetworkReply *reply) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_activeRequests.remove(reply->request().url()); |
|
|
|
m_activeRequests.remove(reply->request().url()); |
|
|
|
|
|
|
|
|
|
|
|
if(reply->error() == QNetworkReply::NoError && reply->size() > 0) { |
|
|
|
if(reply->error() == QNetworkReply::NoError && reply->size() > 0) { |
|
|
|
qDebug() << "HtmlBrowser::resourceLoaded() save " << reply->request().url().toString(); |
|
|
|
qDebug() << "HtmlBrowser::resourceLoaded() save " << reply->request().url().toString(); |
|
|
|
} else { |
|
|
|
} |
|
|
|
// If resource failed to load, replace it with warning icon and store it in cache for 1 day.
|
|
|
|
else { |
|
|
|
// Otherwise HTMLBrowser will keep trying to download it every time article is displayed,
|
|
|
|
// If resource failed to load, replace it with warning icon and store it in cache for 1 day.
|
|
|
|
// since it's not possible to cache error responses.
|
|
|
|
// Otherwise HTMLBrowser will keep trying to download it every time article is displayed,
|
|
|
|
QNetworkCacheMetaData metaData; |
|
|
|
// since it's not possible to cache error responses.
|
|
|
|
QNetworkCacheMetaData::AttributesMap atts; |
|
|
|
QNetworkCacheMetaData metaData; |
|
|
|
metaData.setUrl(reply->request().url()); |
|
|
|
QNetworkCacheMetaData::AttributesMap atts; |
|
|
|
metaData.setSaveToDisk(true); |
|
|
|
metaData.setUrl(reply->request().url()); |
|
|
|
atts[QNetworkRequest::HttpStatusCodeAttribute] = 200; |
|
|
|
metaData.setSaveToDisk(true); |
|
|
|
atts[QNetworkRequest::HttpReasonPhraseAttribute] = "Ok"; |
|
|
|
atts[QNetworkRequest::HttpStatusCodeAttribute] = 200; |
|
|
|
metaData.setAttributes(atts); |
|
|
|
atts[QNetworkRequest::HttpReasonPhraseAttribute] = "Ok"; |
|
|
|
metaData.setLastModified(QDateTime::currentDateTime()); |
|
|
|
metaData.setAttributes(atts); |
|
|
|
metaData.setExpirationDate(QDateTime::currentDateTime().addDays(1)); |
|
|
|
metaData.setLastModified(QDateTime::currentDateTime()); |
|
|
|
QIODevice *dev = m_diskCache->prepare(metaData); |
|
|
|
metaData.setExpirationDate(QDateTime::currentDateTime().addDays(1)); |
|
|
|
if(!dev) |
|
|
|
QIODevice *dev = m_diskCache->prepare(metaData); |
|
|
|
return; |
|
|
|
if(!dev) |
|
|
|
QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(32, 32).save(dev, "PNG"); |
|
|
|
return; |
|
|
|
m_diskCache->insert(dev); |
|
|
|
QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(32, 32).save(dev, "PNG"); |
|
|
|
} |
|
|
|
m_diskCache->insert(dev); |
|
|
|
// Refresh the document display and keep scrollbars where they are
|
|
|
|
} |
|
|
|
int sx = horizontalScrollBar()->value(); |
|
|
|
// Refresh the document display and keep scrollbars where they are
|
|
|
|
int sy = verticalScrollBar()->value(); |
|
|
|
int sx = horizontalScrollBar()->value(); |
|
|
|
document()->setHtml(document()->toHtml()); |
|
|
|
int sy = verticalScrollBar()->value(); |
|
|
|
horizontalScrollBar()->setValue(sx); |
|
|
|
document()->setHtml(document()->toHtml()); |
|
|
|
verticalScrollBar()->setValue(sy); |
|
|
|
horizontalScrollBar()->setValue(sx); |
|
|
|
|
|
|
|
verticalScrollBar()->setValue(sy); |
|
|
|
} |
|
|
|
} |
|
|
|