Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativelistmodelworkeragent.cpp
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 #include "qdeclarativelistmodelworkeragent_p.h"
43 #include "qdeclarativelistmodel_p_p.h"
44 #include <private/qdeclarativedata_p.h>
45 #include <private/qdeclarativeengine_p.h>
46 #include <qdeclarativeinfo.h>
47
48 #include <QtCore/qcoreevent.h>
49 #include <QtCore/qcoreapplication.h>
50 #include <QtCore/qdebug.h>
51
52
53 QT_BEGIN_NAMESPACE
54
55
56 void QDeclarativeListModelWorkerAgent::Data::clearChange(int uid)
57 {
58     for (int i=0 ; i < changes.count() ; ++i) {
59         if (changes[i].modelUid == uid) {
60             changes.removeAt(i);
61             --i;
62         }
63     }
64 }
65
66 void QDeclarativeListModelWorkerAgent::Data::insertChange(int uid, int index, int count)
67 {
68     Change c = { uid, Change::Inserted, index, count, 0, QList<int>() };
69     changes << c;
70 }
71
72 void QDeclarativeListModelWorkerAgent::Data::removeChange(int uid, int index, int count)
73 {
74     Change c = { uid, Change::Removed, index, count, 0, QList<int>() };
75     changes << c;
76 }
77
78 void QDeclarativeListModelWorkerAgent::Data::moveChange(int uid, int index, int count, int to)
79 {
80     Change c = { uid, Change::Moved, index, count, to, QList<int>() };
81     changes << c;
82 }
83
84 void QDeclarativeListModelWorkerAgent::Data::changedChange(int uid, int index, int count, const QList<int> &roles)
85 {
86     Change c = { uid, Change::Changed, index, count, 0, roles };
87     changes << c;
88 }
89
90 QDeclarativeListModelWorkerAgent::QDeclarativeListModelWorkerAgent(QDeclarativeListModel *model)
91 : m_ref(1), m_orig(model), m_copy(new QDeclarativeListModel(model, this))
92 {
93 }
94
95 QDeclarativeListModelWorkerAgent::~QDeclarativeListModelWorkerAgent()
96 {
97     mutex.lock();
98     syncDone.wakeAll();
99     mutex.unlock();
100 }
101
102 void QDeclarativeListModelWorkerAgent::setV8Engine(QV8Engine *eng)
103 {
104     m_copy->m_engine = eng;
105 }
106
107 void QDeclarativeListModelWorkerAgent::addref()
108 {
109     m_ref.ref();
110 }
111
112 void QDeclarativeListModelWorkerAgent::release()
113 {
114     bool del = !m_ref.deref();
115
116     if (del)
117         delete this;
118 }
119
120 int QDeclarativeListModelWorkerAgent::count() const
121 {
122     return m_copy->count();
123 }
124
125 void QDeclarativeListModelWorkerAgent::clear()
126 {
127     m_copy->clear();
128 }
129
130 void QDeclarativeListModelWorkerAgent::remove(QDeclarativeV8Function *args)
131 {
132     m_copy->remove(args);
133 }
134
135 void QDeclarativeListModelWorkerAgent::append(QDeclarativeV8Function *args)
136 {
137     m_copy->append(args);
138 }
139
140 void QDeclarativeListModelWorkerAgent::insert(QDeclarativeV8Function *args)
141 {
142     m_copy->insert(args);
143 }
144
145 QDeclarativeV8Handle QDeclarativeListModelWorkerAgent::get(int index) const
146 {
147     return m_copy->get(index);
148 }
149
150 void QDeclarativeListModelWorkerAgent::set(int index, const QDeclarativeV8Handle &value)
151 {
152     m_copy->set(index, value);
153 }
154
155 void QDeclarativeListModelWorkerAgent::setProperty(int index, const QString& property, const QVariant& value)
156 {
157     m_copy->setProperty(index, property, value);
158 }
159
160 void QDeclarativeListModelWorkerAgent::move(int from, int to, int count)
161 {
162     m_copy->move(from, to, count);
163 }
164
165 void QDeclarativeListModelWorkerAgent::sync()
166 {
167     Sync *s = new Sync;
168     s->data = data;
169     s->list = m_copy;
170     data.changes.clear();
171
172     mutex.lock();
173     QCoreApplication::postEvent(this, s);
174     syncDone.wait(&mutex);
175     mutex.unlock();
176 }
177
178 bool QDeclarativeListModelWorkerAgent::event(QEvent *e)
179 {
180     if (e->type() == QEvent::User) {
181
182         QMutexLocker locker(&mutex);
183         Sync *s = static_cast<Sync *>(e);
184
185         const QList<Change> &changes = s->data.changes;
186
187         bool cc = m_orig->count() != s->list->count();
188
189         QHash<int, QDeclarativeListModel *> targetModelDynamicHash;
190         QHash<int, ListModel *> targetModelStaticHash;
191
192         Q_ASSERT(m_orig->m_dynamicRoles == s->list->m_dynamicRoles);
193         if (m_orig->m_dynamicRoles)
194             QDeclarativeListModel::sync(s->list, m_orig, &targetModelDynamicHash);
195         else
196             ListModel::sync(s->list->m_listModel, m_orig->m_listModel, &targetModelStaticHash);
197
198         for (int ii = 0; ii < changes.count(); ++ii) {
199             const Change &change = changes.at(ii);
200
201             QDeclarativeListModel *model = 0;
202             if (m_orig->m_dynamicRoles) {
203                 model = targetModelDynamicHash.value(change.modelUid);
204             } else {
205                 ListModel *lm = targetModelStaticHash.value(change.modelUid);
206                 if (lm)
207                     model = lm->m_modelCache;
208             }
209
210             if (model) {
211                 switch (change.type) {
212                 case Change::Inserted:
213                     emit model->itemsInserted(change.index, change.count);
214                     break;
215                 case Change::Removed:
216                     emit model->itemsRemoved(change.index, change.count);
217                     break;
218                 case Change::Moved:
219                     emit model->itemsMoved(change.index, change.to, change.count);
220                     break;
221                 case Change::Changed:
222                     emit model->itemsChanged(change.index, change.count, change.roles);
223                     break;
224                 }
225             }
226         }
227
228         syncDone.wakeAll();
229         locker.unlock();
230
231         if (cc)
232             emit m_orig->countChanged();
233     }
234
235     return QObject::event(e);
236 }
237
238 QT_END_NAMESPACE
239