Merge branch 'master' of git://scm.dev.nokia.troll.no/qt/qtdeclarative
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativetypenamescriptclass.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "private/qdeclarativetypenamescriptclass_p.h"
43
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativetypenamecache_p.h"
46
47 QT_BEGIN_NAMESPACE
48
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();
53     }
54     ~TypeNameData() {
55         if (typeNamespace) typeNamespace->release();
56     }
57
58     QObject *object;
59     QDeclarativeType *type;
60     QDeclarativeTypeNameCache *typeNamespace;
61     QDeclarativeTypeNameScriptClass::TypeNameMode mode;
62 };
63
64 QDeclarativeTypeNameScriptClass::QDeclarativeTypeNameScriptClass(QDeclarativeEngine *bindEngine)
65 : QScriptDeclarativeClass(QDeclarativeEnginePrivate::getScriptEngine(bindEngine)), 
66   engine(bindEngine), object(0), type(0), api(0)
67 {
68 }
69
70 QDeclarativeTypeNameScriptClass::~QDeclarativeTypeNameScriptClass()
71 {
72 }
73
74 QScriptValue QDeclarativeTypeNameScriptClass::newObject(QObject *object, QDeclarativeType *type, TypeNameMode mode)
75 {
76     QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
77
78     return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, type, mode));
79 }
80
81 QScriptValue QDeclarativeTypeNameScriptClass::newObject(QObject *object, QDeclarativeTypeNameCache *ns, TypeNameMode mode)
82 {
83     QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
84
85     return QScriptDeclarativeClass::newObject(scriptEngine, this, new TypeNameData(object, ns, mode));
86 }
87
88 QScriptClass::QueryFlags 
89 QDeclarativeTypeNameScriptClass::queryProperty(Object *obj, const Identifier &name, 
90                                       QScriptClass::QueryFlags flags)
91 {
92     Q_UNUSED(flags);
93
94     TypeNameData *data = (TypeNameData *)obj;
95
96     object = 0;
97     type = 0;
98     api = 0;
99     QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
100
101     if (data->typeNamespace) {
102         QDeclarativeTypeNameCache::Data *d = data->typeNamespace->data(name);
103         if (d && d->type) {
104             type = d->type;
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;
115             }
116
117             api = moduleApi;
118             if (api->qobjectApi) {
119                 return ep->objectClass->queryProperty(api->qobjectApi, name, flags, 0,
120                                                       QDeclarativeObjectScriptClass::SkipAttachedProperties);
121             } else {
122                 return QScriptClass::HandlesReadAccess;
123             }
124
125             return 0;
126
127         } else {
128             return 0;
129         }
130
131     } else if (data->type) {
132
133         if (startsWithUpper(name)) {
134             QString strName = toString(name);
135             // Must be an enum
136             if (data->mode == IncludeEnums) {
137                 // ### Optimize
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());
143                     if (value != -1) {
144                         enumValue = value;
145                         return QScriptClass::HandlesReadAccess;
146                     }
147                 }
148             }
149             return 0;
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);
155         }
156
157     }
158
159     return 0;
160 }
161
162 QDeclarativeTypeNameScriptClass::Value 
163 QDeclarativeTypeNameScriptClass::property(Object *obj, const Identifier &name)
164 {
165     QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
166     QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine);
167     if (type) {
168         return Value(scriptEngine, newObject(((TypeNameData *)obj)->object, type, ((TypeNameData *)obj)->mode));
169     } else if (object) {
170         return ep->objectClass->property(object, name);
171     } else if (api && api->qobjectApi) {
172         return ep->objectClass->property(api->qobjectApi, name);
173     } else if (api) {
174         return propertyValue(api->scriptApi, name);
175     } else {
176         return Value(scriptEngine, enumValue);
177     }
178 }
179
180 void QDeclarativeTypeNameScriptClass::setProperty(Object *, const Identifier &n, const QScriptValue &v)
181 {
182     Q_ASSERT(!type);
183
184     QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine);
185     if (api) {
186         Q_ASSERT(api->qobjectApi);
187         ep->objectClass->setProperty(api->qobjectApi, n, v, context());
188     } else {
189         Q_ASSERT(object);
190         ep->objectClass->setProperty(object, n, v, context());
191     }
192 }
193
194 QT_END_NAMESPACE
195