summaryrefslogtreecommitdiffstats
path: root/src/imports/contacts/qdeclarativecontact.cpp
blob: 2bbd2a63c7d190f3add03715da4d1389553d46b3 (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml 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 "qdeclarativecontact_p.h"

#include <QtCore/qurl.h>

#include <QtQml/qqmlengine.h>

#include <QtContacts/qcontactdetails.h>
#include <QtContacts/qcontactmanager.h>

#include "qdeclarativecontactmodel_p.h"

QTCONTACTS_USE_NAMESPACE

QT_BEGIN_NAMESPACE

/*!
    \qmltype Contact
    \instantiates QDeclarativeContact
    \brief The Contact element represents an addressbook contact.
    \ingroup qml-contacts-main
    \inqmlmodule QtContacts

    The Contact element is part of the \b{QtContacts} module.

    A Contact object has a collection of details (like a name, phone numbers and
    email addresses).  Each detail (which can have multiple fields) is stored
    in an appropriate subclass of ContactDetail, and the Contact allows
    retrieving these details in various ways.

    If some of the contact details are not unique details, all of this type of detail values
    can be accessed by the property with the name in plural. For example, if there are 3 phone numbers stored in
    a contact, they can be accessed by contact.phoneNumbers property, which holds a list of
    all PhoneNumber details. If a contact does not contain a detail of particular type,
    the value of the corresponding singular property (e.g. phoneNumber) in undefined and the
    plural (e.g. phoneNumbers) is empty. The list of properties which support multiple detail
    instances depends on the contact engine implementations.

    \sa QContact
*/

// call-back function templates for list properties
template <typename T, QDeclarativeContactDetail::DetailType detailType>
static qsizetype list_property_count(QQmlListProperty<T> *property)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object)
        return object->details(detailType).size();
    else
        return 0;
}

template <typename T, QDeclarativeContactDetail::DetailType detailType>
static T *list_property_at(QQmlListProperty<T> *property, qsizetype index)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object)
        return qobject_cast<T *>(qvariant_cast<QObject*>(object->details(detailType).at(index)));
    else
        return 0;
}

QDeclarativeContact::QDeclarativeContact(QObject *parent)
    :QObject(parent)
    , m_modified(false)
{
    connect(this, SIGNAL(contactChanged()), SLOT(setModified()));
}

QDeclarativeContact::~QDeclarativeContact()
{
    clearDetails();
}

void QDeclarativeContact::setContact(const QContact& contact)
{
    m_id = contact.id();
    m_collectionId = contact.collectionId();
    foreach (QDeclarativeContactDetail *detail, m_details)
        delete detail;
    m_details.clear();
    m_preferredDetails.clear();

    QList<QContactDetail> details(contact.details());
    foreach (const QContactDetail &detail, details) {
        QDeclarativeContactDetail *contactDetail = QDeclarativeContactDetailFactory::createContactDetail(static_cast<QDeclarativeContactDetail::DetailType>(detail.type()));
        contactDetail->setParent(this);
        contactDetail->setDetail(detail);
        connect(contactDetail, SIGNAL(detailChanged()), this, SIGNAL(contactChanged()));
        m_details.append(contactDetail);
    }

    QMap<QString, QContactDetail> prefDetails(contact.preferredDetails());
    QMap<QString, QContactDetail>::const_iterator  it = prefDetails.begin();
    while (it != prefDetails.end()) {
        m_preferredDetails.insert(it.key(), it.value().key());
        it++;
    }

    m_modified = false;
    emit contactChanged();
}

QContact QDeclarativeContact::contact() const
{
    QContact contact;
    contact.setId(m_id);
    contact.setCollectionId(m_collectionId);
    foreach (QDeclarativeContactDetail *detail, m_details)
        contact.saveDetail(&detail->detail());

    QVariantMap prefDetails = preferredDetails();
    QVariantMap::const_iterator it = prefDetails.begin();
    while (it != prefDetails.end()) {
        contact.setPreferredDetail(it.key(), it.value().value<QDeclarativeContactDetail *>()->detail());
        it++;
    }
    return contact;
}

