summaryrefslogtreecommitdiffstats
path: root/src/imports/jsondb/jsondblistmodel.cpp
blob: fe6fe3ae05b5e2b1a0bb7da5d10b17c50dce549f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//#define JSONDB_LISTMODEL_DEBUG
//#define JSONDB_LISTMODEL_BENCHMARK

#include "plugin.h"
#include "jsondblistmodel.h"

#include <QJSEngine>
#include <QJSValueIterator>
#include <QDebug>
#ifdef JSONDB_LISTMODEL_BENCHMARK
#include <QElapsedTimer>
#endif

/*!
  \internal
  \class JsonDbListModel
*/

QT_BEGIN_NAMESPACE_JSONDB


/*!
    \qmlclass JsonDbListModel JsonDbListModel
    \inqmlmodule QtJsonDb 1.0
    \since 1.0
    \brief Provides a ListModel displaying data matching a query

    The JsonDbListModel provides a read-only ListModel usable with views such as
    ListView or GridView displaying data items matching a query. The sorting is done using
    an index set on the JsonDb server. If it doesn't find a matching index for the sortkey,
    the model goes into Error status. Maximum number of items in the model cache can be set
    by cacheSize property.

    When an item is not present in the internal cache, the model can return an 'undefined'
    object from data() method. It will be queued for retrieval and the model will notify its
    presence using the dataChanged() signal.

    The model is initialized by retrieving the result in chunks. After receiving the first
    chunk, the model is reset with items from it. The status will be "Querying" during
    fetching data and will be changed to "Ready".

    \note This is still a work in progress, so expect minor changes.

    \code
    JsonDb.JsonDbListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        cacheSize: 100
        partitions: [JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endcode
*/

JsonDbListModel::JsonDbListModel(QObject *parent):
    QJsonDbQueryModel(QJsonDbConnection::defaultConnection(), parent)
{
    QJsonDbConnection::defaultConnection()->connectToServer();
    connect(this, SIGNAL(rowCountChanged(int)), SIGNAL(countChanged()));
}

JsonDbListModel::~JsonDbListModel()
{
}

//---------------- METHODS------------------------------

void JsonDbListModel::classBegin()
{
}

void JsonDbListModel::componentComplete()
{
    populate();
    connect (this, SIGNAL(objectAvailable(int,QJsonObject,QString)), this, SLOT(onObjectAvailable(int,QJsonObject,QString)));
}

/*!
    \qmlmethod  QtJsonDb1::JsonDbListModel::get(int index, function callback)
    Calls the callback with object at the specified \a index in the model. The result.object property
    contains the object in its raw form as returned by the query, the rolenames
    are not applied. The object.partition is the partition for the returned.
    If the index is out of range it returns an object with empty partition & object properties.

    \code
    function updateCallback(error, response) {
        if (error) {
            console.log("Update Error :"+JSON.stringify(error));
            return,
        }
        console.log("Response from Update");
        console.log("response.id = "+response.id +" count = "+response.items.length);
        for (var i = 0; i < response.items.length; i++) {
            console.log("_uuid = "+response.items[i]._uuid +" ._version = "+response.items[i]._version);
        }
    }
    function updateItemCallback(index, response) {
        if (response) {
            response.object.firstName = response.object.firstName+ "*";
            response.partition.update(response.object, updateCallback);
        }
    }
    onClicked: {
        contacts.get(listView.currentIndex, updateItemCallback);
    }
    \endcode
*/
void JsonDbListModel::get(int index, const QJSValue &callback)
{
    getCallbacks.insertMulti(index, callback);
    fetchObject(index);
}

/*!
    \qmlmethod int QtJsonDb1::JsonDbListModel::indexOf(string uuid)

    Returns the index of the object with the \a uuid in the model. If the object is
    not found it returns -1
*/
int JsonDbListModel::indexOf(const QString &uuid) const
{
    return QJsonDbQueryModel::indexOf(uuid);
}

/*!
    \qmlmethod object QtJsonDb1::JsonDbListModel::getPartition(int index)

    Returns the partition object at the specified \a index in the model. If
    the index is out of range it returns an empty object.
*/

JsonDbPartition* JsonDbListModel::getPartition(int index)
{
    const QString partitionName = this->partitionName(index);
    if (!this->nameToJsonDbPartitionMap.contains(partitionName)) {
        this->nameToJsonDbPartitionMap.insert(partitionName,
                                           new JsonDbPartition(partitionName));
    }
    return this->nameToJsonDbPartitionMap.value(partitionName);
}

