From 05800bafb963a10f291d2a9604dc90cd7ac37c7b Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Fri, 4 Feb 2022 21:53:28 -0700 Subject: [PATCH] Add user agent controls. --- src/{browserwindow.cpp => BrowserWindow.cpp} | 32 ++++- src/{browserwindow.h => BrowserWindow.h} | 9 +- src/CMakeLists.txt | 15 ++- src/{mainview.cpp => MainView.cpp} | 42 ++++-- src/{mainview.h => MainView.h} | 10 +- src/{mainview.ui => MainView.ui} | 0 ...seeventfilter.cpp => MouseEventFilter.cpp} | 2 +- ...{mouseeventfilter.h => MouseEventFilter.h} | 0 src/{settings.kcfg => Settings.kcfg} | 4 + src/{settings.kcfgc => Settings.kcfgc} | 2 +- ...{settingsgeneral.ui => SettingsGeneral.ui} | 0 src/SettingsPrivacy.ui | 120 ++++++++++++++++++ src/UserAgentHelper.cpp | 61 +++++++++ src/UserAgentHelper.h | 35 +++++ src/main.cpp | 2 +- src/settingsprivacy.ui | 54 -------- 16 files changed, 307 insertions(+), 81 deletions(-) rename src/{browserwindow.cpp => BrowserWindow.cpp} (77%) rename src/{browserwindow.h => BrowserWindow.h} (85%) rename src/{mainview.cpp => MainView.cpp} (86%) rename src/{mainview.h => MainView.h} (87%) rename src/{mainview.ui => MainView.ui} (100%) rename src/{mouseeventfilter.cpp => MouseEventFilter.cpp} (98%) rename src/{mouseeventfilter.h => MouseEventFilter.h} (100%) rename src/{settings.kcfg => Settings.kcfg} (93%) rename src/{settings.kcfgc => Settings.kcfgc} (98%) rename src/{settingsgeneral.ui => SettingsGeneral.ui} (100%) create mode 100644 src/SettingsPrivacy.ui create mode 100644 src/UserAgentHelper.cpp create mode 100644 src/UserAgentHelper.h delete mode 100644 src/settingsprivacy.ui diff --git a/src/browserwindow.cpp b/src/BrowserWindow.cpp similarity index 77% rename from src/browserwindow.cpp rename to src/BrowserWindow.cpp index baf9fe9..546a951 100644 --- a/src/browserwindow.cpp +++ b/src/BrowserWindow.cpp @@ -18,10 +18,10 @@ */ // Application headers. -#include "browserwindow.h" -#include "settings.h" -#include "ui_settingsprivacy.h" -#include "ui_settingsgeneral.h" +#include "BrowserWindow.h" +#include "Settings.h" +#include "ui_SettingsPrivacy.h" +#include "ui_SettingsGeneral.h" // KDE Frameworks headers. #include @@ -76,6 +76,19 @@ void BrowserWindow::settingsConfigure() privacySettingsUi.setupUi(privacySettingsWidgetPointer); generalSettingsUi.setupUi(generalSettingsWidgetPointer); + // Get handles for the user agent widgets. + QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent; + userAgentLabelPointer = privacySettingsUi.userAgentLabel; + + // Instantiate the user agent helper. + userAgentHelperPointer = new UserAgentHelper(); + + // Display the initial user agent. + updateUserAgentLabel(userAgentComboBoxPointer->currentText()); + + // Update the user agent when the combo box changes. + connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(QString)), this, SLOT(updateUserAgentLabel(QString))); + // Instantiate a settings config dialog from the settings.kcfg file. KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self()); @@ -89,9 +102,12 @@ void BrowserWindow::settingsConfigure() // Make it so. configDialogPointer->show(); + // Expand the config dialog. + configDialogPointer->resize(1000, 500); + // Apply the settings when they are updated. connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings())); - connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettings())); + connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload())); } } @@ -100,3 +116,9 @@ void BrowserWindow::updateStatusBar(const QString &statusBarMessage) // Display the status bar message. statusBar()->showMessage(statusBarMessage); } + +void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) +{ + // Update the user agent label. + userAgentLabelPointer->setText(userAgentHelperPointer->getUserAgent(userAgentName)); +} diff --git a/src/browserwindow.h b/src/BrowserWindow.h similarity index 85% rename from src/browserwindow.h rename to src/BrowserWindow.h index fc1e53d..31ac510 100644 --- a/src/browserwindow.h +++ b/src/BrowserWindow.h @@ -21,7 +21,11 @@ #define BROWSERWINDOW_H // Application headers. -#include "mainview.h" +#include "MainView.h" +#include "UserAgentHelper.h" + +// Qt framework headers. +#include // KDE Frameworks headers. #include @@ -40,9 +44,12 @@ private Q_SLOTS: void fileNew(); void settingsConfigure(); void updateStatusBar(const QString &statusBarMessage); + void updateUserAgentLabel(const QString &userAgentName); private: // Define the private variables. MainView *mainViewPointer; + QLabel *userAgentLabelPointer; + UserAgentHelper *userAgentHelperPointer; }; #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 425f0e7..8f184ad 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,10 +21,11 @@ add_executable(privacy-browser resources.qrc) # List the sources to include in the executable. target_sources(privacy-browser PRIVATE - browserwindow.cpp + BrowserWindow.cpp main.cpp - mainview.cpp - mouseeventfilter.cpp + MainView.cpp + MouseEventFilter.cpp + UserAgentHelper.cpp ) # Add the Qt logging category. This will create the `debug.h` header file. @@ -35,13 +36,13 @@ ecm_qt_declare_logging_category(privacy-browser ) # Include the KConfig controller file. -kconfig_add_kcfg_files(privacy-browser settings.kcfgc) +kconfig_add_kcfg_files(privacy-browser Settings.kcfgc) # Use KDE Frameworks to handle internationalization of the following UI files. ki18n_wrap_ui(privacy-browser - mainview.ui - settingsprivacy.ui - settingsgeneral.ui + MainView.ui + SettingsPrivacy.ui + SettingsGeneral.ui ) # Link the following libraries. diff --git a/src/mainview.cpp b/src/MainView.cpp similarity index 86% rename from src/mainview.cpp rename to src/MainView.cpp index 416014b..cf84ecc 100644 --- a/src/mainview.cpp +++ b/src/MainView.cpp @@ -18,11 +18,14 @@ */ // Application headers -#include "browserwindow.h" -#include "mainview.h" -#include "mouseeventfilter.h" -#include "settings.h" -#include "ui_mainview.h" +#include "BrowserWindow.h" +#include "MainView.h" +#include "MouseEventFilter.h" +#include "Settings.h" +#include "ui_MainView.h" + +// Qt framework headers. +#include MainView::MainView(QWidget *parent) : QWidget(parent) { @@ -43,6 +46,7 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Get handles for the aspects of the WebEngine. QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page(); webEngineHistoryPointer = webEnginePagePointer->history(); + webEngineProfilePointer = webEnginePagePointer->profile(); webEngineSettingsPointer = webEngineViewPointer->settings(); // Update the webengine view from the URL line edit. @@ -68,6 +72,12 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Listen for hovered link URLs. connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString))); + // Disable the cache. + webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache); + + // Instantiate the user agent helper. + userAgentHelperPointer = new UserAgentHelper(); + // Don't allow JavaScript to open windows. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); @@ -77,8 +87,8 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Apply the application settings. applyApplicationSettings(); - // Apply the domain settings. - applyDomainSettings(); + // Apply the domain settings. `false` does not reload the website. + applyDomainSettings(false); // Set the focus on the WebEngine view. webEngineViewPointer->setFocus(); @@ -104,7 +114,13 @@ void MainView::applyApplicationSettings() // TODO. } -void MainView::applyDomainSettings() +void MainView::applyDomainSettingsAndReload() +{ + // Apply the domain setings. `true` reloads the website. + applyDomainSettings(true); +} + +void MainView::applyDomainSettings(bool reloadWebsite) { // Set the JavaScript status. webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript()); @@ -119,8 +135,14 @@ void MainView::applyDomainSettings() javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); } - // Reload the website. - webEngineViewPointer->reload(); + // Apply the user agent. + webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent())); + + // Reload the website if requested. + if (reloadWebsite) + { + webEngineViewPointer->reload(); + } } void MainView::goHome() diff --git a/src/mainview.h b/src/MainView.h similarity index 87% rename from src/mainview.h rename to src/MainView.h index 018594e..f08975e 100644 --- a/src/mainview.h +++ b/src/MainView.h @@ -20,6 +20,9 @@ #ifndef MAINVIEW_H #define MAINVIEW_H +// Application headers. +#include "UserAgentHelper.h" + // Qt framework headers. #include #include @@ -45,7 +48,7 @@ signals: public Q_SLOTS: // Define the public slots. void applyApplicationSettings(); - void applyDomainSettings(); + void applyDomainSettingsAndReload(); private Q_SLOTS: // Define the private slots. @@ -62,7 +65,12 @@ private: QPushButton *javaScriptButtonPointer; KLineEdit *urlLineEditPointer; QWebEngineHistory *webEngineHistoryPointer; + QWebEngineProfile *webEngineProfilePointer; QWebEngineSettings *webEngineSettingsPointer; QWebEngineView *webEngineViewPointer; + UserAgentHelper *userAgentHelperPointer; + + // Define the private functions. + void applyDomainSettings(bool reloadWebsite); }; #endif diff --git a/src/mainview.ui b/src/MainView.ui similarity index 100% rename from src/mainview.ui rename to src/MainView.ui diff --git a/src/mouseeventfilter.cpp b/src/MouseEventFilter.cpp similarity index 98% rename from src/mouseeventfilter.cpp rename to src/MouseEventFilter.cpp index 4f1ec82..3454d06 100644 --- a/src/mouseeventfilter.cpp +++ b/src/MouseEventFilter.cpp @@ -18,7 +18,7 @@ */ // Application headers. -#include "mouseeventfilter.h" +#include "MouseEventFilter.h" // Qt headers. #include diff --git a/src/mouseeventfilter.h b/src/MouseEventFilter.h similarity index 100% rename from src/mouseeventfilter.h rename to src/MouseEventFilter.h diff --git a/src/settings.kcfg b/src/Settings.kcfg similarity index 93% rename from src/settings.kcfg rename to src/Settings.kcfg index f739cf6..fe9b22d 100644 --- a/src/settings.kcfg +++ b/src/Settings.kcfg @@ -31,6 +31,10 @@ false + + + PrivacyBrowser/1.0 + diff --git a/src/settings.kcfgc b/src/Settings.kcfgc similarity index 98% rename from src/settings.kcfgc rename to src/Settings.kcfgc index 5304975..3129834 100644 --- a/src/settings.kcfgc +++ b/src/Settings.kcfgc @@ -17,7 +17,7 @@ # Specify the KConfig file. -File=settings.kcfg +File=Settings.kcfg # Specify the class name, which will be used to autogenerate .cpp and .h files. ClassName=Settings diff --git a/src/settingsgeneral.ui b/src/SettingsGeneral.ui similarity index 100% rename from src/settingsgeneral.ui rename to src/SettingsGeneral.ui diff --git a/src/SettingsPrivacy.ui b/src/SettingsPrivacy.ui new file mode 100644 index 0000000..264ac2d --- /dev/null +++ b/src/SettingsPrivacy.ui @@ -0,0 +1,120 @@ + + + + + + + PrivacySettings + + + + + + + + JavaScript + + + + JavaScript allows websites to run programs (scripts) on the device. The default is disabled. + + + + + + + + + UserAgent + + + + + + + + + 500 + 0 + + + + + The default is Privacy Browser + + + + true + + + + + Privacy Browser + + + + + + Firefox Linux + + + + + + Chromium Linux + + + + + + Firefox Windows + + + + + + Chrome Windows + + + + + + Edge Windows + + + + + + Safari macOS + + + --> + + + + + + + Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + + diff --git a/src/UserAgentHelper.cpp b/src/UserAgentHelper.cpp new file mode 100644 index 0000000..d1182ef --- /dev/null +++ b/src/UserAgentHelper.cpp @@ -0,0 +1,61 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC 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. + * + * Privacy Browser PC 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 Privacy Browser PC. If not, see . + */ + +// Application headers. +#include "UserAgentHelper.h" + +// The default constructor. +UserAgentHelper::UserAgentHelper() {}; + +QString UserAgentHelper::getUserAgent(const QString &userAgentName) +{ + if (userAgentName == "Privacy Browser") + { + return "PrivacyBrowser/1.0"; + } + else if (userAgentName == "Firefox Linux") + { + return "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"; + } + else if (userAgentName == "Chromium Linux") + { + return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"; + } + else if (userAgentName == "Firefox Windows") + { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0"; + } + else if (userAgentName == "Chrome Windows") + { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"; + } + else if (userAgentName == "Edge Windows") + { + return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34"; + } + else if (userAgentName == "Safari macOS") + { + return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15"; + } + else + { + // Return the custom user agent. + return userAgentName; + } +} diff --git a/src/UserAgentHelper.h b/src/UserAgentHelper.h new file mode 100644 index 0000000..8d59e4f --- /dev/null +++ b/src/UserAgentHelper.h @@ -0,0 +1,35 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC 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. + * + * Privacy Browser PC 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 Privacy Browser PC. If not, see . + */ + +#ifndef USERAGENTHELPER_H +#define USERAGENTHELPER_H + +// Qt framework headers. +#include + +class UserAgentHelper +{ +public: + // The default constructor. + UserAgentHelper(); + + // The public functions. + QString getUserAgent(const QString &userAgentName); +}; +#endif diff --git a/src/main.cpp b/src/main.cpp index 80909e2..bf2e23f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ */ // Application headers. -#include "browserwindow.h" +#include "BrowserWindow.h" // KDE Frameworks headers. #include diff --git a/src/settingsprivacy.ui b/src/settingsprivacy.ui deleted file mode 100644 index 5c80ede..0000000 --- a/src/settingsprivacy.ui +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - - PrivacySettings - - - - - - 0 - - - - - - - - - - - JavaScript allows websites to run programs (scripts) on the device. The default is disabled. - - - - - - - - JavaScript - - - - - - -- 2.43.0