#include "webview.h" #include #include #include #include #include #include WebView::WebView(QWidget* parent) : 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 if (_urls.empty()) return; QUrl url = _urls.pop(); this->load(url); } void WebView::contextMenuEvent(QContextMenuEvent* ev) { // 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(); } QWebEngineView* WebView::createWindow(QWebEnginePage::WebWindowType) { // 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::onDownloadRequested(QWebEngineDownloadItem* item) { 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.")); }