Skip to content

Commit

Permalink
Merge branch 'jb50799-favorite' into 'master'
Browse files Browse the repository at this point in the history
[buteo-sync-plugins-social] Sync the favorite status of Google contacts. JB#50799

See merge request mer-core/buteo-sync-plugins-social!88
  • Loading branch information
blam committed Feb 25, 2021
2 parents a0f8626 + 54caf1b commit 665a079
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
61 changes: 42 additions & 19 deletions src/google/google-contacts/googlepeoplejson.cpp
Expand Up @@ -38,6 +38,7 @@
#include <QContactGuid>
#include <QContactNickname>
#include <QContactDisplayLabel>
#include <QContactFavorite>
#include <QFile>

#include <QCoreApplication>
Expand All @@ -48,6 +49,8 @@

namespace {

static const QString StarredContactGroupName = QStringLiteral("contactGroups/starred");

QDate jsonObjectToDate(const QJsonObject &object)
{
const int year = object.value("year").toInt();
Expand Down Expand Up @@ -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) {
Expand All @@ -542,10 +546,19 @@ bool GooglePeople::Membership::saveContactDetails(
}
}
}
if (membership.contactGroupResourceName == StarredContactGroupName) {
isFavorite = true;
}

contactGroupResourceNames.append(membership.contactGroupResourceName);
}

QContactFavorite favoriteDetail = contact->detail<QContactFavorite>();
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)) {
Expand All @@ -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<QContactFavorite>();
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;
}

Expand Down Expand Up @@ -1117,6 +1148,8 @@ QJsonObject GooglePeople::Person::contactToJsonObject(const QContact &contact,
contact, &person, addedFields);
addJsonValuesForContact<Event>(QStringLiteral("events"),
contact, &person, addedFields);
addJsonValuesForContact<Membership>(QStringLiteral("memberships"),
contact, &person, addedFields);
addJsonValuesForContact<Name>(QStringLiteral("names"),
contact, &person, addedFields);
addJsonValuesForContact<Nickname>(QStringLiteral("nicknames"),
Expand All @@ -1128,16 +1161,6 @@ QJsonObject GooglePeople::Person::contactToJsonObject(const QContact &contact,
addJsonValuesForContact<Url>(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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/google/google-contacts/googlepeoplejson.h
Expand Up @@ -156,7 +156,7 @@ namespace GooglePeople
int accountId,
const QList<QContactCollection> &candidateCollections);
static Membership fromJsonObject(const QJsonObject &obj);
static QJsonArray jsonValuesForContact(const QContact &contact);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

class Name
Expand Down

0 comments on commit 665a079

Please sign in to comment.