93b9d3b41eb546480d8b9ce435ff56f81334b3e4
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecontext_p.h
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 #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 "private/qdeclarativedata_p.h"
59 #include "private/qdeclarativeintegercache_p.h"
60 #include "private/qdeclarativetypenamecache_p.h"
61 #include "private/qdeclarativenotifier_p.h"
62 #include "qdeclarativelist.h"
63 #include "private/qdeclarativeparser_p.h"
64
65 #include <QtCore/qhash.h>
66 #include <QtScript/qscriptvalue.h>
67 #include <QtCore/qset.h>
68
69 #include <private/qobject_p.h>
70 #include "private/qdeclarativeguard_p.h"
71
72 #include <private/qv8_p.h>
73
74 QT_BEGIN_NAMESPACE
75
76 class QDeclarativeContext;
77 class QDeclarativeExpression;
78 class QDeclarativeEngine;
79 class QDeclarativeExpression;
80 class QDeclarativeExpressionPrivate;
81 class QDeclarativeAbstractExpression;
82 class QDeclarativeV4Bindings;
83 class QDeclarativeContextData;
84
85 class QDeclarativeContextPrivate : public QObjectPrivate
86 {
87     Q_DECLARE_PUBLIC(QDeclarativeContext)
88 public:
89     QDeclarativeContextPrivate();
90
91     QDeclarativeContextData *data;
92
93     QList<QVariant> propertyValues;
94     int notifyIndex;
95
96     static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
97         return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
98     }
99     static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
100         return static_cast<QDeclarativeContext *>(context->q_func());
101     }
102
103     // Only used for debugging
104     QList<QPointer<QObject> > instances;
105
106     static int context_count(QDeclarativeListProperty<QObject> *);
107     static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
108 };
109
110 class QDeclarativeComponentAttached;
111 class QDeclarativeGuardedContextData;
112 class Q_AUTOTEST_EXPORT QDeclarativeContextData
113 {
114 public:
115     QDeclarativeContextData();
116     QDeclarativeContextData(QDeclarativeContext *);
117     void clearContext();
118     void destroy();
119     void invalidate();
120
121     inline bool isValid() const {
122         return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
123     }
124
125     // My parent context and engine
126     QDeclarativeContextData *parent;
127     QDeclarativeEngine *engine;
128
129     void setParent(QDeclarativeContextData *, bool parentTakesOwnership = false);
130     void refreshExpressions();
131
132     void addObject(QObject *);
133
134     QUrl resolvedUrl(const QUrl &);
135
136     // My containing QDeclarativeContext.  If isInternal is true this owns publicContext.  
137     // If internal is false publicContext owns this.
138     QDeclarativeContext *asQDeclarativeContext();
139     QDeclarativeContextPrivate *asQDeclarativeContextPrivate();
140     quint32 isInternal:1;
141     quint32 ownedByParent:1; // unrelated to isInternal; parent context deletes children if true.
142     quint32 isJSContext:1;
143     quint32 dummy:29;
144     QDeclarativeContext *publicContext;
145
146     // Property name cache
147     QDeclarativeIntegerCache *propertyNames;
148
149     // Context object
150     QObject *contextObject;
151
152     // Any script blocks that exist on this context
153     QList<v8::Persistent<v8::Object> > importedScripts;
154
155     // Context base url
156     QUrl url;
157
158     // List of imports that apply to this context
159     QDeclarativeTypeNameCache *imports;
160
161     // My children
162     QDeclarativeContextData *childContexts;
163
164     // My peers in parent's childContexts list
165     QDeclarativeContextData  *nextChild;
166     QDeclarativeContextData **prevChild;
167
168     // Expressions that use this context
169     QDeclarativeAbstractExpression *expressions;
170
171     // Doubly-linked list of objects that are owned by this context
172     QDeclarativeData *contextObjects;
173
174     // Doubly-linked list of context guards (XXX merge with contextObjects)
175     QDeclarativeGuardedContextData *contextGuards;
176
177     // id guards
178     struct ContextGuard : public QDeclarativeGuard<QObject>
179     {
180         ContextGuard() : context(0) {}
181         inline ContextGuard &operator=(QObject *obj)
182         { QDeclarativeGuard<QObject>::operator=(obj); return *this; }
183         virtual void objectDestroyed(QObject *) { 
184             if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted) bindings.notify(); 
185         }
186         QDeclarativeContextData *context;
187         QDeclarativeNotifier bindings;
188     };
189     ContextGuard *idValues;
190     int idValueCount;
191     void setIdProperty(int, QObject *);
192     void setIdPropertyData(QDeclarativeIntegerCache *);
193
194     // Optimized binding pointer
195     QDeclarativeV4Bindings *optimizedBindings;
196
197     // Linked contexts. this owns linkedContext.
198     QDeclarativeContextData *linkedContext;
199
200     // Linked list of uses of the Component attached property in this
201     // context
202     QDeclarativeComponentAttached *componentAttached;
203
204     // Return the outermost id for obj, if any.
205     QString findObjectId(const QObject *obj) const;
206
207     static QDeclarativeContextData *get(QDeclarativeContext *context) {
208         return QDeclarativeContextPrivate::get(context)->data;
209     }
210
211 private:
212     ~QDeclarativeContextData() {}
213 };
214
215 class QDeclarativeGuardedContextData
216 {
217 public:
218     inline QDeclarativeGuardedContextData();
219     inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
220     inline ~QDeclarativeGuardedContextData();
221
222     inline QDeclarativeContextData *contextData();
223     inline void setContextData(QDeclarativeContextData *);
224
225     inline bool isNull() const { return !m_contextData; }
226
227     inline operator QDeclarativeContextData*() const { return m_contextData; }
228     inline QDeclarativeContextData* operator->() const { return m_contextData; }
229     inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
230
231 private:
232     QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
233     QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
234     friend class QDeclarativeContextData;
235
236     inline void clear();
237
238     QDeclarativeContextData *m_contextData;
239     QDeclarativeGuardedContextData  *m_next;
240     QDeclarativeGuardedContextData **m_prev;
241 };
242
243 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
244 : m_contextData(0), m_next(0), m_prev(0)
245 {
246 }
247
248 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
249 : m_contextData(0), m_next(0), m_prev(0)
250 {
251     setContextData(data);
252 }
253
254 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
255 {
256     clear();
257 }
258
259 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
260 {
261     clear();
262
263     if (contextData) {
264         m_contextData = contextData;
265         m_next = contextData->contextGuards;
266         if (m_next) m_next->m_prev = &m_next;
267         m_prev = &contextData->contextGuards;
268         contextData->contextGuards = this;
269     }
270 }
271
272 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
273 {
274     return m_contextData;
275 }
276
277 void QDeclarativeGuardedContextData::clear()
278 {
279     if (m_prev) {
280         *m_prev = m_next;
281         if (m_next) m_next->m_prev = m_prev;
282         m_contextData = 0;
283         m_next = 0;
284         m_prev = 0;
285     }
286 }
287
288 QDeclarativeGuardedContextData &
289 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
290 {
291     setContextData(d);
292     return *this;
293 }
294
295 QT_END_NAMESPACE
296
297 #endif // QDECLARATIVECONTEXT_P_H