Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb53422' into 'master'
Jb53422

See merge request mer-core/buteo-sync-plugins-social!90
  • Loading branch information
blam committed Mar 11, 2021
2 parents 88beda1 + 734477e commit e0d5a9b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 38 deletions.
63 changes: 44 additions & 19 deletions src/google/google-contacts/googlepeoplejson.cpp
Expand Up @@ -51,7 +51,7 @@ namespace {

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

QDate jsonObjectToDate(const QJsonObject &object)
QDate jsonObjectToDate(const QJsonObject &object, bool *ok)
{
const int year = object.value("year").toInt();
const int month = object.value("month").toInt();
Expand All @@ -61,6 +61,9 @@ QDate jsonObjectToDate(const QJsonObject &object)
if (!date.isValid()) {
SOCIALD_LOG_ERROR("Cannot read date from JSON:" << object);
}
if (ok) {
*ok = date.isValid();
}
return date;
}

Expand All @@ -80,7 +83,11 @@ QList<T> jsonArrayToList(const QJsonArray &array)
{
QList<T> values;
for (auto it = array.constBegin(); it != array.constEnd(); ++it) {
values.append(T::fromJsonObject(it->toObject()));
bool error = false;
const T &value = T::fromJsonObject(it->toObject(), &error);
if (!error) {
values.append(value);
}
}
return values;
}
Expand Down Expand Up @@ -169,7 +176,7 @@ bool shouldAddDetailChanges(const QContactDetail &detail, bool *hasChanges)

}

