From fc7ec7a877ce088d0ed9879af5791b3c3e3ee241 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 7 Jul 2017 16:15:55 +0200 Subject: Block downloads with user feedback --- src/slxbrowser.cpp | 4 ++-- src/slxbrowser.h | 4 ++-- src/webview.cpp | 35 +++++++++++++++++++++++++++++++++++ src/webview.h | 19 ++++++++++++++++--- 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("

Network Access Error

");
+	if (!_browser->wasAbortedDownload() && !ok) {
+		_browser->page()->mainFrame()->setHtml("

Page Load Error

");
 		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 
 
 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 _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 
+#include 
+#include 
+#include 
+
+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 
 #include 
+#include 
+
+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 _urls;
+	QTimer *_timer;
+	bool _abortedDownload;
 };
 
 #endif
-- 
cgit v1.2.3-55-g7522