Skip to content

Commit

Permalink
WIP: JB#45600
Browse files Browse the repository at this point in the history
Don't store organizer/attendee information if that simply refers
to the owning calendar id.
  • Loading branch information
Chris Adams committed May 23, 2019
1 parent 081d169 commit 539e240
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
67 changes: 46 additions & 21 deletions src/google/google-calendars/googlecalendarsyncadaptor.cpp
Expand Up @@ -654,32 +654,44 @@ void extractRecurrence(const QJsonArray &recurrence, KCalCore::Event::Ptr event,
}
}

void extractOrganizer(const QJsonObject &creatorObj, const QJsonObject &organizerObj, KCalCore::Event::Ptr event)
void extractOrganizer(const QJsonObject &creatorObj, const QJsonObject &organizerObj, KCalCore::Event::Ptr event, const GoogleCalendarSyncAdaptor::CalendarInfo &calendarInfo)
{
if (!organizerObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
|| !organizerObj.value(QLatin1String("email")).toVariant().toString().isEmpty()) {
const QString organizerEmail = organizerObj.value(QLatin1String("email")).toVariant().toString();
if (organizerEmail != calendarInfo.calendarId || calendarInfo.access != GoogleCalendarSyncAdaptor::Owner) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
organizerObj.value(QLatin1String("displayName")).toVariant().toString(),
organizerObj.value(QLatin1String("email")).toVariant().toString()));
organizerEmail));
event->setOrganizer(organizer);
} else if (!creatorObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
return;
}
}

if (!creatorObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
|| !creatorObj.value(QLatin1String("email")).toVariant().toString().isEmpty()) {
const QString creatorEmail = creatorObj.value(QLatin1String("email")).toVariant().toString();
if (creatorEmail != calendarInfo.calendarId || calendarInfo.access != GoogleCalendarSyncAdaptor::Owner) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
creatorObj.value(QLatin1String("displayName")).toVariant().toString(),
creatorObj.value(QLatin1String("email")).toVariant().toString()));
creatorEmail));
event->setOrganizer(organizer);
} else {
return;
}
}

KCalCore::Person::Ptr organizer(new KCalCore::Person);
event->setOrganizer(organizer);
}
}

void extractAttendees(const QJsonArray &attendees, KCalCore::Event::Ptr event)
void extractAttendees(const QJsonArray &attendees, KCalCore::Event::Ptr event, const GoogleCalendarSyncAdaptor::CalendarInfo &calendarInfo)
{
event->clearAttendees();
for (int i = 0; i < attendees.size(); ++i) {
QJsonObject attendeeObj = attendees.at(i).toObject();
if (!attendeeObj.value(QLatin1String("organizer")).toVariant().toBool()) {
const QString attendeeEmail = attendeeObj.value(QLatin1String("email")).toVariant().toString();
if (attendeeEmail != calendarInfo.calendarId || calendarInfo.access != GoogleCalendarSyncAdaptor::Owner) {
KCalCore::Attendee::Ptr attendee(new KCalCore::Attendee(
attendeeObj.value(QLatin1String("displayName")).toVariant().toString(),
attendeeObj.value(QLatin1String("email")).toVariant().toString()));
Expand All @@ -704,21 +716,25 @@ void extractAttendees(const QJsonArray &attendees, KCalCore::Event::Ptr event)
}
attendee->setRSVP(true);
event->addAttendee(attendee);
}
} else {
KCalCore::Person::Ptr calOrganizer = event->organizer();
if (!calOrganizer.isNull() && !calOrganizer->isEmpty()) {
continue;
}
if (!attendeeObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
|| !attendeeObj.value(QLatin1String("email")).toVariant().toString().isEmpty()) {
const QString attendeeEmail = attendeeObj.value(QLatin1String("email")).toVariant().toString();
if (attendeeEmail != calendarInfo.calendarId || calendarInfo.access != GoogleCalendarSyncAdaptor::Owner) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
attendeeObj.value(QLatin1String("displayName")).toVariant().toString(),
attendeeObj.value(QLatin1String("email")).toVariant().toString()));
attendeeEmail));
event->setOrganizer(organizer);
}
}
}
}
}

#define START_EVENT_UPDATES_IF_REQUIRED(event, changed) \
if (*changed == false) { \
Expand Down Expand Up @@ -815,7 +831,7 @@ void extractAlarms(const QJsonObject &json, KCalCore::Event::Ptr event, int defa
}
}