// Offered for backwards compatibility only
JsonDbPartition* JsonDbListModel::partition()
{
    qWarning() << "Property partition is deprecated. Use 'partitions' instead.";
    return getPartition(0);
}

// Offered for backwards compatibility only
void JsonDbListModel::setPartition(JsonDbPartition *newPartition)
{
    if (!newPartition) {
        qWarning("Invalid partition object");
        return;
    }

    setPartitionNames(QStringList());
    foreach (const QString &partitionName, nameToJsonDbPartitionMap.keys()) {
        nameToJsonDbPartitionMap.take(partitionName)->deleteLater();
    }
    const QString partitionName = newPartition->name();
    appendPartitionName(partitionName);
    if (!nameToJsonDbPartitionMap.contains(partitionName)) {
        nameToJsonDbPartitionMap.insert(partitionName,
                                        new JsonDbPartition(partitionName));
    }
    qWarning() << "Property partition is deprecated. Use 'partitions' instead.";
}

int JsonDbListModel::limit() const
{
    qWarning() << "Property limit is deprecated. Use 'cacheSize' instead.";
    return 0;
}

void JsonDbListModel::setLimit(int newCacheSize)
{
    Q_UNUSED(newCacheSize);
    qWarning() << "Property limit is deprecated. Use 'cacheSize' instead.";
}

int JsonDbListModel::chunkSize() const
{
    qWarning() << "Property chunkSize is deprecated. Use 'cacheSize' instead.";
    return 0;
}

void JsonDbListModel::setChunkSize(int newChunkSize)
{
    Q_UNUSED(newChunkSize);
    qWarning() << "Property chunkSize is deprecated. Use 'cacheSize' instead.";
}

int JsonDbListModel::lowWaterMark() const
{
    qWarning() << "Property lowWaterMark is deprecated. It is set automatically.";
    return 0;
}

void JsonDbListModel::setLowWaterMark(int newLowWaterMark)
{
    Q_UNUSED(newLowWaterMark);
    qWarning() << "Property lowWaterMark is deprecated. It is set automatically.";
}

//---------------- PROPERTIES------------------------------

/*!
    \qmlproperty object QtJsonDb1::JsonDbListModel::bindings
    Holds the bindings for the placeholders used in the query string. Note that
    the placeholder marker '%' should not be included as part of the keys.

    \qml
    JsonDb.JsonDbListModel {
        id: listModel
        query: '[?_type="Contact"][?name=%firstName]'
        bindings :{'firstName':'Book'}
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endqml

    \sa {QtJsonDb1::JsonDbListModel::query} {query}

*/

/*!
    \qmlproperty int QtJsonDb1::JsonDbListModel::cacheSize
    Holds the maximum number of items cached by the model.
*/

/*!
    \qmlproperty object QtJsonDb1::JsonDbListModel::error
    \readonly

    This property holds the current error information for the object. It contains:
    \list
    \li error.code -  code for the current error.
    \li error.message - detailed explanation of the error
    \endlist
*/

/*!
    \qmlproperty string QtJsonDb1::JsonDbListModel::query

    The query string in JsonQuery format used by the model to fetch
    items from the database. Setting an empty query clears all the elements

    In the following example, the JsonDbListModel would contain all
    the objects with \a _type "CONTACT" from partition called "com.nokia.shared"

    \qml
    JsonDb.JsonDbListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endqml

*/

/*!
    \qmlproperty int QtJsonDb1::JsonDbListModel::rowCount
    The number of items in the model.
*/

/*!
    \qmlproperty ListOrObject QtJsonDb1::JsonDbListModel::roleNames

    Controls which properties to expose from the objects matching the query.

    Setting \a roleNames to a list of strings causes the model to expose
    corresponding object values as roles to the delegate for each item viewed.

    \code
    JsonDb.JsonDbListModel {
        id: listModel
        query: "[?_type=\"MyType\"]"
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        roleNames: ['a', 'b']
    }
    ListView {
        model: listModel
        Text {
            text: a + ":" + b
        }
    \endcode

    Setting \a roleNames to a dictionary remaps properties in the object
    to the specified roles in the model.

    In the following example, role \a a would yield the value of
    property \a aLongName in the objects. Role \a liftedProperty would
    yield the value of \a o.nested.property for each matching object \a
    o in the database.

    \code
    function makeRoleNames() {
        return { 'a': 'aLongName', 'liftedProperty': 'nested.property' };
    }
    JsonDb.JsonDbListModel {
        id: listModel
        query: "[?_type=\"MyType\"]"
        partitions: [JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        roleNames: makeRoleNames()
    }
    ListView {
        model: listModel
        Text {
            text: a + " " + liftedProperty
        }
    }
    \endcode
*/

