]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/windows/BrowserWindow.cpp
Implement loading of new tabs from the context menu.
[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() : 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     // Get a handle for the status bar.
251     QStatusBar *statusBarPointer = statusBar();
252
253     // Create a progress bar.
254     progressBarPointer = new QProgressBar();
255
256     // Add the progress bar to to the status bar.
257     statusBarPointer->addPermanentWidget(progressBarPointer);
258
259     // Update the status bar with the URL when a link is hovered.
260     connect(tabWidgetPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
261
262     // Update the progress bar.
263     connect(tabWidgetPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
264     connect(tabWidgetPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
265
266     // Update the URL line edit focus.
267     connect(tabWidgetPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
268
269     // Get the URL line edit palettes.
270     noDomainSettingsPalette = urlLineEditPointer->palette();
271     domainSettingsPalette = urlLineEditPointer->palette();
272
273     // Modify the domain settings palette.
274     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
275
276     // Update the applied palette.
277     connect(tabWidgetPointer, SIGNAL(updateDomainSettingsIndicator(const bool)), this, SLOT(updateDomainSettingsIndicator(const bool)));
278
279     // Process full screen requests.
280     connect(tabWidgetPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
281
282     // Create keyboard shortcuts.
283     QShortcut *ctrlTShortcutPointer = new QShortcut(QKeySequence(i18nc("The open new tab shortcut.", "Ctrl+t")), this);
284     QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
285     QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
286
287     // Connect the keyboard shortcuts to the actions.
288     connect(ctrlTShortcutPointer, SIGNAL(activated()), tabWidgetPointer, SLOT(addTab()));
289     connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
290     connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
291
292     // Load the initial website.
293     tabWidgetPointer->loadInitialWebsite();
294 }
295
296 void BrowserWindow::addOrEditDomainSettings() const
297 {
298     // Remove the focus from the URL line edit.
299     urlLineEditPointer->clearFocus();
300
301     // Create the domain settings dialog pointer.
302     DomainSettingsDialog *domainSettingsDialogPointer;
303
304     // Get the current domain settings name.
305     QString &currentDomainSettingsName = tabWidgetPointer->getDomainSettingsName();
306
307     // Run the commands according to the current domain settings status.
308     if (currentDomainSettingsName == QStringLiteral(""))  // Domain settings are not currently applied.
309     {
310         // Instruct the domain settings dialog to add a new domain.
311         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
312     }
313     else  // Domain settings are currently applied.
314     {
315         // Instruct the domain settings dialog to edit the current domain.
316         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsName);
317     }
318
319     // Set the dialog window title.
320     domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
321
322     // Set the modality.
323     domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
324
325     // Show the dialog.
326     domainSettingsDialogPointer->show();
327
328     // Reload the tabs when domain settings are updated.
329     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
330 }
331
332 void BrowserWindow::back() const
333 {
334     // Remove the focus from the URL line edit.
335     urlLineEditPointer->clearFocus();
336
337     // Go back.
338     tabWidgetPointer->back();
339 }
340
341 void BrowserWindow::clearUrlLineEditFocus() const
342 {
343     // Remove the focus from the URL line edit.
344     urlLineEditPointer->clearFocus();
345 }
346
347 void BrowserWindow::escape() const
348 {
349     // Exit full screen browsing if it is enabled.
350     if (fullScreenActionPointer->isChecked())
351         fullScreenActionPointer->trigger();
352 }
353
354 void BrowserWindow::fileNew() const
355 {
356     // Display a new instance of Privacy Browser.
357     (new BrowserWindow)->show();
358 }
359
360 void BrowserWindow::forward() const
361 {
362     // Remove the focus from the URL line edit.
363     urlLineEditPointer->clearFocus();
364
365     // Go forward.
366     tabWidgetPointer->forward();
367 }
368
369 void BrowserWindow::fullScreenRequested(const bool toggleOn)
370 {
371     // Toggle full screen mode.
372     if (toggleOn)  // Turn full screen mode on.
373     {
374         // Set the window to be full screen.
375         fullScreenActionPointer->setFullScreen(window(), true);
376
377         // Hide all the bars.
378         menuBar()->setVisible(false);
379         navigationToolBarPointer->setVisible(false);
380         urlToolBarPointer->setVisible(false);
381         tabWidgetPointer->setTabBarVisible(false);
382         statusBar()->setVisible(false);
383     }
384     else  // Turn full screen mode off.
385     {
386         // Set the window to not be full screen.
387         fullScreenActionPointer->setFullScreen(window(), false);
388
389         // Show all the bars.
390         menuBar()->setVisible(true);
391         navigationToolBarPointer->setVisible(true);
392         urlToolBarPointer->setVisible(true);
393         tabWidgetPointer->setTabBarVisible(true);
394         statusBar()->setVisible(true);
395     }
396 }
397
398 void BrowserWindow::getZoomFactorFromUser()
399 {
400     // Create an OK flag.
401     bool okClicked;
402
403     // 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.
404     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
405                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
406                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
407
408     // Update the zoom factor if the user clicked OK.
409     if (okClicked)
410     {
411         // Update the current zoom factor.
412         currentZoomFactor = newZoomFactor;
413
414         // Set the new zoom factor.
415         tabWidgetPointer->applyOnTheFlyZoomFactor(newZoomFactor);
416
417         // Update the on-the-fly action text.
418         updateZoomFactorAction(newZoomFactor);
419     }
420 }
421
422 void BrowserWindow::home() const
423 {
424     // Remove the focus from the URL line edit.
425     urlLineEditPointer->clearFocus();
426
427     // Go home.
428     tabWidgetPointer->home();
429 }
430
431 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
432 {
433     // Remove the focus from the URL line edit.
434     urlLineEditPointer->clearFocus();
435
436     // Load the URL.
437     tabWidgetPointer->loadUrlFromLineEdit(url);
438 }
439
440 void BrowserWindow::refresh() const
441 {
442     // Remove the focus from the URL line edit.
443     urlLineEditPointer->clearFocus();
444
445     // Refresh the web page.
446     tabWidgetPointer->refresh();
447 }
448
449 void BrowserWindow::showCookiesDialog()
450 {
451     // Remove the focus from the URL line edit.
452     urlLineEditPointer->clearFocus();
453
454     // Instantiate the cookie settings dialog.
455     CookiesDialog *cookiesDialogPointer = new CookiesDialog(tabWidgetPointer->getCookieList());
456
457     // Show the dialog.
458     cookiesDialogPointer->show();
459
460     // Connect the dialog signals.
461     connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), tabWidgetPointer, SLOT(addCookieToStore(QNetworkCookie)));
462     connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), tabWidgetPointer, SLOT(deleteAllCookies()));
463     connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), tabWidgetPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
464 }
465
466 void BrowserWindow::showDownloadLocationBrowseDialog() const
467 {
468     // Get the current download location.
469     QString currentDownloadLocation = downloadLocationComboBoxPointer->currentText();
470
471     // Resolve the system download directory if specified.
472     if (currentDownloadLocation == QStringLiteral("System Download Directory"))
473         currentDownloadLocation = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
474
475     // Get the new download location.
476     QString newDownloadLocation = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download location dialog caption", "Select Download Location"), currentDownloadLocation);
477
478     // Populate the download location combo box according to the new download location.
479     if (newDownloadLocation == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation))  // The default download location was selected.
480     {
481         // Populate the download location with the default text.
482         downloadLocationComboBoxPointer->setCurrentText("System Download Directory");
483     }
484     else if (newDownloadLocation != QStringLiteral(""))  // A different directory was selected.
485     {
486         // Populate the download location.
487         downloadLocationComboBoxPointer->setCurrentText(newDownloadLocation);
488     }
489 }
490
491 void BrowserWindow::showDomainSettingsDialog() const
492 {
493     // Remove the focus from the URL line edit.
494     urlLineEditPointer->clearFocus();
495
496     // Instantiate the domain settings dialog.
497     DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
498
499     // Show the dialog.
500     domainSettingsDialogPointer->show();
501
502     // Reload the tabs when domain settings are updated.
503     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
504 }
505
506 void BrowserWindow::showProgressBar(const int &progress) const
507 {
508     // Set the progress bar value.
509     progressBarPointer->setValue(progress);
510
511     // Show the progress bar.
512     progressBarPointer->show();
513 }
514
515 void BrowserWindow::showSettingsDialog()
516 {
517     // Create the settings widgets.
518     QWidget *privacySettingsWidgetPointer = new QWidget;
519     QWidget *generalSettingsWidgetPointer = new QWidget;
520
521     // Instantiate the settings UI.
522     Ui::PrivacySettings privacySettingsUi;
523     Ui::GeneralSettings generalSettingsUi;
524
525     // Setup the UI to display the settings widgets.
526     privacySettingsUi.setupUi(privacySettingsWidgetPointer);
527     generalSettingsUi.setupUi(generalSettingsWidgetPointer);
528
529     // Get handles for the widgets.
530     QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
531     userAgentLabelPointer = privacySettingsUi.userAgentLabel;
532     QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
533     searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
534     downloadLocationComboBoxPointer = generalSettingsUi.kcfg_downloadLocation;
535     QPushButton *browseButtonPointer = generalSettingsUi.browseButton;
536
537     // Populate the combo box labels.
538     updateUserAgentLabel(userAgentComboBoxPointer->currentText());
539     updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
540
541     // Update the labels when the combo boxes change.
542     connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
543     connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
544
545     // Connect the download location directory browse button.
546     connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadLocationBrowseDialog()));
547
548     // Instantiate a settings config dialog from the settings.kcfg file.
549     configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
550
551     // Add the settings widgets as config dialog pages.
552     configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
553     configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
554
555     // Prevent interaction with the parent window while the dialog is open.
556     configDialogPointer->setWindowModality(Qt::WindowModal);
557
558     // Make it so.
559     configDialogPointer->show();
560
561     // TODO.  KConfigDialog does not respect expanding size policies.  <https://redmine.stoutner.com/issues/823>
562     //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
563     //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
564     //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
565     //configDialogPointer->adjustSize();
566
567     // Expand the config dialog.
568     configDialogPointer->resize(1000, 500);
569
570     // Apply the settings when they are updated.
571     connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyApplicationSettings()));
572     connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
573 }
574
575 QSize BrowserWindow::sizeHint() const
576 {
577     // Return the default window size.
578     return QSize(1500, 1200);
579 }
580
581 void BrowserWindow::toggleDomStorage() const
582 {
583     // Remove the focus from the URL line edit.
584     urlLineEditPointer->clearFocus();
585
586     // Toggle DOM storage.
587     tabWidgetPointer->toggleDomStorage();
588 }
589
590 void BrowserWindow::toggleJavaScript() const
591 {
592     // Remove the focus from the URL line edit.
593     urlLineEditPointer->clearFocus();
594
595     // Toggle JavaScript.
596     tabWidgetPointer->toggleJavaScript();
597 }
598
599 void BrowserWindow::toggleLocalStorage() const
600 {
601     // Remove the focus from the URL line edit.
602     urlLineEditPointer->clearFocus();
603
604     // Toggle local storage.
605     tabWidgetPointer->toggleLocalStorage();
606 }
607
608 void BrowserWindow::toggleFullScreen()
609 {
610     // Toggle the full screen status.
611     if (fullScreenActionPointer->isChecked())  // Enable full screen browsing mode.
612     {
613         // Enable full screen mode.
614         fullScreenActionPointer->setFullScreen(window(), true);
615
616         // Hide the menu bar if specified.
617         if (Settings::fullScreenHideMenuBar())
618             menuBar()->setVisible(false);
619
620         // Hide the toolbars if specified.
621         if (Settings::fullScreenHideToolBars())
622         {
623             navigationToolBarPointer->setVisible(false);
624             urlToolBarPointer->setVisible(false);
625         }
626
627         // Hide the tab bar if specified.
628         if (Settings::fullScreenHideTabBar())
629             tabWidgetPointer->setTabBarVisible(false);
630
631         // Hide the status bar if specified.
632         if (Settings::fullScreenHideStatusBar())
633             statusBar()->setVisible(false);
634     }
635     else  // Disable full screen browsing mode.
636     {
637         // Disable full screen mode.
638         fullScreenActionPointer->setFullScreen(window(), false);
639
640         // Show the menu bar.
641         menuBar()->setVisible(true);
642
643         // Show the toolbars.
644         navigationToolBarPointer->setVisible(true);
645         urlToolBarPointer->setVisible(true);
646
647         // Show the tab bar.
648         tabWidgetPointer->setTabBarVisible(true);
649
650         // Show the status bar.
651         statusBar()->setVisible(true);
652     }
653 }
654
655 void BrowserWindow::updateCookiesAction(const int numberOfCookies) const
656 {
657     // Update the action text.
658     cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", numberOfCookies));
659 }
660
661 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
662 {
663     // Set the action checked status.
664     domStorageActionPointer->setChecked(isEnabled);
665 }
666
667 void BrowserWindow::updateDomainSettingsIndicator(const bool status)
668 {
669     // Set the domain palette according to the status.
670     if (status)
671         urlLineEditPointer->setPalette(domainSettingsPalette);
672     else
673         urlLineEditPointer->setPalette(noDomainSettingsPalette);
674 }
675
676 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
677 {
678     // Update the JavaScript status.
679     javaScriptEnabled = isEnabled;
680
681     // Set the icon according to the status.
682     if (javaScriptEnabled)
683         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/javascript-warning")));
684     else
685         javaScriptActionPointer->setIcon(QIcon(QStringLiteral(":/icons/privacy-mode")));
686
687     // Set the action checked status.
688     javaScriptActionPointer->setChecked(javaScriptEnabled);
689
690     // Update the status of the DOM storage action.
691     domStorageActionPointer->setEnabled(javaScriptEnabled & localStorageEnabled);
692 }
693
694 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled)
695 {
696     // Update the local storage status.
697     localStorageEnabled = isEnabled;
698
699     // Update the icon.
700     if (localStorageEnabled)
701         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota-high")));
702     else
703         localStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("disk-quota")));
704
705     // Set the action checked status.
706     localStorageActionPointer->setChecked(localStorageEnabled);
707
708     // Update the status of the DOM storage action.
709     domStorageActionPointer->setEnabled(localStorageEnabled & javaScriptEnabled);
710 }
711
712 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine, const bool &updateCustomSearchEngineStatus)
713 {
714     // Initialize the custom search engine flag.
715     bool customSearchEngine = false;
716
717     if (searchEngine == "Mojeek")  // Mojeek.
718     {
719         // Check the Mojeek user agent action.
720         searchEngineMojeekActionPointer->setChecked(true);
721
722         // Update the search engine menu action text.
723         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Mojeek"));
724     }
725     else if (searchEngine == "Monocles")  // Monocles.
726     {
727         // Check the Monocles user agent action.
728         searchEngineMonoclesActionPointer->setChecked(true);
729
730         // Update the search engine menu action text.
731         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Monocles"));
732     }
733     else if (searchEngine == "MetaGer")  // MetaGer.
734     {
735         // Check the MetaGer user agent action.
736         searchEngineMetagerActionPointer->setChecked(true);
737
738         // Update the search engine menu action text.
739         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - MetaGer"));
740     }
741     else if (searchEngine == "Google")  // Google.
742     {
743         // Check the Google user agent action.
744         searchEngineGoogleActionPointer->setChecked(true);
745
746         // Update the search engine menu action text.
747         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Google"));
748     }
749     else if (searchEngine == "Bing")  // Bing.
750     {
751         // Check the Bing user agent action.
752         searchEngineBingActionPointer->setChecked(true);
753
754         // Update the search engine menu action text.
755         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Bing"));
756     }
757     else if (searchEngine == "Yahoo")  // Yahoo.
758     {
759         // Check the Yahoo user agent action.
760         searchEngineYahooActionPointer->setChecked(true);
761
762         // Update the search engine menu action text.
763         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Yahoo"));
764     }
765     else  // Custom search engine.
766     {
767         // Check the user agent.
768         searchEngineCustomActionPointer->setChecked(true);
769
770         // Update the search engine menu action text.
771         searchEngineMenuActionPointer->setText(i18nc("The main search engine menu action", "Search Engine - Custom"));
772
773         // Set the custom search engine text.
774         searchEngineCustomActionPointer->setText(searchEngine);
775
776         // Set the custom search engine flag.
777         customSearchEngine = true;
778     }
779
780     // Update the custom search engine enabled boolean.
781     if (updateCustomSearchEngineStatus)
782         customSearchEngineEnabled = customSearchEngine;
783
784     // Format the custom search engine.
785     if (customSearchEngineEnabled)
786     {
787         // Enable the custom search engine.
788         searchEngineCustomActionPointer->setEnabled(true);
789     }
790     else
791     {
792         // Disable the custom search engine.
793         searchEngineCustomActionPointer->setEnabled(false);
794
795         // Reset the custom search engine text.
796         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
797     }
798 }
799
800 void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool &updateCustomUserAgentStatus)
801 {
802     // Initialize the custom user agent flag.
803     bool customUserAgent = false;
804
805     // Check the indicated on-the-fly user agent.
806     if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT)  // Privacy Browser.
807     {
808         // Check the Privacy Browser user agent action.
809         userAgentPrivacyBrowserActionPointer->setChecked(true);
810
811         // Update the user agent menu action text.
812         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
813     }
814     else if (userAgent == TabWidget::webEngineDefaultUserAgent)  // WebEngine default.
815     {
816         // check the WebEngine default user agent action.
817         userAgentWebEngineDefaultActionPointer->setChecked(true);
818
819         // Update the user agent menu action text.
820         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - WebEngine default"));
821     }
822     else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT)  // Firefox on Linux.
823     {
824         // Check the Firefox on Linux user agent action.
825         userAgentFirefoxLinuxActionPointer->setChecked(true);
826
827         // Update the user agent menu action text.
828         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Linux"));
829     }
830     else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT)  // Chromium on Linux.
831     {
832         // Check the Chromium on Linux user agent action.
833         userAgentChromiumLinuxActionPointer->setChecked(true);
834
835         // Update the user agent menu action text.
836         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chromium on Linux"));
837     }
838     else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT)  // Firefox on Windows.
839     {
840         // Check the Firefox on Windows user agent action.
841         userAgentFirefoxWindowsActionPointer->setChecked(true);
842
843         // Update the user agent menu action text.
844         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Firefox on Windows"));
845     }
846     else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT)  // Chrome on Windows.
847     {
848         // Check the Chrome on Windows user agent action.
849         userAgentChromeWindowsActionPointer->setChecked(true);
850
851         // Update the user agent menu action text.
852         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Chrome on Windows"));
853     }
854     else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT)  // Edge on Windows.
855     {
856         // Check the Edge on Windows user agent action.
857         userAgentEdgeWindowsActionPointer->setChecked(true);
858
859         // Update the user agent menu action text.
860         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Edge on Windows"));
861     }
862     else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT)  // Safari on macOS.
863     {
864         // Check the Safari on macOS user agent action.
865         userAgentSafariMacosActionPointer->setChecked(true);
866
867         // Update the user agent menu action text.
868         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Safari on macOS"));
869     }
870     else  // Custom user agent.
871     {
872         // Check the user agent.
873         userAgentCustomActionPointer->setChecked(true);
874
875         // Update the user agent menu action text.
876         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Custom"));
877
878         // Set the custom user agent text.
879         userAgentCustomActionPointer->setText(userAgent);
880
881         // Set the custom user agent flag.
882         customUserAgent = true;
883     }
884
885     // Update the custom user agent enabled boolean.
886     if (updateCustomUserAgentStatus)
887         customUserAgentEnabled = customUserAgent;
888
889
890     // Format the custom user agent.
891     if (customUserAgentEnabled)
892     {
893         // Enable the custom user agent.
894         userAgentCustomActionPointer->setEnabled(true);
895     }
896     else
897     {
898         // Disable the custom user agent.
899         userAgentCustomActionPointer->setEnabled(false);
900
901         // Reset the custom user agent text.
902         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
903     }
904 }
905
906 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
907 {
908     // Set the current zoom factor.
909     currentZoomFactor = zoomFactor;
910
911     // Update the zoom factor action text, formatting the double with 2 decimal places.
912     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
913 }
914
915 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
916 {
917     // Update the search engine label.
918     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
919 }
920
921 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
922 {
923     // Update the URL line edit if it does not have focus.
924     if (!urlLineEditPointer->hasFocus())
925     {
926         // Get the new URL string.
927         QString newUrlString = newUrl.toString();
928
929         // Update the URL line edit.
930         urlLineEditPointer->setText(newUrlString);
931
932         // Set the focus if the new URL is blank.
933         if (newUrlString == QStringLiteral(""))
934             urlLineEditPointer->setFocus();
935     }
936
937     // Store the current URL.
938     currentUrl = newUrl;
939 }
940
941 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
942 {
943     // Update the user agent label.
944     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
945 }