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.
31 #include <QWebEngineProfile>
33 MainView::MainView(QWidget *parent) : QWidget(parent)
35 // Instantiate the mainview UI.
36 Ui::MainView mainViewUi;
39 mainViewUi.setupUi(this);
41 // Get handles for the views.
42 backButtonPointer = mainViewUi.backButton;
43 forwardButtonPointer = mainViewUi.forwardButton;
44 QPushButton *refreshButtonPointer = mainViewUi.refreshButton;
45 QPushButton *homeButtonPointer = mainViewUi.homeButton;
46 urlLineEditPointer = mainViewUi.urlLineEdit;
47 javaScriptButtonPointer = mainViewUi.javaScript;
48 webEngineViewPointer = mainViewUi.webEngineView;
50 // Get handles for the aspects of the WebEngine.
51 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
52 webEngineHistoryPointer = webEnginePagePointer->history();
53 webEngineProfilePointer = webEnginePagePointer->profile();
54 webEngineSettingsPointer = webEngineViewPointer->settings();
56 // Update the webengine view from the URL line edit.
57 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
59 // Update the URL line edit form the webengine view.
60 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
61 connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface()));
62 connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface()));
64 // Setup the URL bar buttons.
65 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
66 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
67 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
68 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
69 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
71 // Instantiate the mouse event pointer.
72 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
74 // Install the mouse event filter.
75 qApp->installEventFilter(mouseEventFilterPointer);
77 // Listen for hovered link URLs.
78 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
80 // Instantiate the URL request interceptor.
81 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
83 // Set the URL request interceptor.
84 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
86 // Reapply the domain settings when the host changes.
87 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading()));
90 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
92 // Don't allow JavaScript to open windows.
93 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
95 // Set the zoom factor.
96 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
98 // Apply the application settings.
99 applyApplicationSettings();
101 // Apply the domain settings. `false` does not reload the website.
102 applyDomainSettings(false);
104 // Set the focus on the WebEngine view.
105 webEngineViewPointer->setFocus();
107 // Get the arguments.
108 QStringList argumentsStringList = qApp->arguments();
110 // Check to see if the arguments lists contains a URL.
111 if (argumentsStringList.size() > 1)
113 // Load the URL from the arguments list.
114 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
118 // Load the homepage.
123 void MainView::applyApplicationSettings() const
128 // 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.
129 void MainView::applyDomainSettingsAndReload() const
131 // Apply the domain settings. `true` reloads the website.
132 applyDomainSettings(true);
135 // 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.
136 void MainView::applyDomainSettingsWithoutReloading() const
138 // Apply the domain settings `false` does not reload the website.
139 applyDomainSettings(false);
142 void MainView::applyDomainSettings(bool reloadWebsite) const
144 // Set the JavaScript status.
145 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
147 // Update the JavaScript button.
148 if (Settings::javaScript())
150 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
154 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
157 // Apply the user agent.
158 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
160 // Reload the website if requested.
163 webEngineViewPointer->reload();
167 void MainView::goHome() const
169 // Load the homepage.
170 webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage()));
173 void MainView::loadUrlFromTextBox(QString urlFromUser) const
175 // Remove the focus from the URL line edit.
176 urlLineEditPointer->clearFocus();
178 // Decide if the text is more likely to be a URL or a search.
179 if (urlFromUser.contains(".")) // The text is likely a URL.
181 // Check if the URL does not start with a valid protocol.
182 if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
184 // Add `https://` to the beginning of the URL.
185 urlFromUser = "https://" + urlFromUser;
189 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
191 else // The text is likely a search.
194 webEngineViewPointer->setUrl(QUrl::fromUserInput(SearchEngineHelper::getSearchUrl(Settings::searchEngine()) + urlFromUser));
198 void MainView::pageLinkHovered(const QString &linkUrl) const
200 // Emit a signal so that the browser window can update the status bar.
201 emit linkHovered(linkUrl);
204 void MainView::toggleJavaScript() const
206 // Toggle JavaScript.
207 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
209 // Update the JavaScript button.
210 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
212 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
216 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
219 // Reload the website.
220 webEngineViewPointer->reload();
223 void MainView::updateInterface() const
225 // Update the URL line edit if it does not have focus.
226 if (!urlLineEditPointer->hasFocus())
228 // Update the URL line edit.
229 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
232 // Update the status of the forward and back buttons.
233 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
234 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
236 // 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>
237 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());