]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
Set all the appropriate WebAttributes. https://redmine.stoutner.com/issues/800
[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 "UrlRequestInterceptor.h"
26 #include "helpers/DomainsDatabaseHelper.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.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 void BrowserView::applyDomainSettingsAndReload() const
108 {
109     // Apply the domain settings.  `true` reloads the website.
110     applyDomainSettings(webEngineViewPointer->url().host(), true);
111 }
112
113 // 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.
114 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname) const
115 {
116     // Apply the domain settings  `false` does not reload the website.
117     applyDomainSettings(hostname, false);
118 }
119
120 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) const
121 {
122     // Get the record for the hostname.
123     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
124
125     // Check if the hostname has domain settings.
126     if (domainQuery.isValid())  // The hostname has domain settings.
127     {
128         // Get the domain record.
129         QSqlRecord domainRecord = domainQuery.record();
130
131         // Set the JavaScript status.
132         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
133         {
134             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
135             {
136                 // Set the default JavaScript status.
137                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
138
139                 break;
140             }
141
142             case (DomainsDatabaseHelper::DISABLED):
143             {
144                 // Disable JavaScript.
145                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
146
147                 break;
148             }
149
150             case (DomainsDatabaseHelper::ENABLED):
151             {
152                 // Enable JavaScript.
153                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
154
155                 break;
156             }
157         }
158
159         // Set the user agent.
160         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
161
162         // Set the zoom factor.
163         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
164
165         // Apply the domain settings palette to the URL line edit.
166         emit updateDomainSettingsIndicator(true);
167     }
168     else  // The hostname does not have domain settings.
169     {
170         // Set the JavaScript status.
171         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
172
173         // Set the user agent.
174         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
175
176         // Set the zoom factor.
177         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
178
179         // Apply the no domain settings palette to the URL line edit.
180         emit updateDomainSettingsIndicator(false);
181     }
182
183     // Emit the on-the-fly menu update signals.
184     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
185     emit userAgentUpdated(webEngineProfilePointer->httpUserAgent());
186     emit zoomFactorUpdated(Settings::zoomFactor());
187
188     // Reload the website if requested.
189     if (reloadWebsite)
190     {
191         webEngineViewPointer->reload();
192     }
193 }
194
195 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
196 {
197     // Store the search engine name.
198     QString searchEngineName = searchEngineActionPointer->text();
199
200     // Strip out any `&` characters.
201     searchEngineName.remove('&');
202
203     // Store the search engine string.
204     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
205 }
206
207 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
208 {
209     // Get the user agent name.
210     QString userAgentName = userAgentActionPointer->text();
211
212     // Strip out any `&` characters.
213     userAgentName.remove('&');
214
215     // Apply the user agent.
216     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
217
218     // Reload the website.
219     webEngineViewPointer->reload();
220 }
221
222 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor) const
223 {
224     // Set the zoom factor.
225     webEngineViewPointer->setZoomFactor(zoomFactor);
226 }
227
228 void BrowserView::back() const
229 {
230     // Go back.
231     webEngineViewPointer->back();
232 }
233
234 void BrowserView::forward() const
235 {
236     // Go forward.
237     webEngineViewPointer->forward();
238 }
239
240 void BrowserView::home() const
241 {
242     // Load the homepage.
243     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
244 }
245
246 void BrowserView::loadInitialWebsite()
247 {
248     // Apply the application settings.
249     applyApplicationSettings();
250
251     // Get the arguments.
252     QStringList argumentsStringList = qApp->arguments();
253
254     // Check to see if the arguments lists contains a URL.
255     if (argumentsStringList.size() > 1)
256     {
257         // Load the URL from the arguments list.
258         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
259     }
260     else
261     {
262         // Load the homepage.
263         home();
264     }
265 }
266
267 void BrowserView::loadUrlFromLineEdit(QString url) const
268 {
269     // Decide if the text is more likely to be a URL or a search.
270     if (url.startsWith("file://"))  // The text is likely a file URL.
271     {
272         // Load the URL.
273         webEngineViewPointer->load(QUrl::fromUserInput(url));
274     }
275     else if (url.contains("."))  // The text is likely a URL.
276     {
277         // Check if the URL does not start with a valid protocol.
278         if (!url.startsWith("http"))
279         {
280             // Add `https://` to the beginning of the URL.
281             url = "https://" + url;
282         }
283
284         // Load the URL.
285         webEngineViewPointer->load(QUrl::fromUserInput(url));
286     }
287     else  // The text is likely a search.
288     {
289         // Load the search.
290         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
291     }
292 }
293
294 void BrowserView::pageLinkHovered(const QString &linkUrl) const
295 {
296     // Emit a signal so that the browser window can update the status bar.
297     emit linkHovered(linkUrl);
298 }
299
300 void BrowserView::refresh() const
301 {
302     // Reload the website.
303     webEngineViewPointer->reload();
304 }
305
306 void BrowserView::toggleJavaScript() const
307 {
308     // Toggle JavaScript.
309     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
310
311     // Update the JavaScript icon.
312     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
313
314     // Reload the website.
315     webEngineViewPointer->reload();
316 }
317
318 void BrowserView::updateInterface() const
319 {
320     // Update the URL line edit.
321     emit updateUrlLineEdit(webEngineViewPointer->url().toString());
322
323     // Update the status of the forward and back buttons.
324     emit updateBackAction(webEngineHistoryPointer->canGoBack());
325     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
326
327     // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  Hopefully it will be fixed in Qt6.  <https://bugreports.qt.io/browse/QTBUG-51992>
328     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
329 }