From fe9f5cdee9e6e16eac1858f7f661516c24f71fa5 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 23 Mar 2022 16:42:02 -0700 Subject: [PATCH] Add User Agent to Domain Settings. --- src/CMakeLists.txt | 4 +- src/DomainSettingsDialog.ui | 92 ++++++++++++++++- src/SettingsGeneral.ui | 10 +- src/SettingsPrivacy.ui | 10 +- src/dialogs/DomainSettingsDialog.cpp | 107 +++++++++++++++++++- src/dialogs/DomainSettingsDialog.h | 16 ++- src/helpers/DomainsDatabaseHelper.cpp | 16 ++- src/helpers/DomainsDatabaseHelper.h | 1 + src/helpers/UserAgentHelper.cpp | 136 +++++++++++++++++++------- src/helpers/UserAgentHelper.h | 32 +++++- src/views/BrowserView.cpp | 22 ++--- src/windows/BrowserWindow.cpp | 59 +++++------ src/windows/BrowserWindow.h | 2 +- 13 files changed, 400 insertions(+), 107 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c679f99..55b3e3f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,8 +39,8 @@ kconfig_add_kcfg_files(privacy-browser Settings.kcfgc) ki18n_wrap_ui(privacy-browser BrowserView.ui DomainSettingsDialog.ui - SettingsPrivacy.ui SettingsGeneral.ui + SettingsPrivacy.ui ) # Link the following libraries. @@ -64,9 +64,9 @@ target_link_libraries(privacy-browser # Add the subdirectories. add_subdirectory(dialogs) add_subdirectory(helpers) +add_subdirectory(ui.rc) add_subdirectory(views) add_subdirectory(windows) -add_subdirectory(ui.rc) # Install Privacy Browser using the default KDE arguments. install(TARGETS privacy-browser ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/src/DomainSettingsDialog.ui b/src/DomainSettingsDialog.ui index e6f9aca..a2abaf7 100644 --- a/src/DomainSettingsDialog.ui +++ b/src/DomainSettingsDialog.ui @@ -26,8 +26,8 @@ 0 0 - 700 - 500 + 1000 + 1500 @@ -139,6 +139,94 @@ + + + + + Qt::RichText + + + + + + + + + User Agent + + + + + + + + + 0 + 0 + + + + + true + + + + + System default + + + + + + Privacy Browser + + + + + + Firefox on Linux + + + + + + Chromium on Linux + + + + + + Firefox on Windows + + + + + + Chrome on Windows + + + + + + Edge on Windows + + + + + + Safari on macOS + + + + + + + + + Qt::RichText + + + diff --git a/src/SettingsGeneral.ui b/src/SettingsGeneral.ui index 9e33b8a..65b693e 100644 --- a/src/SettingsGeneral.ui +++ b/src/SettingsGeneral.ui @@ -52,11 +52,11 @@ - - - 0 - 500 - + + + 0 + 0 + diff --git a/src/SettingsPrivacy.ui b/src/SettingsPrivacy.ui index 8c701d8..74c7a0f 100644 --- a/src/SettingsPrivacy.ui +++ b/src/SettingsPrivacy.ui @@ -48,11 +48,11 @@ - - - 0 - 500 - + + + 0 + 0 + diff --git a/src/dialogs/DomainSettingsDialog.cpp b/src/dialogs/DomainSettingsDialog.cpp index 0ebfb14..e44af92 100644 --- a/src/dialogs/DomainSettingsDialog.cpp +++ b/src/dialogs/DomainSettingsDialog.cpp @@ -19,8 +19,10 @@ // Application headers. #include "DomainSettingsDialog.h" +#include "Settings.h" #include "ui_DomainSettingsDialog.h" #include "helpers/DomainsDatabaseHelper.h" +#include "helpers/UserAgentHelper.h" // Qt toolkit headers. #include @@ -40,6 +42,9 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent) domainSettingsWidgetPointer = domainSettingsDialogUi.domainSettingsWidget; domainNameLineEditPointer = domainSettingsDialogUi.domainNameLineEdit; javaScriptComboBoxPointer = domainSettingsDialogUi.javaScriptComboBox; + javaScriptLabelPointer = domainSettingsDialogUi.javaScriptLabel; + userAgentComboBoxPointer = domainSettingsDialogUi.userAgentComboBox; + userAgentLabelPointer = domainSettingsDialogUi.userAgentLabel; QPushButton *addDomainButtonPointer = domainSettingsDialogUi.addDomainButton; deleteDomainButtonPointer = domainSettingsDialogUi.deleteDomainButton; QDialogButtonBox *dialogButtonBoxPointer = domainSettingsDialogUi.dialogButtonBox; @@ -82,6 +87,7 @@ DomainSettingsDialog::DomainSettingsDialog(QWidget *parent) : QDialog(parent) // Connect the domain settings. connect(domainNameLineEditPointer, SIGNAL(textEdited(QString)), this, SLOT(domainNameChanged(QString))); connect(javaScriptComboBoxPointer, SIGNAL(currentIndexChanged(int)), this, SLOT(javaScriptChanged(int))); + connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(QString)), this, SLOT(userAgentChanged(QString))); // Connect the buttons. connect(addDomainButtonPointer, SIGNAL(released()), this, SLOT(showAddMessageBox())); @@ -115,6 +121,9 @@ void DomainSettingsDialog::apply() const // Update the UI. updateUi(); + + // Emit the domain settings updated signal. + emit domainSettingsUpdated(); } void DomainSettingsDialog::cancel() @@ -144,6 +153,22 @@ void DomainSettingsDialog::domainSelected(QModelIndex modelIndex) const // Populate the JavaScript combo box. javaScriptComboBoxPointer->setCurrentIndex(modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT)).data().toInt()); + // Get the user agent string. + QString userAgent = modelIndex.siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT)).data().toString(); + + // Get the user agent index. + int userAgentIndex = UserAgentHelper::getDomainSettingsUserAgentIndex(userAgent); + + // Set the user agent combo box index. + userAgentComboBoxPointer->setCurrentIndex(userAgentIndex); + + // Set the custom user agent if specified. + if (userAgentIndex == -1) userAgentComboBoxPointer->setCurrentText(userAgent); + + // Populate the labels. + populateJavaScriptLabel(); + populateUserAgentLabel(userAgentComboBoxPointer->currentText()); + // Update the UI. updateUi(); } @@ -154,6 +179,9 @@ void DomainSettingsDialog::javaScriptChanged(int newIndex) const domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT)), newIndex); + // Populate the JavaScript label. + populateJavaScriptLabel(); + // Update the UI. updateUi(); } @@ -164,17 +192,73 @@ void DomainSettingsDialog::ok() // Submit all pending changes. domainsTableModelPointer->submitAll(); + // Emit the domain settings updated signal. + domainSettingsUpdated(); + // Close the dialog. accept(); } +void DomainSettingsDialog::populateJavaScriptLabel() const +{ + // Populate the label according to the currently selected index. + switch (javaScriptComboBoxPointer->currentIndex()) + { + case (DomainsDatabaseHelper::SYSTEM_DEFAULT): + { + // Set the text according to the system default. + if (Settings::javaScript()) + { + javaScriptLabelPointer->setText(i18nc("Domains settings labels", "JavaScript enabled")); + } + else + { + javaScriptLabelPointer->setText(i18nc("Domain settings labels", "JavaScript disabled")); + } + + break; + } + + case (DomainsDatabaseHelper::DISABLED): + { + // Set the label text in bold. + javaScriptLabelPointer->setText(i18nc("Domain settings labels. The tags should be retained.", "JavaScript disabled")); + + break; + } + + case (DomainsDatabaseHelper::ENABLED): + { + // Set the label text in bold. + javaScriptLabelPointer->setText(i18nc("Domains settings labels. The tags should be retained.", "JavaScript enabled")); + + break; + } + } +} + +void DomainSettingsDialog::populateUserAgentLabel(const QString &userAgentName) const +{ + // Populate the label according to the type. + if (userAgentName == UserAgentHelper::SYSTEM_DEFAULT_TRANSLATED) + { + // Display the system default user agent name. + userAgentLabelPointer->setText(UserAgentHelper::getTranslatedUserAgentName(Settings::userAgent())); + } + else + { + // Display the user agent name in bold. + userAgentLabelPointer->setText("" + userAgentName + ""); + } +} + void DomainSettingsDialog::reset() const { // Cancel all pending changes. domainsTableModelPointer->revertAll(); - // Repopulate the domain name line edit. - domainNameLineEditPointer->setText(domainsListViewPointer->currentIndex().data().toString()); + // Repopulate the domain settings. + domainSelected(domainsListViewPointer->currentIndex()); // Update the UI. updateUi(); @@ -201,9 +285,12 @@ void DomainSettingsDialog::showAddMessageBox() // Add the new domain name. newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::DOMAIN_NAME), newDomainName); - // Set the default value of `0` for the other columns. + // Set the default value of `0` for JavaScript. newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::JAVASCRIPT), 0); + // Set the default value for the user agent. + newDomainRecord.setValue(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT), UserAgentHelper::SYSTEM_DEFAULT_DATABASE); + // Insert the new domain. `-1` appends it to the end. domainsTableModelPointer->insertRecord(-1, newDomainRecord); @@ -301,3 +388,17 @@ void DomainSettingsDialog::updateUi() const domainSettingsWidgetPointer->setVisible(domainsTableModelPointer->rowCount() > 0); } +void DomainSettingsDialog::userAgentChanged(const QString updatedUserAgent) const +{ + // Update the domains table model. + domainsTableModelPointer->setData(domainsListViewPointer->selectionModel()->currentIndex().siblingAtColumn(domainsTableModelPointer->fieldIndex(DomainsDatabaseHelper::USER_AGENT)), + UserAgentHelper::getDatabaseUserAgentName(updatedUserAgent)); + + // Populate the user agent label. + populateUserAgentLabel(updatedUserAgent); + + // Update the UI. + updateUi(); +} + + diff --git a/src/dialogs/DomainSettingsDialog.h b/src/dialogs/DomainSettingsDialog.h index 3ae50b5..6acfcb5 100644 --- a/src/dialogs/DomainSettingsDialog.h +++ b/src/dialogs/DomainSettingsDialog.h @@ -25,6 +25,7 @@ // Qt toolkit headers. #include +#include #include class DomainSettingsDialog : public QDialog @@ -36,6 +37,9 @@ public: // The primary constructor. explicit DomainSettingsDialog(QWidget *parent = nullptr); +signals: + void domainSettingsUpdated() const; + private Q_SLOTS: // The private slots. void apply() const; @@ -47,11 +51,9 @@ private Q_SLOTS: void reset() const; void showAddMessageBox(); void showDeleteMessageBox() const; + void userAgentChanged(const QString updatedUserAgent) const; private: - // The private functions. - void updateUi() const; - // The private variables. QPushButton *applyButtonPointer; QPushButton *deleteDomainButtonPointer; @@ -60,6 +62,14 @@ private: QWidget *domainSettingsWidgetPointer; QSqlTableModel *domainsTableModelPointer; QComboBox *javaScriptComboBoxPointer; + QLabel *javaScriptLabelPointer; QPushButton *resetButtonPointer; + QComboBox *userAgentComboBoxPointer; + QLabel *userAgentLabelPointer; + + // The private functions. + void populateJavaScriptLabel() const; + void populateUserAgentLabel(const QString &userAgentName) const; + void updateUi() const; }; #endif diff --git a/src/helpers/DomainsDatabaseHelper.cpp b/src/helpers/DomainsDatabaseHelper.cpp index 5de297d..48b28a3 100644 --- a/src/helpers/DomainsDatabaseHelper.cpp +++ b/src/helpers/DomainsDatabaseHelper.cpp @@ -19,18 +19,20 @@ // Application headers. #include "DomainsDatabaseHelper.h" +#include "UserAgentHelper.h" // Define the public static domain constants. const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database"; const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains"; // Define the private static schema constants. -const int DomainsDatabaseHelper::SCHEMA_VERSION = 1; +const int DomainsDatabaseHelper::SCHEMA_VERSION = 2; // Define the public static database field names. const QString DomainsDatabaseHelper::_ID = "_id"; const QString DomainsDatabaseHelper::DOMAIN_NAME = "domain_name"; const QString DomainsDatabaseHelper::JAVASCRIPT = "javascript"; +const QString DomainsDatabaseHelper::USER_AGENT = "user_agent"; // The default constructor. DomainsDatabaseHelper::DomainsDatabaseHelper() {} @@ -72,6 +74,15 @@ void DomainsDatabaseHelper::addDatabase() // Set the default value. domainsDatabase.exec("UPDATE " + DOMAINS_TABLE + " SET " + JAVASCRIPT + " = 0" ); + + [[fallthrough]]; + } + + // Upgrade from schema version 1. + case 1: + { + // Add the User Agent column. + domainsDatabase.exec("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "'"); } } @@ -88,7 +99,8 @@ void DomainsDatabaseHelper::addDatabase() createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "(" + _ID + " INTEGER PRIMARY KEY, " + DOMAIN_NAME + " TEXT, " + - JAVASCRIPT + " INTEGER DEFAULT 0)" + JAVASCRIPT + " INTEGER DEFAULT 0," + + USER_AGENT + " TEXT DEFAULT '" + UserAgentHelper::SYSTEM_DEFAULT_DATABASE + "')" ); // Execute the query. diff --git a/src/helpers/DomainsDatabaseHelper.h b/src/helpers/DomainsDatabaseHelper.h index b02a4cf..4badf41 100644 --- a/src/helpers/DomainsDatabaseHelper.h +++ b/src/helpers/DomainsDatabaseHelper.h @@ -44,6 +44,7 @@ public: static const QString DOMAIN_NAME; static const QString DOMAINS_TABLE; static const QString JAVASCRIPT; + static const QString USER_AGENT; private: // The private static constants. diff --git a/src/helpers/UserAgentHelper.cpp b/src/helpers/UserAgentHelper.cpp index 4b1fd85..1b02146 100644 --- a/src/helpers/UserAgentHelper.cpp +++ b/src/helpers/UserAgentHelper.cpp @@ -18,44 +18,112 @@ */ // Application headers. +#include "Settings.h" #include "UserAgentHelper.h" +// KDE Framework headers. +#include + +// 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::FIREFOX_LINUX_DATABASE = QStringLiteral("Firefox Linux"); +const QString UserAgentHelper::CHROMIUM_LINUX_DATABASE = QStringLiteral("Chromium Linux"); +const QString UserAgentHelper::FIREFOX_WINDOWS_DATABASE = QStringLiteral("Firefox Windows"); +const QString UserAgentHelper::CHROME_WINDOWS_DATABASE = QStringLiteral("Chrome Windows"); +const QString UserAgentHelper::EDGE_WINDOWS_DATABASE = QStringLiteral("Edge Windows"); +const QString UserAgentHelper::SAFARI_MACOS_DATABASE = QStringLiteral("Safari macOS"); + +// 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::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"); +const QString UserAgentHelper::CHROME_WINDOWS_TRANSLATED = i18n("Chrome on Windows"); +const QString UserAgentHelper::EDGE_WINDOWS_TRANSLATED = i18n("Edge on Windows"); +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/98.0.4758.102 Safari/537.36"); +const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"); +const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 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/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56"); +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.1 Safari/605.1.15"); + // The default constructor. UserAgentHelper::UserAgentHelper() {}; -QString UserAgentHelper::getUserAgent(const QString &userAgentName) +QString UserAgentHelper::getUserAgentFromDatabaseName(const QString &userAgentDatabaseName) +{ + if (userAgentDatabaseName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser. + 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. + else if (userAgentDatabaseName == CHROME_WINDOWS_DATABASE) return CHROME_WINDOWS_USER_AGENT; // Chrome Windows. + else if (userAgentDatabaseName == EDGE_WINDOWS_DATABASE) return EDGE_WINDOWS_USER_AGENT; // Edge Windows. + else if (userAgentDatabaseName == SAFARI_MACOS_DATABASE) return SAFARI_MACOS_USER_AGENT; // Safari macOS. + else return userAgentDatabaseName; // Return the custom user agent. +} + +QString UserAgentHelper::getUserAgentFromTranslatedName(const QString &userAgentTranslatedName) +{ + if (userAgentTranslatedName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser. + 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. + else if (userAgentTranslatedName == CHROME_WINDOWS_TRANSLATED) return CHROME_WINDOWS_USER_AGENT; // Chrome on Windows. + else if (userAgentTranslatedName == EDGE_WINDOWS_TRANSLATED) return EDGE_WINDOWS_USER_AGENT; // Edge on Windows. + else if (userAgentTranslatedName == SAFARI_MACOS_TRANSLATED) return SAFARI_MACOS_USER_AGENT; // Safari on macOS. + 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. + 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) { - if (userAgentName == "Privacy Browser") // Privacy Browser. - { - return "PrivacyBrowser/1.0"; - } - else if (userAgentName == "Firefox Linux") // Firefox Linux. - { - return "Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0"; - } - else if (userAgentName == "Chromium Linux") // Chromium Linux. - { - return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"; - } - else if (userAgentName == "Firefox Windows") // Firefox Windows. - { - return "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"; - } - else if (userAgentName == "Chrome Windows") // Chrome Windows. - { - return "Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"; - } - else if (userAgentName == "Edge Windows") // Edge Windows. - { - return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56"; - } - else if (userAgentName == "Safari macOS") // Safari macOS. - { - return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"; - } - else - { - // Return the custom user agent. - return userAgentName; - } + // Return the translated user agent name. + if (userAgentName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_TRANSLATED; // Privacy Browser. + 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. + else if (userAgentName == CHROME_WINDOWS_DATABASE) return CHROME_WINDOWS_TRANSLATED; // Chrome on Windows. + else if (userAgentName == EDGE_WINDOWS_DATABASE) return EDGE_WINDOWS_TRANSLATED; // Edge on Windows. + else if (userAgentName == SAFARI_MACOS_DATABASE) return SAFARI_MACOS_TRANSLATED; // Safari on macOS. + else return userAgentName; // Return the custom user agent. } diff --git a/src/helpers/UserAgentHelper.h b/src/helpers/UserAgentHelper.h index f936968..1043d37 100644 --- a/src/helpers/UserAgentHelper.h +++ b/src/helpers/UserAgentHelper.h @@ -29,7 +29,37 @@ public: // The default constructor. UserAgentHelper(); + // The public constants. + static const QString CHROMIUM_LINUX_DATABASE; + static const QString CHROMIUM_LINUX_TRANSLATED; + static const QString CHROMIUM_LINUX_USER_AGENT; + static const QString CHROME_WINDOWS_DATABASE; + static const QString CHROME_WINDOWS_TRANSLATED; + static const QString CHROME_WINDOWS_USER_AGENT; + static const QString EDGE_WINDOWS_DATABASE; + static const QString EDGE_WINDOWS_TRANSLATED; + static const QString EDGE_WINDOWS_USER_AGENT; + static const QString FIREFOX_LINUX_DATABASE; + static const QString FIREFOX_LINUX_TRANSLATED; + static const QString FIREFOX_LINUX_USER_AGENT; + static const QString FIREFOX_WINDOWS_DATABASE; + static const QString FIREFOX_WINDOWS_TRANSLATED; + static const QString FIREFOX_WINDOWS_USER_AGENT; + static const QString PRIVACY_BROWSER_DATABASE; + static const QString PRIVACY_BROWSER_TRANSLATED; + static const QString PRIVACY_BROWSER_USER_AGENT; + static const QString SAFARI_MACOS_DATABASE; + static const QString SAFARI_MACOS_TRANSLATED; + static const QString SAFARI_MACOS_USER_AGENT; + static const QString SYSTEM_DEFAULT_DATABASE; + static const QString SYSTEM_DEFAULT_TRANSLATED; + // The public functions. - static QString getUserAgent(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); }; #endif diff --git a/src/views/BrowserView.cpp b/src/views/BrowserView.cpp index 3a116e2..c6a984c 100644 --- a/src/views/BrowserView.cpp +++ b/src/views/BrowserView.cpp @@ -60,7 +60,7 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent) // Update the webengine view from the URL line edit. connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString))); - // Update the URL line edit form the webengine view. + // Update the URL line edit from the webengine view. connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface())); connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface())); @@ -139,8 +139,6 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload // Check if the hostname has domain settings. if (domainQuery.isValid()) // The hostname has domain settings. { - - // Get the domain record. QSqlRecord domainRecord = domainQuery.record(); @@ -172,8 +170,8 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } } - // Apply the user agent. - webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent())); + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString())); // Set the zoom factor. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); @@ -186,8 +184,8 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload // Set the JavaScript status. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript()); - // Apply the user agent. - webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent())); + // Set the user agent. + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent())); // Set the zoom factor. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); @@ -207,7 +205,7 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload } // Emit the on-the-fly menu update signals. - emit userAgentUpdated(Settings::userAgent()); + emit userAgentUpdated(webEngineProfilePointer->httpUserAgent()); emit zoomFactorUpdated(Settings::zoomFactor()); // Reload the website if requested. @@ -238,7 +236,7 @@ void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const userAgentName.remove('&'); // Apply the user agent. - webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(userAgentName)); + webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName)); // Reload the website. webEngineViewPointer->reload(); @@ -310,14 +308,14 @@ void BrowserView::openDomainSettings() const // Set the dialog window title. domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings")); - // Resize the dialog window. - domainSettingsDialogPointer->resize(1500, 1000); - // Set the modality. domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);; // Show the dialog. domainSettingsDialogPointer->show(); + + // Reload the tabs when domain settings are updated. + connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), this, SLOT(applyDomainSettingsAndReload())); } void BrowserView::pageLinkHovered(const QString &linkUrl) const diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 1ab6cfc..3d6eb61 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -105,13 +105,13 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() searchEngineCustomActionPointer->setCheckable(true); // Set the non-mutable action text. - userAgentPrivacyBrowserActionPointer->setText(i18nc("@action", "Privacy Browser")); - userAgentFirefoxLinuxActionPointer->setText(i18nc("@action", "Firefox Linux")); - userAgentChromiumLinuxActionPointer->setText(i18nc("@action", "Chromium Linux")); - userAgentFirefoxWindowsActionPointer->setText(i18nc("@action", "Firefox Windows")); - userAgentChromeWindowsActionPointer->setText(i18nc("@action", "Chrome Windows")); - userAgentEdgeWindowsActionPointer->setText(i18nc("@action", "Edge Windows")); - userAgentSafariMacosActionPointer->setText(i18nc("@action", "Safari macOS")); + userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED); + userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED); + userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED); + userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED); + userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED); + userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED); + userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED); searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek")); searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles")); searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer")); @@ -231,6 +231,12 @@ void BrowserWindow::settingsConfigure() // Make it so. configDialogPointer->show(); + // TODO. KConfigDialog does not respect expanding size policies. + configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + configDialogPointer->adjustSize(); + // Expand the config dialog. configDialogPointer->resize(1000, 500); @@ -304,34 +310,13 @@ void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const bool customUserAgent = false; // Check the indicated on-the-fly user agent. - if (userAgent == "Privacy Browser") // Privacy Browser. - { - userAgentPrivacyBrowserActionPointer->setChecked(true); - } - else if (userAgent == "Firefox Linux") // Firefox Linux. - { - userAgentFirefoxLinuxActionPointer->setChecked(true); - } - else if (userAgent == "Chromium Linux") // Chromium Linux. - { - userAgentChromiumLinuxActionPointer->setChecked(true); - } - else if (userAgent == "Firefox Windows") // Firefox Windows. - { - userAgentFirefoxWindowsActionPointer->setChecked(true); - } - else if (userAgent == "Chrome Windows") // Chrome Windows. - { - userAgentChromeWindowsActionPointer->setChecked(true); - } - else if (userAgent == "Edge Windows") // Edge Windows. - { - userAgentEdgeWindowsActionPointer->setChecked(true); - } - else if (userAgent == "Safari macOS") // Safari macOS. - { - userAgentSafariMacosActionPointer->setChecked(true); - } + if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser. + 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. + else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true); // Chrome Windows. + else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true); // Edge Windows. + else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true); // Safara macOS. else // Custom user agent. { // Check the user agent. @@ -382,8 +367,8 @@ void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const statusBar()->showMessage(statusBarMessage); } -void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const +void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const { // Update the user agent label. - userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName)); + userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName)); } diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index 7b64a96..f4dc071 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -49,7 +49,7 @@ private Q_SLOTS: void updateOnTheFlyZoomFactor(const double &zoomFactor); void updateSearchEngineLabel(const QString &searchEngineString) const; void updateStatusBar(const QString &statusBarMessage) const; - void updateUserAgentLabel(const QString &userAgentName) const; + void updateUserAgentLabel(const QString &userAgentDatabaseName) const; private: // The private variables. -- 2.45.2