/*!
    \qmlproperty bool Contact::modified

    This property holds the dirty flag of the Contact object.
    If the Contact has been changed, returns true, otherwise returns false.
*/
bool QDeclarativeContact::modified() const
{
     return m_modified;
}

void QDeclarativeContact::setModified()
{
    m_modified = true;
}

/*!
    \qmlproperty enumeration Contact::type

    This property holds type of the Contact, the value can be one of:

    \list
    \li Contact.Contact
    \li Contact.Group
    \li Contact.Facet
    \endlist
*/
QDeclarativeContactType::ContactType QDeclarativeContact::type() const
{
    foreach (QDeclarativeContactDetail *detail, m_details) {
        if (QDeclarativeContactDetail::Type == detail->detailType())
           return static_cast<QDeclarativeContactType *>(detail)->type();
    }
    return QDeclarativeContactType::Contact;
}

/*!
    \qmlmethod Contact::removeDetail(detail)

    Removes the given contact \a detail from the contact, returns true if successful, otherwise returns false.
*/

bool QDeclarativeContact::removeDetail(QDeclarativeContactDetail* detail)
{
    if (detail) {
        if (!detail->removable())
            return false;
        int key = detail->detail().key();
        int i = 0;
        foreach (QDeclarativeContactDetail *contactDetail, m_details) {
            if (key == contactDetail->detail().key()) {
                removePreferredDetail(detail);
                delete contactDetail;
                m_details.removeAt(i);
                emit contactChanged();
                return true;
            }
        ++i;
        }
    }
    return false;
}

void QDeclarativeContact::removePreferredDetail(QDeclarativeContactDetail* detail)
{
    QMap<QString, int> cpy = m_preferredDetails;
    QMap<QString, int>::const_iterator it = cpy.begin();
    while (it != cpy.end()) {
        if (it.value() == detail->detail().key()) {
            m_preferredDetails.remove(it.key());
            break;
        }
        it++;
    }
}

/*!
    \qmlmethod Contact::addDetail(detail)

    Adds the given contact \a detail to the contact, returns true if successful, otherwise returns false.

    Note: If the \a detail has been added into the same contact before, this operation will be ignored,
    so if you want to add a \a detail multiple times, the \a detail should be copied before calling this function.
*/
bool QDeclarativeContact::addDetail(QDeclarativeContactDetail* detail)
{
    if (!detail || m_details.contains(detail))
        return false;

    QDeclarativeContactDetail *contactDetail = QDeclarativeContactDetailFactory::createContactDetail(detail->detailType());
    contactDetail->setParent(this);
    contactDetail->setDetail(detail->detail());
    connect(contactDetail, SIGNAL(detailChanged()), this, SIGNAL(contactChanged()));
    m_details.append(contactDetail);

    m_modified = true;
    emit contactChanged();
    return true;
}

/*!
    \qmlmethod Contact::setPreferredDetail(actionName, detail)

    Set a particular detail (\a preferredDetail) as the preferred detail for any actions with the given \a actionName.

    The \a preferredDetail object must exist in this object, and the \a actionName cannot be empty.

    Returns true if the preference could be recorded, and false otherwise.

    \sa preferredDetail()
 */
bool QDeclarativeContact::setPreferredDetail(const QString& actionName, QDeclarativeContactDetail* detail)
{
   if (actionName.isEmpty() || !detail || !m_details.contains(detail))
        return false;

   if (m_preferredDetails.contains(actionName) && m_preferredDetails[actionName] == detail->detail().key())
       return false;

   m_preferredDetails.insert(actionName, detail->detail().key());
   m_modified = true;
   emit contactChanged();
   return true;
}

/*!
    \qmlmethod Contact::isPreferredDetail(actionName, detail)

    Returns true if the given \a detail is a preferred detail for the given \a actionName,
    or for any action if the \a actionName is empty.

    \sa preferredDetail()
 */
bool QDeclarativeContact::isPreferredDetail(const QString& actionName, QDeclarativeContactDetail* detail) const
{
    if (actionName.isEmpty() || !detail || !m_details.contains(detail))
         return false;

    if (!m_preferredDetails.contains(actionName))
        return false;

    return (m_preferredDetails[actionName] == detail->detail().key());
}

