summaryrefslogtreecommitdiffstats
path: root/tests/auto/qplacemanager_jsondb/jsondbutils.cpp
blob: d2a41c0a581584f2dcf8526cb57b11731e2a528f (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the test suite 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$
**
****************************************************************************/

#include "jsondbutils.h"

#include <QtJsonDb/QJsonDbReadRequest>
#include <QtJsonDb/QJsonDbRemoveRequest>
#include <QtJsonDb/QJsonDbWriteRequest>

#include <QDebug>
#include <QMap>
#include <QString>
#include <QStringList>
#include <QLocalSocket>
#include <QDir>
#include <QTest>
#include <QSignalSpy>
#include <QUuid>

#ifndef WAIT_UNTIL
#define WAIT_UNTIL(__expr) \
        do { \
        const int __step = 50; \
        const int __timeout = 10000; \
        if (!(__expr)) { \
            QTest::qWait(0); \
        } \
        for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \
            QTest::qWait(__step); \
        } \
    } while (0)
#endif

const QLatin1String JsonDbUtils::Uuid("_uuid");
const QLatin1String JsonDbUtils::Type("_type");

const QLatin1String JsonDbUtils::Name("displayName");
const QLatin1String JsonDbUtils::PlaceType("com.nokia.mt.location.Place");

//only categories that are directly assigned to a place
const QLatin1String JsonDbUtils::CategoryUuids("categoryUuids");

//all categories that a place belongs to,
//it includes all ancestors of the assigned categories
const QLatin1String JsonDbUtils::AllCategoryUuids("allCategoryUuids");

const QLatin1String JsonDbUtils::CategoryType("com.nokia.mt.location.PlaceCategory");

//includes all ancestor category ids and also the current category id
//as the last element.   The first category is a top level category id.
const QLatin1String JsonDbUtils::Lineage("lineageUuids");

const QLatin1String JsonDbUtils::CategoryParentId("parentUuid");

const QLatin1String JsonDbUtils::NotificationType("notification");
const QLatin1String JsonDbUtils::Actions("actions");

//coord
const QLatin1String JsonDbUtils::Coordinate("geo");
const QLatin1String JsonDbUtils::Latitude("latitude");
const QLatin1String JsonDbUtils::Longitude("longitude");

//address
const QLatin1String JsonDbUtils::Address("address");
const QLatin1String JsonDbUtils::Street("street");
const QLatin1String JsonDbUtils::District("district");
const QLatin1String JsonDbUtils::City("locality");
const QLatin1String JsonDbUtils::County("county");
const QLatin1String JsonDbUtils::State("region");
const QLatin1String JsonDbUtils::Country("country");
const QLatin1String JsonDbUtils::CountryCode("countryCode");
const QLatin1String JsonDbUtils::PostalCode("postalCode");

const QLatin1String JsonDbUtils::Location("location");

//contact details
const QLatin1String JsonDbUtils::Phones("phones");
const QLatin1String JsonDbUtils::Emails("emails");
const QLatin1String JsonDbUtils::Websites("urls");

const QLatin1String JsonDbUtils::PhoneSubType("subType");
const QLatin1String JsonDbUtils::SubTypeFax("fax");
const QLatin1String JsonDbUtils::SubTypeLandline("landline");

const QLatin1String JsonDbUtils::Label("label");
const QLatin1String JsonDbUtils::Value("value");
const QLatin1String JsonDbUtils::Url("url");

const QLatin1String JsonDbUtils::ExtendedAttributes("extendedAttributes");
const QLatin1String JsonDbUtils::Text("text");

const QLatin1String JsonDbUtils::Height("height");
const QLatin1String JsonDbUtils::Width("width");

const QLatin1String JsonDbUtils::Thumbnails("thumbnails");
const QLatin1String JsonDbUtils::Small("small");
const QLatin1String JsonDbUtils::Medium("medium");
const QLatin1String JsonDbUtils::Large("large");
const QLatin1String JsonDbUtils::Fullscreen("fullscreen");

const QLatin1String JsonDbUtils::SmallIconParam("smallUrl");
const QLatin1String JsonDbUtils::MediumIconParam("mediumUrl");
const QLatin1String JsonDbUtils::LargeIconParam("largeUrl");
const QLatin1String JsonDbUtils::FullscreenIconParam("fullscreenUrl");

const QLatin1String JsonDbUtils::SmallIconSizeParam("smallSize");
const QLatin1String JsonDbUtils::MediumIconSizeParam("mediumSize");
const QLatin1String JsonDbUtils::LargeIconSizeParam("largeSize");
const QLatin1String JsonDbUtils::FullscreenIconSizeParam("fullscreenSize");

const QLatin1String JsonDbUtils::CreatedDateTime("createdDateTime");
const QLatin1String JsonDbUtils::ModifiedDateTime("modifiedDateTime");

const QLatin1String JsonDbUtils::DefaultPartition("");
const QLatin1String JsonDbUtils::PartitionType("Partition");

