summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2017-07-07 16:15:55 +0200
committerSimon Rettberg2017-07-07 16:15:55 +0200
commitfc7ec7a877ce088d0ed9879af5791b3c3e3ee241 (patch)
tree56bbbb1445bf0c83f59a2624eab5599e49bf9c95
parentcmake 2.8.12 compat (diff)
downloadslxbrowser-fc7ec7a877ce088d0ed9879af5791b3c3e3ee241.tar.gz
slxbrowser-fc7ec7a877ce088d0ed9879af5791b3c3e3ee241.tar.xz
slxbrowser-fc7ec7a877ce088d0ed9879af5791b3c3e3ee241.zip
Block downloads with user feedback
-rw-r--r--src/slxbrowser.cpp4
-rw-r--r--src/slxbrowser.h4
-rw-r--r--src/webview.cpp35
-rw-r--r--src/webview.h19
4 files changed, 55 insertions, 7 deletions
diff --git a/src/slxbrowser.cpp b/src/slxbrowser.cpp
index aec5201..45c235a 100644
--- a/src/slxbrowser.cpp
+++ b/src/slxbrowser.cpp
@@ -64,8 +64,8 @@ void SLXbrowser::loadStarted()
void SLXbrowser::loadFinished(bool ok)
{
_progress->hide();
- if (!ok) {
- _browser->page()->mainFrame()->setHtml("<html><body style='background:blue;color:white'><br><br><center><h1>Network Access Error</h1><pre id='content'></pre></body></html>");
+ if (!_browser->wasAbortedDownload() && !ok) {
+ _browser->page()->mainFrame()->setHtml("<html><body style='background:blue;color:white'><br><br><center><h1>Page Load Error</h1><pre id='content'></pre></body></html>");
QWebElement el = _browser->page()->mainFrame()->documentElement().findFirst("#content");
QString str;
if (!_sslErrors.empty()) {
diff --git a/src/slxbrowser.h b/src/slxbrowser.h
index fbd4c4a..dc357dd 100644
--- a/src/slxbrowser.h
+++ b/src/slxbrowser.h
@@ -7,7 +7,7 @@
#include <QTimer>
class QNetworkReply;
-class QWebView;
+class WebView;
class QProgressBar;
class QNetworkReply;
@@ -29,7 +29,7 @@ private slots:
private:
QString _url;
bool _ignoreSslErrors;
- QWebView *_browser;
+ WebView *_browser;
QProgressBar *_progress;
QTimer _reset;
QList<QSslError> _sslErrors;
diff --git a/src/webview.cpp b/src/webview.cpp
index aa9c650..29afb19 100644
--- a/src/webview.cpp
+++ b/src/webview.cpp
@@ -1,5 +1,20 @@
#include "webview.h"
#include <QWebFrame>
+#include <QNetworkReply>
+#include <QMessageBox>
+#include <QTimer>
+
+WebView::WebView(QWidget* parent)
+ : QWebView(parent),
+ _timer(new QTimer(this)),
+ _abortedDownload(false) {
+ _timer->setSingleShot(true);
+ connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));
+ page()->setForwardUnsupportedContent(true);
+ connect(page(), SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*)));
+ connect(page(), SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(downloadRequest(QNetworkRequest)));
+ connect(_timer, SIGNAL(timeout()), this, SLOT(downloadDeniedMessage()));
+}
void WebView::windowCloseRequested()
{
@@ -16,3 +31,23 @@ QWebView* WebView::createWindow(QWebPage::WebWindowType)
_urls.push(this->url());
return this;
}
+
+void WebView::unsupportedContent(QNetworkReply* rep)
+{
+ _abortedDownload = true;
+ rep->abort();
+ rep->deleteLater();
+ _timer->start(1);
+}
+
+void WebView::downloadRequest(QNetworkRequest)
+{
+ _timer->start(1);
+}
+
+void WebView::downloadDeniedMessage()
+{
+ QMessageBox::warning(this->parentWidget(), QString::fromUtf8("Denied"),
+ QString::fromUtf8("The requested action triggered a download, which is not allowed.\n\n"
+ "Diese Aktion löst einen Download aus, was nicht erlaubt ist."));
+}
diff --git a/src/webview.h b/src/webview.h
index 753c984..9d7e28d 100644
--- a/src/webview.h
+++ b/src/webview.h
@@ -3,6 +3,10 @@
#include <QStack>
#include <QWebView>
+#include <QNetworkRequest>
+
+class QNetworkReply;
+class QTimer;
/**
* Make sure pages that want to load in a new tab are actually loaded in the same page,
@@ -10,19 +14,28 @@
*/
class WebView : public QWebView
{
- Q_OBJECT
+Q_OBJECT
public:
- WebView(QWidget* parent = NULL) : QWebView(parent) {
- connect(page(), SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));
+ WebView(QWidget* parent = NULL);
+ bool wasAbortedDownload() {
+ bool r = _abortedDownload;
+ _abortedDownload = false;
+ return r;
}
+
protected:
QWebView *createWindow(QWebPage::WebWindowType);
protected slots:
void windowCloseRequested();
+ void unsupportedContent(QNetworkReply*);
+ void downloadRequest(QNetworkRequest);
+ void downloadDeniedMessage();
private:
QStack<QUrl> _urls;
+ QTimer *_timer;
+ bool _abortedDownload;
};
#endif