summaryrefslogtreecommitdiff
path: root/src/location/places/qplace.cpp
blob: e20cf127feea53db1b4a110cf65ba6a182b22231 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qplace.h"
#include "qplace_p.h"

#include <QtPositioning/QGeoLocation>

#ifdef QPLACE_DEBUG
#include <QDebug>
#endif

#include <QStringList>

QT_BEGIN_NAMESPACE

template<>
QPlacePrivate *QSharedDataPointer<QPlacePrivate>::clone()
{
    return d->clone();
}

QT_DEFINE_QSDP_SPECIALIZATION_DTOR(QPlacePrivate)

/*!
    \class QPlace
    \inmodule QtLocation
    \ingroup QtLocation-places
    \ingroup QtLocation-places-data
    \since 5.6

    \brief The QPlace class represents a set of data about a place.

    \input place-definition.qdocinc

    \section2 Contact Information
    The contact information of a place is based around a common set of
    \l {Contact Types}{contact types}. To retrieve all the phone numbers
    of a place, one would do:

    \snippet places/requesthandler.h Phone numbers

    The contact types are string values by design to allow for providers
    to introduce new contact types.

    For convenience there are a set of functions which return the value
    of the first contact detail of each type.
    \list
        \li QPlace::primaryPhone()
        \li QPlace::primaryEmail()
        \li QPlace::primaryWebsite()
        \li QPlace::primaryFax()
    \endlist

    \section2 Extended Attributes
    Places may have additional attributes which are not covered in the formal API.
    Similar to contacts attributes are based around a common set of
    \l {Attribute Types}{attribute types}.  To retrieve an extended attribute one
    would do:
    \snippet places/requesthandler.h Opening hours

    The attribute types are string values by design to allow providers
    to introduce new attribute types.

    \section2 Content
    The QPlace object is only meant to be a convenient container to hold
    rich content such as images, reviews and so on.  Retrieval of content
    should happen via QPlaceManager::getPlaceContent().

    The content is stored as a QPlaceContent::Collection which contains
    both the index of the content, as well as the content itself.  This enables
    developers to check whether a particular item has already been retrieved
    and if not, then request that content.

    \section3 Attribution
    Places have a field for a rich text attribution string.  Some providers
    may require that the attribution be shown when a place is displayed
    to a user.

    \section2 Categories
    Different categories may be assigned to a place to indicate that the place
    is associated with those categories.  When saving a place, the only meaningful
    data is the category id, the rest of the category data is effectively ignored.
    The category must already exist before saving the place (it is not possible
    to create a new category, assign it to the place, save the place and expect
    the category to be created).

    \section2 Saving Caveats
    \input place-caveats.qdocinc
*/

/*!
    Constructs an empty place object.
*/
QPlace::QPlace()
    : d_ptr(new QPlacePrivateDefault())
{
}

/*!
    Constructs an place object using \a dd as private implementation.
*/
QPlace::QPlace(const QSharedDataPointer<QPlacePrivate> &dd): d_ptr(dd)
{
}


/*!
    Constructs a copy of \a other.
*/
QPlace::QPlace(const QPlace &other) noexcept = default;

/*!
    Destroys this place.
*/
QPlace::~QPlace() = default;

/*!
    Assigns \a other to this place and returns a reference
    to this place.
*/
QPlace &QPlace::operator= (const QPlace & other) noexcept
{
    if (this == &other)
        return *this;

    d_ptr = other.d_ptr;
    return *this;
}

inline QPlacePrivate *QPlace::d_func()
{
    return static_cast<QPlacePrivate *>(d_ptr.data());
}

inline const QPlacePrivate *QPlace::d_func() const
{
    return static_cast<const QPlacePrivate *>(d_ptr.constData());
}

/*!
    \fn bool QPlace::operator==(const QPlace &lhs, const QPlace &rhs) noexcept

    Returns true if \a lhs is equal to \a rhs,
    otherwise returns false.
*/

/*!
    \fn bool QPlace::operator!=(const QPlace &lhs, const QPlace &rhs) noexcept

    Returns true if \a lhs is not equal to \a rhs,
    otherwise returns false.
*/

