From 1811a03414b7076ed02b25d6e69f264a74e13a8c Mon Sep 17 00:00:00 2001
From: Martin Rotter <rotter@praktik.cz>
Date: Wed, 5 Jan 2022 09:45:43 +0100
Subject: [PATCH] implement #556

---
 .../desktop/com.github.rssguard.appdata.xml   |  2 +-
 src/librssguard/network-web/cookiejar.cpp     | 67 +++++++++++++++++--
 src/librssguard/network-web/cookiejar.h       | 13 ++++
 3 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/resources/desktop/com.github.rssguard.appdata.xml b/resources/desktop/com.github.rssguard.appdata.xml
index cc573b754..a6e55a639 100644
--- a/resources/desktop/com.github.rssguard.appdata.xml
+++ b/resources/desktop/com.github.rssguard.appdata.xml
@@ -26,7 +26,7 @@
   <url type="donation">https://github.com/sponsors/martinrotter</url>
   <content_rating type="oars-1.1" />
   <releases>
-    <release version="4.0.4" date="2022-01-04"/>
+    <release version="4.0.4" date="2022-01-05"/>
   </releases>
   <content_rating type="oars-1.0">
     <content_attribute id="violence-cartoon">none</content_attribute>
diff --git a/src/librssguard/network-web/cookiejar.cpp b/src/librssguard/network-web/cookiejar.cpp
index ef4c047bc..1e1a363cc 100644
--- a/src/librssguard/network-web/cookiejar.cpp
+++ b/src/librssguard/network-web/cookiejar.cpp
@@ -13,7 +13,26 @@
 #include <QNetworkCookie>
 #include <QSettings>
 
+#if defined(USE_WEBENGINE)
+#include <QWebEngineCookieStore>
+#include <QWebEngineProfile>
+#endif
+
 CookieJar::CookieJar(QObject* parent) : QNetworkCookieJar(parent) {
+#if defined(USE_WEBENGINE)
+  QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(QWebEngineProfile::PersistentCookiesPolicy::NoPersistentCookies);
+
+  m_webEngineCookies = QWebEngineProfile::defaultProfile()->cookieStore();
+
+  // When cookies change in WebEngine, then change in main cookie jar too.
+  connect(m_webEngineCookies, &QWebEngineCookieStore::cookieAdded, this, [=](const QNetworkCookie& cookie) {
+    insertCookieInternal(cookie, false, true);
+  });
+  connect(m_webEngineCookies, &QWebEngineCookieStore::cookieRemoved, this, [=](const QNetworkCookie& cookie) {
+    deleteCookieInternal(cookie, false);
+  });
+#endif
+
   loadCookies();
 }
 
@@ -56,7 +75,7 @@ void CookieJar::loadCookies() {
       auto cookie = QNetworkCookie::parseCookies(encoded);
 
       if (!cookie.isEmpty()) {
-        if (!QNetworkCookieJar::insertCookie(cookie.at(0))) {
+        if (!insertCookieInternal(cookie.at(0), true, false)) {
           qCriticalNN << LOGSEC_NETWORK
                       << "Failed to load cookie"
                       << QUOTE_W_SPACE(cookie_key)
@@ -95,32 +114,70 @@ bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookie_list, cons
   return QNetworkCookieJar::setCookiesFromUrl(cookie_list, url);
 }
 
-bool CookieJar::insertCookie(const QNetworkCookie& cookie) {
+bool CookieJar::insertCookieInternal(const QNetworkCookie& cookie, bool notify_others, bool should_save) {
   auto result = QNetworkCookieJar::insertCookie(cookie);
 
   if (result) {
-    saveCookies();
+    if (should_save) {
+      saveCookies();
+    }
+
+#if defined(USE_WEBENGINE)
+    if (notify_others) {
+      m_webEngineCookies->setCookie(cookie);
+    }
+#else
+    Q_UNUSED(notify_others)
+#endif
   }
 
   return result;
 }
 
-bool CookieJar::updateCookie(const QNetworkCookie& cookie) {
+bool CookieJar::updateCookieInternal(const QNetworkCookie& cookie, bool notify_others) {
   auto result = QNetworkCookieJar::updateCookie(cookie);
 
   if (result) {
     saveCookies();
+
+#if defined(USE_WEBENGINE)
+    if (notify_others) {
+      m_webEngineCookies->setCookie(cookie);
+    }
+#else
+    Q_UNUSED(notify_others)
+#endif
   }
 
   return result;
 }
 
-bool CookieJar::deleteCookie(const QNetworkCookie& cookie) {
+bool CookieJar::deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others) {
   auto result = QNetworkCookieJar::deleteCookie(cookie);
 
   if (result) {
     saveCookies();
+
+#if defined(USE_WEBENGINE)
+    if (notify_others) {
+      m_webEngineCookies->deleteCookie(cookie);
+    }
+#else
+    Q_UNUSED(notify_others)
+#endif
   }
 
   return result;
 }
+
+bool CookieJar::insertCookie(const QNetworkCookie& cookie) {
+  return insertCookieInternal(cookie, true, true);
+}
+
+bool CookieJar::deleteCookie(const QNetworkCookie& cookie) {
+  return deleteCookieInternal(cookie, true);
+}
+
+bool CookieJar::updateCookie(const QNetworkCookie& cookie) {
+  return updateCookieInternal(cookie, true);
+}
diff --git a/src/librssguard/network-web/cookiejar.h b/src/librssguard/network-web/cookiejar.h
index 8b6507e39..abfcc44d9 100644
--- a/src/librssguard/network-web/cookiejar.h
+++ b/src/librssguard/network-web/cookiejar.h
@@ -5,6 +5,10 @@
 
 #include <QNetworkCookieJar>
 
+#if defined(USE_WEBENGINE)
+class QWebEngineCookieStore;
+#endif
+
 class CookieJar : public QNetworkCookieJar {
   public:
     explicit CookieJar(QObject* parent = nullptr);
@@ -19,8 +23,17 @@ class CookieJar : public QNetworkCookieJar {
     static QList<QNetworkCookie> extractCookiesFromUrl(const QString& url);
 
   private:
+    bool insertCookieInternal(const QNetworkCookie& cookie, bool notify_others, bool should_save);
+    bool updateCookieInternal(const QNetworkCookie& cookie, bool notify_others);
+    bool deleteCookieInternal(const QNetworkCookie& cookie, bool notify_others);
+
     void loadCookies();
     void saveCookies();
+
+  private:
+#if defined(USE_WEBENGINE)
+    QWebEngineCookieStore * m_webEngineCookies;
+#endif
 };
 
 #endif // COOKIEJAR_H