2d647948f21f8663a2b23d4aa3e5aa67860c479f
[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/DomainSettingsDialog.h"
26 #include "helpers/SearchEngineHelper.h"
27 #include "helpers/UserAgentHelper.h"
28
29 // KDE Frameworks headers.
30 #include <KActionCollection>
31 #include <KToolBar>
32
33 // Qt toolkit headers.
34 #include <QInputDialog>
35 #include <QStatusBar>
36
37 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
38 {
39     // Instantiate the main view pointer.
40     browserViewPointer = new BrowserView(this);
41
42     // Set the main view as the central widget.
43     setCentralWidget(browserViewPointer);
44
45     // Get a handle for the action collection.
46     KActionCollection *actionCollectionPointer = this->actionCollection();
47
48     // Add the standard actions.
49     QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
50     QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
51     KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
52     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
53     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
54     KStandardAction::preferences(this, SLOT(settingsConfigure()), actionCollectionPointer);
55     KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
56
57     // Add the custom actions.
58     userAgentPrivacyBrowserActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_privacy_browser"));
59     userAgentFirefoxLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_linux"));
60     userAgentChromiumLinuxActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chromium_linux"));
61     userAgentFirefoxWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_firefox_windows"));
62     userAgentChromeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_chrome_windows"));
63     userAgentEdgeWindowsActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_edge_windows"));
64     userAgentSafariMacosActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_safari_macos"));
65     userAgentCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("user_agent_custom"));
66     zoomFactorActionPointer = actionCollectionPointer->addAction(QStringLiteral("zoom_factor"));
67     searchEngineMojeekActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_mojeek"));
68     searchEngineMonoclesActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_monocles"));
69     searchEngineMetagerActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_metager"));
70     searchEngineGoogleActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_google"));
71     searchEngineBingActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_bing"));
72     searchEngineYahooActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_yahoo"));
73     searchEngineCustomActionPointer = actionCollectionPointer->addAction(QStringLiteral("search_engine_custom"));
74     javaScriptActionPointer = actionCollectionPointer->addAction(QStringLiteral("javascript"));
75     domainSettingsActionPointer = actionCollectionPointer->addAction(QStringLiteral("domain_settings"));
76
77     // Create the action groups
78     QActionGroup *userAgentActionGroupPointer = new QActionGroup(this);
79     QActionGroup *searchEngineActionGroupPointer = new QActionGroup(this);
80
81     // Add the actions to the groups.
82     userAgentActionGroupPointer->addAction(userAgentPrivacyBrowserActionPointer);
83     userAgentActionGroupPointer->addAction(userAgentFirefoxLinuxActionPointer);
84     userAgentActionGroupPointer->addAction(userAgentChromiumLinuxActionPointer);
85     userAgentActionGroupPointer->addAction(userAgentFirefoxWindowsActionPointer);
86     userAgentActionGroupPointer->addAction(userAgentChromeWindowsActionPointer);
87     userAgentActionGroupPointer->addAction(userAgentEdgeWindowsActionPointer);
88     userAgentActionGroupPointer->addAction(userAgentSafariMacosActionPointer);
89     userAgentActionGroupPointer->addAction(userAgentCustomActionPointer);
90     searchEngineActionGroupPointer->addAction(searchEngineMojeekActionPointer);
91     searchEngineActionGroupPointer->addAction(searchEngineMonoclesActionPointer);
92     searchEngineActionGroupPointer->addAction(searchEngineMetagerActionPointer);
93     searchEngineActionGroupPointer->addAction(searchEngineGoogleActionPointer);
94     searchEngineActionGroupPointer->addAction(searchEngineBingActionPointer);
95     searchEngineActionGroupPointer->addAction(searchEngineYahooActionPointer);
96     searchEngineActionGroupPointer->addAction(searchEngineCustomActionPointer);
97
98     // Set some actions to be checkable.
99     userAgentPrivacyBrowserActionPointer->setCheckable(true);
100     userAgentFirefoxLinuxActionPointer->setCheckable(true);
101     userAgentChromiumLinuxActionPointer->setCheckable(true);
102     userAgentFirefoxWindowsActionPointer->setCheckable(true);
103     userAgentChromeWindowsActionPointer->setCheckable(true);
104     userAgentEdgeWindowsActionPointer->setCheckable(true);
105     userAgentSafariMacosActionPointer->setCheckable(true);
106     userAgentCustomActionPointer->setCheckable(true);
107     searchEngineMojeekActionPointer->setCheckable(true);
108     searchEngineMonoclesActionPointer->setCheckable(true);
109     searchEngineMetagerActionPointer->setCheckable(true);
110     searchEngineGoogleActionPointer->setCheckable(true);
111     searchEngineBingActionPointer->setCheckable(true);
112     searchEngineYahooActionPointer->setCheckable(true);
113     searchEngineCustomActionPointer->setCheckable(true);
114
115     // Set the non-mutable action text.
116     userAgentPrivacyBrowserActionPointer->setText(UserAgentHelper::PRIVACY_BROWSER_TRANSLATED);
117     userAgentFirefoxLinuxActionPointer->setText(UserAgentHelper::FIREFOX_LINUX_TRANSLATED);
118     userAgentChromiumLinuxActionPointer->setText(UserAgentHelper::CHROMIUM_LINUX_TRANSLATED);
119     userAgentFirefoxWindowsActionPointer->setText(UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED);
120     userAgentChromeWindowsActionPointer->setText(UserAgentHelper::CHROME_WINDOWS_TRANSLATED);
121     userAgentEdgeWindowsActionPointer->setText(UserAgentHelper::EDGE_WINDOWS_TRANSLATED);
122     userAgentSafariMacosActionPointer->setText(UserAgentHelper::SAFARI_MACOS_TRANSLATED);
123     searchEngineMojeekActionPointer->setText(i18nc("Search engine", "Mojeek"));
124     searchEngineMonoclesActionPointer->setText(i18nc("Search engine", "Monocles"));
125     searchEngineMetagerActionPointer->setText(i18nc("Search engine", "MetaGer"));
126     searchEngineGoogleActionPointer->setText(i18nc("Search engine", "Google"));
127     searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
128     searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
129     javaScriptActionPointer->setText(i18nc("JavaScript button", "JavaScript"));
130     domainSettingsActionPointer->setText(i18nc("Domain Settings button", "Domain Settings"));
131
132     // Set the action icons.
133     userAgentPrivacyBrowserActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
134     userAgentFirefoxLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
135     userAgentChromiumLinuxActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
136     userAgentFirefoxWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("firefox-esr")));
137     userAgentChromeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("chromium")));
138     userAgentEdgeWindowsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
139     userAgentSafariMacosActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
140     userAgentCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("user-group-properties")));
141     searchEngineMojeekActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
142     searchEngineMonoclesActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
143     searchEngineMetagerActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
144     searchEngineGoogleActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-google")));
145     searchEngineBingActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
146     searchEngineYahooActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("im-yahoo")));
147     searchEngineCustomActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("search")));
148     zoomFactorActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
149     domainSettingsActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("network-server-symbolic")));
150
151     // Update the on-the-fly menus.
152     connect(browserViewPointer, SIGNAL(userAgentUpdated(QString)), this, SLOT(updateOnTheFlyUserAgent(QString)));
153     connect(browserViewPointer, SIGNAL(zoomFactorUpdated(double)), this, SLOT(updateOnTheFlyZoomFactor(double)));
154     connect(browserViewPointer, SIGNAL(searchEngineUpdated(QString)), this, SLOT(updateOnTheFlySearchEngine(QString)));
155
156     // Apply the on-the-fly settings when selected.
157     connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
158     connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
159
160     // Display dialogs.
161     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
162
163     // Connect the URL toolbar actions.
164     connect(javaScriptActionPointer, SIGNAL(triggered()), this, SLOT(toggleJavaScript()));
165     connect(domainSettingsActionPointer, SIGNAL(triggered()), this, SLOT(openDomainSettings()));
166
167     // Update the URL toolbar actions.
168     connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
169     connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
170     connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
171
172     // Setup the GUI based on the browser_ui.rc file.
173     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
174
175     // Get a handle for the URL toolbar.
176     KToolBar *urlToolBarPointer = toolBar(QStringLiteral("url_toolbar"));
177
178     // Create a URL line edit.
179     urlLineEditPointer = new KLineEdit();
180
181     // Populate the URL toolbar.
182     urlToolBarPointer->addWidget(urlLineEditPointer);
183     urlToolBarPointer->addAction(javaScriptActionPointer);
184     urlToolBarPointer->addAction(domainSettingsActionPointer);
185
186     // Load a new URL from the URL line edit.
187     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
188
189     // Update the URL line edit on page loads.
190     connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QString)), this, SLOT(updateUrlLineEdit(QString)));
191
192     // Get a handle for the status bar.
193     QStatusBar *statusBarPointer = statusBar();
194
195     // Update the status bar with the URL when a link is hovered.
196     connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
197
198     // Get the URL line edit palettes.
199     noDomainSettingsPalette = urlLineEditPointer->palette();
200     domainSettingsPalette = urlLineEditPointer->palette();
201
202     // Modify the domain settings palette.
203     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
204
205     // Update the applied palette.
206     connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool)), this, SLOT(updateDomainSettingsIndicator(bool)));
207
208     // Load the initial website.
209     browserViewPointer->loadInitialWebsite();
210 }
211
212 void BrowserWindow::back() const
213 {
214     // Remove the focus from the URL line edit.
215     urlLineEditPointer->clearFocus();
216
217     // Go back.
218     browserViewPointer->back();
219 }
220
221 void BrowserWindow::fileNew() const
222 {
223     // Display a new instance of Privacy Browser.
224     (new BrowserWindow)->show();
225 }
226
227 void BrowserWindow::forward() const
228 {
229     // Remove the focus from the URL line edit.
230     urlLineEditPointer->clearFocus();
231
232     // Go forward.
233     browserViewPointer->forward();
234 }
235
236 void BrowserWindow::getZoomFactorFromUser()
237 {
238     // Create an OK flag.
239     bool okClicked;
240
241     // 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.
242     double newZoomFactor = QInputDialog::getDouble(this, i18nc("The on-the-fly zoom factor dialog title", "On-The-Fly Zoom Factor"),
243                                                    i18nc("The instruction text of the on-the-fly zoom factor dialog", "Enter a zoom factor between 0.25 and 5.00"),
244                                                    currentZoomFactor, .025, 5.00, 2, &okClicked, Qt::WindowFlags(), 0.25);
245
246     // Update the zoom factor if the user clicked OK.
247     if (okClicked)
248     {
249         // Update the current zoom factor.
250         currentZoomFactor = newZoomFactor;
251
252         // Set the new zoom factor.
253         browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
254
255         // Update the on-the-fly action text.
256         updateOnTheFlyZoomFactor(newZoomFactor);
257     }
258 }
259
260 void BrowserWindow::home() const
261 {
262     // Remove the focus from the URL line edit.
263     urlLineEditPointer->clearFocus();
264
265     // Go home.
266     browserViewPointer->home();
267 }
268
269 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
270 {
271     // Remove the focus from the URL line edit.
272     urlLineEditPointer->clearFocus();
273
274     // Load the URL.
275     browserViewPointer->loadUrlFromLineEdit(url);
276 }
277
278 void BrowserWindow::openDomainSettings() const
279 {
280     // Remove the focus from the URL line edit.
281     urlLineEditPointer->clearFocus();
282
283     // Instantiate the domain settings window.
284     DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
285
286     // Set the dialog window title.
287     domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
288
289     // Set the modality.
290     domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
291
292     // Show the dialog.
293     domainSettingsDialogPointer->show();
294
295     // Reload the tabs when domain settings are updated.
296     connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
297 }
298
299 void BrowserWindow::refresh() const
300 {
301     // Remove the focus from the URL line edit.
302     urlLineEditPointer->clearFocus();
303
304     // Refresh the web page.
305     browserViewPointer->refresh();
306 }
307
308 void BrowserWindow::toggleJavaScript() const
309 {
310     // Remove the focus from the URL line edit.
311     urlLineEditPointer->clearFocus();
312
313     // Toggle JavaScript.
314     browserViewPointer->toggleJavaScript();
315 }
316
317 QSize BrowserWindow::sizeHint() const
318 {
319     // Return the default window size.
320     return QSize(1500, 1200);
321 }
322
323 void BrowserWindow::settingsConfigure()
324 {
325     // Check to make sure the dialog box isn't already displayed.
326     if (KConfigDialog::exists(QStringLiteral("settings")))
327     {
328         // Show the existing config dialog if it is hidden.
329         configDialogPointer->show();
330
331         // Raise the existing config dialog if it is below other windows.
332         configDialogPointer->raise();
333
334         // Restore the existing config dialog if it has been minimized.
335         if (configDialogPointer->isMinimized()) {
336             configDialogPointer->showNormal();
337         }
338
339         // Activate the existing config dialog, which brings its virtual desktop into focus.
340         configDialogPointer->activateWindow();
341     }
342     else
343     {
344         // Create the settings widgets.
345         QWidget *privacySettingsWidgetPointer = new QWidget;
346         QWidget *generalSettingsWidgetPointer = new QWidget;
347
348         // Instantiate the settings UI.
349         Ui::PrivacySettings privacySettingsUi;
350         Ui::GeneralSettings generalSettingsUi;
351
352         // Setup the UI to display the settings widgets.
353         privacySettingsUi.setupUi(privacySettingsWidgetPointer);
354         generalSettingsUi.setupUi(generalSettingsWidgetPointer);
355
356         // Get handles for the widgets.
357         QComboBox *userAgentComboBoxPointer = privacySettingsUi.kcfg_userAgent;
358         userAgentLabelPointer = privacySettingsUi.userAgentLabel;
359         QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine;
360         searchEngineLabelPointer = generalSettingsUi.searchEngineLabel;
361
362         // Populate the combo box labels.
363         updateUserAgentLabel(userAgentComboBoxPointer->currentText());
364         updateSearchEngineLabel(searchEngineComboBoxPointer->currentText());
365
366         // Update the labels when the combo boxes change.
367         connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString)));
368         connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString)));
369
370         // Instantiate a settings config dialog from the settings.kcfg file.
371         configDialogPointer = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
372
373         // Add the settings widgets as config dialog pages.
374         configDialogPointer->addPage(privacySettingsWidgetPointer, i18nc("@title:tab", "Privacy"), QStringLiteral("privacy-browser"));
375         configDialogPointer->addPage(generalSettingsWidgetPointer, i18nc("@title:tab", "General"), QStringLiteral("breeze-settings"));
376
377         // Delete the config dialog when it is closed.
378         configDialogPointer->setAttribute(Qt::WA_DeleteOnClose);
379
380         // Make it so.
381         configDialogPointer->show();
382
383         // TODO.  KConfigDialog does not respect expanding size policies.  <https://redmine.stoutner.com/issues/823>
384         //configDialogPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
385         //privacySettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
386         //generalSettingsWidgetPointer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
387         //configDialogPointer->adjustSize();
388
389         // Expand the config dialog.
390         configDialogPointer->resize(1000, 500);
391
392         // Apply the settings when they are updated.
393         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
394         connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
395     }
396 }
397
398 void BrowserWindow::updateDomainSettingsIndicator(const bool &status) const
399 {
400     // Set the domain palette according to the status.
401     if (status) urlLineEditPointer->setPalette(domainSettingsPalette);
402     else urlLineEditPointer->setPalette(noDomainSettingsPalette);
403 }
404
405 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled) const
406 {
407     // Set the icon according to the status.
408     if (isEnabled) javaScriptActionPointer->setIcon(QIcon(":/icons/javascript-warning"));
409     else javaScriptActionPointer->setIcon(QIcon(":/icons/privacy-mode"));
410 }
411
412 void BrowserWindow::updateOnTheFlySearchEngine(const QString &searchEngine) const
413 {
414     // Initialize the custom search engine flag.
415     bool customSearchEngine = false;
416
417     if (searchEngine == "Mojeek")  // Mojeek.
418     {
419         searchEngineMojeekActionPointer->setChecked(true);
420     }
421     else if (searchEngine == "Monocles")  // Monocles.
422     {
423         searchEngineMonoclesActionPointer->setChecked(true);
424     }
425     else if (searchEngine == "MetaGer")  // MetaGer.
426     {
427         searchEngineMetagerActionPointer->setChecked(true);
428     }
429     else if (searchEngine == "Google")  // Google.
430     {
431         searchEngineGoogleActionPointer->setChecked(true);
432     }
433     else if (searchEngine == "Bing")  // Bing.
434     {
435         searchEngineBingActionPointer->setChecked(true);
436     }
437     else if (searchEngine == "Yahoo")  // Yahoo.
438     {
439         searchEngineYahooActionPointer->setChecked(true);
440     }
441     else  // Custom search engine.
442     {
443         // Check the user agent.
444         searchEngineCustomActionPointer->setChecked(true);
445
446         // Set the custom search engine flag.
447         customSearchEngine = true;
448     }
449
450     // Format the custom search engine.
451     if (customSearchEngine)
452     {
453         // Enable the custom search engine.
454         searchEngineCustomActionPointer->setEnabled(true);
455
456         // Set the custom search engine text.
457         searchEngineCustomActionPointer->setText(searchEngine);
458     }
459     else
460     {
461         // Disable the custom search engine.
462         searchEngineCustomActionPointer->setEnabled(false);
463
464         // Reset the custom search engine text.
465         searchEngineCustomActionPointer->setText(i18nc("@action", "Custom"));
466     }
467 }
468
469 void BrowserWindow::updateOnTheFlyUserAgent(const QString &userAgent) const
470 {
471     // Initialize the custom user agent flag.
472     bool customUserAgent = false;
473
474     // Check the indicated on-the-fly user agent.
475     if (userAgent == UserAgentHelper::PRIVACY_BROWSER_USER_AGENT) userAgentPrivacyBrowserActionPointer->setChecked(true);  // Privacy Browser.
476     else if (userAgent == UserAgentHelper::FIREFOX_LINUX_USER_AGENT) userAgentFirefoxLinuxActionPointer->setChecked(true);  // Firefox Linux.
477     else if (userAgent == UserAgentHelper::CHROMIUM_LINUX_USER_AGENT) userAgentChromiumLinuxActionPointer->setChecked(true);  // Chromium Linux.
478     else if (userAgent == UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT) userAgentFirefoxWindowsActionPointer->setChecked(true);  // Firefox Windows.
479     else if (userAgent == UserAgentHelper::CHROME_WINDOWS_USER_AGENT) userAgentChromeWindowsActionPointer->setChecked(true);  // Chrome Windows.
480     else if (userAgent == UserAgentHelper::EDGE_WINDOWS_USER_AGENT) userAgentEdgeWindowsActionPointer->setChecked(true);  // Edge Windows.
481     else if (userAgent == UserAgentHelper::SAFARI_MACOS_USER_AGENT) userAgentSafariMacosActionPointer->setChecked(true);  // Safara macOS.
482     else  // Custom user agent.
483     {
484         // Check the user agent.
485         userAgentCustomActionPointer->setChecked(true);
486
487         // Set the custom user agent flag.
488         customUserAgent = true;
489     }
490
491
492     // Format the custom user agent.
493     if (customUserAgent)
494     {
495         // Enable the custom user agent.
496         userAgentCustomActionPointer->setEnabled(true);
497
498         // Set the custom user agent text.
499         userAgentCustomActionPointer->setText(userAgent);
500     }
501     else
502     {
503         // Disable the custom user agent.
504         userAgentCustomActionPointer->setEnabled(false);
505
506         // Reset the custom user agent text.
507         userAgentCustomActionPointer->setText(i18nc("@action", "Custom"));
508     }
509 }
510
511 void BrowserWindow::updateOnTheFlyZoomFactor(const double &zoomFactor)
512 {
513     // Set the current zoom factor.
514     currentZoomFactor = zoomFactor;
515
516     // Update the zoom factor action text, formatting the double with 2 decimal places.
517     zoomFactorActionPointer->setText(ki18nc("@action", "Zoom Factor - %1").subs(zoomFactor, 0, '0', 2).toString());
518 }
519
520 void BrowserWindow::updateSearchEngineLabel(const QString &searchEngineString) const
521 {
522     // Update the search engine label.
523     searchEngineLabelPointer->setText(SearchEngineHelper::getSearchUrl(searchEngineString));
524 }
525
526 void BrowserWindow::updateUrlLineEdit(const QString &newUrl) const
527 {
528     // Update the URL line edit if it does not have focus.
529     if (!urlLineEditPointer->hasFocus())
530     {
531         // Update the URL line edit.
532         urlLineEditPointer->setText(newUrl);
533     }
534 }
535
536 void BrowserWindow::updateUserAgentLabel(const QString &userAgentDatabaseName) const
537 {
538     // Update the user agent label.
539     userAgentLabelPointer->setText(UserAgentHelper::getUserAgentFromDatabaseName(userAgentDatabaseName));
540 }