bool QPlace::isEqual(const QPlace &other) const noexcept
{
    return ( (d_ptr.constData() == other.d_ptr.constData())
             || (*d_ptr) == (*other.d_ptr));
}

/*!
    Returns categories that this place belongs to.
*/
QList<QPlaceCategory> QPlace::categories() const
{
    return d_ptr->categories();
}

/*!
    Sets a single \a category that this place belongs to.
*/
void QPlace::setCategory(const QPlaceCategory &category)
{
    d_ptr->setCategories(QList<QPlaceCategory>());
    d_ptr->setCategories(QList<QPlaceCategory>() << category);
}

/*!
    Sets the \a categories that this place belongs to.
*/
void QPlace::setCategories(const QList<QPlaceCategory> &categories)
{
     d_ptr->setCategories(categories);
}

/*!
    Returns the location of the place.
*/
QGeoLocation QPlace::location() const
{
    return d_ptr->location();
}

/*!
    Sets the \a location of the place.
*/
void QPlace::setLocation(const QGeoLocation &location)
{
    d_ptr->setLocation(location);
}

/*!
    Returns an aggregated rating of the place.
*/
QPlaceRatings QPlace::ratings() const
{
    return d_ptr->ratings();
}

/*!
    Sets the aggregated \a rating of the place.
*/
void QPlace::setRatings(const QPlaceRatings &rating)
{
    d_ptr->setRatings(rating);
}

/*!
    Returns the supplier of this place.
*/
QPlaceSupplier QPlace::supplier() const
{
    return d_ptr->supplier();
}

/*!
    Sets the supplier of this place to \a supplier.
*/
void QPlace::setSupplier(const QPlaceSupplier &supplier)
{
    d_ptr->setSupplier(supplier);
}

/*!
    Returns a collection of content associated with a place.
    This collection is a map with the key being the index of the content object
    and value being the content object itself.

    The \a type specifies which kind of content is to be retrieved.
*/
QPlaceContent::Collection QPlace::content(QPlaceContent::Type type) const
{
    return d_ptr->m_contentCollections.value(type);
}

/*!
    Sets a collection of \a content for the given \a type.
*/
void QPlace::setContent(QPlaceContent::Type type, const QPlaceContent::Collection &content)
{
    d_ptr->m_contentCollections.insert(type, content);
}

/*!
    Adds a collection of \a content of the given \a type to the place.  Any index in \a content
    that already exists is overwritten.
*/
void QPlace::insertContent(QPlaceContent::Type type, const QPlaceContent::Collection &content)
{
    for (auto iter = content.cbegin(), end = content.cend(); iter != end; ++iter)
        d_ptr->m_contentCollections[type].insert(iter.key(), iter.value());
}

/*!
    Returns the total count of content objects of the given \a type.
    This total count indicates how many the manager/provider should have available.
    (As opposed to how many objects this place instance is currently assigned).

    A negative count indicates that the total number of items is unknown.
    By default the total content count is set to 0.
*/
int QPlace::totalContentCount(QPlaceContent::Type type) const
{
    return d_ptr->m_contentCounts.value(type, 0);
}

/*!
    Sets the \a totalCount of content objects of the given \a type.
*/
void QPlace::setTotalContentCount(QPlaceContent::Type type, int totalCount)
{
    d_ptr->m_contentCounts.insert(type, totalCount);
}

/*!
    Returns the name of the place.
*/
QString QPlace::name() const
{
    return d_ptr->name();
}

/*!
    Sets the \a name of the place.
*/
void QPlace::setName(const QString &name)
{
    d_ptr->setName(name);
}

/*!
    Returns the identifier of the place.  The place identifier is only meaningful to the QPlaceManager that
    generated it and is not transferable between managers.  The place identifier is not guaranteed
    to be universally unique, but unique for the manager that generated it.
*/
QString QPlace::placeId() const
{
    return d_ptr->placeId();
}

