|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtScript module of the Qt Toolkit. |
|
8 ** |
|
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 |
|
14 ** this package. |
|
15 ** |
|
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. |
|
23 ** |
|
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. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qscriptextensionplugin.h" |
|
43 |
|
44 #include "qscriptvalue.h" |
|
45 #include "qscriptengine.h" |
|
46 |
|
47 QT_BEGIN_NAMESPACE |
|
48 |
|
49 /*! |
|
50 \since 4.3 |
|
51 \class QScriptExtensionPlugin |
|
52 \brief The QScriptExtensionPlugin class provides an abstract base for custom QScript extension plugins. |
|
53 |
|
54 \ingroup plugins |
|
55 |
|
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. |
|
59 |
|
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. |
|
64 |
|
65 \sa QScriptEngine::importExtension(), {Creating QtScript Extensions} |
|
66 */ |
|
67 |
|
68 /*! |
|
69 \fn QStringList QScriptExtensionPlugin::keys() const |
|
70 |
|
71 Returns the list of keys this plugin supports. |
|
72 |
|
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}). |
|
75 |
|
76 \sa initialize() |
|
77 */ |
|
78 |
|
79 /*! |
|
80 \fn void QScriptExtensionPlugin::initialize(const QString& key, QScriptEngine *engine) |
|
81 |
|
82 Initializes the extension specified by \a key in the given \a engine. |
|
83 The key must come from the set of keys(). |
|
84 |
|
85 \sa keys() |
|
86 */ |
|
87 |
|
88 /*! |
|
89 Constructs a script extension plugin with the given \a parent. |
|
90 |
|
91 Note that this constructor is invoked automatically by the |
|
92 Q_EXPORT_PLUGIN2() macro, so there is no need for calling it |
|
93 explicitly. |
|
94 */ |
|
95 QScriptExtensionPlugin::QScriptExtensionPlugin(QObject *parent) |
|
96 : QObject(parent) |
|
97 { |
|
98 } |
|
99 |
|
100 /*! |
|
101 Destroys the script extension plugin. |
|
102 |
|
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. |
|
105 */ |
|
106 QScriptExtensionPlugin::~QScriptExtensionPlugin() |
|
107 { |
|
108 } |
|
109 |
|
110 /*! |
|
111 |
|
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". |
|
121 |
|
122 Use this function to avoid global namespace pollution when installing |
|
123 your extensions in the engine. |
|
124 |
|
125 \sa initialize() |
|
126 */ |
|
127 QScriptValue QScriptExtensionPlugin::setupPackage( |
|
128 const QString &key, QScriptEngine *engine) const |
|
129 { |
|
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)); |
|
134 if (!oo.isValid()) { |
|
135 oo = engine->newObject(); |
|
136 o.setProperty(components.at(i), oo); |
|
137 } |
|
138 o = oo; |
|
139 } |
|
140 return o; |
|
141 } |
|
142 |
|
143 QT_END_NAMESPACE |