/*!
    \qmlmethod Contact::preferredDetail(actionName, detail)

    Returns the preferred detail for a given \a actionName.

    If the \a actionName is empty, or there is no preference recorded for
    the supplied \a actionName, returns null.

    \sa preferredDetails()
 */
QDeclarativeContactDetail* QDeclarativeContact::preferredDetail(const QString& actionName) const
{
    int id = m_preferredDetails.value(actionName, -1);
    if (id == -1)
        return 0;

    foreach (QDeclarativeContactDetail* detail, m_details) {
        if (detail->detail().key() == id)
            return detail;
    }
    return 0;
}


/*!
    \qmlproperty map<string, ContactDetail> Contact::preferredDetails

    This property holds the recorded detail preferences for action names.

    Each entry in the map has the action name as the key, and the corresponding
    preferred detail as the value.
 */
QVariantMap QDeclarativeContact::preferredDetails() const
{
    QVariantMap result;
    QMap<QString, int>::const_iterator it = m_preferredDetails.begin();
    while (it != m_preferredDetails.end()) {
        result.insert(it.key(), QVariant::fromValue<QDeclarativeContactDetail*>(preferredDetail(it.key())));
        it++;
    }
    return result;
}

/*!
    \qmlproperty string OContact::collectionId

    This property holds the id of collection where the contact belongs to.
*/
QString QDeclarativeContact::collectionId() const
{
    return m_collectionId.toString();
}

void QDeclarativeContact::setCollectionId(const QString &collectionId)
{
    QContactCollectionId newCollectionId(QContactCollectionId::fromString(collectionId));

    // in case invalid collectionId-string, fromString() will return default collectionId-string
    // instead of the intended collectionId-string
    if (newCollectionId.toString() == collectionId && m_collectionId.toString() != collectionId) {
        m_collectionId = newCollectionId;
        m_modified = true;
        emit contactChanged();
    }
}

/*!
    \qmlproperty list<ContactDetail> Contact::contactDetails

    This property holds the list of all the details that the contact has.
*/
QQmlListProperty<QDeclarativeContactDetail> QDeclarativeContact::contactDetails()
{
    return { this,
             nullptr,
             &QDeclarativeContact::_q_detail_append,
             &QDeclarativeContact::_q_detail_count,
             &QDeclarativeContact::_q_detail_at,
             &QDeclarativeContact::_q_detail_clear };
}

/*!
    \qmlproperty int Contact::contactId

    This property holds the id of the Contact object.
    This property is read only.
*/
QString QDeclarativeContact::contactId() const
{
    return m_id.toString();
}

/*!
    \qmlproperty string Contact::manager

    This property holds the manager name which the Contact object comes from.
*/
QString QDeclarativeContact::manager() const
{
    return m_id.managerUri();
}

/*!
    \qmlmethod QDeclarativeContactDetail* QDeclarativeContact::detail(int type)

    Returns contactDetail object which detail name or detail type is \a name.
*/
QDeclarativeContactDetail* QDeclarativeContact::detail(int type)
{
    foreach (QDeclarativeContactDetail *detail, m_details) {
        if (type == detail->detailType()) {
            return detail;
        }
    }
    return 0;
}

/*!
    \qmlmethod QVariantList QDeclarativeContact::details(int type)

    Returns a list of ContactDetail objects which detail name or detail type is \a name.
*/
QVariantList QDeclarativeContact::details(int type)
{
    QVariantList list;
    foreach (QDeclarativeContactDetail *detail, m_details) {
        if (type == detail->detailType()) {
            list.append(QVariant::fromValue((QObject*)detail));
        }
    }
    return list;
}

/*!
    \qmlmethod Contact::clearDetails()

    Remove all detail objects in this contact.
*/
void QDeclarativeContact::clearDetails()
{
    if (m_details.isEmpty())
        return;

    foreach (QDeclarativeContactDetail *detail, m_details)
        delete detail;
    m_details.clear();
    m_modified = true;
    emit contactChanged();
}


