2010-01-07 03:04:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007-2008 Richard J. Moore <rich@kde.org>
|
|
|
|
* Copyright 2009 Aaron J. Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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 SCRIPTENV_H
|
|
|
|
#define SCRIPTENV_H
|
|
|
|
|
|
|
|
#include <QScriptEngine>
|
|
|
|
#include <QSet>
|
|
|
|
|
|
|
|
#include <KPluginInfo>
|
|
|
|
|
|
|
|
#include "authorization.h"
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
class ScriptEnv : public QObject
|
2010-01-07 03:04:59 +01:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
enum AllowedUrl { NoUrls = 0,
|
|
|
|
HttpUrls = 1,
|
|
|
|
NetworkUrls = 2,
|
2011-04-09 20:56:02 +02:00
|
|
|
LocalUrls = 4,
|
|
|
|
AppLaunching = 8};
|
2010-01-07 03:04:59 +01:00
|
|
|
Q_DECLARE_FLAGS(AllowedUrls, AllowedUrl)
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
ScriptEnv(QObject *parent, QScriptEngine *engine);
|
2010-01-07 03:04:59 +01:00
|
|
|
~ScriptEnv();
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
/**
|
|
|
|
* Adds common properties (e.g. registerEventFilter)
|
|
|
|
* for a 'main' object (e.g. plasmoid, engine, runner)
|
|
|
|
*/
|
|
|
|
void addMainObjectProperties(QScriptValue &value);
|
|
|
|
|
2010-02-22 12:33:21 +01:00
|
|
|
/** Returns the QScriptEngine in use. */
|
|
|
|
QScriptEngine *engine() const;
|
|
|
|
|
|
|
|
/** Returns the ScriptEnv in use for a given QScriptEngine or 0. */
|
2010-03-09 23:18:45 +01:00
|
|
|
static ScriptEnv *findScriptEnv(QScriptEngine *engine);
|
2010-02-22 12:33:21 +01:00
|
|
|
|
2010-03-25 00:31:08 +01:00
|
|
|
static void registerEnums(QScriptValue &scriptValue, const QMetaObject &meta);
|
2010-01-07 03:04:59 +01:00
|
|
|
bool include(const QString &path);
|
|
|
|
|
|
|
|
bool importExtensions(const KPluginInfo &info, QScriptValue &obj, Authorization &authorizer);
|
|
|
|
QSet<QString> loadedExtensions() const;
|
|
|
|
|
2010-03-09 23:18:45 +01:00
|
|
|
bool checkForErrors(bool fatal);
|
|
|
|
|
2010-08-05 21:05:37 +02:00
|
|
|
bool addEventListener(const QString &event, const QScriptValue &func);
|
|
|
|
bool removeEventListener(const QString &event, const QScriptValue &func);
|
2010-08-04 05:53:41 +02:00
|
|
|
|
2010-08-05 23:13:13 +02:00
|
|
|
QScriptValue callFunction(QScriptValue &func, const QScriptValueList &args = QScriptValueList(), const QScriptValue &activator = QScriptValue());
|
2010-08-04 05:53:41 +02:00
|
|
|
bool callEventListeners(const QString &event, const QScriptValueList &args = QScriptValueList());
|
|
|
|
bool hasEventListeners(const QString &event) const;
|
|
|
|
|
2010-09-05 00:45:55 +02:00
|
|
|
QString filePathFromScriptContext(const char *type, const QString &file) const;
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
Q_SIGNALS:
|
|
|
|
void reportError(ScriptEnv *engine, bool fatal);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void registerGetUrl(QScriptValue &obj);
|
2011-04-09 20:56:02 +02:00
|
|
|
void registerOpenUrl(QScriptValue &obj);
|
2010-01-07 03:04:59 +01:00
|
|
|
bool importBuiltinExtension(const QString &extension, QScriptValue &obj);
|
|
|
|
|
|
|
|
static QScriptValue debug(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue print(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue runApplication(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue runCommand(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue openUrl(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue getUrl(QScriptContext *context, QScriptEngine *engine);
|
2010-08-04 05:53:41 +02:00
|
|
|
static QScriptValue listAddons(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue loadAddon(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue registerAddon(QScriptContext *context, QScriptEngine *engine);
|
2010-08-05 21:05:37 +02:00
|
|
|
static QScriptValue addEventListener(QScriptContext *context, QScriptEngine *engine);
|
|
|
|
static QScriptValue removeEventListener(QScriptContext *context, QScriptEngine *engine);
|
2010-09-03 23:31:37 +02:00
|
|
|
static QScriptValue hasExtension(QScriptContext *context, QScriptEngine *engine);
|
2010-09-04 00:36:27 +02:00
|
|
|
static QScriptValue throwNonFatalError(const QString &msg, QScriptContext *context, QScriptEngine *engine);
|
2010-05-08 01:37:25 +02:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void signalException();
|
|
|
|
|
|
|
|
private:
|
2010-08-05 21:05:37 +02:00
|
|
|
/** Call to set common properties on the global object **/
|
|
|
|
void setupGlobalObject();
|
|
|
|
|
2010-01-07 03:04:59 +01:00
|
|
|
QSet<QString> m_extensions;
|
|
|
|
AllowedUrls m_allowedUrls;
|
2010-02-22 12:33:21 +01:00
|
|
|
QScriptEngine *m_engine;
|
2010-08-04 05:53:41 +02:00
|
|
|
QHash<QString, QScriptValueList> m_eventListeners;
|
2010-01-07 03:04:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(ScriptEnv::AllowedUrls)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|