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