From: Soren Stoutner Date: Tue, 12 Apr 2022 20:29:06 +0000 (-0700) Subject: Add a WebEngine Default user agent. X-Git-Tag: v0.1~39 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=2e5db0d355267f5823610100947970af12ca6342;p=PrivacyBrowserPC.git Add a WebEngine Default user agent. --- diff --git a/src/dialogs/DomainSettingsDialog.cpp b/src/dialogs/DomainSettingsDialog.cpp index b7f2c93..b5be32a 100644 --- a/src/dialogs/DomainSettingsDialog.cpp +++ b/src/dialogs/DomainSettingsDialog.cpp @@ -391,7 +391,7 @@ void DomainSettingsDialog::populateUserAgentLabel(const QString &userAgentName) if (userAgentName == UserAgentHelper::SYSTEM_DEFAULT_TRANSLATED) { // Display the system default user agent name. - userAgentLabelPointer->setText(UserAgentHelper::getTranslatedUserAgentName(Settings::userAgent())); + userAgentLabelPointer->setText(UserAgentHelper::getTranslatedUserAgentNameFromDatabaseName(Settings::userAgent())); } else { @@ -508,7 +508,7 @@ void DomainSettingsDialog::userAgentChanged(const QString &updatedUserAgent) con { // Update the domains table model. domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT)), - UserAgentHelper::getDatabaseUserAgentName(updatedUserAgent)); + UserAgentHelper::getDatabaseUserAgentNameFromTranslatedName(updatedUserAgent)); // Populate the user agent label. populateUserAgentLabel(updatedUserAgent); diff --git a/src/helpers/UserAgentHelper.cpp b/src/helpers/UserAgentHelper.cpp index 1b02146..9a51f22 100644 --- a/src/helpers/UserAgentHelper.cpp +++ b/src/helpers/UserAgentHelper.cpp @@ -20,6 +20,7 @@ // Application headers. #include "Settings.h" #include "UserAgentHelper.h" +#include "views/BrowserView.h" // KDE Framework headers. #include @@ -27,6 +28,7 @@ // Define the public database constants. const QString UserAgentHelper::SYSTEM_DEFAULT_DATABASE = QStringLiteral("System Default"); const QString UserAgentHelper::PRIVACY_BROWSER_DATABASE = QStringLiteral("Privacy Browser"); +const QString UserAgentHelper::WEB_ENGINE_DEFAULT_DATABASE = QStringLiteral("WebEngine Default"); const QString UserAgentHelper::FIREFOX_LINUX_DATABASE = QStringLiteral("Firefox Linux"); const QString UserAgentHelper::CHROMIUM_LINUX_DATABASE = QStringLiteral("Chromium Linux"); const QString UserAgentHelper::FIREFOX_WINDOWS_DATABASE = QStringLiteral("Firefox Windows"); @@ -37,6 +39,7 @@ const QString UserAgentHelper::SAFARI_MACOS_DATABASE = QStringLiteral("Safari ma // Define the public translated constants. const QString UserAgentHelper::SYSTEM_DEFAULT_TRANSLATED = i18n("System default"); const QString UserAgentHelper::PRIVACY_BROWSER_TRANSLATED = i18n("Privacy Browser"); +const QString UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED = i18n("WebEngine default"); const QString UserAgentHelper::FIREFOX_LINUX_TRANSLATED = i18n("Firefox on Linux"); const QString UserAgentHelper::CHROMIUM_LINUX_TRANSLATED = i18n("Chromium on Linux"); const QString UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED = i18n("Firefox on Windows"); @@ -56,9 +59,40 @@ const QString UserAgentHelper::SAFARI_MACOS_USER_AGENT = QStringLiteral("Mozilla // The default constructor. UserAgentHelper::UserAgentHelper() {}; +QString UserAgentHelper::getDatabaseUserAgentNameFromTranslatedName(const QString &translatedUserAgentName) +{ + // Return the database user agent name. + if (translatedUserAgentName == SYSTEM_DEFAULT_TRANSLATED) return SYSTEM_DEFAULT_DATABASE; // System Default. + else if (translatedUserAgentName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_DATABASE; // Privacy Browser. + else if (translatedUserAgentName == WEB_ENGINE_DEFAULT_TRANSLATED) return WEB_ENGINE_DEFAULT_DATABASE; // WebEngine default. + else if (translatedUserAgentName == FIREFOX_LINUX_TRANSLATED) return FIREFOX_LINUX_DATABASE; // Firefox Linux. + else if (translatedUserAgentName == CHROMIUM_LINUX_TRANSLATED) return CHROMIUM_LINUX_DATABASE; // Chromium Linux. + else if (translatedUserAgentName == FIREFOX_WINDOWS_TRANSLATED) return FIREFOX_WINDOWS_DATABASE; // Firefox Windows. + else if (translatedUserAgentName == CHROME_WINDOWS_TRANSLATED) return CHROME_WINDOWS_DATABASE; // Chrome Windows. + else if (translatedUserAgentName == EDGE_WINDOWS_TRANSLATED) return EDGE_WINDOWS_DATABASE; // Edge Windows. + else if (translatedUserAgentName == SAFARI_MACOS_TRANSLATED) return SAFARI_MACOS_DATABASE; // Safari macOS. + else return translatedUserAgentName; // Return the custom user agent. +} + +int UserAgentHelper::getDomainSettingsUserAgentIndex(const QString &userAgentName) +{ + // Return the domain settings user agent index. + if (userAgentName == SYSTEM_DEFAULT_DATABASE) return 0; // System Default. + else if (userAgentName == PRIVACY_BROWSER_DATABASE) return 1; // Privacy Browser. + else if (userAgentName == WEB_ENGINE_DEFAULT_DATABASE) return 2; // WebEngine default. + else if (userAgentName == FIREFOX_LINUX_DATABASE) return 3; // Firefox Linux. + else if (userAgentName == CHROMIUM_LINUX_DATABASE) return 4; // Chromium Linux. + else if (userAgentName == FIREFOX_WINDOWS_DATABASE) return 5; // Firefox Windows. + else if (userAgentName == CHROME_WINDOWS_DATABASE) return 6; // Chrome Windows. + else if (userAgentName == EDGE_WINDOWS_DATABASE) return 7; // Edge Windows. + else if (userAgentName == SAFARI_MACOS_DATABASE) return 8; // Safari macOS. + else return -1; // Custom user agent. +} + QString UserAgentHelper::getUserAgentFromDatabaseName(const QString &userAgentDatabaseName) { if (userAgentDatabaseName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser. + else if (userAgentDatabaseName == WEB_ENGINE_DEFAULT_DATABASE) return BrowserView::webEngineDefaultUserAgent; // WebEngine default. else if (userAgentDatabaseName == FIREFOX_LINUX_DATABASE) return FIREFOX_LINUX_USER_AGENT; // Firefox Linux. else if (userAgentDatabaseName == CHROMIUM_LINUX_DATABASE) return CHROMIUM_LINUX_USER_AGENT; // Chromium Linux. else if (userAgentDatabaseName == FIREFOX_WINDOWS_DATABASE) return FIREFOX_WINDOWS_USER_AGENT; // Firefox Windows. @@ -71,6 +105,7 @@ QString UserAgentHelper::getUserAgentFromDatabaseName(const QString &userAgentDa QString UserAgentHelper::getUserAgentFromTranslatedName(const QString &userAgentTranslatedName) { if (userAgentTranslatedName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser. + else if (userAgentTranslatedName == WEB_ENGINE_DEFAULT_TRANSLATED) return BrowserView::webEngineDefaultUserAgent; // WebEngine default. else if (userAgentTranslatedName == FIREFOX_LINUX_TRANSLATED) return FIREFOX_LINUX_USER_AGENT; // Firefox on Linux. else if (userAgentTranslatedName == CHROMIUM_LINUX_TRANSLATED) return CHROMIUM_LINUX_USER_AGENT; // Chromium on Linux. else if (userAgentTranslatedName == FIREFOX_WINDOWS_TRANSLATED) return FIREFOX_WINDOWS_USER_AGENT; // Firefox on Windows. @@ -80,45 +115,18 @@ QString UserAgentHelper::getUserAgentFromTranslatedName(const QString &userAgent else return userAgentTranslatedName; // Return the custom user agent. } -QString UserAgentHelper::getDatabaseUserAgentName(const QString &translatedUserAgentName) -{ - // Return the database user agent name. - if (translatedUserAgentName == SYSTEM_DEFAULT_TRANSLATED) return SYSTEM_DEFAULT_DATABASE; // System Default. - else if (translatedUserAgentName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_DATABASE; // Privacy Browser. - else if (translatedUserAgentName == FIREFOX_LINUX_TRANSLATED) return FIREFOX_LINUX_DATABASE; // Firefox Linux. - else if (translatedUserAgentName == CHROMIUM_LINUX_TRANSLATED) return CHROMIUM_LINUX_DATABASE; // Chromium Linux. - else if (translatedUserAgentName == FIREFOX_WINDOWS_TRANSLATED) return FIREFOX_WINDOWS_DATABASE; // Firefox Windows. - else if (translatedUserAgentName == CHROME_WINDOWS_TRANSLATED) return CHROME_WINDOWS_DATABASE; // Chrome Windows. - else if (translatedUserAgentName == EDGE_WINDOWS_TRANSLATED) return EDGE_WINDOWS_DATABASE; // Edge Windows. - else if (translatedUserAgentName == SAFARI_MACOS_TRANSLATED) return SAFARI_MACOS_DATABASE; // Safari macOS. - else return translatedUserAgentName; // Return the custom user agent. -} - -int UserAgentHelper::getDomainSettingsUserAgentIndex(const QString &userAgentName) -{ - // Return the domain settings user agent index. - if (userAgentName == SYSTEM_DEFAULT_DATABASE) return 0; // System Default. - else if (userAgentName == PRIVACY_BROWSER_DATABASE) return 1; // Privacy Browser. - else if (userAgentName == FIREFOX_LINUX_DATABASE) return 2; // Firefox Linux. - else if (userAgentName == CHROMIUM_LINUX_DATABASE) return 3; // Chromium Linux. - else if (userAgentName == FIREFOX_WINDOWS_DATABASE) return 4; // Firefox Windows. - else if (userAgentName == CHROME_WINDOWS_DATABASE) return 5; // Chrome Windows. - else if (userAgentName == EDGE_WINDOWS_DATABASE) return 6; // Edge Windows. - else if (userAgentName == SAFARI_MACOS_DATABASE) return 7; // Safari macOS. - else return -1; // Custom user agent. -} - QString UserAgentHelper::getResultingDomainSettingsUserAgent(const QString &rawUserAgent) { - // Return the resulting user agent. + // Return the resulting user agent. If `System default` is selected, it looks up the default user agent from the settings. if (rawUserAgent == SYSTEM_DEFAULT_DATABASE) return getUserAgentFromDatabaseName(Settings::userAgent()); // Return the default user agent. else return getUserAgentFromDatabaseName(rawUserAgent); // Return the domain user agent. } -QString UserAgentHelper::getTranslatedUserAgentName(const QString &userAgentName) +QString UserAgentHelper::getTranslatedUserAgentNameFromDatabaseName(const QString &userAgentName) { // Return the translated user agent name. if (userAgentName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_TRANSLATED; // Privacy Browser. + else if (userAgentName == WEB_ENGINE_DEFAULT_DATABASE) return WEB_ENGINE_DEFAULT_TRANSLATED; // WebEngine default. else if (userAgentName == FIREFOX_LINUX_DATABASE) return FIREFOX_LINUX_TRANSLATED; // Firefox on Linux. else if (userAgentName == CHROMIUM_LINUX_DATABASE) return CHROMIUM_LINUX_TRANSLATED; // Chromium on Linux. else if (userAgentName == FIREFOX_WINDOWS_DATABASE) return FIREFOX_WINDOWS_TRANSLATED; // Firefox on Windows. diff --git a/src/helpers/UserAgentHelper.h b/src/helpers/UserAgentHelper.h index 1043d37..839a5ed 100644 --- a/src/helpers/UserAgentHelper.h +++ b/src/helpers/UserAgentHelper.h @@ -53,13 +53,15 @@ public: static const QString SAFARI_MACOS_USER_AGENT; static const QString SYSTEM_DEFAULT_DATABASE; static const QString SYSTEM_DEFAULT_TRANSLATED; + static const QString WEB_ENGINE_DEFAULT_DATABASE; + static const QString WEB_ENGINE_DEFAULT_TRANSLATED; // The public functions. + static QString getDatabaseUserAgentNameFromTranslatedName(const QString &translatedUserAgentName); + static int getDomainSettingsUserAgentIndex(const QString &userAgentName); static QString getUserAgentFromDatabaseName(const QString &userAgentDatabaseName); static QString getUserAgentFromTranslatedName(const QString &userAgentTranslatedName); - static QString getDatabaseUserAgentName(const QString &translatedUserAgentName); - static int getDomainSettingsUserAgentIndex(const QString &userAgentName); static QString getResultingDomainSettingsUserAgent(const QString &rawUserAgent); - static QString getTranslatedUserAgentName(const QString &userAgentName); + static QString getTranslatedUserAgentNameFromDatabaseName(const QString &userAgentName); }; #endif diff --git a/src/ui.rc/browser_ui.rc b/src/ui.rc/browser_ui.rc index dbbeb31..0e1d60b 100644 --- a/src/ui.rc/browser_ui.rc +++ b/src/ui.rc/browser_ui.rc @@ -32,6 +32,7 @@ On-The-Fly Settings User Agent + diff --git a/src/ui/DomainSettingsDialog.ui b/src/ui/DomainSettingsDialog.ui index 0b2f56f..c39047c 100644 --- a/src/ui/DomainSettingsDialog.ui +++ b/src/ui/DomainSettingsDialog.ui @@ -225,6 +225,12 @@ + + + WebEngine default + + + Firefox on Linux diff --git a/src/ui/SettingsPrivacy.ui b/src/ui/SettingsPrivacy.ui index a1445b6..6fa7c5b 100644 --- a/src/ui/SettingsPrivacy.ui +++ b/src/ui/SettingsPrivacy.ui @@ -82,6 +82,12 @@ + + + WebEngine Default + + + Firefox Linux diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index fb2473a..2e29ea0 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -32,6 +32,9 @@ #include #include +// Initialize the public static variables. +QString BrowserView::webEngineDefaultUserAgent = QStringLiteral(""); + BrowserView::BrowserView(QWidget *parent) : QWidget(parent) { // Instantiate the browser view UI. @@ -56,6 +59,9 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) webEngineHistoryPointer = webEnginePagePointer->history(); webEngineSettingsPointer = webEngineViewPointer->settings(); + // Store a copy of the WebEngine default user agent. + webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent(); + // Update the URL line edit when the URL changes. connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl))); diff --git a/src/views/BrowserView.h b/src/views/BrowserView.h index 3c3bd7d..d61ceae 100644 --- a/src/views/BrowserView.h +++ b/src/views/BrowserView.h @@ -44,6 +44,9 @@ public: void toggleJavaScript() const; void toggleLocalStorage() const; + // The public static variables. + static QString webEngineDefaultUserAgent; + signals: // The signals. void hideProgressBar() const; diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 4461419..a29b503 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -56,6 +56,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Add the custom actions. userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser")); + userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default")); userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux")); userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux")); userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows")); @@ -81,6 +82,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Add the actions to the groups. userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer); + userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer); userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer); userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer); userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer); @@ -98,6 +100,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Set some actions to be checkable. userAgentPrivacyBrowserActionPointer->setCheckable(true); + userAgentWebEngineDefaultActionPointer->setCheckable(true); userAgentFirefoxLinuxActionPointer->setCheckable(true); userAgentChromiumLinuxActionPointer->setCheckable(true); userAgentFirefoxWindowsActionPointer->setCheckable(true); @@ -115,6 +118,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Set the non-mutable action text. userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED); + userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED); userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED); userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED); userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED); @@ -133,6 +137,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() // Set the action icons. userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode")); + userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties"))); userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr"))); userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium"))); userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr"))); @@ -552,6 +557,7 @@ void BrowserWindow::updateUserAgentActions(const QString &userAgent) const // Check the indicated on-the-fly user agent. if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser. + else if (userAgent == BrowserView::webEngineDefaultUserAgent) userAgentWebEngineDefaultActionPointer->setChecked(true); // WebEngine default. else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true); // Firefox Linux. else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true); // Chromium Linux. else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true); // Firefox Windows. diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index e4eee69..5be55f4 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -91,6 +91,7 @@ private: QAction *searchEngineCustomActionPointer; QLabel *userAgentLabelPointer; QAction *userAgentPrivacyBrowserActionPointer; + QAction *userAgentWebEngineDefaultActionPointer; QAction *userAgentFirefoxLinuxActionPointer; QAction *userAgentChromiumLinuxActionPointer; QAction *userAgentFirefoxWindowsActionPointer;