/*!
    \qmlmethod Contact::save()

    Saves this Contact if the contact has been modified.

    \sa Contact::modified
*/
void QDeclarativeContact::save()
{
    if (modified()) {
        QDeclarativeContactModel* model = qobject_cast<QDeclarativeContactModel*>(parent());
        if (model) {
            model->saveContact(this);
            m_modified = false;
        }
    }
}

// convenient access to most frequently used details
/*!
    \qmlproperty Address Contact::address

    This property holds the address detail of the Contact object. In case a contact has several addresses then
    the first one is returned.
*/
QDeclarativeContactAddress* QDeclarativeContact::address()
{
    return getDetail<QDeclarativeContactAddress>(QDeclarativeContactDetail::Address);
}

/*!
    \qmlproperty list<Address> Contact::addresses

    This property holds the address details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactAddress> QDeclarativeContact::addresses()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactAddress, QDeclarativeContactDetail::Address>,
             &list_property_at<QDeclarativeContactAddress, QDeclarativeContactDetail::Address> };
}

/*!
    \qmlproperty Anniversary Contact::anniversary

    This property holds the anniversary detail of the Contact object.
*/
QDeclarativeContactAnniversary* QDeclarativeContact::anniversary()
{
    return getDetail<QDeclarativeContactAnniversary>(QDeclarativeContactDetail::Anniversary);
}

/*!
    \qmlproperty Avatar Contact::avatar

    This property holds the avatar detail of the Contact object.
*/
QDeclarativeContactAvatar* QDeclarativeContact::avatar()
{
    return getDetail<QDeclarativeContactAvatar>(QDeclarativeContactDetail::Avatar);
}

/*!
    \qmlproperty Birthday Contact::birthday

    This property holds the birthday detail of the Contact object.
*/
QDeclarativeContactBirthday*  QDeclarativeContact::birthday()
{
    return getDetail<QDeclarativeContactBirthday>(QDeclarativeContactDetail::Birthday);
}

/*!
    \qmlproperty DisplayLabel Contact::displayLabel

    This property holds the displayLabel detail of the Contact object.
    display label is the one which gets displayed when a contact is created as per versit doc specs this is a "FN" property
*/
QDeclarativeContactDisplayLabel*  QDeclarativeContact::displayLabel()
{
    return getDetail<QDeclarativeContactDisplayLabel>(QDeclarativeContactDetail::DisplayLabel);
}

/*!
    \qmlproperty EmailAddress Contact::email

    This property holds the email address detail of the Contact object. In case a contact has several email addresses then
    the first one is returned.
*/
QDeclarativeContactEmailAddress*  QDeclarativeContact::email()
{
    return getDetail<QDeclarativeContactEmailAddress>(QDeclarativeContactDetail::Email);
}

/*!
    \qmlproperty list<EmailAddress> Contact::emails

    This property holds the email address details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactEmailAddress> QDeclarativeContact::emails()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactEmailAddress, QDeclarativeContactDetail::Email>,
             &list_property_at<QDeclarativeContactEmailAddress, QDeclarativeContactDetail::Email> };
}

/*!
    \qmlproperty ExtendedDetail Contact::extendedDetail

    This property holds the extended detail of the Contact object. In case a contact has several extended
    details then the first one is returned.
*/
QDeclarativeContactExtendedDetail*  QDeclarativeContact::extendedDetail()
{
    return getDetail<QDeclarativeContactExtendedDetail>(QDeclarativeContactDetail::ExtendedDetail);
}

/*!
    \qmlproperty list<ExtendedDetail> Contact::extendedDetails

    This property holds the extended details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactExtendedDetail> QDeclarativeContact::extendedDetails()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactExtendedDetail, QDeclarativeContactDetail::ExtendedDetail>,
             &list_property_at<QDeclarativeContactExtendedDetail, QDeclarativeContactDetail::ExtendedDetail> };
}

/*!
    \qmlproperty Family Contact::family

    This property holds the family detail of the Contact object.
*/
QDeclarativeContactFamily*  QDeclarativeContact::family()
{
    return getDetail<QDeclarativeContactFamily>(QDeclarativeContactDetail::Family);
}

/*!
    \qmlproperty Favorite Contact::favorite

    This property holds the favorite detail of the Contact object.
*/
QDeclarativeContactFavorite*  QDeclarativeContact::favorite()
{
    return getDetail<QDeclarativeContactFavorite>(QDeclarativeContactDetail::Favorite);
}