GooglePeople::Source GooglePeople::Source::fromJsonObject(const QJsonObject &object)
GooglePeople::Source GooglePeople::Source::fromJsonObject(const QJsonObject &object, bool *)
{
Source ret;
ret.type = object.value("type").toString();
Expand Down Expand Up @@ -223,7 +230,7 @@ bool GooglePeople::Address::saveContactDetails(QContact *contact, const QList<Ad
return true;
}

GooglePeople::Address GooglePeople::Address::fromJsonObject(const QJsonObject &obj)
GooglePeople::Address GooglePeople::Address::fromJsonObject(const QJsonObject &obj, bool *)
{
Address ret;
ret.metadata = FieldMetadata::fromJsonObject(obj.value("metadata").toObject());
Expand Down Expand Up @@ -296,7 +303,7 @@ bool GooglePeople::Biography::saveContactDetails(QContact *contact, const QList<
return saveContactDetail(contact, &detail);
}

GooglePeople::Biography GooglePeople::Biography::fromJsonObject(const QJsonObject &object)
GooglePeople::Biography GooglePeople::Biography::fromJsonObject(const QJsonObject &object, bool *)
{
Biography ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -332,11 +339,20 @@ bool GooglePeople::Birthday::saveContactDetails(QContact *contact, const QList<B
return saveContactDetail(contact, &detail);
}

GooglePeople::Birthday GooglePeople::Birthday::fromJsonObject(const QJsonObject &object)
GooglePeople::Birthday GooglePeople::Birthday::fromJsonObject(const QJsonObject &object, bool *error)
{
bool dateOk = false;
const QDate date = jsonObjectToDate(object.value("date").toObject(), &dateOk);
if (error) {
*error = !dateOk;
}
if (!dateOk) {
return Birthday();
}

Birthday ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
ret.date = jsonObjectToDate(object.value("date").toObject());
ret.date = date;
return ret;
}

Expand Down Expand Up @@ -388,7 +404,7 @@ bool GooglePeople::EmailAddress::saveContactDetails(QContact *contact, const QLi
return true;
}

GooglePeople::EmailAddress GooglePeople::EmailAddress::fromJsonObject(const QJsonObject &object)
GooglePeople::EmailAddress GooglePeople::EmailAddress::fromJsonObject(const QJsonObject &object, bool *)
{
EmailAddress ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -467,11 +483,20 @@ bool GooglePeople::Event::saveContactDetails(QContact *contact, const QList<Even
return true;
}

GooglePeople::Event GooglePeople::Event::fromJsonObject(const QJsonObject &object)
GooglePeople::Event GooglePeople::Event::fromJsonObject(const QJsonObject &object, bool *error)
{
bool dateOk = false;
const QDate date = jsonObjectToDate(object.value("date").toObject(), &dateOk);
if (error) {
*error = !dateOk;
}
if (!dateOk) {
return Event();
}

Event ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
ret.date = jsonObjectToDate(object.value("date").toObject());
ret.date = date;
ret.type = object.value("type").toString();
return ret;
}
Expand Down Expand Up @@ -568,7 +593,7 @@ bool GooglePeople::Membership::saveContactDetails(
return true;
}

GooglePeople::Membership GooglePeople::Membership::fromJsonObject(const QJsonObject &object)
GooglePeople::Membership GooglePeople::Membership::fromJsonObject(const QJsonObject &object, bool *)
{
Membership ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -631,7 +656,7 @@ bool GooglePeople::Name::saveContactDetails(QContact *contact, const QList<Name>
return saveContactDetail(contact, &detail);
}

GooglePeople::Name GooglePeople::Name::fromJsonObject(const QJsonObject &object)
GooglePeople::Name GooglePeople::Name::fromJsonObject(const QJsonObject &object, bool *)
{
Name ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -676,7 +701,7 @@ bool GooglePeople::Nickname::saveContactDetails(QContact *contact, const QList<N
return true;
}

GooglePeople::Nickname GooglePeople::Nickname::fromJsonObject(const QJsonObject &object)
GooglePeople::Nickname GooglePeople::Nickname::fromJsonObject(const QJsonObject &object, bool *)
{
Nickname ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -718,7 +743,7 @@ bool GooglePeople::Organization::saveContactDetails(QContact *contact, const QLi
return true;
}

GooglePeople::Organization GooglePeople::Organization::fromJsonObject(const QJsonObject &object)
GooglePeople::Organization GooglePeople::Organization::fromJsonObject(const QJsonObject &object, bool *)
{
Organization ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -795,7 +820,7 @@ bool GooglePeople::PhoneNumber::saveContactDetails(QContact *contact, const QLis
return true;
}

GooglePeople::PhoneNumber GooglePeople::PhoneNumber::fromJsonObject(const QJsonObject &object)
GooglePeople::PhoneNumber GooglePeople::PhoneNumber::fromJsonObject(const QJsonObject &object, bool *)
{
PhoneNumber ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -882,7 +907,7 @@ QString GooglePeople::PersonMetadata::etag(const QContact &contact)
return sourceInfo.value("etag").toString();
}

GooglePeople::PersonMetadata GooglePeople::PersonMetadata::fromJsonObject(const QJsonObject &object)
GooglePeople::PersonMetadata GooglePeople::PersonMetadata::fromJsonObject(const QJsonObject &object, bool *)
{
PersonMetadata ret;
ret.sources = jsonArrayToList<Source>(object.value("sources").toArray());
Expand Down Expand Up @@ -961,7 +986,7 @@ bool GooglePeople::Photo::saveContactDetails(QContact *contact, const QList<Phot
return true;
}

GooglePeople::Photo GooglePeople::Photo::fromJsonObject(const QJsonObject &object)
GooglePeople::Photo GooglePeople::Photo::fromJsonObject(const QJsonObject &object, bool *)
{
Photo ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -1008,7 +1033,7 @@ bool GooglePeople::Url::saveContactDetails(QContact *contact, const QList<Url> &
return true;
}

GooglePeople::Url GooglePeople::Url::fromJsonObject(const QJsonObject &object)
GooglePeople::Url GooglePeople::Url::fromJsonObject(const QJsonObject &object, bool *)
{
Url ret;
ret.metadata = FieldMetadata::fromJsonObject(object.value("metadata").toObject());
Expand Down Expand Up @@ -1091,7 +1116,7 @@ GooglePeople::Person GooglePeople::Person::fromJsonObject(const QJsonObject &obj
{
Person ret;
ret.resourceName = object.value("resourceName").toString();
ret.metadata = PersonMetadata::fromJsonObject(object.value("metadata").toObject());
ret.metadata = PersonMetadata::fromJsonObject(object.value("metadata").toObject(), nullptr);
ret.addresses = jsonArrayToList<Address>(object.value("addresses").toArray());
ret.biographies = jsonArrayToList<Biography>(object.value("biographies").toArray());
ret.birthdays = jsonArrayToList<Birthday>(object.value("birthdays").toArray());
Expand Down
28 changes: 14 additions & 14 deletions src/google/google-contacts/googlepeoplejson.h
Expand Up @@ -45,7 +45,7 @@ namespace GooglePeople
ProfileMetadata profileMetadata;
*/

static Source fromJsonObject(const QJsonObject &obj);
static Source fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
};

class FieldMetadata
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace GooglePeople
QString countryCode;

static bool saveContactDetails(QContact *contact, const QList<Address> &values);
static Address fromJsonObject(const QJsonObject &obj);
static Address fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -90,7 +90,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Biography> &values);
static Biography fromJsonObject(const QJsonObject &obj);
static Biography fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -105,7 +105,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Birthday> &values);
static Birthday fromJsonObject(const QJsonObject &obj);
static Birthday fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -119,7 +119,7 @@ namespace GooglePeople
QString displayName;

static bool saveContactDetails(QContact *contact, const QList<EmailAddress> &values);
static EmailAddress fromJsonObject(const QJsonObject &obj);
static EmailAddress fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -135,7 +135,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Event> &values);
static Event fromJsonObject(const QJsonObject &obj);
static Event fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -155,7 +155,7 @@ namespace GooglePeople
const QList<Membership> &values,
int accountId,
const QList<QContactCollection> &candidateCollections);
static Membership fromJsonObject(const QJsonObject &obj);
static Membership fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -182,7 +182,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Name> &values);
static Name fromJsonObject(const QJsonObject &obj);
static Name fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -197,7 +197,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Nickname> &values);
static Nickname fromJsonObject(const QJsonObject &obj);
static Nickname fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -222,7 +222,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<Organization> &values);
static Organization fromJsonObject(const QJsonObject &obj);
static Organization fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -239,7 +239,7 @@ namespace GooglePeople
*/

static bool saveContactDetails(QContact *contact, const QList<PhoneNumber> &values);
static PhoneNumber fromJsonObject(const QJsonObject &obj);
static PhoneNumber fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -254,7 +254,7 @@ namespace GooglePeople
static QString etag(const QContact &contact);

static bool saveContactDetails(QContact *contact, const PersonMetadata &value);
static PersonMetadata fromJsonObject(const QJsonObject &obj);
static PersonMetadata fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonObject toJsonObject(const QContact &contact);
};

Expand All @@ -270,7 +270,7 @@ namespace GooglePeople
QString *localAvatarFile = nullptr);

static bool saveContactDetails(QContact *contact, const QList<Photo> &values);
static Photo fromJsonObject(const QJsonObject &obj);
static Photo fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand All @@ -283,7 +283,7 @@ namespace GooglePeople
QString formattedType;

static bool saveContactDetails(QContact *contact, const QList<Url> &values);
static Url fromJsonObject(const QJsonObject &obj);
static Url fromJsonObject(const QJsonObject &obj, bool *error = nullptr);
static QJsonArray jsonValuesForContact(const QContact &contact, bool *hasChanges);
};

Expand Down
41 changes: 36 additions & 5 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.cpp
Expand Up @@ -188,6 +188,33 @@ void GoogleContactSqliteSyncAdaptor::syncFinishedSuccessfully()
void GoogleContactSqliteSyncAdaptor::syncFinishedWithError()
{
SOCIALD_LOG_ERROR("Sync finished with error");

if (q->m_collection.id().isNull()) {
return;
}

// If sync fails, clear the sync token and date for the collection, so that the next sync
// requests a full contact listing, to ensure we are up-to-date with the server.

q->m_collection.setExtendedMetaData(CollectionKeySyncToken, QString());
q->m_collection.setExtendedMetaData(CollectionKeySyncTokenDate, QString());

QHash<QContactCollection*, QList<QContact>* > modifiedCollections;
QList<QContact> emptyContacts;
modifiedCollections.insert(&q->m_collection, &emptyContacts);

QtContactsSqliteExtensions::ContactManagerEngine *cme = QtContactsSqliteExtensions::contactManagerEngine(*q->m_contactManager);
QContactManager::Error error = QContactManager::NoError;

if (!cme->storeChanges(nullptr,
&modifiedCollections,
QList<QContactCollectionId>(),
QtContactsSqliteExtensions::ContactManagerEngine::PreserveLocalChanges,
true,
&error)) {
SOCIALD_LOG_ERROR("Failed to clear sync token for account:" << q->m_accountId
<< "due to error:" << error);
}
}

//-------------------------------------
Expand Down Expand Up @@ -419,7 +446,8 @@ void GoogleTwoWayContactSyncAdaptor::contactsFinishedHandler()

if (reply->error() == QNetworkReply::ProtocolInvalidOperationError) {
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 400) {
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 400
&& !m_retriedConnectionsList) {
SOCIALD_LOG_INFO("Will request new sync token, got error from server:"
<< reply->readAll());
DataRequestType requestType = static_cast<DataRequestType>(
Expand All @@ -428,6 +456,7 @@ void GoogleTwoWayContactSyncAdaptor::contactsFinishedHandler()
reply->property("contactChangeNotifier").toInt());
m_connectionsListParams.requestSyncToken = true;
m_connectionsListParams.syncToken.clear();
m_retriedConnectionsList = true;
requestData(requestType, contactChangeNotifier);
decrementSemaphore(m_accountId);
return;
Expand Down Expand Up @@ -1108,12 +1137,14 @@ void GoogleTwoWayContactSyncAdaptor::purgeAccount(int pid)

void GoogleTwoWayContactSyncAdaptor::finalize(int accountId)
{
if (syncAborted()|| status() == SocialNetworkSyncAdaptor::Error) {
m_sqliteSync->syncFinishedWithError();
return;
}

if (accountId != m_accountId
|| m_accessToken.isEmpty()
|| syncAborted()
|| status() == SocialNetworkSyncAdaptor::Error) {
|| m_accessToken.isEmpty()) {
// account failure occurred before sync process was started,
// or other error occurred during sync.
// in this case we have nothing left to do except cleanup.
return;
}
Expand Down
Expand Up @@ -173,6 +173,7 @@ class GoogleTwoWayContactSyncAdaptor : public GoogleDataTypeSyncAdaptor

int m_accountId = 0;
int m_apiRequestsRemaining = 0;
bool m_retriedConnectionsList = false;
bool m_allowFinalCleanup = false;
};

Expand Down

0 comments on commit e0d5a9b

Please sign in to comment.