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 "BrowserWindow.h"
23 #include "MouseEventFilter.h"
25 #include "ui_MainView.h"
26 #include "UrlRequestInterceptor.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
30 // Qt framework headers.
32 #include <QWebEngineProfile>
34 MainView::MainView(QWidget *parent) : QWidget(parent)
36 // Instantiate the mainview UI.
37 Ui::MainView mainViewUi;
40 mainViewUi.setupUi(this);
42 // Get handles for the views.
43 backButtonPointer = mainViewUi.backButton;
44 forwardButtonPointer = mainViewUi.forwardButton;
45 QPushButton *refreshButtonPointer = mainViewUi.refreshButton;
46 QPushButton *homeButtonPointer = mainViewUi.homeButton;
47 urlLineEditPointer = mainViewUi.urlLineEdit;
48 javaScriptButtonPointer = mainViewUi.javaScript;
49 webEngineViewPointer = mainViewUi.webEngineView;
51 // Get handles for the aspects of the WebEngine.
52 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
53 webEngineHistoryPointer = webEnginePagePointer->history();
54 webEngineProfilePointer = webEnginePagePointer->profile();
55 webEngineSettingsPointer = webEngineViewPointer->settings();
57 // Update the webengine view from the URL line edit.
58 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
60 // Update the URL line edit form the webengine view.
61 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
62 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface()));
63 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface()));
65 // Setup the URL bar buttons.
66 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
67 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
68 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
69 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
70 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
72 // Instantiate the mouse event pointer.
73 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
75 // Install the mouse event filter.
76 qApp->installEventFilter(mouseEventFilterPointer);
78 // Listen for hovered link URLs.
79 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
81 // Instantiate the URL request interceptor.
82 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
84 // Set the URL request interceptor.
85 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
87 // Reapply the domain settings when the host changes.
88 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading()));
91 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
93 // Don't allow JavaScript to open windows.
94 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
96 // Apply the application settings.
97 applyApplicationSettings();
99 // Set the focus on the WebEngine view.
100 webEngineViewPointer->setFocus();
102 // Get the arguments.
103 QStringList argumentsStringList = qApp->arguments();
105 // Check to see if the arguments lists contains a URL.
106 if (argumentsStringList.size() > 1)
108 // Load the URL from the arguments list.
109 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
113 // Load the homepage.
118 void MainView::applyApplicationSettings()
120 // Set the search engine URL.
121 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
123 // Emit the search engine updated signal, which causes the on-the-fly menu to be updated.
124 emit searchEngineUpdated(Settings::searchEngine());
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 MainView::applyDomainSettingsAndReload() const
130 // Apply the domain settings. `true` reloads the website.
131 applyDomainSettings(true);
134 // 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.
135 void MainView::applyDomainSettingsWithoutReloading() const
137 // Apply the domain settings `false` does not reload the website.
138 applyDomainSettings(false);
141 void MainView::applyDomainSettings(bool reloadWebsite) const
143 // Set the JavaScript status.
144 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
146 // Update the JavaScript button.
147 if (Settings::javaScript())
149 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
153 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
156 // Apply the user agent.
157 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
159 // Set the zoom factor.
160 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
162 // Emit the on-the-fly menu update signals.
163 emit userAgentUpdated(Settings::userAgent());
164 emit zoomFactorUpdated(Settings::zoomFactor());
166 // Reload the website if requested.
169 webEngineViewPointer->reload();
173 void MainView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
175 // Store the search engine name.
176 QString searchEngineName = searchEngineActionPointer->text();
178 // Strip out any `&` characters.
179 searchEngineName.remove('&');
181 // Store the search engine string.
182 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
185 void MainView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
187 // Get the user agent name.
188 QString userAgentName = userAgentActionPointer->text();
190 // Strip out any `&` characters.
191 userAgentName.remove('&');
193 // Apply the user agent.
194 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(userAgentName));
196 // Reload the website.
197 webEngineViewPointer->reload();
200 void MainView::applyOnTheFlyZoomFactor(const double &zoomFactor) const
202 // Set the zoom factor.
203 webEngineViewPointer->setZoomFactor(zoomFactor);
206 void MainView::goHome() const
208 // Load the homepage.
209 webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage()));
212 void MainView::loadUrlFromTextBox(QString urlFromUser) const
214 // Remove the focus from the URL line edit.
215 urlLineEditPointer->clearFocus();
217 // Decide if the text is more likely to be a URL or a search.
218 if (urlFromUser.contains(".")) // The text is likely a URL.
220 // Check if the URL does not start with a valid protocol.
221 if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
223 // Add `https://` to the beginning of the URL.
224 urlFromUser = "https://" + urlFromUser;
228 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
230 else // The text is likely a search.
233 webEngineViewPointer->setUrl(QUrl::fromUserInput(searchEngineUrl + urlFromUser));
237 void MainView::pageLinkHovered(const QString &linkUrl) const
239 // Emit a signal so that the browser window can update the status bar.
240 emit linkHovered(linkUrl);
243 void MainView::toggleJavaScript() const
245 // Toggle JavaScript.
246 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
248 // Update the JavaScript button.
249 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
251 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
255 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
258 // Reload the website.
259 webEngineViewPointer->reload();
262 void MainView::updateInterface() const
264 // Update the URL line edit if it does not have focus.
265 if (!urlLineEditPointer->hasFocus())
267 // Update the URL line edit.
268 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
271 // Update the status of the forward and back buttons.
272 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
273 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
275 // 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>
276 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());