]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
21e1493aaa63c7482314936ac664e85f889d8825
[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 "MouseEventFilter.h"
23 #include "Settings.h"
24 #include "ui_BrowserView.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 #include <QWebEngineProfile>
34
35 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
36 {
37     // Instantiate the browser view UI.
38     Ui::BrowserView browserViewUi;
39
40     // Setup the UI.
41     browserViewUi.setupUi(this);
42
43     // Get handles for the views.
44     webEngineViewPointer = browserViewUi.webEngineView;
45
46     // Get handles for the aspects of the WebEngine.
47     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
48     webEngineHistoryPointer = webEnginePagePointer->history();
49     webEngineProfilePointer = webEnginePagePointer->profile();
50     webEngineSettingsPointer = webEngineViewPointer->settings();
51
52     // Update the URL line edit when the URL changes.
53     connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
54
55     // Update the progress bar.
56     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
57     connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
58     connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
59
60     // Instantiate the mouse event filter pointer.
61     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
62
63     // Install the mouse event filter.
64     qApp->installEventFilter(mouseEventFilterPointer);
65
66     // Listen for hovered link URLs.
67     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
68
69     // Instantiate the URL request interceptor.
70     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
71
72     // Set the URL request interceptor.
73     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
74
75     // Reapply the domain settings when the host changes.
76     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
77
78     // Disable the cache.
79     webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
80
81     // Don't allow JavaScript to open windows.
82     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
83
84     // Allow keyboard navigation.
85     webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
86
87     // Enable full screen support.
88     webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
89
90     // Require user interaction to play media.
91     webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
92
93     // Limit WebRTC to public IP addresses.
94     webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
95
96     // Set the focus on the WebEngine view.
97     webEngineViewPointer->setFocus();
98 }
99
100 void BrowserView::applyApplicationSettings()
101 {
102     // Set the search engine URL.
103     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
104
105     // Emit the update search engine actions signal.
106     emit updateSearchEngineActions(Settings::searchEngine());
107 }
108
109 // 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.
110 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
111 void BrowserView::applyDomainSettingsAndReload()
112 {
113     // Apply the domain settings.  `true` reloads the website.
114     applyDomainSettings(webEngineViewPointer->url().host(), true);
115 }
116
117 // 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.
118 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
119 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
120 {
121     // Apply the domain settings  `false` does not reload the website.
122     applyDomainSettings(hostname, false);
123 }
124
125 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
126 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
127 {
128     // Get the record for the hostname.
129     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
130
131     // Check if the hostname has domain settings.
132     if (domainQuery.isValid())  // The hostname has domain settings.
133     {
134         // Get the domain record.
135         QSqlRecord domainRecord = domainQuery.record();
136
137         // Set the JavaScript status.
138         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
139         {
140             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
141             {
142                 // Set the default JavaScript status.
143                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
144
145                 break;
146             }
147
148             case (DomainsDatabaseHelper::DISABLED):
149             {
150                 // Disable JavaScript.
151                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
152
153                 break;
154             }
155
156             case (DomainsDatabaseHelper::ENABLED):
157             {
158                 // Enable JavaScript.
159                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
160
161                 break;
162             }
163         }
164
165         // Set the user agent.
166         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
167
168         // Check if a custom zoom factor is set.
169         if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
170         {
171             // Store the current zoom factor.
172             currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
173         }
174         else
175         {
176             // Reset the current zoom factor.
177             currentZoomFactor = Settings::zoomFactor();
178         }
179
180         // Set the zoom factor.    The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
181         webEngineViewPointer->setZoomFactor(currentZoomFactor);
182
183         // Apply the domain settings palette to the URL line edit.
184         emit updateDomainSettingsIndicator(true);
185     }
186     else  // The hostname does not have domain settings.
187     {
188         // Set the JavaScript status.
189         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
190
191         // Set the user agent.
192         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
193
194         // Store the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
195         currentZoomFactor = Settings::zoomFactor();
196
197         // Set the zoom factor.
198         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
199
200         // Apply the no domain settings palette to the URL line edit.
201         emit updateDomainSettingsIndicator(false);
202     }
203
204     // Emit the update actions signals.
205     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
206     emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
207     emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
208
209     // Reload the website if requested.
210     if (reloadWebsite)
211     {
212         webEngineViewPointer->reload();
213     }
214 }
215
216 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
217 {
218     // Store the search engine name.
219     QString searchEngineName = searchEngineActionPointer->text();
220
221     // Strip out any `&` characters.
222     searchEngineName.remove('&');
223
224     // Store the search engine string.
225     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
226 }
227
228 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
229 {
230     // Get the user agent name.
231     QString userAgentName = userAgentActionPointer->text();
232
233     // Strip out any `&` characters.
234     userAgentName.remove('&');
235
236     // Apply the user agent.
237     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
238
239     // Reload the website.
240     webEngineViewPointer->reload();
241 }
242
243 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
244 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
245 {
246     // Update the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
247     currentZoomFactor = zoomFactor;
248
249     // Set the zoom factor.
250     webEngineViewPointer->setZoomFactor(zoomFactor);
251 }
252
253 void BrowserView::back() const
254 {
255     // Go back.
256     webEngineViewPointer->back();
257 }
258
259 void BrowserView::forward() const
260 {
261     // Go forward.
262     webEngineViewPointer->forward();
263 }
264
265 void BrowserView::home() const
266 {
267     // Load the homepage.
268     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
269 }
270
271 void BrowserView::loadFinished() const
272 {
273     // Hide the progress bar.
274     emit hideProgressBar();
275 }
276
277 void BrowserView::loadInitialWebsite()
278 {
279     // Apply the application settings.
280     applyApplicationSettings();
281
282     // Get the arguments.
283     QStringList argumentsStringList = qApp->arguments();
284
285     // Check to see if the arguments lists contains a URL.
286     if (argumentsStringList.size() > 1)
287     {
288         // Load the URL from the arguments list.
289         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
290     }
291     else
292     {
293         // Load the homepage.
294         home();
295     }
296 }
297
298 void BrowserView::loadProgress(const int &progress) const
299 {
300     // Show the progress bar.
301     emit showProgressBar(progress);
302 }
303
304 void BrowserView::loadStarted() const
305 {
306     // Show the progress bar.
307     emit showProgressBar(0);
308 }
309
310 void BrowserView::loadUrlFromLineEdit(QString url) const
311 {
312     // Decide if the text is more likely to be a URL or a search.
313     if (url.startsWith("file://"))  // The text is likely a file URL.
314     {
315         // Load the URL.
316         webEngineViewPointer->load(QUrl::fromUserInput(url));
317     }
318     else if (url.contains("."))  // The text is likely a URL.
319     {
320         // Check if the URL does not start with a valid protocol.
321         if (!url.startsWith("http"))
322         {
323             // Add `https://` to the beginning of the URL.
324             url = "https://" + url;
325         }
326
327         // Load the URL.
328         webEngineViewPointer->load(QUrl::fromUserInput(url));
329     }
330     else  // The text is likely a search.
331     {
332         // Load the search.
333         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
334     }
335 }
336
337 void BrowserView::pageLinkHovered(const QString &linkUrl) const
338 {
339     // Emit a signal so that the browser window can update the status bar.
340     emit linkHovered(linkUrl);
341 }
342
343 void BrowserView::refresh() const
344 {
345     // Reload the website.
346     webEngineViewPointer->reload();
347 }
348
349 void BrowserView::toggleJavaScript() const
350 {
351     // Toggle JavaScript.
352     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
353
354     // Update the JavaScript icon.
355     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
356
357     // Reload the website.
358     webEngineViewPointer->reload();
359 }
360
361 void BrowserView::updateUrl(const QUrl &url) const
362 {
363     // Update the URL line edit.
364     emit updateUrlLineEdit(url.toString());
365
366     // Update the status of the forward and back buttons.
367     emit updateBackAction(webEngineHistoryPointer->canGoBack());
368     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
369
370     // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  <https://redmine.stoutner.com/issues/799>
371     webEngineViewPointer->setZoomFactor(currentZoomFactor);
372 }