]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/views/BrowserView.cpp
Implement manual adding of cookies.
[PrivacyBrowserPC.git] / src / views / BrowserView.cpp
index fb2473a6977c406deb3221bcadac4ca5394409d9..b5c915bc1b1e9e3556b3b5c8501dd5643a372d20 100644 (file)
@@ -19,9 +19,9 @@
 
 // Application headers.
 #include "BrowserView.h"
-#include "MouseEventFilter.h"
 #include "Settings.h"
 #include "ui_BrowserView.h"
+#include "filters/MouseEventFilter.h"
 #include "helpers/DomainsDatabaseHelper.h"
 #include "helpers/SearchEngineHelper.h"
 #include "helpers/UserAgentHelper.h"
@@ -30,7 +30,9 @@
 
 // Qt framework headers.
 #include <QAction>
-#include <QWebEngineProfile>
+
+// Initialize the public static variables.
+QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
 
 BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
 {
@@ -47,7 +49,7 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
 
     // Create a WebEngine page.
-    QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
+    webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
 
     // Set the WebEngine page.
     webEngineViewPointer->setPage(webEnginePagePointer);
@@ -55,6 +57,13 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     // Get handles for the aspects of the WebEngine.
     webEngineHistoryPointer = webEnginePagePointer->history();
     webEngineSettingsPointer = webEngineViewPointer->settings();
+    webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
+
+    // Store a copy of each cookie when it is added.
+    connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), this, SLOT(cookieAdded(QNetworkCookie)));
+
+    // Store a copy of the WebEngine default user agent.
+    webEngineDefaultUserAgent = webEngineProfilePointer->httpUserAgent();
 
     // Update the URL line edit when the URL changes.
     connect(webEngineViewPointer, SIGNAL(urlChanged(const QUrl)), this, SLOT(updateUrl(const QUrl)));
@@ -65,11 +74,15 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     connect(webEngineViewPointer, SIGNAL(loadFinished(const bool)), this, SLOT(loadFinished()));
 
     // Instantiate the mouse event filter pointer.
-    MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter(webEngineViewPointer);
+    MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
 
     // Install the mouse event filter.
     qApp->installEventFilter(mouseEventFilterPointer);
 
+    // Process mouse forward and back commands.
+    connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
+    connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
+
     // Listen for hovered link URLs.
     connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
 
@@ -101,6 +114,33 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     webEngineViewPointer->setFocus();
 }
 
+BrowserView::~BrowserView()
+{
+    // Delay the deletion of the WebEngine page to prevent the following error:  `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
+    webEnginePagePointer->deleteLater();
+}
+
+// The cookie is copied instead of referenced so that changes made to the cookie do not create a race condition with the display of the cookie in the dialog.
+void BrowserView::addCookieToStore(QNetworkCookie cookie) const
+{
+    // Create a url.
+    QUrl url;
+
+    // Check to see if the domain does not start with a `.` because Qt makes this harder than it should be.  <https://doc.qt.io/qt-5/qwebenginecookiestore.html#setCookie>
+    if (!cookie.domain().startsWith(QStringLiteral(".")))
+    {
+        // Populate the URL.
+        url.setHost(cookie.domain());
+        url.setScheme(QStringLiteral("https"));
+
+        // Clear the domain from the cookie.
+        cookie.setDomain(QStringLiteral(""));
+    }
+
+    // Add the cookie to the store.
+    webEngineCookieStorePointer->setCookie(cookie, url);
+}
+
 void BrowserView::applyApplicationSettings()
 {
     // Set the search engine URL.
@@ -292,6 +332,18 @@ void BrowserView::back() const
     webEngineViewPointer->back();
 }
 
+void BrowserView::cookieAdded(const QNetworkCookie &cookie) const
+{
+    // Add the cookie to the cookie list.
+    emit addCookie(cookie);
+}
+
+void BrowserView::deleteAllCookies() const
+{
+    // Delete all the cookies.
+    webEngineCookieStorePointer->deleteAllCookies();
+}
+
 void BrowserView::forward() const
 {
     // Go forward.
@@ -370,6 +422,32 @@ void BrowserView::loadUrlFromLineEdit(QString url) const
     }
 }
 
+void BrowserView::mouseBack() const
+{
+    // Go back if possible.
+    if (webEngineHistoryPointer->canGoBack())
+    {
+        // Clear the URL line edit focus.
+        emit clearUrlLineEditFocus();
+
+        // Go back.
+        webEngineViewPointer->back();
+    }
+}
+
+void BrowserView::mouseForward() const
+{
+    // Go forward if possible.
+    if (webEngineHistoryPointer->canGoForward())
+    {
+        // Clear the URL line edit focus.
+        emit clearUrlLineEditFocus();
+
+        // Go forward.
+        webEngineViewPointer->forward();
+    }
+}
+
 void BrowserView::pageLinkHovered(const QString &linkUrl) const
 {
     // Emit a signal so that the browser window can update the status bar.