1 /****************************************************************************
3 ** Copyright (C) 2011 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 QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "private/qdeclarativetypenamescriptclass_p.h"
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativetypenamecache_p.h"
49 struct TypeNameData : public QScriptDeclarativeClass::Object {
50 TypeNameData(QObject *o, QDeclarativeType *t, QDeclarativeTypeNameScriptClass::TypeNameMode m) : object(o), type(t), typeNamespace(0), mode(m) {}
51 TypeNameData(QObject *o, QDeclarativeTypeNameCache *n, QDeclarativeTypeNameScriptClass::TypeNameMode m) : object(o), type(0), typeNamespace(n), mode(m) {
52 if (typeNamespace) typeNamespace->addref();
55 if (typeNamespace) typeNamespace->release();
59 QDeclarativeType *type;
60 QDeclarativeTypeNameCache *typeNamespace;
61 QDeclarativeTypeNameScriptClass::TypeNameMode mode;
64 QDeclarativeTypeNameScriptClass::QDeclarativeTypeNameScriptClass(QDeclarativeEngine *bindEngine)
65 : QScriptDeclarativeClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)),
66 engine(bindEngine), object(0), type(0), api(0)
70 QDeclarativeTypeNameScriptClass::~QDeclarativeTypeNameScriptClass()
74 QScriptValue QDeclarativeTypeNameScriptClass::newObject(QObject *object, QDeclarativeType *type, TypeNameMode mode)
76 QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
78 return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, type, mode));
81 QScriptValue QDeclarativeTypeNameScriptClass::newObject(QObject *object, QDeclarativeTypeNameCache *ns, TypeNameMode mode)
83 QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
85 return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, ns, mode));
88 QScriptClass::QueryFlags
89 QDeclarativeTypeNameScriptClass::queryProperty(Object *obj, const Identifier &name,
90 QScriptClass::QueryFlags flags)
94 TypeNameData *data = (TypeNameData *)obj;
99 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
101 if (data->typeNamespace) {
102 QDeclarativeTypeNameCache::Data *d = data->typeNamespace->data(name);
105 return QScriptClass::HandlesReadAccess;
106 } else if (QDeclarativeMetaType::ModuleApiInstance *moduleApi = data->typeNamespace->moduleApi()) {
107 if (moduleApi->scriptCallback) {
108 moduleApi->scriptApi = moduleApi->scriptCallback(engine, &ep->scriptEngine);
109 moduleApi->scriptCallback = 0;
110 moduleApi->qobjectCallback = 0;
111 } else if (moduleApi->qobjectCallback) {
112 moduleApi->qobjectApi = moduleApi->qobjectCallback(engine, &ep->scriptEngine);
113 moduleApi->scriptCallback = 0;
114 moduleApi->qobjectCallback = 0;
118 if (api->qobjectApi) {
119 return ep->objectClass->queryProperty(api->qobjectApi, name, flags, 0,
120 QDeclarativeObjectScriptClass::SkipAttachedProperties);
122 return QScriptClass::HandlesReadAccess;
131 } else if (data->type) {
133 if (startsWithUpper(name)) {
134 QString strName = toString(name);
136 if (data->mode == IncludeEnums) {
138 QByteArray enumName = strName.toUtf8();
139 const QMetaObject *metaObject = data->type->baseMetaObject();
140 for (int ii = metaObject->enumeratorCount() - 1; ii >= 0; --ii) {
141 QMetaEnum e = metaObject->enumerator(ii);
142 int value = e.keyToValue(enumName.constData());
145 return QScriptClass::HandlesReadAccess;
150 } else if (data->object) {
151 // Must be an attached property
152 object = qmlAttachedPropertiesObjectById(data->type->attachedPropertiesId(), data->object);
153 if (!object) return 0;
154 return ep->objectClass->queryProperty(object, name, flags, 0);
162 QDeclarativeTypeNameScriptClass::Value
163 QDeclarativeTypeNameScriptClass::property(Object *obj, const Identifier &name)
165 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
166 QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
168 return Value(scriptEngine, newObject(((TypeNameData *)obj)->object, type, ((TypeNameData *)obj)->mode));
170 return ep->objectClass->property(object, name);
171 } else if (api && api->qobjectApi) {
172 return ep->objectClass->property(api->qobjectApi, name);
174 return propertyValue(api->scriptApi, name);
176 return Value(scriptEngine, enumValue);
180 void QDeclarativeTypeNameScriptClass::setProperty(Object *, const Identifier &n, const QScriptValue &v)
184 QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
186 Q_ASSERT(api->qobjectApi);
187 ep->objectClass->setProperty(api->qobjectApi, n, v, context());
190 ep->objectClass->setProperty(object, n, v, context());