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 "databases/CookiesDatabase.h"
26 #include "dialogs/CookiesDialog.h"
27 #include "dialogs/DomainSettingsDialog.h"
28 #include "helpers/SearchEngineHelper.h"
29 #include "helpers/UserAgentHelper.h"
31 // KDE Frameworks headers.
32 #include <KActionCollection>
35 // Qt toolkit headers.
36 #include <QInputDialog>
37 #include <QNetworkCookie>
42 // Construct the class.
43 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
45 // Initialize the variables.
46 cookieListPointer = new std::list<QNetworkCookie>;
47 javaScriptEnabled = false;
48 localStorageEnabled = false;
50 // Instantiate the main view pointer.
51 browserViewPointer = new BrowserView(this);
53 // Set the main view as the central widget.
54 setCentralWidget(browserViewPointer);
56 // Get a handle for the action collection.
57 KActionCollection *actionCollectionPointer = this->actionCollection();
59 // Add the standard actions.
60 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
61 KStandardAction::print(browserViewPointer, SLOT(print()), actionCollectionPointer);
62 KStandardAction::printPreview(browserViewPointer, SLOT(printPreview()), actionCollectionPointer);
63 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
64 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
65 fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
66 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
67 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
68 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
69 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
71 // Add the custom actions.
72 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
73 userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
74 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
75 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
76 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
77 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
78 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
79 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
80 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
81 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
82 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
83 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
84 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
85 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
86 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
87 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
88 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
89 QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
90 cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
91 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
92 localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
93 domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
95 // Create the action groups
96 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
97 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
99 // Add the actions to the groups.
100 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
101 userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
102 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
103 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
104 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
105 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
106 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
107 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
108 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
109 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
110 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
111 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
112 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
113 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
114 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
115 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
117 // Set some actions to be checkable.
118 javaScriptActionPointer->setCheckable(true);
119 localStorageActionPointer->setCheckable(true);
120 domStorageActionPointer->setCheckable(true);
121 userAgentPrivacyBrowserActionPointer->setCheckable(true);
122 userAgentWebEngineDefaultActionPointer->setCheckable(true);
123 userAgentFirefoxLinuxActionPointer->setCheckable(true);
124 userAgentChromiumLinuxActionPointer->setCheckable(true);
125 userAgentFirefoxWindowsActionPointer->setCheckable(true);
126 userAgentChromeWindowsActionPointer->setCheckable(true);
127 userAgentEdgeWindowsActionPointer->setCheckable(true);
128 userAgentSafariMacosActionPointer->setCheckable(true);
129 userAgentCustomActionPointer->setCheckable(true);
130 searchEngineMojeekActionPointer->setCheckable(true);
131 searchEngineMonoclesActionPointer->setCheckable(true);
132 searchEngineMetagerActionPointer->setCheckable(true);
133 searchEngineGoogleActionPointer->setCheckable(true);
134 searchEngineBingActionPointer->setCheckable(true);
135 searchEngineYahooActionPointer->setCheckable(true);
136 searchEngineCustomActionPointer->setCheckable(true);
138 // Set the action text.
139 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
140 userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
141 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
142 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
143 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
144 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
145 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
146 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
147 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
148 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
149 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
150 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
151 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
152 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
153 domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
154 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
155 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
156 localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
157 domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
159 // Set the action icons.
160 userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
161 userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
162 userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
163 userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
164 userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
165 userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
166 userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
167 userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
168 userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
169 searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
170 searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
171 searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
172 searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
173 searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
174 searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
175 searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
176 zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
177 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
178 cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
179 domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
181 // Update the on-the-fly menus.
182 connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
183 connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
184 connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
186 // Apply the on-the-fly settings when selected.
187 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
188 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
191 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
192 connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
193 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
195 // Connect the URL toolbar actions.
196 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
197 connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
198 connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
200 // Update the URL toolbar actions.
201 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
202 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
203 connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
204 connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
205 connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
207 // Setup the GUI based on the browser_ui.rc file.
208 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
210 // Get lists of the actions' associated widgets.
211 QList<QWidget*> userAgentAssociatedWidgetsPointerList = userAgentPrivacyBrowserActionPointer->associatedWidgets();
212 QList<QWidget*> searchEngineAssociatedWidgetsPointerList = searchEngineMojeekActionPointer->associatedWidgets();
214 // Get the menu widget pointers. It is the second entry, after the main window.
215 QWidget *userAgentMenuWidgetPointer = userAgentAssociatedWidgetsPointerList[1];
216 QWidget *searchEngineMenuWidgetPointer = searchEngineAssociatedWidgetsPointerList[1];
218 // Get the menu pointers.
219 QMenu *userAgentMenuPointer = qobject_cast<QMenu*>(userAgentMenuWidgetPointer);
220 QMenu *searchEngineMenuPointer = qobject_cast<QMenu*>(searchEngineMenuWidgetPointer);
222 // Get the menu actions.
223 userAgentMenuActionPointer = userAgentMenuPointer->menuAction();
224 searchEngineMenuActionPointer = searchEngineMenuPointer->menuAction();
226 // Get handles for the toolbars.
227 navigationToolBarPointer = toolBar(QStringLiteral("navigation_toolbar"));
228 urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
230 // Create a URL line edit.
231 urlLineEditPointer = new KLineEdit();
233 // Add an edit or add domain settings action to the URL line edit.
234 QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
236 // Add or edit the current domain settings.
237 connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
239 // Populate the URL toolbar.
240 urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
242 // Load a new URL from the URL line edit.
243 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
245 // Update the URL line edit on page loads.
246 connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
248 // Get a handle for the status bar.
249 QStatusBar *statusBarPointer = statusBar();
251 // Create a progress bar.
252 progressBarPointer = new QProgressBar();
254 // Add the progress bar to to the status bar.
255 statusBarPointer->addPermanentWidget(progressBarPointer);
257 // Update the status bar with the URL when a link is hovered.
258 connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
260 // Update the progress bar.
261 connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
262 connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
264 // Clear the URL line edit focus when requested.
265 connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
267 // Get the URL line edit palettes.
268 noDomainSettingsPalette = urlLineEditPointer->palette();
269 domainSettingsPalette = urlLineEditPointer->palette();
271 // Modify the domain settings palette.
272 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
274 // Update the applied palette.
275 connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
277 // Process cookie changes.
278 connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
279 connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
281 // Process full screen requests.
282 connect(browserViewPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
284 // Create keyboard shortcuts.
285 QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
286 QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
288 // Connect the keyboard shortcuts to the actions.
289 connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
290 connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
292 // Load the initial website.
293 browserViewPointer->loadInitialWebsite();
296 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
298 //qDebug() << "Add cookie: " << newCookie.toRawForm();
300 // Add the new cookie to the list.
301 cookieListPointer->push_front(newCookie);
303 // Update the action text.
304 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
306 // Update the cookie if it is durable and has new data.
307 if (CookiesDatabase::isUpdate(newCookie))
308 CookiesDatabase::updateCookie(newCookie);
311 void BrowserWindow::addOrEditDomainSettings() const
313 // Remove the focus from the URL line edit.
314 urlLineEditPointer->clearFocus();
316 // Create the domain settings dialog pointer.
317 DomainSettingsDialog *domainSettingsDialogPointer;
319 // Run the commands according to the current domain settings status.
320 if (currentDomainSettingsDomain == "") // Domain settings are not currently applied.
322 // Instruct the domain settings dialog to add a new domain.
323 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
325 else // Domain settings are currently applied.
327 // Instruct the domain settings dialog to edit the current domain.
328 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
331 // Set the dialog window title.
332 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
335 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
338 domainSettingsDialogPointer->show();
340 // Reload the tabs when domain settings are updated.
341 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
344 void BrowserWindow::back() const
346 // Remove the focus from the URL line edit.
347 urlLineEditPointer->clearFocus();
350 browserViewPointer->back();
353 void BrowserWindow::clearUrlLineEditFocus() const
355 // Remove the focus from the URL line edit.
356 urlLineEditPointer->clearFocus();
359 void BrowserWindow::escape() const
361 // Exit full screen browsing if it is enabled.
362 if (fullScreenActionPointer->isChecked())
363 fullScreenActionPointer->trigger();
366 void BrowserWindow::fileNew() const
368 // Display a new instance of Privacy Browser.
369 (new BrowserWindow)->show();
372 void BrowserWindow::forward() const
374 // Remove the focus from the URL line edit.
375 urlLineEditPointer->clearFocus();
378 browserViewPointer->forward();
381 void BrowserWindow::fullScreenRequested(const bool toggleOn)
383 // Toggle full screen mode.
384 if (toggleOn) // Turn full screen mode on.
386 // Set the window to be full screen.
387 fullScreenActionPointer->setFullScreen(window(), true);
389 // Hide all the bars.
390 menuBar()->setVisible(false);
391 navigationToolBarPointer->setVisible(false);
392 urlToolBarPointer->setVisible(false);
393 statusBar()->setVisible(false);
395 else // Turn full screen mode off.
397 // Set the window to not be full screen.
398 fullScreenActionPointer->setFullScreen(window(), false);
400 // Show all the bars.
401 menuBar()->setVisible(true);
402 navigationToolBarPointer->setVisible(true);
403 urlToolBarPointer->setVisible(true);
404 statusBar()->setVisible(true);
408 void BrowserWindow::getZoomFactorFromUser()
410 // Create an OK flag.
413 // 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.
414 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
415 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
416 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
418 // Update the zoom factor if the user clicked OK.
421 // Update the current zoom factor.
422 currentZoomFactor = newZoomFactor;
424 // Set the new zoom factor.
425 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
427 // Update the on-the-fly action text.
428 updateZoomFactorAction(newZoomFactor);
432 void BrowserWindow::home() const
434 // Remove the focus from the URL line edit.
435 urlLineEditPointer->clearFocus();
438 browserViewPointer->home();
441 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
443 // Remove the focus from the URL line edit.
444 urlLineEditPointer->clearFocus();
447 browserViewPointer->loadUrlFromLineEdit(url);
450 void BrowserWindow::openCookiesDialog()
452 // Remove the focus from the URL line edit.
453 urlLineEditPointer->clearFocus();
455 // Instantiate the cookie settings dialog.
456 CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
459 cookiesDialogPointer->show();
461 // Connect the dialog signals.
462 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
463 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
464 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
467 void BrowserWindow::openDomainSettings() const
469 // Remove the focus from the URL line edit.
470 urlLineEditPointer->clearFocus();
472 // Instantiate the domain settings dialog.
473 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
476 domainSettingsDialogPointer->show();
478 // Reload the tabs when domain settings are updated.
479 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
482 void BrowserWindow::refresh() const
484 // Remove the focus from the URL line edit.
485 urlLineEditPointer->clearFocus();
487 // Refresh the web page.
488 browserViewPointer->refresh();
491 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
493 //qDebug() << "Remove cookie: " << cookie.toRawForm();
495 // Remove the cookie from the list.
496 cookieListPointer->remove(cookie);
498 // Update the action text.
499 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
502 void BrowserWindow::showProgressBar(const int &progress) const
504 // Set the progress bar value.
505 progressBarPointer->setValue(progress);
507 // Show the progress bar.
508 progressBarPointer->show();
511 QSize BrowserWindow::sizeHint() const
513 // Return the default window size.
514 return QSize(1500, 1200);
517 void BrowserWindow::settingsConfigure()
519 // Check to make sure the dialog box isn't already displayed.
520 if (KConfigDialog::exists(QStringLiteral("settings")))
522 // Show the existing config dialog if it is hidden.
523 configDialogPointer->show();
525 // Raise the existing config dialog if it is below other windows.
526 configDialogPointer->raise();
528 // Restore the existing config dialog if it has been minimized.
529 if (configDialogPointer->isMinimized()) {
530 configDialogPointer->showNormal();
533 // Activate the existing config dialog, which brings its virtual desktop into focus.
534 configDialogPointer->activateWindow();
538 // Create the settings widgets.
539 QWidget *privacySettingsWidgetPointer = new QWidget;
540 QWidget *generalSettingsWidgetPointer = new QWidget;
542 // Instantiate the settings UI.
543 Ui::PrivacySettings privacySettingsUi;
544 Ui::GeneralSettings generalSettingsUi;
546 // Setup the UI to display the settings widgets.
547 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
548 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
550 // Get handles for the widgets.
551 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
552 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
553 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
554 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
556 // Populate the combo box labels.
557 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
558 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
560 // Update the labels when the combo boxes change.
561 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
562 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
564 // Instantiate a settings config dialog from the settings.kcfg file.
565 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
567 // Add the settings widgets as config dialog pages.
568 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
569 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
571 // Delete the config dialog when it is closed.
572 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
575 configDialogPointer->show();
577 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
578 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
579 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
580 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
581 //configDialogPointer->adjustSize();
583 // Expand the config dialog.
584 configDialogPointer->resize(1000, 500);
586 // Apply the settings when they are updated.
587 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
588 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
592 void BrowserWindow::toggleDomStorage() const
594 // Remove the focus from the URL line edit.
595 urlLineEditPointer->clearFocus();
597 // Toggle DOM storage.
598 browserViewPointer->toggleDomStorage();
601 void BrowserWindow::toggleJavaScript() const
603 // Remove the focus from the URL line edit.
604 urlLineEditPointer->clearFocus();
606 // Toggle JavaScript.
607 browserViewPointer->toggleJavaScript();
610 void BrowserWindow::toggleLocalStorage() const
612 // Remove the focus from the URL line edit.
613 urlLineEditPointer->clearFocus();
615 // Toggle local storage.
616 browserViewPointer->toggleLocalStorage();
619 void BrowserWindow::toggleFullScreen()
621 // Toggle the full screen status.
622 if (fullScreenActionPointer->isChecked()) // Enable full screen browsing mode.
624 // Enable full screen mode.
625 fullScreenActionPointer->setFullScreen(window(), true);
627 // Hide the menu bar if specified.
628 if (Settings::fullScreenHideMenuBar())
629 menuBar()->setVisible(false);
631 // Hide the toolbars if specified.
632 if (Settings::fullScreenHideToolBars())
634 navigationToolBarPointer->setVisible(false);
635 urlToolBarPointer->setVisible(false);
638 // Hide the status bar if specified.
639 if (Settings::fullScreenHideStatusBar())
640 statusBar()->setVisible(false);
642 else // Disable full screen browsing mode.
644 // Disable full screen mode.
645 fullScreenActionPointer->setFullScreen(window(), false);
647 // Show the menu bar.
648 menuBar()->setVisible(true);
650 // Show the toolbars.
651 navigationToolBarPointer->setVisible(true);
652 urlToolBarPointer->setVisible(true);
654 // Show the status bar.
655 statusBar()->setVisible(true);
659 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
661 // Set the action checked status.
662 domStorageActionPointer->setChecked(isEnabled);
665 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
667 // Set the domain palette according to the status.
669 urlLineEditPointer->setPalette(domainSettingsPalette);
671 urlLineEditPointer->setPalette(noDomainSettingsPalette);
674 currentDomainSettingsDomain = domainSettingsDomain;
677 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
679 // Update the JavaScript status.
680 javaScriptEnabled = isEnabled;
682 // Set the icon according to the status.
683 if (javaScriptEnabled)
684 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
686 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
688 // Set the action checked status.
689 javaScriptActionPointer->setChecked(javaScriptEnabled);
691 // Update the status of the DOM storage action.
692 domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
695 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
697 // Update the local storage status.
698 localStorageEnabled = isEnabled;
701 if (localStorageEnabled)
702 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
704 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
706 // Set the action checked status.
707 localStorageActionPointer->setChecked(localStorageEnabled);
709 // Update the status of the DOM storage action.
710 domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
713 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
715 // Initialize the custom search engine flag.
716 bool customSearchEngine = false;
718 if (searchEngine == "Mojeek") // Mojeek.
720 // Check the Mojeek user agent action.
721 searchEngineMojeekActionPointer->setChecked(true);
723 // Update the search engine menu action text.
724 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
726 else if (searchEngine == "Monocles") // Monocles.
728 // Check the Monocles user agent action.
729 searchEngineMonoclesActionPointer->setChecked(true);
731 // Update the search engine menu action text.
732 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
734 else if (searchEngine == "MetaGer") // MetaGer.
736 // Check the MetaGer user agent action.
737 searchEngineMetagerActionPointer->setChecked(true);
739 // Update the search engine menu action text.
740 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
742 else if (searchEngine == "Google") // Google.
744 // Check the Google user agent action.
745 searchEngineGoogleActionPointer->setChecked(true);
747 // Update the search engine menu action text.
748 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
750 else if (searchEngine == "Bing") // Bing.
752 // Check the Bing user agent action.
753 searchEngineBingActionPointer->setChecked(true);
755 // Update the search engine menu action text.
756 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
758 else if (searchEngine == "Yahoo") // Yahoo.
760 // Check the Yahoo user agent action.
761 searchEngineYahooActionPointer->setChecked(true);
763 // Update the search engine menu action text.
764 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
766 else // Custom search engine.
768 // Check the user agent.
769 searchEngineCustomActionPointer->setChecked(true);
771 // Update the search engine menu action text.
772 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
774 // Set the custom search engine text.
775 searchEngineCustomActionPointer->setText(searchEngine);
777 // Set the custom search engine flag.
778 customSearchEngine = true;
781 // Update the custom search engine enabled boolean.
782 if (updateCustomSearchEngineStatus)
783 customSearchEngineEnabled = customSearchEngine;
785 // Format the custom search engine.
786 if (customSearchEngineEnabled)
788 // Enable the custom search engine.
789 searchEngineCustomActionPointer->setEnabled(true);
793 // Disable the custom search engine.
794 searchEngineCustomActionPointer->setEnabled(false);
796 // Reset the custom search engine text.
797 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
801 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
803 // Initialize the custom user agent flag.
804 bool customUserAgent = false;
806 // Check the indicated on-the-fly user agent.
807 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) // Privacy Browser.
809 // Check the Privacy Browser user agent action.
810 userAgentPrivacyBrowserActionPointer->setChecked(true);
812 // Update the user agent menu action text.
813 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
815 else if (userAgent == BrowserView::webEngineDefaultUserAgent) // WebEngine default.
817 // check the WebEngine default user agent action.
818 userAgentWebEngineDefaultActionPointer->setChecked(true);
820 // Update the user agent menu action text.
821 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
823 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) // Firefox on Linux.
825 // Check the Firefox on Linux user agent action.
826 userAgentFirefoxLinuxActionPointer->setChecked(true);
828 // Update the user agent menu action text.
829 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
831 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) // Chromium on Linux.
833 // Check the Chromium on Linux user agent action.
834 userAgentChromiumLinuxActionPointer->setChecked(true);
836 // Update the user agent menu action text.
837 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
839 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) // Firefox on Windows.
841 // Check the Firefox on Windows user agent action.
842 userAgentFirefoxWindowsActionPointer->setChecked(true);
844 // Update the user agent menu action text.
845 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
847 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) // Chrome on Windows.
849 // Check the Chrome on Windows user agent action.
850 userAgentChromeWindowsActionPointer->setChecked(true);
852 // Update the user agent menu action text.
853 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
855 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) // Edge on Windows.
857 // Check the Edge on Windows user agent action.
858 userAgentEdgeWindowsActionPointer->setChecked(true);
860 // Update the user agent menu action text.
861 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
863 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) // Safari on macOS.
865 // Check the Safari on macOS user agent action.
866 userAgentSafariMacosActionPointer->setChecked(true);
868 // Update the user agent menu action text.
869 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
871 else // Custom user agent.
873 // Check the user agent.
874 userAgentCustomActionPointer->setChecked(true);
876 // Update the user agent menu action text.
877 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
879 // Set the custom user agent text.
880 userAgentCustomActionPointer->setText(userAgent);
882 // Set the custom user agent flag.
883 customUserAgent = true;
886 // Update the custom user agent enabled boolean.
887 if (updateCustomUserAgentStatus)
888 customUserAgentEnabled = customUserAgent;
891 // Format the custom user agent.
892 if (customUserAgentEnabled)
894 // Enable the custom user agent.
895 userAgentCustomActionPointer->setEnabled(true);
899 // Disable the custom user agent.
900 userAgentCustomActionPointer->setEnabled(false);
902 // Reset the custom user agent text.
903 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
907 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
909 // Set the current zoom factor.
910 currentZoomFactor = zoomFactor;
912 // Update the zoom factor action text, formatting the double with 2 decimal places.
913 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
916 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
918 // Update the search engine label.
919 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
922 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
924 // Update the URL line edit if it does not have focus.
925 if (!urlLineEditPointer->hasFocus())
927 // Update the URL line edit.
928 urlLineEditPointer->setText(newUrl.toString());
931 // Store the current URL.
935 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
937 // Update the user agent label.
938 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));