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 MainView::MainView(QWidget *parent) : QWidget(parent)
29 // Instantiate the mainview UI.
30 Ui::MainView mainViewUi;
33 mainViewUi.setupUi(this);
35 // Get handles for the views.
36 backButtonPointer = mainViewUi.backButton;
37 forwardButtonPointer = mainViewUi.forwardButton;
38 QPushButton *homeButtonPointer = mainViewUi.homeButton;
39 urlLineEditPointer = mainViewUi.urlLineEdit;
40 javaScriptButtonPointer = mainViewUi.javaScript;
41 webEngineViewPointer = mainViewUi.webEngineView;
43 // Get handles for the aspects of the WebEngine.
44 QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
45 webEngineHistoryPointer = webEnginePagePointer->history();
46 webEngineSettingsPointer = webEngineViewPointer->settings();
48 // Update the webengine view from the URL line edit.
49 connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
51 // Update the URL line edit form the webengine view.
52 connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface()));
53 connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface()));
54 connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface()));
56 // Setup the URL bar buttons.
57 connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back()));
58 connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward()));
59 connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome()));
60 connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript()));
62 // Instantiate the mouse event pointer.
63 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
65 // Install the mouse event filter.
66 qApp->installEventFilter(mouseEventFilterPointer);
68 // Listen for hovered link URLs.
69 connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
71 // Don't allow JavaScript to open windows.
72 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
74 // Set the zoom factor.
75 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());
77 // Apply the application settings.
78 applyApplicationSettings();
80 // Apply the domain settings.
81 applyDomainSettings();
83 // Set the focus on the WebEngine view.
84 webEngineViewPointer->setFocus();
87 QStringList argumentsStringList = qApp->arguments();
89 // Check to see if the arguments lists contains a URL.
90 if (argumentsStringList.size() > 1)
92 // Load the URL from the arguments list.
93 webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1)));
102 void MainView::applyApplicationSettings()
107 void MainView::applyDomainSettings()
109 // Set the JavaScript status.
110 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript());
112 // Update the JavaScript button.
113 if (Settings::javaScript())
115 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
119 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
122 // Reload the website.
123 webEngineViewPointer->reload();
126 void MainView::goHome()
128 // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing.
129 webEngineViewPointer->setUrl(Settings::homepage());
132 void MainView::loadUrl(const QString &urlFromUser)
134 // Remove the focus from the URL line edit.
135 urlLineEditPointer->clearFocus();
137 // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS.
138 webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser));
141 void MainView::pageLinkHovered(const QString &linkUrl)
143 // Emit a signal so that the browser window can update the status bar.
144 emit linkHovered(linkUrl);
147 void MainView::toggleJavaScript()
149 // Toggle JavaScript.
150 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
152 // Update the JavaScript button.
153 if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled))
155 javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning"));
159 javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode"));
162 // Reload the website.
163 webEngineViewPointer->reload();
166 void MainView::updateInterface()
168 // Update the URL line edit if it does not have focus.
169 if (!urlLineEditPointer->hasFocus())
171 // Update the URL line edit.
172 urlLineEditPointer->setText(webEngineViewPointer->url().toString());
175 // Update the status of the forward and back buttons.
176 backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack());
177 forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward());
179 // 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>
180 webEngineViewPointer->setZoomFactor(Settings::zoomFactor());