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 "UrlRequestInterceptor.h"
26 #include "dialogs/DomainSettingsDialog.h"
27 #include "helpers/DomainsDatabaseHelper.h"
28 #include "helpers/SearchEngineHelper.h"
29 #include "helpers/UserAgentHelper.h"
30 #include "windows/BrowserWindow.h"
32 // Qt framework headers.
34 #include <QWebEngineProfile>
36 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
38 // Instantiate the browser view UI.
39 Ui::BrowserView browserViewUi;
42 browserViewUi.setupUi(this);
44 // Get handles for the views.
45 backButtonPointer = browserViewUi.backButton;
46 forwardButtonPointer = browserViewUi.forwardButton;
47 QPushButton *refreshButtonPointer = browserViewUi.refreshButton;
48 QPushButton *homeButtonPointer = browserViewUi.homeButton;
49 urlLineEditPointer = browserViewUi.urlLineEdit;
50 javaScriptButtonPointer = browserViewUi.javaScript;
51 QPushButton *domainSettingsButtonPointer = browserViewUi.domainSettingsButton;
52 webEngineViewPointer = browserViewUi.webEngineView;
54 // Get handles for the aspects of the WebEngine.
55 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
56 webEngineHistoryPointer = webEnginePagePointer->history();
57 webEngineProfilePointer = webEnginePagePointer->profile();
58 webEngineSettingsPointer = webEngineViewPointer->settings();
60 // Update the webengine view from the URL line edit.
61 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
63 // Update the URL line edit from the webengine view.
64 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
65 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface()));
66 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface()));
68 // Setup the URL bar buttons.
69 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
70 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
71 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
72 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
73 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
74 connect(domainSettingsButtonPointer, SIGNAL(clicked()), this, SLOT(openDomainSettings()));
76 // Get the URL line edit palettes.
77 noDomainSettingsPalette = urlLineEditPointer->palette();
78 domainSettingsPalette = urlLineEditPointer->palette();
80 // Modify the domain settings palette.
81 domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
83 // Instantiate the mouse event pointer.
84 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
86 // Install the mouse event filter.
87 qApp->installEventFilter(mouseEventFilterPointer);
89 // Listen for hovered link URLs.
90 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
92 // Instantiate the URL request interceptor.
93 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
95 // Set the URL request interceptor.
96 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
98 // Reapply the domain settings when the host changes.
99 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
101 // Disable the cache.
102 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
104 // Don't allow JavaScript to open windows.
105 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
107 // Set the focus on the WebEngine view.
108 webEngineViewPointer->setFocus();
111 void BrowserView::applyApplicationSettings()
113 // Set the search engine URL.
114 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
116 // Emit the search engine updated signal, which causes the on-the-fly menu to be updated.
117 emit searchEngineUpdated(Settings::searchEngine());
120 // 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.
121 void BrowserView::applyDomainSettingsAndReload() const
123 // Apply the domain settings. `true` reloads the website.
124 applyDomainSettings(webEngineViewPointer->url().host(), true);
127 // 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.
128 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname) const
130 // Apply the domain settings `false` does not reload the website.
131 applyDomainSettings(hostname, false);
134 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) const
136 // Get the record for the hostname.
137 QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
139 // Check if the hostname has domain settings.
140 if (domainQuery.isValid()) // The hostname has domain settings.
142 // Get the domain record.
143 QSqlRecord domainRecord = domainQuery.record();
145 // Set the JavaScript status.
146 switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
148 case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
150 // Set the default JavaScript status.
151 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
156 case (DomainsDatabaseHelper::DISABLED):
158 // Disable JavaScript.
159 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
164 case (DomainsDatabaseHelper::ENABLED):
166 // Enable JavaScript.
167 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
173 // Set the user agent.
174 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
176 // Set the zoom factor.
177 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
179 // Apply the domain settings palette to the URL line edit.
180 urlLineEditPointer->setPalette(domainSettingsPalette);
182 else // The hostname does not have domain settings.
184 // Set the JavaScript status.
185 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
187 // Set the user agent.
188 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
190 // Set the zoom factor.
191 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
193 // Apply the no domain settings palette to the URL line edit.
194 urlLineEditPointer->setPalette(noDomainSettingsPalette);
197 // Update the JavaScript button.
198 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
200 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
204 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
207 // Emit the on-the-fly menu update signals.
208 emit userAgentUpdated(webEngineProfilePointer->httpUserAgent());
209 emit zoomFactorUpdated(Settings::zoomFactor());
211 // Reload the website if requested.
214 webEngineViewPointer->reload();
218 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
220 // Store the search engine name.
221 QString searchEngineName = searchEngineActionPointer->text();
223 // Strip out any `&` characters.
224 searchEngineName.remove('&');
226 // Store the search engine string.
227 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
230 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
232 // Get the user agent name.
233 QString userAgentName = userAgentActionPointer->text();
235 // Strip out any `&` characters.
236 userAgentName.remove('&');
238 // Apply the user agent.
239 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
241 // Reload the website.
242 webEngineViewPointer->reload();
245 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor) const
247 // Set the zoom factor.
248 webEngineViewPointer->setZoomFactor(zoomFactor);
251 void BrowserView::goHome() const
253 // Load the homepage.
254 webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage()));
257 void BrowserView::loadInitialWebsite()
259 // Apply the application settings.
260 applyApplicationSettings();
262 // Get the arguments.
263 QStringList argumentsStringList = qApp->arguments();
265 // Check to see if the arguments lists contains a URL.
266 if (argumentsStringList.size() > 1)
268 // Load the URL from the arguments list.
269 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
273 // Load the homepage.
278 void BrowserView::loadUrlFromTextBox(QString urlFromUser) const
280 // Remove the focus from the URL line edit.
281 urlLineEditPointer->clearFocus();
283 // Decide if the text is more likely to be a URL or a search.
284 if (urlFromUser.contains(".")) // The text is likely a URL.
286 // Check if the URL does not start with a valid protocol.
287 if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
289 // Add `https://` to the beginning of the URL.
290 urlFromUser = "https://" + urlFromUser;
294 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
296 else // The text is likely a search.
299 webEngineViewPointer->setUrl(QUrl::fromUserInput(searchEngineUrl + urlFromUser));
303 void BrowserView::openDomainSettings() const
305 // Instantiate the domain settings window.
306 DomainSettingsDialog *domainSettingsDialogPointer = new DomainSettingsDialog();
308 // Set the dialog window title.
309 domainSettingsDialogPointer->setWindowTitle(i18nc("The domain settings dialog title", "Domain Settings"));
312 domainSettingsDialogPointer->setWindowModality(Qt::WindowModality::WindowModal);;
315 domainSettingsDialogPointer->show();
317 // Reload the tabs when domain settings are updated.
318 connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), this, SLOT(applyDomainSettingsAndReload()));
321 void BrowserView::pageLinkHovered(const QString &linkUrl) const
323 // Emit a signal so that the browser window can update the status bar.
324 emit linkHovered(linkUrl);
327 void BrowserView::toggleJavaScript() const
329 // Toggle JavaScript.
330 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
332 // Update the JavaScript button.
333 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
335 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
339 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
342 // Reload the website.
343 webEngineViewPointer->reload();
346 void BrowserView::updateInterface() const
348 // Update the URL line edit if it does not have focus.
349 if (!urlLineEditPointer->hasFocus())
351 // Update the URL line edit.
352 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
355 // Update the status of the forward and back buttons.
356 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
357 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
359 // 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>
360 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());