/*!
    \qmlproperty string QtJsonDb1::JsonDbListModel::sortOrder

    The order used by the model to sort the items. Make sure that there
    is a matching Index in the database for this sortOrder. This has to be
    specified in the JsonQuery format.

    In the following example, the JsonDbListModel would contain all
    the objects of type \a "CONTACT" sorted by their \a firstName field

    \qml
    JsonDb.JsonDbListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        sortOrder: "[/firstName]"
    }
    \endqml

    \sa {QtJsonDb1::JsonDbListModel::bindings} {bindings}

*/

/*!
    \qmlproperty State QtJsonDb1::JsonDbListModel::state
    \readonly
    The current state of the model.
    \list
    \li State.None - The model is not initialized
    \li State.Querying - It is querying the results from server
    \li State.Ready - Results are ready
    \li State.Error - Cannot find a matching index on the server
    \endlist
*/

/*!
    \qmlproperty list QtJsonDb1::JsonDbListModel::partitions
    Holds the list of partition objects for the model.
    \code
    JsonDb.JsonDbListModel {
        id: contacts
        query: '[?_type="Contact"]'
        partitions :[nokiaPartition, nokiaPartition2]
        roleNames: ["firstName", "lastName", "_uuid", "_version"]
        sortOrder:"[/firstName]"
    }

    \endcode
*/

int JsonDbListModel::count() const
{
    qWarning() << "Property 'count' is deprecated. Use 'rowCount' instead.";
    return rowCount();
}


QQmlListProperty<JsonDbPartition> JsonDbListModel::partitions()
{
    return QQmlListProperty<JsonDbPartition>(this, 0
                                                     , &partitions_append
                                                     , &partitions_count
                                                     , &partitions_at
                                             , &partitions_clear);
}

void JsonDbListModel::partitions_append(QQmlListProperty<JsonDbPartition> *p, JsonDbPartition *v)
{
    if (!v) {
        qWarning("Invalid partition object");
        return;
    }
    JsonDbListModel *q = qobject_cast<JsonDbListModel *>(p->object);
    if (q) {
        const QString partitionName = v->name();
        q->appendPartitionName(partitionName);
        if (!q->nameToJsonDbPartitionMap.contains(partitionName)) {
            q->nameToJsonDbPartitionMap.insert(partitionName,
                                               new JsonDbPartition(partitionName));
        }
    }
}

int JsonDbListModel::partitions_count(QQmlListProperty<JsonDbPartition> *p)
{
    JsonDbListModel *q = qobject_cast<JsonDbListModel *>(p->object);
    if (q) {
        return q->partitionNames().size();
    }
    return 0;
}

JsonDbPartition* JsonDbListModel::partitions_at(QQmlListProperty<JsonDbPartition> *p, int idx)
{
    JsonDbListModel *q = qobject_cast<JsonDbListModel *>(p->object);
    if (q && idx < q->partitionNames().size()) {
        QString partitionName = q->partitionName(idx);
        if (q->nameToJsonDbPartitionMap.contains(partitionName))
            return q->nameToJsonDbPartitionMap.value(partitionName);
    }
    return 0;
}

void JsonDbListModel::partitions_clear(QQmlListProperty<JsonDbPartition> *p)
{
    JsonDbListModel *q = qobject_cast<JsonDbListModel *>(p->object);
    if (q) {
        q->setPartitionNames(QStringList());
        foreach (const QString &partitionName, q->nameToJsonDbPartitionMap.keys()) {
            q->nameToJsonDbPartitionMap.take(partitionName)->deleteLater();
        }
    }
}

void JsonDbListModel::onObjectAvailable(int index, QJsonObject availableObject, QString partitionName)
{
    if (getCallbacks.contains(index)) {
        QVariant object = QVariant (availableObject.toVariantMap());
        JsonDbPartition *partition = new JsonDbPartition(partitionName);
        QJSValue result = g_declEngine->newObject();
        result.setProperty(QLatin1String("object"), g_declEngine->toScriptValue(object));
        result.setProperty(QLatin1String("partition"), g_declEngine->newQObject(partition));

        QMap<int, QJSValue>::iterator callbacksIter = getCallbacks.find(index);

        while ((callbacksIter != getCallbacks.end()) && (callbacksIter.key() == index)) {
            if (callbacksIter.value().isCallable()) {
                QJSValueList args;
                args << QJSValue(index) << result;
                callbacksIter.value().call(args);
            }
            callbacksIter ++;
        }
        getCallbacks.remove(index);
    }
}

#include "moc_jsondblistmodel.cpp"
QT_END_NAMESPACE_JSONDB