-
Notifications
You must be signed in to change notification settings - Fork 0
/
webkitcookiejar.cpp
75 lines (61 loc) · 1.75 KB
/
webkitcookiejar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "webkitcookiejar.h"
#include <QStandardPaths>
#include <QSettings>
#include <QtSql/QSqlQuery>
#include <QNetworkCookie>
WebKitCookieJar::WebKitCookieJar(QObject *parent) :
QNetworkCookieJar(parent)
, loaded(false)
{
connectionName = "webkit_coonection";
cookieStoragePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation) +
QLatin1String("/.QtWebKit") +
QLatin1String("/cookies.db");
database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), connectionName);
database.setDatabaseName(cookieStoragePath);
}
WebKitCookieJar::~WebKitCookieJar()
{
if (database.isOpen())
database.close();
}
void WebKitCookieJar::load()
{
if (loaded)
return;
if (!database.open()) {
qWarning("Can't open cookie database");
return;
}
QList<QNetworkCookie> cookies;
QSqlQuery sqlQuery(database);
sqlQuery.prepare(QLatin1String("SELECT cookie FROM cookies"));
sqlQuery.exec();
while (sqlQuery.next())
cookies.append(QNetworkCookie::parseCookies(sqlQuery.value(0).toByteArray()));
if (cookies.empty()) {
emit noCookies(QUrl());
return;
}
setAllCookies(cookies);
loaded = true;
}
void WebKitCookieJar::reload()
{
loaded = false;
load();
}
QList<QNetworkCookie> WebKitCookieJar::cookiesForUrl(const QUrl &url) const
{
WebKitCookieJar *that = const_cast<WebKitCookieJar*>(this);
that->load();
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
if (cookies.empty())
emit that->noCookies(url);
return cookies;
}
bool WebKitCookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
load();
return true;
}