]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/MainView.cpp
99c9355053652253527d727961ba751041dbb898
[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 "UrlRequestInterceptor.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
29
30 // Qt framework headers.
31 #include <QAction>
32 #include <QWebEngineProfile>
33
34 MainView::MainView(QWidget *parent) : QWidget(parent)
35 {
36     // Instantiate the mainview UI.
37     Ui::MainView mainViewUi;
38
39     // Setup the UI.
40     mainViewUi.setupUi(this);
41
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;
50
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();
56
57     // Update the webengine view from the URL line edit.
58     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromTextBox(const QString)));
59
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()));
64
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()));
71
72     // Instantiate the mouse event pointer.
73     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
74
75     // Install the mouse event filter.
76     qApp->installEventFilter(mouseEventFilterPointer);
77
78     // Listen for hovered link URLs.
79     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
80
81     // Instantiate the URL request interceptor.
82     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
83
84     // Set the URL request interceptor.
85     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
86
87     // Reapply the domain settings when the host changes.
88     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings()), this, SLOT(applyDomainSettingsWithoutReloading()));
89
90     // Disable the cache.
91     webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
92
93     // Don't allow JavaScript to open windows.
94     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
95
96     // Apply the application settings.
97     applyApplicationSettings();
98
99     // Set the focus on the WebEngine view.
100     webEngineViewPointer->setFocus();
101
102     // Get the arguments.
103     QStringList argumentsStringList = qApp->arguments();
104
105     // Check to see if the arguments lists contains a URL.
106     if (argumentsStringList.size() > 1)
107     {
108         // Load the URL from the arguments list.
109         webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
110     }
111     else
112     {
113         // Load the homepage.
114         goHome();
115     }
116 }
117
118 void MainView::applyApplicationSettings()
119 {
120     // Set the search engine URL.
121     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
122
123     // Emit the search engine updated signal, which causes the on-the-fly menu to be updated.
124     emit searchEngineUpdated(Settings::searchEngine());
125 }
126
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
129 {
130     // Apply the domain settings.  `true` reloads the website.
131     applyDomainSettings(true);
132 }
133
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
136 {
137     // Apply the domain settings  `false` does not reload the website.
138     applyDomainSettings(false);
139 }
140
141 void MainView::applyDomainSettings(bool reloadWebsite) const
142 {
143     // Set the JavaScript status.
144     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
145
146     // Update the JavaScript button.
147     if (Settings::javaScript())
148     {
149         javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
150     }
151     else
152     {
153         javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
154     }
155
156     // Apply the user agent.
157     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(Settings::userAgent()));
158
159     // Emit the user agent updated signal, which causes the on-the-fly menu to be updated.
160     emit userAgentUpdated(Settings::userAgent());
161
162     // Set the zoom factor.
163     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
164
165     // Reload the website if requested.
166     if (reloadWebsite)
167     {
168         webEngineViewPointer->reload();
169     }
170 }
171
172 void MainView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
173 {
174     // Store the search engine name.
175     QString searchEngineName = searchEngineActionPointer->text();
176
177     // Strip out any `&` characters.
178     searchEngineName.remove('&');
179
180     // Store the search engine string.
181     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
182 }
183
184 void MainView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
185 {
186     // Get the user agent name.
187     QString userAgentName = userAgentActionPointer->text();
188
189     // Strip out any `&` characters.
190     userAgentName.remove('&');
191
192     // Apply the user agent.
193     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgent(userAgentName));
194
195     // Reload the website.
196     webEngineViewPointer->reload();
197 }
198
199 void MainView::goHome() const
200 {
201     // Load the homepage.
202     webEngineViewPointer->setUrl(QUrl::fromUserInput(Settings::homepage()));
203 }
204
205 void MainView::loadUrlFromTextBox(QString urlFromUser) const
206 {
207     // Remove the focus from the URL line edit.
208     urlLineEditPointer->clearFocus();
209
210     // Decide if the text is more likely to be a URL or a search.
211     if (urlFromUser.contains("."))  // The text is likely a URL.
212     {
213         // Check if the URL does not start with a valid protocol.
214         if (!urlFromUser.startsWith("http") && !urlFromUser.startsWith("file://"))
215         {
216             // Add `https://` to the beginning of the URL.
217             urlFromUser = "https://" + urlFromUser;
218         }
219
220         // Load the URL.
221         webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
222     }
223     else  // The text is likely a search.
224     {
225         // Load the search.
226         webEngineViewPointer->setUrl(QUrl::fromUserInput(searchEngineUrl + urlFromUser));
227     }
228 }
229
230 void MainView::pageLinkHovered(const QString &linkUrl) const
231 {
232     // Emit a signal so that the browser window can update the status bar.
233     emit linkHovered(linkUrl);
234 }
235
236 void MainView::toggleJavaScript() const
237 {
238     // Toggle JavaScript.
239     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
240
241     // Update the JavaScript button.
242     if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
243     {
244         javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
245     }
246     else
247     {
248         javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
249     }
250
251     // Reload the website.
252     webEngineViewPointer->reload();
253 }
254
255 void MainView::updateInterface() const
256 {
257     // Update the URL line edit if it does not have focus.
258     if (!urlLineEditPointer->hasFocus())
259     {
260         // Update the URL line edit.
261         urlLineEditPointer->setText(webEngineViewPointer->url().toString());
262     }
263
264     // Update the status of the forward and back buttons.
265     backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
266     forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
267
268     // 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>
269     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
270 }