File indexing completed on 2024-12-22 04:41:13

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlcookies.h"
0019 #include "mainapplication.h"
0020 #include "cookiejar.h"
0021 #include "qwebengineprofile.h"
0022 #include "qml/qmlstaticdata.h"
0023 #include <QQmlEngine>
0024 
0025 QmlCookies::QmlCookies(QObject *parent)
0026     : QObject(parent)
0027 {
0028     connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [this](const QNetworkCookie &network_cookie){
0029         QmlCookie *cookie = QmlStaticData::instance().getCookie(network_cookie);
0030         QVariantMap map;
0031         map.insert(QSL("cookie"), QVariant::fromValue(cookie));
0032         map.insert(QSL("removed"), false);
0033         Q_EMIT changed(map);
0034     });
0035 
0036     connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [this](const QNetworkCookie &network_cookie){
0037         QmlCookie *cookie = QmlStaticData::instance().getCookie(network_cookie);
0038         QVariantMap map;
0039         map.insert(QSL("cookie"), QVariant::fromValue(cookie));
0040         map.insert(QSL("removed"), true);
0041         Q_EMIT changed(map);
0042     });
0043 }
0044 
0045 QNetworkCookie QmlCookies::getNetworkCookie(const QVariantMap &map)
0046 {
0047     if (!map.contains(QSL("name")) || !map.contains(QSL("url"))) {
0048         qWarning() << "Error:" << "Wrong arguments passed to" << __FUNCTION__;
0049         return QNetworkCookie();
0050     }
0051     const QString name = map.value(QSL("name")).toString();
0052     const QString url = map.value(QSL("url")).toString();
0053     QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
0054     for (const QNetworkCookie &cookie : std::as_const(cookies)) {
0055         if (QString::fromUtf8(cookie.name()) == name && cookie.domain() == url) {
0056             return cookie;
0057         }
0058     }
0059     return QNetworkCookie();
0060 }
0061 
0062 QmlCookie *QmlCookies::get(const QVariantMap &map)
0063 {
0064     QNetworkCookie netCookie = getNetworkCookie(map);
0065     return QmlStaticData::instance().getCookie(netCookie);
0066 }
0067 
0068 QList<QObject*> QmlCookies::getAll(const QVariantMap &map)
0069 {
0070     QList<QObject*> qmlCookies;
0071     const QString name = map.value(QSL("name")).toString();
0072     const QString url = map.value(QSL("url")).toString();
0073     const QString path = map.value(QSL("path")).toString();
0074     const bool secure = map.value(QSL("secure")).toBool();
0075     const bool session = map.value(QSL("session")).toBool();
0076     QVector<QNetworkCookie> cookies = mApp->cookieJar()->getAllCookies();
0077     for (QNetworkCookie cookie : std::as_const(cookies)) {
0078         if ((!map.contains(QSL("name")) || QString::fromUtf8(cookie.name()) == name)
0079                 && (!map.contains(QSL("url")) || cookie.domain() == url)
0080                 && (!map.contains(QSL("path")) || cookie.path() == path)
0081                 && (!map.contains(QSL("secure")) || cookie.isSecure() == secure)
0082                 && (!map.contains(QSL("session")) || cookie.isSessionCookie() == session)) {
0083             QmlCookie *qmlCookie = QmlStaticData::instance().getCookie(cookie);
0084             qmlCookies.append(qmlCookie);
0085         }
0086     }
0087     return qmlCookies;
0088 }
0089 
0090 void QmlCookies::set(const QVariantMap &map)
0091 {
0092     const QString name = map.value(QSL("name")).toString();
0093     const QString url = map.value(QSL("url")).toString();
0094     const QString path = map.value(QSL("path")).toString();
0095     const bool secure = map.value(QSL("secure")).toBool();
0096     const QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map.value(QSL("expirationDate")).toDouble());
0097     const bool httpOnly = map.value(QSL("httpOnly")).toBool();
0098     const QString value = map.value(QSL("value")).toString();
0099     QNetworkCookie cookie;
0100     cookie.setName(name.toUtf8());
0101     cookie.setDomain(url);
0102     cookie.setPath(path);
0103     cookie.setSecure(secure);
0104     cookie.setExpirationDate(expirationDate);
0105     cookie.setHttpOnly(httpOnly);
0106     cookie.setValue(value.toUtf8());
0107     mApp->webProfile()->cookieStore()->setCookie(cookie);
0108 }
0109 
0110 void QmlCookies::remove(const QVariantMap &map)
0111 {
0112     QNetworkCookie netCookie = getNetworkCookie(map);
0113     mApp->webProfile()->cookieStore()->deleteCookie(netCookie);
0114 }