CookieJar: Don't accidentally overwrite cookies. #2808
authorChristian Kamm <kamm@incasoftware.de>
Fri, 27 Mar 2015 09:49:24 +0000 (10:49 +0100)
committerChristian Kamm <kamm@incasoftware.de>
Fri, 27 Mar 2015 09:49:24 +0000 (10:49 +0100)
Calling save() in the CookieJar destructor was problematic. For instance
we sometimes create a new QNAM with a new CookieJar and then call
setCookieJar() on it to assign some other jar. That destroy the fresh
jar and potentially overwrite cookies.

Also explicitly saving the account's cookies when the account is saved
is more explicit and thus more reliable than counting on the Account
destructor to do it.

src/libsync/account.cpp
src/libsync/cookiejar.cpp
src/libsync/cookiejar.h

index 14e3ad20208015a89d7d5513d5f49e5b2d195fa7..a46611d1129fbcbf4f7a7bd8e705b803af814b97 100644 (file)
@@ -131,6 +131,15 @@ void Account::save()
     if (!certs.isEmpty()) {
         settings->setValue( QLatin1String(caCertsKeyC), certs );
     }
+
+    // Save cookies.
+    if (_am) {
+        CookieJar* jar = qobject_cast<CookieJar*>(_am->cookieJar());
+        if (jar) {
+            qDebug() << "Saving cookies.";
+            jar->save();
+        }
+    }
 }
 
 AccountPtr Account::restore()
index 62aa2ccbf2f87b621496d6c4813c6c6e104c3083..62900597c42fe67d1eec4cfd732260dad850044c 100644 (file)
@@ -71,7 +71,6 @@ CookieJar::CookieJar(QObject *parent) :
 
 CookieJar::~CookieJar()
 {
-    save();
 }
 
 bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
index a7c8c671cfb6484f6f71c2a8c9da2e89bb59f6ba..99cbfbf748bb6245a644714e79a0110fe8759fd1 100644 (file)
@@ -34,10 +34,11 @@ public:
     using QNetworkCookieJar::setAllCookies;
     using QNetworkCookieJar::allCookies;
 
+    void save();
+
 signals:
     void newCookiesForUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url);
 private:
-    void save();
     void restore();
     QList<QNetworkCookie> removeExpired(const QList<QNetworkCookie> &cookies);
     QString storagePath() const;