]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/windows/BrowserWindow.cpp
33aef29f77ad309caf13e4acc3ad3d5c6b1cf30a
[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 "databases/CookiesDatabase.h"
26 #include "dialogs/CookiesDialog.h"
27 #include "dialogs/DomainSettingsDialog.h"
28 #include "helpers/SearchEngineHelper.h"
29 #include "helpers/UserAgentHelper.h"
30
31 // KDE Frameworks headers.
32 #include <KActionCollection>
33 #include <KToolBar>
34
35 // Qt toolkit headers.
36 #include <QInputDialog>
37 #include <QNetworkCookie>
38 #include <QMenuBar>
39 #include <QShortcut>
40 #include <QStatusBar>
41
42 // Construct the class.
43 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
44 {
45     // Initialize the variables.
46     cookieListPointer = new std::list<QNetworkCookie>;
47     javaScriptEnabled = false;
48     localStorageEnabled = false;
49
50     // Instantiate the main view pointer.
51     browserViewPointer = new BrowserView(this);
52
53     // Set the main view as the central widget.
54     setCentralWidget(browserViewPointer);
55
56     // Get a handle for the action collection.
57     KActionCollection *actionCollectionPointer = this->actionCollection();
58
59     // Add the standard actions.
60     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
61     KStandardAction::print(browserViewPointer, SLOT(print()), actionCollectionPointer);
62     KStandardAction::printPreview(browserViewPointer, SLOT(printPreview()), actionCollectionPointer);
63     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
64     KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
65     fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
66     QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
67     QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
68     KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
69     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
70
71     // Add the custom actions.
72     userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
73     userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
74     userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
75     userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
76     userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
77     userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
78     userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
79     userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
80     userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
81     zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
82     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
83     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
84     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
85     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
86     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
87     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
88     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
89     QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
90     cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
91     javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
92     localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
93     domStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("dom_storage"));
94
95     // Create the action groups
96     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
97     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
98
99     // Add the actions to the groups.
100     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
101     userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
102     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
103     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
104     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
105     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
106     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
107     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
108     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
109     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
110     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
111     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
112     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
113     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
114     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
115     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
116
117     // Set some actions to be checkable.
118     javaScriptActionPointer->setCheckable(true);
119     localStorageActionPointer->setCheckable(true);
120     domStorageActionPointer->setCheckable(true);
121     userAgentPrivacyBrowserActionPointer->setCheckable(true);
122     userAgentWebEngineDefaultActionPointer->setCheckable(true);
123     userAgentFirefoxLinuxActionPointer->setCheckable(true);
124     userAgentChromiumLinuxActionPointer->setCheckable(true);
125     userAgentFirefoxWindowsActionPointer->setCheckable(true);
126     userAgentChromeWindowsActionPointer->setCheckable(true);
127     userAgentEdgeWindowsActionPointer->setCheckable(true);
128     userAgentSafariMacosActionPointer->setCheckable(true);
129     userAgentCustomActionPointer->setCheckable(true);
130     searchEngineMojeekActionPointer->setCheckable(true);
131     searchEngineMonoclesActionPointer->setCheckable(true);
132     searchEngineMetagerActionPointer->setCheckable(true);
133     searchEngineGoogleActionPointer->setCheckable(true);
134     searchEngineBingActionPointer->setCheckable(true);
135     searchEngineYahooActionPointer->setCheckable(true);
136     searchEngineCustomActionPointer->setCheckable(true);
137
138     // Set the action text.
139     userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
140     userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
141     userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
142     userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
143     userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
144     userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
145     userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
146     userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
147     searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
148     searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
149     searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
150     searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
151     searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
152     searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
153     domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
154     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
155     javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
156     localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
157     domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
158
159     // Set the action icons.
160     userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
161     userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
162     userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
163     userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
164     userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
165     userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
166     userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
167     userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
168     userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
169     searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
170     searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
171     searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
172     searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
173     searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
174     searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
175     searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
176     zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
177     domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
178     cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
179     domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
180
181     // Update the on-the-fly menus.
182     connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
183     connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
184     connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
185
186     // Apply the on-the-fly settings when selected.
187     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
188     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
189
190     // Display dialogs.
191     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
192     connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
193     connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
194
195     // Connect the URL toolbar actions.
196     connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
197     connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
198     connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
199
200     // Update the URL toolbar actions.
201     connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
202     connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
203     connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
204     connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
205     connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
206
207     // Setup the GUI based on the browser_ui.rc file.
208     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
209
210     // Get lists of the actions' associated widgets.
211     QList<QWidget*> userAgentAssociatedWidgetsPointerList = userAgentPrivacyBrowserActionPointer->associatedWidgets();
212     QList<QWidget*> searchEngineAssociatedWidgetsPointerList = searchEngineMojeekActionPointer->associatedWidgets();
213
214     // Get the menu widget pointers.  It is the second entry, after the main window.
215     QWidget *userAgentMenuWidgetPointer = userAgentAssociatedWidgetsPointerList[1];
216     QWidget *searchEngineMenuWidgetPointer = searchEngineAssociatedWidgetsPointerList[1];
217
218     // Get the menu pointers.
219     QMenu *userAgentMenuPointer = qobject_cast<QMenu*>(userAgentMenuWidgetPointer);
220     QMenu *searchEngineMenuPointer = qobject_cast<QMenu*>(searchEngineMenuWidgetPointer);
221
222     // Get the menu actions.
223     userAgentMenuActionPointer = userAgentMenuPointer->menuAction();
224     searchEngineMenuActionPointer = searchEngineMenuPointer->menuAction();
225
226     // Get handles for the toolbars.
227     navigationToolBarPointer = toolBar(QStringLiteral("navigation_toolbar"));
228     urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
229
230     // Create a URL line edit.
231     urlLineEditPointer = new KLineEdit();
232
233     // Add an edit or add domain settings action to the URL line edit.
234     QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
235
236     // Add or edit the current domain settings.
237     connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
238
239     // Populate the URL toolbar.
240     urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
241
242     // Load a new URL from the URL line edit.
243     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
244
245     // Update the URL line edit on page loads.
246     connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
247
248     // Get a handle for the status bar.
249     QStatusBar *statusBarPointer = statusBar();
250
251     // Create a progress bar.
252     progressBarPointer = new QProgressBar();
253
254     // Add the progress bar to to the status bar.
255     statusBarPointer->addPermanentWidget(progressBarPointer);
256
257     // Update the status bar with the URL when a link is hovered.
258     connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
259
260     // Update the progress bar.
261     connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
262     connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
263
264     // Clear the URL line edit focus when requested.
265     connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
266
267     // Get the URL line edit palettes.
268     noDomainSettingsPalette = urlLineEditPointer->palette();
269     domainSettingsPalette = urlLineEditPointer->palette();
270
271     // Modify the domain settings palette.
272     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
273
274     // Update the applied palette.
275     connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
276
277     // Process cookie changes.
278     connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
279     connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
280
281     // Process full screen requests.
282     connect(browserViewPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
283
284     // Create keyboard shortcuts.
285     QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
286     QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
287
288     // Connect the keyboard shortcuts to the actions.
289     connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
290     connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
291
292     // Load the initial website.
293     browserViewPointer->loadInitialWebsite();
294 }
295
296 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
297 {
298     //qDebug() << "Add cookie:  " << newCookie.toRawForm();
299
300     // Add the new cookie to the list.
301     cookieListPointer->push_front(newCookie);
302
303     // Update the action text.
304     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
305
306     // Update the cookie if it is durable and has new data.
307     if (CookiesDatabase::isUpdate(newCookie))
308         CookiesDatabase::updateCookie(newCookie);
309 }
310
311 void BrowserWindow::addOrEditDomainSettings() const
312 {
313     // Remove the focus from the URL line edit.
314     urlLineEditPointer->clearFocus();
315
316     // Create the domain settings dialog pointer.
317     DomainSettingsDialog *domainSettingsDialogPointer;
318
319     // Run the commands according to the current domain settings status.
320     if (currentDomainSettingsDomain == "")  // Domain settings are not currently applied.
321     {
322         // Instruct the domain settings dialog to add a new domain.
323         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
324     }
325     else  // Domain settings are currently applied.
326     {
327         // Instruct the domain settings dialog to edit the current domain.
328         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
329     }
330
331     // Set the dialog window title.
332     domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
333
334     // Set the modality.
335     domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
336
337     // Show the dialog.
338     domainSettingsDialogPointer->show();
339
340     // Reload the tabs when domain settings are updated.
341     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
342 }
343
344 void BrowserWindow::back() const
345 {
346     // Remove the focus from the URL line edit.
347     urlLineEditPointer->clearFocus();
348
349     // Go back.
350     browserViewPointer->back();
351 }
352
353 void BrowserWindow::clearUrlLineEditFocus() const
354 {
355     // Remove the focus from the URL line edit.
356     urlLineEditPointer->clearFocus();
357 }
358
359 void BrowserWindow::escape() const
360 {
361     // Exit full screen browsing if it is enabled.
362     if (fullScreenActionPointer->isChecked())
363         fullScreenActionPointer->trigger();
364 }
365
366 void BrowserWindow::fileNew() const
367 {
368     // Display a new instance of Privacy Browser.
369     (new BrowserWindow)->show();
370 }
371
372 void BrowserWindow::forward() const
373 {
374     // Remove the focus from the URL line edit.
375     urlLineEditPointer->clearFocus();
376
377     // Go forward.
378     browserViewPointer->forward();
379 }
380
381 void BrowserWindow::fullScreenRequested(const bool toggleOn)
382 {
383     // Toggle full screen mode.
384     if (toggleOn)  // Turn full screen mode on.
385     {
386         // Set the window to be full screen.
387         fullScreenActionPointer->setFullScreen(window(), true);
388
389         // Hide all the bars.
390         menuBar()->setVisible(false);
391         navigationToolBarPointer->setVisible(false);
392         urlToolBarPointer->setVisible(false);
393         statusBar()->setVisible(false);
394     }
395     else  // Turn full screen mode off.
396     {
397         // Set the window to not be full screen.
398         fullScreenActionPointer->setFullScreen(window(), false);
399
400         // Show all the bars.
401         menuBar()->setVisible(true);
402         navigationToolBarPointer->setVisible(true);
403         urlToolBarPointer->setVisible(true);
404         statusBar()->setVisible(true);
405     }
406 }
407
408 void BrowserWindow::getZoomFactorFromUser()
409 {
410     // Create an OK flag.
411     bool okClicked;
412
413     // 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.
414     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
415                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
416                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
417
418     // Update the zoom factor if the user clicked OK.
419     if (okClicked)
420     {
421         // Update the current zoom factor.
422         currentZoomFactor = newZoomFactor;
423
424         // Set the new zoom factor.
425         browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
426
427         // Update the on-the-fly action text.
428         updateZoomFactorAction(newZoomFactor);
429     }
430 }
431
432 void BrowserWindow::home() const
433 {
434     // Remove the focus from the URL line edit.
435     urlLineEditPointer->clearFocus();
436
437     // Go home.
438     browserViewPointer->home();
439 }
440
441 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
442 {
443     // Remove the focus from the URL line edit.
444     urlLineEditPointer->clearFocus();
445
446     // Load the URL.
447     browserViewPointer->loadUrlFromLineEdit(url);
448 }
449
450 void BrowserWindow::openCookiesDialog()
451 {
452     // Remove the focus from the URL line edit.
453     urlLineEditPointer->clearFocus();
454
455     // Instantiate the cookie settings dialog.
456     CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
457
458     // Show the dialog.
459     cookiesDialogPointer->show();
460
461     // Connect the dialog signals.
462     connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
463     connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
464     connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
465 }
466
467 void BrowserWindow::openDomainSettings() const
468 {
469     // Remove the focus from the URL line edit.
470     urlLineEditPointer->clearFocus();
471
472     // Instantiate the domain settings dialog.
473     DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
474
475     // Show the dialog.
476     domainSettingsDialogPointer->show();
477
478     // Reload the tabs when domain settings are updated.
479     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
480 }
481
482 void BrowserWindow::refresh() const
483 {
484     // Remove the focus from the URL line edit.
485     urlLineEditPointer->clearFocus();
486
487     // Refresh the web page.
488     browserViewPointer->refresh();
489 }
490
491 void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
492 {
493     //qDebug() << "Remove cookie:  " << cookie.toRawForm();
494
495     // Remove the cookie from the list.
496     cookieListPointer->remove(cookie);
497
498     // Update the action text.
499     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
500 }
501
502 void BrowserWindow::showProgressBar(const int &progress) const
503 {
504     // Set the progress bar value.
505     progressBarPointer->setValue(progress);
506
507     // Show the progress bar.
508     progressBarPointer->show();
509 }
510
511 QSize BrowserWindow::sizeHint() const
512 {
513     // Return the default window size.
514     return QSize(1500, 1200);
515 }
516
517 void BrowserWindow::settingsConfigure()
518 {
519     // Check to make sure the dialog box isn't already displayed.
520     if (KConfigDialog::exists(QStringLiteral("settings")))
521     {
522         // Show the existing config dialog if it is hidden.
523         configDialogPointer->show();
524
525         // Raise the existing config dialog if it is below other windows.
526         configDialogPointer->raise();
527
528         // Restore the existing config dialog if it has been minimized.
529         if (configDialogPointer->isMinimized()) {
530             configDialogPointer->showNormal();
531         }
532
533         // Activate the existing config dialog, which brings its virtual desktop into focus.
534         configDialogPointer->activateWindow();
535     }
536     else
537     {
538         // Create the settings widgets.
539         QWidget *privacySettingsWidgetPointer = new QWidget;
540         QWidget *generalSettingsWidgetPointer = new QWidget;
541
542         // Instantiate the settings UI.
543         Ui::PrivacySettings privacySettingsUi;
544         Ui::GeneralSettings generalSettingsUi;
545
546         // Setup the UI to display the settings widgets.
547         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
548         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
549
550         // Get handles for the widgets.
551         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
552         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
553         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
554         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
555
556         // Populate the combo box labels.
557         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
558         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
559
560         // Update the labels when the combo boxes change.
561         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
562         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
563
564         // Instantiate a settings config dialog from the settings.kcfg file.
565         configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
566
567         // Add the settings widgets as config dialog pages.
568         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
569         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
570
571         // Delete the config dialog when it is closed.
572         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
573
574         // Make it so.
575         configDialogPointer->show();
576
577         // TODO.  KConfigDialog does not respect expanding size policies.  <https://redmine.stoutner.com/issues/823>
578         //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
579         //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
580         //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
581         //configDialogPointer->adjustSize();
582
583         // Expand the config dialog.
584         configDialogPointer->resize(1000, 500);
585
586         // Apply the settings when they are updated.
587         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
588         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
589     }
590 }
591
592 void BrowserWindow::toggleDomStorage() const
593 {
594     // Remove the focus from the URL line edit.
595     urlLineEditPointer->clearFocus();
596
597     // Toggle DOM storage.
598     browserViewPointer->toggleDomStorage();
599 }
600
601 void BrowserWindow::toggleJavaScript() const
602 {
603     // Remove the focus from the URL line edit.
604     urlLineEditPointer->clearFocus();
605
606     // Toggle JavaScript.
607     browserViewPointer->toggleJavaScript();
608 }
609
610 void BrowserWindow::toggleLocalStorage() const
611 {
612     // Remove the focus from the URL line edit.
613     urlLineEditPointer->clearFocus();
614
615     // Toggle local storage.
616     browserViewPointer->toggleLocalStorage();
617 }
618
619 void BrowserWindow::toggleFullScreen()
620 {
621     // Toggle the full screen status.
622     if (fullScreenActionPointer->isChecked())  // Enable full screen browsing mode.
623     {
624         // Enable full screen mode.
625         fullScreenActionPointer->setFullScreen(window(), true);
626
627         // Hide the menu bar if specified.
628         if (Settings::fullScreenHideMenuBar())
629             menuBar()->setVisible(false);
630
631         // Hide the toolbars if specified.
632         if (Settings::fullScreenHideToolBars())
633         {
634             navigationToolBarPointer->setVisible(false);
635             urlToolBarPointer->setVisible(false);
636         }
637
638         // Hide the status bar if specified.
639         if (Settings::fullScreenHideStatusBar())
640             statusBar()->setVisible(false);
641     }
642     else  // Disable full screen browsing mode.
643     {
644         // Disable full screen mode.
645         fullScreenActionPointer->setFullScreen(window(), false);
646
647         // Show the menu bar.
648         menuBar()->setVisible(true);
649
650         // Show the toolbars.
651         navigationToolBarPointer->setVisible(true);
652         urlToolBarPointer->setVisible(true);
653
654         // Show the status bar.
655         statusBar()->setVisible(true);
656     }
657 }
658
659 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
660 {
661     // Set the action checked status.
662     domStorageActionPointer->setChecked(isEnabled);
663 }
664
665 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
666 {
667     // Set the domain palette according to the status.
668     if (status)
669         urlLineEditPointer->setPalette(domainSettingsPalette);
670     else
671         urlLineEditPointer->setPalette(noDomainSettingsPalette);
672
673     // Store the domain.
674     currentDomainSettingsDomain = domainSettingsDomain;
675 }
676
677 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
678 {
679     // Update the JavaScript status.
680     javaScriptEnabled = isEnabled;
681
682     // Set the icon according to the status.
683     if (javaScriptEnabled)
684         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
685     else
686         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
687
688     // Set the action checked status.
689     javaScriptActionPointer->setChecked(javaScriptEnabled);
690
691     // Update the status of the DOM storage action.
692     domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
693 }
694
695 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
696 {
697     // Update the local storage status.
698     localStorageEnabled = isEnabled;
699
700     // Update the icon.
701     if (localStorageEnabled)
702         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
703     else
704         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
705
706     // Set the action checked status.
707     localStorageActionPointer->setChecked(localStorageEnabled);
708
709     // Update the status of the DOM storage action.
710     domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
711 }
712
713 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
714 {
715     // Initialize the custom search engine flag.
716     bool customSearchEngine = false;
717
718     if (searchEngine == "Mojeek")  // Mojeek.
719     {
720         // Check the Mojeek user agent action.
721         searchEngineMojeekActionPointer->setChecked(true);
722
723         // Update the search engine menu action text.
724         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
725     }
726     else if (searchEngine == "Monocles")  // Monocles.
727     {
728         // Check the Monocles user agent action.
729         searchEngineMonoclesActionPointer->setChecked(true);
730
731         // Update the search engine menu action text.
732         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
733     }
734     else if (searchEngine == "MetaGer")  // MetaGer.
735     {
736         // Check the MetaGer user agent action.
737         searchEngineMetagerActionPointer->setChecked(true);
738
739         // Update the search engine menu action text.
740         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
741     }
742     else if (searchEngine == "Google")  // Google.
743     {
744         // Check the Google user agent action.
745         searchEngineGoogleActionPointer->setChecked(true);
746
747         // Update the search engine menu action text.
748         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
749     }
750     else if (searchEngine == "Bing")  // Bing.
751     {
752         // Check the Bing user agent action.
753         searchEngineBingActionPointer->setChecked(true);
754
755         // Update the search engine menu action text.
756         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
757     }
758     else if (searchEngine == "Yahoo")  // Yahoo.
759     {
760         // Check the Yahoo user agent action.
761         searchEngineYahooActionPointer->setChecked(true);
762
763         // Update the search engine menu action text.
764         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
765     }
766     else  // Custom search engine.
767     {
768         // Check the user agent.
769         searchEngineCustomActionPointer->setChecked(true);
770
771         // Update the search engine menu action text.
772         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
773
774         // Set the custom search engine text.
775         searchEngineCustomActionPointer->setText(searchEngine);
776
777         // Set the custom search engine flag.
778         customSearchEngine = true;
779     }
780
781     // Update the custom search engine enabled boolean.
782     if (updateCustomSearchEngineStatus)
783         customSearchEngineEnabled = customSearchEngine;
784
785     // Format the custom search engine.
786     if (customSearchEngineEnabled)
787     {
788         // Enable the custom search engine.
789         searchEngineCustomActionPointer->setEnabled(true);
790     }
791     else
792     {
793         // Disable the custom search engine.
794         searchEngineCustomActionPointer->setEnabled(false);
795
796         // Reset the custom search engine text.
797         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
798     }
799 }
800
801 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
802 {
803     // Initialize the custom user agent flag.
804     bool customUserAgent = false;
805
806     // Check the indicated on-the-fly user agent.
807     if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT)  // Privacy Browser.
808     {
809         // Check the Privacy Browser user agent action.
810         userAgentPrivacyBrowserActionPointer->setChecked(true);
811
812         // Update the user agent menu action text.
813         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
814     }
815     else if (userAgent == BrowserView::webEngineDefaultUserAgent)  // WebEngine default.
816     {
817         // check the WebEngine default user agent action.
818         userAgentWebEngineDefaultActionPointer->setChecked(true);
819
820         // Update the user agent menu action text.
821         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
822     }
823     else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT)  // Firefox on Linux.
824     {
825         // Check the Firefox on Linux user agent action.
826         userAgentFirefoxLinuxActionPointer->setChecked(true);
827
828         // Update the user agent menu action text.
829         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
830     }
831     else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT)  // Chromium on Linux.
832     {
833         // Check the Chromium on Linux user agent action.
834         userAgentChromiumLinuxActionPointer->setChecked(true);
835
836         // Update the user agent menu action text.
837         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
838     }
839     else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT)  // Firefox on Windows.
840     {
841         // Check the Firefox on Windows user agent action.
842         userAgentFirefoxWindowsActionPointer->setChecked(true);
843
844         // Update the user agent menu action text.
845         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
846     }
847     else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT)  // Chrome on Windows.
848     {
849         // Check the Chrome on Windows user agent action.
850         userAgentChromeWindowsActionPointer->setChecked(true);
851
852         // Update the user agent menu action text.
853         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
854     }
855     else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT)  // Edge on Windows.
856     {
857         // Check the Edge on Windows user agent action.
858         userAgentEdgeWindowsActionPointer->setChecked(true);
859
860         // Update the user agent menu action text.
861         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
862     }
863     else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT)  // Safari on macOS.
864     {
865         // Check the Safari on macOS user agent action.
866         userAgentSafariMacosActionPointer->setChecked(true);
867
868         // Update the user agent menu action text.
869         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
870     }
871     else  // Custom user agent.
872     {
873         // Check the user agent.
874         userAgentCustomActionPointer->setChecked(true);
875
876         // Update the user agent menu action text.
877         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
878
879         // Set the custom user agent text.
880         userAgentCustomActionPointer->setText(userAgent);
881
882         // Set the custom user agent flag.
883         customUserAgent = true;
884     }
885
886     // Update the custom user agent enabled boolean.
887     if (updateCustomUserAgentStatus)
888         customUserAgentEnabled = customUserAgent;
889
890
891     // Format the custom user agent.
892     if (customUserAgentEnabled)
893     {
894         // Enable the custom user agent.
895         userAgentCustomActionPointer->setEnabled(true);
896     }
897     else
898     {
899         // Disable the custom user agent.
900         userAgentCustomActionPointer->setEnabled(false);
901
902         // Reset the custom user agent text.
903         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
904     }
905 }
906
907 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
908 {
909     // Set the current zoom factor.
910     currentZoomFactor = zoomFactor;
911
912     // Update the zoom factor action text, formatting the double with 2 decimal places.
913     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
914 }
915
916 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
917 {
918     // Update the search engine label.
919     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
920 }
921
922 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
923 {
924     // Update the URL line edit if it does not have focus.
925     if (!urlLineEditPointer->hasFocus())
926     {
927         // Update the URL line edit.
928         urlLineEditPointer->setText(newUrl.toString());
929     }
930
931     // Store the current URL.
932     currentUrl = newUrl;
933 }
934
935 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
936 {
937     // Update the user agent label.
938     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
939 }