2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "BrowserWindow.h"
23 #include "ui_SettingsPrivacy.h"
24 #include "ui_SettingsGeneral.h"
25 #include "dialogs/CookiesDialog.h"
26 #include "dialogs/DomainSettingsDialog.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
30 // KDE Frameworks headers.
31 #include <KActionCollection>
34 // Qt toolkit headers.
35 #include <QInputDialog>
36 #include <QNetworkCookie>
39 // Construct the class.
40 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
42 // Initialize the variables.
43 cookieListPointer = new std::list<QNetworkCookie>;
44 javaScriptEnabled = false;
45 localStorageEnabled = false;
47 // Instantiate the main view pointer.
48 browserViewPointer = new BrowserView(this);
50 // Set the main view as the central widget.
51 setCentralWidget(browserViewPointer);
53 // Get a handle for the action collection.
54 KActionCollection *actionCollectionPointer = this->actionCollection();
56 // Add the standard actions.
57 QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
58 QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
59 KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
60 KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
61 KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
62 KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
63 KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
65 // Add the custom actions.
66 userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
67 userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
68 userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
69 userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
70 userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
71 userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
72 userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
73 userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
74 userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
75 zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
76 searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
77 searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
78 searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
79 searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
80 searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
81 searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
82 searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
83 QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
84 cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
85 javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
86 localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
87 domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
89 // Create the action groups
90 QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
91 QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
93 // Add the actions to the groups.
94 userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
95 userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
96 userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
97 userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
98 userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
99 userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
100 userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
101 userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
102 userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
103 searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
104 searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
105 searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
106 searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
107 searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
108 searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
109 searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
111 // Set some actions to be checkable.
112 javaScriptActionPointer->setCheckable(true);
113 localStorageActionPointer->setCheckable(true);
114 domStorageActionPointer->setCheckable(true);
115 userAgentPrivacyBrowserActionPointer->setCheckable(true);
116 userAgentWebEngineDefaultActionPointer->setCheckable(true);
117 userAgentFirefoxLinuxActionPointer->setCheckable(true);
118 userAgentChromiumLinuxActionPointer->setCheckable(true);
119 userAgentFirefoxWindowsActionPointer->setCheckable(true);
120 userAgentChromeWindowsActionPointer->setCheckable(true);
121 userAgentEdgeWindowsActionPointer->setCheckable(true);
122 userAgentSafariMacosActionPointer->setCheckable(true);
123 userAgentCustomActionPointer->setCheckable(true);
124 searchEngineMojeekActionPointer->setCheckable(true);
125 searchEngineMonoclesActionPointer->setCheckable(true);
126 searchEngineMetagerActionPointer->setCheckable(true);
127 searchEngineGoogleActionPointer->setCheckable(true);
128 searchEngineBingActionPointer->setCheckable(true);
129 searchEngineYahooActionPointer->setCheckable(true);
130 searchEngineCustomActionPointer->setCheckable(true);
132 // Set the action text.
133 userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
134 userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
135 userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
136 userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
137 userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
138 userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
139 userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
140 userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
141 searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
142 searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
143 searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
144 searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
145 searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
146 searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
147 domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
148 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
149 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
150 localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
151 domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
153 // Set the action icons.
154 userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
155 userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
156 userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
157 userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
158 userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
159 userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
160 userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
161 userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
162 userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
163 searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
164 searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
165 searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
166 searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
167 searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
168 searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
169 searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
170 zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
171 domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
172 cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
173 domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
175 // Update the on-the-fly menus.
176 connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString)), this, SLOT(updateUserAgentActions(QString)));
177 connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
178 connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString)), this, SLOT(updateSearchEngineActions(QString)));
180 // Apply the on-the-fly settings when selected.
181 connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
182 connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
185 connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
186 connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
187 connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
189 // Connect the URL toolbar actions.
190 connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
191 connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
192 connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
194 // Update the URL toolbar actions.
195 connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
196 connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
197 connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
198 connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
199 connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
201 // Setup the GUI based on the browser_ui.rc file.
202 setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
204 // Get a handle for the URL toolbar.
205 KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
207 // Create a URL line edit.
208 urlLineEditPointer = new KLineEdit();
210 // Add an edit or add domain settings action to the URL line edit.
211 QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
213 // Add or edit the current domain settings.
214 connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
216 // Populate the URL toolbar.
217 urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
219 // Load a new URL from the URL line edit.
220 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
222 // Update the URL line edit on page loads.
223 connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
225 // Get a handle for the status bar.
226 QStatusBar *statusBarPointer = statusBar();
228 // Create a progress bar.
229 progressBarPointer = new QProgressBar();
231 // Add the progress bar to to the status bar.
232 statusBarPointer->addPermanentWidget(progressBarPointer);
234 // Update the status bar with the URL when a link is hovered.
235 connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
237 // Update the progress bar.
238 connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
239 connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
241 // Clear the URL line edit focus when requested.
242 connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
244 // Get the URL line edit palettes.
245 noDomainSettingsPalette = urlLineEditPointer->palette();
246 domainSettingsPalette = urlLineEditPointer->palette();
248 // Modify the domain settings palette.
249 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
251 // Update the applied palette.
252 connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
254 // Process cookie changes.
255 connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
256 connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
258 // Load the initial website.
259 browserViewPointer->loadInitialWebsite();
262 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
264 qDebug() << "Add cookie: " << newCookie.toRawForm();
266 // Add the new cookie to the list.
267 cookieListPointer->push_front(newCookie);
269 // Update the action text.
270 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
273 void BrowserWindow::addOrEditDomainSettings() const
275 // Remove the focus from the URL line edit.
276 urlLineEditPointer->clearFocus();
278 // Create the domain settings dialog pointer.
279 DomainSettingsDialog *domainSettingsDialogPointer;
281 // Run the commands according to the current domain settings status.
282 if (currentDomainSettingsDomain == "") // Domain settings are not currently applied.
284 // Instruct the domain settings dialog to add a new domain.
285 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
287 else // Domain settings are currently applied.
289 // Instruct the domain settings dialog to edit the current domain.
290 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
293 // Set the dialog window title.
294 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
297 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
300 domainSettingsDialogPointer->show();
302 // Reload the tabs when domain settings are updated.
303 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
306 void BrowserWindow::back() const
308 // Remove the focus from the URL line edit.
309 urlLineEditPointer->clearFocus();
312 browserViewPointer->back();
315 void BrowserWindow::clearUrlLineEditFocus() const
317 // Remove the focus from the URL line edit.
318 urlLineEditPointer->clearFocus();
321 void BrowserWindow::fileNew() const
323 // Display a new instance of Privacy Browser.
324 (new BrowserWindow)->show();
327 void BrowserWindow::forward() const
329 // Remove the focus from the URL line edit.
330 urlLineEditPointer->clearFocus();
333 browserViewPointer->forward();
336 void BrowserWindow::getZoomFactorFromUser()
338 // Create an OK flag.
341 // 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.
342 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
343 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
344 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
346 // Update the zoom factor if the user clicked OK.
349 // Update the current zoom factor.
350 currentZoomFactor = newZoomFactor;
352 // Set the new zoom factor.
353 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
355 // Update the on-the-fly action text.
356 updateZoomFactorAction(newZoomFactor);
360 void BrowserWindow::home() const
362 // Remove the focus from the URL line edit.
363 urlLineEditPointer->clearFocus();
366 browserViewPointer->home();
369 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
371 // Remove the focus from the URL line edit.
372 urlLineEditPointer->clearFocus();
375 browserViewPointer->loadUrlFromLineEdit(url);
378 void BrowserWindow::openCookiesDialog()
380 // Remove the focus from the URL line edit.
381 urlLineEditPointer->clearFocus();
383 // Instantiate the cookie settings dialog.
384 CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
387 cookiesDialogPointer->show();
389 // Connect the dialog signals.
390 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
391 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
392 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
395 void BrowserWindow::openDomainSettings() const
397 // Remove the focus from the URL line edit.
398 urlLineEditPointer->clearFocus();
400 // Instantiate the domain settings dialog.
401 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
404 domainSettingsDialogPointer->show();
406 // Reload the tabs when domain settings are updated.
407 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
410 void BrowserWindow::refresh() const
412 // Remove the focus from the URL line edit.
413 urlLineEditPointer->clearFocus();
415 // Refresh the web page.
416 browserViewPointer->refresh();
419 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
421 qDebug() << "Remove cookie: " << cookie.toRawForm();
423 // Remove the cookie from the list.
424 cookieListPointer->remove(cookie);
426 // Update the action text.
427 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
430 void BrowserWindow::showProgressBar(const int &progress) const
432 // Set the progress bar value.
433 progressBarPointer->setValue(progress);
435 // Show the progress bar.
436 progressBarPointer->show();
439 QSize BrowserWindow::sizeHint() const
441 // Return the default window size.
442 return QSize(1500, 1200);
445 void BrowserWindow::settingsConfigure()
447 // Check to make sure the dialog box isn't already displayed.
448 if (KConfigDialog::exists(QStringLiteral("settings")))
450 // Show the existing config dialog if it is hidden.
451 configDialogPointer->show();
453 // Raise the existing config dialog if it is below other windows.
454 configDialogPointer->raise();
456 // Restore the existing config dialog if it has been minimized.
457 if (configDialogPointer->isMinimized()) {
458 configDialogPointer->showNormal();
461 // Activate the existing config dialog, which brings its virtual desktop into focus.
462 configDialogPointer->activateWindow();
466 // Create the settings widgets.
467 QWidget *privacySettingsWidgetPointer = new QWidget;
468 QWidget *generalSettingsWidgetPointer = new QWidget;
470 // Instantiate the settings UI.
471 Ui::PrivacySettings privacySettingsUi;
472 Ui::GeneralSettings generalSettingsUi;
474 // Setup the UI to display the settings widgets.
475 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
476 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
478 // Get handles for the widgets.
479 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
480 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
481 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
482 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
484 // Populate the combo box labels.
485 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
486 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
488 // Update the labels when the combo boxes change.
489 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
490 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
492 // Instantiate a settings config dialog from the settings.kcfg file.
493 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
495 // Add the settings widgets as config dialog pages.
496 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
497 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
499 // Delete the config dialog when it is closed.
500 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
503 configDialogPointer->show();
505 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
506 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
507 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
508 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
509 //configDialogPointer->adjustSize();
511 // Expand the config dialog.
512 configDialogPointer->resize(1000, 500);
514 // Apply the settings when they are updated.
515 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
516 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
520 void BrowserWindow::toggleLocalStorage() const
522 // Remove the focus from teh URL line edit.
523 urlLineEditPointer->clearFocus();
525 // Toggle local storage.
526 browserViewPointer->toggleLocalStorage();
529 void BrowserWindow::toggleJavaScript() const
531 // Remove the focus from the URL line edit.
532 urlLineEditPointer->clearFocus();
534 // Toggle JavaScript.
535 browserViewPointer->toggleJavaScript();
538 void BrowserWindow::toggleDomStorage() const
540 // Remove the focus from the URL line edit.
541 urlLineEditPointer->clearFocus();
543 // Toggle DOM storage.
544 browserViewPointer->toggleDomStorage();
547 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
549 // Set the action checked status.
550 domStorageActionPointer->setChecked(isEnabled);
553 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
555 // Set the domain palette according to the status.
557 urlLineEditPointer->setPalette(domainSettingsPalette);
559 urlLineEditPointer->setPalette(noDomainSettingsPalette);
562 currentDomainSettingsDomain = domainSettingsDomain;
565 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
567 // Update the JavaScript status.
568 javaScriptEnabled = isEnabled;
570 // Set the icon according to the status.
571 if (javaScriptEnabled)
572 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
574 javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
576 // Set the action checked status.
577 javaScriptActionPointer->setChecked(javaScriptEnabled);
579 // Update the status of the DOM storage action.
580 domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
583 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
585 // Update the local storage status.
586 localStorageEnabled = isEnabled;
589 if (localStorageEnabled)
590 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
592 localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
594 // Set the action checked status.
595 localStorageActionPointer->setChecked(localStorageEnabled);
597 // Update the status of the DOM storage action.
598 domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
601 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine) const
603 // Initialize the custom search engine flag.
604 bool customSearchEngine = false;
606 if (searchEngine == "Mojeek") // Mojeek.
608 searchEngineMojeekActionPointer->setChecked(true);
610 else if (searchEngine == "Monocles") // Monocles.
612 searchEngineMonoclesActionPointer->setChecked(true);
614 else if (searchEngine == "MetaGer") // MetaGer.
616 searchEngineMetagerActionPointer->setChecked(true);
618 else if (searchEngine == "Google") // Google.
620 searchEngineGoogleActionPointer->setChecked(true);
622 else if (searchEngine == "Bing") // Bing.
624 searchEngineBingActionPointer->setChecked(true);
626 else if (searchEngine == "Yahoo") // Yahoo.
628 searchEngineYahooActionPointer->setChecked(true);
630 else // Custom search engine.
632 // Check the user agent.
633 searchEngineCustomActionPointer->setChecked(true);
635 // Set the custom search engine flag.
636 customSearchEngine = true;
639 // Format the custom search engine.
640 if (customSearchEngine)
642 // Enable the custom search engine.
643 searchEngineCustomActionPointer->setEnabled(true);
645 // Set the custom search engine text.
646 searchEngineCustomActionPointer->setText(searchEngine);
650 // Disable the custom search engine.
651 searchEngineCustomActionPointer->setEnabled(false);
653 // Reset the custom search engine text.
654 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
658 void BrowserWindow::updateUserAgentActions(const QString &userAgent) const
660 // Initialize the custom user agent flag.
661 bool customUserAgent = false;
663 // Check the indicated on-the-fly user agent.
664 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser.
665 else if (userAgent == BrowserView::webEngineDefaultUserAgent) userAgentWebEngineDefaultActionPointer->setChecked(true); // WebEngine default.
666 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true); // Firefox Linux.
667 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true); // Chromium Linux.
668 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true); // Firefox Windows.
669 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true); // Chrome Windows.
670 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true); // Edge Windows.
671 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true); // Safara macOS.
672 else // Custom user agent.
674 // Check the user agent.
675 userAgentCustomActionPointer->setChecked(true);
677 // Set the custom user agent flag.
678 customUserAgent = true;
682 // Format the custom user agent.
685 // Enable the custom user agent.
686 userAgentCustomActionPointer->setEnabled(true);
688 // Set the custom user agent text.
689 userAgentCustomActionPointer->setText(userAgent);
693 // Disable the custom user agent.
694 userAgentCustomActionPointer->setEnabled(false);
696 // Reset the custom user agent text.
697 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
701 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
703 // Set the current zoom factor.
704 currentZoomFactor = zoomFactor;
706 // Update the zoom factor action text, formatting the double with 2 decimal places.
707 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
710 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
712 // Update the search engine label.
713 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
716 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
718 // Update the URL line edit if it does not have focus.
719 if (!urlLineEditPointer->hasFocus())
721 // Update the URL line edit.
722 urlLineEditPointer->setText(newUrl.toString());
725 // Store the current URL.
729 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
731 // Update the user agent label.
732 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));