Merge lp:~mardy/signon-ui/persistent-cookies into lp:signon-ui

Proposed by Alberto Mardegan
Status: Merged
Approved by: David King
Approved revision: 50
Merged at revision: 51
Proposed branch: lp:~mardy/signon-ui/persistent-cookies
Merge into: lp:signon-ui
Diff against target: 223 lines (+127/-4)
3 files modified
src/cookie-jar-manager.cpp (+110/-1)
src/cookie-jar-manager.h (+16/-3)
src/main.cpp (+1/-0)
To merge this branch: bzr merge lp:~mardy/signon-ui/persistent-cookies
Reviewer Review Type Date Requested Status
David King (community) Approve
jenkins (community) continuous-integration Approve
Review via email: mp+116828@code.launchpad.net

Description of the change

Implement persistent cookie storage

Cookies are saved into ~/.cache/signon-ui/cookies/<id>.jar

To post a comment you must log in.
Revision history for this message
jenkins (martin-mrazik+qa) wrote :
review: Needs Fixing (continuous-integration)
50. By Alberto Mardegan

Revert changes to DBus XML file

Latest Qt not yet available on Precise.

Revision history for this message
jenkins (martin-mrazik+qa) wrote :
review: Approve (continuous-integration)
Revision history for this message
David King (amigadave) wrote :

