]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/MainView.cpp
Prefer HTTPS for URLS without a protocol.
[PrivacyBrowserPC.git] / src / MainView.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers
21 #include "BrowserWindow.h"
22 #include "MainView.h"
23 #include "MouseEventFilter.h"
24 #include "Settings.h"
25 #include "ui_MainView.h"
26 #include "UserAgentHelper.h"
27
28 // Qt framework headers.
29 #include <QWebEngineProfile>
30
31 MainView::MainView(QWidget *parent) : QWidget(parent)
32 {
33     // Instantiate the mainview UI.
34     Ui::MainView mainViewUi;
35
36     // Setup the UI.
37     mainViewUi.setupUi(this);
38
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;
47
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();
53
54     // Update the webengine view from the URL line edit.
55     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
56
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()));
61
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()));
68
69     // Instantiate the mouse event pointer.
70     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
71
72     // Install the mouse event filter.
73     qApp->installEventFilter(mouseEventFilterPointer);
74
75     // Listen for hovered link URLs.
76     connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
77
78     // Disable the cache.
79     webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
80
81     // Don't allow JavaScript to open windows.
82     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
83
84     // Set the zoom factor.
85     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
86
87     // Apply the application settings.
88     applyApplicationSettings();
89
90     // Apply the domain settings.  `false` does not reload the website.
91     applyDomainSettings(false);
92
93     // Set the focus on the WebEngine view.
94     webEngineViewPointer->setFocus();
95
96     // Get the arguments.
97     QStringList argumentsStringList = qApp->arguments();
98
99     // Check to see if the arguments lists contains a URL.
100     if (argumentsStringList.size() > 1)
101     {
102         // Load the URL from the arguments list.
103         webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
104     }
105     else
106     {
107         // Load the homepage.
108         goHome();
109     }
110 }
111
112 void MainView::applyApplicationSettings() const
113 {
114     // TODO.
115 }
116
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
119 {
120     // Apply the domain setings.  `true` reloads the website.
121     applyDomainSettings(true);
122 }
123
124 void MainView::applyDomainSettings(bool reloadWebsite) const
125 {
126     // Set the JavaScript status.
127     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
128
129     // Update the JavaScript button.
130     if (Settings::javaScript())
131     {
132         javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
133     }
134     else
135     {
136         javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
137     }
138
139     // Apply the user agent.
140     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
141
142     // Reload the website if requested.
143     if (reloadWebsite)
144     {
145         webEngineViewPointer->reload();
146     }
147 }
148
149 void MainView::goHome() const
150 {
151     // Load the homepage.
152     webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage().toString()));
153 }
154
155 void MainView::loadUrlFromTextBox(QString urlFromUser) const
156 {
157     // Remove the focus from the URL line edit.
158     urlLineEditPointer->clearFocus();
159
160     // Check if the URL does not start with a valid protocol.
161     if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
162     {
163         // Add `https://` to the beginning of the URL.
164         urlFromUser = "https://" + urlFromUser;
165     }
166
167     // Load the URL.
168     webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
169 }
170
171 void MainView::pageLinkHovered(const QString &linkUrl) const
172 {
173     // Emit a signal so that the browser window can update the status bar.
174     emit linkHovered(linkUrl);
175 }
176
177 void MainView::toggleJavaScript() const
178 {
179     // Toggle JavaScript.
180     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
181
182     // Update the JavaScript button.
183     if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
184     {
185         javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
186     }
187     else
188     {
189         javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
190     }
191
192     // Reload the website.
193     webEngineViewPointer->reload();
194 }
195
196 void MainView::updateInterface() const
197 {
198     // Update the URL line edit if it does not have focus.
199     if (!urlLineEditPointer->hasFocus())
200     {
201         // Update the URL line edit.
202         urlLineEditPointer->setText(webEngineViewPointer->url().toString());
203     }
204
205     // Update the status of the forward and back buttons.
206     backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
207     forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
208
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());
211 }