summaryrefslogtreecommitdiff
path: root/src/location/declarativeplaces/qdeclarativeplace.cpp
blob: f4b14f9c954b3119383751e1bbaf251da8344265 (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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativeplace_p.h"
#include "qdeclarativecontactdetails_p.h"
#include "qdeclarativegeoserviceprovider_p.h"
#include "error_messages_p.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QMetaObject>
#include <QtQml/QQmlEngine>
#include <QtQml/QQmlInfo>
#include <QtLocation/QGeoServiceProvider>
#include <QtLocation/QPlaceAttribute>
#include <QtLocation/QPlaceManager>
#include <QtLocation/QPlaceDetailsReply>
#include <QtLocation/QPlaceReply>
#include <QtLocation/QPlaceIdReply>
#include <QtLocation/QPlaceContactDetail>

QT_BEGIN_NAMESPACE

/*!
    \qmltype Place
    \instantiates QDeclarativePlace
    \inqmlmodule QtLocation
    \ingroup qml-QtLocation5-places
    \ingroup qml-QtLocation5-places-data
    \since QtLocation 5.5

    \brief The Place type represents a location that is a position of interest.

    The Place type represents a physical location with additional metadata describing that
    location.  Contrasted with \l Location, \l Address, and
    \l {coordinate} type which are used to describe where a location is.
    The basic properties of a Place are its \l name and \l location.

    Place objects are typically obtained from a search model and will generally only have their
    basic properties set. The \l detailsFetched property can be used to test if further property
    values need to be fetched from the \l Plugin. This can be done by invoking the \l getDetails()
    method.  Progress of the fetching operation can be monitored with the \l status property, which
    will be set to Place.Fetching when the details are being fetched.

    The Place type has many properties holding information about the location. Details on how to
    contact the place are available from the \l contactDetails property.  Convenience properties
    for obtaining the primary \l {primaryPhone}{phone}, \l {primaryFax}{fax},
    \l {primaryEmail}{email} and \l {primaryWebsite}{website} are also available.

    Each place is assigned zero or more \l categories.  Categories are typically used when
    searching for a particular kind of place, such as a restaurant or hotel.  Some places have a
    \l ratings object, which gives an indication of the quality of the place.

    Place metadata is provided by a \l supplier who may require that an \l attribution message be
    displayed to the user when the place details are viewed.

    Places have an associated \l icon which can be used to represent a place on a map or to
    decorate a delegate in a view.

    Places may have additional rich content associated with them.  The currently supported rich
    content include editorial descriptions, reviews and images.  These are exposed as a set of
    models for retrieving the content.  Editorial descriptions of the place are available from the
    \l editorialModel property.  Reviews of the place are available from the \l reviewModel
    property.  A gallery of pictures of the place can be accessed using the \l imageModel property.

    Places may have additional attributes which are not covered in the formal API.  The
    \l extendedAttributes property provides access to these.  The type of extended attributes
    available is specific to each \l Plugin.

    A Place is almost always tied to a \l plugin.  The \l plugin property must be set before it is
    possible to call \l save(), \l remove() or \l getDetails().  The \l reviewModel, \l imageModel
    and \l editorialModel are only valid then the \l plugin property is set.

    \section2 Saving a Place

    If the \l Plugin supports it, the Place type can be used to save a place.  First create a new
    Place and set its properties:

    \snippet declarative/places.qml Place savePlace def

    Then invoke the \l save() method:

    \snippet declarative/places.qml Place savePlace

    The \l status property will change to Place.Saving and then to Place.Ready if the save was
    successful or to Place.Error if an error occurs.

    If the \l placeId property is set, the backend will update an existing place otherwise it will
    create a new place.  On success the \l placeId property will be updated with the identifier of the newly
    saved place.

    \section3 Caveats
    \input place-caveats.qdocinc

    \section3 Saving Between Plugins
    When saving places between plugins, there are a few things to be aware of.
    Some fields of a place such as the id, categories and icons are plugin specific entities. For example
    the categories in one manager may not be recognised in another.
    Therefore trying to save a place directly from one plugin to another is not possible.

    It is generally recommended that saving across plugins be handled as saving \l {Favorites}{favorites}
    as explained in the Favorites section.  However there is another approach which is to create a new place,
    set its (destination) plugin and then use the \l copyFrom() method to copy the details of the original place.
    Using \l copyFrom() only copies data that is supported by the destination plugin,
    plugin specific data such as the place identifier is not copied over. Once the copy is done,
    the place is in a suitable state to be saved.

    The following snippet provides an example of saving a place to a different plugin
    using the \l copyFrom method:

    \snippet declarative/places.qml Place save to different plugin

    \section2 Removing a Place

    To remove a place, ensure that a Place object with a valid \l placeId property exists and call
    its \l remove() method.  The \l status property will change to Place.Removing and then to
    Place.Ready if the save was successful or to Place.Error if an error occurs.

    \section2 Favorites
    The Places API supports the concept of favorites. Favorites are generally implemented
    by using two plugins, the first plugin is typically a read-only source of places (origin plugin) and a second
    read/write plugin (destination plugin) is used to store places from the origin as favorites.

    Each Place has a favorite property which is intended to contain the corresponding place
    from the destination plugin (the place itself is sourced from the origin plugin).  Because both the original
    place and favorite instances are available, the developer can choose which
    properties to show to the user. For example the favorite may have a modified name which should
    be displayed rather than the original name.

    \snippet declarative/places.qml Place favorite

    The following demonstrates how to save a new favorite instance.  A call is made
    to create/initialize the favorite instance and then the instance is saved.

    \snippet declarative/places.qml Place saveFavorite

    The following demonstrates favorite removal:

    \snippet declarative/places.qml Place removeFavorite 1
    \dots
    \snippet declarative/places.qml Place removeFavorite 2

    The PlaceSearchModel has a favoritesPlugin property.  If the property is set, any places found
    during a search are checked against the favoritesPlugin to see if there is a corresponding
    favorite place.  If so, the favorite property of the Place is set, otherwise the favorite
    property is remains null.

    \sa PlaceSearchModel
*/

QDeclarativePlace::QDeclarativePlace(QObject *parent)
:   QObject(parent),
    m_extendedAttributes(new QQmlPropertyMap(this)),
    m_contactDetails(new QDeclarativeContactDetails(this))
{
    connect(m_contactDetails, &QDeclarativeContactDetails::valueChanged,
            this, &QDeclarativePlace::contactsModified);

    setPlace(QPlace());
}

QDeclarativePlace::QDeclarativePlace(const QPlace &src, QDeclarativeGeoServiceProvider *plugin, QObject *parent)
:   QObject(parent),
    m_extendedAttributes(new QQmlPropertyMap(this)),
    m_contactDetails(new QDeclarativeContactDetails(this)),
    m_plugin(plugin)
{
    Q_ASSERT(plugin);

    connect(m_contactDetails, &QDeclarativeContactDetails::valueChanged,
            this, &QDeclarativePlace::contactsModified);

    setPlace(src);
}

QDeclarativePlace::~QDeclarativePlace()
{
}

// From QQmlParserStatus
void QDeclarativePlace::componentComplete()
{
    m_complete = true;
}

/*!
    \qmlproperty Plugin Place::plugin

    This property holds the \l Plugin that provided this place which can be used to retrieve more information about the service.
*/
void QDeclarativePlace::setPlugin(QDeclarativeGeoServiceProvider *plugin)
{
    if (m_plugin == plugin)
        return;

    m_plugin = plugin;
    if (m_complete)
        emit pluginChanged();

    if (m_plugin->isAttached()) {
        pluginReady();
    } else {
        connect(m_plugin, &QDeclarativeGeoServiceProvider::attached,
                this, &QDeclarativePlace::pluginReady);
    }
}

void QDeclarativePlace::pluginReady()
{
    QGeoServiceProvider *serviceProvider = m_plugin->sharedGeoServiceProvider();
    QPlaceManager *placeManager = serviceProvider->placeManager();
    if (!placeManager || serviceProvider->error() != QGeoServiceProvider::NoError) {
        setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, PLUGIN_ERROR)
                         .arg(m_plugin->name()).arg(serviceProvider->errorString()));
        return;
    }
}

