Cleaned debug prints
[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 const QString COOKIES_SETTING = "cookies";
31
32 NetworkCookieJar::NetworkCookieJar(QObject *parent)
33     : QNetworkCookieJar(parent)
34 {
35     qDebug() << __PRETTY_FUNCTION__;
36
37     loadCookies();
38 }
39
40 NetworkCookieJar::~NetworkCookieJar()
41 {
42     qDebug() << __PRETTY_FUNCTION__;
43
44     saveCookies();
45 }
46
47 void NetworkCookieJar::clearCookiesSetting()
48 {
49     qDebug() << __PRETTY_FUNCTION__;
50
51     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
52     settings.remove(COOKIES_SETTING);
53 }
54
55 void NetworkCookieJar::loadCookies()
56 {
57     qDebug() << __PRETTY_FUNCTION__;
58
59     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
60
61     QStringList list = settings.value(COOKIES_SETTING).toStringList();
62
63     if(!list.isEmpty()) {
64         QList<QNetworkCookie> cookieList;
65         for(int i = 0; i < list.count(); i++) {
66             cookieList.append(QNetworkCookie::parseCookies(list.at(i).toAscii()));
67         }
68
69         setAllCookies(cookieList);
70     }
71 }
72
73 void NetworkCookieJar::saveCookies()
74 {
75     qDebug() << __PRETTY_FUNCTION__;
76
77     QList<QNetworkCookie> cookieList = allCookies();
78     QStringList list;
79
80     for(int i = 0; i < cookieList.count(); i++) {
81         QNetworkCookie cookie = cookieList.at(i);
82         QByteArray byteArray = cookie.toRawForm(QNetworkCookie::Full);
83         list.append(QString(byteArray));
84     }
85     list.removeDuplicates();
86
87     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
88     settings.setValue(COOKIES_SETTING, list);
89 }