From a729ccf7bfde4888504b74cb9c63a7522a66afdf Mon Sep 17 00:00:00 2001 From: Anmol Gautam Date: Thu, 24 May 2018 22:50:27 +0530 Subject: [PATCH] Created Cookies API --- src/lib/CMakeLists.txt | 2 + src/lib/plugins/qml/api/cookies/qmlcookie.cpp | 81 ++++++++++++ src/lib/plugins/qml/api/cookies/qmlcookie.h | 48 ++++++++ .../plugins/qml/api/cookies/qmlcookies.cpp | 115 ++++++++++++++++++ src/lib/plugins/qml/api/cookies/qmlcookies.h | 39 ++++++ src/lib/plugins/qml/qmlplugins.cpp | 29 +++++ src/lib/plugins/qml/qmlplugins.h | 3 + 7 files changed, 317 insertions(+) create mode 100644 src/lib/plugins/qml/api/cookies/qmlcookie.cpp create mode 100644 src/lib/plugins/qml/api/cookies/qmlcookie.h create mode 100644 src/lib/plugins/qml/api/cookies/qmlcookies.cpp create mode 100644 src/lib/plugins/qml/api/cookies/qmlcookies.h diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index ba3208b4c..8ce353710 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -163,6 +163,8 @@ set(SRCS ${SRCS} plugins/qml/api/topsites/qmltopsites.cpp plugins/qml/api/history/qmlhistoryitem.cpp plugins/qml/api/history/qmlhistory.cpp + plugins/qml/api/cookies/qmlcookie.cpp + plugins/qml/api/cookies/qmlcookies.cpp popupwindow/popuplocationbar.cpp popupwindow/popupstatusbarmessage.cpp popupwindow/popupwebview.cpp diff --git a/src/lib/plugins/qml/api/cookies/qmlcookie.cpp b/src/lib/plugins/qml/api/cookies/qmlcookie.cpp new file mode 100644 index 000000000..09ef70e24 --- /dev/null +++ b/src/lib/plugins/qml/api/cookies/qmlcookie.cpp @@ -0,0 +1,81 @@ +/* ============================================================ +* 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 + +QmlCookie::QmlCookie(QNetworkCookie *cookie, QObject *parent) : + QObject(parent) + , m_cookie(cookie) +{ +} + +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()); +} diff --git a/src/lib/plugins/qml/api/cookies/qmlcookie.h b/src/lib/plugins/qml/api/cookies/qmlcookie.h new file mode 100644 index 000000000..92ab77dd1 --- /dev/null +++ b/src/lib/plugins/qml/api/cookies/qmlcookie.h @@ -0,0 +1,48 @@ +/* ============================================================ +* 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 . +* ============================================================ */ +#ifndef QMLCOOKIE_H +#define QMLCOOKIE_H + +#include +#include +#include + +class QmlCookie : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString domain READ domain CONSTANT) + Q_PROPERTY(QDateTime expirationDate READ expirationDate CONSTANT) + Q_PROPERTY(QString name READ name CONSTANT) + Q_PROPERTY(QString path READ path CONSTANT) + Q_PROPERTY(bool secure READ secure CONSTANT) + Q_PROPERTY(bool session READ session CONSTANT) + Q_PROPERTY(QString value READ value CONSTANT) +public: + explicit QmlCookie(QNetworkCookie *cookie = 0, QObject *parent = 0); + QString domain() const; + QDateTime expirationDate() const; + QString name() const; + QString path() const; + bool secure() const; + bool session() const; + QString value() const; +private: + QNetworkCookie *m_cookie; +}; + +#endif // QMLCOOKIE_H diff --git a/src/lib/plugins/qml/api/cookies/qmlcookies.cpp b/src/lib/plugins/qml/api/cookies/qmlcookies.cpp new file mode 100644 index 000000000..342627604 --- /dev/null +++ b/src/lib/plugins/qml/api/cookies/qmlcookies.cpp @@ -0,0 +1,115 @@ +/* ============================================================ +* 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" + +QmlCookies::QmlCookies(QObject *parent) : + QObject(parent) +{ + connect(mApp->cookieJar(), &CookieJar::cookieAdded, this, [=](QNetworkCookie network_cookie){ + QmlCookie *cookie = new QmlCookie(&network_cookie); + emit changed(cookie, false); + }); + + connect(mApp->cookieJar(), &CookieJar::cookieRemoved, this, [=](QNetworkCookie network_cookie){ + QmlCookie *cookie = new QmlCookie(&network_cookie); + emit changed(cookie, true); + }); +} + +QNetworkCookie *QmlCookies::getNetworkCookie(const QVariantMap &map) +{ + if (!map["name"].isValid() || !map["url"].isValid()) { + qWarning() << "Error:" << "Wrong arguments passed to" << __FUNCTION__; + return nullptr; + } + QString name = map["name"].toString(); + QString url = map["url"].toString(); + QVector cookies = mApp->cookieJar()->getAllCookies(); + for (QNetworkCookie cookie : cookies) { + if (cookie.name() == name && cookie.domain() == url) { + QNetworkCookie *netCookie = new QNetworkCookie(cookie); + return netCookie; + } + } + return nullptr; +} + +QmlCookie *QmlCookies::get(const QVariantMap &map) +{ + QNetworkCookie *netCookie = getNetworkCookie(map); + if (!netCookie) { + return nullptr; + } + return new QmlCookie(netCookie); +} + +QList QmlCookies::getAll(const QVariantMap &map) +{ + QList qmlCookies; + QString name = map["name"].toString(); + QString url = map["url"].toString(); + QString path = map["path"].toString(); + bool secure = map["secure"].toBool(); + bool session = map["session"].toBool(); + QVector cookies = mApp->cookieJar()->getAllCookies(); + for (QNetworkCookie cookie : cookies) { + if ((!map["name"].isValid() || cookie.name() == name) + && (!map["url"].isValid() || cookie.domain() == url) + && (!map["path"].isValid() || cookie.path() == path) + && (!map["secure"].isValid() || cookie.isSecure() == secure) + && (!map["session"].isValid() || cookie.isSessionCookie() == session)) { + QNetworkCookie *netCookie = new QNetworkCookie(cookie); + QmlCookie *qmlCookie = new QmlCookie(netCookie); + qmlCookies.append(qmlCookie); + } + } + return qmlCookies; +} + +void QmlCookies::set(const QVariantMap &map) +{ + QString name = map["name"].toString(); + QString url = map["url"].toString(); + QString path = map["path"].toString(); + bool secure = map["secure"].toBool(); + QDateTime expirationDate = QDateTime::fromMSecsSinceEpoch(map["expirationDate"].toDouble()); + bool httpOnly = map["httpOnly"].toBool(); + QString value = map["value"].toString(); + QNetworkCookie cookie; + cookie.setName(name.toLocal8Bit()); + cookie.setDomain(url); + cookie.setPath(path); + cookie.setSecure(secure); + cookie.setExpirationDate(expirationDate); + cookie.setHttpOnly(httpOnly); + cookie.setValue(value.toLocal8Bit()); + mApp->webProfile()->cookieStore()->setCookie(cookie); +} + +void QmlCookies::remove(const QVariantMap &map) +{ + QNetworkCookie *netCookie = getNetworkCookie(map); + if (!netCookie) { + qWarning() << "Error:" << "Cannot find cookie"; + return; + } + mApp->webProfile()->cookieStore()->deleteCookie(*netCookie); +} diff --git a/src/lib/plugins/qml/api/cookies/qmlcookies.h b/src/lib/plugins/qml/api/cookies/qmlcookies.h new file mode 100644 index 000000000..555802ad2 --- /dev/null +++ b/src/lib/plugins/qml/api/cookies/qmlcookies.h @@ -0,0 +1,39 @@ +/* ============================================================ +* 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 . +* ============================================================ */ +#ifndef QMLCOOKIES_H +#define QMLCOOKIES_H + +#include +#include "qmlcookie.h" + +class QmlCookies : public QObject +{ + Q_OBJECT +public: + explicit QmlCookies(QObject *parent = 0); + Q_INVOKABLE QmlCookie *get(const QVariantMap &map); + Q_INVOKABLE QList getAll(const QVariantMap &map); + Q_INVOKABLE void set(const QVariantMap &map); + Q_INVOKABLE void remove(const QVariantMap &map); +signals: + void changed(QmlCookie *cookie, bool removed); +private: + QNetworkCookie *getNetworkCookie(const QVariantMap &map); +}; + +#endif // QMLCOOKIES_H diff --git a/src/lib/plugins/qml/qmlplugins.cpp b/src/lib/plugins/qml/qmlplugins.cpp index 255afc758..3157bd9a3 100644 --- a/src/lib/plugins/qml/qmlplugins.cpp +++ b/src/lib/plugins/qml/qmlplugins.cpp @@ -23,6 +23,8 @@ #include "api/topsites/qmltopsites.h" #include "api/history/qmlhistoryitem.h" #include "api/history/qmlhistory.h" +#include "api/cookies/qmlcookie.h" +#include "api/cookies/qmlcookies.h" #include @@ -39,6 +41,9 @@ void QmlPlugins::registerQmlTypes() registerQmlHistoryItem(); registerQmlHistory(); + + registerQmlCookie(); + registerQmlCookies(); } // private static @@ -118,3 +123,27 @@ void QmlPlugins::registerQmlHistory() return object; }); } + +// private static +void QmlPlugins::registerQmlCookie() +{ + qmlRegisterSingletonType("org.kde.falkon", 1, 0, "Cookie", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + auto *object = new QmlCookie(); + return object; + }); +} + +// private static +void QmlPlugins::registerQmlCookies() +{ + qmlRegisterSingletonType("org.kde.falkon", 1, 0, "Cookies", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + auto *object = new QmlCookies(); + return object; + }); +} diff --git a/src/lib/plugins/qml/qmlplugins.h b/src/lib/plugins/qml/qmlplugins.h index f8c3b8f04..a815ecbe2 100644 --- a/src/lib/plugins/qml/qmlplugins.h +++ b/src/lib/plugins/qml/qmlplugins.h @@ -30,6 +30,9 @@ class QmlPlugins static void registerQmlHistoryItem(); static void registerQmlHistory(); + + static void registerQmlCookie(); + static void registerQmlCookies(); public: static void registerQmlTypes(); };