]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/BrowserView.cpp
319f9e51b7624ef49776445be3d1c776025dc550
[PrivacyBrowserPC.git] / src / views / BrowserView.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 "BrowserView.h"
22 #include "MouseEventFilter.h"
23 #include "Settings.h"
24 #include "ui_BrowserView.h"
25 #include "UrlRequestInterceptor.h"
26 #include "helpers/DomainsDatabaseHelper.h"
27 #include "helpers/SearchEngineHelper.h"
28 #include "helpers/UserAgentHelper.h"
29 #include "windows/BrowserWindow.h"
30
31 // Qt framework headers.
32 #include <QAction>
33 #include <QWebEngineProfile>
34
35 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
36 {
37     // Instantiate the browser view UI.
38     Ui::BrowserView browserViewUi;
39
40     // Setup the UI.
41     browserViewUi.setupUi(this);
42
43     // Get handles for the views.
44     webEngineViewPointer = browserViewUi.webEngineView;
45
46     // Get handles for the aspects of the WebEngine.
47     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
48     webEngineHistoryPointer = webEnginePagePointer->history();
49     webEngineProfilePointer = webEnginePagePointer->profile();
50     webEngineSettingsPointer = webEngineViewPointer->settings();
51
52     // Update the URL line edit from the webengine view.
53     connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
54     connect(webEngineViewPointer, SIGNAL(loadProgress(const int)), this, SLOT(updateInterface()));
55     connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(updateInterface()));
56
57     // Instantiate the mouse event filter pointer.
58     MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
59
60     // Install the mouse event filter.
61     qApp->installEventFilter(mouseEventFilterPointer);
62
63     // Listen for hovered link URLs.
64     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
65
66     // Instantiate the URL request interceptor.
67     UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
68
69     // Set the URL request interceptor.
70     webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
71
72     // Reapply the domain settings when the host changes.
73     connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
74
75     // Disable the cache.
76     webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache);
77
78     // Don't allow JavaScript to open windows.
79     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
80
81     // Set the focus on the WebEngine view.
82     webEngineViewPointer->setFocus();
83 }
84
85 void BrowserView::applyApplicationSettings()
86 {
87     // Set the search engine URL.
88     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
89
90     // Emit the search engine updated signal, which causes the on-the-fly menu to be updated.
91     emit searchEngineUpdated(Settings::searchEngine());
92 }
93
94 // 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.
95 void BrowserView::applyDomainSettingsAndReload() const
96 {
97     // Apply the domain settings.  `true` reloads the website.
98     applyDomainSettings(webEngineViewPointer->url().host(), true);
99 }
100
101 // 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.
102 void BrowserView::applyDomainSettingsWithoutReloading(const QString &hostname) const
103 {
104     // Apply the domain settings  `false` does not reload the website.
105     applyDomainSettings(hostname, false);
106 }
107
108 void BrowserView::applyDomainSettings(const QString &hostname, const bool reloadWebsite) const
109 {
110     // Get the record for the hostname.
111     QSqlQuery domainQuery = DomainsDatabaseHelper::getDomainQuery(hostname);
112
113     // Check if the hostname has domain settings.
114     if (domainQuery.isValid())  // The hostname has domain settings.
115     {
116         // Get the domain record.
117         QSqlRecord domainRecord = domainQuery.record();
118
119         // Set the JavaScript status.
120         switch (domainRecord.field(DomainsDatabaseHelper::JAVASCRIPT).value().toInt())
121         {
122             case (DomainsDatabaseHelper::SYSTEM_DEFAULT):
123             {
124                 // Set the default JavaScript status.
125                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
126
127                 break;
128             }
129
130             case (DomainsDatabaseHelper::DISABLED):
131             {
132                 // Disable JavaScript.
133                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
134
135                 break;
136             }
137
138             case (DomainsDatabaseHelper::ENABLED):
139             {
140                 // Enable JavaScript.
141                 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
142
143                 break;
144             }
145         }
146
147         // Set the user agent.
148         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabaseHelper::USER_AGENT).value().toString()));
149
150         // Set the zoom factor.
151         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
152
153         // Apply the domain settings palette to the URL line edit.
154         emit updateDomainSettingsIndicator(true);
155     }
156     else  // The hostname does not have domain settings.
157     {
158         // Set the JavaScript status.
159         webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
160
161         // Set the user agent.
162         webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
163
164         // Set the zoom factor.
165         webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
166
167         // Apply the no domain settings palette to the URL line edit.
168         emit updateDomainSettingsIndicator(false);
169     }
170
171     // Emit the on-the-fly menu update signals.
172     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
173     emit userAgentUpdated(webEngineProfilePointer->httpUserAgent());
174     emit zoomFactorUpdated(Settings::zoomFactor());
175
176     // Reload the website if requested.
177     if (reloadWebsite)
178     {
179         webEngineViewPointer->reload();
180     }
181 }
182
183 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
184 {
185     // Store the search engine name.
186     QString searchEngineName = searchEngineActionPointer->text();
187
188     // Strip out any `&` characters.
189     searchEngineName.remove('&');
190
191     // Store the search engine string.
192     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
193 }
194
195 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
196 {
197     // Get the user agent name.
198     QString userAgentName = userAgentActionPointer->text();
199
200     // Strip out any `&` characters.
201     userAgentName.remove('&');
202
203     // Apply the user agent.
204     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
205
206     // Reload the website.
207     webEngineViewPointer->reload();
208 }
209
210 void BrowserView::applyOnTheFlyZoomFactor(const double &zoomFactor) const
211 {
212     // Set the zoom factor.
213     webEngineViewPointer->setZoomFactor(zoomFactor);
214 }
215
216 void BrowserView::back() const
217 {
218     // Go back.
219     webEngineViewPointer->back();
220 }
221
222 void BrowserView::forward() const
223 {
224     // Go forward.
225     webEngineViewPointer->forward();
226 }
227
228 void BrowserView::home() const
229 {
230     // Load the homepage.
231     webEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
232 }
233
234 void BrowserView::loadInitialWebsite()
235 {
236     // Apply the application settings.
237     applyApplicationSettings();
238
239     // Get the arguments.
240     QStringList argumentsStringList = qApp->arguments();
241
242     // Check to see if the arguments lists contains a URL.
243     if (argumentsStringList.size() > 1)
244     {
245         // Load the URL from the arguments list.
246         webEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
247     }
248     else
249     {
250         // Load the homepage.
251         home();
252     }
253 }
254
255 void BrowserView::loadUrlFromLineEdit(QString url) const
256 {
257     // Decide if the text is more likely to be a URL or a search.
258     if (url.contains("."))  // The text is likely a URL.
259     {
260         // Check if the URL does not start with a valid protocol.
261         if (!url.startsWith("http") && !url.startsWith("file://"))
262         {
263             // Add `https://` to the beginning of the URL.
264             url = "https://" + url;
265         }
266
267         // Load the URL.
268         webEngineViewPointer->load(QUrl::fromUserInput(url));
269     }
270     else  // The text is likely a search.
271     {
272         // Load the search.
273         webEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
274     }
275 }
276
277 void BrowserView::pageLinkHovered(const QString &linkUrl) const
278 {
279     // Emit a signal so that the browser window can update the status bar.
280     emit linkHovered(linkUrl);
281 }
282
283 void BrowserView::refresh() const
284 {
285     // Reload the website.
286     webEngineViewPointer->reload();
287 }
288
289 void BrowserView::toggleJavaScript() const
290 {
291     // Toggle JavaScript.
292     webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
293
294     // Update the JavaScript icon.
295     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
296
297     // Reload the website.
298     webEngineViewPointer->reload();
299 }
300
301 void BrowserView::updateInterface() const
302 {
303     // Update the URL line edit.
304     emit updateUrlLineEdit(webEngineViewPointer->url().toString());
305
306     // Update the status of the forward and back buttons.
307     emit updateBackAction(webEngineHistoryPointer->canGoBack());
308     emit updateForwardAction(webEngineHistoryPointer->canGoForward());
309
310     // 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>
311     webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
312 }