From 6acd73c4148bac2a8c2f637e70080a43b12fd14e Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 15 Aug 2022 14:35:20 -0700 Subject: [PATCH] Enable opening links in new windows. --- src/widgets/PrivacyWebEngineView.cpp | 15 +++- src/widgets/PrivacyWebEngineView.h | 1 + src/widgets/TabWidget.cpp | 129 ++++++++++++++++----------- src/widgets/TabWidget.h | 6 +- src/windows/BrowserWindow.cpp | 16 +++- src/windows/BrowserWindow.h | 3 +- 6 files changed, 107 insertions(+), 63 deletions(-) diff --git a/src/widgets/PrivacyWebEngineView.cpp b/src/widgets/PrivacyWebEngineView.cpp index ba56d31..10a3a47 100644 --- a/src/widgets/PrivacyWebEngineView.cpp +++ b/src/widgets/PrivacyWebEngineView.cpp @@ -41,8 +41,6 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const } QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) { - qDebug().noquote().nospace() << "Web window type: " << webWindowType; - // Get a handle for the browser window. BrowserWindow *browserWindowPointer = qobject_cast(window()); @@ -54,8 +52,19 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType return browserWindowPointer->tabWidgetPointer->addTab(true); } + case QWebEnginePage::WebBrowserWindow: { + // Create a new browser window. + BrowserWindow *newBrowserWindowPointer = new BrowserWindow(); + + // Show the new browser window. + newBrowserWindowPointer->show(); + + // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu. + return newBrowserWindowPointer->tabWidgetPointer->loadBlankInitialWebsite(); + } + default: { - // Return an null pointer. + // Return an null pointer for opening a background tab and opening a web dialog. return nullptr; } } diff --git a/src/widgets/PrivacyWebEngineView.h b/src/widgets/PrivacyWebEngineView.h index 8a04957..863cf5d 100644 --- a/src/widgets/PrivacyWebEngineView.h +++ b/src/widgets/PrivacyWebEngineView.h @@ -36,6 +36,7 @@ public: // The public variables. std::list *cookieListPointer = new std::list; QString domainSettingsName = QStringLiteral(""); + int loadProgressInt = -1; bool localStorageEnabled = false; signals: diff --git a/src/widgets/TabWidget.cpp b/src/widgets/TabWidget.cpp index 9e843ef..eb82f23 100644 --- a/src/widgets/TabWidget.cpp +++ b/src/widgets/TabWidget.cpp @@ -166,12 +166,55 @@ PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView) QWebEngineSettings *webEngineSettingsPointer = webEnginePagePointer->settings(); // Update the URL line edit when the URL changes. - connect(privacyWebEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl))); + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::urlChanged, [privacyWebEngineViewPointer, this] (const QUrl &newUrl) + { + // Only update the UI if this is the current tab. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + { + // Update the URL line edit. + emit updateUrlLineEdit(newUrl); - // Update the progress bar. - connect(privacyWebEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted())); - connect(privacyWebEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int))); - connect(privacyWebEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished())); + // Update the status of the forward and back buttons. + emit updateBackAction(currentWebEngineHistoryPointer->canGoBack()); + emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward()); + } + + // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. It can be removed once is fixed. + privacyWebEngineViewPointer->setZoomFactor(currentZoomFactor); + }); + + // Update the progress bar when a load is started. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadStarted, [privacyWebEngineViewPointer, this] () + { + // Store the load progress. + privacyWebEngineViewPointer->loadProgressInt = 0; + + // Show the progress bar if this is the current tab. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + emit showProgressBar(0); + }); + + // Update the progress bar when a load progresses. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadProgress, [privacyWebEngineViewPointer, this] (const int progress) + { + // Store the load progress. + privacyWebEngineViewPointer->loadProgressInt = progress; + + // Update the progress bar if this is the current tab. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + emit showProgressBar(progress); + }); + + // Update the progress bar when a load finishes. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadFinished, [privacyWebEngineViewPointer, this] () + { + // Store the load progress. + privacyWebEngineViewPointer->loadProgressInt = -1; + + // Hide the progress bar if this is the current tab. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + emit hideProgressBar(); + }); // Handle full screen requests. connect(webEnginePagePointer, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this, SLOT(fullScreenRequested(QWebEngineFullScreenRequest))); @@ -236,16 +279,13 @@ PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView) // Limit WebRTC to public IP addresses. webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true); - // Define an update cookie count lambda. - auto updateCookieCount = [privacyWebEngineViewPointer, this] (const int numberOfCookies) + // Update the cookies action. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, [privacyWebEngineViewPointer, this] (const int numberOfCookies) { // Update the cookie action if the specified privacy WebEngine view is the current privacy WebEngine view. if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) emit updateCookiesAction(numberOfCookies); - }; - - // Update the cookies action. - connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, this, updateCookieCount); + }); // Process cookie changes. connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), privacyWebEngineViewPointer, SLOT(addCookieToList(QNetworkCookie))); @@ -258,21 +298,22 @@ PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView) for (QNetworkCookie *cookiePointer : *durableCookiesListPointer) addCookieToStore(*cookiePointer, webEngineCookieStorePointer); - // Define an update tab title lambda. - auto updateTabTitle = [privacyWebEngineViewPointer, this] (const QString &title) + // Update the title when it changes. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, [this, privacyWebEngineViewPointer] (const QString &title) { // Get the index for this tab. int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer); // Update the title for this tab. tabWidgetPointer->setTabText(tabIndex, title); - }; - // Update the title when it changes. - connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, this, updateTabTitle); + // Update the window title if this is the current tab. + if (tabIndex == tabWidgetPointer->currentIndex()) + emit updateWindowTitle(title); + }); - // Define an update tab icon lambda. - auto updateTabIcon = [privacyWebEngineViewPointer, this] (const QIcon &icon) + // Update the icon when it changes. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, [privacyWebEngineViewPointer, this] (const QIcon &icon) { // Get the index for this tab. int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer); @@ -282,10 +323,7 @@ PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView) tabWidgetPointer->setTabIcon(tabIndex, defaultTabIcon); else tabWidgetPointer->setTabIcon(tabIndex, icon); - }; - - // Update the icon when it changes. - connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, this, updateTabIcon); + }); // Move to the new tab. tabWidgetPointer->setCurrentIndex(newTabIndex); @@ -601,10 +639,13 @@ void TabWidget::home() const currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage())); } -void TabWidget::loadFinished() const +PrivacyWebEngineView* TabWidget::loadBlankInitialWebsite() { - // Hide the progress bar. - emit hideProgressBar(); + // Apply the application settings. + applyApplicationSettings(); + + // Return the current privacy WebEngine view pointer. + return currentPrivacyWebEngineViewPointer; } void TabWidget::loadInitialWebsite() @@ -628,18 +669,6 @@ void TabWidget::loadInitialWebsite() } } -void TabWidget::loadProgress(const int &progress) const -{ - // Show the progress bar. - emit showProgressBar(progress); -} - -void TabWidget::loadStarted() const -{ - // Show the progress bar. - emit showProgressBar(0); -} - void TabWidget::loadUrlFromLineEdit(QString url) const { // Decide if the text is more likely to be a URL or a search. @@ -870,25 +899,21 @@ void TabWidget::updateUiWithTabSettings() emit clearUrlLineEditFocus(); // Update the UI. + emit updateBackAction(currentWebEngineHistoryPointer->canGoBack()); + emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size()); emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QStringLiteral("")); + emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); + emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward()); emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled); - emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); + emit updateWindowTitle(currentPrivacyWebEngineViewPointer->title()); + emit updateUrlLineEdit(currentPrivacyWebEngineViewPointer->url()); emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true); emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor()); - emit updateUrlLineEdit(currentPrivacyWebEngineViewPointer->url()); - emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size()); -} -void TabWidget::updateUrl(const QUrl &url) const -{ - // Update the URL line edit. - emit updateUrlLineEdit(url); - - // Update the status of the forward and back buttons. - emit updateBackAction(currentWebEngineHistoryPointer->canGoBack()); - emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward()); - - // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. - currentPrivacyWebEngineViewPointer->setZoomFactor(currentZoomFactor); + // Update the progress bar. + if (currentPrivacyWebEngineViewPointer->loadProgressInt >= 0) + emit showProgressBar(currentPrivacyWebEngineViewPointer->loadProgressInt); + else + emit hideProgressBar(); } diff --git a/src/widgets/TabWidget.h b/src/widgets/TabWidget.h index 6a24313..ed9ad03 100644 --- a/src/widgets/TabWidget.h +++ b/src/widgets/TabWidget.h @@ -50,6 +50,7 @@ public: // The public functions. void applyOnTheFlyZoomFactor(const double &zoomFactor); + PrivacyWebEngineView* loadBlankInitialWebsite(); void loadInitialWebsite(); std::list* getCookieList() const; QString& getDomainSettingsName() const; @@ -80,6 +81,7 @@ signals: void updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus) const; void updateUrlLineEdit(const QUrl &newUrl) const; void updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus) const; + void updateWindowTitle(const QString &title) const; void updateZoomFactorAction(const double &zoomFactor) const; public Q_SLOTS: @@ -108,15 +110,11 @@ private Q_SLOTS: void addFirstTab(); void deleteTab(const int tabIndex); void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest) const; - void loadFinished() const; - void loadProgress(const int &progress) const; - void loadStarted() const; void pageLinkHovered(const QString &linkUrl) const; void printWebpage(QPrinter *printerPointer) const; void showSaveDialog(QWebEngineDownloadItem *downloadItemPointer) const; void showSaveFilePickerDialog(QUrl &downloadUrl, QString &suggestedFileName); void updateUiWithTabSettings(); - void updateUrl(const QUrl &url) const; private: // The private variables. diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index acf72db..064c7ed 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -40,7 +40,7 @@ #include // Construct the class. -BrowserWindow::BrowserWindow() : KXmlGuiWindow() +BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow() { // Initialize the variables. javaScriptEnabled = false; @@ -247,6 +247,9 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Update the URL line edit on page loads. connect(tabWidgetPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl))); + // Update the window title. + connect(tabWidgetPointer, SIGNAL(updateWindowTitle(const QString)), this, SLOT(updateWindowTitle(const QString))); + // Get a handle for the status bar. QStatusBar *statusBarPointer = statusBar(); @@ -289,8 +292,9 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger())); connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape())); - // Load the initial website. - tabWidgetPointer->loadInitialWebsite(); + // Load the initial website if this is the first window. + if (firstWindow) + tabWidgetPointer->loadInitialWebsite(); } void BrowserWindow::addOrEditDomainSettings() const @@ -943,3 +947,9 @@ void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) c // Update the user agent label. userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName)); } + +void BrowserWindow::updateWindowTitle(const QString &title) +{ + // Update the window title. + setWindowTitle(title); +} diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index f880cc8..a131a44 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -40,7 +40,7 @@ class BrowserWindow : public KXmlGuiWindow public: // The default constructor. - BrowserWindow(); + BrowserWindow(bool firstWindow=true); // The public functions. QSize sizeHint() const override; @@ -81,6 +81,7 @@ private Q_SLOTS: void updateSearchEngineLabel(const QString &searchEngineString) const; void updateUrlLineEdit(const QUrl &newUrl); void updateUserAgentLabel(const QString &userAgentDatabaseName) const; + void updateWindowTitle(const QString &title); private: // The private variables. -- 2.43.0