Tested and seems fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/cookie-jar-manager.cpp'
2--- src/cookie-jar-manager.cpp 2012-05-31 08:21:16 +0000
3+++ src/cookie-jar-manager.cpp 2012-07-26 11:32:19 +0000
4@@ -22,12 +22,18 @@
5
6 #include "debug.h"
7
8+#include <QCoreApplication>
9 #include <QDBusMetaType>
10+#include <QDataStream>
11+#include <QDesktopServices>
12+#include <QDir>
13+#include <QFile>
14 #include <QHash>
15
16 using namespace SignOnUi;
17
18 static CookieJarManager *m_instance = 0;
19+static const unsigned int JAR_VERSION = 1;
20
21 namespace SignOnUi {
22
23@@ -41,6 +47,7 @@
24 {
25 TRACE() << "Setting cookies for url:" << url;
26 TRACE() << cookieList;
27+ queueSave();
28 return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
29 }
30
31@@ -51,15 +58,108 @@
32 private:
33 mutable CookieJarManager *q_ptr;
34 QHash<quint32, CookieJar*> cookieJars;
35+ QDir cookieDir;
36 };
37
38 } // namespace
39
40+QDataStream &operator<<(QDataStream &stream,
41+ const QList<QNetworkCookie> &list)
42+{
43+ stream << JAR_VERSION;
44+ stream << quint32(list.size());
45+ foreach (const QNetworkCookie &cookie, list) {
46+ stream << cookie.toRawForm();
47+ }
48+ return stream;
49+}
50+
51+QDataStream &operator>>(QDataStream &stream,
52+ QList<QNetworkCookie> &list)
53+{
54+ list.clear();
55+
56+ quint32 version;
57+ stream >> version;
58+
59+ if (version != JAR_VERSION)
60+ return stream;
61+
62+ quint32 count;
63+ stream >> count;
64+ for (quint32 i = 0; i < count; i++)
65+ {
66+ QByteArray value;
67+ stream >> value;
68+ QList<QNetworkCookie> newCookies = QNetworkCookie::parseCookies(value);
69+ if (newCookies.count() == 0 && value.length() != 0) {
70+ qWarning() << "CookieJar: Unable to parse saved cookie:" << value;
71+ }
72+ foreach (const QNetworkCookie &cookie, newCookies) {
73+ list.append(cookie);
74+ }
75+ if (stream.atEnd())
76+ break;
77+ }
78+ return stream;
79+}
80+
81+CookieJar::CookieJar(QString cookiePath, QObject *parent):
82+ QNetworkCookieJar(parent),
83+ m_cookiePath(cookiePath)
84+{
85+ // Prepare the auto-save timer
86+ m_saveTimer.setInterval(10 * 1000);
87+ m_saveTimer.setSingleShot(true);
88+ QObject::connect(&m_saveTimer, SIGNAL(timeout()),
89+ this, SLOT(save()));
90+
91+ // Load any saved cookies
92+ QFile file(m_cookiePath);
93+ file.open(QIODevice::ReadOnly);
94+ QDataStream in(&file);
95+
96+ QList<QNetworkCookie> cookies;
97+ in >> cookies;
98+ setAllCookies(cookies);
99+}
100+
101+void CookieJar::save()
102+{
103+ TRACE() << "saving to" << m_cookiePath;
104+ QFile file(m_cookiePath);
105+ file.open(QIODevice::WriteOnly);
106+ QDataStream out(&file);
107+
108+ out << allCookies();
109+
110+ /* clear any running timer */
111+ m_saveTimer.stop();
112+}
113+
114+void CookieJar::queueSave()
115+{
116+ m_saveTimer.start();
117+}
118+
119 CookieJarManager::CookieJarManager(QObject *parent):
120 QObject(parent),
121 d_ptr(new CookieJarManagerPrivate)
122 {
123+ Q_D(CookieJarManager);
124+
125 qDBusRegisterMetaType<RawCookies>();
126+ qRegisterMetaTypeStreamOperators<QList<QNetworkCookie> >("QList<QNetworkCookie>");
127+
128+ d->cookieDir =
129+ QDesktopServices::storageLocation(QDesktopServices::CacheLocation) +
130+ QDir::separator() + "cookies";
131+ if (!d->cookieDir.exists()) {
132+ d->cookieDir.mkpath(".");
133+ }
134+
135+ QObject::connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
136+ this, SLOT(saveAll()));
137 }
138
139 CookieJarManager::~CookieJarManager()
140@@ -83,9 +183,18 @@
141 if (d->cookieJars.contains(id)) {
142 return d->cookieJars[id];
143 } else {
144- CookieJar *cookieJar = new CookieJar(this);
145+ QString fileName = QString::fromLatin1("%1.jar").arg(id);
146+ CookieJar *cookieJar =
147+ new CookieJar(d->cookieDir.absoluteFilePath(fileName), this);
148 d->cookieJars.insert(id, cookieJar);
149 return cookieJar;
150 }
151 }
152
153+void CookieJarManager::saveAll()
154+{
155+ Q_D(CookieJarManager);
156+ foreach (CookieJar *jar, d->cookieJars) {
157+ jar->save();
158+ }
159+}
160
161=== modified file 'src/cookie-jar-manager.h'
162--- src/cookie-jar-manager.h 2012-05-31 08:21:16 +0000
163+++ src/cookie-jar-manager.h 2012-07-26 11:32:19 +0000
164@@ -25,6 +25,7 @@
165 #include <QNetworkCookieJar>
166 #include <QObject>
167 #include <QString>
168+#include <QTimer>
169
170 namespace SignOnUi {
171
172@@ -35,17 +36,26 @@
173 Q_OBJECT
174
175 public:
176- CookieJar(QObject *parent = 0):
177- QNetworkCookieJar(parent) {
178- }
179+ CookieJar(QString cookiePath, QObject *parent = 0);
180 ~CookieJar() {}
181
182 QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
183 bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList,
184 const QUrl &url);
185 void setCookies(const QList<QNetworkCookie> &cookieList) {
186+ queueSave();
187 setAllCookies(cookieList);
188 }
189+
190+public Q_SLOTS:
191+ void save();
192+
193+private:
194+ void queueSave();
195+
196+private:
197+ QString m_cookiePath;
198+ QTimer m_saveTimer;
199 };
200
201 class CookieJarManagerPrivate;
202@@ -61,6 +71,9 @@
203
204 CookieJar *cookieJarForIdentity(uint id);
205
206+public Q_SLOTS:
207+ void saveAll();
208+
209 protected:
210 explicit CookieJarManager(QObject *parent = 0);
211
212
213=== modified file 'src/main.cpp'
214--- src/main.cpp 2012-04-03 11:46:12 +0000
215+++ src/main.cpp 2012-07-26 11:32:19 +0000
216@@ -37,6 +37,7 @@
217 int main(int argc, char **argv)
218 {
219 QApplication app(argc, argv);
220+ app.setApplicationName("signon-ui");
221 app.setQuitOnLastWindowClosed(false);
222
223 /* read environment variables */

Subscribers

People subscribed via source and target branches

to all changes: