Skip to content

Commit

Permalink
[sociald] Perform upload step of two-way sync only if required. Contr…
Browse files Browse the repository at this point in the history
…ibutes to JB#13718

This commit ensures that the Google contacts and calendars sync
adaptors only upsync local modifications if the direction setting
in the sync profile dictates that it should.

Contributes to JB#13718
  • Loading branch information
Chris Adams committed Apr 15, 2014
1 parent b060efd commit 2ac4c84
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/google/google-calendars/google.Calendars.xml
Expand Up @@ -10,6 +10,6 @@
<schedule enabled="false" interval="" days="1,2,3,4,5,6,7" syncconfiguredtime="" time="05:00:00" />

<profile name="google-calendars" type="client" >
<key name="Sync Direction" value="from-remote" />
<key name="Sync Direction" value="two-way" />
</profile>
</profile>
68 changes: 37 additions & 31 deletions src/google/google-calendars/googlecalendarsyncadaptor.cpp
Expand Up @@ -836,43 +836,49 @@ void GoogleCalendarSyncAdaptor::updateLocalCalendarNotebookEvents(int accountId,
.arg(googleNotebook->name()).arg(accountId)
.arg(remoteAdded).arg(remoteModified).arg(remoteRemoved));

// only upsync changes if we're doing a delta sync.
if (since.isValid()) {
// And push our changes up to the server. XXX TODO: Request Batching!
int localAdded = 0, localModified = 0, localRemoved = 0;

// first, push up deletions.
Q_FOREACH (const QString &deletedGcalId, deletedMap.keys()) {
QString incidenceUid = deletedMap.value(deletedGcalId);
localRemoved++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncDelete,
incidenceUid, calendarId, deletedGcalId, QByteArray());
}
// only upsync changes if we're doing a delta sync, and upsync is enabled
if (!m_accountSyncProfile || m_accountSyncProfile->syncDirection() != Buteo::SyncProfile::SYNC_DIRECTION_FROM_REMOTE) {
if (since.isValid()) {
// And push our changes up to the server. XXX TODO: Request Batching!
int localAdded = 0, localModified = 0, localRemoved = 0;

// first, push up deletions.
Q_FOREACH (const QString &deletedGcalId, deletedMap.keys()) {
QString incidenceUid = deletedMap.value(deletedGcalId);
localRemoved++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncDelete,
incidenceUid, calendarId, deletedGcalId, QByteArray());
}

// second, push up modifications.
Q_FOREACH (const QString &updatedGcalId, updatedMap.keys()) {
KCalCore::Event::Ptr event = updatedMap.value(updatedGcalId);
if (event) {
localModified++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncModify,
event->uid(), calendarId, updatedGcalId, QJsonDocument(kCalToJson(event, m_icalFormat)).toJson());
// second, push up modifications.
Q_FOREACH (const QString &updatedGcalId, updatedMap.keys()) {
KCalCore::Event::Ptr event = updatedMap.value(updatedGcalId);
if (event) {
localModified++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncModify,
event->uid(), calendarId, updatedGcalId, QJsonDocument(kCalToJson(event, m_icalFormat)).toJson());
}
}
}

// finally, push up insertions.
Q_FOREACH (KCalCore::Incidence::Ptr incidence, addedList) {
KCalCore::Event::Ptr event = m_calendar->event(incidence->uid());
if (event) {
localAdded++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncInsert,
event->uid(), calendarId, QString(), QJsonDocument(kCalToJson(event, m_icalFormat)).toJson());
// finally, push up insertions.
Q_FOREACH (KCalCore::Incidence::Ptr incidence, addedList) {
KCalCore::Event::Ptr event = m_calendar->event(incidence->uid());
if (event) {
localAdded++;
upsyncChanges(accountId, accessToken, GoogleCalendarSyncAdaptor::UpsyncInsert,
event->uid(), calendarId, QString(), QJsonDocument(kCalToJson(event, m_icalFormat)).toJson());
}
}
}

TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("Delta sync with Google calendar %1 for account %2: local A/M/R: %3 / %4 / %5"))
.arg(googleNotebook->name()).arg(accountId)
.arg(localAdded).arg(localModified).arg(localRemoved));
}
} else {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("Delta sync with Google calendar %1 for account %2: local A/M/R: %3 / %4 / %5"))
.arg(googleNotebook->name()).arg(accountId)
.arg(localAdded).arg(localModified).arg(localRemoved));
QString(QLatin1String("skipping upload of local calendar changes due to profile direction setting for account %1"))
.arg(accountId));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/google/google-contacts/google.Contacts.xml
Expand Up @@ -10,6 +10,6 @@
<schedule enabled="false" interval="" days="1,2,3,4,5,6,7" syncconfiguredtime="" time="05:00:00" />