void jsonToKCal(const QJsonObject &json, KCalCore::Event::Ptr event, int defaultReminderStartOffset, KCalCore::ICalFormat &icalFormat, bool *changed)
void jsonToKCal(const QJsonObject &json, KCalCore::Event::Ptr event, const GoogleCalendarSyncAdaptor::CalendarInfo &calendarInfo, int defaultReminderStartOffset, KCalCore::ICalFormat &icalFormat, bool *changed)
{
bool alreadyStarted = *changed; // if this is true, we don't need to call startUpdates/endUpdates() in this function.
if (!alreadyStarted && gCalETag(event) == json.value(QLatin1String("etag")).toVariant().toString()) {
Expand All @@ -839,8 +855,8 @@ void jsonToKCal(const QJsonObject &json, KCalCore::Event::Ptr event, int default
}
setRemoteUidCustomField(event, json.value(QLatin1String("iCalUID")).toVariant().toString(), json.value(QLatin1String("id")).toVariant().toString());
extractRecurrence(json.value(QLatin1String("recurrence")).toArray(), event, icalFormat);
extractOrganizer(json.value(QLatin1String("creator")).toObject(), json.value(QLatin1String("organizer")).toObject(), event);
extractAttendees(json.value(QLatin1String("attendees")).toArray(), event);
extractOrganizer(json.value(QLatin1String("creator")).toObject(), json.value(QLatin1String("organizer")).toObject(), event, calendarInfo);
extractAttendees(json.value(QLatin1String("attendees")).toArray(), event, calendarInfo);
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, isReadOnly, setReadOnly, json.value(QLatin1String("locked")).toVariant().toBool(), changed)
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, summary, setSummary, json.value(QLatin1String("summary")).toVariant().toString(), changed)
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, description, setDescription, json.value(QLatin1String("description")).toVariant().toString(), changed)
Expand Down Expand Up @@ -876,13 +892,13 @@ bool remoteModificationIsReal(const QJsonObject &json, KCalCore::Event::Ptr even
return false; // this event has not changed server-side since we last saw it.
}