/*!
    Sets the \a identifier of the place.
*/
void QPlace::setPlaceId(const QString &identifier)
{
    d_ptr->setPlaceId(identifier);
}

/*!
    Returns a rich text attribution string of the place.  Note, some providers may have a
    requirement where the attribution must be shown whenever a place is displayed to an end user.
*/
QString QPlace::attribution() const
{
    return d_ptr->attribution();
}

/*!
    Sets the \a attribution string of the place.
*/
void QPlace::setAttribution(const QString &attribution)
{
    d_ptr->setAttribution(attribution);
}

/*!
    Returns the icon of the place.
*/
QPlaceIcon QPlace::icon() const
{
    return d_ptr->icon();
}

/*!
    Sets the \a icon of the place.
*/
void QPlace::setIcon(const QPlaceIcon &icon)
{
    d_ptr->setIcon(icon);
}

/*!
    Returns the primary phone number for this place.  This accesses the first contact detail
    of the \l {QPlaceContactDetail::Phone}{phone number type}.  If no phone details exist, then an empty string is returned.
*/
QString QPlace::primaryPhone() const
{
    QList<QPlaceContactDetail> phoneNumbers = d_ptr->contacts().value(QPlaceContactDetail::Phone);
    if (!phoneNumbers.isEmpty())
        return phoneNumbers.at(0).value();
    else
        return QString();
}

/*!
    Returns the primary fax number for this place.  This convenience function accesses the first contact
    detail of the \l {QPlaceContactDetail::Fax}{fax type}.  If no fax details exist, then an empty string is returned.
*/
QString QPlace::primaryFax() const
{
    QList<QPlaceContactDetail> faxNumbers = d_ptr->contacts().value(QPlaceContactDetail::Fax);
    if (!faxNumbers.isEmpty())
        return faxNumbers.at(0).value();
    else
        return QString();
}

/*!
    Returns the primary email address for this place.  This convenience function accesses the first
    contact detail of the \l {QPlaceContactDetail::Email}{email type}.  If no email addresses exist, then
    an empty string is returned.
*/
QString QPlace::primaryEmail() const
{
    QList<QPlaceContactDetail> emailAddresses = d_ptr->contacts().value(QPlaceContactDetail::Email);
    if (!emailAddresses.isEmpty())
        return emailAddresses.at(0).value();
    else
        return QString();
}

/*!
    Returns the primary website of the place.  This convenience function accesses the first
    contact detail of the \l {QPlaceContactDetail::Website}{website type}.  If no websites exist,
    then an empty string is returned.
*/
QUrl QPlace::primaryWebsite() const
{
    QList<QPlaceContactDetail> websites = d_ptr->contacts().value(QPlaceContactDetail::Website);
    if (!websites.isEmpty())
        return QUrl(websites.at(0).value());
    else
        return QString();
}

/*!
    Returns true if the details of this place have been fetched,
    otherwise returns false.
*/
bool QPlace::detailsFetched() const
{
    return d_ptr->detailsFetched();
}

/*!
    Sets whether the details of this place have been \a fetched or not.
*/
void QPlace::setDetailsFetched(bool fetched)
{
    d_ptr->setDetailsFetched(fetched);
}

/*!
    Returns the types of extended attributes that this place has.
*/
QStringList QPlace::extendedAttributeTypes() const
{
    return d_ptr->extendedAttributes().keys();
}

/*!
    Returns the exteded attribute corresponding to the specified \a attributeType.
    If the place does not have that particular attribute type, a default constructed
    QPlaceExtendedAttribute is returned.
*/
QPlaceAttribute QPlace::extendedAttribute(const QString &attributeType) const
{
    return d_ptr->extendedAttribute(attributeType);
}

/*!
    Assigns an \a attribute of the given \a attributeType to a place.  If the given \a attributeType
    already exists in the place, then it is overwritten.

    If \a attribute is a default constructed QPlaceAttribute, then the \a attributeType
    is removed from the place which means it will not be listed by QPlace::extendedAttributeTypes().
*/
void QPlace::setExtendedAttribute(const QString &attributeType,
                                    const QPlaceAttribute &attribute)
{
    if (attribute == QPlaceAttribute())
        d_ptr->extendedAttributes().remove(attributeType);
    else
        d_ptr->extendedAttributes().insert(attributeType, attribute);
}

