From 155dc2d05673b19f94c805accf75b03566609749 Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Thu, 21 Oct 2010 17:39:05 +0000 Subject: [PATCH] use a custom network access manager, based on the kio one, but has a special handling for package:/foo urls svn path=/trunk/KDE/kdebase/runtime/; revision=1188205 --- scriptengines/javascript/CMakeLists.txt | 2 + .../declarative/packageaccessmanager.cpp | 51 +++++++++++++++++++ .../declarative/packageaccessmanager.h | 43 ++++++++++++++++ .../packageaccessmanagerfactory.cpp | 41 +++++++++++++++ .../declarative/packageaccessmanagerfactory.h | 41 +++++++++++++++ .../plasmoid/declarativeappletscript.cpp | 6 +++ 6 files changed, 184 insertions(+) create mode 100644 scriptengines/javascript/declarative/packageaccessmanager.cpp create mode 100644 scriptengines/javascript/declarative/packageaccessmanager.h create mode 100644 scriptengines/javascript/declarative/packageaccessmanagerfactory.cpp create mode 100644 scriptengines/javascript/declarative/packageaccessmanagerfactory.h diff --git a/scriptengines/javascript/CMakeLists.txt b/scriptengines/javascript/CMakeLists.txt index 976fb6cea..ba9c0fb3d 100644 --- a/scriptengines/javascript/CMakeLists.txt +++ b/scriptengines/javascript/CMakeLists.txt @@ -114,6 +114,8 @@ install(FILES data/plasma-javascriptaddon.desktop DESTINATION ${SERVICETYPES_INS set(declarative_appletscript_SRCS common/javascriptaddonpackagestructure.cpp common/declarativescriptenv.cpp + declarative/packageaccessmanager.cpp + declarative/packageaccessmanagerfactory.cpp plasmoid/abstractjsappletscript.cpp plasmoid/appletauthorization.cpp plasmoid/appletinterface.cpp diff --git a/scriptengines/javascript/declarative/packageaccessmanager.cpp b/scriptengines/javascript/declarative/packageaccessmanager.cpp new file mode 100644 index 000000000..302ab1152 --- /dev/null +++ b/scriptengines/javascript/declarative/packageaccessmanager.cpp @@ -0,0 +1,51 @@ +/* + * Copyright 2010 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "packageaccessmanager.h" + +#include + +PackageAccessManager::PackageAccessManager(const Plasma::Package *package, QObject *parent) + : KIO::AccessManager(parent), + m_package(package) +{ +} + +PackageAccessManager::~PackageAccessManager() +{ +} + + +QNetworkReply *PackageAccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData) +{ + QUrl reqUrl(req.url()); + + if (reqUrl.scheme() == "plasmapackage") { + QNetworkRequest request = req; + reqUrl.setScheme("file"); + reqUrl.setPath(m_package->path()+"/contents/"+reqUrl.path()); + request.setUrl(reqUrl); + return KIO::AccessManager::createRequest(op, request, outgoingData); + } else { + return KIO::AccessManager::createRequest(op, req, outgoingData); + } +} + + + diff --git a/scriptengines/javascript/declarative/packageaccessmanager.h b/scriptengines/javascript/declarative/packageaccessmanager.h new file mode 100644 index 000000000..2d9ec0374 --- /dev/null +++ b/scriptengines/javascript/declarative/packageaccessmanager.h @@ -0,0 +1,43 @@ +/* + * Copyright 2010 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef PACKAGEACCESSMANAGER_H +#define PACKAGEACCESSMANAGER_H + +#include + +namespace Plasma +{ + class Package; +} + +class PackageAccessManager : public KIO::AccessManager +{ +public: + PackageAccessManager(const Plasma::Package *package, QObject *parent = 0); + ~PackageAccessManager(); + +protected: + QNetworkReply *createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData = 0); + +private: + const Plasma::Package *m_package; +}; + +#endif diff --git a/scriptengines/javascript/declarative/packageaccessmanagerfactory.cpp b/scriptengines/javascript/declarative/packageaccessmanagerfactory.cpp new file mode 100644 index 000000000..1031c9e1f --- /dev/null +++ b/scriptengines/javascript/declarative/packageaccessmanagerfactory.cpp @@ -0,0 +1,41 @@ +/* + * Copyright 2010 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "packageaccessmanagerfactory.h" + +#include + +#include "packageaccessmanager.h" + +PackageAccessManagerFactory::PackageAccessManagerFactory(const Plasma::Package *package) + : QDeclarativeNetworkAccessManagerFactory(), + m_package(package) +{ +} + +PackageAccessManagerFactory::~PackageAccessManagerFactory() +{ +} + +QNetworkAccessManager *PackageAccessManagerFactory::create(QObject *parent) +{ + return new PackageAccessManager(m_package, parent); +} + + diff --git a/scriptengines/javascript/declarative/packageaccessmanagerfactory.h b/scriptengines/javascript/declarative/packageaccessmanagerfactory.h new file mode 100644 index 000000000..d382c99d3 --- /dev/null +++ b/scriptengines/javascript/declarative/packageaccessmanagerfactory.h @@ -0,0 +1,41 @@ +/* + * Copyright 2010 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef PACKAGEACCESSMANAGERFACTORY_H +#define PACKAGEACCESSMANAGERFACTORY_H + +#include + +namespace Plasma +{ + class Package; +} + +class PackageAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory +{ +public: + PackageAccessManagerFactory(const Plasma::Package *package); + ~PackageAccessManagerFactory(); + QNetworkAccessManager *create(QObject *parent); + +private: + const Plasma::Package *m_package; +}; + +#endif diff --git a/scriptengines/javascript/plasmoid/declarativeappletscript.cpp b/scriptengines/javascript/plasmoid/declarativeappletscript.cpp index 77a1ab903..d63959363 100644 --- a/scriptengines/javascript/plasmoid/declarativeappletscript.cpp +++ b/scriptengines/javascript/plasmoid/declarativeappletscript.cpp @@ -49,6 +49,7 @@ #include "plasmoid/themedsvg.h" #include "common/scriptenv.h" +#include "declarative/packageaccessmanagerfactory.h" #include "simplebindings/bytearrayclass.h" #include "simplebindings/dataenginereceiver.h" #include "simplebindings/i18n.h" @@ -78,8 +79,13 @@ bool DeclarativeAppletScript::init() m_declarativeWidget = new Plasma::DeclarativeWidget(applet()); m_declarativeWidget->setInitializationDelayed(true); + //make possible to import extensions from the package + //FIXME: probably to be removed, would make possible to use native code from within the package :/ m_declarativeWidget->engine()->addImportPath(package()->path()+"/contents/script"); + //use our own custom network access manager that will acces Plasma packages and to manage security (i.e. deny access to remote stuff when the proper extension isn't enabled + m_declarativeWidget->engine()->setNetworkAccessManagerFactory(new PackageAccessManagerFactory(package())); + m_declarativeWidget->setQmlPath(mainScript()); if (!m_declarativeWidget->engine() || !m_declarativeWidget->engine()->rootContext() || !m_declarativeWidget->engine()->rootContext()->isValid() || m_declarativeWidget->mainComponent()->isError()) {