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 cookiesEnabled = false;
45 javaScriptEnabled = 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 onTheFlyCookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("on-the-fly_cookies"));
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 userAgentPrivacyBrowserActionPointer->setCheckable(true);
113 userAgentWebEngineDefaultActionPointer->setCheckable(true);
114 userAgentFirefoxLinuxActionPointer->setCheckable(true);
115 userAgentChromiumLinuxActionPointer->setCheckable(true);
116 userAgentFirefoxWindowsActionPointer->setCheckable(true);
117 userAgentChromeWindowsActionPointer->setCheckable(true);
118 userAgentEdgeWindowsActionPointer->setCheckable(true);
119 userAgentSafariMacosActionPointer->setCheckable(true);
120 userAgentCustomActionPointer->setCheckable(true);
121 searchEngineMojeekActionPointer->setCheckable(true);
122 searchEngineMonoclesActionPointer->setCheckable(true);
123 searchEngineMetagerActionPointer->setCheckable(true);
124 searchEngineGoogleActionPointer->setCheckable(true);
125 searchEngineBingActionPointer->setCheckable(true);
126 searchEngineYahooActionPointer->setCheckable(true);
127 searchEngineCustomActionPointer->setCheckable(true);
129 // Get the number of cookies.
130 int numberOfCookies = cookieListPointer->size();
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", numberOfCookies));
149 javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
150 onTheFlyCookiesActionPointer->setText(i18nc("The On-The-Fly Cookies action, which also display the number of cookies", "Cookies - %1", numberOfCookies));
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 onTheFlyCookiesActionPointer->setIcon(QIcon(":/icons/cookies-off"));
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(onTheFlyCookiesActionPointer, SIGNAL(triggered()), this, SLOT(toggleCookies()));
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(updateCookiesAction(bool)), this, SLOT(updateCookiesAction(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 // Get the number of cookies.
270 int numberOfCookies = cookieListPointer->size();
272 // Update the action text.
273 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", numberOfCookies));
274 onTheFlyCookiesActionPointer->setText(i18nc("The On-The-Fly Cookies action, which also display the number of cookies", "Cookies - %1", numberOfCookies));
277 void BrowserWindow::addOrEditDomainSettings() const
279 // Remove the focus from the URL line edit.
280 urlLineEditPointer->clearFocus();
282 // Create the domain settings dialog pointer.
283 DomainSettingsDialog *domainSettingsDialogPointer;
285 // Run the commands according to the current domain settings status.
286 if (currentDomainSettingsDomain == "") // Domain settings are not currently applied.
288 // Instruct the domain settings dialog to add a new domain.
289 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
291 else // Domain settings are currently applied.
293 // Instruct the domain settings dialog to edit the current domain.
294 domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
297 // Set the dialog window title.
298 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
301 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
304 domainSettingsDialogPointer->show();
306 // Reload the tabs when domain settings are updated.
307 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
310 void BrowserWindow::back() const
312 // Remove the focus from the URL line edit.
313 urlLineEditPointer->clearFocus();
316 browserViewPointer->back();
319 void BrowserWindow::clearUrlLineEditFocus() const
321 // Remove the focus from the URL line edit.
322 urlLineEditPointer->clearFocus();
325 void BrowserWindow::fileNew() const
327 // Display a new instance of Privacy Browser.
328 (new BrowserWindow)->show();
331 void BrowserWindow::forward() const
333 // Remove the focus from the URL line edit.
334 urlLineEditPointer->clearFocus();
337 browserViewPointer->forward();
340 void BrowserWindow::getZoomFactorFromUser()
342 // Create an OK flag.
345 // 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.
346 double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
347 i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
348 currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
350 // Update the zoom factor if the user clicked OK.
353 // Update the current zoom factor.
354 currentZoomFactor = newZoomFactor;
356 // Set the new zoom factor.
357 browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
359 // Update the on-the-fly action text.
360 updateZoomFactorAction(newZoomFactor);
364 void BrowserWindow::home() const
366 // Remove the focus from the URL line edit.
367 urlLineEditPointer->clearFocus();
370 browserViewPointer->home();
373 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
375 // Remove the focus from the URL line edit.
376 urlLineEditPointer->clearFocus();
379 browserViewPointer->loadUrlFromLineEdit(url);
382 void BrowserWindow::openCookiesDialog()
384 // Remove the focus from the URL line edit.
385 urlLineEditPointer->clearFocus();
387 // Instantiate the cookie settings dialog.
388 CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
391 cookiesDialogPointer->show();
393 // Connect the dialog signals.
394 connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
395 connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
396 connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
399 void BrowserWindow::openDomainSettings() const
401 // Remove the focus from the URL line edit.
402 urlLineEditPointer->clearFocus();
404 // Instantiate the domain settings dialog.
405 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
408 domainSettingsDialogPointer->show();
410 // Reload the tabs when domain settings are updated.
411 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
414 void BrowserWindow::refresh() const
416 // Remove the focus from the URL line edit.
417 urlLineEditPointer->clearFocus();
419 // Refresh the web page.
420 browserViewPointer->refresh();
423 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
425 qDebug() << "Remove cookie: " << cookie.toRawForm();
427 // Remove the cookie from the list.
428 cookieListPointer->remove(cookie);
430 // Get the number of cookies.
431 int numberOfCookies = cookieListPointer->size();
433 // Update the action text.
434 cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", numberOfCookies));
435 onTheFlyCookiesActionPointer->setText(i18nc("The On-The-Fly Cookies action, which also display the number of cookies", "Cookies - %1", numberOfCookies));
438 void BrowserWindow::showProgressBar(const int &progress) const
440 // Set the progress bar value.
441 progressBarPointer->setValue(progress);
443 // Show the progress bar.
444 progressBarPointer->show();
447 QSize BrowserWindow::sizeHint() const
449 // Return the default window size.
450 return QSize(1500, 1200);
453 void BrowserWindow::settingsConfigure()
455 // Check to make sure the dialog box isn't already displayed.
456 if (KConfigDialog::exists(QStringLiteral("settings")))
458 // Show the existing config dialog if it is hidden.
459 configDialogPointer->show();
461 // Raise the existing config dialog if it is below other windows.
462 configDialogPointer->raise();
464 // Restore the existing config dialog if it has been minimized.
465 if (configDialogPointer->isMinimized()) {
466 configDialogPointer->showNormal();
469 // Activate the existing config dialog, which brings its virtual desktop into focus.
470 configDialogPointer->activateWindow();
474 // Create the settings widgets.
475 QWidget *privacySettingsWidgetPointer = new QWidget;
476 QWidget *generalSettingsWidgetPointer = new QWidget;
478 // Instantiate the settings UI.
479 Ui::PrivacySettings privacySettingsUi;
480 Ui::GeneralSettings generalSettingsUi;
482 // Setup the UI to display the settings widgets.
483 privacySettingsUi.setupUi(privacySettingsWidgetPointer);
484 generalSettingsUi.setupUi(generalSettingsWidgetPointer);
486 // Get handles for the widgets.
487 QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
488 userAgentLabelPointer = privacySettingsUi.userAgentLabel;
489 QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
490 searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
492 // Populate the combo box labels.
493 updateUserAgentLabel(userAgentComboBoxPointer->currentText());
494 updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
496 // Update the labels when the combo boxes change.
497 connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
498 connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
500 // Instantiate a settings config dialog from the settings.kcfg file.
501 configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
503 // Add the settings widgets as config dialog pages.
504 configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
505 configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
507 // Delete the config dialog when it is closed.
508 configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
511 configDialogPointer->show();
513 // TODO. KConfigDialog does not respect expanding size policies. <https://redmine.stoutner.com/issues/823>
514 //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
515 //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
516 //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
517 //configDialogPointer->adjustSize();
519 // Expand the config dialog.
520 configDialogPointer->resize(1000, 500);
522 // Apply the settings when they are updated.
523 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
524 connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
528 void BrowserWindow::toggleCookies() const
530 // Remove the focus from teh URL line edit.
531 urlLineEditPointer->clearFocus();
534 browserViewPointer->toggleCookies();
537 void BrowserWindow::toggleJavaScript() const
539 // Remove the focus from the URL line edit.
540 urlLineEditPointer->clearFocus();
542 // Toggle JavaScript.
543 browserViewPointer->toggleJavaScript();
546 void BrowserWindow::toggleDomStorage() const
548 // Remove the focus from the URL line edit.
549 urlLineEditPointer->clearFocus();
551 // Toggle DOM storage.
552 browserViewPointer->toggleDomStorage();
555 void BrowserWindow::updateCookiesAction(const bool &isEnabled)
557 // Update the cookies status.
558 cookiesEnabled = isEnabled;
562 onTheFlyCookiesActionPointer->setIcon(QIcon(":/icons/cookies-on"));
564 onTheFlyCookiesActionPointer->setIcon(QIcon(":/icons/cookies-off"));
566 // Update the status of the DOM storage action.
567 domStorageActionPointer->setEnabled(cookiesEnabled & javaScriptEnabled);
570 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
572 // Set the domain palette according to the status.
574 urlLineEditPointer->setPalette(domainSettingsPalette);
576 urlLineEditPointer->setPalette(noDomainSettingsPalette);
579 currentDomainSettingsDomain = domainSettingsDomain;
582 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
584 // Update the JavaScript status.
585 javaScriptEnabled = isEnabled;
587 // Set the icon according to the status.
588 if (javaScriptEnabled)
589 javaScriptActionPointer->setIcon(QIcon(":/icons/javascript-warning"));
591 javaScriptActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
593 // Update the status of the DOM storage action.
594 domStorageActionPointer->setEnabled(javaScriptEnabled & cookiesEnabled);
597 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
599 // Set the icon according to the status.
601 domStorageActionPointer->setIcon(QIcon::fromTheme("disk-quota-low"));
603 domStorageActionPointer->setIcon(QIcon::fromTheme("disk-quota"));
606 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine) const
608 // Initialize the custom search engine flag.
609 bool customSearchEngine = false;
611 if (searchEngine == "Mojeek") // Mojeek.
613 searchEngineMojeekActionPointer->setChecked(true);
615 else if (searchEngine == "Monocles") // Monocles.
617 searchEngineMonoclesActionPointer->setChecked(true);
619 else if (searchEngine == "MetaGer") // MetaGer.
621 searchEngineMetagerActionPointer->setChecked(true);
623 else if (searchEngine == "Google") // Google.
625 searchEngineGoogleActionPointer->setChecked(true);
627 else if (searchEngine == "Bing") // Bing.
629 searchEngineBingActionPointer->setChecked(true);
631 else if (searchEngine == "Yahoo") // Yahoo.
633 searchEngineYahooActionPointer->setChecked(true);
635 else // Custom search engine.
637 // Check the user agent.
638 searchEngineCustomActionPointer->setChecked(true);
640 // Set the custom search engine flag.
641 customSearchEngine = true;
644 // Format the custom search engine.
645 if (customSearchEngine)
647 // Enable the custom search engine.
648 searchEngineCustomActionPointer->setEnabled(true);
650 // Set the custom search engine text.
651 searchEngineCustomActionPointer->setText(searchEngine);
655 // Disable the custom search engine.
656 searchEngineCustomActionPointer->setEnabled(false);
658 // Reset the custom search engine text.
659 searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
663 void BrowserWindow::updateUserAgentActions(const QString &userAgent) const
665 // Initialize the custom user agent flag.
666 bool customUserAgent = false;
668 // Check the indicated on-the-fly user agent.
669 if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true); // Privacy Browser.
670 else if (userAgent == BrowserView::webEngineDefaultUserAgent) userAgentWebEngineDefaultActionPointer->setChecked(true); // WebEngine default.
671 else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true); // Firefox Linux.
672 else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true); // Chromium Linux.
673 else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true); // Firefox Windows.
674 else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true); // Chrome Windows.
675 else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true); // Edge Windows.
676 else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true); // Safara macOS.
677 else // Custom user agent.
679 // Check the user agent.
680 userAgentCustomActionPointer->setChecked(true);
682 // Set the custom user agent flag.
683 customUserAgent = true;
687 // Format the custom user agent.
690 // Enable the custom user agent.
691 userAgentCustomActionPointer->setEnabled(true);
693 // Set the custom user agent text.
694 userAgentCustomActionPointer->setText(userAgent);
698 // Disable the custom user agent.
699 userAgentCustomActionPointer->setEnabled(false);
701 // Reset the custom user agent text.
702 userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
706 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
708 // Set the current zoom factor.
709 currentZoomFactor = zoomFactor;
711 // Update the zoom factor action text, formatting the double with 2 decimal places.
712 zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
715 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
717 // Update the search engine label.
718 searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
721 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
723 // Update the URL line edit if it does not have focus.
724 if (!urlLineEditPointer->hasFocus())
726 // Update the URL line edit.
727 urlLineEditPointer->setText(newUrl.toString());
730 // Store the current URL.
734 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
736 // Update the user agent label.
737 userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));