2
* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
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.
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.
30
#include "cookiejar.h"
33
#include <QStandardPaths>
35
#include <QDesktopServices>
38
#include <QTextStream>
40
TestBrowserCookieJar::TestBrowserCookieJar(QObject* parent)
41
: QNetworkCookieJar(parent)
42
, m_storageEnabled(false)
44
// We use a timer for the real disk write to avoid multiple IO
45
// syscalls in sequence (when loading pages which set multiple cookies).
46
m_timer.setInterval(10000);
47
m_timer.setSingleShot(true);
48
connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
50
#ifndef QT_NO_DESKTOPSERVICES
52
QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
54
QString path = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
57
QString path = QDir::homePath() + "/.QtTestBrowser";
61
m_file.setFileName(path + "/cookieJar");
64
TestBrowserCookieJar::~TestBrowserCookieJar()
66
if (m_storageEnabled) {
72
bool TestBrowserCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
74
bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
75
if (status && m_storageEnabled)
80
void TestBrowserCookieJar::setDiskStorageEnabled(bool enabled)
82
m_storageEnabled = enabled;
84
if (enabled && allCookies().isEmpty())
87
// When disabling, save current cookies.
88
if (!enabled && !allCookies().isEmpty())
92
void TestBrowserCookieJar::scheduleSaveToDisk()
94
// We extract the raw cookies here because the user may
95
// enable/disable/clear cookies while the timer is running.
100
void TestBrowserCookieJar::extractRawCookies()
102
QList<QNetworkCookie> cookies = allCookies();
103
m_rawCookies.clear();
105
foreach (const QNetworkCookie &cookie, cookies) {
106
if (!cookie.isSessionCookie())
107
m_rawCookies.append(cookie.toRawForm());
111
void TestBrowserCookieJar::saveToDisk()
115
if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
116
QTextStream out(&m_file);
117
foreach (const QByteArray &cookie, m_rawCookies)
118
out << cookie + "\n";
121
qWarning("IO error handling cookiejar file");
124
void TestBrowserCookieJar::loadFromDisk()
126
if (!m_file.exists())
129
QList<QNetworkCookie> cookies;
131
if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
132
QTextStream in(&m_file);
134
cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
137
qWarning("IO error handling cookiejar file");
139
setAllCookies(cookies);
142
void TestBrowserCookieJar::reset()
144
setAllCookies(QList<QNetworkCookie>());
145
if (m_storageEnabled)
146
scheduleSaveToDisk();