-/*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
- *
- * This file is part of Privacy Browser PC <https://www.stoutner.com/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 <http://www.gnu.org/licenses/>.
- */
-
-// Application headers.
-#include "BrowserWindow.h"
-#include "Settings.h"
-#include "ui_SettingsPrivacy.h"
-#include "ui_SettingsGeneral.h"
-#include "helpers/SearchEngineHelper.h"
-#include "helpers/UserAgentHelper.h"
-
-// KDE Frameworks headers.
-#include <KActionCollection>
-#include <KConfigDialog>
-
-// Qt framework headers.
-#include <QInputDialog>
-#include <QStatusBar>
-
-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);
-
- // Add the custom actions.
- userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
- userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
- userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
- userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
- userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
- userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
- userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
- userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
- zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
- searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
- searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
- searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
- searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
- searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
- searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
- searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
-
- // Create the action groups
- QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
- QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
-
- // Add the actions to the groups.
- userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
- userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
- userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
- userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
- userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
- userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
- userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
- userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
- searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
-
- // Set some actions to be checkable.
- userAgentPrivacyBrowserActionPointer->setCheckable(true);
- userAgentFirefoxLinuxActionPointer->setCheckable(true);
- userAgentChromiumLinuxActionPointer->setCheckable(true);
- userAgentFirefoxWindowsActionPointer->setCheckable(true);
- userAgentChromeWindowsActionPointer->setCheckable(true);
- userAgentEdgeWindowsActionPointer->setCheckable(true);
- userAgentSafariMacosActionPointer->setCheckable(true);
- userAgentCustomActionPointer->setCheckable(true);
- searchEngineMojeekActionPointer->setCheckable(true);
- searchEngineMonoclesActionPointer->setCheckable(true);
- searchEngineMetagerActionPointer->setCheckable(true);
- searchEngineGoogleActionPointer->setCheckable(true);
- searchEngineBingActionPointer->setCheckable(true);
- searchEngineYahooActionPointer->setCheckable(true);
- searchEngineCustomActionPointer->setCheckable(true);
-
- // Set the non-mutable action names.
- 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"));
- searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
- searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
- searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
- searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
- searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
- searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
-
- // Update the on-the-fly menus.
- connect(mainViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
- connect(mainViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
- connect(mainViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
-
- // Apply the on-the-fly settings when selected.
- connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
- connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
-
- // Display dialogs.
- connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
-
- // 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();
-
- // Load the initial webstie.
- mainViewPointer->loadInitialWebsite();
-}
-
-void BrowserWindow::fileNew() const
-{
- // Display a new instance of Privacy Browser.
- (new BrowserWindow)->show();
-}
-
-void BrowserWindow::getZoomFactorFromUser()
-{
- // Create an OK flag.
- bool okClicked;
-
- // Display a dialog to get the new zoom factor from the user. Format the double to display two decimals and have a 0.25 step.
- double newZoomFactor = QInputDialog::getDouble(this, i18nc("The tile of the on-the-fly zoom factor dialog", "On-The-Fly Zoom Factor"),
- i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
- currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
-
- if (okClicked)
- {
- // Update the current zoom factor.
- currentZoomFactor = newZoomFactor;
-
- // Set the new zoom factor.
- mainViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
-
- // Update the on-the-fly action text.
- updateOnTheFlyZoomFactor(newZoomFactor);
- }
-}
-
-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 widgets.
- QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
- userAgentLabelPointer = privacySettingsUi.userAgentLabel;
- QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
- searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
-
- // Populate the combo box labels.
- updateUserAgentLabel(userAgentComboBoxPointer->currentText());
- updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
-
- // Update the labels when the combo boxs change.
- connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
- connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const 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::updateOnTheFlySearchEngine(const QString &searchEngine) const
-{
- // Initialize the custom search engine flag.
- bool customSearchEngine = false;
-
- if (searchEngine == "Mojeek") // Mojeek.
- {
- searchEngineMojeekActionPointer->setChecked(true);
- }
- else if (searchEngine == "Monocles") // Monocles.
- {
- searchEngineMonoclesActionPointer->setChecked(true);
- }
- else if (searchEngine == "MetaGer") // MetaGer.
- {
- searchEngineMetagerActionPointer->setChecked(true);
- }
- else if (searchEngine == "Google") // Google.
- {
- searchEngineGoogleActionPointer->setChecked(true);
- }
- else if (searchEngine == "Bing") // Bing.
- {
- searchEngineBingActionPointer->setChecked(true);
- }
- else if (searchEngine == "Yahoo") // Yahoo.
- {
- searchEngineYahooActionPointer->setChecked(true);
- }
- else // Custom search engine.
- {
- // Check the user agent.
- searchEngineCustomActionPointer->setChecked(true);
-
- // Set the custom search engine flag.
- customSearchEngine = true;
- }
-
-
- // Format the custom search engine.
- if (customSearchEngine)
- {
- // Enable the custom search engine.
- searchEngineCustomActionPointer->setEnabled(true);
-
- // Set the custom search engine text.
- searchEngineCustomActionPointer->setText(searchEngine);
- }
- else
- {
- // Disable the custom search engine.
- searchEngineCustomActionPointer->setEnabled(false);
-
- // Reset the custom search engine text.
- searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
- }
-}
-
-void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
-{
- // Initialize the custom user agent flag.
- 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);
- }
- else // Custom user agent.
- {
- // Check the user agent.
- userAgentCustomActionPointer->setChecked(true);
-
- // Set the custom user agent flag.
- customUserAgent = true;
- }
-
-
- // Format the custom user agent.
- if (customUserAgent)
- {
- // Enable the custom user agent.
- userAgentCustomActionPointer->setEnabled(true);
-
- // Set the custom user agent text.
- userAgentCustomActionPointer->setText(userAgent);
- }
- else
- {
- // Disable the custom user agent.
- userAgentCustomActionPointer->setEnabled(false);
-
- // Reset the custom user agent text.
- userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
- }
-}
-
-void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
-{
- // Set the current zoom factor.
- currentZoomFactor = Settings::zoomFactor();
-
- // Update the zoom factor action text, formatting the double with 2 decimal places.
- zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
-}
-
-void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
-{
- // Update the search engine label.
- searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
-}
-
-void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
-{
- // Display the status bar message.
- statusBar()->showMessage(statusBarMessage);
-}
-
-void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
-{
- // Update the user agent label.
- userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));
-}