<profile name="google-contacts" type="client" >
<key name="Sync Direction" value="from-remote" />
<key name="Sync Direction" value="two-way" />
</profile>
</profile>
49 changes: 28 additions & 21 deletions src/google/google-contacts/googletwowaycontactsyncadaptor.cpp
Expand Up @@ -493,32 +493,39 @@ bool GoogleTwoWayContactSyncAdaptor::testAccountProvenance(const QContact &conta
void GoogleTwoWayContactSyncAdaptor::upsyncLocalChangesList(int accountId)
{
bool postedData = false;
QMultiMap<GoogleContactStream::UpdateType, QPair<QContact, QStringList> > batch;
for (int i = m_localChanges[accountId].size() - 1; i >= 0; --i) {
QPair<QContact, GoogleContactStream::UpdateType> entry = m_localChanges[accountId].takeAt(i);
QStringList extraXmlElements = m_unsupportedXmlElements[accountId].value(entry.first.detail<QContactGuid>().guid());
if (entry.second == GoogleContactStream::Add) {
// new contacts need to be inserted into the My Contacts group
QString myContactsGroupAtomId = m_myContactsGroupAtomIds[accountId];
if (myContactsGroupAtomId.isEmpty()) {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("skipping upload of locally added contact %1 to account %2 due to unknown My Contacts group atom id"))
.arg(entry.first.id().toString()).arg(accountId));
if (!m_accountSyncProfile || m_accountSyncProfile->syncDirection() != Buteo::SyncProfile::SYNC_DIRECTION_FROM_REMOTE) {
// two-way sync is the default setting. Upsync the changes.
QMultiMap<GoogleContactStream::UpdateType, QPair<QContact, QStringList> > batch;
for (int i = m_localChanges[accountId].size() - 1; i >= 0; --i) {
QPair<QContact, GoogleContactStream::UpdateType> entry = m_localChanges[accountId].takeAt(i);
QStringList extraXmlElements = m_unsupportedXmlElements[accountId].value(entry.first.detail<QContactGuid>().guid());
if (entry.second == GoogleContactStream::Add) {
// new contacts need to be inserted into the My Contacts group
QString myContactsGroupAtomId = m_myContactsGroupAtomIds[accountId];
if (myContactsGroupAtomId.isEmpty()) {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("skipping upload of locally added contact %1 to account %2 due to unknown My Contacts group atom id"))
.arg(entry.first.id().toString()).arg(accountId));
} else {
extraXmlElements.append(QStringLiteral("<gContact:groupMembershipInfo deleted=\"false\" href=\"%1\"></gContact:groupMembershipInfo>").arg(myContactsGroupAtomId));
batch.insertMulti(entry.second, qMakePair(entry.first, extraXmlElements));
}
} else {
extraXmlElements.append(QStringLiteral("<gContact:groupMembershipInfo deleted=\"false\" href=\"%1\"></gContact:groupMembershipInfo>").arg(myContactsGroupAtomId));
batch.insertMulti(entry.second, qMakePair(entry.first, extraXmlElements));
}
} else {
batch.insertMulti(entry.second, qMakePair(entry.first, extraXmlElements));
}

if (batch.size() == SOCIALD_GOOGLE_MAX_CONTACT_ENTRY_RESULTS || i == 0) {
GoogleContactStream encoder(false, accountId, m_emailAddresses[accountId]);
QByteArray encodedContactUpdates = encoder.encode(batch);
storeToRemote(accountId, m_accessTokens[accountId], encodedContactUpdates);
postedData = true;
break;
if (batch.size() == SOCIALD_GOOGLE_MAX_CONTACT_ENTRY_RESULTS || i == 0) {
GoogleContactStream encoder(false, accountId, m_emailAddresses[accountId]);
QByteArray encodedContactUpdates = encoder.encode(batch);
storeToRemote(accountId, m_accessTokens[accountId], encodedContactUpdates);
postedData = true;
break;
}
}
} else {
TRACE(SOCIALD_INFORMATION,
QString(QLatin1String("skipping upload of local contacts changes due to profile direction setting for account %1"))
.arg(accountId));
}

if (!postedData) {
Expand Down

0 comments on commit 2ac4c84

Please sign in to comment.