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 "helpers/SearchEngineHelper.h"
27 #include "helpers/UserAgentHelper.h"
28 #include "windows/BrowserWindow.h"
29 #include "windows/DomainSettingsWindow.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 backButtonPointer = browserViewUi.backButton;
45 forwardButtonPointer = browserViewUi.forwardButton;
46 QPushButton *refreshButtonPointer = browserViewUi.refreshButton;
47 QPushButton *homeButtonPointer = browserViewUi.homeButton;
48 urlLineEditPointer = browserViewUi.urlLineEdit;
49 javaScriptButtonPointer = browserViewUi.javaScript;
50 QPushButton *domainSettingsButtonPointer = browserViewUi.domainSettingsButton;
51 webEngineViewPointer = browserViewUi.webEngineView;
53 // Get handles for the aspects of the WebEngine.
54 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
55 webEngineHistoryPointer = webEnginePagePointer->history();
56 webEngineProfilePointer = webEnginePagePointer->profile();
57 webEngineSettingsPointer = webEngineViewPointer->settings();
59 // Update the webengine view from the URL line edit.
60 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
62 // Update the URL line edit form the webengine view.
63 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
64 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface()));
65 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface()));
67 // Setup the URL bar buttons.
68 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
69 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
70 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
71 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
72 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
73 connect(domainSettingsButtonPointer, SIGNAL(clicked()), this, SLOT(openDomainSettings()));
75 // Instantiate the mouse event pointer.
76 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
78 // Install the mouse event filter.
79 qApp->installEventFilter(mouseEventFilterPointer);
81 // Listen for hovered link URLs.
82 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
84 // Instantiate the URL request interceptor.
85 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
87 // Set the URL request interceptor.
88 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
90 // Reapply the domain settings when the host changes.
91 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading()));
94 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
96 // Don't allow JavaScript to open windows.
97 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
99 // Set the focus on the WebEngine view.
100 webEngineViewPointer->setFocus();
103 void BrowserView::applyApplicationSettings()
105 // Set the search engine URL.
106 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
108 // Emit the search engine updated signal, which causes the on-the-fly menu to be updated.
109 emit searchEngineUpdated(Settings::searchEngine());
112 // 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.
113 void BrowserView::applyDomainSettingsAndReload() const
115 // Apply the domain settings. `true` reloads the website.
116 applyDomainSettings(true);
119 // 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.
120 void BrowserView::applyDomainSettingsWithoutReloading() const
122 // Apply the domain settings `false` does not reload the website.
123 applyDomainSettings(false);
126 void BrowserView::applyDomainSettings(bool reloadWebsite) const
128 // Set the JavaScript status.
129 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
131 // Update the JavaScript button.
132 if (Settings::javaScript())
134 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
138 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
141 // Apply the user agent.
142 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
144 // Set the zoom factor.
145 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
147 // Emit the on-the-fly menu update signals.
148 emit userAgentUpdated(Settings::userAgent());
149 emit zoomFactorUpdated(Settings::zoomFactor());
151 // Reload the website if requested.
154 webEngineViewPointer->reload();
158 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
160 // Store the search engine name.
161 QString searchEngineName = searchEngineActionPointer->text();
163 // Strip out any `&` characters.
164 searchEngineName.remove('&');
166 // Store the search engine string.
167 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
170 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
172 // Get the user agent name.
173 QString userAgentName = userAgentActionPointer->text();
175 // Strip out any `&` characters.
176 userAgentName.remove('&');
178 // Apply the user agent.
179 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(userAgentName));
181 // Reload the website.
182 webEngineViewPointer->reload();
185 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor) const
187 // Set the zoom factor.
188 webEngineViewPointer->setZoomFactor(zoomFactor);
191 void BrowserView::goHome() const
193 // Load the homepage.
194 webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage()));
197 void BrowserView::loadInitialWebsite()
199 // Apply the application settings.
200 applyApplicationSettings();
202 // Get the arguments.
203 QStringList argumentsStringList = qApp->arguments();
205 // Check to see if the arguments lists contains a URL.
206 if (argumentsStringList.size() > 1)
208 // Load the URL from the arguments list.
209 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
213 // Load the homepage.
218 void BrowserView::loadUrlFromTextBox(QString urlFromUser) const
220 // Remove the focus from the URL line edit.
221 urlLineEditPointer->clearFocus();
223 // Decide if the text is more likely to be a URL or a search.
224 if (urlFromUser.contains(".")) // The text is likely a URL.
226 // Check if the URL does not start with a valid protocol.
227 if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
229 // Add `https://` to the beginning of the URL.
230 urlFromUser = "https://" + urlFromUser;
234 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
236 else // The text is likely a search.
239 webEngineViewPointer->setUrl(QUrl::fromUserInput(searchEngineUrl + urlFromUser));
243 void BrowserView::openDomainSettings() const
245 // Get a list of the top level widgets.
246 const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
248 // Initialize a domain settings window exists boolean.
249 bool domainSettingsWindowExists = false;
251 // Iterate through the top level widgets.
252 for (QWidget *widgetPointer : topLevelWidgets)
254 // Check for an existing domain settings window.
255 if (widgetPointer->objectName() == QStringLiteral("domain_settings"))
257 // Show the existing domain settings window if it is hidden.
258 widgetPointer->show();
260 // Raise the existing domain settings window if it is below other windows.
261 widgetPointer->raise();
263 // Restore the existing domain settings window if it has been minimized.
264 if (widgetPointer->isMinimized()) {
265 widgetPointer->showNormal();
268 // Activate the existing domain settings window, which brings its virtual desktop into focus.
269 widgetPointer->activateWindow();
271 // Update the domain settings window exists boolean.
272 domainSettingsWindowExists = true;
276 if (!domainSettingsWindowExists)
278 // Instantiate the domain settings window.
279 DomainSettingsWindow *domainSettingsWindowPointer = new DomainSettingsWindow();
282 domainSettingsWindowPointer->show();
286 void BrowserView::pageLinkHovered(const QString &linkUrl) const
288 // Emit a signal so that the browser window can update the status bar.
289 emit linkHovered(linkUrl);
292 void BrowserView::toggleJavaScript() const
294 // Toggle JavaScript.
295 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
297 // Update the JavaScript button.
298 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
300 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
304 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
307 // Reload the website.
308 webEngineViewPointer->reload();
311 void BrowserView::updateInterface() const
313 // Update the URL line edit if it does not have focus.
314 if (!urlLineEditPointer->hasFocus())
316 // Update the URL line edit.
317 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
320 // Update the status of the forward and back buttons.
321 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
322 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
324 // 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>
325 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());