/*!
    Remove the attribute of \a attributeType from the place.

    The attribute will no longer be listed by QPlace::extendedAttributeTypes()
*/
void QPlace::removeExtendedAttribute(const QString &attributeType)
{
    setExtendedAttribute(attributeType, QPlaceAttribute());
}

/*!
    Returns the type of contact details this place has.

    See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
*/
QStringList QPlace::contactTypes() const
{
    return d_ptr->contacts().keys();
}

/*!
    Returns a list of contact details of the specified \a contactType.

    See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
*/
QList<QPlaceContactDetail> QPlace::contactDetails(const QString &contactType) const
{
    return d_ptr->contacts().value(contactType);
}

/*!
    Sets the contact \a details of a specified \a contactType.

    If \a details is empty, then the \a contactType is removed from the place such
    that it is no longer returned by QPlace::contactTypes().

    See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
*/
void QPlace::setContactDetails(const QString &contactType, QList<QPlaceContactDetail> details)
{
    if (details.isEmpty())
        d_ptr->contacts().remove(contactType);
    else
        d_ptr->contacts().insert(contactType, details);
}

/*!
    Appends a contact \a detail of a specified \a contactType.

    See QPlaceContactDetail for a list of common \l {QPlaceContactDetail::Email}{contact types}.
*/
void QPlace::appendContactDetail(const QString &contactType, const QPlaceContactDetail &detail)
{
    QList<QPlaceContactDetail> details = d_ptr->contacts().value(contactType);
    details.append(detail);
    d_ptr->contacts().insert(contactType, details);
}

/*!
    Removes all the contact details of a given \a contactType.

    The \a contactType is no longer returned when QPlace::contactTypes() is called.
*/
void QPlace::removeContactDetails(const QString &contactType)
{
    d_ptr->contacts().remove(contactType);
}

/*!
    Sets the visibility of the place to \a visibility.
*/
void QPlace::setVisibility(QLocation::Visibility visibility)
{
    d_ptr->setVisibility(visibility);
}

/*!
    Returns the visibility of the place.

    The default visibility of a new place is set to QtLocatin::Unspecified visibility.
    If a place is saved with unspecified visibility the backend chooses an appropriate
    default visibility to use when saving.
*/
QLocation::Visibility QPlace::visibility() const
{
    return d_ptr->visibility();
}

/*!
    Returns a boolean indicating whether the all the fields of the place are empty or not.
*/
bool QPlace::isEmpty() const
{
    return d_ptr->isEmpty();
}

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

QPlacePrivate::QPlacePrivate()
:   QSharedData()
{
}

QPlacePrivate::QPlacePrivate(const QPlacePrivate &other)
        : QSharedData(other),
        m_contentCollections(other.m_contentCollections),
        m_contentCounts(other.m_contentCounts)
{
}

QPlacePrivate::~QPlacePrivate() {}

bool QPlacePrivate::operator== (const QPlacePrivate &other) const
{
    return (categories() == other.categories()
            && location() == other.location()
            && ratings() == other.ratings()
            && supplier() == other.supplier()
            && m_contentCollections == other.m_contentCollections
            && m_contentCounts == other.m_contentCounts
            && name() == other.name()
            && placeId() == other.placeId()
            && attribution() == other.attribution()
            && contacts() == other.contacts()
            && extendedAttributes() == other.extendedAttributes()
            && visibility() == other.visibility()
            && icon() == other.icon()
            );
}


bool QPlacePrivate::isEmpty() const
{
    return (categories().isEmpty()
            && location().isEmpty()
            && ratings().isEmpty()
            && supplier().isEmpty()
            && m_contentCollections.isEmpty()
            && m_contentCounts.isEmpty()
            && name().isEmpty()
            && placeId().isEmpty()
            && attribution().isEmpty()
            && contacts().isEmpty()
            && extendedAttributes().isEmpty()
            && QLocation::UnspecifiedVisibility == visibility()
            && icon().isEmpty()
            );
}