QDeclarativeGeoServiceProvider *QDeclarativePlace::plugin() const
{
    return m_plugin;
}

/*!
    \qmlproperty ReviewModel Place::reviewModel

    This property holds a model which can be used to retrieve reviews about the place.
*/
QDeclarativePlaceReviewModel *QDeclarativePlace::reviewModel()
{
    if (!m_reviewModel) {
        m_reviewModel = new QDeclarativePlaceReviewModel(this);
        m_reviewModel->setPlace(this);
    }

    return m_reviewModel;
}

/*!
    \qmlproperty ImageModel Place::imageModel

    This property holds a model which can be used to retrieve images of the place.
*/
QDeclarativePlaceImageModel *QDeclarativePlace::imageModel()
{
    if (!m_imageModel) {
        m_imageModel = new QDeclarativePlaceImageModel(this);
        m_imageModel->setPlace(this);
    }

    return m_imageModel;
}

/*!
    \qmlproperty EditorialModel Place::editorialModel

    This property holds a model which can be used to retrieve editorial descriptions of the place.
*/
QDeclarativePlaceEditorialModel *QDeclarativePlace::editorialModel()
{
    if (!m_editorialModel) {
        m_editorialModel = new QDeclarativePlaceEditorialModel(this);
        m_editorialModel->setPlace(this);
    }

    return m_editorialModel;
}

