diff --git a/src/google/google-contacts/googlepeoplejson.cpp b/src/google/google-contacts/googlepeoplejson.cpp index 496a1c4..f781cab 100644 --- a/src/google/google-contacts/googlepeoplejson.cpp +++ b/src/google/google-contacts/googlepeoplejson.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ namespace { +static const QString StarredContactGroupName = QStringLiteral("contactGroups/starred"); + QDate jsonObjectToDate(const QJsonObject &object) { const int year = object.value("year").toInt(); @@ -533,6 +536,7 @@ bool GooglePeople::Membership::saveContactDetails( contact->setCollectionId(QContactCollectionId()); QStringList contactGroupResourceNames; + bool isFavorite = false; for (const Membership &membership : values) { if (contact->collectionId().isNull()) { for (const QContactCollection &collection : candidateCollections) { @@ -542,10 +546,19 @@ bool GooglePeople::Membership::saveContactDetails( } } } + if (membership.contactGroupResourceName == StarredContactGroupName) { + isFavorite = true; + } contactGroupResourceNames.append(membership.contactGroupResourceName); } + QContactFavorite favoriteDetail = contact->detail(); + favoriteDetail.setFavorite(isFavorite); + if (!saveContactDetail(contact, &favoriteDetail)) { + return false; + } + // Preserve contactGroupResourceName values since a Person can belong to multiple contact // groups but a QContact can only belong to one collection. if (!saveContactExtendedDetail(contact, QStringLiteral("contactGroupResourceNames"), contactGroupResourceNames)) { @@ -566,22 +579,40 @@ GooglePeople::Membership GooglePeople::Membership::fromJsonObject(const QJsonObj return ret; } -QJsonArray GooglePeople::Membership::jsonValuesForContact(const QContact &contact) +QJsonArray GooglePeople::Membership::jsonValuesForContact(const QContact &contact, bool *hasChanges) { QJsonArray array; QStringList contactGroupResourceNames = contactExtendedDetail( contact, QStringLiteral("contactGroupResourceNames")).toStringList(); - for (const QString &contactGroupResourceName : contactGroupResourceNames) { - QJsonObject membership; - // Add the nested contactGroupMembership object. Don't need to add "contactGroupId" - // property as that is deprecated. - QJsonObject contactGroupMembershipObject; - contactGroupMembershipObject.insert("contactGroupResourceName", contactGroupResourceName); - membership.insert("contactGroupMembership", contactGroupMembershipObject); + const QContactFavorite favoriteDetail = contact.detail(); + if (shouldAddDetailChanges(favoriteDetail, hasChanges)) { + const bool isFavorite = favoriteDetail.isFavorite(); + if (isFavorite && contactGroupResourceNames.indexOf(StarredContactGroupName) < 0) { + contactGroupResourceNames.append(StarredContactGroupName); + } else if (!isFavorite) { + contactGroupResourceNames.removeOne(StarredContactGroupName); + } + } - array.append(membership); + if (contact.id().isNull()) { + // This is a new contact, so add its collection into the list of memberships. + *hasChanges = true; } + + if (*hasChanges) { + // Add the list of all known memberships of this contact. + for (const QString &contactGroupResourceName : contactGroupResourceNames) { + QJsonObject membership; + // Add the nested contactGroupMembership object. Don't need to add "contactGroupId" + // property as that is deprecated. + QJsonObject contactGroupMembershipObject; + contactGroupMembershipObject.insert("contactGroupResourceName", contactGroupResourceName); + membership.insert("contactGroupMembership", contactGroupMembershipObject); + array.append(membership); + } + } + return array; } @@ -1117,6 +1148,8 @@ QJsonObject GooglePeople::Person::contactToJsonObject(const QContact &contact, contact, &person, addedFields); addJsonValuesForContact(QStringLiteral("events"), contact, &person, addedFields); + addJsonValuesForContact(QStringLiteral("memberships"), + contact, &person, addedFields); addJsonValuesForContact(QStringLiteral("names"), contact, &person, addedFields); addJsonValuesForContact(QStringLiteral("nicknames"), @@ -1128,16 +1161,6 @@ QJsonObject GooglePeople::Person::contactToJsonObject(const QContact &contact, addJsonValuesForContact(QStringLiteral("urls"), contact, &person, addedFields); - if (contact.id().isNull()) { - // The membership (collection) of a contact never changes, so it only needs to be added - // if this is a new contact. - person.insert(QStringLiteral("memberships"), - Membership::jsonValuesForContact(contact)); - if (addedFields) { - addedFields->append(QStringLiteral("memberships")); - } - } - return person; } diff --git a/src/google/google-contacts/googlepeoplejson.h b/src/google/google-contacts/googlepeoplejson.h index f1b5c26..e4c7a37 100644 --- a/src/google/google-contacts/googlepeoplejson.h +++ b/src/google/google-contacts/googlepeoplejson.h @@ -156,7 +156,7 @@ namespace GooglePeople int accountId, const QList &candidateCollections); static Membership fromJsonObject(const QJsonObject &obj); - static QJsonArray jsonValuesForContact(const QContact &contact); + static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges); }; class Name