]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/mainview.cpp
Enable setting as the default browser.
[PrivacyBrowserPC.git] / src / mainview.cpp
index 25f3b4c5ea0d1b6f39de2439abea2bff200d58f3..416014b6ba5b92c904816afd8e54a25930027b02 100644 (file)
 #include "mainview.h"
 #include "mouseeventfilter.h"
 #include "settings.h"
-
-// KDE Framework headers.
-#include <KLineEdit>
-
-// Qt headers.
-#include <QWebEngineHistory>
-#include <QWebEngineView>
+#include "ui_mainview.h"
 
 MainView::MainView(QWidget *parent) : QWidget(parent)
 {
@@ -43,11 +37,13 @@ MainView::MainView(QWidget *parent) : QWidget(parent)
     forwardButtonPointer = mainViewUi.forwardButton;
     QPushButton *homeButtonPointer = mainViewUi.homeButton;
     urlLineEditPointer = mainViewUi.urlLineEdit;
+    javaScriptButtonPointer = mainViewUi.javaScript;
     webEngineViewPointer = mainViewUi.webEngineView;
 
-    // Get handles for the webpage and history.
+    // Get handles for the aspects of the WebEngine.
     QWebEnginePage *webEnginePagePointer = webEngineViewPointer->page();
     webEngineHistoryPointer = webEnginePagePointer->history();
+    webEngineSettingsPointer = webEngineViewPointer->settings();
 
     // Update the webengine view from the URL line edit.
     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrl(const QString)));
@@ -61,6 +57,7 @@ MainView::MainView(QWidget *parent) : QWidget(parent)
     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);
@@ -71,14 +68,59 @@ MainView::MainView(QWidget *parent) : QWidget(parent)
     // Listen for hovered link URLs.
     connect(webEnginePagePointer, SIGNAL(linkHovered(QString)), this, SLOT(pageLinkHovered(QString)));
 
+    // 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.
+    applyDomainSettings();
+
     // Set the focus on the WebEngine view.
     webEngineViewPointer->setFocus();
 
-    // Load the homepage.
-    goHome();
+    // 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::applyDomainSettings()
+{
+    // 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"));
+    }
+
+    // Reload the website.
+    webEngineViewPointer->reload();
 }
 
 void MainView::goHome()
@@ -102,13 +144,32 @@ void MainView::pageLinkHovered(const QString &linkUrl)
     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->setUrl(webEngineViewPointer->url().toString());
+        urlLineEditPointer->setText(webEngineViewPointer->url().toString());
     }
 
     // Update the status of the forward and back buttons.