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 the user agent.
166 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
168 // Check if a custom zoom factor is set.
169 if (domainRecord.field(DomainsDatabaseHelper::ZOOM_FACTOR).value().toInt())
171 // Store the current zoom factor.
172 currentZoomFactor = domainRecord.field(DomainsDatabaseHelper::CUSTOM_ZOOM_FACTOR).value().toDouble();
176 // Reset the current zoom factor.
177 currentZoomFactor = Settings::zoomFactor();
180 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
181 webEngineViewPointer->setZoomFactor(currentZoomFactor);
183 // Apply the domain settings palette to the URL line edit.
184 emit updateDomainSettingsIndicator(true);
186 else // The hostname does not have domain settings.
188 // Set the JavaScript status.
189 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
191 // Set the user agent.
192 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
194 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
195 currentZoomFactor = Settings::zoomFactor();
197 // Set the zoom factor.
198 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
200 // Apply the no domain settings palette to the URL line edit.
201 emit updateDomainSettingsIndicator(false);
204 // Emit the update actions signals.
205 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
206 emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
207 emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
209 // Reload the website if requested.
212 webEngineViewPointer->reload();
216 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
218 // Store the search engine name.
219 QString searchEngineName = searchEngineActionPointer->text();
221 // Strip out any `&` characters.
222 searchEngineName.remove('&');
224 // Store the search engine string.
225 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
228 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
230 // Get the user agent name.
231 QString userAgentName = userAgentActionPointer->text();
233 // Strip out any `&` characters.
234 userAgentName.remove('&');
236 // Apply the user agent.
237 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
239 // Reload the website.
240 webEngineViewPointer->reload();
243 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
244 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor)
246 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
247 currentZoomFactor = zoomFactor;
249 // Set the zoom factor.
250 webEngineViewPointer->setZoomFactor(zoomFactor);
253 void BrowserView::back() const
256 webEngineViewPointer->back();
259 void BrowserView::forward() const
262 webEngineViewPointer->forward();
265 void BrowserView::home() const
267 // Load the homepage.
268 webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
271 void BrowserView::loadFinished() const
273 // Hide the progress bar.
274 emit hideProgressBar();
277 void BrowserView::loadInitialWebsite()
279 // Apply the application settings.
280 applyApplicationSettings();
282 // Get the arguments.
283 QStringList argumentsStringList = qApp->arguments();
285 // Check to see if the arguments lists contains a URL.
286 if (argumentsStringList.size() > 1)
288 // Load the URL from the arguments list.
289 webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
293 // Load the homepage.
298 void BrowserView::loadProgress(const int &progress) const
300 // Show the progress bar.
301 emit showProgressBar(progress);
304 void BrowserView::loadStarted() const
306 // Show the progress bar.
307 emit showProgressBar(0);
310 void BrowserView::loadUrlFromLineEdit(QString url) const
312 // Decide if the text is more likely to be a URL or a search.
313 if (url.startsWith("file://")) // The text is likely a file URL.
316 webEngineViewPointer->load(QUrl::fromUserInput(url));
318 else if (url.contains(".")) // The text is likely a URL.
320 // Check if the URL does not start with a valid protocol.
321 if (!url.startsWith("http"))
323 // Add `https://` to the beginning of the URL.
324 url = "https://" + url;
328 webEngineViewPointer->load(QUrl::fromUserInput(url));
330 else // The text is likely a search.
333 webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
337 void BrowserView::pageLinkHovered(const QString &linkUrl) const
339 // Emit a signal so that the browser window can update the status bar.
340 emit linkHovered(linkUrl);
343 void BrowserView::refresh() const
345 // Reload the website.
346 webEngineViewPointer->reload();
349 void BrowserView::toggleJavaScript() const
351 // Toggle JavaScript.
352 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
354 // Update the JavaScript icon.
355 emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
357 // Reload the website.
358 webEngineViewPointer->reload();
361 void BrowserView::updateUrl(const QUrl &url) const
363 // Update the URL line edit.
364 emit updateUrlLineEdit(url.toString());
366 // Update the status of the forward and back buttons.
367 emit updateBackAction(webEngineHistoryPointer->canGoBack());
368 emit updateForwardAction(webEngineHistoryPointer->canGoForward());
370 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. <https://redmine.stoutner.com/issues/799>
371 webEngineViewPointer->setZoomFactor(currentZoomFactor);