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 "dialogs/DomainSettingsDialog.h"
26 #include "helpers/SearchEngineHelper.h"
27 #include "helpers/UserAgentHelper.h"
29 // KDE Frameworks headers.
30 #include <KActionCollection>
33 // Qt toolkit headers.
34 #include <QInputDialog>
37 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
39 // Instantiate the main view pointer.
40 browserViewPointer = new BrowserView(this);
42 // Set the main view as the central widget.
43 setCentralWidget(browserViewPointer);
45 // Get a handle for the action collection.
46 KActionCollection *actionCollectionPointer = this->actionCollection();
48 // Add the standard actions.
49 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
50 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
51 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
52 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
53 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
54 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
55 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
57 // Add the custom actions.
58 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
59 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
60 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
61 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
62 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
63 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
64 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
65 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
66 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
67 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
68 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
69 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
70 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
71 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
72 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
73 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
74 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
75 domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
77 // Create the action groups
78 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
79 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
81 // Add the actions to the groups.
82 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
83 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
84 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
85 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
86 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
87 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
88 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
89 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
90 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
91 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
92 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
93 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
94 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
95 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
96 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
98 // Set some actions to be checkable.
99 userAgentPrivacyBrowserActionPointer->setCheckable(true);
100 userAgentFirefoxLinuxActionPointer->setCheckable(true);
101 userAgentChromiumLinuxActionPointer->setCheckable(true);
102 userAgentFirefoxWindowsActionPointer->setCheckable(true);
103 userAgentChromeWindowsActionPointer->setCheckable(true);
104 userAgentEdgeWindowsActionPointer->setCheckable(true);
105 userAgentSafariMacosActionPointer->setCheckable(true);
106 userAgentCustomActionPointer->setCheckable(true);
107 searchEngineMojeekActionPointer->setCheckable(true);
108 searchEngineMonoclesActionPointer->setCheckable(true);
109 searchEngineMetagerActionPointer->setCheckable(true);
110 searchEngineGoogleActionPointer->setCheckable(true);
111 searchEngineBingActionPointer->setCheckable(true);
112 searchEngineYahooActionPointer->setCheckable(true);
113 searchEngineCustomActionPointer->setCheckable(true);
115 // Set the non-mutable action text.
116 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
117 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
118 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
119 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
120 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
121 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
122 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
123 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
124 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
125 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
126 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
127 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
128 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
129 javaScriptActionPointer->setText(i18nc("JavaScript button", "JavaScript"));
130 domainSettingsActionPointer->setText(i18nc("Domain Settings button", "Domain Settings"));
132 // Set the action icons.
133 //refreshActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
134 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("network-server-symbolic")));
136 // Update the on-the-fly menus.
137 connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
138 connect(browserViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
139 connect(browserViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
141 // Apply the on-the-fly settings when selected.
142 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
143 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
146 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
148 // Connect the URL toolbar actions.
149 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
150 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
152 // Update the URL toolbar actions.
153 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
154 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
155 connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
157 // Setup the GUI based on the browser_ui.rc file.
158 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
160 // Get a handle for the URL toolbar.
161 KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
163 // Create a URL line edit.
164 urlLineEditPointer = new KLineEdit();
166 // Populate the URL toolbar.
167 urlToolBarPointer->addWidget(urlLineEditPointer);
168 urlToolBarPointer->addAction(javaScriptActionPointer);
169 urlToolBarPointer->addAction(domainSettingsActionPointer);
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::openDomainSettings() const
265 // Remove the focus from the URL line edit.
266 urlLineEditPointer->clearFocus();
268 // Instantiate the domain settings window.
269 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
271 // Set the dialog window title.
272 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
275 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
278 domainSettingsDialogPointer->show();
280 // Reload the tabs when domain settings are updated.
281 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
284 void BrowserWindow::refresh() const
286 // Remove the focus from the URL line edit.
287 urlLineEditPointer->clearFocus();
289 // Refresh the web page.
290 browserViewPointer->refresh();
293 void BrowserWindow::toggleJavaScript() const
295 // Remove the focus from the URL line edit.
296 urlLineEditPointer->clearFocus();
298 // Toggle JavaScript.
299 browserViewPointer->toggleJavaScript();
302 QSize BrowserWindow::sizeHint() const
304 // Return the default window size.
305 return QSize(1500, 1200);
308 void BrowserWindow::settingsConfigure()
310 // Check to make sure the dialog box isn't already displayed.
311 if (KConfigDialog::exists(QStringLiteral("settings")))
313 // Show the existing config dialog if it is hidden.
314 configDialogPointer->show();
316 // Raise the existing config dialog if it is below other windows.
317 configDialogPointer->raise();
319 // Restore the existing config dialog if it has been minimized.
320 if (configDialogPointer->isMinimized()) {
321 configDialogPointer->showNormal();
324 // Activate the existing config dialog, which brings its virtual desktop into focus.
325 configDialogPointer->activateWindow();
329 // Create the settings widgets.
330 QWidget *privacySettingsWidgetPointer = new QWidget;
331 QWidget *generalSettingsWidgetPointer = new QWidget;
333 // Instantiate the settings UI.
334 Ui::PrivacySettings privacySettingsUi;
335 Ui::GeneralSettings generalSettingsUi;
337 // Setup the UI to display the settings widgets.
338 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
339 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
341 // Get handles for the widgets.
342 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
343 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
344 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
345 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
347 // Populate the combo box labels.
348 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
349 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
351 // Update the labels when the combo boxes change.
352 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
353 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
355 // Instantiate a settings config dialog from the settings.kcfg file.
356 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
358 // Add the settings widgets as config dialog pages.
359 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
360 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
362 // Delete the config dialog when it is closed.
363 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
366 configDialogPointer->show();
368 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
369 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
370 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
371 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
372 //configDialogPointer->adjustSize();
374 // Expand the config dialog.
375 configDialogPointer->resize(1000, 500);
377 // Apply the settings when they are updated.
378 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
379 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
383 void BrowserWindow::updateDomainSettingsIndicator(const bool &status) const
385 // Set the domain palette according to the status.
386 if (status) urlLineEditPointer->setPalette(domainSettingsPalette);
387 else urlLineEditPointer->setPalette(noDomainSettingsPalette);
390 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled) const
392 // Set the icon according to the status.
393 if (isEnabled) javaScriptActionPointer->setIcon(QIcon(":/icons/javascript-warning"));
394 else javaScriptActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
397 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
399 // Initialize the custom search engine flag.
400 bool customSearchEngine = false;
402 if (searchEngine == "Mojeek") // Mojeek.
404 searchEngineMojeekActionPointer->setChecked(true);
406 else if (searchEngine == "Monocles") // Monocles.
408 searchEngineMonoclesActionPointer->setChecked(true);
410 else if (searchEngine == "MetaGer") // MetaGer.
412 searchEngineMetagerActionPointer->setChecked(true);
414 else if (searchEngine == "Google") // Google.
416 searchEngineGoogleActionPointer->setChecked(true);
418 else if (searchEngine == "Bing") // Bing.
420 searchEngineBingActionPointer->setChecked(true);
422 else if (searchEngine == "Yahoo") // Yahoo.
424 searchEngineYahooActionPointer->setChecked(true);
426 else // Custom search engine.
428 // Check the user agent.
429 searchEngineCustomActionPointer->setChecked(true);
431 // Set the custom search engine flag.
432 customSearchEngine = true;
435 // Format the custom search engine.
436 if (customSearchEngine)
438 // Enable the custom search engine.
439 searchEngineCustomActionPointer->setEnabled(true);
441 // Set the custom search engine text.
442 searchEngineCustomActionPointer->setText(searchEngine);
446 // Disable the custom search engine.
447 searchEngineCustomActionPointer->setEnabled(false);
449 // Reset the custom search engine text.
450 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
454 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
456 // Initialize the custom user agent flag.
457 bool customUserAgent = false;
459 // Check the indicated on-the-fly user agent.
460 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser.
461 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true); // Firefox Linux.
462 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true); // Chromium Linux.
463 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true); // Firefox Windows.
464 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true); // Chrome Windows.
465 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true); // Edge Windows.
466 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true); // Safara macOS.
467 else // Custom user agent.
469 // Check the user agent.
470 userAgentCustomActionPointer->setChecked(true);
472 // Set the custom user agent flag.
473 customUserAgent = true;
477 // Format the custom user agent.
480 // Enable the custom user agent.
481 userAgentCustomActionPointer->setEnabled(true);
483 // Set the custom user agent text.
484 userAgentCustomActionPointer->setText(userAgent);
488 // Disable the custom user agent.
489 userAgentCustomActionPointer->setEnabled(false);
491 // Reset the custom user agent text.
492 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
496 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
498 // Set the current zoom factor.
499 currentZoomFactor = Settings::zoomFactor();
501 // Update the zoom factor action text, formatting the double with 2 decimal places.
502 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
505 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
507 // Update the search engine label.
508 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
511 void BrowserWindow::updateUrlLineEdit(const QString &newUrl) const
513 // Update the URL line edit if it does not have focus.
514 if (!urlLineEditPointer->hasFocus())
516 // Update the URL line edit.
517 urlLineEditPointer->setText(newUrl);
521 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
523 // Update the user agent label.
524 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));