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 // Create an off-the-record profile (the default when no profile name is specified).
47 webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
49 // Create a WebEngine page.
50 QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
52 // Set the WebEngine page.
53 webEngineViewPointer->setPage(webEnginePagePointer);
55 // Get handles for the aspects of the WebEngine.
56 webEngineHistoryPointer = webEnginePagePointer->history();
57 webEngineSettingsPointer = webEngineViewPointer->settings();
59 // Update the URL line edit when the URL changes.
60 connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
62 // Update the progress bar.
63 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
64 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(loadProgress(const int)));
65 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
67 // Instantiate the mouse event filter pointer.
68 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
70 // Install the mouse event filter.
71 qApp->installEventFilter(mouseEventFilterPointer);
73 // Listen for hovered link URLs.
74 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
76 // Instantiate the URL request interceptor.
77 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
79 // Set the URL request interceptor.
80 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
82 // Reapply the domain settings when the host changes.
83 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
85 // Don't allow JavaScript to open windows.
86 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
88 // Allow keyboard navigation.
89 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
91 // Enable full screen support.
92 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
94 // Require user interaction to play media.
95 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
97 // Limit WebRTC to public IP addresses.
98 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
100 // Set the focus on the WebEngine view.
101 webEngineViewPointer->setFocus();
104 void BrowserView::applyApplicationSettings()
106 // Set the search engine URL.
107 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
109 // Emit the update search engine actions signal.
110 emit updateSearchEngineActions(Settings::searchEngine());
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 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
115 void BrowserView::applyDomainSettingsAndReload()
117 // Apply the domain settings. `true` reloads the website.
118 applyDomainSettings(webEngineViewPointer->url().host(), true);
121 // 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.
122 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
123 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname)
125 // Apply the domain settings `false` does not reload the website.
126 applyDomainSettings(hostname, false);
129 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
130 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
132 // Get the record for the hostname.
133 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
135 // Check if the hostname has domain settings.
136 if (domainQuery.isValid()) // The hostname has domain settings.
138 // Get the domain record.
139 QSqlRecord domainRecord = domainQuery.record();
141 // Set the JavaScript status.
142 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
144 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
146 // Set the default JavaScript status.
147 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
152 case (DomainsDatabaseHelper::DISABLED):
154 // Disable JavaScript.
155 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
160 case (DomainsDatabaseHelper::ENABLED):
162 // Enable JavaScript.
163 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
169 // Set local storage.
170 switch (domainRecord.field(DomainsDatabaseHelper::LOCAL_STORAGE).value().toInt())
172 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
174 // Set the default local storage status.
175 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
180 case (DomainsDatabaseHelper::DISABLED):
182 // Disable local storage.
183 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
188 case (DomainsDatabaseHelper::ENABLED):
190 // Enable local storage.
191 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
197 // Set the user agent.
198 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
200 // Check if a custom zoom factor is set.
201 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
203 // Store the current zoom factor.
204 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
208 // Reset the current zoom factor.
209 currentZoomFactor = Settings::zoomFactor();
212 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
213 webEngineViewPointer->setZoomFactor(currentZoomFactor);
215 // Apply the domain settings palette to the URL line edit.
216 emit updateDomainSettingsIndicator(true, domainRecord.field(DomainsDatabaseHelper::DOMAIN_NAME).value().toString());
218 else // The hostname does not have domain settings.
220 // Set the JavaScript status.
221 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
223 // Set local storage.
224 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::localStorage());
226 // Set the user agent.
227 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
229 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
230 currentZoomFactor = Settings::zoomFactor();
232 // Set the zoom factor.
233 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
235 // Apply the no domain settings palette to the URL line edit.
236 emit updateDomainSettingsIndicator(false, QStringLiteral(""));
239 // Emit the update actions signals.
240 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
241 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
242 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
243 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
245 // Reload the website if requested.
248 webEngineViewPointer->reload();
252 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
254 // Store the search engine name.
255 QString searchEngineName = searchEngineActionPointer->text();
257 // Strip out any `&` characters.
258 searchEngineName.remove('&');
260 // Store the search engine string.
261 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
264 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
266 // Get the user agent name.
267 QString userAgentName = userAgentActionPointer->text();
269 // Strip out any `&` characters.
270 userAgentName.remove('&');
272 // Apply the user agent.
273 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
275 // Reload the website.
276 webEngineViewPointer->reload();
279 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
280 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
282 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
283 currentZoomFactor = zoomFactor;
285 // Set the zoom factor.
286 webEngineViewPointer->setZoomFactor(zoomFactor);
289 void BrowserView::back() const
292 webEngineViewPointer->back();
295 void BrowserView::forward() const
298 webEngineViewPointer->forward();
301 void BrowserView::home() const
303 // Load the homepage.
304 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
307 void BrowserView::loadFinished() const
309 // Hide the progress bar.
310 emit hideProgressBar();
313 void BrowserView::loadInitialWebsite()
315 // Apply the application settings.
316 applyApplicationSettings();
318 // Get the arguments.
319 QStringList argumentsStringList = qApp->arguments();
321 // Check to see if the arguments lists contains a URL.
322 if (argumentsStringList.size() > 1)
324 // Load the URL from the arguments list.
325 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
329 // Load the homepage.
334 void BrowserView::loadProgress(const int &progress) const
336 // Show the progress bar.
337 emit showProgressBar(progress);
340 void BrowserView::loadStarted() const
342 // Show the progress bar.
343 emit showProgressBar(0);
346 void BrowserView::loadUrlFromLineEdit(QString url) const
348 // Decide if the text is more likely to be a URL or a search.
349 if (url.startsWith("file://")) // The text is likely a file URL.
352 webEngineViewPointer->load(QUrl::fromUserInput(url));
354 else if (url.contains(".")) // The text is likely a URL.
356 // Check if the URL does not start with a valid protocol.
357 if (!url.startsWith("http"))
359 // Add `https://` to the beginning of the URL.
360 url = "https://" + url;
364 webEngineViewPointer->load(QUrl::fromUserInput(url));
366 else // The text is likely a search.
369 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
373 void BrowserView::pageLinkHovered(const QString &linkUrl) const
375 // Emit a signal so that the browser window can update the status bar.
376 emit linkHovered(linkUrl);
379 void BrowserView::refresh() const
381 // Reload the website.
382 webEngineViewPointer->reload();
385 void BrowserView::toggleJavaScript() const
387 // Toggle JavaScript.
388 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
390 // Update the JavaScript icon.
391 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
393 // Reload the website.
394 webEngineViewPointer->reload();
397 void BrowserView::toggleLocalStorage() const
399 // Toggle local storage.
400 webEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
402 // Update the local storage icon.
403 emit updateLocalStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
405 // Reload the website.
406 webEngineViewPointer->reload();
409 void BrowserView::updateUrl(const QUrl &url) const
411 // Update the URL line edit.
412 emit updateUrlLineEdit(url);
414 // Update the status of the forward and back buttons.
415 emit updateBackAction(webEngineHistoryPointer->canGoBack());
416 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
418 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
419 webEngineViewPointer->setZoomFactor(currentZoomFactor);