]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/windows/BrowserWindow.cpp
Add local storage domain settings.
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "BrowserWindow.h"
22 #include "Settings.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"
29
30 // KDE Frameworks headers.
31 #include <KActionCollection>
32 #include <KToolBar>
33
34 // Qt toolkit headers.
35 #include <QInputDialog>
36 #include <QNetworkCookie>
37 #include <QStatusBar>
38
39 // Construct the class.
40 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
41 {
42     // Initialize the variables.
43     cookieListPointer = new std::list<QNetworkCookie>;
44     javaScriptEnabled = false;
45     localStorageEnabled = false;
46
47     // Instantiate the main view pointer.
48     browserViewPointer = new BrowserView(this);
49
50     // Set the main view as the central widget.
51     setCentralWidget(browserViewPointer);
52
53     // Get a handle for the action collection.
54     KActionCollection *actionCollectionPointer = this->actionCollection();
55
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);
64
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"));
88
89     // Create the action groups
90     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
91     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
92
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);
110
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);
131
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"));
152
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")));
174
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)));
179
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*)));
183
184     // Display dialogs.
185     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
186     connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
187     connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
188
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()));
193
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)));
200
201     // Setup the GUI based on the browser_ui.rc file.
202     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
203
204     // Get a handle for the URL toolbar.
205     KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
206
207     // Create a URL line edit.
208     urlLineEditPointer = new KLineEdit();
209
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);
212
213     // Add or edit the current domain settings.
214     connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
215
216     // Populate the URL toolbar.
217     urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
218
219     // Load a new URL from the URL line edit.
220     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
221
222     // Update the URL line edit on page loads.
223     connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
224
225     // Get a handle for the status bar.
226     QStatusBar *statusBarPointer = statusBar();
227
228     // Create a progress bar.
229     progressBarPointer = new QProgressBar();
230
231     // Add the progress bar to to the status bar.
232     statusBarPointer->addPermanentWidget(progressBarPointer);
233
234     // Update the status bar with the URL when a link is hovered.
235     connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
236
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()));
240
241     // Clear the URL line edit focus when requested.
242     connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
243
244     // Get the URL line edit palettes.
245     noDomainSettingsPalette = urlLineEditPointer->palette();
246     domainSettingsPalette = urlLineEditPointer->palette();
247
248     // Modify the domain settings palette.
249     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
250
251     // Update the applied palette.
252     connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
253
254     // Process cookie changes.
255     connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
256     connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
257
258     // Load the initial website.
259     browserViewPointer->loadInitialWebsite();
260 }
261
262 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
263 {
264     qDebug() << "Add cookie:  " << newCookie.toRawForm();
265
266     // Add the new cookie to the list.
267     cookieListPointer->push_front(newCookie);
268
269     // Update the action text.
270     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
271 }
272
273 void BrowserWindow::addOrEditDomainSettings() const
274 {
275     // Remove the focus from the URL line edit.
276     urlLineEditPointer->clearFocus();
277
278     // Create the domain settings dialog pointer.
279     DomainSettingsDialog *domainSettingsDialogPointer;
280
281     // Run the commands according to the current domain settings status.
282     if (currentDomainSettingsDomain == "")  // Domain settings are not currently applied.
283     {
284         // Instruct the domain settings dialog to add a new domain.
285         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
286     }
287     else  // Domain settings are currently applied.
288     {
289         // Instruct the domain settings dialog to edit the current domain.
290         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
291     }
292
293     // Set the dialog window title.
294     domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
295
296     // Set the modality.
297     domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
298
299     // Show the dialog.
300     domainSettingsDialogPointer->show();
301
302     // Reload the tabs when domain settings are updated.
303     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
304 }
305
306 void BrowserWindow::back() const
307 {
308     // Remove the focus from the URL line edit.
309     urlLineEditPointer->clearFocus();
310
311     // Go back.
312     browserViewPointer->back();
313 }
314
315 void BrowserWindow::clearUrlLineEditFocus() const
316 {
317     // Remove the focus from the URL line edit.
318     urlLineEditPointer->clearFocus();
319 }
320
321 void BrowserWindow::fileNew() const
322 {
323     // Display a new instance of Privacy Browser.
324     (new BrowserWindow)->show();
325 }
326
327 void BrowserWindow::forward() const
328 {
329     // Remove the focus from the URL line edit.
330     urlLineEditPointer->clearFocus();
331
332     // Go forward.
333     browserViewPointer->forward();
334 }
335
336 void BrowserWindow::getZoomFactorFromUser()
337 {
338     // Create an OK flag.
339     bool okClicked;
340
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);
345
346     // Update the zoom factor if the user clicked OK.
347     if (okClicked)
348     {
349         // Update the current zoom factor.
350         currentZoomFactor = newZoomFactor;
351
352         // Set the new zoom factor.
353         browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
354
355         // Update the on-the-fly action text.
356         updateZoomFactorAction(newZoomFactor);
357     }
358 }
359
360 void BrowserWindow::home() const
361 {
362     // Remove the focus from the URL line edit.
363     urlLineEditPointer->clearFocus();
364
365     // Go home.
366     browserViewPointer->home();
367 }
368
369 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
370 {
371     // Remove the focus from the URL line edit.
372     urlLineEditPointer->clearFocus();
373
374     // Load the URL.
375     browserViewPointer->loadUrlFromLineEdit(url);
376 }
377
378 void BrowserWindow::openCookiesDialog()
379 {
380     // Remove the focus from the URL line edit.
381     urlLineEditPointer->clearFocus();
382
383     // Instantiate the cookie settings dialog.
384     CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
385
386     // Show the dialog.
387     cookiesDialogPointer->show();
388
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)));
393 }
394
395 void BrowserWindow::openDomainSettings() const
396 {
397     // Remove the focus from the URL line edit.
398     urlLineEditPointer->clearFocus();
399
400     // Instantiate the domain settings dialog.
401     DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
402
403     // Show the dialog.
404     domainSettingsDialogPointer->show();
405
406     // Reload the tabs when domain settings are updated.
407     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
408 }
409
410 void BrowserWindow::refresh() const
411 {
412     // Remove the focus from the URL line edit.
413     urlLineEditPointer->clearFocus();
414
415     // Refresh the web page.
416     browserViewPointer->refresh();
417 }
418
419 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
420 {
421     qDebug() << "Remove cookie:  " << cookie.toRawForm();
422
423     // Remove the cookie from the list.
424     cookieListPointer->remove(cookie);
425
426     // Update the action text.
427     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
428 }
429
430 void BrowserWindow::showProgressBar(const int &progress) const
431 {
432     // Set the progress bar value.
433     progressBarPointer->setValue(progress);
434
435     // Show the progress bar.
436     progressBarPointer->show();
437 }
438
439 QSize BrowserWindow::sizeHint() const
440 {
441     // Return the default window size.
442     return QSize(1500, 1200);
443 }
444
445 void BrowserWindow::settingsConfigure()
446 {
447     // Check to make sure the dialog box isn't already displayed.
448     if (KConfigDialog::exists(QStringLiteral("settings")))
449     {
450         // Show the existing config dialog if it is hidden.
451         configDialogPointer->show();
452
453         // Raise the existing config dialog if it is below other windows.
454         configDialogPointer->raise();
455
456         // Restore the existing config dialog if it has been minimized.
457         if (configDialogPointer->isMinimized()) {
458             configDialogPointer->showNormal();
459         }
460
461         // Activate the existing config dialog, which brings its virtual desktop into focus.
462         configDialogPointer->activateWindow();
463     }
464     else
465     {
466         // Create the settings widgets.
467         QWidget *privacySettingsWidgetPointer = new QWidget;
468         QWidget *generalSettingsWidgetPointer = new QWidget;
469
470         // Instantiate the settings UI.
471         Ui::PrivacySettings privacySettingsUi;
472         Ui::GeneralSettings generalSettingsUi;
473
474         // Setup the UI to display the settings widgets.
475         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
476         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
477
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;
483
484         // Populate the combo box labels.
485         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
486         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
487
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)));
491
492         // Instantiate a settings config dialog from the settings.kcfg file.
493         configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
494
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"));
498
499         // Delete the config dialog when it is closed.
500         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
501
502         // Make it so.
503         configDialogPointer->show();
504
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();
510
511         // Expand the config dialog.
512         configDialogPointer->resize(1000, 500);
513
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()));
517     }
518 }
519
520 void BrowserWindow::toggleLocalStorage() const
521 {
522     // Remove the focus from teh URL line edit.
523     urlLineEditPointer->clearFocus();
524
525     // Toggle local storage.
526     browserViewPointer->toggleLocalStorage();
527 }
528
529 void BrowserWindow::toggleJavaScript() const
530 {
531     // Remove the focus from the URL line edit.
532     urlLineEditPointer->clearFocus();
533
534     // Toggle JavaScript.
535     browserViewPointer->toggleJavaScript();
536 }
537
538 void BrowserWindow::toggleDomStorage() const
539 {
540     // Remove the focus from the URL line edit.
541     urlLineEditPointer->clearFocus();
542
543     // Toggle DOM storage.
544     browserViewPointer->toggleDomStorage();
545 }
546
547 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
548 {
549     // Set the action checked status.
550     domStorageActionPointer->setChecked(isEnabled);
551 }
552
553 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
554 {
555     // Set the domain palette according to the status.
556     if (status)
557         urlLineEditPointer->setPalette(domainSettingsPalette);
558     else
559         urlLineEditPointer->setPalette(noDomainSettingsPalette);
560
561     // Store the domain.
562     currentDomainSettingsDomain = domainSettingsDomain;
563 }
564
565 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
566 {
567     // Update the JavaScript status.
568     javaScriptEnabled = isEnabled;
569
570     // Set the icon according to the status.
571     if (javaScriptEnabled)
572         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
573     else
574         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
575
576     // Set the action checked status.
577     javaScriptActionPointer->setChecked(javaScriptEnabled);
578
579     // Update the status of the DOM storage action.
580     domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
581 }
582
583 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
584 {
585     // Update the local storage status.
586     localStorageEnabled = isEnabled;
587
588     // Update the icon.
589     if (localStorageEnabled)
590         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
591     else
592         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
593
594     // Set the action checked status.
595     localStorageActionPointer->setChecked(localStorageEnabled);
596
597     // Update the status of the DOM storage action.
598     domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
599 }
600
601 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine) const
602 {
603     // Initialize the custom search engine flag.
604     bool customSearchEngine = false;
605
606     if (searchEngine == "Mojeek")  // Mojeek.
607     {
608         searchEngineMojeekActionPointer->setChecked(true);
609     }
610     else if (searchEngine == "Monocles")  // Monocles.
611     {
612         searchEngineMonoclesActionPointer->setChecked(true);
613     }
614     else if (searchEngine == "MetaGer")  // MetaGer.
615     {
616         searchEngineMetagerActionPointer->setChecked(true);
617     }
618     else if (searchEngine == "Google")  // Google.
619     {
620         searchEngineGoogleActionPointer->setChecked(true);
621     }
622     else if (searchEngine == "Bing")  // Bing.
623     {
624         searchEngineBingActionPointer->setChecked(true);
625     }
626     else if (searchEngine == "Yahoo")  // Yahoo.
627     {
628         searchEngineYahooActionPointer->setChecked(true);
629     }
630     else  // Custom search engine.
631     {
632         // Check the user agent.
633         searchEngineCustomActionPointer->setChecked(true);
634
635         // Set the custom search engine flag.
636         customSearchEngine = true;
637     }
638
639     // Format the custom search engine.
640     if (customSearchEngine)
641     {
642         // Enable the custom search engine.
643         searchEngineCustomActionPointer->setEnabled(true);
644
645         // Set the custom search engine text.
646         searchEngineCustomActionPointer->setText(searchEngine);
647     }
648     else
649     {
650         // Disable the custom search engine.
651         searchEngineCustomActionPointer->setEnabled(false);
652
653         // Reset the custom search engine text.
654         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
655     }
656 }
657
658 void BrowserWindow::updateUserAgentActions(const QString &userAgent) const
659 {
660     // Initialize the custom user agent flag.
661     bool customUserAgent = false;
662
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.
673     {
674         // Check the user agent.
675         userAgentCustomActionPointer->setChecked(true);
676
677         // Set the custom user agent flag.
678         customUserAgent = true;
679     }
680
681
682     // Format the custom user agent.
683     if (customUserAgent)
684     {
685         // Enable the custom user agent.
686         userAgentCustomActionPointer->setEnabled(true);
687
688         // Set the custom user agent text.
689         userAgentCustomActionPointer->setText(userAgent);
690     }
691     else
692     {
693         // Disable the custom user agent.
694         userAgentCustomActionPointer->setEnabled(false);
695
696         // Reset the custom user agent text.
697         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
698     }
699 }
700
701 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
702 {
703     // Set the current zoom factor.
704     currentZoomFactor = zoomFactor;
705
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());
708 }
709
710 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
711 {
712     // Update the search engine label.
713     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
714 }
715
716 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
717 {
718     // Update the URL line edit if it does not have focus.
719     if (!urlLineEditPointer->hasFocus())
720     {
721         // Update the URL line edit.
722         urlLineEditPointer->setText(newUrl.toString());
723     }
724
725     // Store the current URL.
726     currentUrl = newUrl;
727 }
728
729 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
730 {
731     // Update the user agent label.
732     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
733 }