Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecontext_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 QtDeclarative 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 QDECLARATIVECONTEXT_P_H
43 #define QDECLARATIVECONTEXT_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 "qdeclarativecontext.h"
57
58 #include "qdeclarativedata_p.h"
59 #include "qdeclarativeintegercache_p.h"
60 #include "qdeclarativetypenamecache_p.h"
61 #include "qdeclarativenotifier_p.h"
62 #include "qdeclarativelist.h"
63 #include "qdeclarativescript_p.h"
64
65 #include <QtCore/qhash.h>
66 #include <QtDeclarative/qjsvalue.h>
67 #include <QtCore/qset.h>
68
69 #include <private/qobject_p.h>
70 #include <private/qflagpointer_p.h>
71 #include <private/qdeclarativeguard_p.h>
72
73 #include <private/qv8_p.h>
74
75
76 QT_BEGIN_NAMESPACE
77
78 class QV8Bindings;
79 class QDeclarativeContext;
80 class QDeclarativeExpression;
81 class QDeclarativeEngine;
82 class QDeclarativeExpression;
83 class QDeclarativeExpressionPrivate;
84 class QDeclarativeAbstractExpression;
85 class QV4Bindings;
86 class QDeclarativeContextData;
87
88 class QDeclarativeContextPrivate : public QObjectPrivate
89 {
90     Q_DECLARE_PUBLIC(QDeclarativeContext)
91 public:
92     QDeclarativeContextPrivate();
93
94     QDeclarativeContextData *data;
95
96     QList<QVariant> propertyValues;
97     int notifyIndex;
98
99     static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
100         return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
101     }
102     static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
103         return static_cast<QDeclarativeContext *>(context->q_func());
104     }
105
106     // Only used for debugging
107     QList<QPointer<QObject> > instances;
108
109     static int context_count(QDeclarativeListProperty<QObject> *);
110     static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
111 };
112
113 class QDeclarativeVME;
114 class QDeclarativeComponentAttached;
115 class QDeclarativeGuardedContextData;
116 class Q_DECLARATIVE_EXPORT QDeclarativeContextData
117 {
118 public:
119     QDeclarativeContextData();
120     QDeclarativeContextData(QDeclarativeContext *);
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     QDeclarativeContextData *parent;
131     QDeclarativeEngine *engine;
132
133     void setParent(QDeclarativeContextData *, bool parentTakesOwnership = false);
134     void refreshExpressions();
135
136     void addObject(QObject *);
137
138     QUrl resolvedUrl(const QUrl &);
139
140     // My containing QDeclarativeContext.  If isInternal is true this owns publicContext.  
141     // If internal is false publicContext owns this.
142     QDeclarativeContext *asQDeclarativeContext();
143     QDeclarativeContextPrivate *asQDeclarativeContextPrivate();
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     QDeclarativeContext *publicContext;
151
152     // VME data that is constructing this context if any
153     void *activeVMEData;
154
155     // Property name cache
156     QDeclarativeIntegerCache *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
167     // List of imports that apply to this context
168     QDeclarativeTypeNameCache *imports;
169
170     // My children
171     QDeclarativeContextData *childContexts;
172
173     // My peers in parent's childContexts list
174     QDeclarativeContextData  *nextChild;
175     QDeclarativeContextData **prevChild;
176
177     // Expressions that use this context
178     QDeclarativeAbstractExpression *expressions;
179
180     // Doubly-linked list of objects that are owned by this context
181     QDeclarativeData *contextObjects;
182
183     // Doubly-linked list of context guards (XXX merge with contextObjects)
184     QDeclarativeGuardedContextData *contextGuards;
185
186     // id guards
187     struct ContextGuard : public QDeclarativeGuard<QObject>
188     {
189         inline ContextGuard();
190         inline ContextGuard &operator=(QObject *obj);
191         inline void objectDestroyed(QObject *);
192
193         inline bool wasSet() const;
194
195         QFlagPointer<QDeclarativeContextData> context;
196         QDeclarativeNotifier bindings;
197     };
198     ContextGuard *idValues;
199     int idValueCount;
200     void setIdProperty(int, QObject *);
201     void setIdPropertyData(QDeclarativeIntegerCache *);
202
203     // Linked contexts. this owns linkedContext.
204     QDeclarativeContextData *linkedContext;
205
206     // Linked list of uses of the Component attached property in this
207     // context
208     QDeclarativeComponentAttached *componentAttached;
209
210     // Optimized binding objects.  Needed for deferred properties.
211     QV4Bindings *v4bindings;
212     QV8Bindings *v8bindings;
213
214     // Return the outermost id for obj, if any.
215     QString findObjectId(const QObject *obj) const;
216
217     static QDeclarativeContextData *get(QDeclarativeContext *context) {
218         return QDeclarativeContextPrivate::get(context)->data;
219     }
220
221 private:
222     void refreshExpressionsRecursive(bool isGlobal);
223     void refreshExpressionsRecursive(QDeclarativeAbstractExpression *);
224     ~QDeclarativeContextData() {}
225 };
226
227 class QDeclarativeGuardedContextData
228 {
229 public:
230     inline QDeclarativeGuardedContextData();
231     inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
232     inline ~QDeclarativeGuardedContextData();
233
234     inline QDeclarativeContextData *contextData();
235     inline void setContextData(QDeclarativeContextData *);
236
237     inline bool isNull() const { return !m_contextData; }
238
239     inline operator QDeclarativeContextData*() const { return m_contextData; }
240     inline QDeclarativeContextData* operator->() const { return m_contextData; }
241     inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
242
243 private:
244     QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
245     QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
246     friend class QDeclarativeContextData;
247
248     inline void clear();
249
250     QDeclarativeContextData *m_contextData;
251     QDeclarativeGuardedContextData  *m_next;
252     QDeclarativeGuardedContextData **m_prev;
253 };
254
255 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
256 : m_contextData(0), m_next(0), m_prev(0)
257 {
258 }
259
260 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
261 : m_contextData(0), m_next(0), m_prev(0)
262 {
263     setContextData(data);
264 }
265
266 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
267 {
268     clear();
269 }
270
271 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
272 {
273     clear();
274
275     if (contextData) {
276         m_contextData = contextData;
277         m_next = contextData->contextGuards;
278         if (m_next) m_next->m_prev = &m_next;
279         m_prev = &contextData->contextGuards;
280         contextData->contextGuards = this;
281     }
282 }
283
284 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
285 {
286     return m_contextData;
287 }
288
289 void QDeclarativeGuardedContextData::clear()
290 {
291     if (m_prev) {
292         *m_prev = m_next;
293         if (m_next) m_next->m_prev = m_prev;
294         m_contextData = 0;
295         m_next = 0;
296         m_prev = 0;
297     }
298 }
299
300 QDeclarativeGuardedContextData &
301 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
302 {
303     setContextData(d);
304     return *this;
305 }
306
307 QDeclarativeContextData::ContextGuard::ContextGuard()
308 : context(0)
309 {
310 }
311
312 QDeclarativeContextData::ContextGuard &QDeclarativeContextData::ContextGuard::operator=(QObject *obj)
313 {
314     QDeclarativeGuard<QObject>::operator=(obj);
315     context.setFlag();
316     bindings.notify(); // For alias connections
317     return *this;
318 }
319
320 void QDeclarativeContextData::ContextGuard::objectDestroyed(QObject *)
321 {
322     if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted)
323         bindings.notify();
324 }
325
326 bool QDeclarativeContextData::ContextGuard::wasSet() const
327 {
328     return context.flag();
329 }
330
331 QT_END_NAMESPACE
332
333 #endif // QDECLARATIVECONTEXT_P_H