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.
33 #include <QInputDialog>
36 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
38 // Instantiate the main view pointer.
39 mainViewPointer = new MainView(this);
41 // Set the main view as the central widget.
42 setCentralWidget(mainViewPointer);
44 // Get a handle for the action collectoin.
45 KActionCollection *actionCollectionPointer = this->actionCollection();
47 // Add the standard actions.
48 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
49 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
50 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
52 // Add the custom actions.
53 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
54 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
55 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
56 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
57 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
58 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
59 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
60 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
61 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
62 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
63 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
64 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
65 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
66 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
67 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
68 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
70 // Create the action groups
71 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
72 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
74 // Add the actions to the groups.
75 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
76 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
77 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
78 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
79 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
80 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
81 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
82 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
83 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
84 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
85 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
86 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
87 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
88 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
89 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
91 // Set some actions to be checkable.
92 userAgentPrivacyBrowserActionPointer->setCheckable(true);
93 userAgentFirefoxLinuxActionPointer->setCheckable(true);
94 userAgentChromiumLinuxActionPointer->setCheckable(true);
95 userAgentFirefoxWindowsActionPointer->setCheckable(true);
96 userAgentChromeWindowsActionPointer->setCheckable(true);
97 userAgentEdgeWindowsActionPointer->setCheckable(true);
98 userAgentSafariMacosActionPointer->setCheckable(true);
99 userAgentCustomActionPointer->setCheckable(true);
100 searchEngineMojeekActionPointer->setCheckable(true);
101 searchEngineMonoclesActionPointer->setCheckable(true);
102 searchEngineMetagerActionPointer->setCheckable(true);
103 searchEngineGoogleActionPointer->setCheckable(true);
104 searchEngineBingActionPointer->setCheckable(true);
105 searchEngineYahooActionPointer->setCheckable(true);
106 searchEngineCustomActionPointer->setCheckable(true);
108 // Set the non-mutable action names.
109 userAgentPrivacyBrowserActionPointer->setText(i18nc("@action", "Privacy Browser"));
110 userAgentFirefoxLinuxActionPointer->setText(i18nc("@action", "Firefox Linux"));
111 userAgentChromiumLinuxActionPointer->setText(i18nc("@action", "Chromium Linux"));
112 userAgentFirefoxWindowsActionPointer->setText(i18nc("@action", "Firefox Windows"));
113 userAgentChromeWindowsActionPointer->setText(i18nc("@action", "Chrome Windows"));
114 userAgentEdgeWindowsActionPointer->setText(i18nc("@action", "Edge Windows"));
115 userAgentSafariMacosActionPointer->setText(i18nc("@action", "Safari macOS"));
116 searchEngineMojeekActionPointer->setText(i18nc("@action", "Mojeek"));
117 searchEngineMonoclesActionPointer->setText(i18nc("@action", "Monocles"));
118 searchEngineMetagerActionPointer->setText(i18nc("@action", "MetaGer"));
119 searchEngineGoogleActionPointer->setText(i18nc("@action", "Google"));
120 searchEngineBingActionPointer->setText(i18nc("@action", "Bing"));
121 searchEngineYahooActionPointer->setText(i18nc("@action", "Yahoo"));
123 // Update the on-the-fly menus.
124 connect(mainViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
125 connect(mainViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
126 connect(mainViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
128 // Apply the on-the-fly settings when selected.
129 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
130 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), mainViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
133 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
135 // Set the current zoom factor.
136 currentZoomFactor = Settings::zoomFactor();
138 // Initialize the on-the-fly menus, as the slot connections had not been created when MainView was constructed.
139 updateOnTheFlyZoomFactor(currentZoomFactor);
140 updateOnTheFlySearchEngine(Settings::searchEngine());
142 // Update the status bar with the URL when a link is hovered.
143 connect(mainViewPointer, SIGNAL(linkHovered(QString)), this, SLOT(updateStatusBar(QString)));
145 // Setup the GUI based on the privacybrowserui.rc file.
149 void BrowserWindow::fileNew() const
151 // Display a new instance of Privacy Browser.
152 (new BrowserWindow)->show();
155 void BrowserWindow::getZoomFactorFromUser()
157 // Create an OK flag.
160 // 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.
161 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The tile of the on-the-fly zoom factor dialog", "On-The-Fly Zoom Factor"),
162 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
163 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
167 // Update the current zoom factor.
168 currentZoomFactor = newZoomFactor;
170 // Set the new zoom factor.
171 mainViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
173 // Update the on-the-fly action text.
174 updateOnTheFlyZoomFactor(newZoomFactor);
178 void BrowserWindow::settingsConfigure()
180 // Check to make sure the dialog box isn't already displayed.
181 if (!KConfigDialog::exists(QStringLiteral("settings")))
183 // Create the settings widgets.
184 QWidget *privacySettingsWidgetPointer = new QWidget;
185 QWidget *generalSettingsWidgetPointer = new QWidget;
187 // Instantiate the settings UI.
188 Ui::PrivacySettings privacySettingsUi;
189 Ui::GeneralSettings generalSettingsUi;
191 // Setup the UI to display the settings widgets.
192 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
193 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
195 // Get handles for the widgets.
196 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
197 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
198 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
199 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
201 // Populate the combo box labels.
202 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
203 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
205 // Update the labels when the combo boxs change.
206 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
207 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
209 // Instantiate a settings config dialog from the settings.kcfg file.
210 KConfigDialog *configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
212 // Add the settings widgets as config dialog pages.
213 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
214 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
216 // Delete the config dialog when it is closed.
217 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
220 configDialogPointer->show();
222 // Expand the config dialog.
223 configDialogPointer->resize(1000, 500);
225 // Apply the settings when they are updated.
226 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyApplicationSettings()));
227 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), mainViewPointer, SLOT(applyDomainSettingsAndReload()));
231 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
233 // Initialize the custom search engine flag.
234 bool customSearchEngine = false;
236 if (searchEngine == "Mojeek") // Mojeek.
238 searchEngineMojeekActionPointer->setChecked(true);
240 else if (searchEngine == "Monocles") // Monocles.
242 searchEngineMonoclesActionPointer->setChecked(true);
244 else if (searchEngine == "MetaGer") // MetaGer.
246 searchEngineMetagerActionPointer->setChecked(true);
248 else if (searchEngine == "Google") // Google.
250 searchEngineGoogleActionPointer->setChecked(true);
252 else if (searchEngine == "Bing") // Bing.
254 searchEngineBingActionPointer->setChecked(true);
256 else if (searchEngine == "Yahoo") // Yahoo.
258 searchEngineYahooActionPointer->setChecked(true);
260 else // Custom search engine.
262 // Check the user agent.
263 searchEngineCustomActionPointer->setChecked(true);
265 // Set the custom search engine flag.
266 customSearchEngine = true;
270 // Format the custom search engine.
271 if (customSearchEngine)
273 // Enable the custom search engine.
274 searchEngineCustomActionPointer->setEnabled(true);
276 // Set the custom search engine text.
277 searchEngineCustomActionPointer->setText(searchEngine);
281 // Disable the custom search engine.
282 searchEngineCustomActionPointer->setEnabled(false);
284 // Reset the custom search engine text.
285 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
289 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
291 // Initialize the custom user agent flag.
292 bool customUserAgent = false;
294 // Check the indicated on-the-fly user agent.
295 if (userAgent == "Privacy Browser") // Privacy Browser.
297 userAgentPrivacyBrowserActionPointer->setChecked(true);
299 else if (userAgent == "Firefox Linux") // Firefox Linux.
301 userAgentFirefoxLinuxActionPointer->setChecked(true);
303 else if (userAgent == "Chromium Linux") // Chromium Linux.
305 userAgentChromiumLinuxActionPointer->setChecked(true);
307 else if (userAgent == "Firefox Windows") // Firefox Windows.
309 userAgentFirefoxWindowsActionPointer->setChecked(true);
311 else if (userAgent == "Chrome Windows") // Chrome Windows.
313 userAgentChromeWindowsActionPointer->setChecked(true);
315 else if (userAgent == "Edge Windows") // Edge Windows.
317 userAgentEdgeWindowsActionPointer->setChecked(true);
319 else if (userAgent == "Safari macOS") // Safari macOS.
321 userAgentSafariMacosActionPointer->setChecked(true);
323 else // Custom user agent.
325 // Check the user agent.
326 userAgentCustomActionPointer->setChecked(true);
328 // Set the custom user agent flag.
329 customUserAgent = true;
333 // Format the custom user agent.
336 // Enable the custom user agent.
337 userAgentCustomActionPointer->setEnabled(true);
339 // Set the custom user agent text.
340 userAgentCustomActionPointer->setText(userAgent);
344 // Disable the custom user agent.
345 userAgentCustomActionPointer->setEnabled(false);
347 // Reset the custom user agent text.
348 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
352 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor) const
354 // Update the zoom factor action text, formatting the double with 2 decimal places.
355 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
358 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
360 // Update the search engine label.
361 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
364 void BrowserWindow::updateStatusBar(const QString &statusBarMessage) const
366 // Display the status bar message.
367 statusBar()->showMessage(statusBarMessage);
370 void BrowserWindow::updateUserAgentLabel(const QString &userAgentName) const
372 // Update the user agent label.
373 userAgentLabelPointer->setText(UserAgentHelper::getUserAgent(userAgentName));