mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 18:56:34 +01:00
Created Cookies API
This commit is contained in:
parent
59d1cc22f5
commit
a729ccf7bf
@ -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
|
||||
|
81
src/lib/plugins/qml/api/cookies/qmlcookie.cpp
Normal file
81
src/lib/plugins/qml/api/cookies/qmlcookie.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "qmlcookie.h"
|
||||
#include <QDebug>
|
||||
|
||||
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());
|
||||
}
|
48
src/lib/plugins/qml/api/cookies/qmlcookie.h
Normal file
48
src/lib/plugins/qml/api/cookies/qmlcookie.h
Normal file
@ -0,0 +1,48 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef QMLCOOKIE_H
|
||||
#define QMLCOOKIE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDateTime>
|
||||
#include <QNetworkCookie>
|
||||
|
||||
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
|
115
src/lib/plugins/qml/api/cookies/qmlcookies.cpp
Normal file
115
src/lib/plugins/qml/api/cookies/qmlcookies.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#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<QNetworkCookie> 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<QObject*> QmlCookies::getAll(const QVariantMap &map)
|
||||
{
|
||||
QList<QObject*> 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<QNetworkCookie> 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);
|
||||
}
|
39
src/lib/plugins/qml/api/cookies/qmlcookies.h
Normal file
39
src/lib/plugins/qml/api/cookies/qmlcookies.h
Normal file
@ -0,0 +1,39 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#ifndef QMLCOOKIES_H
|
||||
#define QMLCOOKIES_H
|
||||
|
||||
#include <QObject>
|
||||
#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<QObject*> 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
|
@ -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 <QQmlEngine>
|
||||
|
||||
@ -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<QmlCookie>("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<QmlCookies>("org.kde.falkon", 1, 0, "Cookies", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||
Q_UNUSED(engine)
|
||||
Q_UNUSED(scriptEngine)
|
||||
|
||||
auto *object = new QmlCookies();
|
||||
return object;
|
||||
});
|
||||
}
|
||||
|
@ -30,6 +30,9 @@ class QmlPlugins
|
||||
|
||||
static void registerQmlHistoryItem();
|
||||
static void registerQmlHistory();
|
||||
|
||||
static void registerQmlCookie();
|
||||
static void registerQmlCookies();
|
||||
public:
|
||||
static void registerQmlTypes();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user