Skip to content

Latest commit

 

History

History
2424 lines (2259 loc) · 127 KB

googlecalendarsyncadaptor.cpp

File metadata and controls

2424 lines (2259 loc) · 127 KB
 
1
2
/****************************************************************************
**
Jun 13, 2014
Jun 13, 2014
3
** Copyright (C) 2013-2014 Jolla Ltd.
4
5
** Contact: Chris Adams <chris.adams@jollamobile.com>
**
Jun 13, 2014
Jun 13, 2014
6
7
8
9
10
11
12
13
14
15
16
17
18
19
** This program/library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation.
**
** This program/library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program/library; if not, write to the Free
** Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
** 02110-1301 USA
**
20
21
22
****************************************************************************/
#include "googlecalendarsyncadaptor.h"
Sep 15, 2015
Sep 15, 2015
23
#include "googlecalendarincidencecomparator.h"
24
25
26
27
28
#include "trace.h"
#include <QtCore/QUrlQuery>
#include <QtCore/QFile>
#include <QtCore/QDir>
Feb 10, 2014
Feb 10, 2014
29
#include <QtCore/QByteArray>
30
31
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
Feb 10, 2014
Feb 10, 2014
32
33
#include <QtCore/QJsonDocument>
#include <QtCore/QSettings>
Dec 18, 2018
Dec 18, 2018
34
#include <QtCore/QSet>
35
36
37
38
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
Jan 14, 2015
Jan 14, 2015
39
#include <ksystemtimezone.h>
Mar 30, 2015
Mar 30, 2015
40
#include <kdatetime.h>
Jan 14, 2015
Jan 14, 2015
41
Feb 10, 2014
Feb 10, 2014
42
//----------------------------------------------
Feb 10, 2014
Feb 10, 2014
44
45
#define RFC3339_FORMAT "%Y-%m-%dT%H:%M:%S%:z"
#define RFC3339_FORMAT_NTZC "%Y-%m-%dT%H:%M:%S%z"
Apr 29, 2015
Apr 29, 2015
46
47
#define RFC3339_QDATE_FORMAT_MS "yyyy-MM-ddThh:mm:ss.zzzZ"
#define RFC3339_QDATE_FORMAT_MS_NTZC "yyyy-MM-ddThh:mm:ss.zzz"
Feb 10, 2014
Feb 10, 2014
48
49
50
#define KDATEONLY_FORMAT "%Y-%m-%d"
#define QDATEONLY_FORMAT "yyyy-MM-dd"
#define KLONGTZ_FORMAT "%:Z"
Jan 14, 2015
Jan 14, 2015
51
52
53
#define RFC5545_KDATETIME_FORMAT "%Y%m%dT%H%M%SZ"
#define RFC5545_KDATETIME_FORMAT_NTZC "%Y%m%dT%H%M%S"
#define RFC5545_QDATE_FORMAT "yyyyMMdd"
Feb 10, 2014
Feb 10, 2014
54
55
56
namespace {
Jan 13, 2015
Jan 13, 2015
57
58
static int GOOGLE_CAL_SYNC_PLUGIN_VERSION = 2;
Dec 18, 2018
Dec 18, 2018
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
int nearestNemoReminderStartOffset(int googleStartOffset)
{
// Google supports arbitrary start offsets, whereas in Nemo's UI
// we only allow specific reminder offsets.
// See nemo-qml-plugin-calendar::NemoCalendarEvent::Reminder for
// those offset definitions.
// Also, Nemo reminder offsets are negative and in seconds,
// whereas Google start offsets are positive and in minutes.
if (googleStartOffset >= 0 && googleStartOffset <= 5) {
return -5 * 60; // 5 minutes before event start
} else if (googleStartOffset > 5 && googleStartOffset <= 15) {
return -15 * 60; // 15 minutes before event start
} else if (googleStartOffset > 15 && googleStartOffset <= 30) {
return -30 * 60; // 30 minutes before event start
} else if (googleStartOffset > 30 && googleStartOffset <= 60) {
return -60 * 60; // 1 hour before event start
} else if (googleStartOffset > 60 && googleStartOffset <= 120) {
return -120 * 60; // 2 hours before event start
} else if (googleStartOffset > 120 && googleStartOffset <= 1440) {
return -1440 * 60; // 1 day before event start (24 hours)
} else if (googleStartOffset > 1440) {
return -2880 * 60; // 2 days before event start (48 hours)
}
// default reminder: 15 minutes before event start.
return -15 * 60;
}
int googleStartOffsetForReminderStartOffset(int seconds)
{
if (seconds >= 0) {
return 0;
} else if (seconds >= (-5 * 60)) {
return 5;
} else if (seconds >= (-15 * 60)) {
return 15;
} else if (seconds >= (-30 * 60)) {
return 30;
} else if (seconds >= (-60 * 60)) {
return 60;
} else if (seconds >= (-120 * 60)) {
return 120;
} else if (seconds >= (-1440 * 60)) {
return 1440;
} else {
return 2880;
}
}
Jan 14, 2015
Jan 14, 2015
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
void errorDumpStr(const QString &str)
{
// Dump the entire string to the log.
// Note that the log cannot handle newlines,
// so we separate the string into chunks.
Q_FOREACH (const QString &chunk, str.split('\n', QString::SkipEmptyParts)) {
SOCIALD_LOG_ERROR(chunk);
}
}
void traceDumpStr(const QString &str)
{
// 8 is the minimum log level for TRACE logs
// as defined in Buteo's LogMacros.h
if (Buteo::Logger::instance()->getLogLevel() < 8) {
return;
}
// Dump the entire string to the log.
// Note that the log cannot handle newlines,
// so we separate the string into chunks.
Q_FOREACH (const QString &chunk, str.split('\n', QString::SkipEmptyParts)) {
SOCIALD_LOG_TRACE(chunk);
}
}
Apr 20, 2015
Apr 20, 2015
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// returns true if the ghost-event cleanup sync has been performed.
bool ghostEventCleanupPerformed()
{
QString settingsFileName = QString::fromLatin1("%1/%2/gcal.ini")
.arg(QString::fromLatin1(PRIVILEGED_DATA_DIR))
.arg(QString::fromLatin1(SYNC_DATABASE_DIR));
QSettings settingsFile(settingsFileName, QSettings::IniFormat);
return settingsFile.value(QString::fromLatin1("cleaned"), QVariant::fromValue<bool>(false)).toBool();
}
void setGhostEventCleanupPerformed()
{
QString settingsFileName = QString::fromLatin1("%1/%2/gcal.ini")
.arg(QString::fromLatin1(PRIVILEGED_DATA_DIR))
.arg(QString::fromLatin1(SYNC_DATABASE_DIR));
QSettings settingsFile(settingsFileName, QSettings::IniFormat);
settingsFile.setValue(QString::fromLatin1("cleaned"), QVariant::fromValue<bool>(true));
settingsFile.sync();
}
Sep 5, 2016
Sep 5, 2016
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
void uniteIncidenceLists(const KCalCore::Incidence::List &first, KCalCore::Incidence::List *second)
{
int originalSecondSize = second->size();
bool foundMatch = false;
Q_FOREACH (KCalCore::Incidence::Ptr inc, first) {
foundMatch = false;
for (int i = 0; i < originalSecondSize; ++i) {
if (inc->uid() == second->at(i)->uid() && inc->recurrenceId() == second->at(i)->recurrenceId()) {
// found a match
foundMatch = true;
break;
}
}
if (!foundMatch) {
second->append(inc);
}
}
}
Feb 10, 2014
Feb 10, 2014
172
173
QString gCalEventId(KCalCore::Incidence::Ptr event)
{
Apr 29, 2015
Apr 29, 2015
174
175
176
177
178
179
180
181
182
// we abuse the comments field to store our gcal-id.
// we should use a custom property, but those are deleted on incidence deletion.
const QStringList &comments(event->comments());
Q_FOREACH (const QString &comment, comments) {
if (comment.startsWith("jolla-sociald:gcal-id:")) {
return comment.mid(22);
}
}
return QString();
Feb 10, 2014
Feb 10, 2014
183
}
Mar 9, 2018
Mar 9, 2018
184
Feb 10, 2014
Feb 10, 2014
185
186
void setGCalEventId(KCalCore::Incidence::Ptr event, const QString &id)
{
Apr 29, 2015
Apr 29, 2015
187
188
189
190
191
192
193
194
195
196
197
198
199
// we abuse the comments field to store our gcal-id.
// we should use a custom property, but those are deleted on incidence deletion.
const QStringList &comments(event->comments());
Q_FOREACH (const QString &comment, comments) {
if (comment.startsWith("jolla-sociald:gcal-id:")) {
// remove any existing gcal-id comment.
event->removeComment(comment);
break;
}
}
event->addComment(QStringLiteral("jolla-sociald:gcal-id:%1").arg(id));
}
Jun 28, 2017
Jun 28, 2017
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
void setRemoteUidCustomField(KCalCore::Incidence::Ptr event, const QString &uid, const QString &id)
{
// store it also in a custom property purely for invitation lookup purposes.
if (!uid.isEmpty()) {
event->setNonKDECustomProperty("X-SAILFISHOS-REMOTE-UID", uid.toUtf8());
} else {
// Google Calendar invites are sent as invitations with uid suffixed with @google.com.
if (id.endsWith(QLatin1String("@google.com"), Qt::CaseInsensitive)) {
event->setNonKDECustomProperty("X-SAILFISHOS-REMOTE-UID", id.toUtf8());
} else {
QString suffixedId = id;
suffixedId.append(QLatin1String("@google.com"));
event->setNonKDECustomProperty("X-SAILFISHOS-REMOTE-UID", suffixedId.toUtf8());
}
}
}
Jun 26, 2015
Jun 26, 2015
217
218
219
220
QString gCalETag(KCalCore::Incidence::Ptr event)
{
return event->customProperty("jolla-sociald", "gcal-etag");
}
Mar 9, 2018
Mar 9, 2018
221
Jun 26, 2015
Jun 26, 2015
222
223
224
225
226
227
void setGCalETag(KCalCore::Incidence::Ptr event, const QString &etag)
{
// note: custom properties are purged on incidence deletion.
event->setCustomProperty("jolla-sociald", "gcal-etag", etag);
}
Apr 29, 2015
Apr 29, 2015
228
229
230
231
232
233
234
235
KDateTime datetimeFromUpdateStr(const QString &update)
{
// generally, this is an RFC3339 date with timezone information, like:
// 2015-04-25T12:02:40.988Z
// However, our version of KDateTime is old enough that we don't support this
// date format directly.
bool tzIncluded = update.endsWith('Z');
QDateTime qdt = tzIncluded
Mar 4, 2016
Mar 4, 2016
236
237
? QLocale::c().toDateTime(update, RFC3339_QDATE_FORMAT_MS)
: QLocale::c().toDateTime(update, RFC3339_QDATE_FORMAT_MS_NTZC);
Apr 29, 2015
Apr 29, 2015
238
239
240
241
if (tzIncluded) {
qdt.setTimeSpec(Qt::UTC);
}
return KDateTime(qdt, tzIncluded ? KDateTime::UTC : KDateTime::ClockTime);
Feb 10, 2014
Feb 10, 2014
242
243
}
Jan 14, 2015
Jan 14, 2015
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
QList<KDateTime> datetimesFromExRDateStr(const QString &exrdatestr, bool *isDateOnly)
{
// possible forms:
// RDATE:19970714T123000Z
// RDATE;VALUE=DATE-TIME:19970714T123000Z
// RDATE;VALUE=DATE-TIME:19970714T123000Z,19970715T123000Z
// RDATE;TZID=America/New_York:19970714T083000
// RDATE;VALUE=PERIOD:19960403T020000Z/19960403T040000Z,19960404T010000Z/PT3H
// RDATE;VALUE=DATE:19970101,19970120
QList<KDateTime> retn;
QString str = exrdatestr;
*isDateOnly = false; // by default.
if (str.startsWith(QStringLiteral("exdate"), Qt::CaseInsensitive)) {
str.remove(0, 6);
} else if (str.startsWith(QStringLiteral("rdate"), Qt::CaseInsensitive)) {
str.remove(0, 5);
} else {
SOCIALD_LOG_ERROR("not an ex/rdate string:" << exrdatestr);
return retn;
}
if (str.startsWith(';')) {
str.remove(0,1);
if (str.startsWith("DATE-TIME:", Qt::CaseInsensitive)) {
str.remove(0, 10);
QStringList dts = str.split(',');
Q_FOREACH (const QString &dtstr, dts) {
if (dtstr.endsWith('Z')) {
// UTC
KDateTime kdt = KDateTime::fromString(dtstr, RFC5545_KDATETIME_FORMAT);
kdt.setTimeSpec(KDateTime::Spec::UTC());
retn.append(kdt);
} else {
// Floating time
KDateTime kdt = KDateTime::fromString(dtstr, RFC5545_KDATETIME_FORMAT_NTZC);
kdt.setTimeSpec(KDateTime::Spec::ClockTime());
retn.append(kdt);
}
}
} else if (str.startsWith("DATE:", Qt::CaseInsensitive)) {
str.remove(0, 5);
QStringList dts = str.split(',');
Q_FOREACH(const QString &dstr, dts) {
Mar 4, 2016
Mar 4, 2016
289
QDate date = QLocale::c().toDate(dstr, RFC5545_QDATE_FORMAT);
Jan 14, 2015
Jan 14, 2015
290
291
292
293
294
295
KDateTime kdt(date, KDateTime::Spec::ClockTime());
retn.append(kdt);
}
} else if (str.startsWith("PERIOD:", Qt::CaseInsensitive)) {
SOCIALD_LOG_ERROR("unsupported parameter in ex/rdate string:" << exrdatestr);
// TODO: support PERIOD formats, or just switch to CalDAV for Google sync...
Jan 15, 2015
Jan 15, 2015
296
} else if (str.startsWith("TZID=") && str.contains(':')) {
Jan 15, 2015
Jan 15, 2015
297
298
str.remove(0, 5);
QString tzidstr = str.mid(0, str.indexOf(':')); // something like: "Australia/Brisbane"
Jan 14, 2015
Jan 14, 2015
299
300
301
302
303
KTimeZone tz = KSystemTimeZones::zone(tzidstr);
str.remove(0, tzidstr.size()+1);
QStringList dts = str.split(',');
Q_FOREACH (const QString &dtstr, dts) {
KDateTime kdt = KDateTime::fromString(dtstr, RFC5545_KDATETIME_FORMAT_NTZC);
Jan 15, 2015
Jan 15, 2015
304
305
306
307
308
309
if (!kdt.isValid()) {
// try parsing from alternate formats
kdt = KDateTime::fromString(dtstr, RFC3339_FORMAT_NTZC);
}
if (!kdt.isValid()) {
SOCIALD_LOG_ERROR("unable to parse datetime from ex/rdate string:" << exrdatestr);
Jan 14, 2015
Jan 14, 2015
310
} else {
Jan 15, 2015
Jan 15, 2015
311
312
313
314
315
316
317
if (tz.isValid()) {
kdt.setTimeSpec(tz);
} else {
kdt.setTimeSpec(KDateTime::Spec::ClockTime());
SOCIALD_LOG_INFO("WARNING: unknown tzid:" << tzidstr << "; assuming clock-time instead!");
}
retn.append(kdt);
Jan 14, 2015
Jan 14, 2015
318
319
320
321
322
323
324
325
326
327
328
329
}
}
} else {
SOCIALD_LOG_ERROR("invalid parameter in ex/rdate string:" << exrdatestr);
}
} else if (str.startsWith(':')) {
str.remove(0,1);
QStringList dts = str.split(',');
Q_FOREACH (const QString &dtstr, dts) {
if (dtstr.endsWith('Z')) {
// UTC
KDateTime kdt = KDateTime::fromString(dtstr, RFC5545_KDATETIME_FORMAT);
Jan 15, 2015
Jan 15, 2015
330
331
332
333
334
335
336
337
338
339
340
if (!kdt.isValid()) {
// try parsing from alternate formats
kdt = KDateTime::fromString(dtstr, RFC3339_FORMAT);
}
if (!kdt.isValid()) {
SOCIALD_LOG_ERROR("unable to parse datetime from ex/rdate string:" << exrdatestr);
} else {
// parsed successfully
kdt.setTimeSpec(KDateTime::Spec::UTC());
retn.append(kdt);
}
Jan 14, 2015
Jan 14, 2015
341
342
343
} else {
// Floating time
KDateTime kdt = KDateTime::fromString(dtstr, RFC5545_KDATETIME_FORMAT_NTZC);
Jan 15, 2015
Jan 15, 2015
344
345
346
347
348
349
350
351
352
353
354
if (!kdt.isValid()) {
// try parsing from alternate formats
kdt = KDateTime::fromString(dtstr, RFC3339_FORMAT_NTZC);
}
if (!kdt.isValid()) {
SOCIALD_LOG_ERROR("unable to parse datetime from ex/rdate string:" << exrdatestr);
} else {
// parsed successfully
kdt.setTimeSpec(KDateTime::Spec::ClockTime());
retn.append(kdt);
}
Jan 14, 2015
Jan 14, 2015
355
356
357
358
359
360
361
362
363
}
}
} else {
SOCIALD_LOG_ERROR("not a valid ex/rdate string:" << exrdatestr);
}
return retn;
}
Feb 10, 2014
Feb 10, 2014
364
365
366
QJsonArray recurrenceArray(KCalCore::Event::Ptr event, KCalCore::ICalFormat &icalFormat)
{
QJsonArray retn;
Jan 14, 2015
Jan 14, 2015
367
368
// RRULE
Feb 10, 2014
Feb 10, 2014
369
370
371
372
373
374
KCalCore::Recurrence *kcalRecurrence = event->recurrence();
Q_FOREACH (KCalCore::RecurrenceRule *rrule, kcalRecurrence->rRules()) {
QString rruleStr = icalFormat.toString(rrule);
rruleStr.replace("\r\n", "");
retn.append(QJsonValue(rruleStr));
}
Jan 14, 2015
Jan 14, 2015
375
376
// EXRULE
Feb 10, 2014
Feb 10, 2014
377
378
379
380
381
382
Q_FOREACH (KCalCore::RecurrenceRule *exrule, kcalRecurrence->exRules()) {
QString exruleStr = icalFormat.toString(exrule);
exruleStr.replace("RRULE", "EXRULE");
exruleStr.replace("\r\n", "");
retn.append(QJsonValue(exruleStr));
}
Jan 14, 2015
Jan 14, 2015
383
384
385
// RDATE (date)
QString rdates;
Feb 10, 2014
Feb 10, 2014
386
Q_FOREACH (const QDate &rdate, kcalRecurrence->rDates()) {
Mar 4, 2016
Mar 4, 2016
387
rdates.append(QLocale::c().toString(rdate, RFC5545_QDATE_FORMAT));
Jan 14, 2015
Jan 14, 2015
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
rdates.append(',');
}
if (rdates.size()) {
rdates.chop(1); // trailing comma
retn.append(QJsonValue(QString::fromLatin1("RDATE;VALUE=DATE:%1").arg(rdates)));
}
// RDATE (date-time)
QString rdatetimes;
Q_FOREACH (const KDateTime &rdatetime, kcalRecurrence->rDateTimes()) {
if (rdatetime.timeSpec() == KDateTime::Spec::ClockTime()) {
rdatetimes.append(rdatetime.toString(RFC5545_KDATETIME_FORMAT_NTZC));
} else {
rdatetimes.append(rdatetime.toUtc().toString(RFC5545_KDATETIME_FORMAT));
}
rdatetimes.append(',');
}
if (rdatetimes.size()) {
rdatetimes.chop(1); // trailing comma
retn.append(QJsonValue(QString::fromLatin1("RDATE;VALUE=DATE-TIME:%1").arg(rdatetimes)));
Feb 10, 2014
Feb 10, 2014
408
}
Jan 14, 2015
Jan 14, 2015
409
410
411
// EXDATE (date)
QString exdates;
Feb 10, 2014
Feb 10, 2014
412
Q_FOREACH (const QDate &exdate, kcalRecurrence->exDates()) {
Mar 4, 2016
Mar 4, 2016
413
exdates.append(QLocale::c().toString(exdate, RFC5545_QDATE_FORMAT));
Jan 14, 2015
Jan 14, 2015
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
exdates.append(',');
}
if (exdates.size()) {
exdates.chop(1); // trailing comma
retn.append(QJsonValue(QString::fromLatin1("EXDATE;VALUE=DATE:%1").arg(exdates)));
}
// EXDATE (date-time)
QString exdatetimes;
Q_FOREACH (const KDateTime &exdatetime, kcalRecurrence->exDateTimes()) {
if (exdatetime.timeSpec() == KDateTime::Spec::ClockTime()) {
exdatetimes.append(exdatetime.toString(RFC5545_KDATETIME_FORMAT_NTZC));
} else {
exdatetimes.append(exdatetime.toUtc().toString(RFC5545_KDATETIME_FORMAT));
}
exdatetimes.append(',');
}
if (exdatetimes.size()) {
exdatetimes.chop(1); // trailing comma
retn.append(QJsonValue(QString::fromLatin1("EXDATE;VALUE=DATE-TIME:%1").arg(exdatetimes)));
Feb 10, 2014
Feb 10, 2014
434
}
Jan 14, 2015
Jan 14, 2015
435
Feb 10, 2014
Feb 10, 2014
436
437
438
return retn;
}
Mar 30, 2015
Mar 30, 2015
439
440
441
442
443
444
445
446
447
448
449
KDateTime parseRecurrenceId(const QJsonObject &originalStartTime)
{
QString recurrenceIdStr = originalStartTime.value(QLatin1String("dateTime")).toVariant().toString();
QString recurrenceIdTzStr = originalStartTime.value(QLatin1String("timeZone")).toVariant().toString();
KDateTime recurrenceId = KDateTime::fromString(recurrenceIdStr, RFC3339_FORMAT);
if (!recurrenceIdTzStr.isEmpty()) {
recurrenceId = recurrenceId.toTimeSpec(KTimeZone(recurrenceIdTzStr));
}
return recurrenceId;
}
Sep 15, 2015
Sep 15, 2015
450
QJsonObject kCalToJson(KCalCore::Event::Ptr event, KCalCore::ICalFormat &icalFormat, bool setUidProperty = false)
Feb 10, 2014
Feb 10, 2014
451
452
{
QString eventId = gCalEventId(event);
Dec 18, 2018
Dec 18, 2018
453
QJsonObject start, end;
Feb 10, 2014
Feb 10, 2014
454
Jun 20, 2018
Jun 20, 2018
455
456
457
QJsonArray attendees;
const KCalCore::Attendee::List attendeesList = event->attendees();
if (!attendeesList.isEmpty()) {
Sep 24, 2018
Sep 24, 2018
458
const QString &organizerEmail = event->organizer()->email();
Jun 20, 2018
Jun 20, 2018
459
Q_FOREACH (auto att, attendeesList) {
Sep 24, 2018
Sep 24, 2018
460
if (att->email().isEmpty() || att->email() == organizerEmail) {
Jun 20, 2018
Jun 20, 2018
461
462
463
464
465
466
467
468
469
470
471
472
473
474
continue;
}
QJsonObject attendee;
attendee.insert("email", att->email());
if (att->role() == KCalCore::Attendee::OptParticipant) {
attendee.insert("optional", true);
}
const QString &name = att->name();
if (!name.isEmpty()) {
attendee.insert("displayName", name);
}
attendees.append(attendee);
}
}
Feb 10, 2014
Feb 10, 2014
475
476
477
// insert the date/time and timeZone information into the Json object.
// note that timeZone is required for recurring events, for some reason.
if (event->dtStart().isDateOnly() || (event->allDay() && event->dtStart().time() == QTime(0,0,0))) {
Mar 4, 2016
Mar 4, 2016
478
start.insert(QLatin1String("date"), QLocale::c().toString(event->dtStart().date(), QDATEONLY_FORMAT));
Feb 10, 2014
Feb 10, 2014
479
480
481
482
483
} else {
start.insert(QLatin1String("dateTime"), event->dtStart().toString(RFC3339_FORMAT));
start.insert(QLatin1String("timeZone"), QJsonValue(event->dtStart().toString(KLONGTZ_FORMAT)));
}
if (event->dtEnd().isDateOnly() || (event->allDay() && event->dtEnd().time() == QTime(0,0,0))) {
Mar 20, 2014
Mar 20, 2014
484
// note: for iCal spec, allDay events need to have an end date of real-end-date+1 as end date is exclusive.
Mar 4, 2016
Mar 4, 2016
485
end.insert(QLatin1String("date"), QLocale::c().toString(event->dateEnd().addDays(1), QDATEONLY_FORMAT));
Feb 10, 2014
Feb 10, 2014
486
487
488
489
490
491
492
} else {
end.insert(QLatin1String("dateTime"), event->dtEnd().toString(RFC3339_FORMAT));
end.insert(QLatin1String("timeZone"), QJsonValue(event->dtEnd().toString(KLONGTZ_FORMAT)));
}
QJsonObject retn;
if (!eventId.isEmpty()) retn.insert(QLatin1String("id"), eventId);
Oct 28, 2015
Oct 28, 2015
493
494
495
496
497
498
if (event->recurrence()) {
QJsonArray recArray = recurrenceArray(event, icalFormat);
if (recArray.size()) {
retn.insert(QLatin1String("recurrence"), recArray);
}
}
Feb 10, 2014
Feb 10, 2014
499
500
501
502
503
504
retn.insert(QLatin1String("summary"), event->summary());
retn.insert(QLatin1String("description"), event->description());
retn.insert(QLatin1String("location"), event->location());
retn.insert(QLatin1String("start"), start);
retn.insert(QLatin1String("end"), end);
retn.insert(QLatin1String("sequence"), QString::number(event->revision()+1));
Jun 20, 2018
Jun 20, 2018
505
506
507
if (!attendees.isEmpty()) {
retn.insert(QLatin1String("attendees"), attendees);
}
Feb 10, 2014
Feb 10, 2014
508
509
510
//retn.insert(QLatin1String("locked"), event->readOnly()); // only allow locking server-side.
// we may wish to support locking/readonly from local side also, in the future.
Dec 18, 2018
Dec 18, 2018
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// if the event has no alarms associated with it, don't let Google add one automatically
// otherwise, attempt to upsync the alarms as popup reminders.
QJsonObject reminders;
if (event->alarms().count()) {
QJsonArray overrides;
KCalCore::Alarm::List alarms = event->alarms();
for (int i = 0; i < alarms.count(); ++i) {
// only upsync non-procedure alarms as popup reminders.
QSet<int> seenMinutes;
if (alarms.at(i)->type() != KCalCore::Alarm::Procedure) {
int minutes = googleStartOffsetForReminderStartOffset(
alarms.at(i)->startOffset().asSeconds());
if (!seenMinutes.contains(minutes)) {
QJsonObject override;
override.insert(QLatin1String("method"), QLatin1String("popup"));
override.insert(QLatin1String("minutes"), minutes);
overrides.append(override);
seenMinutes.insert(minutes);
}
}
}
reminders.insert(QLatin1String("overrides"), overrides);
Oct 28, 2015
Oct 28, 2015
533
}
Dec 18, 2018
Dec 18, 2018
534
535
reminders.insert(QLatin1String("useDefault"), false);
retn.insert(QLatin1String("reminders"), reminders);
Oct 28, 2015
Oct 28, 2015
536
Sep 15, 2015
Sep 15, 2015
537
538
539
540
541
542
543
544
545
546
547
548
if (setUidProperty) {
// now we store private extended properties: local uid.
// this allows us to detect partially-upsynced artifacts during subsequent syncs.
// usually this codepath will be hit for localAdditions being upsynced,
// but sometimes also if we need to update the mapping due to clean-sync.
QJsonObject privateExtendedProperties;
privateExtendedProperties.insert(QLatin1String("x-jolla-sociald-mkcal-uid"), event->uid());
QJsonObject extendedProperties;
extendedProperties.insert(QLatin1String("private"), privateExtendedProperties);
retn.insert(QLatin1String("extendedProperties"), extendedProperties);
}
Feb 10, 2014
Feb 10, 2014
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
return retn;
}
void extractStartAndEnd(const QJsonObject &eventData,
bool *startExists,
bool *endExists,
bool *startIsDateOnly,
bool *endIsDateOnly,
bool *isAllDay,
KDateTime *start,
KDateTime *end)
{
*startIsDateOnly = false, *endIsDateOnly = false;
QString startTimeString, endTimeString;
QJsonObject startTimeData = eventData.value(QLatin1String("start")).toObject();
QJsonObject endTimeData = eventData.value(QLatin1String("end")).toObject();
if (!startTimeData.value(QLatin1String("date")).toVariant().toString().isEmpty()) {
*startExists = true;
*startIsDateOnly = true; // all-day event.
startTimeString = startTimeData.value(QLatin1String("date")).toVariant().toString();
} else if (!startTimeData.value(QLatin1String("dateTime")).toVariant().toString().isEmpty()) {
*startExists = true;
startTimeString = startTimeData.value(QLatin1String("dateTime")).toVariant().toString();
} else {
*startExists = false;
}
if (!endTimeData.value(QLatin1String("date")).toVariant().toString().isEmpty()) {
*endExists = true;
*endIsDateOnly = true; // all-day event.
endTimeString = endTimeData.value(QLatin1String("date")).toVariant().toString();
} else if (!endTimeData.value(QLatin1String("dateTime")).toVariant().toString().isEmpty()) {
*endExists = true;
endTimeString = endTimeData.value(QLatin1String("dateTime")).toVariant().toString();
} else {
*endExists = false;
}
if (*startExists) {
if (!*startIsDateOnly) {
KDateTime parsedStartTime = KDateTime::fromString(startTimeString, RFC3339_FORMAT);
KDateTime ntzcStartTime = KDateTime::fromString(startTimeString, RFC3339_FORMAT_NTZC);
if (ntzcStartTime.time() > parsedStartTime.time()) parsedStartTime = ntzcStartTime;
// different format? let KDateTime detect the format automatically.
if (parsedStartTime.isNull()) {
parsedStartTime = KDateTime::fromString(startTimeString);
}
*start = parsedStartTime.toLocalZone();
} else {
Mar 4, 2016
Mar 4, 2016
599
*start = KDateTime(QLocale::c().toDate(startTimeString, QDATEONLY_FORMAT), QTime(), KDateTime::ClockTime);
Feb 26, 2016
Feb 26, 2016
600
start->setDateOnly(true);
Feb 10, 2014
Feb 10, 2014
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
}
}
if (*endExists) {
if (!*endIsDateOnly) {
KDateTime parsedEndTime = KDateTime::fromString(endTimeString, RFC3339_FORMAT);
KDateTime ntzcEndTime = KDateTime::fromString(endTimeString, RFC3339_FORMAT_NTZC);
if (ntzcEndTime.time() > parsedEndTime.time()) parsedEndTime = ntzcEndTime;
// different format? let KDateTime detect the format automatically.
if (parsedEndTime.isNull()) {
parsedEndTime = KDateTime::fromString(endTimeString);
}
*end = parsedEndTime.toLocalZone();
} else {
Mar 19, 2014
Mar 19, 2014
617
// Special handling for all-day events is required.
Feb 10, 2014
Feb 10, 2014
618
if (*startExists && *startIsDateOnly) {
Mar 4, 2016
Mar 4, 2016
619
620
if (QLocale::c().toDate(startTimeString, QDATEONLY_FORMAT)
== QLocale::c().toDate(endTimeString, QDATEONLY_FORMAT)) {
Feb 10, 2014
Feb 10, 2014
621
622
623
// single-day all-day event
*endExists = false;
*isAllDay = true;
Mar 4, 2016
Mar 4, 2016
624
625
} else if (QLocale::c().toDate(startTimeString, QDATEONLY_FORMAT)
== QLocale::c().toDate(endTimeString, QDATEONLY_FORMAT).addDays(-1)) {
Mar 19, 2014
Mar 19, 2014
626
627
628
629
630
// Google will send a single-day all-day event has having an end-date
// of startDate+1 to conform to iCal spec. Hence, this is actually
// a single-day all-day event, despite the difference in end-date.
*endExists = false;
*isAllDay = true;
Feb 10, 2014
Feb 10, 2014
631
} else {
Mar 19, 2014
Mar 19, 2014
632
633
634
// multi-day all-day event.
// as noted above, Google will send all-day events as having an end-date
// of real-end-date+1 in order to conform to iCal spec (exclusive end dt).
Mar 4, 2016
Mar 4, 2016
635
*end = KDateTime(QLocale::c().toDate(endTimeString, QDATEONLY_FORMAT), QTime(), KDateTime::ClockTime);
Feb 26, 2016
Feb 26, 2016
636
end->setDateOnly(true);
Feb 10, 2014
Feb 10, 2014
637
638
639
*isAllDay = true;
}
} else {
Mar 4, 2016
Mar 4, 2016
640
*end = KDateTime(QLocale::c().toDate(endTimeString, QDATEONLY_FORMAT), QTime(), KDateTime::ClockTime);
Feb 26, 2016
Feb 26, 2016
641
end->setDateOnly(true);
Feb 10, 2014
Feb 10, 2014
642
643
644
645
646
647
648
649
650
*isAllDay = false;
}
}
}
}
void extractRecurrence(const QJsonArray &recurrence, KCalCore::Event::Ptr event, KCalCore::ICalFormat &icalFormat)
{
KCalCore::Recurrence *kcalRecurrence = event->recurrence();
Jan 14, 2015
Jan 14, 2015
651
kcalRecurrence->clear(); // avoid adding duplicate recurrence information
Feb 10, 2014
Feb 10, 2014
652
653
for (int i = 0; i < recurrence.size(); ++i) {
QString ruleStr = recurrence.at(i).toString();
Jan 14, 2015
Jan 14, 2015
654
if (ruleStr.startsWith(QString::fromLatin1("rrule"), Qt::CaseInsensitive)) {
Feb 10, 2014
Feb 10, 2014
655
656
KCalCore::RecurrenceRule *rrule = new KCalCore::RecurrenceRule;
if (!icalFormat.fromString(rrule, ruleStr.mid(6))) {
Jan 14, 2015
Jan 14, 2015
657
658
SOCIALD_LOG_DEBUG("unable to parse RRULE information:" << ruleStr);
traceDumpStr(QString::fromUtf8(QJsonDocument(recurrence).toJson()));
Feb 10, 2014
Feb 10, 2014
659
660
661
} else {
kcalRecurrence->addRRule(rrule);
}
Jan 14, 2015
Jan 14, 2015
662
} else if (ruleStr.startsWith(QString::fromLatin1("exrule"), Qt::CaseInsensitive)) {
Feb 10, 2014
Feb 10, 2014
663
664
KCalCore::RecurrenceRule *exrule = new KCalCore::RecurrenceRule;
if (!icalFormat.fromString(exrule, ruleStr.mid(7))) {
Jan 14, 2015
Jan 14, 2015
665
666
SOCIALD_LOG_DEBUG("unable to parse EXRULE information:" << ruleStr);
traceDumpStr(QString::fromUtf8(QJsonDocument(recurrence).toJson()));
Feb 10, 2014
Feb 10, 2014
667
668
669
} else {
kcalRecurrence->addExRule(exrule);
}
Jan 14, 2015
Jan 14, 2015
670
671
672
673
674
675
} else if (ruleStr.startsWith(QString::fromLatin1("rdate"), Qt::CaseInsensitive)) {
bool isDateOnly = false;
QList<KDateTime> rdatetimes = datetimesFromExRDateStr(ruleStr, &isDateOnly);
if (!rdatetimes.size()) {
SOCIALD_LOG_DEBUG("unable to parse RDATE information:" << ruleStr);
traceDumpStr(QString::fromUtf8(QJsonDocument(recurrence).toJson()));
Feb 10, 2014
Feb 10, 2014
676
} else {
Jan 14, 2015
Jan 14, 2015
677
678
679
680
681
682
683
Q_FOREACH (const KDateTime &kdt, rdatetimes) {
if (isDateOnly) {
kcalRecurrence->addRDate(kdt.date());
} else {
kcalRecurrence->addRDateTime(kdt);
}
}
Feb 10, 2014
Feb 10, 2014
684
}
Jan 14, 2015
Jan 14, 2015
685
686
687
688
689
690
} else if (ruleStr.startsWith(QString::fromLatin1("exdate"), Qt::CaseInsensitive)) {
bool isDateOnly = false;
QList<KDateTime> exdatetimes = datetimesFromExRDateStr(ruleStr, &isDateOnly);
if (!exdatetimes.size()) {
SOCIALD_LOG_DEBUG("unable to parse EXDATE information:" << ruleStr);
traceDumpStr(QString::fromUtf8(QJsonDocument(recurrence).toJson()));
Feb 10, 2014
Feb 10, 2014
691
} else {
Jan 14, 2015
Jan 14, 2015
692
693
694
695
696
697
698
Q_FOREACH (const KDateTime &kdt, exdatetimes) {
if (isDateOnly) {
kcalRecurrence->addExDate(kdt.date());
} else {
kcalRecurrence->addExDateTime(kdt);
}
}
Feb 10, 2014
Feb 10, 2014
699
700
}
} else {
Jan 14, 2015
Jan 14, 2015
701
702
SOCIALD_LOG_DEBUG("unknown recurrence information:" << ruleStr);
traceDumpStr(QString::fromUtf8(QJsonDocument(recurrence).toJson()));
Feb 10, 2014
Feb 10, 2014
703
704
705
706
}
}
}
Jun 30, 2017
Jun 30, 2017
707
708
void extractOrganizer(const QJsonObject &creatorObj, const QJsonObject &organizerObj, KCalCore::Event::Ptr event)
{
Nov 22, 2018
Nov 22, 2018
709
710
711
712
713
714
715
if (!organizerObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
|| !organizerObj.value(QLatin1String("email")).toVariant().toString().isEmpty()) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
organizerObj.value(QLatin1String("displayName")).toVariant().toString(),
organizerObj.value(QLatin1String("email")).toVariant().toString()));
event->setOrganizer(organizer);
} else if (!creatorObj.value(QLatin1String("displayName")).toVariant().toString().isEmpty()
Jun 30, 2017
Jun 30, 2017
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|| !creatorObj.value(QLatin1String("email")).toVariant().toString().isEmpty()) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
creatorObj.value(QLatin1String("displayName")).toVariant().toString(),
creatorObj.value(QLatin1String("email")).toVariant().toString()));
event->setOrganizer(organizer);
} else {
KCalCore::Person::Ptr organizer(new KCalCore::Person);
event->setOrganizer(organizer);
}
}
void extractAttendees(const QJsonArray &attendees, KCalCore::Event::Ptr event)
{
event->clearAttendees();
for (int i = 0; i < attendees.size(); ++i) {
QJsonObject attendeeObj = attendees.at(i).toObject();
if (!attendeeObj.value(QLatin1String("organizer")).toVariant().toBool()) {
KCalCore::Attendee::Ptr attendee(new KCalCore::Attendee(
attendeeObj.value(QLatin1String("displayName")).toVariant().toString(),
attendeeObj.value(QLatin1String("email")).toVariant().toString()));
if (attendeeObj.find(QLatin1String("optional")) != attendeeObj.end()) {
if (attendeeObj.value(QLatin1String("optional")).toVariant().toBool()) {
attendee->setRole(KCalCore::Attendee::OptParticipant);
} else {
attendee->setRole(KCalCore::Attendee::ReqParticipant);
}
}
Nov 22, 2018
Nov 22, 2018
743
744
745
746
747
748
749
750
751
752
753
754
755
if (attendeeObj.find(QLatin1String("responseStatus")) != attendeeObj.end()) {
const QString &responseValue = attendeeObj.value(QLatin1String("responseStatus")).toVariant().toString();
if (responseValue == "needsAction") {
attendee->setStatus(KCalCore::Attendee::NeedsAction);
} else if (responseValue == "accepted") {
attendee->setStatus(KCalCore::Attendee::Accepted);
} else if (responseValue == "declined") {
attendee->setStatus(KCalCore::Attendee::Declined);
} else {
attendee->setStatus(KCalCore::Attendee::Tentative);
}
}
attendee->setRSVP(true);
Jun 30, 2017
Jun 30, 2017
756
event->addAttendee(attendee);
Nov 22, 2018
Nov 22, 2018
757
758
759
760
761
762
763
764
765
766
767
768
} 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()) {
KCalCore::Person::Ptr organizer(new KCalCore::Person(
attendeeObj.value(QLatin1String("displayName")).toVariant().toString(),
attendeeObj.value(QLatin1String("email")).toVariant().toString()));
event->setOrganizer(organizer);
}
Jun 30, 2017
Jun 30, 2017
769
770
771
772
}
}
}
Jun 26, 2015
Jun 26, 2015
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#define START_EVENT_UPDATES_IF_REQUIRED(event, changed) \
if (*changed == false) { \
event->startUpdates(); \
} \
*changed = true;
#define UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, getter, setter, newValue, changed) \
if (event->getter() != newValue) { \
START_EVENT_UPDATES_IF_REQUIRED(event, changed) \
event->setter(newValue); \
}
#define END_EVENT_UPDATES_IF_REQUIRED(event, changed, startedUpdates) \
if (*changed == false) { \
SOCIALD_LOG_DEBUG("Ignoring spurious change reported for:" << \
event->uid() << event->revision() << event->summary()); \
} else if (startedUpdates) { \
event->endUpdates(); \
}
void extractAlarms(const QJsonObject &json, KCalCore::Event::Ptr event, int defaultReminderStartOffset, bool *changed)
Jun 5, 2015
Jun 5, 2015
794
{
Dec 18, 2018
Dec 18, 2018
795
QSet<int> startOffsets;
Jun 5, 2015
Jun 5, 2015
796
797
798
799
if (json.contains(QStringLiteral("reminders"))) {
QJsonObject reminders = json.value(QStringLiteral("reminders")).toObject();
if (reminders.value(QStringLiteral("useDefault")).toBool()) {
if (defaultReminderStartOffset > 0) {
Dec 18, 2018
Dec 18, 2018
800
startOffsets.insert(defaultReminderStartOffset);
Jun 5, 2015
Jun 5, 2015
801
802
803
804
805
806
807
808
} else {
SOCIALD_LOG_DEBUG("not adding default reminder even though requested: not popup or invalid start offset.");
}
} else {
QJsonArray overrides = reminders.value(QStringLiteral("overrides")).toArray();
for (int i = 0; i < overrides.size(); ++i) {
QJsonObject override = overrides.at(i).toObject();
if (override.value(QStringLiteral("method")).toString() == QStringLiteral("popup")) {
Dec 18, 2018
Dec 18, 2018
809
startOffsets.insert(override.value(QStringLiteral("minutes")).toInt());
Jun 5, 2015
Jun 5, 2015
810
811
812
}
}
}
Dec 18, 2018
Dec 18, 2018
813
814
815
816
// search for all reminders to see if they are represented by an alarm.
bool needRemoveAndRecreate = false;
for (QSet<int>::const_iterator it = startOffsets.constBegin(); it != startOffsets.constEnd(); it++) {
int startOffset = nearestNemoReminderStartOffset(*it);
Jun 5, 2015
Jun 5, 2015
817
818
SOCIALD_LOG_DEBUG("event needs reminder with start offset (seconds):" << startOffset);
KCalCore::Alarm::List alarms = event->alarms();
Dec 18, 2018
Dec 18, 2018
819
int alarmsCount = 0;
Jun 5, 2015
Jun 5, 2015
820
821
822
for (int i = 0; i < alarms.count(); ++i) {
// we don't count Procedure type alarms.
if (alarms.at(i)->type() != KCalCore::Alarm::Procedure) {
Dec 18, 2018
Dec 18, 2018
823
alarmsCount += 1;
Jun 5, 2015
Jun 5, 2015
824
if (alarms.at(i)->startOffset().asSeconds() == startOffset) {
Dec 18, 2018
Dec 18, 2018
825
SOCIALD_LOG_DEBUG("event already has reminder with start offset (seconds):" << startOffset);
Jun 5, 2015
Jun 5, 2015
826
} else {
Dec 18, 2018
Dec 18, 2018
827
828
SOCIALD_LOG_DEBUG("event is missing reminder with start offset (seconds):" << startOffset);
needRemoveAndRecreate = true;
Jun 5, 2015
Jun 5, 2015
829
830
831
}
}
}
Dec 18, 2018
Dec 18, 2018
832
833
834
835
836
837
838
839
840
841
842
if (alarmsCount != startOffsets.count()) {
SOCIALD_LOG_DEBUG("event has too many reminders, recreating alarms.");
needRemoveAndRecreate = true;
}
}
if (needRemoveAndRecreate) {
START_EVENT_UPDATES_IF_REQUIRED(event, changed);
KCalCore::Alarm::List alarms = event->alarms();
for (int i = 0; i < alarms.count(); ++i) {
if (alarms.at(i)->type() != KCalCore::Alarm::Procedure) {
event->removeAlarm(alarms.at(i));
Jun 5, 2015
Jun 5, 2015
843
}
Dec 18, 2018
Dec 18, 2018
844
845
846
847
}
for (QSet<int>::const_iterator it = startOffsets.constBegin(); it != startOffsets.constEnd(); it++) {
int startOffset = nearestNemoReminderStartOffset(*it);
SOCIALD_LOG_DEBUG("setting event reminder with start offset (seconds):" << startOffset);
Jun 5, 2015
Jun 5, 2015
848
849
850
851
852
853
KCalCore::Alarm::Ptr alarm = event->newAlarm();
alarm->setEnabled(true);
alarm->setStartOffset(KCalCore::Duration(startOffset));
}
}
}
Dec 18, 2018
Dec 18, 2018
854
if (startOffsets.isEmpty()) {
Jun 5, 2015
Jun 5, 2015
855
856
857
858
859
860
// no reminders were defined in the json received from Google.
// remove any alarms as required from the local event.
KCalCore::Alarm::List alarms = event->alarms();
for (int i = 0; i < alarms.count(); ++i) {
if (alarms.at(i)->type() != KCalCore::Alarm::Procedure) {
SOCIALD_LOG_DEBUG("removing event reminder with start offset (seconds):" << alarms.at(i)->startOffset().asSeconds());
Jun 26, 2015
Jun 26, 2015
861
START_EVENT_UPDATES_IF_REQUIRED(event, changed);
Jun 5, 2015
Jun 5, 2015
862
863
864
865
866
867
event->removeAlarm(alarms.at(i));
}
}
}
}
Jun 26, 2015
Jun 26, 2015
868
void jsonToKCal(const QJsonObject &json, KCalCore::Event::Ptr event, int defaultReminderStartOffset, KCalCore::ICalFormat &icalFormat, bool *changed)
Feb 10, 2014
Feb 10, 2014
869
{
Jun 26, 2015
Jun 26, 2015
870
871
872
873
874
875
876
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()) {
SOCIALD_LOG_DEBUG("Ignoring non-remote-changed:" << event->uid() << ","
<< gCalETag(event) << "==" << json.value(QLatin1String("etag")).toVariant().toString());
return; // this event has not changed server-side since we last saw it.
}
Feb 10, 2014
Feb 10, 2014
877
878
879
880
881
KDateTime start, end;
bool startExists = false, endExists = false;
bool startIsDateOnly = false, endIsDateOnly = false;
bool isAllDay = false;
extractStartAndEnd(json, &startExists, &endExists, &startIsDateOnly, &endIsDateOnly, &isAllDay, &start, &end);
Jun 26, 2015
Jun 26, 2015
882
883
884
885
886
887
888
889
if (gCalEventId(event) != json.value(QLatin1String("id")).toVariant().toString()) {
START_EVENT_UPDATES_IF_REQUIRED(event, changed);
setGCalEventId(event, json.value(QLatin1String("id")).toVariant().toString());
}
if (gCalETag(event) != json.value(QLatin1String("etag")).toVariant().toString()) {
START_EVENT_UPDATES_IF_REQUIRED(event, changed);
setGCalETag(event, json.value(QLatin1String("etag")).toVariant().toString());
}
Jun 28, 2017
Jun 28, 2017
890
setRemoteUidCustomField(event, json.value(QLatin1String("iCalUID")).toVariant().toString(), json.value(QLatin1String("id")).toVariant().toString());
Feb 10, 2014
Feb 10, 2014
891
extractRecurrence(json.value(QLatin1String("recurrence")).toArray(), event, icalFormat);
Jun 30, 2017
Jun 30, 2017
892
893
extractOrganizer(json.value(QLatin1String("creator")).toObject(), json.value(QLatin1String("organizer")).toObject(), event);
extractAttendees(json.value(QLatin1String("attendees")).toArray(), event);
Jun 26, 2015
Jun 26, 2015
894
895
896
897
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)
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, location, setLocation, json.value(QLatin1String("location")).toVariant().toString(), changed)
Feb 1, 2016
Feb 1, 2016
898
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, revision, setRevision, json.value(QLatin1String("sequence")).toVariant().toInt(), changed)
Feb 10, 2014
Feb 10, 2014
899
if (startExists) {
Jun 26, 2015
Jun 26, 2015
900
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, dtStart, setDtStart, start, changed)
Feb 10, 2014
Feb 10, 2014
901
902
}
if (endExists) {
Jun 26, 2015
Jun 26, 2015
903
904
905
906
907
if (!event->hasEndDate() || event->dtEnd() != end) {
START_EVENT_UPDATES_IF_REQUIRED(event, changed);
event->setHasEndDate(true);
event->setDtEnd(end);
}
Feb 10, 2014
Feb 10, 2014
908
} else {
Jun 26, 2015
Jun 26, 2015
909
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, hasEndDate, setHasEndDate, false, changed)
Feb 10, 2014
Feb 10, 2014
910
911
}
if (isAllDay) {
Jun 26, 2015
Jun 26, 2015
912
UPDATE_EVENT_PROPERTY_IF_REQUIRED(event, allDay, setAllDay, false, changed)
Feb 10, 2014
Feb 10, 2014
913
}
Jun 26, 2015
Jun 26, 2015
914
915
extractAlarms(json, event, defaultReminderStartOffset, changed);
END_EVENT_UPDATES_IF_REQUIRED(event, changed, !alreadyStarted);
Feb 10, 2014
Feb 10, 2014
916
917
}
Sep 15, 2015
Sep 15, 2015
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
bool remoteModificationIsReal(const QJsonObject &json, KCalCore::Event::Ptr event)
{
if (gCalEventId(event) != json.value(QLatin1String("id")).toVariant().toString()) {
return true; // this event is either a partial-upsync-artifact or a new remote addition.
}
if (gCalETag(event) != json.value(QLatin1String("etag")).toVariant().toString()) {
return true; // this event has changed server-side since we last saw it.
}
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 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);
Oct 28, 2015
Oct 28, 2015
936
if (GoogleCalendarIncidenceComparator::incidencesEqual(localEvent, remoteEvent, true)) {
Sep 15, 2015
Sep 15, 2015
937
938
939
940
941
return false; // they're equal, so the local modification is not real.
}
return true;
}
Feb 10, 2014
Feb 10, 2014
942
943
944
// returns true if the last sync was marked as successful, and then marks the current
// sync as being unsuccessful. The sync adapter should set it to true manually
// once sync succeeds.
Sep 15, 2015
Sep 15, 2015
945
bool wasLastSyncSuccessful(int accountId, bool *needCleanSync)
Feb 10, 2014
Feb 10, 2014
946
947
948
949
{
QString settingsFileName = QString::fromLatin1("%1/%2/gcal.ini")
.arg(QString::fromLatin1(PRIVILEGED_DATA_DIR))
.arg(QString::fromLatin1(SYNC_DATABASE_DIR));
Sep 15, 2015
Sep 15, 2015
950
951
952
953
954
955
if (!QFile::exists(settingsFileName)) {
SOCIALD_LOG_DEBUG("gcal.ini settings file does not exist, triggering clean sync");
*needCleanSync = true;
return false;
}
Feb 10, 2014
Feb 10, 2014
956
QSettings settingsFile(settingsFileName, QSettings::IniFormat);
Sep 15, 2015
Sep 15, 2015
957
958
// needCleanSync will be true if and only if an unrecoverable error occurred during the previous sync.
*needCleanSync = settingsFile.value(QString::fromLatin1("%1-needCleanSync").arg(accountId), QVariant::fromValue<bool>(false)).toBool();
Feb 10, 2014
Feb 10, 2014
959
960
bool retn = settingsFile.value(QString::fromLatin1("%1-success").arg(accountId), QVariant::fromValue<bool>(false)).toBool();
settingsFile.setValue(QString::fromLatin1("%1-success").arg(accountId), QVariant::fromValue<bool>(false));
Jan 13, 2015
Jan 13, 2015
961
962
963
964
965
966
int pluginVersion = settingsFile.value(QString::fromLatin1("%1-pluginVersion").arg(accountId), QVariant::fromValue<int>(1)).toInt();
if (pluginVersion != GOOGLE_CAL_SYNC_PLUGIN_VERSION) {
settingsFile.setValue(QString::fromLatin1("%1-pluginVersion").arg(accountId), GOOGLE_CAL_SYNC_PLUGIN_VERSION);
SOCIALD_LOG_DEBUG("Google cal sync plugin version mismatch, force clean sync");
retn = false;
}
Feb 10, 2014
Feb 10, 2014
967
968
969
970
971
972
973
974
975
976
977
settingsFile.sync();
return retn;
}
void setLastSyncSuccessful(QList<int> accountIds)
{
QString settingsFileName = QString::fromLatin1("%1/%2/gcal.ini")
.arg(QString::fromLatin1(PRIVILEGED_DATA_DIR))
.arg(QString::fromLatin1(SYNC_DATABASE_DIR));
QSettings settingsFile(settingsFileName, QSettings::IniFormat);
Q_FOREACH(int accountId, accountIds) {
Sep 15, 2015
Sep 15, 2015
978
settingsFile.setValue(QString::fromLatin1("%1-needCleanSync").arg(accountId), QVariant::fromValue<bool>(false));
Feb 10, 2014
Feb 10, 2014
979
980
981
982
983
settingsFile.setValue(QString::fromLatin1("%1-success").arg(accountId), QVariant::fromValue<bool>(true));
}
settingsFile.sync();
}
Feb 1, 2016
Feb 1, 2016
984
985
986
987
988
989
990
991
992
993
994
995
996
void setLastSyncRequiresCleanSync(QList<int> accountIds)
{
QString settingsFileName = QString::fromLatin1("%1/%2/gcal.ini")
.arg(QString::fromLatin1(PRIVILEGED_DATA_DIR))
.arg(QString::fromLatin1(SYNC_DATABASE_DIR));
QSettings settingsFile(settingsFileName, QSettings::IniFormat);
Q_FOREACH(int accountId, accountIds) {
settingsFile.setValue(QString::fromLatin1("%1-needCleanSync").arg(accountId), QVariant::fromValue<bool>(true));
settingsFile.setValue(QString::fromLatin1("%1-success").arg(accountId), QVariant::fromValue<bool>(false));
}
settingsFile.sync();
}
Feb 10, 2014
Feb 10, 2014
997
998
}
Apr 7, 2014
Apr 7, 2014
999
1000
GoogleCalendarSyncAdaptor::GoogleCalendarSyncAdaptor(QObject *parent)
: GoogleDataTypeSyncAdaptor(SocialNetworkSyncAdaptor::Calendars, parent)