2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "cookiejar.h"
|
|
|
|
#include "qupzilla.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-11-05 15:04:29 +01:00
|
|
|
//#define COOKIE_DEBUG
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
//TODO: black/white listing
|
2011-11-05 15:04:29 +01:00
|
|
|
CookieJar::CookieJar(QupZilla* mainClass, QObject* parent)
|
|
|
|
: QNetworkCookieJar(parent)
|
|
|
|
, p_QupZilla(mainClass)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
m_activeProfil = mApp->getActiveProfilPath();
|
2011-11-05 15:04:29 +01:00
|
|
|
loadSettings();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-03-02 16:57:41 +01:00
|
|
|
settings.beginGroup("Web-Browser-Settings");
|
2011-11-05 15:04:29 +01:00
|
|
|
m_allowCookies = settings.value("allowCookies", true).toBool();
|
|
|
|
m_allowCookiesFromDomain = settings.value("allowCookiesFromVisitedDomainOnly", false).toBool();
|
|
|
|
m_filterTrackingCookie = settings.value("filterTrackingCookie", false).toBool();
|
2011-03-02 16:57:41 +01:00
|
|
|
m_deleteOnClose = settings.value("deleteCookiesOnClose", false).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::setAllowCookies(bool allow)
|
|
|
|
{
|
|
|
|
m_allowCookies = allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_allowCookies) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return QNetworkCookieJar::setCookiesFromUrl(QList<QNetworkCookie>(), url);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
QList<QNetworkCookie> newList = cookieList;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(QNetworkCookie cok, newList) {
|
2011-11-05 15:04:29 +01:00
|
|
|
if (m_allowCookiesFromDomain && !QString("." + url.host()).contains(cok.domain().remove("www."))) {
|
2011-03-02 16:57:41 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "purged for domain mismatch" << cok;
|
|
|
|
#endif
|
|
|
|
newList.removeOne(cok);
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-05 15:04:29 +01:00
|
|
|
|
2011-11-06 12:05:16 +01:00
|
|
|
if (m_filterTrackingCookie && cok.name().startsWith("__utm")) {
|
2011-03-02 16:57:41 +01:00
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "purged as tracking " << cok;
|
|
|
|
#endif
|
|
|
|
newList.removeOne(cok);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QNetworkCookieJar::setCookiesFromUrl(newList, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::saveCookies()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_deleteOnClose) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2012-01-04 14:09:27 +01:00
|
|
|
QList<QNetworkCookie> allCookies;
|
|
|
|
if (m_tempList.isEmpty()) {
|
|
|
|
allCookies = getAllCookies();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
allCookies = m_tempList;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QFile file(m_activeProfil + "cookies.dat");
|
2011-03-02 16:57:41 +01:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count = allCookies.count();
|
|
|
|
|
|
|
|
stream << count;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-02 16:57:41 +01:00
|
|
|
stream << allCookies.at(i).toRawForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::restoreCookies()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!QFile::exists(m_activeProfil + "cookies.dat")) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
QList<QNetworkCookie> restoredCookies;
|
2011-11-06 17:01:23 +01:00
|
|
|
QFile file(m_activeProfil + "cookies.dat");
|
2011-03-02 16:57:41 +01:00
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count;
|
|
|
|
|
|
|
|
stream >> count;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QByteArray rawForm;
|
|
|
|
stream >> rawForm;
|
|
|
|
QNetworkCookie cok = QNetworkCookie::parseCookies(rawForm).at(0);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (cok.expirationDate() < now) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
if (cok.isSessionCookie()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
restoredCookies.append(cok);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
setAllCookies(restoredCookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QNetworkCookie> CookieJar::getAllCookies()
|
|
|
|
{
|
|
|
|
return QNetworkCookieJar::allCookies();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::setAllCookies(const QList<QNetworkCookie> &cookieList)
|
|
|
|
{
|
|
|
|
QNetworkCookieJar::setAllCookies(cookieList);
|
|
|
|
}
|
2011-05-20 17:52:32 +02:00
|
|
|
|
|
|
|
void CookieJar::turnPrivateJar(bool state)
|
|
|
|
{
|
|
|
|
if (state) {
|
|
|
|
m_tempList = QNetworkCookieJar::allCookies();
|
|
|
|
QNetworkCookieJar::setAllCookies(QList<QNetworkCookie>());
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-05-20 17:52:32 +02:00
|
|
|
QNetworkCookieJar::setAllCookies(m_tempList);
|
|
|
|
m_tempList.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|