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