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"
27 // Qt framework headers.
28 #include <QWebEngineProfile>
30 MainView::MainView(QWidget *parent) : QWidget(parent)
32 // Instantiate the mainview UI.
33 Ui::MainView mainViewUi;
36 mainViewUi.setupUi(this);
38 // Get handles for the views.
39 backButtonPointer = mainViewUi.backButton;
40 forwardButtonPointer = mainViewUi.forwardButton;
41 QPushButton *refreshButtonPointer = mainViewUi.refreshButton;
42 QPushButton *homeButtonPointer = mainViewUi.homeButton;
43 urlLineEditPointer = mainViewUi.urlLineEdit;
44 javaScriptButtonPointer = mainViewUi.javaScript;
45 webEngineViewPointer = mainViewUi.webEngineView;
47 // Get handles for the aspects of the WebEngine.
48 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
49 webEngineHistoryPointer = webEnginePagePointer->history();
50 webEngineProfilePointer = webEnginePagePointer->profile();
51 webEngineSettingsPointer = webEngineViewPointer->settings();
53 // Update the webengine view from the URL line edit.
54 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
56 // Update the URL line edit form the webengine view.
57 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
58 connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
59 connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
61 // Setup the URL bar buttons.
62 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
63 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
64 connect(refreshButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(reload()));
65 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
66 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
68 // Instantiate the mouse event pointer.
69 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
71 // Install the mouse event filter.
72 qApp->installEventFilter(mouseEventFilterPointer);
74 // Listen for hovered link URLs.
75 connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
78 webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
80 // Instantiate the user agent helper.
81 userAgentHelperPointer = new UserAgentHelper();
83 // Don't allow JavaScript to open windows.
84 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
86 // Set the zoom factor.
87 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
89 // Apply the application settings.
90 applyApplicationSettings();
92 // Apply the domain settings. `false` does not reload the website.
93 applyDomainSettings(false);
95 // Set the focus on the WebEngine view.
96 webEngineViewPointer->setFocus();
99 QStringList argumentsStringList = qApp->arguments();
101 // Check to see if the arguments lists contains a URL.
102 if (argumentsStringList.size() > 1)
104 // Load the URL from the arguments list.
105 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
109 // Load the homepage.
114 void MainView::applyApplicationSettings()
119 void MainView::applyDomainSettingsAndReload()
121 // Apply the domain setings. `true` reloads the website.
122 applyDomainSettings(true);
125 void MainView::applyDomainSettings(bool reloadWebsite)
127 // Set the JavaScript status.
128 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
130 // Update the JavaScript button.
131 if (Settings::javaScript())
133 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
137 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
140 // Apply the user agent.
141 webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent()));
143 // Reload the website if requested.
146 webEngineViewPointer->reload();
150 void MainView::goHome()
152 // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing.
153 webEngineViewPointer->setUrl(Settings::homepage());
156 void MainView::loadUrl(const QString &urlFromUser)
158 // Remove the focus from the URL line edit.
159 urlLineEditPointer->clearFocus();
161 // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS.
162 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
165 void MainView::pageLinkHovered(const QString &linkUrl)
167 // Emit a signal so that the browser window can update the status bar.
168 emit linkHovered(linkUrl);
171 void MainView::toggleJavaScript()
173 // Toggle JavaScript.
174 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
176 // Update the JavaScript button.
177 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
179 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
183 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
186 // Reload the website.
187 webEngineViewPointer->reload();
190 void MainView::updateInterface()
192 // Update the URL line edit if it does not have focus.
193 if (!urlLineEditPointer->hasFocus())
195 // Update the URL line edit.
196 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
199 // Update the status of the forward and back buttons.
200 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
201 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
203 // 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>
204 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());