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