2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
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.
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.
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/>.
20 // Application headers.
21 #include "BrowserView.h"
22 #include "MouseEventFilter.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"
31 // Qt framework headers.
33 #include <QWebEngineProfile>
35 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
37 // Instantiate the browser view UI.
38 Ui::BrowserView browserViewUi;
41 browserViewUi.setupUi(this);
43 // Get handles for the views.
44 webEngineViewPointer = browserViewUi.webEngineView;
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();
52 // Update the URL line edit when the URL changes.
53 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
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()));
60 // Instantiate the mouse event filter pointer.
61 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
63 // Install the mouse event filter.
64 qApp->installEventFilter(mouseEventFilterPointer);
66 // Listen for hovered link URLs.
67 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
69 // Instantiate the URL request interceptor.
70 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
72 // Set the URL request interceptor.
73 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
75 // Reapply the domain settings when the host changes.
76 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
79 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
81 // Don't allow JavaScript to open windows.
82 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
84 // Allow keyboard navigation.
85 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
87 // Enable full screen support.
88 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
90 // Require user interaction to play media.
91 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
93 // Limit WebRTC to public IP addresses.
94 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
96 // Set the focus on the WebEngine view.
97 webEngineViewPointer->setFocus();
100 void BrowserView::applyApplicationSettings()
102 // Set the search engine URL.
103 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
105 // Emit the update search engine actions signal.
106 emit updateSearchEngineActions(Settings::searchEngine());
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()
113 // Apply the domain settings. `true` reloads the website.
114 applyDomainSettings(webEngineViewPointer->url().host(), true);
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)
121 // Apply the domain settings `false` does not reload the website.
122 applyDomainSettings(hostname, false);
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)
128 // Get the record for the hostname.
129 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
131 // Check if the hostname has domain settings.
132 if (domainQuery.isValid()) // The hostname has domain settings.
134 // Get the domain record.
135 QSqlRecord domainRecord = domainQuery.record();
137 // Set the JavaScript status.
138 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
140 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
142 // Set the default JavaScript status.
143 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
148 case (DomainsDatabaseHelper::DISABLED):
150 // Disable JavaScript.
151 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
156 case (DomainsDatabaseHelper::ENABLED):
158 // Enable JavaScript.
159 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
165 // Set local storage.
166 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
168 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
170 // Set the default local storage status.
171 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
176 case (DomainsDatabaseHelper::DISABLED):
178 // Disable local storage.
179 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
184 case (DomainsDatabaseHelper::ENABLED):
186 // Enable local storage.
187 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
193 // Set the user agent.
194 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
196 // Check if a custom zoom factor is set.
197 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
199 // Store the current zoom factor.
200 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
204 // Reset the current zoom factor.
205 currentZoomFactor = Settings::zoomFactor();
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);
211 // Apply the domain settings palette to the URL line edit.
212 emit updateDomainSettingsIndicator(true);
214 else // The hostname does not have domain settings.
216 // Set the JavaScript status.
217 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
219 // Set local storage.
220 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
222 // Set the user agent.
223 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
225 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
226 currentZoomFactor = Settings::zoomFactor();
228 // Set the zoom factor.
229 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
231 // Apply the no domain settings palette to the URL line edit.
232 emit updateDomainSettingsIndicator(false);
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());
241 // Reload the website if requested.
244 webEngineViewPointer->reload();
248 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
250 // Store the search engine name.
251 QString searchEngineName = searchEngineActionPointer->text();
253 // Strip out any `&` characters.
254 searchEngineName.remove('&');
256 // Store the search engine string.
257 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
260 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
262 // Get the user agent name.
263 QString userAgentName = userAgentActionPointer->text();
265 // Strip out any `&` characters.
266 userAgentName.remove('&');
268 // Apply the user agent.
269 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
271 // Reload the website.
272 webEngineViewPointer->reload();
275 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
276 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
278 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
279 currentZoomFactor = zoomFactor;
281 // Set the zoom factor.
282 webEngineViewPointer->setZoomFactor(zoomFactor);
285 void BrowserView::back() const
288 webEngineViewPointer->back();
291 void BrowserView::forward() const
294 webEngineViewPointer->forward();
297 void BrowserView::home() const
299 // Load the homepage.
300 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
303 void BrowserView::loadFinished() const
305 // Hide the progress bar.
306 emit hideProgressBar();
309 void BrowserView::loadInitialWebsite()
311 // Apply the application settings.
312 applyApplicationSettings();
314 // Get the arguments.
315 QStringList argumentsStringList = qApp->arguments();
317 // Check to see if the arguments lists contains a URL.
318 if (argumentsStringList.size() > 1)
320 // Load the URL from the arguments list.
321 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
325 // Load the homepage.
330 void BrowserView::loadProgress(const int &progress) const
332 // Show the progress bar.
333 emit showProgressBar(progress);
336 void BrowserView::loadStarted() const
338 // Show the progress bar.
339 emit showProgressBar(0);
342 void BrowserView::loadUrlFromLineEdit(QString url) const
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.
348 webEngineViewPointer->load(QUrl::fromUserInput(url));
350 else if (url.contains(".")) // The text is likely a URL.
352 // Check if the URL does not start with a valid protocol.
353 if (!url.startsWith("http"))
355 // Add `https://` to the beginning of the URL.
356 url = "https://" + url;
360 webEngineViewPointer->load(QUrl::fromUserInput(url));
362 else // The text is likely a search.
365 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
369 void BrowserView::pageLinkHovered(const QString &linkUrl) const
371 // Emit a signal so that the browser window can update the status bar.
372 emit linkHovered(linkUrl);
375 void BrowserView::refresh() const
377 // Reload the website.
378 webEngineViewPointer->reload();
381 void BrowserView::toggleJavaScript() const
383 // Toggle JavaScript.
384 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
386 // Update the JavaScript icon.
387 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
389 // Reload the website.
390 webEngineViewPointer->reload();
393 void BrowserView::toggleLocalStorage() const
395 // Toggle local storage.
396 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
398 // Update the local storage icon.
399 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
401 // Reload the website.
402 webEngineViewPointer->reload();
405 void BrowserView::updateUrl(const QUrl &url) const
407 // Update the URL line edit.
408 emit updateUrlLineEdit(url.toString());
410 // Update the status of the forward and back buttons.
411 emit updateBackAction(webEngineHistoryPointer->canGoBack());
412 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
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);