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