07ce5f5d897039339513fa075e6ed401d3d67906
[PrivacyBrowserPC.git] / src / views / BrowserView.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 "BrowserView.h"
22 #include "Settings.h"
23 #include "ui_BrowserView.h"
24 #include "filters/MouseEventFilter.h"
25 #include "helpers/DomainsDatabaseHelper.h"
26 #include "helpers/SearchEngineHelper.h"
27 #include "helpers/UserAgentHelper.h"
28 #include "interceptors/UrlRequestInterceptor.h"
29 #include "windows/BrowserWindow.h"
30
31 // Qt framework headers.
32 #include <QAction>
33
34 // Initialize the public static variables.
35 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
36
37 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
38 {
39     // Instantiate the browser view UI.
40     Ui::BrowserView browserViewUi;
41
42     // Setup the UI.
43     browserViewUi.setupUi(this);
44
45     // Get handles for the views.
46     webEngineViewPointer = browserViewUi.webEngineView;
47
48     // Create an off-the-record profile (the default when no profile name is specified).
49     webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
50
51     // Create a WebEngine page.
52     webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
53
54     // Set the WebEngine page.
55     webEngineViewPointer->setPage(webEnginePagePointer);
56
57     // Get handles for the aspects of the WebEngine.
58     webEngineHistoryPointer = webEnginePagePointer->history();
59     webEngineSettingsPointer = webEngineViewPointer->settings();
60     webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
61
62     // Store a copy of each cookie when it is added.
63     connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
64
65     // Store a copy of the WebEngine default user agent.
66     webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
67
68     // Update the URL line edit when the URL changes.
69     connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
70
71     // Update the progress bar.
72     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
73     connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
74     connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
75
76     // Instantiate the mouse event filter pointer.
77     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
78
79     // Install the mouse event filter.
80     qApp->installEventFilter(mouseEventFilterPointer);
81
82     // Process mouse forward and back commands.
83     connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
84     connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
85
86     // Listen for hovered link URLs.
87     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
88
89     // Instantiate the URL request interceptor.
90     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
91
92     // Set the URL request interceptor.
93     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
94
95     // Reapply the domain settings when the host changes.
96     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
97
98     // Don't allow JavaScript to open windows.
99     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
100
101     // Allow keyboard navigation.
102     webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
103
104     // Enable full screen support.
105     webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
106
107     // Require user interaction to play media.
108     webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
109
110     // Limit WebRTC to public IP addresses.
111     webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
112
113     // Set the focus on the WebEngine view.
114     webEngineViewPointer->setFocus();
115 }
116
117 BrowserView::~BrowserView()
118 {
119     // Delay the deletion of the WebEngine page to prevent the following error:  `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
120     webEnginePagePointer->deleteLater();
121 }
122
123 void BrowserView::applyApplicationSettings()
124 {
125     // Set the search engine URL.
126     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
127
128     // Emit the update search engine actions signal.
129     emit updateSearchEngineActions(Settings::searchEngine());
130 }
131
132 // This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument.
133 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
134 void BrowserView::applyDomainSettingsAndReload()
135 {
136     // Apply the domain settings.  `true` reloads the website.
137     applyDomainSettings(webEngineViewPointer->url().host(), true);
138 }
139
140 // This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument.
141 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
142 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
143 {
144     // Apply the domain settings  `false` does not reload the website.
145     applyDomainSettings(hostname, false);
146 }
147
148 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
149 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
150 {
151     // Get the record for the hostname.
152     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
153
154     // Check if the hostname has domain settings.
155     if (domainQuery.isValid())  // The hostname has domain settings.
156     {
157         // Get the domain record.
158         QSqlRecord domainRecord = domainQuery.record();
159
160         // Set the JavaScript status.
161         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
162         {
163             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
164             {
165                 // Set the default JavaScript status.
166                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
167
168                 break;
169             }
170
171             case (DomainsDatabaseHelper::DISABLED):
172             {
173                 // Disable JavaScript.
174                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
175
176                 break;
177             }
178
179             case (DomainsDatabaseHelper::ENABLED):
180             {
181                 // Enable JavaScript.
182                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
183
184                 break;
185             }
186         }
187
188         // Set local storage.
189         switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
190         {
191             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
192             {
193                 // Set the default local storage status.
194                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
195
196                 break;
197             }
198
199             case (DomainsDatabaseHelper::DISABLED):
200             {
201                 // Disable local storage.
202                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
203
204                 break;
205             }
206
207             case (DomainsDatabaseHelper::ENABLED):
208             {
209                 // Enable local storage.
210                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
211
212                 break;
213             }
214         }
215
216         // Set the user agent.
217         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
218
219         // Check if a custom zoom factor is set.
220         if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
221         {
222             // Store the current zoom factor.
223             currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
224         }
225         else
226         {
227             // Reset the current zoom factor.
228             currentZoomFactor = Settings::zoomFactor();
229         }
230
231         // Set the zoom factor.    The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
232         webEngineViewPointer->setZoomFactor(currentZoomFactor);
233
234         // Apply the domain settings palette to the URL line edit.
235         emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
236     }
237     else  // The hostname does not have domain settings.
238     {
239         // Set the JavaScript status.
240         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
241
242         // Set local storage.
243         webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
244
245         // Set the user agent.
246         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
247
248         // Store the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
249         currentZoomFactor = Settings::zoomFactor();
250
251         // Set the zoom factor.
252         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
253
254         // Apply the no domain settings palette to the URL line edit.
255         emit updateDomainSettingsIndicator(false, QStringLiteral(""));
256     }
257
258     // Emit the update actions signals.
259     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
260     emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
261     emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
262     emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
263
264     // Reload the website if requested.
265     if (reloadWebsite)
266     {
267         webEngineViewPointer->reload();
268     }
269 }
270
271 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
272 {
273     // Store the search engine name.
274     QString searchEngineName = searchEngineActionPointer->text();
275
276     // Strip out any `&` characters.
277     searchEngineName.remove('&');
278
279     // Store the search engine string.
280     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
281 }
282
283 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
284 {
285     // Get the user agent name.
286     QString userAgentName = userAgentActionPointer->text();
287
288     // Strip out any `&` characters.
289     userAgentName.remove('&');
290
291     // Apply the user agent.
292     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
293
294     // Reload the website.
295     webEngineViewPointer->reload();
296 }
297
298 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
299 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
300 {
301     // Update the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
302     currentZoomFactor = zoomFactor;
303
304     // Set the zoom factor.
305     webEngineViewPointer->setZoomFactor(zoomFactor);
306 }
307
308 void BrowserView::back() const
309 {
310     // Go back.
311     webEngineViewPointer->back();
312 }
313
314 void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
315 {
316     // Add the cookie to the cookie list.
317     emit addCookie(cookie);
318 }
319
320 void BrowserView::deleteAllCookies() const
321 {
322     // Delete all the cookies.
323     webEngineCookieStorePointer->deleteAllCookies();
324 }
325
326 void BrowserView::forward() const
327 {
328     // Go forward.
329     webEngineViewPointer->forward();
330 }
331
332 void BrowserView::home() const
333 {
334     // Load the homepage.
335     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
336 }
337
338 void BrowserView::loadFinished() const
339 {
340     // Hide the progress bar.
341     emit hideProgressBar();
342 }
343
344 void BrowserView::loadInitialWebsite()
345 {
346     // Apply the application settings.
347     applyApplicationSettings();
348
349     // Get the arguments.
350     QStringList argumentsStringList = qApp->arguments();
351
352     // Check to see if the arguments lists contains a URL.
353     if (argumentsStringList.size() > 1)
354     {
355         // Load the URL from the arguments list.
356         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
357     }
358     else
359     {
360         // Load the homepage.
361         home();
362     }
363 }
364
365 void BrowserView::loadProgress(const int &progress) const
366 {
367     // Show the progress bar.
368     emit showProgressBar(progress);
369 }
370
371 void BrowserView::loadStarted() const
372 {
373     // Show the progress bar.
374     emit showProgressBar(0);
375 }
376
377 void BrowserView::loadUrlFromLineEdit(QString url) const
378 {
379     // Decide if the text is more likely to be a URL or a search.
380     if (url.startsWith("file://"))  // The text is likely a file URL.
381     {
382         // Load the URL.
383         webEngineViewPointer->load(QUrl::fromUserInput(url));
384     }
385     else if (url.contains("."))  // The text is likely a URL.
386     {
387         // Check if the URL does not start with a valid protocol.
388         if (!url.startsWith("http"))
389         {
390             // Add `https://` to the beginning of the URL.
391             url = "https://" + url;
392         }
393
394         // Load the URL.
395         webEngineViewPointer->load(QUrl::fromUserInput(url));
396     }
397     else  // The text is likely a search.
398     {
399         // Load the search.
400         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
401     }
402 }
403
404 void BrowserView::mouseBack() const
405 {
406     // Go back if possible.
407     if (webEngineHistoryPointer->canGoBack())
408     {
409         // Clear the URL line edit focus.
410         emit clearUrlLineEditFocus();
411
412         // Go back.
413         webEngineViewPointer->back();
414     }
415 }
416
417 void BrowserView::mouseForward() const
418 {
419     // Go forward if possible.
420     if (webEngineHistoryPointer->canGoForward())
421     {
422         // Clear the URL line edit focus.
423         emit clearUrlLineEditFocus();
424
425         // Go forward.
426         webEngineViewPointer->forward();
427     }
428 }
429
430 void BrowserView::pageLinkHovered(const QString &linkUrl) const
431 {
432     // Emit a signal so that the browser window can update the status bar.
433     emit linkHovered(linkUrl);
434 }
435
436 void BrowserView::refresh() const
437 {
438     // Reload the website.
439     webEngineViewPointer->reload();
440 }
441
442 void BrowserView::toggleJavaScript() const
443 {
444     // Toggle JavaScript.
445     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
446
447     // Update the JavaScript icon.
448     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
449
450     // Reload the website.
451     webEngineViewPointer->reload();
452 }
453
454 void BrowserView::toggleLocalStorage() const
455 {
456     // Toggle local storage.
457     webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
458
459     // Update the local storage icon.
460     emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
461
462     // Reload the website.
463     webEngineViewPointer->reload();
464 }
465
466 void BrowserView::updateUrl(const QUrl &url) const
467 {
468     // Update the URL line edit.
469     emit updateUrlLineEdit(url);
470
471     // Update the status of the forward and back buttons.
472     emit updateBackAction(webEngineHistoryPointer->canGoBack());
473     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
474
475     // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  <https://redmine.stoutner.com/issues/799>
476     webEngineViewPointer->setZoomFactor(currentZoomFactor);
477 }