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/CookiesDialog.h"
26 #include "dialogs/DomainSettingsDialog.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
30 // KDE Frameworks headers.
31 #include <KActionCollection>
34 // Qt toolkit headers.
35 #include <QFileDialog>
36 #include <QInputDialog>
37 #include <QNetworkCookie>
42 // Construct the class.
43 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
45 // Initialize the variables.
46 javaScriptEnabled = false;
47 localStorageEnabled = false;
49 // Instantiate the privacy tab widget pointer.
50 tabWidgetPointer = new TabWidget(this);
52 // Set the privacy tab widget as the central widget.
53 setCentralWidget(tabWidgetPointer);
55 // Get a handle for the action collection.
56 KActionCollection *actionCollectionPointer = this->actionCollection();
58 // Add the standard actions.
59 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
60 KStandardAction::print(tabWidgetPointer, SLOT(print()), actionCollectionPointer);
61 KStandardAction::printPreview(tabWidgetPointer, SLOT(printPreview()), actionCollectionPointer);
62 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
63 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
64 fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
65 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
66 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
67 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
68 KStandardAction::preferences(this, SLOT(showSettingsDialog()), actionCollectionPointer);
70 // Add the custom actions.
71 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
72 userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
73 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
74 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
75 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
76 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
77 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
78 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
79 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
80 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
81 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
82 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
83 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
84 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
85 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
86 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
87 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
88 QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
89 cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
90 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
91 localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
92 domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
94 // Create the action groups
95 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
96 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
98 // Add the actions to the groups.
99 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
100 userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
101 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
102 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
103 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
104 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
105 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
106 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
107 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
108 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
109 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
110 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
111 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
112 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
113 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
114 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
116 // Set some actions to be checkable.
117 javaScriptActionPointer->setCheckable(true);
118 localStorageActionPointer->setCheckable(true);
119 domStorageActionPointer->setCheckable(true);
120 userAgentPrivacyBrowserActionPointer->setCheckable(true);
121 userAgentWebEngineDefaultActionPointer->setCheckable(true);
122 userAgentFirefoxLinuxActionPointer->setCheckable(true);
123 userAgentChromiumLinuxActionPointer->setCheckable(true);
124 userAgentFirefoxWindowsActionPointer->setCheckable(true);
125 userAgentChromeWindowsActionPointer->setCheckable(true);
126 userAgentEdgeWindowsActionPointer->setCheckable(true);
127 userAgentSafariMacosActionPointer->setCheckable(true);
128 userAgentCustomActionPointer->setCheckable(true);
129 searchEngineMojeekActionPointer->setCheckable(true);
130 searchEngineMonoclesActionPointer->setCheckable(true);
131 searchEngineMetagerActionPointer->setCheckable(true);
132 searchEngineGoogleActionPointer->setCheckable(true);
133 searchEngineBingActionPointer->setCheckable(true);
134 searchEngineYahooActionPointer->setCheckable(true);
135 searchEngineCustomActionPointer->setCheckable(true);
137 // Set the action text.
138 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
139 userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
140 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
141 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
142 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
143 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
144 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
145 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
146 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
147 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
148 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
149 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
150 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
151 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
152 domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
153 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", 0));
154 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
155 localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
156 domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
158 // Set the action icons.
159 userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
160 userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
161 userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
162 userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
163 userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
164 userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
165 userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
166 userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
167 userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
168 searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
169 searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
170 searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
171 searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
172 searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
173 searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
174 searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
175 zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
176 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
177 cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
178 domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("code-class")));
180 // Update the on-the-fly menus.
181 connect(tabWidgetPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
182 connect(tabWidgetPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
183 connect(tabWidgetPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
185 // Apply the on-the-fly settings when selected.
186 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), tabWidgetPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
187 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), tabWidgetPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
189 // Process cookie changes.
190 connect(tabWidgetPointer, SIGNAL(updateCookiesAction(int)), this, SLOT(updateCookiesAction(int)));
193 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
194 connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(showCookiesDialog()));
195 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(showDomainSettingsDialog()));
197 // Connect the URL toolbar actions.
198 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
199 connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
200 connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
202 // Update the URL toolbar actions.
203 connect(tabWidgetPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
204 connect(tabWidgetPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
205 connect(tabWidgetPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
206 connect(tabWidgetPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
207 connect(tabWidgetPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
209 // Setup the GUI based on the browser_ui.rc file.
210 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
212 // Get lists of the actions' associated widgets.
213 QList<QWidget*> userAgentAssociatedWidgetsPointerList = userAgentPrivacyBrowserActionPointer->associatedWidgets();
214 QList<QWidget*> searchEngineAssociatedWidgetsPointerList = searchEngineMojeekActionPointer->associatedWidgets();
216 // Get the menu widget pointers. It is the second entry, after the main window.
217 QWidget *userAgentMenuWidgetPointer = userAgentAssociatedWidgetsPointerList[1];
218 QWidget *searchEngineMenuWidgetPointer = searchEngineAssociatedWidgetsPointerList[1];
220 // Get the menu pointers.
221 QMenu *userAgentMenuPointer = qobject_cast<QMenu*>(userAgentMenuWidgetPointer);
222 QMenu *searchEngineMenuPointer = qobject_cast<QMenu*>(searchEngineMenuWidgetPointer);
224 // Get the menu actions.
225 userAgentMenuActionPointer = userAgentMenuPointer->menuAction();
226 searchEngineMenuActionPointer = searchEngineMenuPointer->menuAction();
228 // Get handles for the toolbars.
229 navigationToolBarPointer = toolBar(QStringLiteral("navigation_toolbar"));
230 urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
232 // Create a URL line edit.
233 urlLineEditPointer = new KLineEdit();
235 // Add an edit or add domain settings action to the URL line edit.
236 QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
238 // Add or edit the current domain settings.
239 connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
241 // Populate the URL toolbar.
242 urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
244 // Load a new URL from the URL line edit.
245 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
247 // Update the URL line edit on page loads.
248 connect(tabWidgetPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
250 // Get a handle for the status bar.
251 QStatusBar *statusBarPointer = statusBar();
253 // Create a progress bar.
254 progressBarPointer = new QProgressBar();
256 // Add the progress bar to to the status bar.
257 statusBarPointer->addPermanentWidget(progressBarPointer);
259 // Update the status bar with the URL when a link is hovered.
260 connect(tabWidgetPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
262 // Update the progress bar.
263 connect(tabWidgetPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
264 connect(tabWidgetPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
266 // Update the URL line edit focus.
267 connect(tabWidgetPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
269 // Get the URL line edit palettes.
270 noDomainSettingsPalette = urlLineEditPointer->palette();
271 domainSettingsPalette = urlLineEditPointer->palette();
273 // Modify the domain settings palette.
274 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
276 // Update the applied palette.
277 connect(tabWidgetPointer, SIGNAL(updateDomainSettingsIndicator(const bool)), this, SLOT(updateDomainSettingsIndicator(const bool)));
279 // Process full screen requests.
280 connect(tabWidgetPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
282 // Create keyboard shortcuts.
283 QShortcut *ctrlTShortcutPointer = new QShortcut(QKeySequence(i18nc("The open new tab shortcut.", "Ctrl+t")), this);
284 QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
285 QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
287 // Connect the keyboard shortcuts to the actions.
288 connect(ctrlTShortcutPointer, SIGNAL(activated()), tabWidgetPointer, SLOT(addTab()));
289 connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
290 connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
292 // Load the initial website.
293 tabWidgetPointer->loadInitialWebsite();
296 void BrowserWindow::addOrEditDomainSettings() const
298 // Remove the focus from the URL line edit.
299 urlLineEditPointer->clearFocus();
301 // Create the domain settings dialog pointer.
302 DomainSettingsDialog *domainSettingsDialogPointer;
304 // Get the current domain settings name.
305 QString ¤tDomainSettingsName = tabWidgetPointer->getDomainSettingsName();
307 // Run the commands according to the current domain settings status.
308 if (currentDomainSettingsName == QStringLiteral("")) // Domain settings are not currently applied.
310 // Instruct the domain settings dialog to add a new domain.
311 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
313 else // Domain settings are currently applied.
315 // Instruct the domain settings dialog to edit the current domain.
316 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsName);
319 // Set the dialog window title.
320 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
323 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
326 domainSettingsDialogPointer->show();
328 // Reload the tabs when domain settings are updated.
329 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
332 void BrowserWindow::back() const
334 // Remove the focus from the URL line edit.
335 urlLineEditPointer->clearFocus();
338 tabWidgetPointer->back();
341 void BrowserWindow::clearUrlLineEditFocus() const
343 // Remove the focus from the URL line edit.
344 urlLineEditPointer->clearFocus();
347 void BrowserWindow::escape() const
349 // Exit full screen browsing if it is enabled.
350 if (fullScreenActionPointer->isChecked())
351 fullScreenActionPointer->trigger();
354 void BrowserWindow::fileNew() const
356 // Display a new instance of Privacy Browser.
357 (new BrowserWindow)->show();
360 void BrowserWindow::forward() const
362 // Remove the focus from the URL line edit.
363 urlLineEditPointer->clearFocus();
366 tabWidgetPointer->forward();
369 void BrowserWindow::fullScreenRequested(const bool toggleOn)
371 // Toggle full screen mode.
372 if (toggleOn) // Turn full screen mode on.
374 // Set the window to be full screen.
375 fullScreenActionPointer->setFullScreen(window(), true);
377 // Hide all the bars.
378 menuBar()->setVisible(false);
379 navigationToolBarPointer->setVisible(false);
380 urlToolBarPointer->setVisible(false);
381 tabWidgetPointer->setTabBarVisible(false);
382 statusBar()->setVisible(false);
384 else // Turn full screen mode off.
386 // Set the window to not be full screen.
387 fullScreenActionPointer->setFullScreen(window(), false);
389 // Show all the bars.
390 menuBar()->setVisible(true);
391 navigationToolBarPointer->setVisible(true);
392 urlToolBarPointer->setVisible(true);
393 tabWidgetPointer->setTabBarVisible(true);
394 statusBar()->setVisible(true);
398 void BrowserWindow::getZoomFactorFromUser()
400 // Create an OK flag.
403 // 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.
404 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
405 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
406 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
408 // Update the zoom factor if the user clicked OK.
411 // Update the current zoom factor.
412 currentZoomFactor = newZoomFactor;
414 // Set the new zoom factor.
415 tabWidgetPointer->applyOnTheFlyZoomFactor(newZoomFactor);
417 // Update the on-the-fly action text.
418 updateZoomFactorAction(newZoomFactor);
422 void BrowserWindow::home() const
424 // Remove the focus from the URL line edit.
425 urlLineEditPointer->clearFocus();
428 tabWidgetPointer->home();
431 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
433 // Remove the focus from the URL line edit.
434 urlLineEditPointer->clearFocus();
437 tabWidgetPointer->loadUrlFromLineEdit(url);
440 void BrowserWindow::refresh() const
442 // Remove the focus from the URL line edit.
443 urlLineEditPointer->clearFocus();
445 // Refresh the web page.
446 tabWidgetPointer->refresh();
449 void BrowserWindow::showCookiesDialog()
451 // Remove the focus from the URL line edit.
452 urlLineEditPointer->clearFocus();
454 // Instantiate the cookie settings dialog.
455 CookiesDialog *cookiesDialogPointer = new CookiesDialog(tabWidgetPointer->getCookieList());
458 cookiesDialogPointer->show();
460 // Connect the dialog signals.
461 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), tabWidgetPointer, SLOT(addCookieToStore(QNetworkCookie)));
462 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), tabWidgetPointer, SLOT(deleteAllCookies()));
463 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), tabWidgetPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
466 void BrowserWindow::showDownloadLocationBrowseDialog() const
468 // Get the current download location.
469 QString currentDownloadLocation = downloadLocationComboBoxPointer->currentText();
471 // Resolve the system download directory if specified.
472 if (currentDownloadLocation == QStringLiteral("System Download Directory"))
473 currentDownloadLocation = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
475 // Get the new download location.
476 QString newDownloadLocation = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download location dialog caption", "Select Download Location"), currentDownloadLocation);
478 // Populate the download location combo box according to the new download location.
479 if (newDownloadLocation == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)) // The default download location was selected.
481 // Populate the download location with the default text.
482 downloadLocationComboBoxPointer->setCurrentText("System Download Directory");
484 else if (newDownloadLocation != QStringLiteral("")) // A different directory was selected.
486 // Populate the download location.
487 downloadLocationComboBoxPointer->setCurrentText(newDownloadLocation);
491 void BrowserWindow::showDomainSettingsDialog() const
493 // Remove the focus from the URL line edit.
494 urlLineEditPointer->clearFocus();
496 // Instantiate the domain settings dialog.
497 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
500 domainSettingsDialogPointer->show();
502 // Reload the tabs when domain settings are updated.
503 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
506 void BrowserWindow::showProgressBar(const int &progress) const
508 // Set the progress bar value.
509 progressBarPointer->setValue(progress);
511 // Show the progress bar.
512 progressBarPointer->show();
515 void BrowserWindow::showSettingsDialog()
517 // Create the settings widgets.
518 QWidget *privacySettingsWidgetPointer = new QWidget;
519 QWidget *generalSettingsWidgetPointer = new QWidget;
521 // Instantiate the settings UI.
522 Ui::PrivacySettings privacySettingsUi;
523 Ui::GeneralSettings generalSettingsUi;
525 // Setup the UI to display the settings widgets.
526 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
527 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
529 // Get handles for the widgets.
530 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
531 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
532 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
533 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
534 downloadLocationComboBoxPointer = generalSettingsUi.kcfg_downloadLocation;
535 QPushButton *browseButtonPointer = generalSettingsUi.browseButton;
537 // Populate the combo box labels.
538 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
539 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
541 // Update the labels when the combo boxes change.
542 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
543 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
545 // Connect the download location directory browse button.
546 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadLocationBrowseDialog()));
548 // Instantiate a settings config dialog from the settings.kcfg file.
549 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
551 // Add the settings widgets as config dialog pages.
552 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
553 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
555 // Prevent interaction with the parent window while the dialog is open.
556 configDialogPointer->setWindowModality(Qt::WindowModal);
559 configDialogPointer->show();
561 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
562 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
563 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
564 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
565 //configDialogPointer->adjustSize();
567 // Expand the config dialog.
568 configDialogPointer->resize(1000, 500);
570 // Apply the settings when they are updated.
571 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyApplicationSettings()));
572 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
575 QSize BrowserWindow::sizeHint() const
577 // Return the default window size.
578 return QSize(1500, 1200);
581 void BrowserWindow::toggleDomStorage() const
583 // Remove the focus from the URL line edit.
584 urlLineEditPointer->clearFocus();
586 // Toggle DOM storage.
587 tabWidgetPointer->toggleDomStorage();
590 void BrowserWindow::toggleJavaScript() const
592 // Remove the focus from the URL line edit.
593 urlLineEditPointer->clearFocus();
595 // Toggle JavaScript.
596 tabWidgetPointer->toggleJavaScript();
599 void BrowserWindow::toggleLocalStorage() const
601 // Remove the focus from the URL line edit.
602 urlLineEditPointer->clearFocus();
604 // Toggle local storage.
605 tabWidgetPointer->toggleLocalStorage();
608 void BrowserWindow::toggleFullScreen()
610 // Toggle the full screen status.
611 if (fullScreenActionPointer->isChecked()) // Enable full screen browsing mode.
613 // Enable full screen mode.
614 fullScreenActionPointer->setFullScreen(window(), true);
616 // Hide the menu bar if specified.
617 if (Settings::fullScreenHideMenuBar())
618 menuBar()->setVisible(false);
620 // Hide the toolbars if specified.
621 if (Settings::fullScreenHideToolBars())
623 navigationToolBarPointer->setVisible(false);
624 urlToolBarPointer->setVisible(false);
627 // Hide the tab bar if specified.
628 if (Settings::fullScreenHideTabBar())
629 tabWidgetPointer->setTabBarVisible(false);
631 // Hide the status bar if specified.
632 if (Settings::fullScreenHideStatusBar())
633 statusBar()->setVisible(false);
635 else // Disable full screen browsing mode.
637 // Disable full screen mode.
638 fullScreenActionPointer->setFullScreen(window(), false);
640 // Show the menu bar.
641 menuBar()->setVisible(true);
643 // Show the toolbars.
644 navigationToolBarPointer->setVisible(true);
645 urlToolBarPointer->setVisible(true);
648 tabWidgetPointer->setTabBarVisible(true);
650 // Show the status bar.
651 statusBar()->setVisible(true);
655 void BrowserWindow::updateCookiesAction(const int numberOfCookies) const
657 // Update the action text.
658 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", numberOfCookies));
661 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
663 // Set the action checked status.
664 domStorageActionPointer->setChecked(isEnabled);
667 void BrowserWindow::updateDomainSettingsIndicator(const bool status)
669 // Set the domain palette according to the status.
671 urlLineEditPointer->setPalette(domainSettingsPalette);
673 urlLineEditPointer->setPalette(noDomainSettingsPalette);
676 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
678 // Update the JavaScript status.
679 javaScriptEnabled = isEnabled;
681 // Set the icon according to the status.
682 if (javaScriptEnabled)
683 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
685 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
687 // Set the action checked status.
688 javaScriptActionPointer->setChecked(javaScriptEnabled);
690 // Update the status of the DOM storage action.
691 domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
694 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
696 // Update the local storage status.
697 localStorageEnabled = isEnabled;
700 if (localStorageEnabled)
701 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
703 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
705 // Set the action checked status.
706 localStorageActionPointer->setChecked(localStorageEnabled);
708 // Update the status of the DOM storage action.
709 domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
712 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
714 // Initialize the custom search engine flag.
715 bool customSearchEngine = false;
717 if (searchEngine == "Mojeek") // Mojeek.
719 // Check the Mojeek user agent action.
720 searchEngineMojeekActionPointer->setChecked(true);
722 // Update the search engine menu action text.
723 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
725 else if (searchEngine == "Monocles") // Monocles.
727 // Check the Monocles user agent action.
728 searchEngineMonoclesActionPointer->setChecked(true);
730 // Update the search engine menu action text.
731 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
733 else if (searchEngine == "MetaGer") // MetaGer.
735 // Check the MetaGer user agent action.
736 searchEngineMetagerActionPointer->setChecked(true);
738 // Update the search engine menu action text.
739 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
741 else if (searchEngine == "Google") // Google.
743 // Check the Google user agent action.
744 searchEngineGoogleActionPointer->setChecked(true);
746 // Update the search engine menu action text.
747 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
749 else if (searchEngine == "Bing") // Bing.
751 // Check the Bing user agent action.
752 searchEngineBingActionPointer->setChecked(true);
754 // Update the search engine menu action text.
755 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
757 else if (searchEngine == "Yahoo") // Yahoo.
759 // Check the Yahoo user agent action.
760 searchEngineYahooActionPointer->setChecked(true);
762 // Update the search engine menu action text.
763 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
765 else // Custom search engine.
767 // Check the user agent.
768 searchEngineCustomActionPointer->setChecked(true);
770 // Update the search engine menu action text.
771 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
773 // Set the custom search engine text.
774 searchEngineCustomActionPointer->setText(searchEngine);
776 // Set the custom search engine flag.
777 customSearchEngine = true;
780 // Update the custom search engine enabled boolean.
781 if (updateCustomSearchEngineStatus)
782 customSearchEngineEnabled = customSearchEngine;
784 // Format the custom search engine.
785 if (customSearchEngineEnabled)
787 // Enable the custom search engine.
788 searchEngineCustomActionPointer->setEnabled(true);
792 // Disable the custom search engine.
793 searchEngineCustomActionPointer->setEnabled(false);
795 // Reset the custom search engine text.
796 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
800 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
802 // Initialize the custom user agent flag.
803 bool customUserAgent = false;
805 // Check the indicated on-the-fly user agent.
806 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) // Privacy Browser.
808 // Check the Privacy Browser user agent action.
809 userAgentPrivacyBrowserActionPointer->setChecked(true);
811 // Update the user agent menu action text.
812 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
814 else if (userAgent == TabWidget::webEngineDefaultUserAgent) // WebEngine default.
816 // check the WebEngine default user agent action.
817 userAgentWebEngineDefaultActionPointer->setChecked(true);
819 // Update the user agent menu action text.
820 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
822 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) // Firefox on Linux.
824 // Check the Firefox on Linux user agent action.
825 userAgentFirefoxLinuxActionPointer->setChecked(true);
827 // Update the user agent menu action text.
828 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
830 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) // Chromium on Linux.
832 // Check the Chromium on Linux user agent action.
833 userAgentChromiumLinuxActionPointer->setChecked(true);
835 // Update the user agent menu action text.
836 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
838 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) // Firefox on Windows.
840 // Check the Firefox on Windows user agent action.
841 userAgentFirefoxWindowsActionPointer->setChecked(true);
843 // Update the user agent menu action text.
844 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
846 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) // Chrome on Windows.
848 // Check the Chrome on Windows user agent action.
849 userAgentChromeWindowsActionPointer->setChecked(true);
851 // Update the user agent menu action text.
852 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
854 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) // Edge on Windows.
856 // Check the Edge on Windows user agent action.
857 userAgentEdgeWindowsActionPointer->setChecked(true);
859 // Update the user agent menu action text.
860 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
862 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) // Safari on macOS.
864 // Check the Safari on macOS user agent action.
865 userAgentSafariMacosActionPointer->setChecked(true);
867 // Update the user agent menu action text.
868 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
870 else // Custom user agent.
872 // Check the user agent.
873 userAgentCustomActionPointer->setChecked(true);
875 // Update the user agent menu action text.
876 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
878 // Set the custom user agent text.
879 userAgentCustomActionPointer->setText(userAgent);
881 // Set the custom user agent flag.
882 customUserAgent = true;
885 // Update the custom user agent enabled boolean.
886 if (updateCustomUserAgentStatus)
887 customUserAgentEnabled = customUserAgent;
890 // Format the custom user agent.
891 if (customUserAgentEnabled)
893 // Enable the custom user agent.
894 userAgentCustomActionPointer->setEnabled(true);
898 // Disable the custom user agent.
899 userAgentCustomActionPointer->setEnabled(false);
901 // Reset the custom user agent text.
902 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
906 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
908 // Set the current zoom factor.
909 currentZoomFactor = zoomFactor;
911 // Update the zoom factor action text, formatting the double with 2 decimal places.
912 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
915 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
917 // Update the search engine label.
918 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
921 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
923 // Update the URL line edit if it does not have focus.
924 if (!urlLineEditPointer->hasFocus())
926 // Get the new URL string.
927 QString newUrlString = newUrl.toString();
929 // Update the URL line edit.
930 urlLineEditPointer->setText(newUrlString);
932 // Set the focus if the new URL is blank.
933 if (newUrlString == QStringLiteral(""))
934 urlLineEditPointer->setFocus();
937 // Store the current URL.
941 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
943 // Update the user agent label.
944 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));