]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Add a background tab action to the context menu. https://redmine.stoutner.com/issues/949
authorSoren Stoutner <soren@stoutner.com>
Wed, 28 Dec 2022 20:43:04 +0000 (13:43 -0700)
committerSoren Stoutner <soren@stoutner.com>
Wed, 28 Dec 2022 20:43:04 +0000 (13:43 -0700)
src/helpers/UserAgentHelper.cpp
src/interceptors/UrlRequestInterceptor.cpp
src/widgets/PrivacyWebEngineView.cpp
src/widgets/PrivacyWebEngineView.h
src/widgets/TabWidget.cpp
src/widgets/TabWidget.h

index 14a71e2cd8720ebd8978ca291a7f1081ecff9aa6..63baca9c56aaf0c315a17b87c5261be7d1a1707a 100644 (file)
@@ -49,12 +49,12 @@ const QString UserAgentHelper::SAFARI_MACOS_TRANSLATED = i18n("Safari on macOS")
 
 // Define the public user agent constants.
 const QString UserAgentHelper::PRIVACY_BROWSER_USER_AGENT = QStringLiteral("PrivacyBrowser/1.0");
-const QString UserAgentHelper::FIREFOX_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0");
-const QString UserAgentHelper::CHROMIUM_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36");
-const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0");
-const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36");
-const QString UserAgentHelper::EDGE_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71");
-const QString UserAgentHelper::SAFARI_MACOS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15");
+const QString UserAgentHelper::FIREFOX_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0");
+const QString UserAgentHelper::CHROMIUM_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");
+const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0");
+const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");
+const QString UserAgentHelper::EDGE_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54");
+const QString UserAgentHelper::SAFARI_MACOS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15");
 
 // Construct the class.
 UserAgentHelper::UserAgentHelper() {};
index 634889867ad386464e5472b1d01c5ff1ccca6075..83f104595ac8ab67e904d04b48b769b3c05556f8 100644 (file)
@@ -31,34 +31,6 @@ UrlRequestInterceptor::UrlRequestInterceptor(QObject *parentObjectPointer) : QWe
 
 void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlRequestInfo)
 {
-    // Handle the request according to the navigation type.
-    switch (urlRequestInfo.navigationType())
-    {
-        case QWebEngineUrlRequestInfo::NavigationTypeLink:
-        case QWebEngineUrlRequestInfo::NavigationTypeTyped:
-        case QWebEngineUrlRequestInfo::NavigationTypeBackForward:
-        case QWebEngineUrlRequestInfo::NavigationTypeRedirect:
-        {
-            // Only check the hosts if the main URL is changing.
-            if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
-            {
-                // Get the hosts.
-                QString requestingHost = urlRequestInfo.initiator().host();
-                QString requestedHost = urlRequestInfo.requestUrl().host();
-
-                // Reapply the domain settings if the host is changing.
-                if (requestingHost != requestedHost)
-                    emit applyDomainSettings(requestedHost);
-            }
-
-            break;
-        }
-
-        default:
-            // Do nothing.
-            break;
-    }
-
     // Handle the request according to the resource type.
     switch (urlRequestInfo.resourceType())
     {
@@ -96,4 +68,33 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &urlReques
             break;
         }
     }
+
+    // Handle the request according to the navigation type.
+    switch (urlRequestInfo.navigationType())
+    {
+        case QWebEngineUrlRequestInfo::NavigationTypeLink:
+        case QWebEngineUrlRequestInfo::NavigationTypeTyped:
+        case QWebEngineUrlRequestInfo::NavigationTypeBackForward:
+        // case QWebEngineUrlRequestInfo::NavigationTypeReload:  This can be uncommented once https://redmine.stoutner.com/issues/821 has been fixed.
+        case QWebEngineUrlRequestInfo::NavigationTypeRedirect:
+        {
+            // Only check the hosts if the main URL is changing.
+            if (urlRequestInfo.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame)
+            {
+                // Get the hosts.
+                QString requestingHost = urlRequestInfo.initiator().host();
+                QString requestedHost = urlRequestInfo.requestUrl().host();
+
+                // Reapply the domain settings if the host is changing.
+                if (requestingHost != requestedHost)
+                    emit applyDomainSettings(requestedHost);
+            }
+
+            break;
+        }
+
+        default:
+            // Do nothing.
+            break;
+    }
 }
index 10a3a47fcf3f7124a194ce4c62f31b273c5eeaac..d7b3f8c1de62a7be5050dc274503d29a690781a2 100644 (file)
 #include "databases/CookiesDatabase.h"
 #include "windows/BrowserWindow.h"
 
+// Qt toolkit headers.
+#include <QContextMenuEvent>
+#include <QMenu>
+
 // Construct the class.
 PrivacyWebEngineView::PrivacyWebEngineView() : QWebEngineView(nullptr) {}
 
@@ -40,19 +44,40 @@ void PrivacyWebEngineView::addCookieToList(const QNetworkCookie &cookie) const
     emit updateCookiesAction(cookieListPointer->size());
 }
 
