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 <QFileDialog>
37 #include <QInputDialog>
38 #include <QNetworkCookie>
43 // Construct the class.
44 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
46 // Initialize the variables.
47 cookieListPointer = new std::list<QNetworkCookie>;
48 javaScriptEnabled = false;
49 localStorageEnabled = false;
51 // Instantiate the main view pointer.
52 browserViewPointer = new BrowserView(this);
54 // Set the main view as the central widget.
55 setCentralWidget(browserViewPointer);
57 // Get a handle for the action collection.
58 KActionCollection *actionCollectionPointer = this->actionCollection();
60 // Add the standard actions.
61 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
62 KStandardAction::print(browserViewPointer, SLOT(print()), actionCollectionPointer);
63 KStandardAction::printPreview(browserViewPointer, SLOT(printPreview()), actionCollectionPointer);
64 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
65 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
66 fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
67 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
68 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
69 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
70 KStandardAction::preferences(this, SLOT(showSettingsDialog()), actionCollectionPointer);
72 // Add the custom actions.
73 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
74 userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
75 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
76 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
77 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
78 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
79 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
80 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
81 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
82 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
83 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
84 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
85 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
86 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
87 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
88 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
89 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
90 QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
91 cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
92 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
93 localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
94 domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
96 // Create the action groups
97 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
98 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
100 // Add the actions to the groups.
101 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
102 userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
103 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
104 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
105 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
106 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
107 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
108 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
109 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
110 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
111 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
112 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
113 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
114 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
115 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
116 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
118 // Set some actions to be checkable.
119 javaScriptActionPointer->setCheckable(true);
120 localStorageActionPointer->setCheckable(true);
121 domStorageActionPointer->setCheckable(true);
122 userAgentPrivacyBrowserActionPointer->setCheckable(true);
123 userAgentWebEngineDefaultActionPointer->setCheckable(true);
124 userAgentFirefoxLinuxActionPointer->setCheckable(true);
125 userAgentChromiumLinuxActionPointer->setCheckable(true);
126 userAgentFirefoxWindowsActionPointer->setCheckable(true);
127 userAgentChromeWindowsActionPointer->setCheckable(true);
128 userAgentEdgeWindowsActionPointer->setCheckable(true);
129 userAgentSafariMacosActionPointer->setCheckable(true);
130 userAgentCustomActionPointer->setCheckable(true);
131 searchEngineMojeekActionPointer->setCheckable(true);
132 searchEngineMonoclesActionPointer->setCheckable(true);
133 searchEngineMetagerActionPointer->setCheckable(true);
134 searchEngineGoogleActionPointer->setCheckable(true);
135 searchEngineBingActionPointer->setCheckable(true);
136 searchEngineYahooActionPointer->setCheckable(true);
137 searchEngineCustomActionPointer->setCheckable(true);
139 // Set the action text.
140 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
141 userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
142 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
143 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
144 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
145 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
146 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
147 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
148 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
149 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
150 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
151 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
152 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
153 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
154 domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
155 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
156 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
157 localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
158 domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
160 // Set the action icons.
161 userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
162 userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
163 userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
164 userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
165 userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
166 userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
167 userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
168 userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
169 userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
170 searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
171 searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
172 searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
173 searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
174 searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
175 searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
176 searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
177 zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
178 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
179 cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
180 domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
182 // Update the on-the-fly menus.
183 connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
184 connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
185 connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
187 // Apply the on-the-fly settings when selected.
188 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
189 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
192 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
193 connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(showCookiesDialog()));
194 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(showDomainSettingsDialog()));
196 // Connect the URL toolbar actions.
197 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
198 connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
199 connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
201 // Update the URL toolbar actions.
202 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
203 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
204 connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
205 connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
206 connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
208 // Setup the GUI based on the browser_ui.rc file.
209 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
211 // Get lists of the actions' associated widgets.
212 QList<QWidget*> userAgentAssociatedWidgetsPointerList = userAgentPrivacyBrowserActionPointer->associatedWidgets();
213 QList<QWidget*> searchEngineAssociatedWidgetsPointerList = searchEngineMojeekActionPointer->associatedWidgets();
215 // Get the menu widget pointers. It is the second entry, after the main window.
216 QWidget *userAgentMenuWidgetPointer = userAgentAssociatedWidgetsPointerList[1];
217 QWidget *searchEngineMenuWidgetPointer = searchEngineAssociatedWidgetsPointerList[1];
219 // Get the menu pointers.
220 QMenu *userAgentMenuPointer = qobject_cast<QMenu*>(userAgentMenuWidgetPointer);
221 QMenu *searchEngineMenuPointer = qobject_cast<QMenu*>(searchEngineMenuWidgetPointer);
223 // Get the menu actions.
224 userAgentMenuActionPointer = userAgentMenuPointer->menuAction();
225 searchEngineMenuActionPointer = searchEngineMenuPointer->menuAction();
227 // Get handles for the toolbars.
228 navigationToolBarPointer = toolBar(QStringLiteral("navigation_toolbar"));
229 urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
231 // Create a URL line edit.
232 urlLineEditPointer = new KLineEdit();
234 // Add an edit or add domain settings action to the URL line edit.
235 QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
237 // Add or edit the current domain settings.
238 connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
240 // Populate the URL toolbar.
241 urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
243 // Load a new URL from the URL line edit.
244 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
246 // Update the URL line edit on page loads.
247 connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
249 // Get a handle for the status bar.
250 QStatusBar *statusBarPointer = statusBar();
252 // Create a progress bar.
253 progressBarPointer = new QProgressBar();
255 // Add the progress bar to to the status bar.
256 statusBarPointer->addPermanentWidget(progressBarPointer);
258 // Update the status bar with the URL when a link is hovered.
259 connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
261 // Update the progress bar.
262 connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
263 connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
265 // Clear the URL line edit focus when requested.
266 connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
268 // Get the URL line edit palettes.
269 noDomainSettingsPalette = urlLineEditPointer->palette();
270 domainSettingsPalette = urlLineEditPointer->palette();
272 // Modify the domain settings palette.
273 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
275 // Update the applied palette.
276 connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
278 // Process cookie changes.
279 connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
280 connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
282 // Process full screen requests.
283 connect(browserViewPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
285 // Create keyboard shortcuts.
286 QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
287 QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
289 // Connect the keyboard shortcuts to the actions.
290 connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
291 connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
293 // Load the initial website.
294 browserViewPointer->loadInitialWebsite();
297 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
299 //qDebug() << "Add cookie: " << newCookie.toRawForm();
301 // Add the new cookie to the list.
302 cookieListPointer->push_front(newCookie);
304 // Update the action text.
305 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
307 // Update the cookie if it is durable and has new data.
308 if (CookiesDatabase::isUpdate(newCookie))
309 CookiesDatabase::updateCookie(newCookie);
312 void BrowserWindow::addOrEditDomainSettings() const
314 // Remove the focus from the URL line edit.
315 urlLineEditPointer->clearFocus();
317 // Create the domain settings dialog pointer.
318 DomainSettingsDialog *domainSettingsDialogPointer;
320 // Run the commands according to the current domain settings status.
321 if (currentDomainSettingsDomain == "") // Domain settings are not currently applied.
323 // Instruct the domain settings dialog to add a new domain.
324 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
326 else // Domain settings are currently applied.
328 // Instruct the domain settings dialog to edit the current domain.
329 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
332 // Set the dialog window title.
333 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
336 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
339 domainSettingsDialogPointer->show();
341 // Reload the tabs when domain settings are updated.
342 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
345 void BrowserWindow::back() const
347 // Remove the focus from the URL line edit.
348 urlLineEditPointer->clearFocus();
351 browserViewPointer->back();
354 void BrowserWindow::clearUrlLineEditFocus() const
356 // Remove the focus from the URL line edit.
357 urlLineEditPointer->clearFocus();
360 void BrowserWindow::escape() const
362 // Exit full screen browsing if it is enabled.
363 if (fullScreenActionPointer->isChecked())
364 fullScreenActionPointer->trigger();
367 void BrowserWindow::fileNew() const
369 // Display a new instance of Privacy Browser.
370 (new BrowserWindow)->show();
373 void BrowserWindow::forward() const
375 // Remove the focus from the URL line edit.
376 urlLineEditPointer->clearFocus();
379 browserViewPointer->forward();
382 void BrowserWindow::fullScreenRequested(const bool toggleOn)
384 // Toggle full screen mode.
385 if (toggleOn) // Turn full screen mode on.
387 // Set the window to be full screen.
388 fullScreenActionPointer->setFullScreen(window(), true);
390 // Hide all the bars.
391 menuBar()->setVisible(false);
392 navigationToolBarPointer->setVisible(false);
393 urlToolBarPointer->setVisible(false);
394 statusBar()->setVisible(false);
396 else // Turn full screen mode off.
398 // Set the window to not be full screen.
399 fullScreenActionPointer->setFullScreen(window(), false);
401 // Show all the bars.
402 menuBar()->setVisible(true);
403 navigationToolBarPointer->setVisible(true);
404 urlToolBarPointer->setVisible(true);
405 statusBar()->setVisible(true);
409 void BrowserWindow::getZoomFactorFromUser()
411 // Create an OK flag.
414 // 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.
415 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
416 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
417 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
419 // Update the zoom factor if the user clicked OK.
422 // Update the current zoom factor.
423 currentZoomFactor = newZoomFactor;
425 // Set the new zoom factor.
426 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
428 // Update the on-the-fly action text.
429 updateZoomFactorAction(newZoomFactor);
433 void BrowserWindow::home() const
435 // Remove the focus from the URL line edit.
436 urlLineEditPointer->clearFocus();
439 browserViewPointer->home();
442 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
444 // Remove the focus from the URL line edit.
445 urlLineEditPointer->clearFocus();
448 browserViewPointer->loadUrlFromLineEdit(url);
451 void BrowserWindow::refresh() const
453 // Remove the focus from the URL line edit.
454 urlLineEditPointer->clearFocus();
456 // Refresh the web page.
457 browserViewPointer->refresh();
460 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
462 //qDebug() << "Remove cookie: " << cookie.toRawForm();
464 // Remove the cookie from the list.
465 cookieListPointer->remove(cookie);
467 // Update the action text.
468 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
471 void BrowserWindow::showCookiesDialog()
473 // Remove the focus from the URL line edit.
474 urlLineEditPointer->clearFocus();
476 // Instantiate the cookie settings dialog.
477 CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
480 cookiesDialogPointer->show();
482 // Connect the dialog signals.
483 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
484 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
485 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
488 void BrowserWindow::showDownloadLocationBrowseDialog() const
490 // Get the current download location.
491 QString currentDownloadLocation = downloadLocationComboBoxPointer->currentText();
493 // Resolve the system download directory if specified.
494 if (currentDownloadLocation == QStringLiteral("System Download Directory"))
495 currentDownloadLocation = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
497 // Get the new download location.
498 QString newDownloadLocation = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download location dialog caption", "Select Download Location"), currentDownloadLocation);
500 // Populate the download location combo box according to the new download location.
501 if (newDownloadLocation == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)) // The default download location was selected.
503 // Populate the download location with the default text.
504 downloadLocationComboBoxPointer->setCurrentText("System Download Directory");
506 else if (newDownloadLocation != QStringLiteral("")) // A different directory was selected.
508 // Populate the download location.
509 downloadLocationComboBoxPointer->setCurrentText(newDownloadLocation);
513 void BrowserWindow::showDomainSettingsDialog() const
515 // Remove the focus from the URL line edit.
516 urlLineEditPointer->clearFocus();
518 // Instantiate the domain settings dialog.
519 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
522 domainSettingsDialogPointer->show();
524 // Reload the tabs when domain settings are updated.
525 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
528 void BrowserWindow::showProgressBar(const int &progress) const
530 // Set the progress bar value.
531 progressBarPointer->setValue(progress);
533 // Show the progress bar.
534 progressBarPointer->show();
537 void BrowserWindow::showSettingsDialog()
539 // Create the settings widgets.
540 QWidget *privacySettingsWidgetPointer = new QWidget;
541 QWidget *generalSettingsWidgetPointer = new QWidget;
543 // Instantiate the settings UI.
544 Ui::PrivacySettings privacySettingsUi;
545 Ui::GeneralSettings generalSettingsUi;
547 // Setup the UI to display the settings widgets.
548 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
549 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
551 // Get handles for the widgets.
552 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
553 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
554 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
555 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
556 downloadLocationComboBoxPointer = generalSettingsUi.kcfg_downloadLocation;
557 QPushButton *browseButtonPointer = generalSettingsUi.browseButton;
559 // Populate the combo box labels.
560 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
561 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
563 // Update the labels when the combo boxes change.
564 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
565 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
567 // Connect the download location directory browse button.
568 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadLocationBrowseDialog()));
570 // Instantiate a settings config dialog from the settings.kcfg file.
571 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
573 // Add the settings widgets as config dialog pages.
574 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
575 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
577 // Prevent interaction with the parent window while the dialog is open.
578 configDialogPointer->setWindowModality(Qt::WindowModal);
581 configDialogPointer->show();
583 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
584 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
585 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
586 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
587 //configDialogPointer->adjustSize();
589 // Expand the config dialog.
590 configDialogPointer->resize(1000, 500);
592 // Apply the settings when they are updated.
593 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
594 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
597 QSize BrowserWindow::sizeHint() const
599 // Return the default window size.
600 return QSize(1500, 1200);
603 void BrowserWindow::toggleDomStorage() const
605 // Remove the focus from the URL line edit.
606 urlLineEditPointer->clearFocus();
608 // Toggle DOM storage.
609 browserViewPointer->toggleDomStorage();
612 void BrowserWindow::toggleJavaScript() const
614 // Remove the focus from the URL line edit.
615 urlLineEditPointer->clearFocus();
617 // Toggle JavaScript.
618 browserViewPointer->toggleJavaScript();
621 void BrowserWindow::toggleLocalStorage() const
623 // Remove the focus from the URL line edit.
624 urlLineEditPointer->clearFocus();
626 // Toggle local storage.
627 browserViewPointer->toggleLocalStorage();
630 void BrowserWindow::toggleFullScreen()
632 // Toggle the full screen status.
633 if (fullScreenActionPointer->isChecked()) // Enable full screen browsing mode.
635 // Enable full screen mode.
636 fullScreenActionPointer->setFullScreen(window(), true);
638 // Hide the menu bar if specified.
639 if (Settings::fullScreenHideMenuBar())
640 menuBar()->setVisible(false);
642 // Hide the toolbars if specified.
643 if (Settings::fullScreenHideToolBars())
645 navigationToolBarPointer->setVisible(false);
646 urlToolBarPointer->setVisible(false);
649 // Hide the status bar if specified.
650 if (Settings::fullScreenHideStatusBar())
651 statusBar()->setVisible(false);
653 else // Disable full screen browsing mode.
655 // Disable full screen mode.
656 fullScreenActionPointer->setFullScreen(window(), false);
658 // Show the menu bar.
659 menuBar()->setVisible(true);
661 // Show the toolbars.
662 navigationToolBarPointer->setVisible(true);
663 urlToolBarPointer->setVisible(true);
665 // Show the status bar.
666 statusBar()->setVisible(true);
670 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
672 // Set the action checked status.
673 domStorageActionPointer->setChecked(isEnabled);
676 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
678 // Set the domain palette according to the status.
680 urlLineEditPointer->setPalette(domainSettingsPalette);
682 urlLineEditPointer->setPalette(noDomainSettingsPalette);
685 currentDomainSettingsDomain = domainSettingsDomain;
688 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
690 // Update the JavaScript status.
691 javaScriptEnabled = isEnabled;
693 // Set the icon according to the status.
694 if (javaScriptEnabled)
695 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
697 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
699 // Set the action checked status.
700 javaScriptActionPointer->setChecked(javaScriptEnabled);
702 // Update the status of the DOM storage action.
703 domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
706 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
708 // Update the local storage status.
709 localStorageEnabled = isEnabled;
712 if (localStorageEnabled)
713 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
715 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
717 // Set the action checked status.
718 localStorageActionPointer->setChecked(localStorageEnabled);
720 // Update the status of the DOM storage action.
721 domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
724 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
726 // Initialize the custom search engine flag.
727 bool customSearchEngine = false;
729 if (searchEngine == "Mojeek") // Mojeek.
731 // Check the Mojeek user agent action.
732 searchEngineMojeekActionPointer->setChecked(true);
734 // Update the search engine menu action text.
735 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
737 else if (searchEngine == "Monocles") // Monocles.
739 // Check the Monocles user agent action.
740 searchEngineMonoclesActionPointer->setChecked(true);
742 // Update the search engine menu action text.
743 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
745 else if (searchEngine == "MetaGer") // MetaGer.
747 // Check the MetaGer user agent action.
748 searchEngineMetagerActionPointer->setChecked(true);
750 // Update the search engine menu action text.
751 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
753 else if (searchEngine == "Google") // Google.
755 // Check the Google user agent action.
756 searchEngineGoogleActionPointer->setChecked(true);
758 // Update the search engine menu action text.
759 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
761 else if (searchEngine == "Bing") // Bing.
763 // Check the Bing user agent action.
764 searchEngineBingActionPointer->setChecked(true);
766 // Update the search engine menu action text.
767 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
769 else if (searchEngine == "Yahoo") // Yahoo.
771 // Check the Yahoo user agent action.
772 searchEngineYahooActionPointer->setChecked(true);
774 // Update the search engine menu action text.
775 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
777 else // Custom search engine.
779 // Check the user agent.
780 searchEngineCustomActionPointer->setChecked(true);
782 // Update the search engine menu action text.
783 searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
785 // Set the custom search engine text.
786 searchEngineCustomActionPointer->setText(searchEngine);
788 // Set the custom search engine flag.
789 customSearchEngine = true;
792 // Update the custom search engine enabled boolean.
793 if (updateCustomSearchEngineStatus)
794 customSearchEngineEnabled = customSearchEngine;
796 // Format the custom search engine.
797 if (customSearchEngineEnabled)
799 // Enable the custom search engine.
800 searchEngineCustomActionPointer->setEnabled(true);
804 // Disable the custom search engine.
805 searchEngineCustomActionPointer->setEnabled(false);
807 // Reset the custom search engine text.
808 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
812 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
814 // Initialize the custom user agent flag.
815 bool customUserAgent = false;
817 // Check the indicated on-the-fly user agent.
818 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) // Privacy Browser.
820 // Check the Privacy Browser user agent action.
821 userAgentPrivacyBrowserActionPointer->setChecked(true);
823 // Update the user agent menu action text.
824 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
826 else if (userAgent == BrowserView::webEngineDefaultUserAgent) // WebEngine default.
828 // check the WebEngine default user agent action.
829 userAgentWebEngineDefaultActionPointer->setChecked(true);
831 // Update the user agent menu action text.
832 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
834 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) // Firefox on Linux.
836 // Check the Firefox on Linux user agent action.
837 userAgentFirefoxLinuxActionPointer->setChecked(true);
839 // Update the user agent menu action text.
840 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
842 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) // Chromium on Linux.
844 // Check the Chromium on Linux user agent action.
845 userAgentChromiumLinuxActionPointer->setChecked(true);
847 // Update the user agent menu action text.
848 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
850 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) // Firefox on Windows.
852 // Check the Firefox on Windows user agent action.
853 userAgentFirefoxWindowsActionPointer->setChecked(true);
855 // Update the user agent menu action text.
856 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
858 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) // Chrome on Windows.
860 // Check the Chrome on Windows user agent action.
861 userAgentChromeWindowsActionPointer->setChecked(true);
863 // Update the user agent menu action text.
864 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
866 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) // Edge on Windows.
868 // Check the Edge on Windows user agent action.
869 userAgentEdgeWindowsActionPointer->setChecked(true);
871 // Update the user agent menu action text.
872 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
874 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) // Safari on macOS.
876 // Check the Safari on macOS user agent action.
877 userAgentSafariMacosActionPointer->setChecked(true);
879 // Update the user agent menu action text.
880 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
882 else // Custom user agent.
884 // Check the user agent.
885 userAgentCustomActionPointer->setChecked(true);
887 // Update the user agent menu action text.
888 userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
890 // Set the custom user agent text.
891 userAgentCustomActionPointer->setText(userAgent);
893 // Set the custom user agent flag.
894 customUserAgent = true;
897 // Update the custom user agent enabled boolean.
898 if (updateCustomUserAgentStatus)
899 customUserAgentEnabled = customUserAgent;
902 // Format the custom user agent.
903 if (customUserAgentEnabled)
905 // Enable the custom user agent.
906 userAgentCustomActionPointer->setEnabled(true);
910 // Disable the custom user agent.
911 userAgentCustomActionPointer->setEnabled(false);
913 // Reset the custom user agent text.
914 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
918 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
920 // Set the current zoom factor.
921 currentZoomFactor = zoomFactor;
923 // Update the zoom factor action text, formatting the double with 2 decimal places.
924 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
927 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
929 // Update the search engine label.
930 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
933 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
935 // Update the URL line edit if it does not have focus.
936 if (!urlLineEditPointer->hasFocus())
938 // Update the URL line edit.
939 urlLineEditPointer->setText(newUrl.toString());
942 // Store the current URL.
946 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
948 // Update the user agent label.
949 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));