/*!
    \qmlproperty Gender Contact::gender

    This property holds the gender detail of the Contact object.
*/
QDeclarativeContactGender*  QDeclarativeContact::gender()
{
    return getDetail<QDeclarativeContactGender>(QDeclarativeContactDetail::Gender);
}

/*!
    \qmlproperty GeoLocation Contact::geolocation

    This property holds the geolocation detail of the Contact object.
*/
QDeclarativeContactGeoLocation*  QDeclarativeContact::geolocation()
{
    return getDetail<QDeclarativeContactGeoLocation>(QDeclarativeContactDetail::Geolocation);
}

/*!
    \qmlproperty GlobalPresence Contact::globalPresence

    This property holds the globalPresence detail of the Contact object.
*/
QDeclarativeContactGlobalPresence*  QDeclarativeContact::globalPresence()
{
    return getDetail<QDeclarativeContactGlobalPresence>(QDeclarativeContactDetail::GlobalPresence);
}

/*!
    \qmlproperty Guid Contact::guid

    This property holds the guid detail of the Contact object.
*/
QDeclarativeContactGuid*  QDeclarativeContact::guid()
{
    return getDetail<QDeclarativeContactGuid>(QDeclarativeContactDetail::Guid);
}

/*!
    \qmlproperty Hobby Contact::hobby

    This property holds the hobby detail of the Contact object.
*/
QDeclarativeContactHobby*  QDeclarativeContact::hobby()
{
    return getDetail<QDeclarativeContactHobby>(QDeclarativeContactDetail::Hobby);
}

/*!
    \qmlproperty Name Contact::name

    This property holds the name detail of the Contact object.
*/
QDeclarativeContactName*  QDeclarativeContact::name()
{
   return getDetail<QDeclarativeContactName>(QDeclarativeContactDetail::Name);
}

/*!
    \qmlproperty Nickname Contact::nickname

    This property holds the nickname detail of the Contact object.
*/
QDeclarativeContactNickname*  QDeclarativeContact::nickname()
{
    return getDetail<QDeclarativeContactNickname>(QDeclarativeContactDetail::NickName);
}

/*!
    \qmlproperty Note Contact::note

    This property holds the note detail of the Contact object.
*/
QDeclarativeContactNote*  QDeclarativeContact::note()
{
    return getDetail<QDeclarativeContactNote>(QDeclarativeContactDetail::Note);
}

/*!
    \qmlproperty OnlineAccount Contact::onlineAccount

    This property holds the onlineAccount detail of the Contact object. In case a contact has several accounts then
    the first one is returned.
*/
QDeclarativeContactOnlineAccount*  QDeclarativeContact::onlineAccount()
{
    return getDetail<QDeclarativeContactOnlineAccount>(QDeclarativeContactDetail::OnlineAccount);
}

/*!
    \qmlproperty Organization Contact::organization

    This property holds the organization detail of the Contact object.
*/
QDeclarativeContactOrganization*  QDeclarativeContact::organization()
{
    return getDetail<QDeclarativeContactOrganization>(QDeclarativeContactDetail::Organization);
}

/*!
    \qmlproperty list<Organization> Contact::organizations

    This property holds the organization details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactOrganization> QDeclarativeContact::organizations()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactOrganization, QDeclarativeContactDetail::Organization>,
             &list_property_at<QDeclarativeContactOrganization, QDeclarativeContactDetail::Organization> };
}

/*!
    \qmlproperty PhoneNumber Contact::phoneNumber

    This property holds the phone number detail of the Contact object. In case a contact has several numbers then
    the first one is returned.
*/
QDeclarativeContactPhoneNumber*  QDeclarativeContact::phoneNumber()
{
    return getDetail<QDeclarativeContactPhoneNumber>(QDeclarativeContactDetail::PhoneNumber);
}