bool localModificationIsReal(const QJsonObject &local, const QJsonObject &remote, int defaultReminderStartOffset, KCalCore::ICalFormat &icalFormat)
bool localModificationIsReal(const QJsonObject &local, const QJsonObject &remote, const GoogleCalendarSyncAdaptor::CalendarInfo &calendarInfo, int defaultReminderStartOffset, KCalCore::ICalFormat &icalFormat)
{
bool changed = true;
KCalCore::Event::Ptr localEvent = KCalCore::Event::Ptr(new KCalCore::Event);
KCalCore::Event::Ptr remoteEvent = KCalCore::Event::Ptr(new KCalCore::Event);
jsonToKCal(local, localEvent, defaultReminderStartOffset, icalFormat, &changed);
jsonToKCal(remote, remoteEvent, defaultReminderStartOffset, icalFormat, &changed);
jsonToKCal(local, localEvent, calendarInfo, defaultReminderStartOffset, icalFormat, &changed);
jsonToKCal(remote, remoteEvent, calendarInfo, defaultReminderStartOffset, icalFormat, &changed);
if (GoogleCalendarIncidenceComparator::incidencesEqual(localEvent, remoteEvent, true)) {
return false; // they're equal, so the local modification is not real.
}
Expand Down Expand Up @@ -1210,6 +1226,7 @@ void GoogleCalendarSyncAdaptor::calendarsFinishedHandler()
QString accessRole = currCalendar.value(QStringLiteral("accessRole")).toString();
if (accessRole == QStringLiteral("owner") || accessRole == QStringLiteral("writer")) {
GoogleCalendarSyncAdaptor::CalendarInfo currCalendarInfo;
currCalendarInfo.calendarId = currCalendar.value(QStringLiteral("id")).toString();
currCalendarInfo.color = currCalendar.value(QStringLiteral("backgroundColor")).toString();
currCalendarInfo.summary = currCalendar.value(QStringLiteral("summary")).toString();
currCalendarInfo.description = currCalendar.value(QStringLiteral("description")).toString();
Expand All @@ -1219,8 +1236,7 @@ void GoogleCalendarSyncAdaptor::calendarsFinishedHandler()
} else {
currCalendarInfo.access = Writer;
}
QString currCalendarId = currCalendar.value(QStringLiteral("id")).toString();
m_serverCalendarIdToCalendarInfo[accountId].insert(currCalendarId, currCalendarInfo);
m_serverCalendarIdToCalendarInfo[accountId].insert(currCalendarInfo.calendarId, currCalendarInfo);
}
}
}
Expand Down Expand Up @@ -1850,7 +1866,10 @@ QList<GoogleCalendarSyncAdaptor::UpsyncChange> GoogleCalendarSyncAdaptor::determ
if (event) {
QJsonObject localEventData = kCalToJson(event, m_icalFormat);
if (unchangedRemoteModifications.contains(updatedGcalId)
&& !localModificationIsReal(localEventData, unchangedRemoteModifications.value(updatedGcalId), m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat)) {
&& !localModificationIsReal(localEventData, unchangedRemoteModifications.value(updatedGcalId),
m_serverCalendarIdToCalendarInfo[accountId].value(calendarId),
m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId),
m_icalFormat)) {
// this local modification is spurious. It may have been reported
// due to the timestamp resolution issue, but in any case the
// event does not differ from the remote one.
Expand Down Expand Up @@ -1909,7 +1928,10 @@ QList<GoogleCalendarSyncAdaptor::UpsyncChange> GoogleCalendarSyncAdaptor::determ
// being reported as a local addition/modification due to the "since" timestamp
// overlap.
if (unchangedRemoteModifications.contains(gcalId)
&& !localModificationIsReal(localEventData, unchangedRemoteModifications.value(gcalId), m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat)) {
&& !localModificationIsReal(localEventData, unchangedRemoteModifications.value(gcalId),
m_serverCalendarIdToCalendarInfo[accountId].value(calendarId),
m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId),
m_icalFormat)) {
// this local addition is spurious. It may have been reported
// due to the timestamp resolution issue, but in any case the
// event does not differ from the remote one which is already updated.
Expand Down Expand Up @@ -2303,7 +2325,8 @@ void GoogleCalendarSyncAdaptor::updateLocalCalendarNotebookEvents(int accountId,
SOCIALD_LOG_DEBUG("Event modified remotely:" << eventId);
KCalCore::Event::Ptr event = allLocalEventsMap.value(eventId);
bool changed = false; // modification, not insert, so initially changed = "false".
jsonToKCal(eventData, event, m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed);
jsonToKCal(eventData, event, m_serverCalendarIdToCalendarInfo[accountId].value(calendarId),
m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed);
} break;
case GoogleCalendarSyncAdaptor::Insert: {
// add a new local event for the remote addition.
Expand Down Expand Up @@ -2345,7 +2368,8 @@ void GoogleCalendarSyncAdaptor::updateLocalCalendarNotebookEvents(int accountId,
}
}
bool changed = true; // set to true as it's an addition, no need to check for delta.
jsonToKCal(eventData, event, m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed); // direct conversion
jsonToKCal(eventData, event, m_serverCalendarIdToCalendarInfo[accountId].value(calendarId),
m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed); // direct conversion
if (!m_calendar->addEvent(event, googleNotebook->uid())) {
SOCIALD_LOG_ERROR("Could not add dissociated occurrence to calendar:" << parentId << recurrenceId.toString());
m_syncSucceeded[accountId] = false;
Expand All @@ -2365,7 +2389,8 @@ void GoogleCalendarSyncAdaptor::updateLocalCalendarNotebookEvents(int accountId,
const QJsonObject eventData(remoteChange.second);
// all changes are modifications to existing events, since it was an upsync response.
bool changed = false;
jsonToKCal(eventData, event, m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed);
jsonToKCal(eventData, event, m_serverCalendarIdToCalendarInfo[accountId].value(calendarId),
m_serverCalendarIdToDefaultReminderTimes[accountId].value(calendarId), m_icalFormat, &changed);
if (changed) {
SOCIALD_LOG_DEBUG("Two-way calendar sync with account" << accountId << ": re-updating event:" << event->summary());
m_storageNeedsSave = true;
Expand Down
30 changes: 16 additions & 14 deletions src/google/google-calendars/googlecalendarsyncadaptor.h
Expand Up @@ -45,12 +45,6 @@ class GoogleCalendarSyncAdaptor : public GoogleDataTypeSyncAdaptor
QString syncServiceName() const;
void sync(const QString &dataTypeString, int accountId);

protected: // implementing GoogleDataTypeSyncAdaptor interface
void purgeDataForOldAccount(int oldId, SocialNetworkSyncAdaptor::PurgeMode mode);
void beginSync(int accountId, const QString &accessToken);
void finalCleanup();

private:
enum ChangeType {
NoChange = 0,
Insert = 1,
Expand All @@ -68,6 +62,22 @@ class GoogleCalendarSyncAdaptor : public GoogleDataTypeSyncAdaptor
Owner = 4
};

struct CalendarInfo {
CalendarInfo() : change(NoChange), access(NoAccess) {}
QString calendarId;
QString summary;
QString description;
QString color;
ChangeType change;
AccessRole access;
};

protected: // implementing GoogleDataTypeSyncAdaptor interface
void purgeDataForOldAccount(int oldId, SocialNetworkSyncAdaptor::PurgeMode mode);
void beginSync(int accountId, const QString &accessToken);
void finalCleanup();

private:
struct UpsyncChange {
UpsyncChange() : accountId(0), upsyncType(NoChange) {}
int accountId;
Expand Down Expand Up @@ -105,14 +115,6 @@ private Q_SLOTS:
void upsyncFinishedHandler();

private:
struct CalendarInfo {
CalendarInfo() : change(NoChange), access(NoAccess) {}
QString summary;
QString description;
QString color;
ChangeType change;
AccessRole access;
};
QMap<int, QMap<QString, CalendarInfo> > m_serverCalendarIdToCalendarInfo;
QMap<int, QMap<QString, int> > m_serverCalendarIdToDefaultReminderTimes;
QMap<int, QMultiMap<QString, QJsonObject> > m_calendarIdToEventObjects;
Expand Down

0 comments on commit 539e240

Please sign in to comment.