/*!
    \qmlproperty QPlace Place::place

    For details on how to use this property to interface between C++ and QML see
    "\l {Place - QPlace} {Interfaces between C++ and QML Code}".
*/
void QDeclarativePlace::setPlace(const QPlace &src)
{
    QPlace previous = m_src;
    m_src = src;

    if (previous.categories() != m_src.categories()) {
        synchronizeCategories();
        emit categoriesChanged();
    }

    if (m_location && m_location->parent() == this) {
        m_location->setLocation(m_src.location());
    } else if (!m_location || m_location->parent() != this) {
        m_location = new QDeclarativeGeoLocation(m_src.location(), this);
        emit locationChanged();
    }

    if (previous.ratings() != m_src.ratings())
        emit ratingsChanged();
    if (previous.supplier() != m_src.supplier())
        emit supplierChanged();
    if (previous.icon() != m_src.icon())
        emit iconChanged();
    if (previous.name() != m_src.name())
        emit nameChanged();
    if (previous.placeId() != m_src.placeId())
        emit placeIdChanged();
    if (previous.attribution() != m_src.attribution())
        emit attributionChanged();
    if (previous.detailsFetched() != m_src.detailsFetched())
        emit detailsFetchedChanged();
    if (previous.primaryPhone() != m_src.primaryPhone())
        emit primaryPhoneChanged();
    if (previous.primaryFax() != m_src.primaryFax())
        emit primaryFaxChanged();
    if (previous.primaryEmail() != m_src.primaryEmail())
        emit primaryEmailChanged();
    if (previous.primaryWebsite() != m_src.primaryWebsite())
        emit primaryWebsiteChanged();

    if (m_reviewModel && m_src.totalContentCount(QPlaceContent::ReviewType) >= 0) {
        m_reviewModel->initializeCollection(m_src.totalContentCount(QPlaceContent::ReviewType),
                                            m_src.content(QPlaceContent::ReviewType));
    }
    if (m_imageModel && m_src.totalContentCount(QPlaceContent::ImageType) >= 0) {
        m_imageModel->initializeCollection(m_src.totalContentCount(QPlaceContent::ImageType),
                                           m_src.content(QPlaceContent::ImageType));
    }
    if (m_editorialModel && m_src.totalContentCount(QPlaceContent::EditorialType) >= 0) {
        m_editorialModel->initializeCollection(m_src.totalContentCount(QPlaceContent::EditorialType),
                                               m_src.content(QPlaceContent::EditorialType));
    }

    pullExtendedAttributes();
    synchronizeContacts();
}

QPlace QDeclarativePlace::place() const
{
    // The properties handled explicirly here are not stored in m_src, but
    // but are instead stored in QDeclarative* objects which we need to update
    // explicitly.

    QPlace result = m_src;

    // Categories
    QList<QPlaceCategory> categories;
    for (QDeclarativeCategory *value : qAsConst(m_categories))
        categories.append(value->category());

    result.setCategories(categories);

    // Location
    result.setLocation(m_location ? m_location->location() : QGeoLocation());

    //contact details
    QList<QPlaceContactDetail> cppDetails;
    for (const QString &key : m_contactDetails->keys()) {
        cppDetails.clear();
        if (m_contactDetails->value(key).typeId() == QMetaType::QVariantList) {
            const QVariantList detailsVarList = m_contactDetails->value(key).toList();
            for (const QVariant &detailVar : detailsVarList)
                cppDetails.append(detailVar.value<QPlaceContactDetail>());
        } else {
            cppDetails.append(m_contactDetails->value(key).value<QPlaceContactDetail>());
        }
        result.setContactDetails(key, cppDetails);
    }

    return result;
}

