diff options
Diffstat (limited to 'src/webview.cpp')
| -rw-r--r-- | src/webview.cpp | 89 |
1 files changed, 58 insertions, 31 deletions
diff --git a/src/webview.cpp b/src/webview.cpp index bbed1cf..bd2861a 100644 --- a/src/webview.cpp +++ b/src/webview.cpp @@ -1,54 +1,81 @@ #include "webview.h" -#include <QWebFrame> -#include <QNetworkReply> #include <QMessageBox> #include <QTimer> +#include <QtWebEngineWidgets/QWebEngineProfile> +#include <QtWebEngineWidgets/QWebEnginePage> +#include <QMenu> +#include <QContextMenuEvent> 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); - page()->settings()->setAttribute(QWebSettings::LocalStorageEnabled, 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())); + : QWebEngineView(parent), + _timer(new QTimer(this)), + _abortedDownload(false) { + _timer->setSingleShot(true); + // Use custom page to manage SSL errors and window behavior + _customPage = new CustomPage(this); + setPage(_customPage); + // Cancel all downloads and show message + connect(page()->profile(), &QWebEngineProfile::downloadRequested, + this, &WebView::onDownloadRequested); + connect(page(), &QWebEnginePage::windowCloseRequested, + this, &WebView::windowCloseRequested); + connect(_timer, &QTimer::timeout, this, &WebView::downloadDeniedMessage); +} + +void WebView::setIgnoreSslErrors(bool on) +{ + if (_customPage) + _customPage->setIgnoreSslErrors(on); } void WebView::windowCloseRequested() { - // If we have an old URL stored on the stack, navigate back to it, otherwise we return and nothing happens - if (_urls.empty()) - return; - QUrl url = _urls.pop(); - page()->mainFrame()->load(url); + // If we have an old URL stored on the stack, navigate back to it + if (_urls.empty()) + return; + QUrl url = _urls.pop(); + this->load(url); } -QWebView* WebView::createWindow(QWebPage::WebWindowType) + +void WebView::contextMenuEvent(QContextMenuEvent* ev) { - // Remember current URL, then return the current Web View so no new window opens - _urls.push(this->url()); - return this; + // Build the default menu + QMenu* menu = page()->createStandardContextMenu(); + + // Find and remove/hide the "View source" action + QAction* viewSource = page()->action(QWebEnginePage::ViewSource); + if (viewSource) { + viewSource->setVisible(false); + viewSource->setEnabled(false); + } + + // Show the customized menu + menu->exec(ev->globalPos()); + menu->deleteLater(); + ev->accept(); } -void WebView::unsupportedContent(QNetworkReply* rep) +QWebEngineView* WebView::createWindow(QWebEnginePage::WebWindowType) { - _abortedDownload = true; - rep->abort(); - rep->deleteLater(); - _timer->start(1); + // Remember current URL, then load into same view (ignore creating new window) + _urls.push(this->url()); + // Return this view so the request is loaded here + return this; } -void WebView::downloadRequest(QNetworkRequest) +void WebView::onDownloadRequested(QWebEngineDownloadItem* item) { - _timer->start(1); + if (!item) + return; + _abortedDownload = true; + item->cancel(); + _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.")); + 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.")); } |
