summaryrefslogtreecommitdiff
path: root/src/imports/location/qdeclarativegeoserviceprovider.cpp
blob: 28cada247162e7171432bf1910afc02832a98f86 (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtLocation 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$
**
****************************************************************************/

#include "qdeclarativegeoserviceprovider_p.h"
#include "qgeoserviceprovider.h"
#include "qgeocodingmanager.h"
#include "qgeomappingmanager.h"
#include "qgeoroutingmanager.h"
#include <QtCore/QStringList>

#include <QDebug>

QT_BEGIN_NAMESPACE

/*!
    \qmlclass Plugin QDeclarativeGeoServiceProvider
    \inqmlmodule QtLocation 5
    \ingroup qml-QtLocation5-common
    \since QtLocation 5.0

    \brief The Plugin element describes a Location based services plugin.

    A Plugin is necessary in order to make use of many other parts of the Qt
    Location functionality, such as a Map element.

    The simplest way, in many cases, to make use of Plugin is to employ the
    withMapping, withRouting and similar feature properties. When creating a
    Plugin, set the desired features in these properties, and the first
    available service plugin meeting the criteria set will be chosen.

    Alternatively, a Plugin can be instantiated from a function that iterates
    through the contents of availableServiceProviders in order to find
    the desired service plugin.
*/

QDeclarativeGeoServiceProvider::QDeclarativeGeoServiceProvider(QObject *parent)
:   QObject(parent), sharedProvider_(0), supportsGeocoding_(false),
    supportsReverseGeocoding_(false), supportsRouting_(false), supportsMapping_(false),
    supportsPlaces_(false), withGeocoding_(false), withReverseGeocoding_(false),
    withRouting_(false), withMapping_(false), withPlaces_(false), complete_(false)
{
    locales_.append(QLocale().name());
}

QDeclarativeGeoServiceProvider::~QDeclarativeGeoServiceProvider()
{
    delete sharedProvider_;
}

/*!
    \qmlproperty string Plugin::name

    This property holds the name of the plugin.
*/
void QDeclarativeGeoServiceProvider::setName(const QString &name)
{
    if (name_ == name)
        return;

    name_ = name;
    delete sharedProvider_;
    sharedProvider_ = 0;
    if (complete_)
        update();
    emit nameChanged(name_);
}

void QDeclarativeGeoServiceProvider::update()
{
    QGeoServiceProvider *serviceProvider = sharedGeoServiceProvider();
    if (!serviceProvider  || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setSupportsGeocoding(false);
        setSupportsReverseGeocoding(false);
        setSupportsRouting(false);
        setSupportsMapping(false);
        setSupportsPlaces(false);
        setSupportedPlacesFeatures(QDeclarativeGeoServiceProvider::NoPlaceFeatures);
        return;
    }

    if (locales_.isEmpty())
        locales_.append(QLocale().name());

    Q_ASSERT(!locales_.isEmpty());
    QGeocodingManager* geocodingManager = serviceProvider->geocodingManager();
    if (!geocodingManager || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setSupportsGeocoding(false);
        setSupportsReverseGeocoding(false);
    } else {
        setSupportsGeocoding(geocodingManager->supportsGeocoding());
        setSupportsReverseGeocoding(geocodingManager->supportsReverseGeocoding());
        geocodingManager->setLocale(QLocale(locales_.at(0)));
    }


    QGeoRoutingManager* routingManager = serviceProvider->routingManager();
    if (!routingManager  || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setSupportsRouting(false);
    } else {
        setSupportsRouting(true);
        routingManager->setLocale(QLocale(locales_.at(0)));
    }


    QGeoMappingManager* mappingManager = serviceProvider->mappingManager();
    if (!mappingManager  || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setSupportsMapping(false);
    } else {
        setSupportsMapping(true);
        mappingManager->setLocale(QLocale(locales_.at(0)));
    }

    QPlaceManager *placeManager = serviceProvider->placeManager();
    if (!placeManager || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setSupportedPlacesFeatures(QDeclarativeGeoServiceProvider::NoPlaceFeatures);
        setSupportsPlaces(false);
    } else {
        setSupportedPlacesFeatures(static_cast<QDeclarativeGeoServiceProvider::PlacesFeatures> ((int)placeManager->supportedFeatures()));
        setSupportsPlaces(true);

        QList<QLocale> localePreferences;
        foreach (const QString &locale, locales_)
            localePreferences.append(locale);

        placeManager->setLocales(localePreferences);
    }
}

QStringList QDeclarativeGeoServiceProvider::availableServiceProviders()
{
    return QGeoServiceProvider::availableServiceProviders();
}

void QDeclarativeGeoServiceProvider::setSupportsGeocoding(bool supports)
{
    if (supports == supportsGeocoding_)
        return;
    supportsGeocoding_ = supports;
    emit supportsGeocodingChanged();
}

void QDeclarativeGeoServiceProvider::setSupportsReverseGeocoding(bool supports)
{
    if (supports == supportsReverseGeocoding_)
        return;
    supportsReverseGeocoding_ = supports;
    emit supportsReverseGeocodingChanged();
}

void QDeclarativeGeoServiceProvider::setSupportsRouting(bool supports)
{
    if (supports == supportsRouting_)
        return;
    supportsRouting_ = supports;
    emit supportsRoutingChanged();
}

void QDeclarativeGeoServiceProvider::setSupportsMapping(bool supports)
{
    if (supports == supportsMapping_)
        return;
    supportsMapping_ = supports;
    emit supportsMappingChanged();
}

void QDeclarativeGeoServiceProvider::setSupportsPlaces(bool supports)
{
    if (supports == supportsPlaces_)
        return;
    supportsPlaces_ = supports;
    emit supportsPlacesChanged();
}

void QDeclarativeGeoServiceProvider::setSupportedPlacesFeatures(PlacesFeatures placesFeatures)
{
    if (placesFeatures_ == placesFeatures)
        return;
    placesFeatures_ = placesFeatures;
    emit supportedPlacesFeaturesChanged();
}

void QDeclarativeGeoServiceProvider::componentComplete()
{
    complete_ = true;
    if (!name_.isEmpty()) {
        update();
        return;
    }
    if (withGeocoding_ || withReverseGeocoding_ || withRouting_ ||
            withMapping_ || withPlaces_) {
        QStringList providers = QGeoServiceProvider::availableServiceProviders();
        foreach (QString name, providers) {
            // hack: avoid notifying anyone else when we're just iterating
            blockSignals(true);
            setName(name);
            blockSignals(false);

            bool ok = true;
            if (withGeocoding_ && !supportsGeocoding_)
                ok = false;
            if (withReverseGeocoding_ && !supportsReverseGeocoding_)
                ok = false;
            if (withRouting_ && !supportsRouting_)
                ok = false;
            if (withMapping_ && !supportsMapping_)
                ok = false;
            if (withPlaces_ && !supportsPlaces_)
                ok = false;

            if (ok) {
                // run it again to send the notifications
                name_ = "";
                setName(name);
                break;
            }
        }
    }
}

QString QDeclarativeGeoServiceProvider::name() const
{
    return name_;
}

/*!
    \qmlproperty bool Plugin::withGeocoding
    \qmlproperty bool Plugin::withReverseGeocoding
    \qmlproperty bool Plugin::withRouting
    \qmlproperty bool Plugin::withMapping
    \qmlproperty bool Plugin::withPlaces

    These properties indicate the desired properties of a plugin. If at least
    one of these properties is set at the creation of the Plugin element, it
    will automatically work out which plugin to use, as the first available
    plugin that fulfills the set criteria.

    Note that these properties will only have any effect if set at creation
    time.
*/
bool QDeclarativeGeoServiceProvider::withGeocoding() const
{
    return withGeocoding_;
}
bool QDeclarativeGeoServiceProvider::withReverseGeocoding() const
{
    return withReverseGeocoding_;
}
bool QDeclarativeGeoServiceProvider::withRouting() const
{
    return withRouting_;
}
bool QDeclarativeGeoServiceProvider::withMapping() const
{
    return withMapping_;
}
bool QDeclarativeGeoServiceProvider::withPlaces() const
{
    return withPlaces_;
}

void QDeclarativeGeoServiceProvider::setWithGeocoding(bool value)
{
    withGeocoding_ = value;
}
void QDeclarativeGeoServiceProvider::setWithReverseGeocoding(bool value)
{
    withReverseGeocoding_ = value;
}
void QDeclarativeGeoServiceProvider::setWithRouting(bool value)
{
    withRouting_ = value;
}
void QDeclarativeGeoServiceProvider::setWithMapping(bool value)
{
    withMapping_ = value;
}
void QDeclarativeGeoServiceProvider::setWithPlaces(bool value)
{
    withPlaces_ = value;
}

/*!
    \qmlproperty bool Plugin::supportsGeocoding

    This property holds whether plugin supports geocoding.
*/
bool QDeclarativeGeoServiceProvider::supportsGeocoding() const
{
    return supportsGeocoding_;
}

/*!
    \qmlproperty bool Plugin::supportsReverseGeocoding

    This property holds whether plugin supports reverse geocoding.
*/
bool QDeclarativeGeoServiceProvider::supportsReverseGeocoding() const
{
    return supportsReverseGeocoding_;
}

/*!
    \qmlproperty bool Plugin::supportsRouting

    This property holds whether plugin supports routing.
*/
bool QDeclarativeGeoServiceProvider::supportsRouting() const
{
    return supportsRouting_;
}

/*!
    \qmlproperty bool Plugin::supportsMapping

    This property holds whether plugin supports mapping.
*/
bool QDeclarativeGeoServiceProvider::supportsMapping() const
{
    return supportsMapping_;
}

/*!
    \qmlproperty bool Plugin::supportsPlaces

    This property holds whether plugin supports places.
*/
bool QDeclarativeGeoServiceProvider::supportsPlaces() const
{
    return supportsPlaces_;
}

/*!
    \qmlproperty enumeration Plugin::supportedPlacesFeatures

    This property holds a set of flags indicating what Places related features are provided by the
    plugin. It can be a binary concatenation of the following values:

    \table
        \row
            \o Plugin.NoFeatures
            \o No features specified/supported (value: 0x0).
        \row
            \o Plugin.SavePlaceFeature
            \o The plugin can be used to save places (value: 0x1).
        \row
            \o Plugin.RemovePlaceFeature
            \o The plugin can be used to remove places (value: 0x2).
        \row
            \o Plugin.SaveCategoryFeature
            \o The plugin can be used to save categories (value: 0x4).
        \row
            \o Plugin.RemoveCategoryFeature
            \o The plugin can be used to remove categories (value: 0x8).
        \row
            \o Plugin.RecommendationsFeature
            \o The plugin can provide recommendations (value: 0x10).
        \row
            \o Plugin.SearchSuggestionsFeature\
            \o The plugin can be used to provide search term suggestions (value: 0x20).
        \row
            \o Plugin.CorrectionsFeature
            \o The plugin can provide search term corrections (value: 0x40).
        \row
            \o Plugin.LocaleFeature
            \o The plugin can provide place data information localized according to a set of preferred locales (0x80).
        \row
            \o Plugin.NotificationsFeature
            \o The plugin has notfication mechanisms for when places/categories are added/modified/removed (0x100).
    \endtable
*/
QDeclarativeGeoServiceProvider::PlacesFeatures QDeclarativeGeoServiceProvider::supportedPlacesFeatures()
{
    if (!supportsPlaces_)
        return QDeclarativeGeoServiceProvider::NoPlaceFeatures;
    return placesFeatures_;
}

QGeoServiceProvider *QDeclarativeGeoServiceProvider::sharedGeoServiceProvider()
{
    if (!sharedProvider_)
        sharedProvider_ = new QGeoServiceProvider(name(), parameterMap());

    return sharedProvider_;
}

/*!
    \qmlproperty stringlist Plugin::locales

    This property holds a set of locale preferences.  If the first locale cannot be accommodated, then
    the backend falls back to using the second, and so on.  By default the locales property contains the system locale.

    The locales are specified as strings which have the format
    "language[_script][_country]" or "C", where:

    \list
    \i language is a lowercase, two-letter, ISO 639 language code,
    \i script is a titlecase, four-letter, ISO 15924 script code,
    \i country is an uppercase, two- or three-letter, ISO 3166 country code (also "419" as defined by United Nations),
    \endlist


    The following code demonstrates how to set a single and multiple locales:
    \snippet snippets/declarative/plugin.qml Plugin locale
*/
QStringList QDeclarativeGeoServiceProvider::locales() const
{
    return locales_;
}

void QDeclarativeGeoServiceProvider::setLocales(const QStringList &locales)
{
    if (locales_ == locales)
        return;

    locales_ = locales;

    if (locales_.isEmpty())
        locales_.append(QLocale().name());

    if (complete_)
        update();

    emit localesChanged();
}

/*!
    \qmlproperty list<PluginParameter> Plugin::parameters
    \default

    This property holds the list of plugin parameters.
*/
QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter> QDeclarativeGeoServiceProvider::parameters()
{
    return QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter>(this,
            0,
            parameter_append,
            parameter_count,
            parameter_at,
            parameter_clear);
}

void QDeclarativeGeoServiceProvider::parameter_append(QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter> *prop, QDeclarativeGeoServiceProviderParameter *parameter)
{
    static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->parameters_.append(parameter);
    delete static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->sharedProvider_;
    static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->sharedProvider_ = 0;
}

int QDeclarativeGeoServiceProvider::parameter_count(QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter> *prop)
{
    return static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->parameters_.count();
}

QDeclarativeGeoServiceProviderParameter* QDeclarativeGeoServiceProvider::parameter_at(QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter> *prop, int index)
{
    return static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->parameters_[index];
}

void QDeclarativeGeoServiceProvider::parameter_clear(QDeclarativeListProperty<QDeclarativeGeoServiceProviderParameter> *prop)
{
    static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->parameters_.clear();
    delete static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->sharedProvider_;
    static_cast<QDeclarativeGeoServiceProvider*>(prop->object)->sharedProvider_ = 0;
}

QMap<QString, QVariant> QDeclarativeGeoServiceProvider::parameterMap() const
{
    QMap<QString, QVariant> map;

    for(int i = 0; i < parameters_.size(); ++i) {
        QDeclarativeGeoServiceProviderParameter *parameter = parameters_.at(i);
        map.insert(parameter->name(), parameter->value());
    }

    return map;
}
/*******************************************************************************
*******************************************************************************/

/*!
    \qmlclass PluginParameter QDeclarativeGeoServiceProviderParameter
    \inqmlmodule QtLocation 5
    \ingroup qml-QtLocation5-common
    \since QtLocation 5.0

    \brief The PluginParameter element describes the parameter to a \l Plugin.
*/

QDeclarativeGeoServiceProviderParameter::QDeclarativeGeoServiceProviderParameter(QObject *parent)
    : QObject(parent) {}

QDeclarativeGeoServiceProviderParameter::~QDeclarativeGeoServiceProviderParameter() {}

/*!
    \qmlproperty string PluginParameter::name

    This property holds the name of the plugin parameter.
*/
void QDeclarativeGeoServiceProviderParameter::setName(const QString &name)
{
    if (name_ == name)
        return;

    name_ = name;

    emit nameChanged(name_);
}

QString QDeclarativeGeoServiceProviderParameter::name() const
{
    return name_;
}

/*!
    \qmlproperty QVariant PluginParameter::value

    This property holds the value of the plugin parameter.
*/
void QDeclarativeGeoServiceProviderParameter::setValue(const QVariant &value)
{
    if (value_ == value)
        return;

    value_ = value;

    emit valueChanged(value_);
}

QVariant QDeclarativeGeoServiceProviderParameter::value() const
{
    return value_;
}

/*******************************************************************************
*******************************************************************************/

#include "moc_qdeclarativegeoserviceprovider_p.cpp"

QT_END_NAMESPACE