const QLatin1String JsonDbUtils::LongitudeIndex("placeLongitudeIndex");
const QLatin1String JsonDbUtils::LatitudeIndex("placeLatitudeIndex");
const QLatin1String JsonDbUtils::PlaceNameIndex("placeDisplayNameIndex");

JsonDbUtils::JsonDbUtils(QObject *parent)
    : QObject(parent)
{
    QStringList args;
    args <<"-reject-stale-updates";
    m_jsondbProcess = launchJsonDbDaemon(args);
    m_connection = new QJsonDbConnection;
    m_connection->connectToServer();
}

JsonDbUtils::~JsonDbUtils()
{
    if (m_jsondbProcess)
        m_jsondbProcess->kill();
}

void JsonDbUtils::cleanDb()
{
    QJsonDbReadRequest *getPlacesRequest = new QJsonDbReadRequest(this);
    getPlacesRequest->setPartition(m_currentPartition);
    getPlacesRequest->setQuery(QString::fromLatin1("[?%1=\"%2\"]")
                               .arg(Type).arg(PlaceType));

    setupRequest(getPlacesRequest, this, SLOT(getPlacesFinished()));
    m_connection->send(getPlacesRequest);
}

void JsonDbUtils::sendRequest(QJsonDbRequest *request)
{
    connect(request, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
            this, SLOT(requestError(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
    m_connection->send(request);
}

QList<QJsonObject> JsonDbUtils::results()
{
    return m_results;
}

void JsonDbUtils::fetchPlaceJson(const QString &uuid)
{
    QJsonDbReadRequest *request = new QJsonDbReadRequest(this);
    request->setPartition(m_currentPartition);
    request->setQuery(QStringLiteral("[?_type=%type][?_uuid=%uuid]"));
    request->bindValue(QStringLiteral("type"), PlaceType);
    request->bindValue(QStringLiteral("uuid"), uuid);
    setupRequest(request, this, SLOT(fetchPlaceJsonFinished()));
    m_connection->send(request);
}

void JsonDbUtils::savePlaceJson(const QJsonObject &object)
{
    QJsonDbWriteRequest *request = new QJsonDbWriteRequest(this);
    request->setPartition(m_currentPartition);
    QList<QJsonObject> objects;
    objects << object;
    request->setObjects(objects);
    setupRequest(request, this, SLOT(savePlaceJsonFinished()));
    m_connection->send(request);
}

bool JsonDbUtils::hasJsonDbConnection() const
{
    return  m_jsondbProcess &&
            m_connection &&
            m_connection->status() == QtJsonDb::QJsonDbConnection::Connected;
}

void JsonDbUtils::setCurrentPartition(const QString &partition)
{
    m_currentPartition = partition;
}

void JsonDbUtils::setupPartition(const QString &partition)
{
    QJsonDbReadRequest *readPartitionRequest = new QJsonDbReadRequest();
    readPartitionRequest->setQuery(QStringLiteral("[?_type=%type][?name=%name]"));
    readPartitionRequest->bindValue(QStringLiteral("type"), JsonDbUtils::PartitionType);
    readPartitionRequest->bindValue(QStringLiteral("name"), partition);
    QSignalSpy readSpy(readPartitionRequest, SIGNAL(finished()));
    sendRequest(readPartitionRequest);
    WAIT_UNTIL(readSpy.count() == 1);
    if (readSpy.isEmpty()) {
        qWarning() << "No finished signal emitted when trying to read partition: " << partition;
        return;
    }

    QList<QJsonObject> results = readPartitionRequest->takeResults();
    if (results.isEmpty()) {
        //create user partition because it doesn't exist
        QJsonObject userPartition;
        userPartition.insert(JsonDbUtils::Uuid, QUuid::createUuid().toString());
        userPartition.insert(JsonDbUtils::Type, JsonDbUtils::PartitionType);
        userPartition.insert(QLatin1String("name"), partition);

        QJsonDbWriteRequest *writePartitionRequest = new QJsonDbWriteRequest(this);
        QList<QJsonObject> objects;
        objects << userPartition;
        writePartitionRequest->setObjects(objects);

        QSignalSpy writeSpy(writePartitionRequest, SIGNAL(finished()));
        sendRequest(writePartitionRequest);
        WAIT_UNTIL(writeSpy.count() == 1);
        if (writeSpy.isEmpty()) {
            qWarning() << "No finished signal emitted when trying to create partition: " << partition;
            return;
        }
    }

    QMetaObject::invokeMethod(this, "partitionSetupDone", Qt::QueuedConnection);
}

void JsonDbUtils::getPlacesFinished()
{
    QList<QJsonObject> results = qobject_cast<QJsonDbReadRequest *>(sender())
                                    ->takeResults();

    if (!results.isEmpty()) {
        QJsonDbRemoveRequest *removePlacesRequest
                = new QJsonDbRemoveRequest(results, this);
        setupRequest(removePlacesRequest,this, SLOT(removePlacesFinished()));
        m_connection->send(removePlacesRequest);
    } else {
        removePlacesFinished();
    }
}

void JsonDbUtils::removePlacesFinished()
{
    QJsonDbReadRequest *getCategoriesRequest = new QJsonDbReadRequest(this);
    getCategoriesRequest->setPartition(m_currentPartition);
    getCategoriesRequest->setQuery(QString::fromLatin1("[?%1=\"%2\"]")
                                   .arg(Type).arg(CategoryType));
    setupRequest(getCategoriesRequest,this, SLOT(getCategoriesFinished()));
    m_connection->send(getCategoriesRequest);
}

void JsonDbUtils::getCategoriesFinished()
{
    QList<QJsonObject> results = qobject_cast<QJsonDbReadRequest *>(sender())
                                    ->takeResults();
    if (!results.isEmpty()) {
        QJsonDbRemoveRequest *removeCategoriesRequest
                = new QJsonDbRemoveRequest(results, this);
        setupRequest(removeCategoriesRequest, this, SLOT(removeCategoriesFinished()));
        m_connection->send(removeCategoriesRequest);
    } else {
        removeCategoriesFinished();
    }
}

void JsonDbUtils::removeCategoriesFinished()
{
    emit dbCleaned();
}

void JsonDbUtils::fetchPlaceJsonFinished()
{
    QJsonDbRequest *request = qobject_cast<QJsonDbRequest *>(sender());
    QList<QJsonObject> results = request->takeResults();
    if (results.count() == 1)
        emit placeFetched(results.first());
    else
        emit placeFetched(QJsonObject());
}

void JsonDbUtils::savePlaceJsonFinished()
{
    emit placeSaved();
}

void JsonDbUtils::requestError(QJsonDbRequest::ErrorCode error,
                               const QString &errorString)
{
    qWarning() << Q_FUNC_INFO << QStringLiteral(" Error code: ") << error
             << QStringLiteral(" Error String: ") << errorString;
}

void JsonDbUtils::setupRequest(QJsonDbRequest *request, QObject *parent,
                                  const char *slot)
{
    Q_ASSERT(request);
    Q_ASSERT(parent);
    Q_ASSERT(slot);

    QObject::connect(request, SIGNAL(finished()), parent, slot);

    QObject::connect(request, SIGNAL(finished()), request, SLOT(deleteLater()));
    QObject::connect(request,
                     SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                     parent,
                     SLOT(requestError(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
    QObject::connect(request,
                     SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                     request,
                     SLOT(deleteLater()));
    request->setPartition(m_currentPartition);
}

QProcess* JsonDbUtils::launchJsonDbDaemon(const QStringList &args)
{
    const QString socketName = QString("tst_qplacemanger_jsondb_%1").arg(getpid());
    QString jsondb_app = QString::fromLocal8Bit(JSONDB_DAEMON_BASE) + QDir::separator() + "jsondb";
    if (!QFile::exists(jsondb_app))
        jsondb_app = QLatin1String("jsondb"); // rely on the PATH

    QProcess *process = new QProcess;
    process->setProcessChannelMode( QProcess::ForwardedChannels );

    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert("JSONDB_SOCKET", socketName);
    process->setProcessEnvironment(env);
    ::setenv("JSONDB_SOCKET", qPrintable(socketName), 1);
    qDebug() << "Starting process" << jsondb_app << args << "with socket" << socketName;
    process->start(jsondb_app, args);
    if (!process->waitForStarted())
        qFatal("Unable to start jsondb database process");

    /* Wait until the jsondb is accepting connections */
    int tries = 0;
    bool connected = false;
    while (!connected && tries++ < 100) {
        QLocalSocket socket;
        socket.connectToServer(socketName);
        if (socket.waitForConnected())
            connected = true;
        QTest::qWait(250);
    }

    if (!connected)
        qFatal("Unable to connect to jsondb process");

    //Start jsondb-client and get it to load a prepopulation file that contains
    //Indexes
    QProcess *prepopulateProcess = new QProcess;
    prepopulateProcess->setProcessEnvironment(env);

    QString jsondb_client = QString::fromLocal8Bit(JSONDB_DAEMON_BASE)
                            + QDir::separator() + "jsondb-client";
    if (!QFile::exists(jsondb_client))
        jsondb_client = QLatin1String("jsondb-client"); // rely on the PATH
    QStringList clientArgs;
    clientArgs << QLatin1String("-load");
    clientArgs << QLatin1String(JSONFILE);
    clientArgs << QLatin1String("-terminate");
    prepopulateProcess->setProcessChannelMode( QProcess::ForwardedChannels);
    prepopulateProcess->start(jsondb_client, clientArgs);

    if (!prepopulateProcess->waitForFinished())
        qFatal("Unable to complete loading index via jsondb-client");

    return process;
}