Open external links in the system browser.

This commit is contained in:
Matthieu Gautier 2018-07-27 18:19:47 +02:00
parent 7900c1a4e1
commit 07e80eefa5
4 changed files with 45 additions and 2 deletions

View File

@ -41,7 +41,8 @@ SOURCES += \
src/tabwidget.cpp \ src/tabwidget.cpp \
src/webview.cpp \ src/webview.cpp \
src/searchbar.cpp \ src/searchbar.cpp \
src/mainmenu.cpp src/mainmenu.cpp \
src/webpage.cpp
HEADERS += \ HEADERS += \
src/mainwindow.h \ src/mainwindow.h \
@ -55,7 +56,8 @@ HEADERS += \
src/tabwidget.h \ src/tabwidget.h \
src/webview.h \ src/webview.h \
src/searchbar.h \ src/searchbar.h \
src/mainmenu.h src/mainmenu.h \
src/webpage.h
FORMS += \ FORMS += \
ui/mainwindow.ui ui/mainwindow.ui

19
src/webpage.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "webpage.h"
#include <QDesktopServices>
WebPage::WebPage(QObject *parent) :
QWebEnginePage(parent)
{
}
bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
{
if (url.scheme() != "zim") {
QDesktopServices::openUrl(url);
return false;
}
return true;
}

20
src/webpage.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef WEBPAGE_H
#define WEBPAGE_H
#include <QWebEnginePage>
class WebPage : public QWebEnginePage
{
Q_OBJECT
public:
explicit WebPage(QObject *parent = nullptr);
protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
signals:
public slots:
};
#endif // WEBPAGE_H

View File

@ -4,10 +4,12 @@
#include <QWebEngineProfile> #include <QWebEngineProfile>
#include <iostream> #include <iostream>
#include "kiwixapp.h" #include "kiwixapp.h"
#include "webpage.h"
WebView::WebView(QWidget *parent) WebView::WebView(QWidget *parent)
: QWebEngineView(parent) : QWebEngineView(parent)
{ {
setPage(new WebPage(this));
auto profile = page()->profile(); auto profile = page()->profile();
auto app = KiwixApp::instance(); auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); profile->installUrlSchemeHandler("zim", app->getSchemeHandler());