From: Soren Stoutner Date: Sat, 5 Feb 2022 04:53:28 +0000 (-0700) Subject: Add user agent controls. X-Git-Tag: v0.1~63 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=05800bafb963a10f291d2a9604dc90cd7ac37c7b;p=PrivacyBrowserPC.git Add user agent controls. --- diff --git a/src/BrowserWindow.cpp b/src/BrowserWindow.cpp new file mode 100644 index 0000000..546a951 --- /dev/null +++ b/src/BrowserWindow.cpp @@ -0,0 +1,124 @@ +/* + * 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 "BrowserWindow.h" +#include "Settings.h" +#include "ui_SettingsPrivacy.h" +#include "ui_SettingsGeneral.h" + +// KDE Frameworks headers. +#include +#include + +// Qt framework headers. +#include + +BrowserWindow::BrowserWindow() : KXmlGuiWindow() +{ + // Instantiate the main view pointer. + mainViewPointer = new MainView(this); + + // Set the main view as the central widget. + setCentralWidget(mainViewPointer); + + // Get a handle for the action collectoin. + KActionCollection *actionCollectionPointer = this->actionCollection(); + + // Add the standard actions. + KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer); + KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer); + KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer); + + // Update the status bar with the URL when a link is hovered. + connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString))); + + // Setup the GUI based on the privacybrowserui.rc file. + setupGUI(); +} + +void BrowserWindow::fileNew() +{ + // Display a new instance of Privacy Browser. + (new BrowserWindow)->show(); +} + +void BrowserWindow::settingsConfigure() +{ + // Check to make sure the dialog box isn't already displayed. + if (!KConfigDialog::exists(QStringLiteral("settings"))) + { + // Create the settings widgets. + QWidget *privacySettingsWidgetPointer = new QWidget; + QWidget *generalSettingsWidgetPointer = new QWidget; + + // Instantiate the settings UI. + Ui::PrivacySettings privacySettingsUi; + Ui::GeneralSettings generalSettingsUi; + + // Setup the UI to display the settings widgets. + 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()); + + // 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. + configDialogPointer->setAttribute(Qt::WA_DeleteOnClose); + + // 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(applyDomainSettingsAndReload())); + } +} + +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 new file mode 100644 index 0000000..31ac510 --- /dev/null +++ b/src/BrowserWindow.h @@ -0,0 +1,55 @@ +/* + * 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 BROWSERWINDOW_H +#define BROWSERWINDOW_H + +// Application headers. +#include "MainView.h" +#include "UserAgentHelper.h" + +// Qt framework headers. +#include + +// KDE Frameworks headers. +#include + +class BrowserWindow : public KXmlGuiWindow +{ + // Include the Q_OBJECT macro. + Q_OBJECT + +public: + // The default constructor. + BrowserWindow(); + +private Q_SLOTS: + // Define the private 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 new file mode 100644 index 0000000..cf84ecc --- /dev/null +++ b/src/MainView.cpp @@ -0,0 +1,203 @@ +/* + * 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 "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) +{ + // Instantiate the mainview UI. + Ui::MainView mainViewUi; + + // Setup the UI. + mainViewUi.setupUi(this); + + // Get handles for the views. + backButtonPointer = mainViewUi.backButton; + forwardButtonPointer = mainViewUi.forwardButton; + QPushButton *homeButtonPointer = mainViewUi.homeButton; + urlLineEditPointer = mainViewUi.urlLineEdit; + javaScriptButtonPointer = mainViewUi.javaScript; + webEngineViewPointer = mainViewUi.webEngineView; + + // 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. + connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString))); + + // Update the URL line edit form the webengine view. + connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface())); + + // Setup the URL bar buttons. + 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); + + // Install the mouse event filter. + qApp->installEventFilter(mouseEventFilterPointer); + + // 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); + + // Set the zoom factor. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + + // Apply the application settings. + applyApplicationSettings(); + + // Apply the domain settings. `false` does not reload the website. + applyDomainSettings(false); + + // Set the focus on the WebEngine view. + webEngineViewPointer->setFocus(); + + // Get the arguments. + QStringList argumentsStringList = qApp->arguments(); + + // Check to see if the arguments lists contains a URL. + if (argumentsStringList.size() > 1) + { + // Load the URL from the arguments list. + webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1))); + } + else + { + // Load the homepage. + goHome(); + } +} + +void MainView::applyApplicationSettings() +{ + // TODO. +} + +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()); + + // Update the JavaScript button. + if (Settings::javaScript()) + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); + } + else + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); + } + + // Apply the user agent. + webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent())); + + // Reload the website if requested. + if (reloadWebsite) + { + webEngineViewPointer->reload(); + } +} + +void MainView::goHome() +{ + // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing. + webEngineViewPointer->setUrl(Settings::homepage()); +} + +void MainView::loadUrl(const QString &urlFromUser) +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS. + webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); +} + +void MainView::pageLinkHovered(const QString &linkUrl) +{ + // Emit a signal so that the browser window can update the status bar. + 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. + if (!urlLineEditPointer->hasFocus()) + { + // Update the URL line edit. + urlLineEditPointer->setText(webEngineViewPointer->url().toString()); + } + + // Update the status of the forward and back buttons. + backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); + forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); + + // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); +} diff --git a/src/MainView.h b/src/MainView.h new file mode 100644 index 0000000..f08975e --- /dev/null +++ b/src/MainView.h @@ -0,0 +1,76 @@ +/* + * 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 MAINVIEW_H +#define MAINVIEW_H + +// Application headers. +#include "UserAgentHelper.h" + +// Qt framework headers. +#include +#include +#include +#include + +// KDE Framework headers. +#include + +class MainView : public QWidget +{ + // Include the Q_OBJECT macro. + Q_OBJECT + +public: + // The primary contructor. + explicit MainView(QWidget *parent); + +signals: + // Define the signals. + void linkHovered(const QString &linkUrl); + +public Q_SLOTS: + // Define the public slots. + void applyApplicationSettings(); + void applyDomainSettingsAndReload(); + +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; + 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 new file mode 100644 index 0000000..c7afeb8 --- /dev/null +++ b/src/MainView.ui @@ -0,0 +1,155 @@ + + + + + + MainView + + + + + + 0 + 0 + 315 + 233 + + + + + + + 0 + + + + + 0 + + + + 0 + + + + 0 + + + + 0 + + + + + + + + 0 + + + + + + + + + + + + 24 + 24 + + + + + true + + + + + + + + + + + + + + 24 + 24 + + + + + true + + + + + + + + + + + + + + 24 + 24 + + + + + true + + + + + + + + + + + + + + + 24 + 24 + + + + + true + + + + + + + + + + + + + diff --git a/src/MouseEventFilter.cpp b/src/MouseEventFilter.cpp new file mode 100644 index 0000000..3454d06 --- /dev/null +++ b/src/MouseEventFilter.cpp @@ -0,0 +1,70 @@ +/* + * 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 "MouseEventFilter.h" + +// Qt headers. +#include +#include + +// The primary constructor. +MouseEventFilter::MouseEventFilter(QWebEngineView *webEngineView) : QObject() +{ + // Save a handle to the WebEngine view. + webEngineViewPointer = webEngineView; +}; + +bool MouseEventFilter::eventFilter(QObject *objectPointer, QEvent *eventPointer) +{ + // Only process mouse button press events. + if (eventPointer->type() == QEvent::MouseButtonPress) + { + // Tell the compiler to ignore the unused object pointer. + (void)objectPointer; + + // Cast the event to a mouse event. + QMouseEvent *mouseEventPointer = static_cast(eventPointer); + + // Run the command according to the button that was pushed. + switch (mouseEventPointer->button()) + { + case (Qt::BackButton): + // Tell the WebEngine to go back. + webEngineViewPointer->back(); + + // Consume the event. + return true; + + case (Qt::ForwardButton): + // Tell the WebEngine to go forward. + webEngineViewPointer->forward(); + + // Consume the event. + return true; + + default: + // Do not consume the event. + return false; + } + } + + // Do not consume the event. + return false; +} diff --git a/src/MouseEventFilter.h b/src/MouseEventFilter.h new file mode 100644 index 0000000..82cee6f --- /dev/null +++ b/src/MouseEventFilter.h @@ -0,0 +1,43 @@ +/* + * 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 MOUSEEVENTFILTER_H +#define MOUSEEVENTFILTER_H + +// Qt headers. +#include +#include + +class MouseEventFilter : public QObject +{ + // Include the Q_OBJECT macro. + Q_OBJECT + +public: + // The primary constructor. + MouseEventFilter(QWebEngineView *webEngineView); + +protected: + bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override; + +private: + // Define the private variables. + QWebEngineView *webEngineViewPointer; +}; +#endif diff --git a/src/Settings.kcfg b/src/Settings.kcfg new file mode 100644 index 0000000..fe9b22d --- /dev/null +++ b/src/Settings.kcfg @@ -0,0 +1,49 @@ + + + + + + + + + + + + + false + + + + PrivacyBrowser/1.0 + + + + + + https://www.mojeek.com/ + + + + 1.00 + + + diff --git a/src/Settings.kcfgc b/src/Settings.kcfgc new file mode 100644 index 0000000..3129834 --- /dev/null +++ b/src/Settings.kcfgc @@ -0,0 +1,26 @@ +# 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 . + + +# Specify the KConfig file. +File=Settings.kcfg + +# Specify the class name, which will be used to autogenerate .cpp and .h files. +ClassName=Settings + +# Make the generated class a singleton. TODO, the default is false. This may not be needed. +Singleton=true diff --git a/src/SettingsGeneral.ui b/src/SettingsGeneral.ui new file mode 100644 index 0000000..de42696 --- /dev/null +++ b/src/SettingsGeneral.ui @@ -0,0 +1,70 @@ + + + + + + + GeneralSettings + + + + + + + + Homepage + + + + + + + + The default is https://www.mojeek.com/. + + + + + + + + + Zoom factor + + + + + + + + Set the zoom factor between 0.25 and 5.00. The default is 1.00. + + + + 0.250000000000000 + + + + 5.000000000000000 + + + + + + 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/browserwindow.cpp b/src/browserwindow.cpp deleted file mode 100644 index baf9fe9..0000000 --- a/src/browserwindow.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 "browserwindow.h" -#include "settings.h" -#include "ui_settingsprivacy.h" -#include "ui_settingsgeneral.h" - -// KDE Frameworks headers. -#include -#include - -// Qt framework headers. -#include - -BrowserWindow::BrowserWindow() : KXmlGuiWindow() -{ - // Instantiate the main view pointer. - mainViewPointer = new MainView(this); - - // Set the main view as the central widget. - setCentralWidget(mainViewPointer); - - // Get a handle for the action collectoin. - KActionCollection *actionCollectionPointer = this->actionCollection(); - - // Add the standard actions. - KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer); - KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer); - KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer); - - // Update the status bar with the URL when a link is hovered. - connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString))); - - // Setup the GUI based on the privacybrowserui.rc file. - setupGUI(); -} - -void BrowserWindow::fileNew() -{ - // Display a new instance of Privacy Browser. - (new BrowserWindow)->show(); -} - -void BrowserWindow::settingsConfigure() -{ - // Check to make sure the dialog box isn't already displayed. - if (!KConfigDialog::exists(QStringLiteral("settings"))) - { - // Create the settings widgets. - QWidget *privacySettingsWidgetPointer = new QWidget; - QWidget *generalSettingsWidgetPointer = new QWidget; - - // Instantiate the settings UI. - Ui::PrivacySettings privacySettingsUi; - Ui::GeneralSettings generalSettingsUi; - - // 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 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. - configDialogPointer->setAttribute(Qt::WA_DeleteOnClose); - - // 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())); - } -} - -void BrowserWindow::updateStatusBar(const QString &statusBarMessage) -{ - // Display the status bar message. - statusBar()->showMessage(statusBarMessage); -} diff --git a/src/browserwindow.h b/src/browserwindow.h deleted file mode 100644 index fc1e53d..0000000 --- a/src/browserwindow.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 BROWSERWINDOW_H -#define BROWSERWINDOW_H - -// Application headers. -#include "mainview.h" - -// KDE Frameworks headers. -#include - -class BrowserWindow : public KXmlGuiWindow -{ - // Include the Q_OBJECT macro. - Q_OBJECT - -public: - // The default constructor. - BrowserWindow(); - -private Q_SLOTS: - // Define the private slots. - void fileNew(); - void settingsConfigure(); - void updateStatusBar(const QString &statusBarMessage); - -private: - // Define the private variables. - MainView *mainViewPointer; -}; -#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/mainview.cpp b/src/mainview.cpp deleted file mode 100644 index 416014b..0000000 --- a/src/mainview.cpp +++ /dev/null @@ -1,181 +0,0 @@ -/* - * 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 "browserwindow.h" -#include "mainview.h" -#include "mouseeventfilter.h" -#include "settings.h" -#include "ui_mainview.h" - -MainView::MainView(QWidget *parent) : QWidget(parent) -{ - // Instantiate the mainview UI. - Ui::MainView mainViewUi; - - // Setup the UI. - mainViewUi.setupUi(this); - - // Get handles for the views. - backButtonPointer = mainViewUi.backButton; - forwardButtonPointer = mainViewUi.forwardButton; - QPushButton *homeButtonPointer = mainViewUi.homeButton; - urlLineEditPointer = mainViewUi.urlLineEdit; - javaScriptButtonPointer = mainViewUi.javaScript; - webEngineViewPointer = mainViewUi.webEngineView; - - // 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))); - - // Update the URL line edit form the webengine view. - connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); - connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface())); - connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface())); - - // Setup the URL bar buttons. - 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); - - // Install the mouse event filter. - qApp->installEventFilter(mouseEventFilterPointer); - - // 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(); - - // Get the arguments. - QStringList argumentsStringList = qApp->arguments(); - - // Check to see if the arguments lists contains a URL. - if (argumentsStringList.size() > 1) - { - // Load the URL from the arguments list. - webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1))); - } - else - { - // Load the homepage. - 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. - webEngineViewPointer->setUrl(Settings::homepage()); -} - -void MainView::loadUrl(const QString &urlFromUser) -{ - // Remove the focus from the URL line edit. - urlLineEditPointer->clearFocus(); - - // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS. - webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); -} - -void MainView::pageLinkHovered(const QString &linkUrl) -{ - // Emit a signal so that the browser window can update the status bar. - 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. - if (!urlLineEditPointer->hasFocus()) - { - // Update the URL line edit. - urlLineEditPointer->setText(webEngineViewPointer->url().toString()); - } - - // Update the status of the forward and back buttons. - backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); - forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); - - // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. - webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); -} diff --git a/src/mainview.h b/src/mainview.h deleted file mode 100644 index 018594e..0000000 --- a/src/mainview.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 MAINVIEW_H -#define MAINVIEW_H - -// Qt framework headers. -#include -#include -#include -#include - -// KDE Framework headers. -#include - -class MainView : public QWidget -{ - // Include the Q_OBJECT macro. - Q_OBJECT - -public: - // The primary contructor. - explicit MainView(QWidget *parent); - -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 deleted file mode 100644 index c7afeb8..0000000 --- a/src/mainview.ui +++ /dev/null @@ -1,155 +0,0 @@ - - - - - - MainView - - - - - - 0 - 0 - 315 - 233 - - - - - - - 0 - - - - - 0 - - - - 0 - - - - 0 - - - - 0 - - - - - - - - 0 - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - - - 24 - 24 - - - - - true - - - - - - - - - - - - - diff --git a/src/mouseeventfilter.cpp b/src/mouseeventfilter.cpp deleted file mode 100644 index 4f1ec82..0000000 --- a/src/mouseeventfilter.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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 "mouseeventfilter.h" - -// Qt headers. -#include -#include - -// The primary constructor. -MouseEventFilter::MouseEventFilter(QWebEngineView *webEngineView) : QObject() -{ - // Save a handle to the WebEngine view. - webEngineViewPointer = webEngineView; -}; - -bool MouseEventFilter::eventFilter(QObject *objectPointer, QEvent *eventPointer) -{ - // Only process mouse button press events. - if (eventPointer->type() == QEvent::MouseButtonPress) - { - // Tell the compiler to ignore the unused object pointer. - (void)objectPointer; - - // Cast the event to a mouse event. - QMouseEvent *mouseEventPointer = static_cast(eventPointer); - - // Run the command according to the button that was pushed. - switch (mouseEventPointer->button()) - { - case (Qt::BackButton): - // Tell the WebEngine to go back. - webEngineViewPointer->back(); - - // Consume the event. - return true; - - case (Qt::ForwardButton): - // Tell the WebEngine to go forward. - webEngineViewPointer->forward(); - - // Consume the event. - return true; - - default: - // Do not consume the event. - return false; - } - } - - // Do not consume the event. - return false; -} diff --git a/src/mouseeventfilter.h b/src/mouseeventfilter.h deleted file mode 100644 index 82cee6f..0000000 --- a/src/mouseeventfilter.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 MOUSEEVENTFILTER_H -#define MOUSEEVENTFILTER_H - -// Qt headers. -#include -#include - -class MouseEventFilter : public QObject -{ - // Include the Q_OBJECT macro. - Q_OBJECT - -public: - // The primary constructor. - MouseEventFilter(QWebEngineView *webEngineView); - -protected: - bool eventFilter(QObject *objectPointer, QEvent *eventPointer) override; - -private: - // Define the private variables. - QWebEngineView *webEngineViewPointer; -}; -#endif diff --git a/src/settings.kcfg b/src/settings.kcfg deleted file mode 100644 index f739cf6..0000000 --- a/src/settings.kcfg +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - - - - - - false - - - - - - https://www.mojeek.com/ - - - - 1.00 - - - diff --git a/src/settings.kcfgc b/src/settings.kcfgc deleted file mode 100644 index 5304975..0000000 --- a/src/settings.kcfgc +++ /dev/null @@ -1,26 +0,0 @@ -# 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 . - - -# Specify the KConfig file. -File=settings.kcfg - -# Specify the class name, which will be used to autogenerate .cpp and .h files. -ClassName=Settings - -# Make the generated class a singleton. TODO, the default is false. This may not be needed. -Singleton=true diff --git a/src/settingsgeneral.ui b/src/settingsgeneral.ui deleted file mode 100644 index de42696..0000000 --- a/src/settingsgeneral.ui +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - GeneralSettings - - - - - - - - Homepage - - - - - - - - The default is https://www.mojeek.com/. - - - - - - - - - Zoom factor - - - - - - - - Set the zoom factor between 0.25 and 5.00. The default is 1.00. - - - - 0.250000000000000 - - - - 5.000000000000000 - - - - - - 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 - - - - - -