+void PrivacyWebEngineView::contextMenuEvent(QContextMenuEvent *contextMenuEvent) {
+    // Get a handle for the
+    QWebEnginePage *webEnginePagePointer = page();
+
+    // Get a handle for the menu.
+    QMenu *contextMenu = webEnginePagePointer->createStandardContextMenu();
+
+    // Get the list of context menu actions.
+    const QList<QAction *> contextMenuActionsList = contextMenu->actions();
+
+    // Add the open link in new background tab action if the context menu already contains the open link in new window action.
+    if (contextMenuActionsList.contains(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow)))
+        contextMenu->insertAction(webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewWindow), webEnginePagePointer->action(QWebEnginePage::OpenLinkInNewBackgroundTab));
+
+    // Display the menu using the location in the context menu event.
+    contextMenu->popup(contextMenuEvent->globalPos());
+}
+
 QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType webWindowType) {
     // Get a handle for the browser window.
     BrowserWindow *browserWindowPointer = qobject_cast<BrowserWindow*>(window());
 
     // Create the requsted window type.
-    switch (webWindowType) {
-        case QWebEnginePage::WebBrowserTab: {
+    switch (webWindowType)
+    {
+        case QWebEnginePage::WebBrowserTab:
+        {
             // Create the new tab and return the privacy WebEngine view pointer.  `true` removes the focus from the blank URL line edit.
             // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
             return browserWindowPointer->tabWidgetPointer->addTab(true);
         }
 
-        case QWebEnginePage::WebBrowserWindow: {
+        case QWebEnginePage::WebBrowserWindow:
+        {
             // Create a new browser window.
             BrowserWindow *newBrowserWindowPointer = new BrowserWindow();
 
@@ -63,7 +88,15 @@ QWebEngineView* PrivacyWebEngineView::createWindow(QWebEnginePage::WebWindowType
             return newBrowserWindowPointer->tabWidgetPointer->loadBlankInitialWebsite();
         }
 
-        default: {
+        case QWebEnginePage::WebBrowserBackgroundTab:
+        {
+            // Create the new tab and return the privacy WebEngine view pointer.  `false` does not clear the URL line edit.  `true` creates a background tab.
+            // The new privacy WebEngine view pointer is returned so it can be populated with the link from the context menu.
+            return browserWindowPointer->tabWidgetPointer->addTab(false, true);
+        }
+
+        default:
+        {
             // Return an null pointer for opening a background tab and opening a web dialog.
             return nullptr;
         }
index c227f428ee2bd1a0aab6a0b89a8708f2a6335519..31319da98c3b8557f972735a0d6cd950b34da82e 100644 (file)
@@ -54,6 +54,7 @@ public Q_SLOTS:
 
 protected:
     // The protected functions.
+    void contextMenuEvent(QContextMenuEvent *contextMenuEvent) override;
     QWebEngineView* createWindow(QWebEnginePage::WebWindowType webWindowType) override;
 };
 #endif
index ff8563c96a9b5d8b311aca160f51b3098e5ab41c..bd62f83beaa521343768bb9355fa3922359cd1f3 100644 (file)
@@ -141,7 +141,7 @@ void TabWidget::addFirstTab()
     tabWidgetPointer->currentWidget()->setFocus();
 }
 
-PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView)
+PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool backgroundTab)
 {
     // Create a privacy WebEngine view.
     PrivacyWebEngineView *privacyWebEngineViewPointer = new PrivacyWebEngineView();
@@ -337,11 +337,12 @@ PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView)
     // Populate the zoom factor.  This is necessary if a URL is being loaded, like a local URL, that does not trigger `applyDomainSettings()`.
     privacyWebEngineViewPointer->setZoomFactor(Settings::zoomFactor());
 
-    // Move to the new tab.
-    tabWidgetPointer->setCurrentIndex(newTabIndex);
+    // Move to the new tab if it is not a background tab.
+    if (!backgroundTab)
+        tabWidgetPointer->setCurrentIndex(newTabIndex);
 
     // Clear the URL line edit focus so that it populates correctly when opening a new tab from the context menu.
-    if (focusNewWebEngineView)
+    if (removeUrlLineEditFocus)
         emit clearUrlLineEditFocus();
 
     // Return the privacy WebEngine view pointer.
index bd857b0d65a8b431c8a7dd08779cde91d057ad30..246daa41cf4860c9c080426309a815e7de3c69f6 100644 (file)
@@ -91,7 +91,7 @@ signals:
 public Q_SLOTS:
     // The public slots.
     void addCookieToStore(QNetworkCookie cookie, QWebEngineCookieStore *webEngineCookieStorePointer = nullptr) const;
-    PrivacyWebEngineView* addTab(const bool focusNewWebEngineView=false);
+    PrivacyWebEngineView* addTab(const bool removeUrlLineEditFocus=false, const bool backgroundTab=false);
     void applyApplicationSettings();
     void applyDomainSettingsAndReload();
     void applyDomainSettingsWithoutReloading(const QString &hostname);