Falkon Develop
Cross-platform Qt-based web browser
qmlcookies.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlcookies.h"
19#include "mainapplication.h"
20#include "cookiejar.h"
21#include "qwebengineprofile.h"
22#include "qml/qmlstaticdata.h"
23#include <QQmlEngine>
24
25QmlCookies::QmlCookies(QObject *parent)
26 : QObject(parent)
27{
28 connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [this](const QNetworkCookie &network_cookie){
29 QmlCookie *cookie = QmlStaticData::instance().getCookie(network_cookie);
30 QVariantMap map;
31 map.insert(QSL("cookie"), QVariant::fromValue(cookie));
32 map.insert(QSL("removed"), false);
33 Q_EMIT changed(map);
34 });
35
36 connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [this](const QNetworkCookie &network_cookie){
37 QmlCookie *cookie = QmlStaticData::instance().getCookie(network_cookie);
38 QVariantMap map;
39 map.insert(QSL("cookie"), QVariant::fromValue(cookie));
40 map.insert(QSL("removed"), true);
41 Q_EMIT changed(map);
42 });
43}
44
45QNetworkCookie QmlCookies::getNetworkCookie(const QVariantMap &map)
46{
47 if (!map.contains(QSL("name")) || !map.contains(QSL("url"))) {
48 qWarning() << "Error:" << "Wrong arguments passed to" << __FUNCTION__;
49 return QNetworkCookie();
50 }
51 const QString name = map.value(QSL("name")).toString();
52 const QString url = map.value(QSL("url")).toString();
53 QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
54 for (const QNetworkCookie &cookie : std::as_const(cookies)) {
55 if (QString::fromUtf8(cookie.name()) == name && cookie.domain() == url) {
56 return cookie;
57 }
58 }
59 return QNetworkCookie();
60}
61
62QmlCookie *QmlCookies::get(const QVariantMap &map)
63{
64 QNetworkCookie netCookie = getNetworkCookie(map);
65 return QmlStaticData::instance().getCookie(netCookie);
66}
67
68QList<QObject*> QmlCookies::getAll(const QVariantMap &map)
69{
70 QList<QObject*> qmlCookies;
71 const QString name = map.value(QSL("name")).toString();
72 const QString url = map.value(QSL("url")).toString();
73 const QString path = map.value(QSL("path")).toString();
74 const bool secure = map.value(QSL("secure")).toBool();
75 const bool session = map.value(QSL("session")).toBool();
76 QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
77 for (const QNetworkCookie &cookie : std::as_const(cookies)) {
78 if ((!map.contains(QSL("name")) || QString::fromUtf8(cookie.name()) == name)
79 && (!map.contains(QSL("url")) || cookie.domain() == url)
80 && (!map.contains(QSL("path")) || cookie.path() == path)
81 && (!map.contains(QSL("secure")) || cookie.isSecure() == secure)
82 && (!map.contains(QSL("session")) || cookie.isSessionCookie() == session)) {
83 QmlCookie *qmlCookie = QmlStaticData::instance().getCookie(cookie);
84 qmlCookies.append(qmlCookie);
85 }
86 }
87 return qmlCookies;
88}
89
90void QmlCookies::set(const QVariantMap &map)
91{
92 const QString name = map.value(QSL("name")).toString();
93 const QString url = map.value(QSL("url")).toString();
94 const QString path = map.value(QSL("path")).toString();
95 const bool secure = map.value(QSL("secure")).toBool();
96 const QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map.value(QSL("expirationDate")).toDouble());
97 const bool httpOnly = map.value(QSL("httpOnly")).toBool();
98 const QString value = map.value(QSL("value")).toString();
99 QNetworkCookie cookie;
100 cookie.setName(name.toUtf8());
101 cookie.setDomain(url);
102 cookie.setPath(path);
103 cookie.setSecure(secure);
104 cookie.setExpirationDate(expirationDate);
105 cookie.setHttpOnly(httpOnly);
106 cookie.setValue(value.toUtf8());
107 mApp->webProfile()->cookieStore()->setCookie(cookie);
108}
109
110void QmlCookies::remove(const QVariantMap &map)
111{
112 QNetworkCookie netCookie = getNetworkCookie(map);
113 mApp->webProfile()->cookieStore()->deleteCookie(netCookie);
114}
Q_INVOKABLE void set(const QVariantMap &map)
Set a cookie.
Definition: qmlcookies.cpp:90
QmlCookies(QObject *parent=nullptr)
Definition: qmlcookies.cpp:25
Q_INVOKABLE QList< QObject * > getAll(const QVariantMap &map)
Get all cookies matching a criteria.
Definition: qmlcookies.cpp:68
Q_INVOKABLE void remove(const QVariantMap &map)
Remove a cookie.
Definition: qmlcookies.cpp:110
Q_INVOKABLE QmlCookie * get(const QVariantMap &map)
Get a cookie.
Definition: qmlcookies.cpp:62
QmlCookie * getCookie(const QNetworkCookie &cookie)
static QmlStaticData & instance()
#define mApp
int value(const QColor &c)
Definition: colors.cpp:238
#define QSL(x)
Definition: qzcommon.h:40