From 1cbda7a594e97dd3ba6e1c0675be6ec9f6a424b2 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 2 Feb 2022 16:08:36 -0700 Subject: [PATCH] Add JavaScript controls. --- src/CMakeLists.txt | 3 +- src/browserwindow.cpp | 24 ++++++---- src/browserwindow.h | 7 +++ src/icons/javascript-warning.svg | 47 ++++++++++++++++++++ src/icons/privacy-mode.svg | 47 ++++++++++++++++++++ src/mainview.cpp | 58 ++++++++++++++++++++++++- src/mainview.h | 8 ++++ src/mainview.ui | 31 ++++++++++--- src/resources.qrc | 2 + src/settings.kcfg | 8 +++- src/{settings.ui => settingsgeneral.ui} | 47 +++----------------- src/settingsprivacy.ui | 54 +++++++++++++++++++++++ 12 files changed, 279 insertions(+), 57 deletions(-) create mode 100644 src/icons/javascript-warning.svg create mode 100644 src/icons/privacy-mode.svg rename src/{settings.ui => settingsgeneral.ui} (62%) create mode 100644 src/settingsprivacy.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1043918..c36a84b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,7 +40,8 @@ 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 - settings.ui + settingsprivacy.ui + settingsgeneral.ui ) # Link the following libraries. diff --git a/src/browserwindow.cpp b/src/browserwindow.cpp index d539fed..baf9fe9 100644 --- a/src/browserwindow.cpp +++ b/src/browserwindow.cpp @@ -19,9 +19,9 @@ // Application headers. #include "browserwindow.h" -#include "mainview.h" #include "settings.h" -#include "ui_settings.h" +#include "ui_settingsprivacy.h" +#include "ui_settingsgeneral.h" // KDE Frameworks headers. #include @@ -33,7 +33,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow() { // Instantiate the main view pointer. - MainView *mainViewPointer = new MainView(this); + mainViewPointer = new MainView(this); // Set the main view as the central widget. setCentralWidget(mainViewPointer); @@ -64,19 +64,23 @@ void BrowserWindow::settingsConfigure() // Check to make sure the dialog box isn't already displayed. if (!KConfigDialog::exists(QStringLiteral("settings"))) { - // Create a general settings page. + // Create the settings widgets. + QWidget *privacySettingsWidgetPointer = new QWidget; QWidget *generalSettingsWidgetPointer = new QWidget; // Instantiate the settings UI. - Ui::Settings settingsUi; + Ui::PrivacySettings privacySettingsUi; + Ui::GeneralSettings generalSettingsUi; - // Setup the UI to display the general settings widget. - settingsUi.setupUi(generalSettingsWidgetPointer); + // Setup the UI to display the settings widgets. + privacySettingsUi.setupUi(privacySettingsWidgetPointer); + generalSettingsUi.setupUi(generalSettingsWidgetPointer); // Instantiate a settings config dialog from the settings.kcfg file. KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self()); - // Add the general settings widget page. + // Add the settings widgets as config dialog pages. + configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser")); configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings")); // Delete the config dialog when it is closed. @@ -84,6 +88,10 @@ void BrowserWindow::settingsConfigure() // Make it so. configDialogPointer->show(); + + // Apply the settings when they are updated. + connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings())); + connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettings())); } } diff --git a/src/browserwindow.h b/src/browserwindow.h index 613f210..fc1e53d 100644 --- a/src/browserwindow.h +++ b/src/browserwindow.h @@ -20,6 +20,9 @@ #ifndef BROWSERWINDOW_H #define BROWSERWINDOW_H +// Application headers. +#include "mainview.h" + // KDE Frameworks headers. #include @@ -37,5 +40,9 @@ private Q_SLOTS: void fileNew(); void settingsConfigure(); void updateStatusBar(const QString &statusBarMessage); + +private: + // Define the private variables. + MainView *mainViewPointer; }; #endif diff --git a/src/icons/javascript-warning.svg b/src/icons/javascript-warning.svg new file mode 100644 index 0000000..39245a7 --- /dev/null +++ b/src/icons/javascript-warning.svg @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/icons/privacy-mode.svg b/src/icons/privacy-mode.svg new file mode 100644 index 0000000..428d507 --- /dev/null +++ b/src/icons/privacy-mode.svg @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mainview.cpp b/src/mainview.cpp index 25f3b4c..3ebee4c 100644 --- a/src/mainview.cpp +++ b/src/mainview.cpp @@ -28,6 +28,7 @@ // Qt headers. #include +#include #include MainView::MainView(QWidget *parent) : QWidget(parent) @@ -43,11 +44,13 @@ MainView::MainView(QWidget *parent) : QWidget(parent) forwardButtonPointer = mainViewUi.forwardButton; QPushButton *homeButtonPointer = mainViewUi.homeButton; urlLineEditPointer = mainViewUi.urlLineEdit; + javaScriptButtonPointer = mainViewUi.javaScript; webEngineViewPointer = mainViewUi.webEngineView; - // Get handles for the webpage and history. + // Get handles for the aspects of the WebEngine. QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page(); webEngineHistoryPointer = webEnginePagePointer->history(); + webEngineSettingsPointer = webEngineViewPointer->settings(); // Update the webengine view from the URL line edit. connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString))); @@ -61,6 +64,7 @@ MainView::MainView(QWidget *parent) : QWidget(parent) connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward())); connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome())); + connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript())); // Instantiate the mouse event pointer. MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer); @@ -71,9 +75,18 @@ MainView::MainView(QWidget *parent) : QWidget(parent) // Listen for hovered link URLs. connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString))); + // Don't allow JavaScript to open windows. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); + // Set the zoom factor. webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + // Apply the application settings. + applyApplicationSettings(); + + // Apply the domain settings. + applyDomainSettings(); + // Set the focus on the WebEngine view. webEngineViewPointer->setFocus(); @@ -81,6 +94,30 @@ MainView::MainView(QWidget *parent) : QWidget(parent) goHome(); } +void MainView::applyApplicationSettings() +{ + // TODO. +} + +void MainView::applyDomainSettings() +{ + // Set the JavaScript status. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript()); + + // Update the JavaScript button. + if (Settings::javaScript()) + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); + } + else + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); + } + + // Reload the website. + webEngineViewPointer->reload(); +} + void MainView::goHome() { // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing. @@ -102,6 +139,25 @@ void MainView::pageLinkHovered(const QString &linkUrl) emit linkHovered(linkUrl); } +void MainView::toggleJavaScript() +{ + // Toggle JavaScript. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); + + // Update the JavaScript button. + if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)) + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); + } + else + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); + } + + // Reload the website. + webEngineViewPointer->reload(); +} + void MainView::updateInterface() { // Update the URL line edit if it does not have focus. diff --git a/src/mainview.h b/src/mainview.h index 76d0bba..ee82d79 100644 --- a/src/mainview.h +++ b/src/mainview.h @@ -36,19 +36,27 @@ signals: // Define the signals. void linkHovered(const QString &linkUrl); +public Q_SLOTS: + // Define the public slots. + void applyApplicationSettings(); + void applyDomainSettings(); + private Q_SLOTS: // Define the private slots. void goHome(); void loadUrl(const QString &urlFromUser); void pageLinkHovered(const QString &linkUrl); + void toggleJavaScript(); void updateInterface(); private: // Define the private variables. QPushButton *backButtonPointer; QPushButton *forwardButtonPointer; + QPushButton *javaScriptButtonPointer; KLineEdit *urlLineEditPointer; QWebEngineHistory *webEngineHistoryPointer; + QWebEngineSettings *webEngineSettingsPointer; QWebEngineView *webEngineViewPointer; }; #endif diff --git a/src/mainview.ui b/src/mainview.ui index 03b60b9..d00087f 100644 --- a/src/mainview.ui +++ b/src/mainview.ui @@ -55,6 +55,7 @@ 0 + @@ -62,7 +63,7 @@ 0 - + @@ -82,7 +83,7 @@ - + @@ -102,7 +103,7 @@ - + @@ -122,14 +123,34 @@ - + + + + + + + + + + + + 24 + 24 + + + + + true + + + - + diff --git a/src/resources.qrc b/src/resources.qrc index 5ac9f4c..cfa6f79 100644 --- a/src/resources.qrc +++ b/src/resources.qrc @@ -19,6 +19,8 @@ + icons/javascript-warning.svg + icons/privacy-mode.svg icons/sc-apps-privacy-browser.svg licenses/GPLv3+.txt diff --git a/src/settings.kcfg b/src/settings.kcfg index 8497069..f739cf6 100644 --- a/src/settings.kcfg +++ b/src/settings.kcfg @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with Privacy Browser PC. If not, see . --> - + + + + false + + + https://www.mojeek.com/ diff --git a/src/settings.ui b/src/settingsgeneral.ui similarity index 62% rename from src/settings.ui rename to src/settingsgeneral.ui index a6c876f..de42696 100644 --- a/src/settings.ui +++ b/src/settingsgeneral.ui @@ -19,71 +19,40 @@ along with Privacy Browser PC. If not, see . --> - Settings - - - - - 0 - 0 - 374 - 200 - - + + GeneralSettings + + Homepage - - - Set the hompage. - - - - Set the homepage. The default is https://www.mojeek.com/. - - Set the hompage. - - - - Set the homepage. The default is https://www.mojeek.com/. + The default is https://www.mojeek.com/. - + Zoom factor - - - Set the zoom factor between 0.25 and 5.00. - - - - Set the zoom factor between 0.25 and 5.00. The default is 1.00. - - Set the zoom factor between 0.25 and 5.00. - - - Set the zoom factor between 0.25 and 5.00. The default is 1.00. @@ -98,8 +67,4 @@ - - - - diff --git a/src/settingsprivacy.ui b/src/settingsprivacy.ui new file mode 100644 index 0000000..5c80ede --- /dev/null +++ b/src/settingsprivacy.ui @@ -0,0 +1,54 @@ + + + + + + + PrivacySettings + + + + + + 0 + + + + + + + + + + + JavaScript allows websites to run programs (scripts) on the device. The default is disabled. + + + + + + + + JavaScript + + + + + + -- 2.45.2