/*!
    \qmlproperty QtPositioning::Location Place::location

    This property holds the location of the place which can be used to retrieve the coordinate,
    address and the bounding box.
*/
void QDeclarativePlace::setLocation(QDeclarativeGeoLocation *location)
{
    if (m_location == location)
        return;

    if (m_location && m_location->parent() == this)
        delete m_location;

    m_location = location;
    emit locationChanged();
}

QDeclarativeGeoLocation *QDeclarativePlace::location() const
{
    return m_location;
}

/*!
    \qmlproperty Ratings Place::ratings

    This property holds ratings of the place.  The ratings provide an indication of the quality of a
    place.
*/
void QDeclarativePlace::setRatings(const QPlaceRatings &rating)
{
    if (m_src.ratings() != rating) {
        m_src.setRatings(rating);
        emit ratingsChanged();
    }
}

QPlaceRatings QDeclarativePlace::ratings() const
{
    return m_src.ratings();
}

/*!
    \qmlproperty Supplier Place::supplier

    This property holds the supplier of the place data.
    The supplier is typically a business or organization that collected the data about the place.
*/
void QDeclarativePlace::setSupplier(const QPlaceSupplier &supplier)
{
    if (m_src.supplier() != supplier) {
        m_src.setSupplier(supplier);
        emit supplierChanged();
    }
}

QPlaceSupplier QDeclarativePlace::supplier() const
{
    return m_src.supplier();
}

/*!
    \qmlproperty Icon Place::icon

    This property holds a graphical icon which can be used to represent the place.
*/
QPlaceIcon QDeclarativePlace::icon() const
{
    return m_src.icon();
}

void QDeclarativePlace::setIcon(const QPlaceIcon &icon)
{
    if (m_src.icon() != icon) {
        m_src.setIcon(icon);
        emit iconChanged();
    }
}

/*!
    \qmlproperty string Place::name

    This property holds the name of the place which can be used to represent the place.
*/
void QDeclarativePlace::setName(const QString &name)
{
    if (m_src.name() != name) {
        m_src.setName(name);
        emit nameChanged();
    }
}

QString QDeclarativePlace::name() const
{
    return m_src.name();
}

/*!
    \qmlproperty string Place::placeId

    This property holds the unique identifier of the place.  The place identifier is only meaningful to the
    \l Plugin that generated it and is not transferable between \l {Plugin}{Plugins}.  The place id
    is not guaranteed to be universally unique, but unique within the \l Plugin that generated it.

    If only the place identifier is known, all other place data can fetched from the \l Plugin.

    \snippet declarative/places.qml Place placeId
*/
void QDeclarativePlace::setPlaceId(const QString &placeId)
{
    if (m_src.placeId() != placeId) {
        m_src.setPlaceId(placeId);
        emit placeIdChanged();
    }
}

QString QDeclarativePlace::placeId() const
{
    return m_src.placeId();
}

/*!
    \qmlproperty string Place::attribution

    This property holds a rich text attribution string for the place.
    Some providers may require that the attribution be shown to the user
    whenever a place is displayed.  The contents of this property should
    be shown to the user if it is not empty.
*/
void QDeclarativePlace::setAttribution(const QString &attribution)
{
    if (m_src.attribution() != attribution) {
        m_src.setAttribution(attribution);
        emit attributionChanged();
    }
}

QString QDeclarativePlace::attribution() const
{
    return m_src.attribution();
}

/*!
    \qmlproperty bool Place::detailsFetched

    This property indicates whether the details of the place have been fetched.  If this property
    is false, the place details have not yet been fetched.  Fetching can be done by invoking the
    \l getDetails() method.

    \sa getDetails()
*/
bool QDeclarativePlace::detailsFetched() const
{
    return m_src.detailsFetched();
}