QPlaceAttribute QPlacePrivate::extendedAttribute(const QString &attributeType) const
{
    return extendedAttributes().value(attributeType);
}



//
//  Default implementation
//

QPlacePrivateDefault::QPlacePrivateDefault()
    : QPlacePrivate(), m_visibility(QLocation::UnspecifiedVisibility), m_detailsFetched(false)
{
}

QPlacePrivateDefault::QPlacePrivateDefault(const QPlacePrivateDefault &other)
    : QPlacePrivate(other),
    m_categories(other.m_categories),
    m_location(other.m_location),
    m_ratings(other.m_ratings),
    m_supplier(other.m_supplier),
    m_name(other.m_name),
    m_placeId(other.m_placeId),
    m_attribution(other.m_attribution),
    m_extendedAttributes(other.m_extendedAttributes),
    m_contacts(other.m_contacts),
    m_visibility(other.m_visibility),
    m_icon(other.m_icon),
    m_detailsFetched(other.m_detailsFetched)
{
}

QPlacePrivateDefault::~QPlacePrivateDefault()
{
}

QPlacePrivate *QPlacePrivateDefault::clone()
{
    return new QPlacePrivateDefault(*this);
}

QList<QPlaceCategory> QPlacePrivateDefault::categories() const
{
    return m_categories;
}

void QPlacePrivateDefault::setCategories(const QList<QPlaceCategory> &categories)
{
    m_categories = categories;
}

QGeoLocation QPlacePrivateDefault::location() const
{
    return m_location;
}

void QPlacePrivateDefault::setLocation(const QGeoLocation &location)
{
    m_location = location;
}

QPlaceRatings QPlacePrivateDefault::ratings() const
{
    return m_ratings;
}

void QPlacePrivateDefault::setRatings(const QPlaceRatings &ratings)
{
    m_ratings = ratings;
}

QPlaceSupplier QPlacePrivateDefault::supplier() const
{
    return m_supplier;
}

void QPlacePrivateDefault::setSupplier(const QPlaceSupplier &supplier)
{
    m_supplier = supplier;
}

QString QPlacePrivateDefault::name() const
{
    return m_name;
}

void QPlacePrivateDefault::setName(const QString &name)
{
    m_name = name;
}

QString QPlacePrivateDefault::placeId() const
{
    return m_placeId;
}

void QPlacePrivateDefault::setPlaceId(const QString &placeIdentifier)
{
    m_placeId = placeIdentifier;
}

QString QPlacePrivateDefault::attribution() const
{
    return m_attribution;
}

void QPlacePrivateDefault::setAttribution(const QString &attribution)
{
    m_attribution = attribution;
}

QLocation::Visibility QPlacePrivateDefault::visibility() const
{
    return m_visibility;
}

void QPlacePrivateDefault::setVisibility(QLocation::Visibility visibility)
{
    m_visibility = visibility;
}

QPlaceIcon QPlacePrivateDefault::icon() const
{
    return m_icon;
}

void QPlacePrivateDefault::setIcon(const QPlaceIcon &icon)
{
    m_icon = icon;
}

bool QPlacePrivateDefault::detailsFetched() const
{
    return m_detailsFetched;
}

void QPlacePrivateDefault::setDetailsFetched(bool fetched)
{
    m_detailsFetched = fetched;
}

QMap<QString, QPlaceAttribute> QPlacePrivateDefault::extendedAttributes() const
{
    return m_extendedAttributes;
}

QMap<QString, QPlaceAttribute> &QPlacePrivateDefault::extendedAttributes()
{
    return m_extendedAttributes;
}

QMap<QString, QList<QPlaceContactDetail> > QPlacePrivateDefault::contacts() const
{
    return m_contacts;
}

QMap<QString, QList<QPlaceContactDetail> > &QPlacePrivateDefault::contacts()
{
    return m_contacts;
}



QT_END_NAMESPACE