d1ccaeaccde6b1c1cd524e5437ebc00e2fa3258c
[situare] / src / network / networkcookiejar.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Henri Lampela - henri.lampela@ixonos.com
6         Sami Rämö - sami.ramo@ixonos.com
7
8     Situare is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License
10     version 2 as published by the Free Software Foundation.
11
12     Situare is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with Situare; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
20     USA.
21 */
22
23 #include <QDebug>
24 #include <QSettings>
25
26 #include "common.h"
27
28 #include "networkcookiejar.h"
29
30 NetworkCookieJar::NetworkCookieJar(QObject *parent)
31     : QNetworkCookieJar(parent)
32 {
33     qWarning() << __PRETTY_FUNCTION__;
34
35     loadCookies();
36 }
37
38 NetworkCookieJar::~NetworkCookieJar()
39 {
40     qWarning() << __PRETTY_FUNCTION__;
41
42     saveCookies();
43 }
44
45 void NetworkCookieJar::loadCookies()
46 {
47     qWarning() << __PRETTY_FUNCTION__;
48
49     QSettings settings(DIRECTORY_NAME, FILE_NAME);
50
51     QStringList list = settings.value(COOKIES, EMPTY).toStringList();
52
53     if(!list.isEmpty()) {
54         QList<QNetworkCookie> cookieList;
55         for(int i = 0; i < list.count(); i++) {
56             cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
57         }
58
59         setAllCookies(cookieList);
60     }
61 }
62
63 void NetworkCookieJar::saveCookies()
64 {
65     qWarning() << __PRETTY_FUNCTION__;
66
67     QList<QNetworkCookie> cookieList = allCookies();
68     QStringList list;
69
70     for(int i = 0; i < cookieList.count(); i++) {
71         QNetworkCookie cookie = cookieList.at(i);
72         QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
73         list.append(QString(byteArray));
74     }
75     list.removeDuplicates();
76
77     QSettings settings(DIRECTORY_NAME, FILE_NAME);
78     settings.setValue(COOKIES, list);
79 }