Merge branch 'master' of git://gitorious.org/qt/qtdeclarative into api_changes
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlcontext_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLCONTEXT_P_H
43 #define QQMLCONTEXT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qqmlcontext.h"
57
58 #include "qqmldata_p.h"
59 #include "qqmlintegercache_p.h"
60 #include "qqmltypenamecache_p.h"
61 #include "qqmlnotifier_p.h"
62 #include "qqmllist.h"
63 #include "qqmlscript_p.h"
64
65 #include <QtCore/qhash.h>
66 #include <QtQml/qjsvalue.h>
67 #include <QtCore/qset.h>
68
69 #include <private/qobject_p.h>
70 #include <private/qflagpointer_p.h>
71 #include <private/qqmlguard_p.h>
72
73 #include <private/qv8_p.h>
74
75
76 QT_BEGIN_NAMESPACE
77
78 class QV8Bindings;
79 class QQmlContext;
80 class QQmlExpression;
81 class QQmlEngine;
82 class QQmlExpression;
83 class QQmlExpressionPrivate;
84 class QQmlAbstractExpression;
85 class QV4Bindings;
86 class QQmlContextData;
87
88 class QQmlContextPrivate : public QObjectPrivate
89 {
90     Q_DECLARE_PUBLIC(QQmlContext)
91 public:
92     QQmlContextPrivate();
93
94     QQmlContextData *data;
95
96     QList<QVariant> propertyValues;
97     int notifyIndex;
98
99     static QQmlContextPrivate *get(QQmlContext *context) {
100         return static_cast<QQmlContextPrivate *>(QObjectPrivate::get(context));
101     }
102     static QQmlContext *get(QQmlContextPrivate *context) {
103         return static_cast<QQmlContext *>(context->q_func());
104     }
105
106     // Only used for debugging
107     QList<QPointer<QObject> > instances;
108
109     static int context_count(QQmlListProperty<QObject> *);
110     static QObject *context_at(QQmlListProperty<QObject> *, int);
111 };
112
113 class QQmlVME;
114 class QQmlComponentAttached;
115 class QQmlGuardedContextData;
116 class Q_QML_EXPORT QQmlContextData
117 {
118 public:
119     QQmlContextData();
120     QQmlContextData(QQmlContext *);
121     void clearContext();
122     void destroy();
123     void invalidate();
124
125     inline bool isValid() const {
126         return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
127     }
128
129     // My parent context and engine
130     QQmlContextData *parent;
131     QQmlEngine *engine;
132
133     void setParent(QQmlContextData *, bool parentTakesOwnership = false);
134     void refreshExpressions();
135
136     void addObject(QObject *);
137
138     QUrl resolvedUrl(const QUrl &);
139
140     // My containing QQmlContext.  If isInternal is true this owns publicContext.  
141     // If internal is false publicContext owns this.
142     QQmlContext *asQQmlContext();
143     QQmlContextPrivate *asQQmlContextPrivate();
144     quint32 isInternal:1;
145     quint32 ownedByParent:1; // unrelated to isInternal; parent context deletes children if true.
146     quint32 isJSContext:1;
147     quint32 isPragmaLibraryContext:1;
148     quint32 unresolvedNames:1; // True if expressions in this context failed to resolve a toplevel name
149     quint32 dummy:28;
150     QQmlContext *publicContext;
151
152     // VME data that is constructing this context if any
153     void *activeVMEData;
154
155     // Property name cache
156     QQmlIntegerCache *propertyNames;
157
158     // Context object
159     QObject *contextObject;
160
161     // Any script blocks that exist on this context
162     QList<v8::Persistent<v8::Object> > importedScripts;
163
164     // Context base url
165     QUrl url;
166     QString urlString;
167
168     // List of imports that apply to this context
169     QQmlTypeNameCache *imports;
170
171     // My children
172     QQmlContextData *childContexts;
173
174     // My peers in parent's childContexts list
175     QQmlContextData  *nextChild;
176     QQmlContextData **prevChild;
177
178     // Expressions that use this context
179     QQmlAbstractExpression *expressions;
180
181     // Doubly-linked list of objects that are owned by this context
182     QQmlData *contextObjects;
183
184     // Doubly-linked list of context guards (XXX merge with contextObjects)
185     QQmlGuardedContextData *contextGuards;
186
187     // id guards
188     struct ContextGuard : public QQmlGuard<QObject>
189     {
190         inline ContextGuard();
191         inline ContextGuard &operator=(QObject *obj);
192         inline void objectDestroyed(QObject *);
193
194         inline bool wasSet() const;
195
196         QFlagPointer<QQmlContextData> context;
197         QQmlNotifier bindings;
198     };
199     ContextGuard *idValues;
200     int idValueCount;
201     void setIdProperty(int, QObject *);
202     void setIdPropertyData(QQmlIntegerCache *);
203
204     // Linked contexts. this owns linkedContext.
205     QQmlContextData *linkedContext;
206
207     // Linked list of uses of the Component attached property in this
208     // context
209     QQmlComponentAttached *componentAttached;
210
211     // Optimized binding objects.  Needed for deferred properties.
212     QV4Bindings *v4bindings;
213     QV8Bindings *v8bindings;
214
215     // Return the outermost id for obj, if any.
216     QString findObjectId(const QObject *obj) const;
217
218     static QQmlContextData *get(QQmlContext *context) {
219         return QQmlContextPrivate::get(context)->data;
220     }
221
222 private:
223     void refreshExpressionsRecursive(bool isGlobal);
224     void refreshExpressionsRecursive(QQmlAbstractExpression *);
225     ~QQmlContextData() {}
226 };
227
228 class QQmlGuardedContextData
229 {
230 public:
231     inline QQmlGuardedContextData();
232     inline QQmlGuardedContextData(QQmlContextData *);
233     inline ~QQmlGuardedContextData();
234
235     inline QQmlContextData *contextData();
236     inline void setContextData(QQmlContextData *);
237
238     inline bool isNull() const { return !m_contextData; }
239
240     inline operator QQmlContextData*() const { return m_contextData; }
241     inline QQmlContextData* operator->() const { return m_contextData; }
242     inline QQmlGuardedContextData &operator=(QQmlContextData *d);
243
244 private:
245     QQmlGuardedContextData &operator=(const QQmlGuardedContextData &);
246     QQmlGuardedContextData(const QQmlGuardedContextData &);
247     friend class QQmlContextData;
248
249     inline void clear();
250
251     QQmlContextData *m_contextData;
252     QQmlGuardedContextData  *m_next;
253     QQmlGuardedContextData **m_prev;
254 };
255
256 QQmlGuardedContextData::QQmlGuardedContextData()
257 : m_contextData(0), m_next(0), m_prev(0)
258 {
259 }
260
261 QQmlGuardedContextData::QQmlGuardedContextData(QQmlContextData *data)
262 : m_contextData(0), m_next(0), m_prev(0)
263 {
264     setContextData(data);
265 }
266
267 QQmlGuardedContextData::~QQmlGuardedContextData()
268 {
269     clear();
270 }
271
272 void QQmlGuardedContextData::setContextData(QQmlContextData *contextData)
273 {
274     clear();
275
276     if (contextData) {
277         m_contextData = contextData;
278         m_next = contextData->contextGuards;
279         if (m_next) m_next->m_prev = &m_next;
280         m_prev = &contextData->contextGuards;
281         contextData->contextGuards = this;
282     }
283 }
284
285 QQmlContextData *QQmlGuardedContextData::contextData()
286 {
287     return m_contextData;
288 }
289
290 void QQmlGuardedContextData::clear()
291 {
292     if (m_prev) {
293         *m_prev = m_next;
294         if (m_next) m_next->m_prev = m_prev;
295         m_contextData = 0;
296         m_next = 0;
297         m_prev = 0;
298     }
299 }
300
301 QQmlGuardedContextData &
302 QQmlGuardedContextData::operator=(QQmlContextData *d)
303 {
304     setContextData(d);
305     return *this;
306 }
307
308 QQmlContextData::ContextGuard::ContextGuard()
309 : context(0)
310 {
311 }
312
313 QQmlContextData::ContextGuard &QQmlContextData::ContextGuard::operator=(QObject *obj)
314 {
315     QQmlGuard<QObject>::operator=(obj);
316     context.setFlag();
317     bindings.notify(); // For alias connections
318     return *this;
319 }
320
321 void QQmlContextData::ContextGuard::objectDestroyed(QObject *)
322 {
323     if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted)
324         bindings.notify();
325 }
326
327 bool QQmlContextData::ContextGuard::wasSet() const
328 {
329     return context.flag();
330 }
331
332 QT_END_NAMESPACE
333
334 #endif // QQMLCONTEXT_P_H