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 "UserAgentHelper.h"
28 // Qt framework headers.
29 #include <QWebEngineProfile>
31 MainView::MainView(QWidget *parent) : QWidget(parent)
33 // Instantiate the mainview UI.
34 Ui::MainView mainViewUi;
37 mainViewUi.setupUi(this);
39 // Get handles for the views.
40 backButtonPointer = mainViewUi.backButton;
41 forwardButtonPointer = mainViewUi.forwardButton;
42 QPushButton *refreshButtonPointer = mainViewUi.refreshButton;
43 QPushButton *homeButtonPointer = mainViewUi.homeButton;
44 urlLineEditPointer = mainViewUi.urlLineEdit;
45 javaScriptButtonPointer = mainViewUi.javaScript;
46 webEngineViewPointer = mainViewUi.webEngineView;
48 // Get handles for the aspects of the WebEngine.
49 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
50 webEngineHistoryPointer = webEnginePagePointer->history();
51 webEngineProfilePointer = webEnginePagePointer->profile();
52 webEngineSettingsPointer = webEngineViewPointer->settings();
54 // Update the webengine view from the URL line edit.
55 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
57 // Update the URL line edit form the webengine view.
58 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
59 connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
60 connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
62 // Setup the URL bar buttons.
63 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
64 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
65 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
66 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
67 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
69 // Instantiate the mouse event pointer.
70 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
72 // Install the mouse event filter.
73 qApp->installEventFilter(mouseEventFilterPointer);
75 // Listen for hovered link URLs.
76 connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
79 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
81 // Don't allow JavaScript to open windows.
82 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
84 // Set the zoom factor.
85 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
87 // Apply the application settings.
88 applyApplicationSettings();
90 // Apply the domain settings. `false` does not reload the website.
91 applyDomainSettings(false);
93 // Set the focus on the WebEngine view.
94 webEngineViewPointer->setFocus();
97 QStringList argumentsStringList = qApp->arguments();
99 // Check to see if the arguments lists contains a URL.
100 if (argumentsStringList.size() > 1)
102 // Load the URL from the arguments list.
103 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
107 // Load the homepage.
112 void MainView::applyApplicationSettings() const
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 void MainView::applyDomainSettingsAndReload() const
120 // Apply the domain setings. `true` reloads the website.
121 applyDomainSettings(true);
124 void MainView::applyDomainSettings(bool reloadWebsite) const
126 // Set the JavaScript status.
127 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
129 // Update the JavaScript button.
130 if (Settings::javaScript())
132 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
136 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
139 // Apply the user agent.
140 webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
142 // Reload the website if requested.
145 webEngineViewPointer->reload();
149 void MainView::goHome() const
151 // Load the homepage.
152 webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage().toString()));
155 void MainView::loadUrlFromTextBox(QString urlFromUser) const
157 // Remove the focus from the URL line edit.
158 urlLineEditPointer->clearFocus();
160 // Check if the URL does not start with a valid protocol.
161 if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
163 // Add `https://` to the beginning of the URL.
164 urlFromUser = "https://" + urlFromUser;
168 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
171 void MainView::pageLinkHovered(const QString &linkUrl) const
173 // Emit a signal so that the browser window can update the status bar.
174 emit linkHovered(linkUrl);
177 void MainView::toggleJavaScript() const
179 // Toggle JavaScript.
180 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
182 // Update the JavaScript button.
183 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
185 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
189 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
192 // Reload the website.
193 webEngineViewPointer->reload();
196 void MainView::updateInterface() const
198 // Update the URL line edit if it does not have focus.
199 if (!urlLineEditPointer->hasFocus())
201 // Update the URL line edit.
202 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
205 // Update the status of the forward and back buttons.
206 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
207 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
209 // 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>
210 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());