2019-02-04 08:45:22 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2013 Mladen Milinkovic <max@smoothware.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2013-11-23 00:51:50 +04:00
|
|
|
#include "htmlbrowser.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDateTime>
|
2017-09-07 03:00:04 +03:00
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QNetworkDiskCache>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2013-11-23 00:51:50 +04:00
|
|
|
#include <QScrollBar>
|
2017-09-07 03:00:04 +03:00
|
|
|
#include <QStyle>
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2016-05-11 14:25:29 +03:00
|
|
|
#include "base/profile.h"
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2017-09-07 03:00:04 +03:00
|
|
|
HtmlBrowser::HtmlBrowser(QWidget *parent)
|
2014-12-07 13:11:18 +03:00
|
|
|
: QTextBrowser(parent)
|
2013-11-23 00:51:50 +04:00
|
|
|
{
|
2014-12-07 13:11:18 +03:00
|
|
|
m_netManager = new QNetworkAccessManager(this);
|
|
|
|
m_diskCache = new QNetworkDiskCache(this);
|
2016-05-11 14:25:29 +03:00
|
|
|
m_diskCache->setCacheDirectory(QDir::cleanPath(specialFolderLocation(SpecialFolder::Cache) + "/rss"));
|
2014-12-07 13:11:18 +03:00
|
|
|
m_diskCache->setMaximumCacheSize(50 * 1024 * 1024);
|
|
|
|
qDebug() << "HtmlBrowser cache path:" << m_diskCache->cacheDirectory() << " max size:" << m_diskCache->maximumCacheSize() / 1024 / 1024 << "MB";
|
|
|
|
m_netManager->setCache(m_diskCache);
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2018-04-18 16:59:41 +03:00
|
|
|
connect(m_netManager, &QNetworkAccessManager::finished, this, &HtmlBrowser::resourceLoaded);
|
2013-11-23 00:51:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HtmlBrowser::~HtmlBrowser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant HtmlBrowser::loadResource(int type, const QUrl &name)
|
|
|
|
{
|
2017-09-07 03:00:04 +03:00
|
|
|
if (type == QTextDocument::ImageResource) {
|
2014-12-07 13:11:18 +03:00
|
|
|
QUrl url(name);
|
2017-09-07 03:00:04 +03:00
|
|
|
if (url.scheme().isEmpty())
|
2014-12-07 13:11:18 +03:00
|
|
|
url.setScheme("http");
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2014-12-07 13:11:18 +03:00
|
|
|
QIODevice *dev = m_diskCache->data(url);
|
2019-12-18 10:10:16 +03:00
|
|
|
if (dev) {
|
2014-12-07 13:11:18 +03:00
|
|
|
qDebug() << "HtmlBrowser::loadResource() cache " << url.toString();
|
|
|
|
QByteArray res = dev->readAll();
|
|
|
|
delete dev;
|
|
|
|
return res;
|
|
|
|
}
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2017-09-07 03:00:04 +03:00
|
|
|
if (!m_activeRequests.contains(url)) {
|
2014-12-07 13:11:18 +03:00
|
|
|
m_activeRequests.insert(url, true);
|
|
|
|
qDebug() << "HtmlBrowser::loadResource() get " << url.toString();
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
req.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
|
|
|
m_netManager->get(req);
|
|
|
|
}
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2019-02-14 20:16:42 +03:00
|
|
|
return {};
|
2014-12-07 13:11:18 +03:00
|
|
|
}
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2014-12-07 13:11:18 +03:00
|
|
|
return QTextBrowser::loadResource(type, name);
|
2013-11-23 00:51:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HtmlBrowser::resourceLoaded(QNetworkReply *reply)
|
|
|
|
{
|
2014-12-07 13:11:18 +03:00
|
|
|
m_activeRequests.remove(reply->request().url());
|
2013-11-23 00:51:50 +04:00
|
|
|
|
2017-09-07 03:00:04 +03:00
|
|
|
if ((reply->error() == QNetworkReply::NoError) && (reply->size() > 0)) {
|
2014-12-07 13:11:18 +03:00
|
|
|
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.
|
|
|
|
// Otherwise HTMLBrowser will keep trying to download it every time article is displayed,
|
|
|
|
// since it's not possible to cache error responses.
|
|
|
|
QNetworkCacheMetaData metaData;
|
|
|
|
QNetworkCacheMetaData::AttributesMap atts;
|
|
|
|
metaData.setUrl(reply->request().url());
|
|
|
|
metaData.setSaveToDisk(true);
|
|
|
|
atts[QNetworkRequest::HttpStatusCodeAttribute] = 200;
|
|
|
|
atts[QNetworkRequest::HttpReasonPhraseAttribute] = "Ok";
|
|
|
|
metaData.setAttributes(atts);
|
|
|
|
metaData.setLastModified(QDateTime::currentDateTime());
|
|
|
|
metaData.setExpirationDate(QDateTime::currentDateTime().addDays(1));
|
|
|
|
QIODevice *dev = m_diskCache->prepare(metaData);
|
2017-09-07 03:00:04 +03:00
|
|
|
if (!dev) return;
|
|
|
|
|
2014-12-07 13:11:18 +03:00
|
|
|
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();
|
|
|
|
int sy = verticalScrollBar()->value();
|
|
|
|
document()->setHtml(document()->toHtml());
|
|
|
|
horizontalScrollBar()->setValue(sx);
|
|
|
|
verticalScrollBar()->setValue(sy);
|
2013-11-23 00:51:50 +04:00
|
|
|
}
|