/*!
    \qmlproperty enumeration Place::status

    This property holds the status of the place.  It can be one of:

    \table
        \row
            \li Place.Ready
            \li No error occurred during the last operation, further operations may be performed on
               the place.
        \row
            \li Place.Saving
            \li The place is currently being saved, no other operation may be performed until
               complete.
        \row
            \li Place.Fetching
            \li The place details are currently being fetched, no other operations may be performed
               until complete.
        \row
            \li Place.Removing
            \li The place is currently being removed, no other operations can be performed until
               complete.
        \row
            \li Place.Error
            \li An error occurred during the last operation, further operations can still be
               performed on the place.
    \endtable

    The status of a place can be checked by connecting the status property
    to a handler function, and then have the handler function process the change
    in status.

    \snippet declarative/places.qml Place checkStatus
    \dots
    \snippet declarative/places.qml Place checkStatus handler

*/
void QDeclarativePlace::setStatus(Status status, const QString &errorString)
{
    Status originalStatus = m_status;
    m_status = status;
    m_errorString = errorString;

    if (originalStatus != m_status)
        emit statusChanged();
}

QDeclarativePlace::Status QDeclarativePlace::status() const
{
    return m_status;
}

/*!
    \internal
*/
void QDeclarativePlace::finished()
{
    if (!m_reply)
        return;

    if (m_reply->error() == QPlaceReply::NoError) {
        switch (m_reply->type()) {
        case (QPlaceReply::IdReply) : {
            QPlaceIdReply *idReply = qobject_cast<QPlaceIdReply *>(m_reply);

            switch (idReply->operationType()) {
            case QPlaceIdReply::SavePlace:
                setPlaceId(idReply->id());
                break;
            case QPlaceIdReply::RemovePlace:
                break;
            default:
                //Other operation types shouldn't ever be received.
                break;
            }
            break;
        }
        case (QPlaceReply::DetailsReply): {
            QPlaceDetailsReply *detailsReply = qobject_cast<QPlaceDetailsReply *>(m_reply);
            setPlace(detailsReply->place());
            break;
        }
        default:
            //other types of replies shouldn't ever be received.
            break;
        }

        m_errorString.clear();

        m_reply->deleteLater();
        m_reply = nullptr;

        setStatus(QDeclarativePlace::Ready);
    } else {
        QString errorString = m_reply->errorString();

        m_reply->deleteLater();
        m_reply = nullptr;

        setStatus(QDeclarativePlace::Error, errorString);
    }
}

/*!
    \internal
*/
void QDeclarativePlace::contactsModified(const QString &key, const QVariant &)
{
    primarySignalsEmission(key);
}

/*!
    \internal
*/
void QDeclarativePlace::cleanupDeletedCategories()
{
    for (QDeclarativeCategory * category : m_categoriesToBeDeleted) {
        if (category->parent() == this)
            delete category;
    }
    m_categoriesToBeDeleted.clear();
}

/*!
    \qmlmethod void Place::getDetails()

    This method starts fetching place details.

    The \l status property will change to Place.Fetching while the fetch is in progress.  On
    success the object's properties will be updated, \l status will be set to Place.Ready and
    \l detailsFetched will be set to true.  On error \l status will be set to Place.Error.  The
    \l errorString() method can be used to get the details of the error.
*/
void QDeclarativePlace::getDetails()
{
    QPlaceManager *placeManager = manager();
    if (!placeManager)
        return;

    m_reply = placeManager->getPlaceDetails(placeId());
    connect(m_reply, &QPlaceReply::finished, this, &QDeclarativePlace::finished);
    setStatus(QDeclarativePlace::Fetching);
}

/*!
    \qmlmethod void Place::save()

    This method performs a save operation on the place.

    The \l status property will change to Place.Saving while the save operation is in progress.  On
    success the \l status will be set to Place.Ready.  On error \l status will be set to Place.Error.
    The \l errorString() method can be used to get the details of the error.

    If the \l placeId property was previously empty, it will be assigned a valid value automatically
    during a successful save operation.

    Note that a \l PlaceSearchModel will call Place::getDetails on any place that it detects an update
    on.  A consequence of this is that whenever a Place from a \l PlaceSearchModel is successfully saved,
    it will be followed by a fetch of place details, leading to a sequence of state changes
    of \c Saving, \c Ready, \c Fetching, \c Ready.

*/
void QDeclarativePlace::save()
{
    QPlaceManager *placeManager = manager();
    if (!placeManager)
        return;

    m_reply = placeManager->savePlace(place());
    connect(m_reply, &QPlaceReply::finished, this, &QDeclarativePlace::finished);
    setStatus(QDeclarativePlace::Saving);
}

