From 8dedb9084e5023238fb476489cfd615761b20fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 11 Aug 2018 13:12:13 +0200 Subject: [PATCH 1/2] Make links open in an external browser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- src/gui/wizard/webview.cpp | 66 ++++++++++++++++++++++++++++++++++++-- src/gui/wizard/webview.h | 6 ++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/gui/wizard/webview.cpp b/src/gui/wizard/webview.cpp index 36451b3af..e50ef4a13 100644 --- a/src/gui/wizard/webview.cpp +++ b/src/gui/wizard/webview.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,27 @@ Q_SIGNALS: void urlCatched(QString user, QString pass, QString host); }; +class WebEngineView : public QWebEngineView { + Q_OBJECT +public: + WebEngineView(QWidget* parent = 0); + bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame); +}; + + +class WebEnginePage : public QWebEnginePage { +public: + WebEnginePage(QWebEngineProfile *profile, QObject* parent = nullptr); + QWebEnginePage * createWindow(QWebEnginePage::WebWindowType type) override; +}; + +// We need a separate class here, since we cannot simply return the same WebEnginePage object +// this leads to a strage segfault somewhere deep inside of the QWebEngine code +class ExternalWebEnginePage : public QWebEnginePage { +public: + ExternalWebEnginePage(QWebEngineProfile *profile, QObject* parent = nullptr); + bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; +}; WebView::WebView(QWidget *parent) : QWidget(parent), @@ -43,13 +65,15 @@ WebView::WebView(QWidget *parent) { _ui.setupUi(this); - _webview = new QWebEngineView(this); + _webview = new WebEngineView(this); _profile = new QWebEngineProfile(this); - _page = new QWebEnginePage(_profile); + _page = new WebEnginePage(_profile); _interceptor = new WebViewPageUrlRequestInterceptor(this); _schemeHandler = new WebViewPageUrlSchemeHandler(this); - _profile->setHttpUserAgent(Utility::userAgentString()); + const QString userAgent(Utility::userAgentString()); + _profile->setHttpUserAgent(userAgent); + QWebEngineProfile::defaultProfile()->setHttpUserAgent(userAgent); _profile->setRequestInterceptor(_interceptor); _profile->installUrlSchemeHandler("nc", _schemeHandler); @@ -120,6 +144,42 @@ void WebViewPageUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *reques emit urlCatched(user, password, server); } + +WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject* parent) : QWebEnginePage(profile, parent) { + +} + +QWebEnginePage * WebEnginePage::createWindow(QWebEnginePage::WebWindowType type) { + ExternalWebEnginePage *view = new ExternalWebEnginePage(this->profile()); + return view; +} + +ExternalWebEnginePage::ExternalWebEnginePage(QWebEngineProfile *profile, QObject* parent) : QWebEnginePage(profile, parent) { + +} + + +bool ExternalWebEnginePage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) +{ + QDesktopServices::openUrl(url); + return false; +} + + +WebEngineView::WebEngineView(QWidget* parent) : QWebEngineView (parent) { + +} + +bool WebEngineView::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) +{ + if (type == QWebEnginePage::NavigationTypeLinkClicked) + { + QDesktopServices::openUrl(url); + return false; + } + return true; +} + } #include "webview.moc" diff --git a/src/gui/wizard/webview.h b/src/gui/wizard/webview.h index 5e5a8793a..01529ef09 100644 --- a/src/gui/wizard/webview.h +++ b/src/gui/wizard/webview.h @@ -14,6 +14,8 @@ namespace OCC { class WebViewPageUrlRequestInterceptor; class WebViewPageUrlSchemeHandler; +class WebEngineView; +class WebEnginePage; class WebView : public QWidget { @@ -28,9 +30,9 @@ signals: private: Ui_WebView _ui; - QWebEngineView *_webview; + WebEngineView *_webview; QWebEngineProfile *_profile; - QWebEnginePage *_page; + WebEnginePage *_page; WebViewPageUrlRequestInterceptor *_interceptor; WebViewPageUrlSchemeHandler *_schemeHandler; From 3576fbb2a593138334ef3ecabf846f684795ea9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 11 Aug 2018 17:47:40 +0200 Subject: [PATCH 2/2] Remove unused subclass of QWebEngineView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- src/gui/wizard/webview.cpp | 25 +------------------------ src/gui/wizard/webview.h | 3 +-- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/src/gui/wizard/webview.cpp b/src/gui/wizard/webview.cpp index e50ef4a13..15c1f314e 100644 --- a/src/gui/wizard/webview.cpp +++ b/src/gui/wizard/webview.cpp @@ -37,14 +37,6 @@ Q_SIGNALS: void urlCatched(QString user, QString pass, QString host); }; -class WebEngineView : public QWebEngineView { - Q_OBJECT -public: - WebEngineView(QWidget* parent = 0); - bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame); -}; - - class WebEnginePage : public QWebEnginePage { public: WebEnginePage(QWebEngineProfile *profile, QObject* parent = nullptr); @@ -65,7 +57,7 @@ WebView::WebView(QWidget *parent) { _ui.setupUi(this); - _webview = new WebEngineView(this); + _webview = new QWebEngineView(this); _profile = new QWebEngineProfile(this); _page = new WebEnginePage(_profile); _interceptor = new WebViewPageUrlRequestInterceptor(this); @@ -165,21 +157,6 @@ bool ExternalWebEnginePage::acceptNavigationRequest(const QUrl &url, QWebEngineP return false; } - -WebEngineView::WebEngineView(QWidget* parent) : QWebEngineView (parent) { - -} - -bool WebEngineView::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) -{ - if (type == QWebEnginePage::NavigationTypeLinkClicked) - { - QDesktopServices::openUrl(url); - return false; - } - return true; -} - } #include "webview.moc" diff --git a/src/gui/wizard/webview.h b/src/gui/wizard/webview.h index 01529ef09..1146cd845 100644 --- a/src/gui/wizard/webview.h +++ b/src/gui/wizard/webview.h @@ -14,7 +14,6 @@ namespace OCC { class WebViewPageUrlRequestInterceptor; class WebViewPageUrlSchemeHandler; -class WebEngineView; class WebEnginePage; class WebView : public QWidget @@ -30,7 +29,7 @@ signals: private: Ui_WebView _ui; - WebEngineView *_webview; + QWebEngineView *_webview; QWebEngineProfile *_profile; WebEnginePage *_page;