/*!
    \qmlproperty list<PhoneNumber> Contact::phoneNumbers

    This property holds the phone number details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactPhoneNumber> QDeclarativeContact::phoneNumbers()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactPhoneNumber, QDeclarativeContactDetail::PhoneNumber>,
             &list_property_at<QDeclarativeContactPhoneNumber, QDeclarativeContactDetail::PhoneNumber> };
}

/*!
    \qmlproperty Presence Contact::presence

    This property holds the presence detail of the Contact object.
*/
QDeclarativeContactPresence*  QDeclarativeContact::presence()
{
    return getDetail<QDeclarativeContactPresence>(QDeclarativeContactDetail::Presence);
}

/*!
    \qmlproperty Ringtone Contact::ringtone

    This property holds the ringtone detail of the Contact object.
*/
QDeclarativeContactRingtone*  QDeclarativeContact::ringtone()
{
    return getDetail<QDeclarativeContactRingtone>(QDeclarativeContactDetail::Ringtone);
}

/*!
    \qmlproperty SyncTarget Contact::syncTarget

    This property holds the syncTarget detail of the Contact object.
*/
QDeclarativeContactSyncTarget*  QDeclarativeContact::syncTarget()
{
    return getDetail<QDeclarativeContactSyncTarget>(QDeclarativeContactDetail::SyncTarget);
}

/*!
    \qmlproperty Tag Contact::tag

    This property holds the tag detail of the Contact object.
*/
QDeclarativeContactTag*  QDeclarativeContact::tag()
{
    return getDetail<QDeclarativeContactTag>(QDeclarativeContactDetail::Tag);
}

/*!
    \qmlproperty Timestamp Contact::timestamp

    This property holds the timestamp detail of the Contact object.
*/
QDeclarativeContactTimestamp*  QDeclarativeContact::timestamp()
{
    return getDetail<QDeclarativeContactTimestamp>(QDeclarativeContactDetail::Timestamp);
}

/*!
    \qmlproperty Url Contact::url

    This property holds the url detail of the Contact object.
*/
QDeclarativeContactUrl*  QDeclarativeContact::url()
{
    return getDetail<QDeclarativeContactUrl>(QDeclarativeContactDetail::Url);
}

/*!
    \qmlproperty list<Url> Contact::urls

    This property holds the url details of the Contact object.
*/
QQmlListProperty<QDeclarativeContactUrl> QDeclarativeContact::urls()
{
    return { this,
             nullptr,
             &list_property_count<QDeclarativeContactUrl, QDeclarativeContactDetail::Url>,
             &list_property_at<QDeclarativeContactUrl, QDeclarativeContactDetail::Url> };
}

/*!
    \qmlproperty Version Contact::version

    This property holds the version detail of the Contact object.
*/
QDeclarativeContactVersion*  QDeclarativeContact::version()
{
    return getDetail<QDeclarativeContactVersion>(QDeclarativeContactDetail::Version);
}

// call-back functions for list property

/*!
    \internal
 */
void QDeclarativeContact::_q_detail_append(QQmlListProperty<QDeclarativeContactDetail> *property, QDeclarativeContactDetail *value)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object)
    {
        object->m_details.append(value);
        value->connect(value, SIGNAL(valueChanged()), SIGNAL(detailChanged()), Qt::UniqueConnection);
        value->connect(value, SIGNAL(detailChanged()), object, SIGNAL(contactChanged()), Qt::UniqueConnection);
    }
}

/*!
    \internal
 */
QDeclarativeContactDetail *QDeclarativeContact::_q_detail_at(QQmlListProperty<QDeclarativeContactDetail> *property, qsizetype index)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object)
        return object->m_details.at(index);
    else
        return 0;
}

/*!
    \internal
 */
void QDeclarativeContact::_q_detail_clear(QQmlListProperty<QDeclarativeContactDetail> *property)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object) {
        foreach (QDeclarativeContactDetail *obj, object->m_details)
            delete obj;
        object->m_details.clear();
    }
}

/*!
    \internal
 */
qsizetype QDeclarativeContact::_q_detail_count(QQmlListProperty<QDeclarativeContactDetail> *property)
{
    QDeclarativeContact *object = qobject_cast<QDeclarativeContact *>(property->object);
    if (object)
        return object->m_details.size();
    else
        return 0;
}

QT_END_NAMESPACE

#include "moc_qdeclarativecontact_p.cpp"