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