diff --git a/src/lib/plugins/qml/api/cookies/qmlcookie.cpp b/src/lib/plugins/qml/api/cookies/qmlcookie.cpp index 608f65a5..33d9fcb3 100644 --- a/src/lib/plugins/qml/api/cookies/qmlcookie.cpp +++ b/src/lib/plugins/qml/api/cookies/qmlcookie.cpp @@ -1,102 +1,103 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * 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 . * ============================================================ */ #include "qmlcookie.h" #include #include QmlCookie::QmlCookie(QNetworkCookie *cookie, QObject *parent) : QObject(parent) , m_cookie(cookie) { QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); } QString QmlCookie::domain() const { if (!m_cookie) { return QString(); } return m_cookie->domain(); } QDateTime QmlCookie::expirationDate() const { if (!m_cookie) { return QDateTime(); } return m_cookie->expirationDate(); } QString QmlCookie::name() const { if (!m_cookie) { return QString(); } return QString(m_cookie->name()); } QString QmlCookie::path() const { if (!m_cookie) { return QString(); } return m_cookie->path(); } bool QmlCookie::secure() const { if (!m_cookie) { return false; } return m_cookie->isSecure(); } bool QmlCookie::session() const { if (!m_cookie) { return false; } return m_cookie->isSessionCookie(); } QString QmlCookie::value() const { if (!m_cookie) { return QString(); } return QString(m_cookie->value()); } QmlCookieData::QmlCookieData() { } QmlCookieData::~QmlCookieData() { qDeleteAll(m_cookies); } QmlCookie *QmlCookieData::get(QNetworkCookie *cookie) { QmlCookie *qmlCookie = m_cookies.value(cookie); if (!qmlCookie) { - qmlCookie = new QmlCookie(cookie); - m_cookies.insert(cookie, qmlCookie); + QNetworkCookie *netCookie = new QNetworkCookie(*cookie); + qmlCookie = new QmlCookie(netCookie); + m_cookies.insert(netCookie, qmlCookie); } return qmlCookie; } diff --git a/src/lib/plugins/qml/api/cookies/qmlcookies.cpp b/src/lib/plugins/qml/api/cookies/qmlcookies.cpp index ec409eba..02095608 100644 --- a/src/lib/plugins/qml/api/cookies/qmlcookies.cpp +++ b/src/lib/plugins/qml/api/cookies/qmlcookies.cpp @@ -1,176 +1,174 @@ /* ============================================================ * Falkon - Qt web browser * Copyright (C) 2018 Anmol Gautam * * 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 . * ============================================================ */ #include "qmlcookies.h" #include "mainapplication.h" #include "cookiejar.h" #include "qwebengineprofile.h" Q_GLOBAL_STATIC(QmlCookieData, cookieData) QmlCookies::QmlCookies(QObject *parent) : QObject(parent) { connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [this](QNetworkCookie network_cookie){ // FIXME: improve this - QmlCookie *cookie = cookieData->get(new QNetworkCookie(network_cookie)); + QmlCookie *cookie = cookieData->get(&network_cookie); QVariantMap map; map.insert(QSL("cookie"), QVariant::fromValue(cookie)); map.insert(QSL("removed"), false); emit changed(map); }); connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [this](QNetworkCookie network_cookie){ // FIXME: improve this - QmlCookie *cookie = cookieData->get(new QNetworkCookie(network_cookie)); + QmlCookie *cookie = cookieData->get(&network_cookie); QVariantMap map; map.insert(QSL("cookie"), QVariant::fromValue(cookie)); map.insert(QSL("removed"), true); emit changed(map); }); } QNetworkCookie *QmlCookies::getNetworkCookie(const QVariantMap &map) { if (!map.contains(QSL("name")) || !map.contains(QSL("url"))) { qWarning() << "Error:" << "Wrong arguments passed to" << __FUNCTION__; return nullptr; } const QString name = map.value(QSL("name")).toString(); const QString url = map.value(QSL("url")).toString(); QVector cookies = mApp->cookieJar()->getAllCookies(); - for (const QNetworkCookie &cookie : cookies) { + for (const QNetworkCookie &cookie : qAsConst(cookies)) { if (cookie.name() == name && cookie.domain() == url) { - QNetworkCookie *netCookie = new QNetworkCookie(cookie); - return netCookie; + return new QNetworkCookie(cookie); } } return nullptr; } /** * @brief Get a cookie * @param A JavaScript object containing * - name: * String representing the name of the cookie * - url: * String representing the url of the cookie * @return Cookie of type [QmlCookie](@ref QmlCookie) * if such cookie exists, else null */ QmlCookie *QmlCookies::get(const QVariantMap &map) { QNetworkCookie *netCookie = getNetworkCookie(map); if (!netCookie) { return nullptr; } return cookieData->get(netCookie); } /** * @brief Get all cookies matching a criteria * @param A JavaScript object containing * - name: * String representing the name of the cookie * - url: * String representing the url of the cookie * - path: * String representing the path of the cookie * - secure: * Bool representing if the cookie is secure * - session: * Bool representing if the cookie is a session cookie * @return List containing cookies, each of type [QmlCookie](@ref QmlCookie) */ QList QmlCookies::getAll(const QVariantMap &map) { QList qmlCookies; const QString name = map.value(QSL("name")).toString(); const QString url = map.value(QSL("url")).toString(); const QString path = map.value(QSL("path")).toString(); const bool secure = map.value(QSL("secure")).toBool(); const bool session = map.value(QSL("session")).toBool(); QVector cookies = mApp->cookieJar()->getAllCookies(); - for (QNetworkCookie cookie : cookies) { + for (QNetworkCookie cookie : qAsConst(cookies)) { if ((!map.contains(QSL("name")) || cookie.name() == name) && (!map.contains(QSL("url")) || cookie.domain() == url) && (!map.contains(QSL("path")) || cookie.path() == path) && (!map.contains(QSL("secure")) || cookie.isSecure() == secure) && (!map.contains(QSL("session")) || cookie.isSessionCookie() == session)) { - QNetworkCookie *netCookie = new QNetworkCookie(cookie); - QmlCookie *qmlCookie = cookieData->get(netCookie); + QmlCookie *qmlCookie = cookieData->get(&cookie); qmlCookies.append(qmlCookie); } } return qmlCookies; } /** * @brief Set a cookie * @param A JavaScript object containing * - name: * String representing the name of the cookie * - url: * String representing the name of the cookie * - path: * String representing the path of the cookie * - secure: * Bool representing if the cookie is secure * - expirationDate: * A JavaScript Date object, representing the expiration date of the cookie * - httpOnly: * Bool representing if the cookie is httpOnly * - value: * String representing the value of the cookie */ void QmlCookies::set(const QVariantMap &map) { const QString name = map.value(QSL("name")).toString(); const QString url = map.value(QSL("url")).toString(); const QString path = map.value(QSL("path")).toString(); const bool secure = map.value(QSL("secure")).toBool(); const QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map.value(QSL("expirationDate")).toDouble()); const bool httpOnly = map.value(QSL("httpOnly")).toBool(); const QString value = map.value(QSL("value")).toString(); QNetworkCookie cookie; cookie.setName(name.toUtf8()); cookie.setDomain(url); cookie.setPath(path); cookie.setSecure(secure); cookie.setExpirationDate(expirationDate); cookie.setHttpOnly(httpOnly); cookie.setValue(value.toUtf8()); mApp->webProfile()->cookieStore()->setCookie(cookie); } /** * @brief Remove a cookie * @param A JavaScript object containing * - name: * String representing the name of the cookie * - url: * String representing the url of the cookie */ void QmlCookies::remove(const QVariantMap &map) { QNetworkCookie *netCookie = getNetworkCookie(map); if (!netCookie) { qWarning() << "Error:" << "Cannot find cookie"; return; } mApp->webProfile()->cookieStore()->deleteCookie(*netCookie); }