]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
Create an add or edit domain settings action.
[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 local storage.
166         switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
167         {
168             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
169             {
170                 // Set the default local storage status.
171                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
172
173                 break;
174             }
175
176             case (DomainsDatabaseHelper::DISABLED):
177             {
178                 // Disable local storage.
179                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
180
181                 break;
182             }
183
184             case (DomainsDatabaseHelper::ENABLED):
185             {
186                 // Enable local storage.
187                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
188
189                 break;
190             }
191         }
192
193         // Set the user agent.
194         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
195
196         // Check if a custom zoom factor is set.
197         if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
198         {
199             // Store the current zoom factor.
200             currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
201         }
202         else
203         {
204             // Reset the current zoom factor.
205             currentZoomFactor = Settings::zoomFactor();
206         }
207
208         // Set the zoom factor.    The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
209         webEngineViewPointer->setZoomFactor(currentZoomFactor);
210
211         // Apply the domain settings palette to the URL line edit.
212         emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
213     }
214     else  // The hostname does not have domain settings.
215     {
216         // Set the JavaScript status.
217         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
218
219         // Set local storage.
220         webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
221
222         // Set the user agent.
223         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
224
225         // Store the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
226         currentZoomFactor = Settings::zoomFactor();
227
228         // Set the zoom factor.
229         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
230
231         // Apply the no domain settings palette to the URL line edit.
232         emit updateDomainSettingsIndicator(false, QStringLiteral(""));
233     }
234
235     // Emit the update actions signals.
236     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
237     emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
238     emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
239     emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
240
241     // Reload the website if requested.
242     if (reloadWebsite)
243     {
244         webEngineViewPointer->reload();
245     }
246 }
247
248 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
249 {
250     // Store the search engine name.
251     QString searchEngineName = searchEngineActionPointer->text();
252
253     // Strip out any `&` characters.
254     searchEngineName.remove('&');
255
256     // Store the search engine string.
257     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
258 }
259
260 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
261 {
262     // Get the user agent name.
263     QString userAgentName = userAgentActionPointer->text();
264
265     // Strip out any `&` characters.
266     userAgentName.remove('&');
267
268     // Apply the user agent.
269     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
270
271     // Reload the website.
272     webEngineViewPointer->reload();
273 }
274
275 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
276 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
277 {
278     // Update the current zoom factor.  This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
279     currentZoomFactor = zoomFactor;
280
281     // Set the zoom factor.
282     webEngineViewPointer->setZoomFactor(zoomFactor);
283 }
284
285 void BrowserView::back() const
286 {
287     // Go back.
288     webEngineViewPointer->back();
289 }
290
291 void BrowserView::forward() const
292 {
293     // Go forward.
294     webEngineViewPointer->forward();
295 }
296
297 void BrowserView::home() const
298 {
299     // Load the homepage.
300     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
301 }
302
303 void BrowserView::loadFinished() const
304 {
305     // Hide the progress bar.
306     emit hideProgressBar();
307 }
308
309 void BrowserView::loadInitialWebsite()
310 {
311     // Apply the application settings.
312     applyApplicationSettings();
313
314     // Get the arguments.
315     QStringList argumentsStringList = qApp->arguments();
316
317     // Check to see if the arguments lists contains a URL.
318     if (argumentsStringList.size() > 1)
319     {
320         // Load the URL from the arguments list.
321         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
322     }
323     else
324     {
325         // Load the homepage.
326         home();
327     }
328 }
329
330 void BrowserView::loadProgress(const int &progress) const
331 {
332     // Show the progress bar.
333     emit showProgressBar(progress);
334 }
335
336 void BrowserView::loadStarted() const
337 {
338     // Show the progress bar.
339     emit showProgressBar(0);
340 }
341
342 void BrowserView::loadUrlFromLineEdit(QString url) const
343 {
344     // Decide if the text is more likely to be a URL or a search.
345     if (url.startsWith("file://"))  // The text is likely a file URL.
346     {
347         // Load the URL.
348         webEngineViewPointer->load(QUrl::fromUserInput(url));
349     }
350     else if (url.contains("."))  // The text is likely a URL.
351     {
352         // Check if the URL does not start with a valid protocol.
353         if (!url.startsWith("http"))
354         {
355             // Add `https://` to the beginning of the URL.
356             url = "https://" + url;
357         }
358
359         // Load the URL.
360         webEngineViewPointer->load(QUrl::fromUserInput(url));
361     }
362     else  // The text is likely a search.
363     {
364         // Load the search.
365         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
366     }
367 }
368
369 void BrowserView::pageLinkHovered(const QString &linkUrl) const
370 {
371     // Emit a signal so that the browser window can update the status bar.
372     emit linkHovered(linkUrl);
373 }
374
375 void BrowserView::refresh() const
376 {
377     // Reload the website.
378     webEngineViewPointer->reload();
379 }
380
381 void BrowserView::toggleJavaScript() const
382 {
383     // Toggle JavaScript.
384     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
385
386     // Update the JavaScript icon.
387     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
388
389     // Reload the website.
390     webEngineViewPointer->reload();
391 }
392
393 void BrowserView::toggleLocalStorage() const
394 {
395     // Toggle local storage.
396     webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
397
398     // Update the local storage icon.
399     emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
400
401     // Reload the website.
402     webEngineViewPointer->reload();
403 }
404
405 void BrowserView::updateUrl(const QUrl &url) const
406 {
407     // Update the URL line edit.
408     emit updateUrlLineEdit(url);
409
410     // Update the status of the forward and back buttons.
411     emit updateBackAction(webEngineHistoryPointer->canGoBack());
412     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
413
414     // Reapply the zoom factor.  This is a bug in QWebEngineView that resets the zoom with every load.  <https://redmine.stoutner.com/issues/799>
415     webEngineViewPointer->setZoomFactor(currentZoomFactor);
416 }