/*!
    \qmlmethod void Place::remove()

    This method performs a remove operation on the place.

    The \l status property will change to Place.Removing while the save operation is in progress.
    On success \l status will be set to Place.Ready.  On error \l status will be set to
    Place.Error.  The \l errorString() method can be used to get the details of the error.
*/
void QDeclarativePlace::remove()
{
    QPlaceManager *placeManager = manager();
    if (!placeManager)
        return;

    m_reply = placeManager->removePlace(place().placeId());
    connect(m_reply, &QPlaceReply::finished, this, &QDeclarativePlace::finished);
    setStatus(QDeclarativePlace::Removing);
}

/*!
    \qmlmethod string Place::errorString()

    Returns a string description of the error of the last operation.  If the last operation
    completed successfully then the string is empty.
*/
QString QDeclarativePlace::errorString() const
{
    return m_errorString;
}

/*!
    \qmlproperty string Place::primaryPhone

    This property holds the primary phone number of the place.  If no "phone" contact detail is
    defined for this place, this property will be an empty string.  It is equivalent to:


    \snippet declarative/places.qml Place primaryPhone
*/
QString QDeclarativePlace::primaryPhone() const
{
    return primaryValue(QPlaceContactDetail::Phone);
}

/*!
    \qmlproperty string Place::primaryFax

    This property holds the primary fax number of the place.  If no "fax" contact detail is
    defined for this place this property will be an empty string.  It is equivalent to

    \snippet declarative/places.qml Place primaryFax
*/
QString QDeclarativePlace::primaryFax() const
{
    return primaryValue(QPlaceContactDetail::Fax);
}

/*!
    \qmlproperty string Place::primaryEmail

    This property holds the primary email address of the place.  If no "email" contact detail is
    defined for this place this property will be an empty string.  It is equivalent to

    \snippet declarative/places.qml Place primaryEmail
*/
QString QDeclarativePlace::primaryEmail() const
{
    return primaryValue(QPlaceContactDetail::Email);
}

/*!
    \qmlproperty string Place::primaryWebsite

    This property holds the primary website url of the place.  If no "website" contact detail is
    defined for this place this property will be an empty string.  It is equivalent to

    \snippet declarative/places.qml Place primaryWebsite
*/

QUrl QDeclarativePlace::primaryWebsite() const
{
    return QUrl(primaryValue(QPlaceContactDetail::Website));
}

/*!
    \qmlproperty ExtendedAttributes Place::extendedAttributes

    This property holds the extended attributes of a place.  Extended attributes are additional
    information about a place not covered by the place's properties.
*/
QQmlPropertyMap *QDeclarativePlace::extendedAttributes() const
{
    return m_extendedAttributes;
}

/*!
    \qmlproperty ContactDetails Place::contactDetails

    This property holds the contact information for this place, for example a phone number or
    a website URL.  This property is a map of \l ContactDetail objects.
*/
QDeclarativeContactDetails *QDeclarativePlace::contactDetails() const
{
    return m_contactDetails;
}

/*!
    \qmlproperty list<Category> Place::categories

    This property holds the list of categories this place is a member of.  The categories that can
    be assigned to a place are specific to each \l plugin.
*/
QQmlListProperty<QDeclarativeCategory> QDeclarativePlace::categories()
{
    return QQmlListProperty<QDeclarativeCategory>(this,
                                                          0, // opaque data parameter
                                                          category_append,
                                                          category_count,
                                                          category_at,
                                                          category_clear);
}

/*!
    \internal
*/
void QDeclarativePlace::category_append(QQmlListProperty<QDeclarativeCategory> *prop,
                                                  QDeclarativeCategory *value)
{
    QDeclarativePlace *object = static_cast<QDeclarativePlace *>(prop->object);

    if (object->m_categoriesToBeDeleted.contains(value))
        object->m_categoriesToBeDeleted.removeAll(value);

    if (!object->m_categories.contains(value)) {
        object->m_categories.append(value);
        QList<QPlaceCategory> list = object->m_src.categories();
        list.append(value->category());
        object->m_src.setCategories(list);

        emit object->categoriesChanged();
    }
}

/*!
    \internal
*/
qsizetype QDeclarativePlace::category_count(QQmlListProperty<QDeclarativeCategory> *prop)
{
    return static_cast<QDeclarativePlace *>(prop->object)->m_categories.count();
}

