From f90a40836c462c647a24d97b35a0a9487cf29feb Mon Sep 17 00:00:00 2001 From: David Rosca Date: Fri, 22 May 2015 23:57:24 +0200 Subject: [PATCH] PopupWindows: Port to QtWebEngine It now correctly detects when to open tabs and when to open popup windows. Support for opening new browser windows is not yet implemented. --- src/lib/lib.pro | 2 - src/lib/popupwindow/popupwebpage.cpp | 155 --------------------------- src/lib/popupwindow/popupwebpage.h | 60 ----------- src/lib/popupwindow/popupwebview.cpp | 25 +---- src/lib/popupwindow/popupwebview.h | 7 +- src/lib/popupwindow/popupwindow.cpp | 25 ++--- src/lib/popupwindow/popupwindow.h | 5 +- src/lib/webkit/webpage.cpp | 74 +++++++------ src/lib/webkit/webpage.h | 3 +- 9 files changed, 62 insertions(+), 294 deletions(-) delete mode 100644 src/lib/popupwindow/popupwebpage.cpp delete mode 100644 src/lib/popupwindow/popupwebpage.h diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 4a2203b28..f9ccd0f92 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -170,7 +170,6 @@ SOURCES += \ plugins/speeddial.cpp \ popupwindow/popuplocationbar.cpp \ popupwindow/popupstatusbarmessage.cpp \ - popupwindow/popupwebpage.cpp \ popupwindow/popupwebview.cpp \ popupwindow/popupwindow.cpp \ preferences/acceptlanguage.cpp \ @@ -373,7 +372,6 @@ HEADERS += \ plugins/speeddial.h \ popupwindow/popuplocationbar.h \ popupwindow/popupstatusbarmessage.h \ - popupwindow/popupwebpage.h \ popupwindow/popupwebview.h \ popupwindow/popupwindow.h \ preferences/acceptlanguage.h \ diff --git a/src/lib/popupwindow/popupwebpage.cpp b/src/lib/popupwindow/popupwebpage.cpp deleted file mode 100644 index b3d579dcb..000000000 --- a/src/lib/popupwindow/popupwebpage.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* ============================================================ */ -#include "popupwebpage.h" -#include "popupwebview.h" -#include "popupwindow.h" -#include "browserwindow.h" -#include "tabwidget.h" -#include "tabbedwebview.h" - -#include -#include - -// Wrapper class to detect whether window is opened from JavaScript window.open method -// It has to be done this way, because QtWebKit has really bad API when it comes to opening -// new windows. -// -// Got an idea how to determine it from kWebKitPart. - -PopupWebPage::PopupWebPage(QWebEnginePage::WebWindowType type, BrowserWindow* window) - : WebPage() - , m_window(window) - , m_type(type) - , m_createNewWindow(false) - , m_menuBarVisible(false) - , m_statusBarVisible(false) - , m_toolBarVisible(false) - , m_isLoading(false) - , m_progress(0) -{ - connect(this, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(slotGeometryChangeRequested(QRect))); -#if QTWEBENGINE_DISABLED - connect(this, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(slotMenuBarVisibilityChangeRequested(bool))); - connect(this, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(slotToolBarVisibilityChangeRequested(bool))); - connect(this, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(slotStatusBarVisibilityChangeRequested(bool))); -#endif - - connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted())); - connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int))); - connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool))); - - QTimer::singleShot(0, this, SLOT(checkBehaviour())); -} - -BrowserWindow* PopupWebPage::mainWindow() const -{ - return m_window; -} - -void PopupWebPage::slotGeometryChangeRequested(const QRect &rect) -{ - if (rect.isValid()) { - m_createNewWindow = true; - } - - m_geometry = rect; -} - -void PopupWebPage::slotMenuBarVisibilityChangeRequested(bool visible) -{ - m_menuBarVisible = visible; -} - -void PopupWebPage::slotStatusBarVisibilityChangeRequested(bool visible) -{ - m_statusBarVisible = visible; -} - -void PopupWebPage::slotToolBarVisibilityChangeRequested(bool visible) -{ - m_toolBarVisible = visible; -} - -void PopupWebPage::slotLoadStarted() -{ - m_isLoading = true; - m_progress = 0; -} - -void PopupWebPage::slotLoadProgress(int prog) -{ - m_progress = prog; -} - -void PopupWebPage::slotLoadFinished(bool state) -{ - Q_UNUSED(state) - - m_isLoading = false; - m_progress = 0; -} - -void PopupWebPage::checkBehaviour() -{ - // If menubar/statusbar/toolbar visibility is explicitly set in window.open call, - // at least one of those variables will be false. - // If so, we should open new window. - // But not when all visibilities are false, it occurs with target=_blank links - - if (!m_createNewWindow && (!m_menuBarVisible || !m_statusBarVisible || !m_toolBarVisible) && - !(!m_menuBarVisible && !m_statusBarVisible && !m_toolBarVisible) - ) { - m_createNewWindow = true; - } - - if (m_createNewWindow) { - PopupWebView* view = new PopupWebView; - view->setWebPage(this); - - PopupWindow* popup = new PopupWindow(view); - popup->setWindowGeometry(m_geometry); - popup->setMenuBarVisibility(m_menuBarVisible); - popup->setStatusBarVisibility(m_statusBarVisible); - popup->setToolBarVisibility(m_toolBarVisible); - popup->show(); - - if (m_isLoading) { - view->fakeLoadingProgress(m_progress); - } - - m_window->addDeleteOnCloseWidget(popup); - - disconnect(this, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(slotGeometryChangeRequested(QRect))); - disconnect(this, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(slotMenuBarVisibilityChangeRequested(bool))); - disconnect(this, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(slotToolBarVisibilityChangeRequested(bool))); - disconnect(this, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(slotStatusBarVisibilityChangeRequested(bool))); - - disconnect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted())); - disconnect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int))); - disconnect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool))); - } - else { - int index = m_window->tabWidget()->addView(QUrl(), Qz::NT_CleanSelectedTab); - TabbedWebView* view = m_window->weView(index); - view->setWebPage(this); - - if (m_isLoading) { - view->fakeLoadingProgress(m_progress); - } - } -} diff --git a/src/lib/popupwindow/popupwebpage.h b/src/lib/popupwindow/popupwebpage.h deleted file mode 100644 index 9d982c036..000000000 --- a/src/lib/popupwindow/popupwebpage.h +++ /dev/null @@ -1,60 +0,0 @@ -/* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* ============================================================ */ -#ifndef POPUPWEBPAGE_H -#define POPUPWEBPAGE_H - -#include "qzcommon.h" -#include "webpage.h" - -class BrowserWindow; - -class QUPZILLA_EXPORT PopupWebPage : public WebPage -{ - Q_OBJECT -public: - explicit PopupWebPage(WebWindowType type, BrowserWindow* window); - - BrowserWindow* mainWindow() const; - -private slots: - void slotGeometryChangeRequested(const QRect &rect); - void slotMenuBarVisibilityChangeRequested(bool visible); - void slotStatusBarVisibilityChangeRequested(bool visible); - void slotToolBarVisibilityChangeRequested(bool visible); - - void slotLoadStarted(); - void slotLoadProgress(int prog); - void slotLoadFinished(bool state); - - void checkBehaviour(); - -private: - BrowserWindow* m_window; - QWebEnginePage::WebWindowType m_type; - bool m_createNewWindow; - - bool m_menuBarVisible; - bool m_statusBarVisible; - bool m_toolBarVisible; - QRect m_geometry; - - bool m_isLoading; - bool m_progress; -}; - -#endif // POPUPWEBPAGE_H diff --git a/src/lib/popupwindow/popupwebview.cpp b/src/lib/popupwindow/popupwebview.cpp index f7401586d..5a0e55bc0 100644 --- a/src/lib/popupwindow/popupwebview.cpp +++ b/src/lib/popupwindow/popupwebview.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2015 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,7 +16,6 @@ * along with this program. If not, see . * ============================================================ */ #include "popupwebview.h" -#include "popupwebpage.h" #include "mainapplication.h" #include "browserwindow.h" #include "tabwidget.h" @@ -29,33 +28,11 @@ PopupWebView::PopupWebView(QWidget* parent) : WebView(parent) - , m_page(0) , m_menu(new Menu(this)) { m_menu->setCloseOnMiddleClick(true); } -void PopupWebView::setWebPage(PopupWebPage* page) -{ - if (m_page == page) { - return; - } - - if (m_page) { - delete m_page; - m_page = 0; - } - - m_page = page; - m_page->setParent(this); - setPage(m_page); -} - -PopupWebPage* PopupWebView::webPage() -{ - return m_page; -} - QWidget* PopupWebView::overlayWidget() { return this; diff --git a/src/lib/popupwindow/popupwebview.h b/src/lib/popupwindow/popupwebview.h index e1df6acef..b31aeacad 100644 --- a/src/lib/popupwindow/popupwebview.h +++ b/src/lib/popupwindow/popupwebview.h @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2015 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,7 +21,6 @@ #include "qzcommon.h" #include "webview.h" -class PopupWebPage; class LoadRequest; class Menu; @@ -31,9 +30,6 @@ class QUPZILLA_EXPORT PopupWebView : public WebView public: explicit PopupWebView(QWidget* parent = 0); - void setWebPage(PopupWebPage* page); - PopupWebPage* webPage(); - QWidget* overlayWidget(); void loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position); void openNewTab(Qz::NewTabPositionFlags position); @@ -47,7 +43,6 @@ public slots: private: void contextMenuEvent(QContextMenuEvent* event); - PopupWebPage* m_page; Menu* m_menu; }; diff --git a/src/lib/popupwindow/popupwindow.cpp b/src/lib/popupwindow/popupwindow.cpp index 034e91c19..216aa4734 100644 --- a/src/lib/popupwindow/popupwindow.cpp +++ b/src/lib/popupwindow/popupwindow.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2015 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ * ============================================================ */ #include "popupwindow.h" #include "popupwebview.h" -#include "popupwebpage.h" +#include "webpage.h" #include "popupstatusbarmessage.h" #include "progressbar.h" #include "pagescreen.h" @@ -34,7 +34,6 @@ PopupWindow::PopupWindow(PopupWebView* view) : QWidget() , m_view(view) - , m_page(qobject_cast(view->page())) , m_search(0) { m_layout = new QVBoxLayout(this); @@ -60,7 +59,7 @@ PopupWindow::PopupWindow(PopupWebView* view) menuFile->addAction(QIcon::fromTheme("document-save"), tr("&Save Page As..."), m_view, SLOT(savePageAs()))->setShortcut(QKeySequence("Ctrl+S")); menuFile->addAction(tr("Save Page Screen"), this, SLOT(savePageScreen())); menuFile->addAction(QIcon::fromTheme("mail-message-new"), tr("Send Link..."), m_view, SLOT(sendPageByMail())); - menuFile->addAction(QIcon::fromTheme("document-print"), tr("&Print..."), m_view, SLOT(printPage()))->setShortcut(QKeySequence("Ctrl+P")); + //menuFile->addAction(QIcon::fromTheme("document-print"), tr("&Print..."), m_view, SLOT(printPage()))->setShortcut(QKeySequence("Ctrl+P")); menuFile->addSeparator(); menuFile->addAction(QIcon::fromTheme("window-close"), tr("Close"), this, SLOT(close()))->setShortcut(QKeySequence("Ctrl+W")); m_menuBar->addMenu(menuFile); @@ -87,7 +86,7 @@ PopupWindow::PopupWindow(PopupWebView* view) m_menuView->addAction(QIcon::fromTheme("zoom-out"), tr("Zoom &Out"), m_view, SLOT(zoomOut()))->setShortcut(QKeySequence("Ctrl+-")); m_menuView->addAction(QIcon::fromTheme("zoom-original"), tr("Reset"), m_view, SLOT(zoomReset()))->setShortcut(QKeySequence("Ctrl+0")); m_menuView->addSeparator(); - m_menuView->addAction(QIcon::fromTheme("text-html"), tr("&Page Source"), m_view, SLOT(showSource()))->setShortcut(QKeySequence("Ctrl+U")); + //m_menuView->addAction(QIcon::fromTheme("text-html"), tr("&Page Source"), m_view, SLOT(showSource()))->setShortcut(QKeySequence("Ctrl+U")); m_menuBar->addMenu(m_menuView); // Make shortcuts available even with hidden menubar @@ -109,16 +108,18 @@ PopupWindow::PopupWindow(PopupWebView* view) connect(m_view, SIGNAL(titleChanged(QString)), this, SLOT(titleChanged())); connect(m_view, SIGNAL(urlChanged(QUrl)), m_locationBar, SLOT(showUrl(QUrl))); connect(m_view, SIGNAL(iconChanged()), m_locationBar, SLOT(showSiteIcon())); - connect(m_view, SIGNAL(statusBarMessage(QString)), this, SLOT(showStatusBarMessage(QString))); + //connect(m_view, SIGNAL(statusBarMessage(QString)), this, SLOT(showStatusBarMessage(QString))); connect(m_view, SIGNAL(loadStarted()), this, SLOT(loadStarted())); connect(m_view, SIGNAL(loadProgress(int)), this, SLOT(loadProgress(int))); connect(m_view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); - connect(m_page, SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(showStatusBarMessage(QString))); - connect(m_page, SIGNAL(geometryChangeRequested(QRect)), this, SLOT(setWindowGeometry(QRect))); - connect(m_page, SIGNAL(statusBarVisibilityChangeRequested(bool)), this, SLOT(setStatusBarVisibility(bool))); - connect(m_page, SIGNAL(menuBarVisibilityChangeRequested(bool)), this, SLOT(setMenuBarVisibility(bool))); - connect(m_page, SIGNAL(toolBarVisibilityChangeRequested(bool)), this, SLOT(setToolBarVisibility(bool))); + connect(m_view->page(), &WebPage::linkHovered, this, &PopupWindow::showStatusBarMessage); + connect(m_view->page(), &WebPage::geometryChangeRequested, this, &PopupWindow::setWindowGeometry); +#if QTWEBENGINE_DISABLED + connect(m_view->page(), &WebPage::statusBarVisibilityChangeRequested, this, &PopupWindow::setStatusBarVisibility); + connect(m_view->page(), &WebPage::menuBarVisibilityChangeRequested, this, &PopupWindow::setMenuBarVisibility); + connect(m_view->page(), &WebPage::toolBarVisibilityChangeRequested, this, &PopupWindow::setToolBarVisibility); +#endif m_view->setFocus(); titleChanged(); @@ -201,7 +202,7 @@ void PopupWindow::loadFinished() void PopupWindow::closeEvent(QCloseEvent* event) { - if (m_page->isRunningLoop()) { + if (m_view->page()->isRunningLoop()) { event->ignore(); return; } diff --git a/src/lib/popupwindow/popupwindow.h b/src/lib/popupwindow/popupwindow.h index 3cb08bc33..e1a40a1ff 100644 --- a/src/lib/popupwindow/popupwindow.h +++ b/src/lib/popupwindow/popupwindow.h @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2015 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,8 +28,8 @@ class QStatusBar; class QMenuBar; class QMenu; +class WebPage; class PopupWebView; -class PopupWebPage; class PopupStatusBarMessage; class PopupLocationBar; class ProgressBar; @@ -66,7 +66,6 @@ private: void closeEvent(QCloseEvent* event); PopupWebView* m_view; - PopupWebPage* m_page; PopupLocationBar* m_locationBar; PopupStatusBarMessage* m_statusBarMessage; ProgressBar* m_progressBar; diff --git a/src/lib/webkit/webpage.cpp b/src/lib/webkit/webpage.cpp index aa14d1dab..4abd8e288 100644 --- a/src/lib/webkit/webpage.cpp +++ b/src/lib/webkit/webpage.cpp @@ -27,8 +27,8 @@ #include "qztools.h" #include "speeddial.h" #include "autofill.h" -#include "popupwebpage.h" #include "popupwebview.h" +#include "popupwindow.h" #include "networkmanagerproxy.h" #include "adblockicon.h" #include "adblockmanager.h" @@ -41,6 +41,7 @@ #include "html5permissions/html5permissionsmanager.h" #include "schemehandlers/fileschemehandler.h" #include "javascript/externaljsobject.h" +#include "tabwidget.h" #ifdef NONBLOCK_JS_DIALOGS #include "ui_jsconfirm.h" @@ -728,31 +729,6 @@ QSslCertificate WebPage::sslCertificate() return QSslCertificate(); } -#if QTWEBENGINE_DISABLED -bool WebPage::acceptNavigationRequest(QWebEngineFrame* frame, const QNetworkRequest &request, NavigationType type) -{ - m_lastRequestType = type; - m_lastRequestUrl = request.url(); - - if (type == QWebEnginePage::NavigationTypeFormResubmitted) { - // Don't show this dialog if app is still starting - if (!view() || !view()->isVisible()) { - return false; - } - QString message = tr("To display this page, QupZilla must resend the request \n" - "(such as a search or order confirmation) that was performed earlier."); - bool result = (QMessageBox::question(view(), tr("Confirm form resubmission"), - message, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes); - if (!result) { - return false; - } - } - - bool accept = QWebEnginePage::acceptNavigationRequest(frame, request, type); - return accept; -} -#endif - void WebPage::populateNetworkRequest(QNetworkRequest &request) { WebPage* pagePointer = this; @@ -772,15 +748,27 @@ void WebPage::populateNetworkRequest(QNetworkRequest &request) QWebEnginePage* WebPage::createWindow(QWebEnginePage::WebWindowType type) { - if (m_view) { - return new PopupWebPage(type, m_view->browserWindow()); + switch (type) { + case QWebEnginePage::WebBrowserWindow: // TODO + case QWebEnginePage::WebBrowserTab: { + int index = m_view->browserWindow()->tabWidget()->addView(QUrl(), Qz::NT_CleanSelectedTab); + TabbedWebView* view = m_view->browserWindow()->weView(index); + view->setPage(new WebPage); + return view->page(); } - if (PopupWebPage* popupPage = qobject_cast(this)) { - return new PopupWebPage(type, popupPage->mainWindow()); + case QWebEnginePage::WebDialog: { + PopupWebView* view = new PopupWebView; + view->setPage(new WebPage); + PopupWindow* popup = new PopupWindow(view); + popup->show(); + m_view->browserWindow()->addDeleteOnCloseWidget(popup); + return view->page(); } - return 0; + default: + return 0; + } } QObject* WebPage::createPlugin(const QString &classid, const QUrl &url, @@ -800,6 +788,30 @@ QObject* WebPage::createPlugin(const QString &classid, const QUrl &url, return 0; } +bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) +{ + m_lastRequestUrl = url; + +#if QTWEBENGINE_DISABLED + if (type == QWebEnginePage::NavigationTypeFormResubmitted) { + // Don't show this dialog if app is still starting + if (!view() || !view()->isVisible()) { + return false; + } + QString message = tr("To display this page, QupZilla must resend the request \n" + "(such as a search or order confirmation) that was performed earlier."); + bool result = (QMessageBox::question(view(), tr("Confirm form resubmission"), + message, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes); + if (!result) { + return false; + } + } +#endif + + return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame); + +} + void WebPage::addAdBlockRule(const AdBlockRule* rule, const QUrl &url) { AdBlockedEntry entry; diff --git a/src/lib/webkit/webpage.h b/src/lib/webkit/webpage.h index d700f30ed..29998bf91 100644 --- a/src/lib/webkit/webpage.h +++ b/src/lib/webkit/webpage.h @@ -128,10 +128,11 @@ private: #if QTWEBENGINE_DISABLED bool supportsExtension(Extension extension) const; bool extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output = 0); - bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type); QString chooseFile(QWebFrame* originatingFrame, const QString &oldFile); #endif + bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) Q_DECL_OVERRIDE; + void handleUnknownProtocol(const QUrl &url); void desktopServicesOpen(const QUrl &url);