2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "BrowserWindow.h"
23 #include "ui_SettingsPrivacy.h"
24 #include "ui_SettingsGeneral.h"
25 #include "helpers/SearchEngineHelper.h"
26 #include "helpers/UserAgentHelper.h"
28 // KDE Frameworks headers.
29 #include <KActionCollection>
30 #include <KConfigDialog>
32 // Qt framework headers.
35 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
37 // Instantiate the main view pointer.
38 mainViewPointer = new MainView(this);
40 // Set the main view as the central widget.
41 setCentralWidget(mainViewPointer);
43 // Get a handle for the action collectoin.
44 KActionCollection *actionCollectionPointer = this->actionCollection();
46 // Add the standard actions.
47 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
48 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
49 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
51 // Add the custom actions.
52 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
53 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
54 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
55 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
56 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
57 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
58 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
59 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
60 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
61 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
62 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
63 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
64 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
65 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
66 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
68 // Create the action groups
69 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
70 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
72 // Add the actions to the groups.
73 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
74 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
75 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
76 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
77 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
78 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
79 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
80 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
81 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
82 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
83 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
84 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
85 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
86 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
87 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
89 // Set some actions to be checkable.
90 userAgentPrivacyBrowserActionPointer->setCheckable(true);
91 userAgentFirefoxLinuxActionPointer->setCheckable(true);
92 userAgentChromiumLinuxActionPointer->setCheckable(true);
93 userAgentFirefoxWindowsActionPointer->setCheckable(true);
94 userAgentChromeWindowsActionPointer->setCheckable(true);
95 userAgentEdgeWindowsActionPointer->setCheckable(true);
96 userAgentSafariMacosActionPointer->setCheckable(true);
97 userAgentCustomActionPointer->setCheckable(true);
98 searchEngineMojeekActionPointer->setCheckable(true);
99 searchEngineMonoclesActionPointer->setCheckable(true);
100 searchEngineMetagerActionPointer->setCheckable(true);
101 searchEngineGoogleActionPointer->setCheckable(true);
102 searchEngineBingActionPointer->setCheckable(true);
103 searchEngineYahooActionPointer->setCheckable(true);
104 searchEngineCustomActionPointer->setCheckable(true);
106 // Set the action names. The custom action text will be set later in the on-the-fly update slots.
107 userAgentPrivacyBrowserActionPointer->setText(i18nc("@action", "Privacy Browser"));
108 userAgentFirefoxLinuxActionPointer->setText(i18nc("@action", "Firefox Linux"));
109 userAgentChromiumLinuxActionPointer->setText(i18nc("@action", "Chromium Linux"));
110 userAgentFirefoxWindowsActionPointer->setText(i18nc("@action", "Firefox Windows"));
111 userAgentChromeWindowsActionPointer->setText(i18nc("@action", "Chrome Windows"));
112 userAgentEdgeWindowsActionPointer->setText(i18nc("@action", "Edge Windows"));
113 userAgentSafariMacosActionPointer->setText(i18nc("@action", "Safari macOS"));
114 searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
115 searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
116 searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
117 searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
118 searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
119 searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
121 // Update the on-the-fly menus.
122 connect(mainViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
123 connect(mainViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
125 // Apply the on-the-fly settings when selected.
126 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
127 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
129 // Initialize the on-the-fly search engine menu, as the slot connection had not been created when MainView was constructed.
130 updateOnTheFlySearchEngine(Settings::searchEngine());
132 // Update the status bar with the URL when a link is hovered.
133 connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
135 // Setup the GUI based on the privacybrowserui.rc file.
139 void BrowserWindow::fileNew() const
141 // Display a new instance of Privacy Browser.
142 (new BrowserWindow)->show();
145 void BrowserWindow::settingsConfigure()
147 // Check to make sure the dialog box isn't already displayed.
148 if (!KConfigDialog::exists(QStringLiteral("settings")))
150 // Create the settings widgets.
151 QWidget *privacySettingsWidgetPointer = new QWidget;
152 QWidget *generalSettingsWidgetPointer = new QWidget;
154 // Instantiate the settings UI.
155 Ui::PrivacySettings privacySettingsUi;
156 Ui::GeneralSettings generalSettingsUi;
158 // Setup the UI to display the settings widgets.
159 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
160 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
162 // Get handles for the widgets.
163 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
164 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
165 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
166 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
168 // Populate the combo box labels.
169 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
170 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
172 // Update the labels when the combo boxs change.
173 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
174 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
176 // Instantiate a settings config dialog from the settings.kcfg file.
177 KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
179 // Add the settings widgets as config dialog pages.
180 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
181 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
183 // Delete the config dialog when it is closed.
184 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
187 configDialogPointer->show();
189 // Expand the config dialog.
190 configDialogPointer->resize(1000, 500);
192 // Apply the settings when they are updated.
193 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
194 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
198 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
200 // Initialize the custom search engine flag.
201 bool customSearchEngine = false;
203 if (searchEngine == "Mojeek") // Mojeek.
205 searchEngineMojeekActionPointer->setChecked(true);
207 else if (searchEngine == "Monocles") // Monocles.
209 searchEngineMonoclesActionPointer->setChecked(true);
211 else if (searchEngine == "MetaGer") // MetaGer.
213 searchEngineMetagerActionPointer->setChecked(true);
215 else if (searchEngine == "Google") // Google.
217 searchEngineGoogleActionPointer->setChecked(true);
219 else if (searchEngine == "Bing") // Bing.
221 searchEngineBingActionPointer->setChecked(true);
223 else if (searchEngine == "Yahoo") // Yahoo.
225 searchEngineYahooActionPointer->setChecked(true);
227 else // Custom search engine.
229 // Check the user agent.
230 searchEngineCustomActionPointer->setChecked(true);
232 // Set the custom search engine flag.
233 customSearchEngine = true;
237 // Format the custom search engine.
238 if (customSearchEngine)
240 // Enable the custom search engine.
241 searchEngineCustomActionPointer->setEnabled(true);
243 // Set the custom search engine text.
244 searchEngineCustomActionPointer->setText(searchEngine);
248 // Disable the custom search engine.
249 searchEngineCustomActionPointer->setEnabled(false);
251 // Reset the custom search engine text.
252 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
256 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
258 // Initialize the custom user agent flag.
259 bool customUserAgent = false;
261 // Check the indicated on-the-fly user agent.
262 if (userAgent == "Privacy Browser") // Privacy Browser.
264 userAgentPrivacyBrowserActionPointer->setChecked(true);
266 else if (userAgent == "Firefox Linux") // Firefox Linux.
268 userAgentFirefoxLinuxActionPointer->setChecked(true);
270 else if (userAgent == "Chromium Linux") // Chromium Linux.
272 userAgentChromiumLinuxActionPointer->setChecked(true);
274 else if (userAgent == "Firefox Windows") // Firefox Windows.
276 userAgentFirefoxWindowsActionPointer->setChecked(true);
278 else if (userAgent == "Chrome Windows") // Chrome Windows.
280 userAgentChromeWindowsActionPointer->setChecked(true);
282 else if (userAgent == "Edge Windows") // Edge Windows.
284 userAgentEdgeWindowsActionPointer->setChecked(true);
286 else if (userAgent == "Safari macOS") // Safari macOS.
288 userAgentSafariMacosActionPointer->setChecked(true);
290 else // Custom user agent.
292 // Check the user agent.
293 userAgentCustomActionPointer->setChecked(true);
295 // Set the custom user agent flag.
296 customUserAgent = true;
300 // Format the custom user agent.
303 // Enable the custom user agent.
304 userAgentCustomActionPointer->setEnabled(true);
306 // Set the custom user agent text.
307 userAgentCustomActionPointer->setText(userAgent);
311 // Disable the custom user agent.
312 userAgentCustomActionPointer->setEnabled(false);
314 // Reset the custom user agent text.
315 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
320 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
322 // Update the search engine label.
323 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
326 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
328 // Display the status bar message.
329 statusBar()->showMessage(statusBarMessage);
332 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
334 // Update the user agent label.
335 userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));