1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtScript module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file. Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights. These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
42
#include "qscriptextensionplugin.h"
44
#include "qscriptvalue.h"
45
#include "qscriptengine.h"
51
\class QScriptExtensionPlugin
52
\brief The QScriptExtensionPlugin class provides an abstract base for custom QScript extension plugins.
56
QScriptExtensionPlugin is a plugin interface that makes it
57
possible to offer extensions that can be loaded dynamically into
58
applications using the QScriptEngine class.
60
Writing a script extension plugin is achieved by subclassing this
61
base class, reimplementing the pure virtual keys() and initialize()
62
functions, and exporting the class using the Q_EXPORT_PLUGIN2()
63
macro. See \l {How to Create Qt Plugins} for details.
65
\sa QScriptEngine::importExtension(), {Creating QtScript Extensions}
69
\fn QStringList QScriptExtensionPlugin::keys() const
71
Returns the list of keys this plugin supports.
73
These keys are usually the names of the "modules" or "packages"
74
that are implemented in the plugin (e.g. \c{com.mycompany.MyProduct}).
80
\fn void QScriptExtensionPlugin::initialize(const QString& key, QScriptEngine *engine)
82
Initializes the extension specified by \a key in the given \a engine.
83
The key must come from the set of keys().
89
Constructs a script extension plugin with the given \a parent.
91
Note that this constructor is invoked automatically by the
92
Q_EXPORT_PLUGIN2() macro, so there is no need for calling it
95
QScriptExtensionPlugin::QScriptExtensionPlugin(QObject *parent)
101
Destroys the script extension plugin.
103
Note that Qt destroys a plugin automatically when it is no longer
104
used, so there is no need for calling the destructor explicitly.
106
QScriptExtensionPlugin::~QScriptExtensionPlugin()
112
This function is provided for convenience when reimplementing
113
initialize(). It splits the given \a key on \c{'.'} (dot), and
114
ensures that there's a corresponding path of objects in the
115
environment of the given \a engine, creating new objects to complete
116
the path if necessary. E.g. if the key is "com.trolltech", after
117
the call to setupPackage() the script expression \c{com.trolltech}
118
will evaluate to an object. More specifically, the engine's Global
119
Object will have a property called "com", which in turn has a
120
property called "trolltech".
122
Use this function to avoid global namespace pollution when installing
123
your extensions in the engine.
127
QScriptValue QScriptExtensionPlugin::setupPackage(
128
const QString &key, QScriptEngine *engine) const
130
QStringList components = key.split(QLatin1Char('.'));
131
QScriptValue o = engine->globalObject();
132
for (int i = 0; i < components.count(); ++i) {
133
QScriptValue oo = o.property(components.at(i));
135
oo = engine->newObject();
136
o.setProperty(components.at(i), oo);