X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2FMainView.cpp;fp=src%2FMainView.cpp;h=cf84eccf063142977d66cd48d259b9fd91e2632f;hp=0000000000000000000000000000000000000000;hb=05800bafb963a10f291d2a9604dc90cd7ac37c7b;hpb=a65c9f31cc84f245da08642cefefe7a17fa941ce diff --git a/src/MainView.cpp b/src/MainView.cpp new file mode 100644 index 0000000..cf84ecc --- /dev/null +++ b/src/MainView.cpp @@ -0,0 +1,203 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of Privacy Browser PC . + * + * Privacy Browser PC is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser PC is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +// Application headers +#include "BrowserWindow.h" +#include "MainView.h" +#include "MouseEventFilter.h" +#include "Settings.h" +#include "ui_MainView.h" + +// Qt framework headers. +#include + +MainView::MainView(QWidget *parent) : QWidget(parent) +{ + // Instantiate the mainview UI. + Ui::MainView mainViewUi; + + // Setup the UI. + mainViewUi.setupUi(this); + + // Get handles for the views. + backButtonPointer = mainViewUi.backButton; + forwardButtonPointer = mainViewUi.forwardButton; + QPushButton *homeButtonPointer = mainViewUi.homeButton; + urlLineEditPointer = mainViewUi.urlLineEdit; + javaScriptButtonPointer = mainViewUi.javaScript; + webEngineViewPointer = mainViewUi.webEngineView; + + // Get handles for the aspects of the WebEngine. + QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page(); + webEngineHistoryPointer = webEnginePagePointer->history(); + webEngineProfilePointer = webEnginePagePointer->profile(); + webEngineSettingsPointer = webEngineViewPointer->settings(); + + // Update the webengine view from the URL line edit. + connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString))); + + // Update the URL line edit form the webengine view. + connect(webEngineViewPointer, SIGNAL(loadStarted()), this, SLOT(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadProgress(int)), this, SLOT(updateInterface())); + connect(webEngineViewPointer, SIGNAL(loadFinished(bool)), this, SLOT(updateInterface())); + + // Setup the URL bar buttons. + connect(backButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(back())); + connect(forwardButtonPointer, SIGNAL(clicked()), webEngineViewPointer, SLOT(forward())); + connect(homeButtonPointer, SIGNAL(clicked()), this, SLOT(goHome())); + connect(javaScriptButtonPointer, SIGNAL(clicked()), this, SLOT(toggleJavaScript())); + + // Instantiate the mouse event pointer. + MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer); + + // Install the mouse event filter. + qApp->installEventFilter(mouseEventFilterPointer); + + // Listen for hovered link URLs. + connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString))); + + // Disable the cache. + webEngineProfilePointer->setHttpCacheType(QWebEngineProfile::NoCache); + + // Instantiate the user agent helper. + userAgentHelperPointer = new UserAgentHelper(); + + // Don't allow JavaScript to open windows. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false); + + // Set the zoom factor. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); + + // Apply the application settings. + applyApplicationSettings(); + + // Apply the domain settings. `false` does not reload the website. + applyDomainSettings(false); + + // Set the focus on the WebEngine view. + webEngineViewPointer->setFocus(); + + // Get the arguments. + QStringList argumentsStringList = qApp->arguments(); + + // Check to see if the arguments lists contains a URL. + if (argumentsStringList.size() > 1) + { + // Load the URL from the arguments list. + webEngineViewPointer->setUrl(QUrl::fromUserInput(argumentsStringList.at(1))); + } + else + { + // Load the homepage. + goHome(); + } +} + +void MainView::applyApplicationSettings() +{ + // TODO. +} + +void MainView::applyDomainSettingsAndReload() +{ + // Apply the domain setings. `true` reloads the website. + applyDomainSettings(true); +} + +void MainView::applyDomainSettings(bool reloadWebsite) +{ + // Set the JavaScript status. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScript()); + + // Update the JavaScript button. + if (Settings::javaScript()) + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); + } + else + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); + } + + // Apply the user agent. + webEngineProfilePointer->setHttpUserAgent(userAgentHelperPointer->getUserAgent(Settings::userAgent())); + + // Reload the website if requested. + if (reloadWebsite) + { + webEngineViewPointer->reload(); + } +} + +void MainView::goHome() +{ + // Load the homepage. TODO. Consider sanitizing the homepage input and adding things like protocols if they are missing. + webEngineViewPointer->setUrl(Settings::homepage()); +} + +void MainView::loadUrl(const QString &urlFromUser) +{ + // Remove the focus from the URL line edit. + urlLineEditPointer->clearFocus(); + + // Load the URL, adding standard protocol sections if needed. TODO. Replace this with logic that prefers HTTPS. + webEngineViewPointer->setUrl(QUrl::fromUserInput(urlFromUser)); +} + +void MainView::pageLinkHovered(const QString &linkUrl) +{ + // Emit a signal so that the browser window can update the status bar. + emit linkHovered(linkUrl); +} + +void MainView::toggleJavaScript() +{ + // Toggle JavaScript. + webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)); + + // Update the JavaScript button. + if (webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled)) + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/javascript-warning")); + } + else + { + javaScriptButtonPointer->setIcon(QIcon(":/icons/privacy-mode")); + } + + // Reload the website. + webEngineViewPointer->reload(); +} + +void MainView::updateInterface() +{ + // Update the URL line edit if it does not have focus. + if (!urlLineEditPointer->hasFocus()) + { + // Update the URL line edit. + urlLineEditPointer->setText(webEngineViewPointer->url().toString()); + } + + // Update the status of the forward and back buttons. + backButtonPointer->setEnabled(webEngineHistoryPointer->canGoBack()); + forwardButtonPointer->setEnabled(webEngineHistoryPointer->canGoForward()); + + // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. Hopefully it will be fixed in Qt6. + webEngineViewPointer->setZoomFactor(Settings::zoomFactor()); +}