/*!
    \internal
*/
QDeclarativeCategory *QDeclarativePlace::category_at(QQmlListProperty<QDeclarativeCategory> *prop,
                                                     qsizetype index)
{
    QDeclarativePlace *object = static_cast<QDeclarativePlace *>(prop->object);
    QDeclarativeCategory *res = NULL;
    if (object->m_categories.count() > index && index > -1) {
        res = object->m_categories[index];
    }
    return res;
}

/*!
    \internal
*/
void QDeclarativePlace::category_clear(QQmlListProperty<QDeclarativeCategory> *prop)
{
    QDeclarativePlace *object = static_cast<QDeclarativePlace *>(prop->object);
    if (object->m_categories.isEmpty())
        return;

    for (auto *category : qAsConst(object->m_categories)) {
        if (category->parent() == object)
            object->m_categoriesToBeDeleted.append(category);
    }

    object->m_categories.clear();
    object->m_src.setCategories(QList<QPlaceCategory>());
    emit object->categoriesChanged();
    QMetaObject::invokeMethod(object, "cleanupDeletedCategories", Qt::QueuedConnection);
}

/*!
    \internal
*/
void QDeclarativePlace::synchronizeCategories()
{
    qDeleteAll(m_categories);
    m_categories.clear();
    for (const QPlaceCategory &value : m_src.categories()) {
        QDeclarativeCategory *declarativeValue = new QDeclarativeCategory(value, m_plugin, this);
        m_categories.append(declarativeValue);
    }
}

/*!
    \qmlproperty enumeration Place::visibility

    This property holds the visibility of the place.  It can be one of:

    \table
        \row
            \li Place.UnspecifiedVisibility
            \li The visibility of the place is unspecified, the default visibility of the \l Plugin
               will be used.
        \row
            \li Place.DeviceVisibility
            \li The place is limited to the current device.  The place will not be transferred off
               of the device.
        \row
            \li Place.PrivateVisibility
            \li The place is private to the current user.  The place may be transferred to an online
               service but is only ever visible to the current user.
        \row
            \li Place.PublicVisibility
            \li The place is public.
    \endtable

    Note that visibility does not affect how the place is displayed
    in the user-interface of an application on the device.  Instead,
    it defines the sharing semantics of the place.
*/
QDeclarativePlace::Visibility QDeclarativePlace::visibility() const
{
    return static_cast<QDeclarativePlace::Visibility>(m_src.visibility());
}

void QDeclarativePlace::setVisibility(Visibility visibility)
{
    if (static_cast<QDeclarativePlace::Visibility>(m_src.visibility()) == visibility)
        return;

    m_src.setVisibility(static_cast<QLocation::Visibility>(visibility));
    emit visibilityChanged();
}

/*!
    \qmlproperty Place Place::favorite

    This property holds the favorite instance of a place.
*/
QDeclarativePlace *QDeclarativePlace::favorite() const
{
    return m_favorite;
}

void QDeclarativePlace::setFavorite(QDeclarativePlace *favorite)
{

    if (m_favorite == favorite)
        return;

    if (m_favorite && m_favorite->parent() == this)
        delete m_favorite;

    m_favorite = favorite;
    emit favoriteChanged();
}

/*!
    \qmlmethod void Place::copyFrom(Place original)

    Copies data from an \a original place into this place.  Only data that is supported by this
    place's plugin is copied over and plugin specific data such as place identifier is not copied over.
*/
void QDeclarativePlace::copyFrom(QDeclarativePlace *original)
{
    QPlaceManager *placeManager = manager();
    if (!placeManager)
        return;

    setPlace(placeManager->compatiblePlace(original->place()));
}

/*!
    \qmlmethod void Place::initializeFavorite(Plugin destinationPlugin)

    Creates a favorite instance for the place which is to be saved into the
    destination plugin \a destinationPlugin. This method does nothing if the
    favorite property is not \c null.
*/
void QDeclarativePlace::initializeFavorite(QDeclarativeGeoServiceProvider *plugin)
{
    if (m_favorite == 0) {
        QDeclarativePlace *place = new QDeclarativePlace(this);
        place->setPlugin(plugin);
        place->copyFrom(this);
        setFavorite(place);
    }
}

