web UI: Open all links in external browser
This commit is contained in:
parent
375492e20a
commit
d66bedf988
|
@ -21,6 +21,7 @@
|
|||
# include <QWebView>
|
||||
# include <QWebFrame>
|
||||
# include <QSslError>
|
||||
# include <QNetworkRequest>
|
||||
#endif
|
||||
|
||||
using namespace Data;
|
||||
|
@ -41,13 +42,16 @@ WebPage::WebPage(WebViewDialog *dlg, WEB_VIEW_PROVIDER *view) :
|
|||
connect(&Data::networkAccessManager(), &QNetworkAccessManager::authenticationRequired, this, static_cast<void(WebPage::*)(QNetworkReply *, QAuthenticator *)>(&WebPage::supplyCredentials));
|
||||
connect(&Data::networkAccessManager(), &QNetworkAccessManager::sslErrors, this, static_cast<void(WebPage::*)(QNetworkReply *, const QList<QSslError> &errors)>(&WebPage::handleSslErrors));
|
||||
#endif
|
||||
|
||||
if(!m_view) {
|
||||
// delegate to external browser if no view is assigned
|
||||
// initialization for new window
|
||||
// -> delegate to external browser if no view is assigned
|
||||
#ifdef SYNCTHINGTRAY_USE_WEBENGINE
|
||||
connect(this, &WebPage::urlChanged, this, &WebPage::delegateToExternalBrowser);
|
||||
connect(this, &WebPage::urlChanged, this, &WebPage::delegateNewWindowToExternalBrowser);
|
||||
#else
|
||||
connect(this->mainFrame(), &QWebFrame::urlChanged, this, &WebPage::delegateToExternalBrowser);
|
||||
connect(this->mainFrame(), &QWebFrame::urlChanged, this, &WebPage::delegateNewWindowToExternalBrowser);
|
||||
#endif
|
||||
// -> there need to be a view, though
|
||||
m_view = new WEB_VIEW_PROVIDER;
|
||||
m_view->setPage(this);
|
||||
}
|
||||
|
@ -72,9 +76,24 @@ bool WebPage::certificateError(const QWebEngineCertificateError &certificateErro
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool WebPage::acceptNavigationRequest(const QUrl &url, WEB_PAGE_PROVIDER::NavigationType type, bool isMainFrame)
|
||||
{
|
||||
Q_UNUSED(isMainFrame)
|
||||
Q_UNUSED(type)
|
||||
return handleNavigationRequest(this->url(), url);
|
||||
}
|
||||
|
||||
#else // SYNCTHINGTRAY_USE_WEBKIT
|
||||
bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, WEB_PAGE_PROVIDER::NavigationType type)
|
||||
{
|
||||
Q_UNUSED(frame)
|
||||
Q_UNUSED(type)
|
||||
return handleNavigationRequest(mainFrame() ? mainFrame()->url() : QUrl(), request.url());
|
||||
}
|
||||
#endif
|
||||
|
||||
void WebPage::delegateToExternalBrowser(const QUrl &url)
|
||||
void WebPage::delegateNewWindowToExternalBrowser(const QUrl &url)
|
||||
{
|
||||
QDesktopServices::openUrl(url);
|
||||
// this page and the associated view are useless
|
||||
|
@ -102,6 +121,33 @@ void WebPage::supplyCredentials(QAuthenticator *authenticator)
|
|||
}
|
||||
}
|
||||
|
||||
bool WebPage::handleNavigationRequest(const QUrl ¤tUrl, const QUrl &targetUrl)
|
||||
{
|
||||
if(currentUrl.isEmpty()) {
|
||||
// allow initial request
|
||||
return true;
|
||||
}
|
||||
// only allow navigation on the same page
|
||||
if(currentUrl.scheme() == targetUrl.scheme()
|
||||
&& currentUrl.host() == targetUrl.host()
|
||||
&& currentUrl.port() == targetUrl.port()) {
|
||||
QString currentPath = currentUrl.path();
|
||||
while(currentPath.endsWith(QChar('/'))) {
|
||||
currentPath.resize(currentPath.size() - 1);
|
||||
}
|
||||
QString targetPath = targetUrl.path();
|
||||
while(targetPath.endsWith(QChar('/'))) {
|
||||
targetPath.resize(targetPath.size() - 1);
|
||||
}
|
||||
if(currentPath == targetPath) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// otherwise open URL in external browser
|
||||
QDesktopServices::openUrl(targetUrl);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef SYNCTHINGTRAY_USE_WEBKIT
|
||||
void WebPage::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
|
||||
{
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
QT_FORWARD_DECLARE_CLASS(WEB_VIEW_PROVIDER)
|
||||
QT_FORWARD_DECLARE_CLASS(QAuthenticator)
|
||||
QT_FORWARD_DECLARE_CLASS(QNetworkReply)
|
||||
QT_FORWARD_DECLARE_CLASS(QNetworkRequest)
|
||||
QT_FORWARD_DECLARE_CLASS(QSslError)
|
||||
|
||||
namespace QtGui {
|
||||
|
@ -29,10 +30,13 @@ protected:
|
|||
WEB_PAGE_PROVIDER *createWindow(WebWindowType type);
|
||||
#ifdef SYNCTHINGTRAY_USE_WEBENGINE
|
||||
bool certificateError(const QWebEngineCertificateError &certificateError);
|
||||
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
|
||||
#else
|
||||
bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
|
||||
#endif
|
||||
|
||||
private slots:
|
||||
void delegateToExternalBrowser(const QUrl &url);
|
||||
void delegateNewWindowToExternalBrowser(const QUrl &url);
|
||||
void supplyCredentials(const QUrl &requestUrl, QAuthenticator *authenticator);
|
||||
void supplyCredentials(QNetworkReply *reply, QAuthenticator *authenticator);
|
||||
void supplyCredentials(QAuthenticator *authenticator);
|
||||
|
@ -41,6 +45,8 @@ private slots:
|
|||
#endif
|
||||
|
||||
private:
|
||||
bool handleNavigationRequest(const QUrl ¤tUrl, const QUrl &url);
|
||||
|
||||
WebViewDialog *m_dlg;
|
||||
WEB_VIEW_PROVIDER *m_view;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue