tizen beta release
[profile/ivi/webkit-efl.git] / Tools / QtTestBrowser / cookiejar.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "cookiejar.h"
29
30 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
31 #include <QStandardPaths>
32 #else
33 #include <QDesktopServices>
34 #endif
35 #include <QDir>
36 #include <QTextStream>
37
38 TestBrowserCookieJar::TestBrowserCookieJar(QObject* parent)
39     : QNetworkCookieJar(parent)
40     , m_storageEnabled(false)
41 {
42     // We use a timer for the real disk write to avoid multiple IO
43     // syscalls in sequence (when loading pages which set multiple cookies).
44     m_timer.setInterval(10000);
45     m_timer.setSingleShot(true);
46     connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
47
48 #ifndef QT_NO_DESKTOPSERVICES
49 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
50     QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
51 #else
52     QString path = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
53 #endif
54 #else
55     QString path = QDir::homePath() + "/.QtTestBrowser";
56 #endif
57
58     QDir().mkpath(path);
59     m_file.setFileName(path + "/cookieJar");
60 }
61
62 TestBrowserCookieJar::~TestBrowserCookieJar()
63 {
64     if (m_storageEnabled) {
65         extractRawCookies();
66         saveToDisk();
67     }
68 }
69
70 bool TestBrowserCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
71 {
72     bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
73     if (status && m_storageEnabled)
74         scheduleSaveToDisk();
75     return status;
76 }
77
78 void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
79 {
80     m_storageEnabled = enabled;
81
82     if (enabled && allCookies().isEmpty())
83         loadFromDisk();
84
85     // When disabling, save current cookies.
86     if (!enabled && !allCookies().isEmpty())
87         scheduleSaveToDisk();
88 }
89
90 void TestBrowserCookieJar::scheduleSaveToDisk()
91 {
92     // We extract the raw cookies here because the user may
93     // enable/disable/clear cookies while the timer is running.
94     extractRawCookies();
95     m_timer.start();
96 }
97
98 void TestBrowserCookieJar::extractRawCookies()
99 {
100     QList<QNetworkCookie> cookies = allCookies();
101     m_rawCookies.clear();
102
103     foreach (const QNetworkCookie &cookie, cookies) {
104         if (!cookie.isSessionCookie())
105             m_rawCookies.append(cookie.toRawForm());
106     }
107 }
108
109 void TestBrowserCookieJar::saveToDisk()
110 {
111     m_timer.stop();
112
113     if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
114         QTextStream out(&m_file);
115         foreach (const QByteArray &cookie, m_rawCookies)
116             out << cookie + "\n";
117         m_file.close();
118     } else
119         qWarning("IO error handling cookiejar file");
120 }
121
122 void TestBrowserCookieJar::loadFromDisk()
123 {
124     if (!m_file.exists())
125         return;
126
127     QList<QNetworkCookie> cookies;
128
129     if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
130         QTextStream in(&m_file);
131         while (!in.atEnd())
132             cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
133         m_file.close();
134     } else
135         qWarning("IO error handling cookiejar file");
136
137     setAllCookies(cookies);
138 }
139
140 void TestBrowserCookieJar::reset()
141 {
142     setAllCookies(QList<QNetworkCookie>());
143     if (m_storageEnabled)
144         scheduleSaveToDisk();
145 }