/*!
    \internal
*/
void QDeclarativePlace::pullExtendedAttributes()
{
    const QStringList keys = m_extendedAttributes->keys();
    for (const QString &key : keys)
        m_extendedAttributes->clear(key);

    const QStringList attributeTypes = m_src.extendedAttributeTypes();
    for (const QString &attributeType : attributeTypes) {
        m_extendedAttributes->insert(attributeType,
            QVariant::fromValue(m_src.extendedAttribute(attributeType)));
    }

    emit extendedAttributesChanged();
}

/*!
    \internal
*/
void QDeclarativePlace::synchronizeContacts()
{
    //clear out contact data
    for (const QString &contactType : m_contactDetails->keys()) {
        const QList<QVariant> contacts = m_contactDetails->value(contactType).toList();
        for (const QVariant &var : contacts) {
            QObject *obj = var.value<QObject *>();
            if (obj->parent() == this)
                delete obj;
        }
        m_contactDetails->insert(contactType, QVariantList());
    }

    //insert new contact data from source place
    for (const QString &contactType : m_src.contactTypes()) {
        const QList<QPlaceContactDetail> sourceContacts = m_src.contactDetails(contactType);
        QVariantList declContacts;
        for (const QPlaceContactDetail &sourceContact : sourceContacts)
            declContacts.append(QVariant::fromValue(sourceContact));
        m_contactDetails->insert(contactType, declContacts);
    }
    primarySignalsEmission();
}

/*!
    \internal
    Helper function to emit the signals for the primary___()
    fields.  It is expected that the values of the primary___()
    functions have already been modified to new values.
*/
void QDeclarativePlace::primarySignalsEmission(const QString &type)
{
    if (type.isEmpty() || type == QPlaceContactDetail::Phone) {
        if (m_prevPrimaryPhone != primaryPhone()) {
            m_prevPrimaryPhone = primaryPhone();
            emit primaryPhoneChanged();
        }
        if (!type.isEmpty())
            return;
    }

    if (type.isEmpty() || type == QPlaceContactDetail::Email) {
        if (m_prevPrimaryEmail != primaryEmail()) {
            m_prevPrimaryEmail = primaryEmail();
            emit primaryEmailChanged();
        }
        if (!type.isEmpty())
            return;
    }

    if (type.isEmpty() || type == QPlaceContactDetail::Website) {
        if (m_prevPrimaryWebsite != primaryWebsite()) {
            m_prevPrimaryWebsite = primaryWebsite();
            emit primaryWebsiteChanged();
        }
        if (!type.isEmpty())
            return;
    }

    if (type.isEmpty() || type == QPlaceContactDetail::Fax) {
        if (m_prevPrimaryFax != primaryFax()) {
            m_prevPrimaryFax = primaryFax();
            emit primaryFaxChanged();
        }
    }
}

/*!
    \internal
    Helper function to return the manager, this manager is intended to be used
    to perform the next operation.  If a an operation is currently underway
    then return a null pointer.
*/
QPlaceManager *QDeclarativePlace::manager()
{
    if (m_status != QDeclarativePlace::Ready && m_status != QDeclarativePlace::Error)
        return nullptr;

    if (m_reply) {
        m_reply->abort();
        m_reply->deleteLater();
        m_reply = nullptr;
    }

    if (!m_plugin) {
           qmlWarning(this) << QStringLiteral("Plugin is not assigned to place.");
           return nullptr;
    }

    QGeoServiceProvider *serviceProvider = m_plugin->sharedGeoServiceProvider();
    if (!serviceProvider)
        return nullptr;

    QPlaceManager *placeManager = serviceProvider->placeManager();

    if (!placeManager) {
        setStatus(Error, QCoreApplication::translate(CONTEXT_NAME, PLUGIN_ERROR)
                         .arg(m_plugin->name()).arg(serviceProvider->errorString()));
        return nullptr;
    }

    return placeManager;
}

/*!
    \internal
*/
QString QDeclarativePlace::primaryValue(const QString &contactType) const
{
    QVariant value = m_contactDetails->value(contactType);
    if (value.userType() == qMetaTypeId<QJSValue>())
        value = value.value<QJSValue>().toVariant();

    if (value.userType() == QMetaType::QVariantList) {
        QVariantList detailList = m_contactDetails->value(contactType).toList();
        if (!detailList.isEmpty())
            return detailList.at(0).value<QPlaceContactDetail>().value();
    } else if (value.metaType() == QMetaType::fromType<QPlaceContactDetail>()) {
        return value.value<QPlaceContactDetail>().value();
    }

    return QString();
}

QT_END_NAMESPACE