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>
32 // Qt toolkit headers.
33 #include <QInputDialog>
36 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
38 // Instantiate the main view pointer.
39 browserViewPointer = new BrowserView(this);
41 // Set the main view as the central widget.
42 setCentralWidget(browserViewPointer);
44 // Get a handle for the action collection.
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"));
69 backActionPointer = actionCollectionPointer->addAction(QStringLiteral("back"));
70 forwardActionPointer = actionCollectionPointer->addAction(QStringLiteral("forward"));
71 refreshActionPointer = actionCollectionPointer->addAction(QStringLiteral("refresh"));
72 homeActionPointer = actionCollectionPointer->addAction(QStringLiteral("home"));
74 // Create the action groups
75 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
76 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
78 // Add the actions to the groups.
79 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
80 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
81 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
82 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
83 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
84 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
85 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
86 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
87 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
88 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
89 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
90 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
91 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
92 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
93 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
95 // Set some actions to be checkable.
96 userAgentPrivacyBrowserActionPointer->setCheckable(true);
97 userAgentFirefoxLinuxActionPointer->setCheckable(true);
98 userAgentChromiumLinuxActionPointer->setCheckable(true);
99 userAgentFirefoxWindowsActionPointer->setCheckable(true);
100 userAgentChromeWindowsActionPointer->setCheckable(true);
101 userAgentEdgeWindowsActionPointer->setCheckable(true);
102 userAgentSafariMacosActionPointer->setCheckable(true);
103 userAgentCustomActionPointer->setCheckable(true);
104 searchEngineMojeekActionPointer->setCheckable(true);
105 searchEngineMonoclesActionPointer->setCheckable(true);
106 searchEngineMetagerActionPointer->setCheckable(true);
107 searchEngineGoogleActionPointer->setCheckable(true);
108 searchEngineBingActionPointer->setCheckable(true);
109 searchEngineYahooActionPointer->setCheckable(true);
110 searchEngineCustomActionPointer->setCheckable(true);
112 // Set the non-mutable action text.
113 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
114 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
115 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
116 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
117 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
118 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
119 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
120 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
121 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
122 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
123 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
124 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
125 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
126 backActionPointer->setText(i18nc("Back button", "Back"));
127 forwardActionPointer->setText(i18nc("Forward button", "Forward"));
128 refreshActionPointer->setText(i18nc("Refresh button", "Refresh"));
129 homeActionPointer->setText(i18nc("Home button", "Home"));
131 // Set the action icons.
132 backActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
133 forwardActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
134 refreshActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
135 homeActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("home")));
137 // Update the on-the-fly menus.
138 connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
139 connect(browserViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
140 connect(browserViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
142 // Apply the on-the-fly settings when selected.
143 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
144 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
147 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
149 // Connect the URL toolbar actions.
150 connect(backActionPointer, SIGNAL(triggered()), this, SLOT(back()));
151 connect(forwardActionPointer, SIGNAL(triggered()), this, SLOT(forward()));
152 connect(refreshActionPointer, SIGNAL(triggered()), this, SLOT(refresh()));
153 connect(homeActionPointer, SIGNAL(triggered()), this, SLOT(home()));
155 // Update the URL toolbar actions.
156 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
157 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
159 // Setup the GUI based on the browser_ui.rc file.
160 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
162 // Get a handle for the URL toolbar.
163 KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
165 // Create a URL line edit.
166 urlLineEditPointer = new KLineEdit();
168 // Add the URL line edit to the URL toolbar.
169 urlToolBarPointer->addWidget(urlLineEditPointer);
171 // Load a new URL from the URL line edit.
172 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
174 // Update the URL line edit on page loads.
175 connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QString)), this, SLOT(updateUrlLineEdit(QString)));
177 // Get a handle for the status bar.
178 QStatusBar *statusBarPointer = statusBar();
180 // Update the status bar with the URL when a link is hovered.
181 connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
183 // Get the URL line edit palettes.
184 noDomainSettingsPalette = urlLineEditPointer->palette();
185 domainSettingsPalette = urlLineEditPointer->palette();
187 // Modify the domain settings palette.
188 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
190 // Update the applied palette.
191 connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool)), this, SLOT(updateDomainSettingsIndicator(bool)));
193 // Load the initial website.
194 browserViewPointer->loadInitialWebsite();
197 void BrowserWindow::back() const
199 // Remove the focus from the URL line edit.
200 urlLineEditPointer->clearFocus();
203 browserViewPointer->back();
206 void BrowserWindow::fileNew() const
208 // Display a new instance of Privacy Browser.
209 (new BrowserWindow)->show();
212 void BrowserWindow::forward() const
214 // Remove the focus from the URL line edit.
215 urlLineEditPointer->clearFocus();
218 browserViewPointer->forward();
221 void BrowserWindow::getZoomFactorFromUser()
223 // Create an OK flag.
226 // 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.
227 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
228 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
229 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
231 // Update the zoom factor if the user clicked OK.
234 // Update the current zoom factor.
235 currentZoomFactor = newZoomFactor;
237 // Set the new zoom factor.
238 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
240 // Update the on-the-fly action text.
241 updateOnTheFlyZoomFactor(newZoomFactor);
245 void BrowserWindow::home() const
247 // Remove the focus from the URL line edit.
248 urlLineEditPointer->clearFocus();
251 browserViewPointer->home();
254 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
256 // Remove the focus from the URL line edit.
257 urlLineEditPointer->clearFocus();
260 browserViewPointer->loadUrlFromLineEdit(url);
263 void BrowserWindow::refresh() const
265 // Remove the focus from the URL line edit.
266 urlLineEditPointer->clearFocus();
268 // Refresh the web page.
269 browserViewPointer->refresh();
272 void BrowserWindow::settingsConfigure()
274 // Check to make sure the dialog box isn't already displayed.
275 if (KConfigDialog::exists(QStringLiteral("settings")))
277 // Show the existing config dialog if it is hidden.
278 configDialogPointer->show();
280 // Raise the existing config dialog if it is below other windows.
281 configDialogPointer->raise();
283 // Restore the existing config dialog if it has been minimized.
284 if (configDialogPointer->isMinimized()) {
285 configDialogPointer->showNormal();
288 // Activate the existing config dialog, which brings its virtual desktop into focus.
289 configDialogPointer->activateWindow();
293 // Create the settings widgets.
294 QWidget *privacySettingsWidgetPointer = new QWidget;
295 QWidget *generalSettingsWidgetPointer = new QWidget;
297 // Instantiate the settings UI.
298 Ui::PrivacySettings privacySettingsUi;
299 Ui::GeneralSettings generalSettingsUi;
301 // Setup the UI to display the settings widgets.
302 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
303 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
305 // Get handles for the widgets.
306 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
307 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
308 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
309 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
311 // Populate the combo box labels.
312 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
313 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
315 // Update the labels when the combo boxes change.
316 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
317 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
319 // Instantiate a settings config dialog from the settings.kcfg file.
320 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
322 // Add the settings widgets as config dialog pages.
323 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
324 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
326 // Delete the config dialog when it is closed.
327 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
330 configDialogPointer->show();
332 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
333 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
334 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
335 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
336 //configDialogPointer->adjustSize();
338 // Expand the config dialog.
339 configDialogPointer->resize(1000, 500);
341 // Apply the settings when they are updated.
342 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
343 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
347 void BrowserWindow::updateDomainSettingsIndicator(const bool status) const
349 // Set the domain palette according to the status.
350 if (status) urlLineEditPointer->setPalette(domainSettingsPalette);
351 else urlLineEditPointer->setPalette(noDomainSettingsPalette);
354 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
356 // Initialize the custom search engine flag.
357 bool customSearchEngine = false;
359 if (searchEngine == "Mojeek") // Mojeek.
361 searchEngineMojeekActionPointer->setChecked(true);
363 else if (searchEngine == "Monocles") // Monocles.
365 searchEngineMonoclesActionPointer->setChecked(true);
367 else if (searchEngine == "MetaGer") // MetaGer.
369 searchEngineMetagerActionPointer->setChecked(true);
371 else if (searchEngine == "Google") // Google.
373 searchEngineGoogleActionPointer->setChecked(true);
375 else if (searchEngine == "Bing") // Bing.
377 searchEngineBingActionPointer->setChecked(true);
379 else if (searchEngine == "Yahoo") // Yahoo.
381 searchEngineYahooActionPointer->setChecked(true);
383 else // Custom search engine.
385 // Check the user agent.
386 searchEngineCustomActionPointer->setChecked(true);
388 // Set the custom search engine flag.
389 customSearchEngine = true;
392 // Format the custom search engine.
393 if (customSearchEngine)
395 // Enable the custom search engine.
396 searchEngineCustomActionPointer->setEnabled(true);
398 // Set the custom search engine text.
399 searchEngineCustomActionPointer->setText(searchEngine);
403 // Disable the custom search engine.
404 searchEngineCustomActionPointer->setEnabled(false);
406 // Reset the custom search engine text.
407 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
411 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
413 // Initialize the custom user agent flag.
414 bool customUserAgent = false;
416 // Check the indicated on-the-fly user agent.
417 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser.
418 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true); // Firefox Linux.
419 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true); // Chromium Linux.
420 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true); // Firefox Windows.
421 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true); // Chrome Windows.
422 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true); // Edge Windows.
423 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true); // Safara macOS.
424 else // Custom user agent.
426 // Check the user agent.
427 userAgentCustomActionPointer->setChecked(true);
429 // Set the custom user agent flag.
430 customUserAgent = true;
434 // Format the custom user agent.
437 // Enable the custom user agent.
438 userAgentCustomActionPointer->setEnabled(true);
440 // Set the custom user agent text.
441 userAgentCustomActionPointer->setText(userAgent);
445 // Disable the custom user agent.
446 userAgentCustomActionPointer->setEnabled(false);
448 // Reset the custom user agent text.
449 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
453 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
455 // Set the current zoom factor.
456 currentZoomFactor = Settings::zoomFactor();
458 // Update the zoom factor action text, formatting the double with 2 decimal places.
459 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
462 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
464 // Update the search engine label.
465 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
468 void BrowserWindow::updateUrlLineEdit(const QString &newUrl) const
470 // Update the URL line edit if it does not have focus.
471 if (!urlLineEditPointer->hasFocus())
473 // Update the URL line edit.
474 urlLineEditPointer->setText(newUrl);
478 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
480 // Update the user agent label.
481 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));