Merge branch 'master' of git://scm.dev.nokia.troll.no/qt/qtdeclarative
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeglobalscriptclass.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/qdeclarativeglobalscriptclass_p.h"
43
44 #include <QtCore/qstringlist.h>
45 #include <QtCore/qvector.h>
46 #include <QtScript/qscriptstring.h>
47 #include <QtScript/qscriptengine.h>
48 #include <QtScript/qscriptvalueiterator.h>
49
50 #include <private/qscriptdeclarativeclass_p.h>
51
52 QT_BEGIN_NAMESPACE
53
54 /*
55     Used to prevent any writes to the global object.
56 */
57 QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine)
58 : QScriptClass(engine)
59 {
60     QString eval = QLatin1String("eval");
61     QString version = QLatin1String("version");
62
63     QScriptValue originalGlobalObject = engine->globalObject();
64
65     QScriptValue newGlobalObject = engine->newObject();
66
67     {
68         QScriptValueIterator iter(originalGlobalObject);
69         QVector<QString> names;
70         QVector<QScriptValue> values;
71         QVector<QScriptValue::PropertyFlags> flags;
72         while (iter.hasNext()) {
73             iter.next();
74
75             QString name = iter.name();
76
77             if (name == version)
78                 continue;
79
80             if (name != eval) {
81                 names.append(name);
82                 values.append(iter.value());
83                 flags.append(iter.flags() | QScriptValue::Undeletable);
84             }
85             newGlobalObject.setProperty(iter.scriptName(), iter.value());
86
87             m_illegalNames.insert(name);
88         }
89         m_staticGlobalObject = QScriptDeclarativeClass::newStaticScopeObject(
90             engine, names.size(), names.constData(), values.constData(), flags.constData());
91     }
92
93     newGlobalObject.setScriptClass(this);
94     engine->setGlobalObject(newGlobalObject);
95 }
96
97 QScriptClass::QueryFlags 
98 QDeclarativeGlobalScriptClass::queryProperty(const QScriptValue &object,
99                                     const QScriptString &name,
100                                     QueryFlags flags, uint *id)
101 {
102     Q_UNUSED(object);
103     Q_UNUSED(name);
104     Q_UNUSED(flags);
105     Q_UNUSED(id);
106     return HandlesWriteAccess;
107 }
108
109 void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, 
110                                        const QScriptString &name,
111                                        uint id, const QScriptValue &value)
112 {
113     Q_UNUSED(object);
114     Q_UNUSED(id);
115     Q_UNUSED(value);
116     QString error = QLatin1String("Invalid write to global property \"") + 
117                     name.toString() + QLatin1Char('\"');
118     engine()->currentContext()->throwError(error);
119 }
120
121 /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */
122 void QDeclarativeGlobalScriptClass::explicitSetProperty(const QStringList &names, const QList<QScriptValue> &values)
123 {
124     Q_ASSERT(names.count() == values.count());
125     QScriptValue globalObject = engine()->globalObject();
126
127     QScriptValue v = engine()->newObject();
128
129     QScriptValueIterator iter(v);
130     while (iter.hasNext()) {
131         iter.next();
132         v.setProperty(iter.scriptName(), iter.value());
133     }
134
135     for (int ii = 0; ii < names.count(); ++ii) {
136         const QString &name = names.at(ii);
137         const QScriptValue &value = values.at(ii);
138         v.setProperty(name, value);
139     }
140
141     v.setScriptClass(this);
142
143     engine()->setGlobalObject(v);
144 }
145
146 QT_END_NAMESPACE
147