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::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
62 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
63 fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
64 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
65 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
66 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
67 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
69 // Add the custom actions.
70 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
71 userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
72 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
73 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
74 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
75 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
76 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
77 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
78 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
79 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
80 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
81 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
82 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
83 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
84 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
85 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
86 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
87 QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
88 cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
89 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
90 localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
91 domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
93 // Create the action groups
94 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
95 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
97 // Add the actions to the groups.
98 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
99 userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
100 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
101 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
102 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
103 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
104 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
105 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
106 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
107 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
108 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
109 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
110 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
111 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
112 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
113 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
115 // Set some actions to be checkable.
116 javaScriptActionPointer->setCheckable(true);
117 localStorageActionPointer->setCheckable(true);
118 domStorageActionPointer->setCheckable(true);
119 userAgentPrivacyBrowserActionPointer->setCheckable(true);
120 userAgentWebEngineDefaultActionPointer->setCheckable(true);
121 userAgentFirefoxLinuxActionPointer->setCheckable(true);
122 userAgentChromiumLinuxActionPointer->setCheckable(true);
123 userAgentFirefoxWindowsActionPointer->setCheckable(true);
124 userAgentChromeWindowsActionPointer->setCheckable(true);
125 userAgentEdgeWindowsActionPointer->setCheckable(true);
126 userAgentSafariMacosActionPointer->setCheckable(true);
127 userAgentCustomActionPointer->setCheckable(true);
128 searchEngineMojeekActionPointer->setCheckable(true);
129 searchEngineMonoclesActionPointer->setCheckable(true);
130 searchEngineMetagerActionPointer->setCheckable(true);
131 searchEngineGoogleActionPointer->setCheckable(true);
132 searchEngineBingActionPointer->setCheckable(true);
133 searchEngineYahooActionPointer->setCheckable(true);
134 searchEngineCustomActionPointer->setCheckable(true);
136 // Set the action text.
137 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
138 userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
139 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
140 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
141 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
142 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
143 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
144 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
145 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
146 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
147 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
148 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
149 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
150 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
151 domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
152 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
153 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
154 localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
155 domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
157 // Set the action icons.
158 userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
159 userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
160 userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
161 userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
162 userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
163 userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
164 userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
165 userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
166 userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
167 searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
168 searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
169 searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
170 searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
171 searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
172 searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
173 searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
174 zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
175 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
176 cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
177 domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
179 // Update the on-the-fly menus.
180 connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
181 connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
182 connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
184 // Apply the on-the-fly settings when selected.
185 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
186 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
189 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
190 connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
191 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
193 // Connect the URL toolbar actions.
194 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
195 connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
196 connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
198 // Update the URL toolbar actions.
199 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
200 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
201 connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
202 connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
203 connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
205 // Setup the GUI based on the browser_ui.rc file.
206 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
208 // Get lists of the actions' associated widgets.
209 QList<QWidget*> userAgentAssociatedWidgetsPointerList = userAgentPrivacyBrowserActionPointer->associatedWidgets();
210 QList<QWidget*> searchEngineAssociatedWidgetsPointerList = searchEngineMojeekActionPointer->associatedWidgets();
212 // Get the menu widget pointers. It is the second entry, after the main window.
213 QWidget *userAgentMenuWidgetPointer = userAgentAssociatedWidgetsPointerList[1];
214 QWidget *searchEngineMenuWidgetPointer = searchEngineAssociatedWidgetsPointerList[1];
216 // Get the menu pointers.
217 QMenu *userAgentMenuPointer = qobject_cast<QMenu*>(userAgentMenuWidgetPointer);
218 QMenu *searchEngineMenuPointer = qobject_cast<QMenu*>(searchEngineMenuWidgetPointer);
220 // Get the menu actions.
221 userAgentMenuActionPointer = userAgentMenuPointer->menuAction();
222 searchEngineMenuActionPointer = searchEngineMenuPointer->menuAction();
224 // Get handles for the toolbars.
225 navigationToolBarPointer = toolBar(QStringLiteral("navigation_toolbar"));
226 urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
228 // Create a URL line edit.
229 urlLineEditPointer = new KLineEdit();
231 // Add an edit or add domain settings action to the URL line edit.
232 QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
234 // Add or edit the current domain settings.
235 connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
237 // Populate the URL toolbar.
238 urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
240 // Load a new URL from the URL line edit.
241 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
243 // Update the URL line edit on page loads.
244 connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
246 // Get a handle for the status bar.
247 QStatusBar *statusBarPointer = statusBar();
249 // Create a progress bar.
250 progressBarPointer = new QProgressBar();
252 // Add the progress bar to to the status bar.
253 statusBarPointer->addPermanentWidget(progressBarPointer);
255 // Update the status bar with the URL when a link is hovered.
256 connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
258 // Update the progress bar.
259 connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
260 connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
262 // Clear the URL line edit focus when requested.
263 connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
265 // Get the URL line edit palettes.
266 noDomainSettingsPalette = urlLineEditPointer->palette();
267 domainSettingsPalette = urlLineEditPointer->palette();
269 // Modify the domain settings palette.
270 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
272 // Update the applied palette.
273 connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
275 // Process cookie changes.
276 connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
277 connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
279 // Process full screen requests.
280 connect(browserViewPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
282 // Create keyboard shortcuts.
283 QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
284 QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
286 // Connect the keyboard shortcuts to the actions.
287 connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
288 connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
290 // Load the initial website.
291 browserViewPointer->loadInitialWebsite();
294 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
296 //qDebug() << "Add cookie: " << newCookie.toRawForm();
298 // Add the new cookie to the list.
299 cookieListPointer->push_front(newCookie);
301 // Update the action text.
302 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
304 // Update the cookie if it is durable and has new data.
305 if (CookiesDatabase::isUpdate(newCookie))
306 CookiesDatabase::updateCookie(newCookie);
309 void BrowserWindow::addOrEditDomainSettings() const
311 // Remove the focus from the URL line edit.
312 urlLineEditPointer->clearFocus();
314 // Create the domain settings dialog pointer.
315 DomainSettingsDialog *domainSettingsDialogPointer;
317 // Run the commands according to the current domain settings status.
318 if (currentDomainSettingsDomain == "") // Domain settings are not currently applied.
320 // Instruct the domain settings dialog to add a new domain.
321 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
323 else // Domain settings are currently applied.
325 // Instruct the domain settings dialog to edit the current domain.
326 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
329 // Set the dialog window title.
330 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
333 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
336 domainSettingsDialogPointer->show();
338 // Reload the tabs when domain settings are updated.
339 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
342 void BrowserWindow::back() const
344 // Remove the focus from the URL line edit.
345 urlLineEditPointer->clearFocus();
348 browserViewPointer->back();
351 void BrowserWindow::clearUrlLineEditFocus() const
353 // Remove the focus from the URL line edit.
354 urlLineEditPointer->clearFocus();
357 void BrowserWindow::escape() const
359 // Exit full screen browsing if it is enabled.
360 if (fullScreenActionPointer->isChecked())
361 fullScreenActionPointer->trigger();
364 void BrowserWindow::fileNew() const
366 // Display a new instance of Privacy Browser.
367 (new BrowserWindow)->show();
370 void BrowserWindow::forward() const
372 // Remove the focus from the URL line edit.
373 urlLineEditPointer->clearFocus();
376 browserViewPointer->forward();
379 void BrowserWindow::fullScreenRequested(const bool toggleOn)
381 // Toggle full screen mode.
382 if (toggleOn) // Turn full screen mode on.
384 // Set the window to be full screen.
385 fullScreenActionPointer->setFullScreen(window(), true);
387 // Hide all the bars.
388 menuBar()->setVisible(false);
389 navigationToolBarPointer->setVisible(false);
390 urlToolBarPointer->setVisible(false);
391 statusBar()->setVisible(false);
393 else // Turn full screen mode off.
395 // Set the window to not be full screen.
396 fullScreenActionPointer->setFullScreen(window(), false);
398 // Show all the bars.
399 menuBar()->setVisible(true);
400 navigationToolBarPointer->setVisible(true);
401 urlToolBarPointer->setVisible(true);
402 statusBar()->setVisible(true);
406 void BrowserWindow::getZoomFactorFromUser()
408 // Create an OK flag.
411 // 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.
412 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
413 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
414 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
416 // Update the zoom factor if the user clicked OK.
419 // Update the current zoom factor.
420 currentZoomFactor = newZoomFactor;
422 // Set the new zoom factor.
423 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
425 // Update the on-the-fly action text.
426 updateZoomFactorAction(newZoomFactor);
430 void BrowserWindow::home() const
432 // Remove the focus from the URL line edit.
433 urlLineEditPointer->clearFocus();
436 browserViewPointer->home();
439 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
441 // Remove the focus from the URL line edit.
442 urlLineEditPointer->clearFocus();
445 browserViewPointer->loadUrlFromLineEdit(url);
448 void BrowserWindow::openCookiesDialog()
450 // Remove the focus from the URL line edit.
451 urlLineEditPointer->clearFocus();
453 // Instantiate the cookie settings dialog.
454 CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
457 cookiesDialogPointer->show();
459 // Connect the dialog signals.
460 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
461 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
462 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
465 void BrowserWindow::openDomainSettings() const
467 // Remove the focus from the URL line edit.
468 urlLineEditPointer->clearFocus();
470 // Instantiate the domain settings dialog.
471 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
474 domainSettingsDialogPointer->show();
476 // Reload the tabs when domain settings are updated.
477 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
480 void BrowserWindow::refresh() const
482 // Remove the focus from the URL line edit.
483 urlLineEditPointer->clearFocus();
485 // Refresh the web page.
486 browserViewPointer->refresh();
489 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
491 //qDebug() << "Remove cookie: " << cookie.toRawForm();
493 // Remove the cookie from the list.
494 cookieListPointer->remove(cookie);
496 // Update the action text.
497 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
500 void BrowserWindow::showProgressBar(const int &progress) const
502 // Set the progress bar value.
503 progressBarPointer->setValue(progress);
505 // Show the progress bar.
506 progressBarPointer->show();
509 QSize BrowserWindow::sizeHint() const
511 // Return the default window size.
512 return QSize(1500, 1200);
515 void BrowserWindow::settingsConfigure()
517 // Check to make sure the dialog box isn't already displayed.
518 if (KConfigDialog::exists(QStringLiteral("settings")))
520 // Show the existing config dialog if it is hidden.
521 configDialogPointer->show();
523 // Raise the existing config dialog if it is below other windows.
524 configDialogPointer->raise();
526 // Restore the existing config dialog if it has been minimized.
527 if (configDialogPointer->isMinimized()) {
528 configDialogPointer->showNormal();
531 // Activate the existing config dialog, which brings its virtual desktop into focus.
532 configDialogPointer->activateWindow();
536 // Create the settings widgets.
537 QWidget *privacySettingsWidgetPointer = new QWidget;
538 QWidget *generalSettingsWidgetPointer = new QWidget;
540 // Instantiate the settings UI.
541 Ui::PrivacySettings privacySettingsUi;
542 Ui::GeneralSettings generalSettingsUi;
544 // Setup the UI to display the settings widgets.
545 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
546 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
548 // Get handles for the widgets.
549 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
550 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
551 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
552 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
554 // Populate the combo box labels.
555 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
556 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
558 // Update the labels when the combo boxes change.
559 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
560 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
562 // Instantiate a settings config dialog from the settings.kcfg file.
563 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
565 // Add the settings widgets as config dialog pages.
566 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
567 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
569 // Delete the config dialog when it is closed.
570 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
573 configDialogPointer->show();
575 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
576 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
577 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
578 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
579 //configDialogPointer->adjustSize();
581 // Expand the config dialog.
582 configDialogPointer->resize(1000, 500);
584 // Apply the settings when they are updated.
585 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
586 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
590 void BrowserWindow::toggleDomStorage() const
592 // Remove the focus from the URL line edit.
593 urlLineEditPointer->clearFocus();
595 // Toggle DOM storage.
596 browserViewPointer->toggleDomStorage();
599 void BrowserWindow::toggleJavaScript() const
601 // Remove the focus from the URL line edit.
602 urlLineEditPointer->clearFocus();
604 // Toggle JavaScript.
605 browserViewPointer->toggleJavaScript();
608 void BrowserWindow::toggleLocalStorage() const
610 // Remove the focus from the URL line edit.
611 urlLineEditPointer->clearFocus();
613 // Toggle local storage.
614 browserViewPointer->toggleLocalStorage();
617 void BrowserWindow::toggleFullScreen()
619 // Toggle the full screen status.
620 if (fullScreenActionPointer->isChecked()) // Enable full screen browsing mode.
622 // Enable full screen mode.
623 fullScreenActionPointer->setFullScreen(window(), true);
625 // Hide the menu bar if specified.
626 if (Settings::fullScreenHideMenuBar())
627 menuBar()->setVisible(false);
629 // Hide the toolbars if specified.
630 if (Settings::fullScreenHideToolBars())
632 navigationToolBarPointer->setVisible(false);
633 urlToolBarPointer->setVisible(false);
636 // Hide the status bar if specified.
637 if (Settings::fullScreenHideStatusBar())
638 statusBar()->setVisible(false);
640 else // Disable full screen browsing mode.
642 // Disable full screen mode.
643 fullScreenActionPointer->setFullScreen(window(), false);
645 // Show the menu bar.
646 menuBar()->setVisible(true);
648 // Show the toolbars.
649 navigationToolBarPointer->setVisible(true);
650 urlToolBarPointer->setVisible(true);
652 // Show the status bar.
653 statusBar()->setVisible(true);
657 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
659 // Set the action checked status.
660 domStorageActionPointer->setChecked(isEnabled);
663 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
665 // Set the domain palette according to the status.
667 urlLineEditPointer->setPalette(domainSettingsPalette);
669 urlLineEditPointer->setPalette(noDomainSettingsPalette);
672 currentDomainSettingsDomain = domainSettingsDomain;
675 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
677 // Update the JavaScript status.
678 javaScriptEnabled = isEnabled;
680 // Set the icon according to the status.
681 if (javaScriptEnabled)
682 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
684 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
686 // Set the action checked status.
687 javaScriptActionPointer->setChecked(javaScriptEnabled);
689 // Update the status of the DOM storage action.
690 domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
693 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
695 // Update the local storage status.
696 localStorageEnabled = isEnabled;
699 if (localStorageEnabled)
700 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
702 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
704 // Set the action checked status.
705 localStorageActionPointer->setChecked(localStorageEnabled);
707 // Update the status of the DOM storage action.
708 domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
711 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
713 // Initialize the custom search engine flag.
714 bool customSearchEngine = false;
716 if (searchEngine == "Mojeek") // Mojeek.
718 // Check the Mojeek user agent action.
719 searchEngineMojeekActionPointer->setChecked(true);
721 // Update the search engine menu action text.
722 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
724 else if (searchEngine == "Monocles") // Monocles.
726 // Check the Monocles user agent action.
727 searchEngineMonoclesActionPointer->setChecked(true);
729 // Update the search engine menu action text.
730 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
732 else if (searchEngine == "MetaGer") // MetaGer.
734 // Check the MetaGer user agent action.
735 searchEngineMetagerActionPointer->setChecked(true);
737 // Update the search engine menu action text.
738 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
740 else if (searchEngine == "Google") // Google.
742 // Check the Google user agent action.
743 searchEngineGoogleActionPointer->setChecked(true);
745 // Update the search engine menu action text.
746 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
748 else if (searchEngine == "Bing") // Bing.
750 // Check the Bing user agent action.
751 searchEngineBingActionPointer->setChecked(true);
753 // Update the search engine menu action text.
754 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
756 else if (searchEngine == "Yahoo") // Yahoo.
758 // Check the Yahoo user agent action.
759 searchEngineYahooActionPointer->setChecked(true);
761 // Update the search engine menu action text.
762 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
764 else // Custom search engine.
766 // Check the user agent.
767 searchEngineCustomActionPointer->setChecked(true);
769 // Update the search engine menu action text.
770 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
772 // Set the custom search engine text.
773 searchEngineCustomActionPointer->setText(searchEngine);
775 // Set the custom search engine flag.
776 customSearchEngine = true;
779 // Update the custom search engine enabled boolean.
780 if (updateCustomSearchEngineStatus)
781 customSearchEngineEnabled = customSearchEngine;
783 // Format the custom search engine.
784 if (customSearchEngineEnabled)
786 // Enable the custom search engine.
787 searchEngineCustomActionPointer->setEnabled(true);
791 // Disable the custom search engine.
792 searchEngineCustomActionPointer->setEnabled(false);
794 // Reset the custom search engine text.
795 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
799 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
801 // Initialize the custom user agent flag.
802 bool customUserAgent = false;
804 // Check the indicated on-the-fly user agent.
805 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) // Privacy Browser.
807 // Check the Privacy Browser user agent action.
808 userAgentPrivacyBrowserActionPointer->setChecked(true);
810 // Update the user agent menu action text.
811 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
813 else if (userAgent == BrowserView::webEngineDefaultUserAgent) // WebEngine default.
815 // check the WebEngine default user agent action.
816 userAgentWebEngineDefaultActionPointer->setChecked(true);
818 // Update the user agent menu action text.
819 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
821 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) // Firefox on Linux.
823 // Check the Firefox on Linux user agent action.
824 userAgentFirefoxLinuxActionPointer->setChecked(true);
826 // Update the user agent menu action text.
827 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
829 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) // Chromium on Linux.
831 // Check the Chromium on Linux user agent action.
832 userAgentChromiumLinuxActionPointer->setChecked(true);
834 // Update the user agent menu action text.
835 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
837 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) // Firefox on Windows.
839 // Check the Firefox on Windows user agent action.
840 userAgentFirefoxWindowsActionPointer->setChecked(true);
842 // Update the user agent menu action text.
843 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
845 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) // Chrome on Windows.
847 // Check the Chrome on Windows user agent action.
848 userAgentChromeWindowsActionPointer->setChecked(true);
850 // Update the user agent menu action text.
851 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
853 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) // Edge on Windows.
855 // Check the Edge on Windows user agent action.
856 userAgentEdgeWindowsActionPointer->setChecked(true);
858 // Update the user agent menu action text.
859 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
861 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) // Safari on macOS.
863 // Check the Safari on macOS user agent action.
864 userAgentSafariMacosActionPointer->setChecked(true);
866 // Update the user agent menu action text.
867 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
869 else // Custom user agent.
871 // Check the user agent.
872 userAgentCustomActionPointer->setChecked(true);
874 // Update the user agent menu action text.
875 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
877 // Set the custom user agent text.
878 userAgentCustomActionPointer->setText(userAgent);
880 // Set the custom user agent flag.
881 customUserAgent = true;
884 // Update the custom user agent enabled boolean.
885 if (updateCustomUserAgentStatus)
886 customUserAgentEnabled = customUserAgent;
889 // Format the custom user agent.
890 if (customUserAgentEnabled)
892 // Enable the custom user agent.
893 userAgentCustomActionPointer->setEnabled(true);
897 // Disable the custom user agent.
898 userAgentCustomActionPointer->setEnabled(false);
900 // Reset the custom user agent text.
901 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
905 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
907 // Set the current zoom factor.
908 currentZoomFactor = zoomFactor;
910 // Update the zoom factor action text, formatting the double with 2 decimal places.
911 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
914 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
916 // Update the search engine label.
917 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
920 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
922 // Update the URL line edit if it does not have focus.
923 if (!urlLineEditPointer->hasFocus())
925 // Update the URL line edit.
926 urlLineEditPointer->setText(newUrl.toString());
929 // Store the current URL.
933 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
935 // Update the user agent label.
936 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));