]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/windows/BrowserWindow.cpp
c70bab18cbffbc5539d43b12b858eb2fb051eac2
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers.
21 #include "BrowserWindow.h"
22 #include "Settings.h"
23 #include "ui_SettingsPrivacy.h"
24 #include "ui_SettingsGeneral.h"
25 #include "dialogs/CookiesDialog.h"
26 #include "dialogs/DomainSettingsDialog.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
29
30 // KDE Frameworks headers.
31 #include <KActionCollection>
32 #include <KToolBar>
33
34 // Qt toolkit headers.
35 #include <QInputDialog>
36 #include <QNetworkCookie>
37 #include <QStatusBar>
38
39 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
40 {
41     // Instantiate the main view pointer.
42     browserViewPointer = new BrowserView(this);
43
44     // Set the main view as the central widget.
45     setCentralWidget(browserViewPointer);
46
47     // Get a handle for the action collection.
48     KActionCollection *actionCollectionPointer = this->actionCollection();
49
50     // Add the standard actions.
51     QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
52     QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
53     KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
54     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
55     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
56     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
57     KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
58
59     // Add the custom actions.
60     userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
61     userAgentWebEngineDefaultActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_webengine_default"));
62     userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
63     userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
64     userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
65     userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
66     userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
67     userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
68     userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
69     zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
70     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
71     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
72     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
73     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
74     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
75     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
76     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
77     QAction *domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
78     QAction *cookiesActionPointer = actionCollectionPointer->addAction(QStringLiteral("cookies"));
79     javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
80     localStorageActionPointer = actionCollectionPointer->addAction(QStringLiteral("local_storage"));
81
82     // Create the action groups
83     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
84     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
85
86     // Add the actions to the groups.
87     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
88     userAgentActionGroupPointer->addAction(userAgentWebEngineDefaultActionPointer);
89     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
90     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
91     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
92     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
93     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
94     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
95     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
96     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
97     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
98     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
99     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
100     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
101     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
102     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
103
104     // Set some actions to be checkable.
105     userAgentPrivacyBrowserActionPointer->setCheckable(true);
106     userAgentWebEngineDefaultActionPointer->setCheckable(true);
107     userAgentFirefoxLinuxActionPointer->setCheckable(true);
108     userAgentChromiumLinuxActionPointer->setCheckable(true);
109     userAgentFirefoxWindowsActionPointer->setCheckable(true);
110     userAgentChromeWindowsActionPointer->setCheckable(true);
111     userAgentEdgeWindowsActionPointer->setCheckable(true);
112     userAgentSafariMacosActionPointer->setCheckable(true);
113     userAgentCustomActionPointer->setCheckable(true);
114     searchEngineMojeekActionPointer->setCheckable(true);
115     searchEngineMonoclesActionPointer->setCheckable(true);
116     searchEngineMetagerActionPointer->setCheckable(true);
117     searchEngineGoogleActionPointer->setCheckable(true);
118     searchEngineBingActionPointer->setCheckable(true);
119     searchEngineYahooActionPointer->setCheckable(true);
120     searchEngineCustomActionPointer->setCheckable(true);
121
122     // Set the non-mutable action text.
123     userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
124     userAgentWebEngineDefaultActionPointer->setText(UserAgentHelper::WEB_ENGINE_DEFAULT_TRANSLATED);
125     userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
126     userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
127     userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
128     userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
129     userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
130     userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
131     searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
132     searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
133     searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
134     searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
135     searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
136     searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
137     domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
138     cookiesActionPointer->setText(i18nc("Cookies action", "Cookies"));
139     javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
140     localStorageActionPointer->setText(i18nc("Local Storage action", "Local Storage"));
141
142     // Set the action icons.
143     userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
144     userAgentWebEngineDefaultActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
145     userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
146     userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
147     userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
148     userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
149     userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
150     userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
151     userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
152     searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
153     searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
154     searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
155     searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
156     searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
157     searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
158     searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
159     zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
160     domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("settings-configure")));
161     cookiesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies")));
162
163     // Update the on-the-fly menus.
164     connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString)), this, SLOT(updateUserAgentActions(QString)));
165     connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
166     connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString)), this, SLOT(updateSearchEngineActions(QString)));
167
168     // Apply the on-the-fly settings when selected.
169     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
170     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
171
172     // Display dialogs.
173     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
174     connect(cookiesActionPointer, SIGNAL(triggered()), this, SLOT(openCookiesDialog()));
175     connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
176
177     // Connect the URL toolbar actions.
178     connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
179     connect(localStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleLocalStorage()));
180
181     // Update the URL toolbar actions.
182     connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
183     connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
184     connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
185     connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
186
187     // Setup the GUI based on the browser_ui.rc file.
188     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
189
190     // Get a handle for the URL toolbar.
191     KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
192
193     // Create a URL line edit.
194     urlLineEditPointer = new KLineEdit();
195
196     // Add an edit or add domain settings action to the URL line edit.
197     QAction *addOrEditDomainSettingsActionPointer = urlLineEditPointer->addAction(QIcon::fromTheme("settings-configure"), QLineEdit::TrailingPosition);
198
199     // Add or edit the current domain settings.
200     connect(addOrEditDomainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(addOrEditDomainSettings()));
201
202     // Populate the URL toolbar.
203     urlToolBarPointer->insertWidget(javaScriptActionPointer, urlLineEditPointer);
204
205     // Load a new URL from the URL line edit.
206     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
207
208     // Update the URL line edit on page loads.
209     connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
210
211     // Get a handle for the status bar.
212     QStatusBar *statusBarPointer = statusBar();
213
214     // Create a progress bar.
215     progressBarPointer = new QProgressBar();
216
217     // Add the progress bar to to the status bar.
218     statusBarPointer->addPermanentWidget(progressBarPointer);
219
220     // Update the status bar with the URL when a link is hovered.
221     connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
222
223     // Update the progress bar.
224     connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
225     connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
226
227     // Clear the URL line edit focus when requested.
228     connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
229
230     // Get the URL line edit palettes.
231     noDomainSettingsPalette = urlLineEditPointer->palette();
232     domainSettingsPalette = urlLineEditPointer->palette();
233
234     // Modify the domain settings palette.
235     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
236
237     // Update the applied palette.
238     connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
239
240     // Initialize the cookie list.
241     cookieListPointer = new QList<QNetworkCookie>;
242
243     // Add new cookies to the list.
244     connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
245
246     // Load the initial website.
247     browserViewPointer->loadInitialWebsite();
248 }
249
250 void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
251 {
252     // Check to see if the list already contains a cookie with this ID.
253     for (QNetworkCookie existingCookie : *cookieListPointer)
254     {
255         // Remove the old version of the cookie.
256         if (existingCookie.hasSameIdentifier(newCookie)) cookieListPointer->removeOne(existingCookie);
257     }
258
259     // Add the new cookie to the list.
260     cookieListPointer->append(newCookie);
261 }
262
263 void BrowserWindow::addOrEditDomainSettings() const
264 {
265     // Remove the focus from the URL line edit.
266     urlLineEditPointer->clearFocus();
267
268     // Create the domain settings dialog pointer.
269     DomainSettingsDialog *domainSettingsDialogPointer;
270
271     // Run the commands according to the current domain settings status.
272     if (currentDomainSettingsDomain == "")  // Domain settings are not currently applied.
273     {
274         // Instruct the domain settings dialog to add a new domain.
275         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
276     }
277     else  // Domain settings are currently applied.
278     {
279         // Instruct the domain settings dialog to edit the current domain.
280         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
281     }
282
283     // Set the dialog window title.
284     domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
285
286     // Set the modality.
287     domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
288
289     // Show the dialog.
290     domainSettingsDialogPointer->show();
291
292     // Reload the tabs when domain settings are updated.
293     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
294 }
295
296 void BrowserWindow::back() const
297 {
298     // Remove the focus from the URL line edit.
299     urlLineEditPointer->clearFocus();
300
301     // Go back.
302     browserViewPointer->back();
303 }
304
305 void BrowserWindow::clearUrlLineEditFocus() const
306 {
307     // Remove the focus from the URL line edit.
308     urlLineEditPointer->clearFocus();
309 }
310
311 void BrowserWindow::fileNew() const
312 {
313     // Display a new instance of Privacy Browser.
314     (new BrowserWindow)->show();
315 }
316
317 void BrowserWindow::forward() const
318 {
319     // Remove the focus from the URL line edit.
320     urlLineEditPointer->clearFocus();
321
322     // Go forward.
323     browserViewPointer->forward();
324 }
325
326 void BrowserWindow::getZoomFactorFromUser()
327 {
328     // Create an OK flag.
329     bool okClicked;
330
331     // 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.
332     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
333                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
334                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
335
336     // Update the zoom factor if the user clicked OK.
337     if (okClicked)
338     {
339         // Update the current zoom factor.
340         currentZoomFactor = newZoomFactor;
341
342         // Set the new zoom factor.
343         browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
344
345         // Update the on-the-fly action text.
346         updateZoomFactorAction(newZoomFactor);
347     }
348 }
349
350 void BrowserWindow::home() const
351 {
352     // Remove the focus from the URL line edit.
353     urlLineEditPointer->clearFocus();
354
355     // Go home.
356     browserViewPointer->home();
357 }
358
359 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
360 {
361     // Remove the focus from the URL line edit.
362     urlLineEditPointer->clearFocus();
363
364     // Load the URL.
365     browserViewPointer->loadUrlFromLineEdit(url);
366 }
367
368 void BrowserWindow::openCookiesDialog()
369 {
370     // Remove the focus from the URL line edit.
371     urlLineEditPointer->clearFocus();
372
373     // Instantiate the cookie settings dialog.
374     CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
375
376     // Show the dialog.
377     cookiesDialogPointer->show();
378
379     // Connect the dialog signals.
380     connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
381     connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
382 }
383
384 void BrowserWindow::openDomainSettings() const
385 {
386     // Remove the focus from the URL line edit.
387     urlLineEditPointer->clearFocus();
388
389     // Instantiate the domain settings dialog.
390     DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
391
392     // Show the dialog.
393     domainSettingsDialogPointer->show();
394
395     // Reload the tabs when domain settings are updated.
396     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
397 }
398
399 void BrowserWindow::refresh() const
400 {
401     // Remove the focus from the URL line edit.
402     urlLineEditPointer->clearFocus();
403
404     // Refresh the web page.
405     browserViewPointer->refresh();
406 }
407
408 void BrowserWindow::showProgressBar(const int &progress) const
409 {
410     // Set the progress bar value.
411     progressBarPointer->setValue(progress);
412
413     // Show the progress bar.
414     progressBarPointer->show();
415 }
416
417 QSize BrowserWindow::sizeHint() const
418 {
419     // Return the default window size.
420     return QSize(1500, 1200);
421 }
422
423 void BrowserWindow::settingsConfigure()
424 {
425     // Check to make sure the dialog box isn't already displayed.
426     if (KConfigDialog::exists(QStringLiteral("settings")))
427     {
428         // Show the existing config dialog if it is hidden.
429         configDialogPointer->show();
430
431         // Raise the existing config dialog if it is below other windows.
432         configDialogPointer->raise();
433
434         // Restore the existing config dialog if it has been minimized.
435         if (configDialogPointer->isMinimized()) {
436             configDialogPointer->showNormal();
437         }
438
439         // Activate the existing config dialog, which brings its virtual desktop into focus.
440         configDialogPointer->activateWindow();
441     }
442     else
443     {
444         // Create the settings widgets.
445         QWidget *privacySettingsWidgetPointer = new QWidget;
446         QWidget *generalSettingsWidgetPointer = new QWidget;
447
448         // Instantiate the settings UI.
449         Ui::PrivacySettings privacySettingsUi;
450         Ui::GeneralSettings generalSettingsUi;
451
452         // Setup the UI to display the settings widgets.
453         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
454         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
455
456         // Get handles for the widgets.
457         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
458         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
459         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
460         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
461
462         // Populate the combo box labels.
463         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
464         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
465
466         // Update the labels when the combo boxes change.
467         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
468         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
469
470         // Instantiate a settings config dialog from the settings.kcfg file.
471         configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
472
473         // Add the settings widgets as config dialog pages.
474         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
475         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
476
477         // Delete the config dialog when it is closed.
478         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
479
480         // Make it so.
481         configDialogPointer->show();
482
483         // TODO.  KConfigDialog does not respect expanding size policies.  <https://redmine.stoutner.com/issues/823>
484         //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
485         //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
486         //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
487         //configDialogPointer->adjustSize();
488
489         // Expand the config dialog.
490         configDialogPointer->resize(1000, 500);
491
492         // Apply the settings when they are updated.
493         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
494         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
495     }
496 }
497
498 void BrowserWindow::toggleJavaScript() const
499 {
500     // Remove the focus from the URL line edit.
501     urlLineEditPointer->clearFocus();
502
503     // Toggle JavaScript.
504     browserViewPointer->toggleJavaScript();
505 }
506
507 void BrowserWindow::toggleLocalStorage() const
508 {
509     // Remove the focus from the URL line edit.
510     urlLineEditPointer->clearFocus();
511
512     // Toggle JavaScript.
513     browserViewPointer->toggleLocalStorage();
514 }
515
516 void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
517 {
518     // Set the domain palette according to the status.
519     if (status) urlLineEditPointer->setPalette(domainSettingsPalette);
520     else urlLineEditPointer->setPalette(noDomainSettingsPalette);
521
522     // Store the domain.
523     currentDomainSettingsDomain = domainSettingsDomain;
524 }
525
526 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled) const
527 {
528     // Set the icon according to the status.
529     if (isEnabled) javaScriptActionPointer->setIcon(QIcon(":/icons/javascript-warning"));
530     else javaScriptActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
531 }
532
533 void BrowserWindow::updateLocalStorageAction(const bool &isEnabled) const
534 {
535     // Set the icon according to the status.
536     if (isEnabled) localStorageActionPointer->setIcon(QIcon::fromTheme("disk-quota-low"));
537     else localStorageActionPointer->setIcon(QIcon::fromTheme("disk-quota"));
538 }
539
540 void BrowserWindow::updateSearchEngineActions(const QString &searchEngine) const
541 {
542     // Initialize the custom search engine flag.
543     bool customSearchEngine = false;
544
545     if (searchEngine == "Mojeek")  // Mojeek.
546     {
547         searchEngineMojeekActionPointer->setChecked(true);
548     }
549     else if (searchEngine == "Monocles")  // Monocles.
550     {
551         searchEngineMonoclesActionPointer->setChecked(true);
552     }
553     else if (searchEngine == "MetaGer")  // MetaGer.
554     {
555         searchEngineMetagerActionPointer->setChecked(true);
556     }
557     else if (searchEngine == "Google")  // Google.
558     {
559         searchEngineGoogleActionPointer->setChecked(true);
560     }
561     else if (searchEngine == "Bing")  // Bing.
562     {
563         searchEngineBingActionPointer->setChecked(true);
564     }
565     else if (searchEngine == "Yahoo")  // Yahoo.
566     {
567         searchEngineYahooActionPointer->setChecked(true);
568     }
569     else  // Custom search engine.
570     {
571         // Check the user agent.
572         searchEngineCustomActionPointer->setChecked(true);
573
574         // Set the custom search engine flag.
575         customSearchEngine = true;
576     }
577
578     // Format the custom search engine.
579     if (customSearchEngine)
580     {
581         // Enable the custom search engine.
582         searchEngineCustomActionPointer->setEnabled(true);
583
584         // Set the custom search engine text.
585         searchEngineCustomActionPointer->setText(searchEngine);
586     }
587     else
588     {
589         // Disable the custom search engine.
590         searchEngineCustomActionPointer->setEnabled(false);
591
592         // Reset the custom search engine text.
593         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
594     }
595 }
596
597 void BrowserWindow::updateUserAgentActions(const QString &userAgent) const
598 {
599     // Initialize the custom user agent flag.
600     bool customUserAgent = false;
601
602     // Check the indicated on-the-fly user agent.
603     if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true);  // Privacy Browser.
604     else if (userAgent == BrowserView::webEngineDefaultUserAgent) userAgentWebEngineDefaultActionPointer->setChecked(true);  // WebEngine default.
605     else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true);  // Firefox Linux.
606     else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true);  // Chromium Linux.
607     else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true);  // Firefox Windows.
608     else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true);  // Chrome Windows.
609     else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true);  // Edge Windows.
610     else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true);  // Safara macOS.
611     else  // Custom user agent.
612     {
613         // Check the user agent.
614         userAgentCustomActionPointer->setChecked(true);
615
616         // Set the custom user agent flag.
617         customUserAgent = true;
618     }
619
620
621     // Format the custom user agent.
622     if (customUserAgent)
623     {
624         // Enable the custom user agent.
625         userAgentCustomActionPointer->setEnabled(true);
626
627         // Set the custom user agent text.
628         userAgentCustomActionPointer->setText(userAgent);
629     }
630     else
631     {
632         // Disable the custom user agent.
633         userAgentCustomActionPointer->setEnabled(false);
634
635         // Reset the custom user agent text.
636         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
637     }
638 }
639
640 void BrowserWindow::updateZoomFactorAction(const double &zoomFactor)
641 {
642     // Set the current zoom factor.
643     currentZoomFactor = zoomFactor;
644
645     // Update the zoom factor action text, formatting the double with 2 decimal places.
646     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
647 }
648
649 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
650 {
651     // Update the search engine label.
652     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
653 }
654
655 void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
656 {
657     // Update the URL line edit if it does not have focus.
658     if (!urlLineEditPointer->hasFocus())
659     {
660         // Update the URL line edit.
661         urlLineEditPointer->setText(newUrl.toString());
662     }
663
664     // Store the current URL.
665     currentUrl = newUrl;
666 }
667
668 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
669 {
670     // Update the user agent label.
671     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
672 }