/
incidencehandler.cpp
654 lines (583 loc) · 29.4 KB
/
incidencehandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* This file is part of buteo-sync-plugin-caldav package
*
* Copyright (C) 2014 Jolla Ltd. and/or its subsidiary(-ies).
*
* Contributors: Bea Lam <bea.lam@jollamobile.com>
*
* This 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 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include "incidencehandler.h"
#include <QDebug>
#include <QBuffer>
#include <QDataStream>
#include <LogMacros.h>
#include <memorycalendar.h>
#include <icalformat.h>
#define PROP_DTEND_ADDED_USING_DTSTART "dtend-added-as-dtstart"
#define COPY_IF_NOT_EQUAL(dest, src, get, set) \
{ \
if (dest->get != src->get) { \
dest->set(src->get); \
} \
}
#define RETURN_FALSE_IF_NOT_EQUAL(a, b, func, desc) {\
if (a->func != b->func) {\
LOG_DEBUG("Incidence" << desc << "" << "properties are not equal:" << a->func << b->func); \
return false;\
}\
}
#define RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, func, desc) {\
if (a->func.trimmed() != b->func.trimmed()) {\
LOG_DEBUG("Incidence" << desc << "" << "properties are not equal:" << a->func.trimmed() << b->func.trimmed()); \
return false;\
}\
}
#define RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(failureCheck, desc, debug) {\
if (failureCheck) {\
LOG_DEBUG("Incidence" << desc << "properties are not equal:" << desc << debug); \
return false;\
}\
}
IncidenceHandler::IncidenceHandler()
{
}
IncidenceHandler::~IncidenceHandler()
{
}
void IncidenceHandler::normalizePersonEmail(KCalCore::Person *p)
{
QString email = p->email().replace(QStringLiteral("mailto:"), QString(), Qt::CaseInsensitive);
if (email != p->email()) {
p->setEmail(email);
}
}
template <typename T>
bool IncidenceHandler::pointerDataEqual(const QVector<QSharedPointer<T> > &vectorA, const QVector<QSharedPointer<T> > &vectorB)
{
if (vectorA.count() != vectorB.count()) {
return false;
}
for (int i=0; i<vectorA.count(); i++) {
if (*vectorA[i].data() != *vectorB[i].data()) {
return false;
}
}
return true;
}
static bool ruleDataEqual(const KCalCore::RecurrenceRule *a, const KCalCore::RecurrenceRule *b)
{
if (*a == *b) {
return true;
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, recurrenceType(), "recurrenceType");
RETURN_FALSE_IF_NOT_EQUAL(a, b, duration(), "duration");
RETURN_FALSE_IF_NOT_EQUAL(a, b, startDt().dateTime(), "startDt");
RETURN_FALSE_IF_NOT_EQUAL(a, b, endDt().dateTime(), "endDt");
RETURN_FALSE_IF_NOT_EQUAL(a, b, frequency(), "frequency");
RETURN_FALSE_IF_NOT_EQUAL(a, b, allDay(), "allDay");
RETURN_FALSE_IF_NOT_EQUAL(a, b, isReadOnly(), "isReadOnly");
RETURN_FALSE_IF_NOT_EQUAL(a, b, bySeconds(), "bySeconds");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byMinutes(), "byMinutes");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byHours(), "byHours");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byDays().count(), "byDays.count");
for (int i = 0; i < a->byDays().count(); i++) {
RETURN_FALSE_IF_NOT_EQUAL(a, b, byDays()[i].day(), "byDays[i].day");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byDays()[i].pos(), "byDays[i].pos");
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, byMonthDays(), "byMonthDays");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byYearDays(), "byYearDays");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byWeekNumbers(), "byWeekNumbers");
RETURN_FALSE_IF_NOT_EQUAL(a, b, byMonths(), "byMonths");
RETURN_FALSE_IF_NOT_EQUAL(a, b, bySetPos(), "bySetPos");
RETURN_FALSE_IF_NOT_EQUAL(a, b, weekStart(), "weekStart");
LOG_DEBUG("unknown difference in recurrence rules:" << a->rrule() << b->rrule());
return false;
}
static bool recurrenceDataEqual(const KCalCore::Recurrence *a, const KCalCore::Recurrence *b)
{
if (*a == *b) {
return true;
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, startDateTime().dateTime(), "startDateTime");
RETURN_FALSE_IF_NOT_EQUAL(a, b, allDay(), "allDay");
RETURN_FALSE_IF_NOT_EQUAL(a, b, recurReadOnly(), "recurReadOnly");
RETURN_FALSE_IF_NOT_EQUAL(a, b, exDates(), "exDates");
RETURN_FALSE_IF_NOT_EQUAL(a, b, rDates(), "rDates");
RETURN_FALSE_IF_NOT_EQUAL(a, b, exDateTimes().count(), "exDateTimes.count");
for (int i = 0; i < a->exDateTimes().count(); i++) {
RETURN_FALSE_IF_NOT_EQUAL(a, b, exDateTimes()[i].dateTime(), "exDateTimes[i]");
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, rDateTimes().count(), "rDateTimes.count");
for (int i = 0; i < a->rDateTimes().count(); i++) {
RETURN_FALSE_IF_NOT_EQUAL(a, b, rDateTimes()[i].dateTime(), "rDateTimes[i]");
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, exRules().count(), "exRules.count");
for (int i = 0; i < a->exRules().count(); i++) {
if (!ruleDataEqual(a->exRules()[i], b->exRules()[i])) {
return false;
}
}
RETURN_FALSE_IF_NOT_EQUAL(a, b, rRules().count(), "rRules.count");
for (int i = 0; i < a->rRules().count(); i++) {
if (!ruleDataEqual(a->rRules()[i], b->rRules()[i])) {
return false;
}
}
LOG_DEBUG("unknown difference in recurrences.");
return false;
}
bool IncidenceHandler::matchIcsData(KCalCore::Incidence::Ptr incidence,
KCalCore::Incidence::List instances,
const Reader::CalendarResource &resource)
{
const KCalCore::Incidence::Ptr &pLMod = IncidenceHandler::incidenceToExport(incidence, instances);
// Find the copy received from the server and detect changes.
Q_FOREACH (const KCalCore::Incidence::Ptr &remoteIncidence, resource.incidences) {
if (remoteIncidence->recurrenceId() == incidence->recurrenceId()) {
const KCalCore::Incidence::Ptr &exportRInc = IncidenceHandler::incidenceToExport(remoteIncidence);
// found the remote incidence. compare it to the local.
return IncidenceHandler::copiedPropertiesAreEqual(pLMod, exportRInc);
}
}
// this is always an internal logic error. We explicitly requested it.
LOG_WARNING("error: couldn't find remote incidence matching! FIXME!");
return false;
}
// Checks whether a specific set of properties are equal.
bool IncidenceHandler::copiedPropertiesAreEqual(const KCalCore::Incidence::Ptr &a, const KCalCore::Incidence::Ptr &b)
{
if (!a || !b) {
qWarning() << "Invalid paramters! a:" << a << "b:" << b;
return false;
}
// Do not compare created() or lastModified() because we don't update these fields when
// an incidence is updated by copyIncidenceProperties(), so they are guaranteed to be unequal.
// Don't compare resources() for now because KCalCore may insert QStringList("") as the resources
// when in fact it should be QStringList(), which causes the comparison to fail.
RETURN_FALSE_IF_NOT_EQUAL(a, b, type(), "type");
RETURN_FALSE_IF_NOT_EQUAL(a, b, duration(), "duration");
RETURN_FALSE_IF_NOT_EQUAL(a, b, hasDuration(), "hasDuration");
RETURN_FALSE_IF_NOT_EQUAL(a, b, isReadOnly(), "isReadOnly");
RETURN_FALSE_IF_NOT_EQUAL(a, b, comments(), "comments");
RETURN_FALSE_IF_NOT_EQUAL(a, b, contacts(), "contacts");
RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, altDescription(), "altDescription");
RETURN_FALSE_IF_NOT_EQUAL(a, b, categories(), "categories");
RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, customStatus(), "customStatus");
RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, description(), "description");
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(!qFuzzyCompare(a->geoLatitude(), b->geoLatitude()), "geoLatitude", (QString("%1 != %2").arg(a->geoLatitude()).arg(b->geoLatitude())));
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(!qFuzzyCompare(a->geoLongitude(), b->geoLongitude()), "geoLongitude", (QString("%1 != %2").arg(a->geoLongitude()).arg(b->geoLongitude())));
RETURN_FALSE_IF_NOT_EQUAL(a, b, hasGeo(), "hasGeo");
RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, location(), "location");
RETURN_FALSE_IF_NOT_EQUAL(a, b, secrecy(), "secrecy");
RETURN_FALSE_IF_NOT_EQUAL(a, b, status(), "status");
RETURN_FALSE_IF_NOT_TRIMMED_EQUAL(a, b, summary(), "summary");
// check recurrence information. Note that we only need to check the recurrence rules for equality if they both recur.
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->recurs() != b->recurs(), "recurs", a->recurs() + " != " + b->recurs());
if (a->recurs() && !recurrenceDataEqual(a->recurrence(), b->recurrence())) {
return false;
}
// some special handling for dtStart() depending on whether it's an all-day event or not.
if (a->allDay() && b->allDay()) {
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtStart().date() != b->dtStart().date(), "dtStart", (a->dtStart().toString() + " != " + b->dtStart().toString()));
} else {
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtStart() != b->dtStart(), "dtStart", (a->dtStart().toString() + " != " + b->dtStart().toString()));
}
// Some servers insert a mailto: in the organizer email address, so ignore this when comparing organizers
KCalCore::Person personA(*a->organizer().data());
KCalCore::Person personB(*b->organizer().data());
normalizePersonEmail(&personA);
normalizePersonEmail(&personB);
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(personA != personB, "organizer", (personA.fullName() + " != " + personB.fullName()));
if (!pointerDataEqual(a->alarms(), b->alarms()))
return false;
if (!pointerDataEqual(a->attachments(), b->attachments()))
return false;
switch (a->type()) {
case KCalCore::IncidenceBase::TypeEvent:
if (!eventsEqual(a.staticCast<KCalCore::Event>(), b.staticCast<KCalCore::Event>())) {
return false;
}
break;
case KCalCore::IncidenceBase::TypeTodo:
if (!todosEqual(a.staticCast<KCalCore::Todo>(), b.staticCast<KCalCore::Todo>())) {
return false;
}
break;
case KCalCore::IncidenceBase::TypeJournal:
if (!journalsEqual(a.staticCast<KCalCore::Journal>(), b.staticCast<KCalCore::Journal>())) {
return false;
}
break;
case KCalCore::IncidenceBase::TypeFreeBusy:
case KCalCore::IncidenceBase::TypeUnknown:
break;
}
return true;
}
bool IncidenceHandler::eventsEqual(const KCalCore::Event::Ptr &a, const KCalCore::Event::Ptr &b)
{
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dateEnd() != b->dateEnd(), "dateEnd", (a->dateEnd().toString() + " != " + b->dateEnd().toString()));
RETURN_FALSE_IF_NOT_EQUAL(a, b, transparency(), "transparency");
// some special handling for dtEnd() depending on whether it's an all-day event or not.
if (a->allDay() && b->allDay()) {
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtEnd().date() != b->dtEnd().date(), "dtEnd", (a->dtEnd().toString() + " != " + b->dtEnd().toString()));
} else {
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtEnd() != b->dtEnd(), "dtEnd", (a->dtEnd().toString() + " != " + b->dtEnd().toString()));
}
// some special handling for isMultiday() depending on whether it's an all-day event or not.
if (a->allDay() && b->allDay()) {
// here we assume that both events are in "export form" (that is, exclusive DTEND)
if (a->dtEnd().date() != b->dtEnd().date()) {
LOG_WARNING("have a->dtStart()" << a->dtStart().toString() << ", a->dtEnd()" << a->dtEnd().toString());
LOG_WARNING("have b->dtStart()" << b->dtStart().toString() << ", b->dtEnd()" << b->dtEnd().toString());
LOG_WARNING("have a->isMultiDay()" << a->isMultiDay() << ", b->isMultiDay()" << b->isMultiDay());
return false;
}
} else {
RETURN_FALSE_IF_NOT_EQUAL(a, b, isMultiDay(), "multiday");
}
// Don't compare hasEndDate() as Event(Event*) does not initialize it based on the validity of
// dtEnd(), so it could be false when dtEnd() is valid. The dtEnd comparison above is sufficient.
return true;
}
bool IncidenceHandler::todosEqual(const KCalCore::Todo::Ptr &a, const KCalCore::Todo::Ptr &b)
{
RETURN_FALSE_IF_NOT_EQUAL(a, b, hasCompletedDate(), "hasCompletedDate");
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtRecurrence() != b->dtRecurrence(), "dtRecurrence", (a->dtRecurrence().toString() + " != " + b->dtRecurrence().toString()));
RETURN_FALSE_IF_NOT_EQUAL(a, b, hasDueDate(), "hasDueDate");
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->dtDue() != b->dtDue(), "dtDue", (a->dtDue().toString() + " != " + b->dtDue().toString()));
RETURN_FALSE_IF_NOT_EQUAL(a, b, hasStartDate(), "hasStartDate");
RETURN_FALSE_IF_NOT_EQUAL(a, b, isCompleted(), "isCompleted");
RETURN_FALSE_IF_NOT_EQUAL_CUSTOM(a->completed() != b->completed(), "completed", (a->completed().toString() + " != " + b->completed().toString()));
RETURN_FALSE_IF_NOT_EQUAL(a, b, isOpenEnded(), "isOpenEnded");
RETURN_FALSE_IF_NOT_EQUAL(a, b, percentComplete(), "percentComplete");
return true;
}
bool IncidenceHandler::journalsEqual(const KCalCore::Journal::Ptr &, const KCalCore::Journal::Ptr &)
{
// no journal-specific properties; it only uses the base incidence properties
return true;
}
void IncidenceHandler::copyIncidenceProperties(KCalCore::Incidence::Ptr dest, const KCalCore::Incidence::Ptr &src)
{
if (!dest || !src) {
qWarning() << "Invalid parameters!";
return;
}
if (dest->type() != src->type()) {
qWarning() << "incidences do not have same type!";
return;
}
KDateTime origCreated = dest->created();
KDateTime origLastModified = dest->lastModified();
// Copy recurrence information if required.
if (*(dest->recurrence()) != *(src->recurrence())) {
dest->recurrence()->clear();
KCalCore::Recurrence *dr = dest->recurrence();
KCalCore::Recurrence *sr = src->recurrence();
// recurrence rules and dates
KCalCore::RecurrenceRule::List srRRules = sr->rRules();
for (QList<KCalCore::RecurrenceRule*>::const_iterator it = srRRules.constBegin(), end = srRRules.constEnd(); it != end; ++it) {
KCalCore::RecurrenceRule *r = new KCalCore::RecurrenceRule(*(*it));
dr->addRRule(r);
}
dr->setRDates(sr->rDates());
dr->setRDateTimes(sr->rDateTimes());
// exception rules and dates
KCalCore::RecurrenceRule::List srExRules = sr->exRules();
for (QList<KCalCore::RecurrenceRule*>::const_iterator it = srExRules.constBegin(), end = srExRules.constEnd(); it != end; ++it) {
KCalCore::RecurrenceRule *r = new KCalCore::RecurrenceRule(*(*it));
dr->addExRule(r);
}
dr->setExDates(sr->exDates());
dr->setExDateTimes(sr->exDateTimes());
}
// copy the duration before the dtEnd as calling setDuration() changes the dtEnd
COPY_IF_NOT_EQUAL(dest, src, duration(), setDuration);
if (dest->type() == KCalCore::IncidenceBase::TypeEvent && src->type() == KCalCore::IncidenceBase::TypeEvent) {
KCalCore::Event::Ptr destEvent = dest.staticCast<KCalCore::Event>();
KCalCore::Event::Ptr srcEvent = src.staticCast<KCalCore::Event>();
COPY_IF_NOT_EQUAL(destEvent, srcEvent, dtEnd(), setDtEnd);
COPY_IF_NOT_EQUAL(destEvent, srcEvent, transparency(), setTransparency);
}
if (dest->type() == KCalCore::IncidenceBase::TypeTodo && src->type() == KCalCore::IncidenceBase::TypeTodo) {
KCalCore::Todo::Ptr destTodo = dest.staticCast<KCalCore::Todo>();
KCalCore::Todo::Ptr srcTodo = src.staticCast<KCalCore::Todo>();
COPY_IF_NOT_EQUAL(destTodo, srcTodo, completed(), setCompleted);
COPY_IF_NOT_EQUAL(destTodo, srcTodo, dtDue(), setDtDue);
COPY_IF_NOT_EQUAL(destTodo, srcTodo, dtRecurrence(), setDtRecurrence);
COPY_IF_NOT_EQUAL(destTodo, srcTodo, percentComplete(), setPercentComplete);
}
// dtStart and dtEnd changes allDay value, so must set those before copying allDay value
COPY_IF_NOT_EQUAL(dest, src, dtStart(), setDtStart);
COPY_IF_NOT_EQUAL(dest, src, allDay(), setAllDay);
COPY_IF_NOT_EQUAL(dest, src, hasDuration(), setHasDuration);
COPY_IF_NOT_EQUAL(dest, src, organizer(), setOrganizer);
COPY_IF_NOT_EQUAL(dest, src, isReadOnly(), setReadOnly);
if (!pointerDataEqual(src->attendees(), dest->attendees())) {
dest->clearAttendees();
Q_FOREACH (const KCalCore::Attendee::Ptr &attendee, src->attendees()) {
dest->addAttendee(attendee);
}
}
if (src->comments() != dest->comments()) {
dest->clearComments();
Q_FOREACH (const QString &comment, src->comments()) {
dest->addComment(comment);
}
}
if (src->contacts() != dest->contacts()) {
dest->clearContacts();
Q_FOREACH (const QString &contact, src->contacts()) {
dest->addContact(contact);
}
}
COPY_IF_NOT_EQUAL(dest, src, altDescription(), setAltDescription);
COPY_IF_NOT_EQUAL(dest, src, categories(), setCategories);
COPY_IF_NOT_EQUAL(dest, src, customStatus(), setCustomStatus);
COPY_IF_NOT_EQUAL(dest, src, description(), setDescription);
COPY_IF_NOT_EQUAL(dest, src, geoLatitude(), setGeoLatitude);
COPY_IF_NOT_EQUAL(dest, src, geoLongitude(), setGeoLongitude);
COPY_IF_NOT_EQUAL(dest, src, hasGeo(), setHasGeo);
COPY_IF_NOT_EQUAL(dest, src, location(), setLocation);
COPY_IF_NOT_EQUAL(dest, src, resources(), setResources);
COPY_IF_NOT_EQUAL(dest, src, secrecy(), setSecrecy);
COPY_IF_NOT_EQUAL(dest, src, status(), setStatus);
COPY_IF_NOT_EQUAL(dest, src, summary(), setSummary);
COPY_IF_NOT_EQUAL(dest, src, revision(), setRevision);
if (!pointerDataEqual(src->alarms(), dest->alarms())) {
dest->clearAlarms();
Q_FOREACH (const KCalCore::Alarm::Ptr &alarm, src->alarms()) {
dest->addAlarm(alarm);
}
}
if (!pointerDataEqual(src->attachments(), dest->attachments())) {
dest->clearAttachments();
Q_FOREACH (const KCalCore::Attachment::Ptr &attachment, src->attachments()) {
dest->addAttachment(attachment);
}
}
// Ensure all custom properties are copied also.
QSharedPointer<KCalCore::CustomProperties> srcCP =
src.staticCast<KCalCore::CustomProperties>();
QSharedPointer<KCalCore::CustomProperties> destCP =
dest.staticCast<KCalCore::CustomProperties>();
if (!(*srcCP.data() == *destCP.data()))
*destCP.data() = *srcCP.data();
// Don't change created and lastModified properties as that affects mkcal
// calculations for when the incidence was added and modified in the db.
if (origCreated != dest->created()) {
dest->setCreated(origCreated);
}
if (origLastModified != dest->lastModified()) {
dest->setLastModified(origLastModified);
}
}
void IncidenceHandler::prepareImportedIncidence(KCalCore::Incidence::Ptr incidence)
{
if (incidence->type() != KCalCore::IncidenceBase::TypeEvent) {
LOG_WARNING("unable to handle imported non-event incidence; skipping");
return;
}
switch (incidence->type()) {
case KCalCore::IncidenceBase::TypeEvent: {
KCalCore::Event::Ptr event = incidence.staticCast<KCalCore::Event>();
if (event->allDay()) {
KDateTime dtStart = event->dtStart();
KDateTime dtEnd = event->dtEnd();
// calendar processing requires all-day events to have a dtEnd
if (!dtEnd.isValid()) {
LOG_DEBUG("Adding DTEND to" << incidence->uid() << "as" << dtStart.toString());
event->setCustomProperty("buteo", PROP_DTEND_ADDED_USING_DTSTART, PROP_DTEND_ADDED_USING_DTSTART);
event->setDtEnd(dtStart);
}
// setting dtStart/End changes the allDay value, so ensure it is still set to true
event->setAllDay(true);
}
break;
}
default:
break;
}
}
// A given incidence has been added or modified locally.
// To upsync the change, we need to construct the .ics data to upload to server.
// Since the incidence may be an occurrence or recurring series incidence,
// we cannot simply convert the incidence to iCal data, but instead we have to
// upsync an .ics containing the whole recurring series.
QString IncidenceHandler::toIcs(KCalCore::Incidence::Ptr incidence,
KCalCore::Incidence::List instances)
{
KCalCore::Incidence::Ptr exportableIncidence = IncidenceHandler::incidenceToExport(incidence, instances);
// create an in-memory calendar
// add to it the required incidences (ie, check if has recurrenceId -> load parent and all instances; etc)
// for each of those, we need to do the IncidenceToExport() modifications first
// then, export from that calendar to .ics file.
KCalCore::MemoryCalendar::Ptr memoryCalendar(new KCalCore::MemoryCalendar(KDateTime::UTC));
// store the base recurring event into the in-memory calendar
if (!memoryCalendar->addIncidence(exportableIncidence)) {
LOG_WARNING("Unable to add base series event to in-memory calendar for incidence:"
<< incidence->uid() << ":" << incidence->recurrenceId().toString());
return QString();
}
// now create the persistent occurrences in the in-memory calendar
Q_FOREACH (KCalCore::Incidence::Ptr instance, instances) {
// We cannot call dissociateSingleOccurrence() on the MemoryCalendar
// as that's an mKCal specific function.
// We cannot call dissociateOccurrence() because that function
// takes only a QDate instead of a KDateTime recurrenceId.
// Thus, we need to manually create an exception occurrence.
KCalCore::Incidence::Ptr exportableOccurrence(exportableIncidence->clone());
exportableOccurrence->setCreated(instance->created());
exportableOccurrence->setRevision(instance->revision());
exportableOccurrence->clearRecurrence();
exportableOccurrence->setRecurrenceId(instance->recurrenceId());
exportableOccurrence->setDtStart(instance->recurrenceId());
// add it, and then update it in-memory.
if (!memoryCalendar->addIncidence(exportableOccurrence)) {
LOG_WARNING("Unable to add this incidence to in-memory calendar for export:"
<< instance->uid() << instance->recurrenceId().toString());
return QString();
} else {
KCalCore::Incidence::Ptr reloadedOccurrence = memoryCalendar->incidence(exportableIncidence->uid(), instance->recurrenceId());
if (!reloadedOccurrence) {
LOG_WARNING("Unable to find this incidence within in-memory calendar for export:"
<< exportableIncidence->uid() << instance->recurrenceId().toString());
return QString();
}
reloadedOccurrence->startUpdates();
IncidenceHandler::copyIncidenceProperties(reloadedOccurrence, IncidenceHandler::incidenceToExport(instance));
reloadedOccurrence->endUpdates();
}
}
KCalCore::ICalFormat icalFormat;
return icalFormat.toString(memoryCalendar, QString(), false);
}
KCalCore::Incidence::Ptr IncidenceHandler::incidenceToExport(KCalCore::Incidence::Ptr sourceIncidence, const KCalCore::Incidence::List &instances)
{
KCalCore::Incidence::Ptr incidence = QSharedPointer<KCalCore::Incidence>(sourceIncidence->clone());
// check to see if the UID is of the special form: NBUID:NotebookUid:EventUid. If so, trim it.
if (incidence->uid().startsWith(QStringLiteral("NBUID:"))) {
QString oldUid = incidence->uid();
QString trimmedUid = oldUid.mid(oldUid.indexOf(':', 6)+1); // remove NBUID:NotebookUid:
incidence->setUid(trimmedUid); // leaving just the EventUid.
}
// remove any (obsolete) markers that tell us that the time was added by us
incidence->removeCustomProperty("buteo", "dtstart-date_only");
incidence->removeCustomProperty("buteo", "dtend-date_only");
// remove any URI or ETAG data we insert into the event for sync purposes.
incidence->removeCustomProperty("buteo", "uri");
incidence->removeCustomProperty("buteo", "etag");
const QStringList &comments(incidence->comments());
Q_FOREACH (const QString &comment, comments) {
if (comment.startsWith("buteo:caldav:uri:") ||
comment.startsWith("buteo:caldav:detached-and-synced") ||
comment.startsWith("buteo:caldav:etag:")) {
LOG_DEBUG("Discarding buteo-prefixed comment:" << comment);
incidence->removeComment(comment);
}
}
// The default storage implementation applies the organizer as an attendee by default.
// Undo this as it turns the incidence into a scheduled event requiring acceptance/rejection/etc.
const KCalCore::Person::Ptr organizer = incidence->organizer();
if (organizer) {
Q_FOREACH (const KCalCore::Attendee::Ptr &attendee, incidence->attendees()) {
if (attendee->email() == organizer->email() && attendee->fullName() == organizer->fullName()) {
LOG_DEBUG("Discarding organizer as attendee" << attendee->fullName());
incidence->deleteAttendee(attendee);
} else {
LOG_DEBUG("Not discarding attendee:" << attendee->fullName() << attendee->email() << ": not organizer:" << organizer->fullName() << organizer->email());
}
}
}
// remove EXDATE values from the recurring incidence which correspond to the persistent occurrences (instances)
if (incidence->recurs()) {
Q_FOREACH (KCalCore::Incidence::Ptr instance, instances) {
KCalCore::DateTimeList exDateTimes = incidence->recurrence()->exDateTimes();
exDateTimes.removeAll(instance->recurrenceId());
incidence->recurrence()->setExDateTimes(exDateTimes);
LOG_DEBUG("Discarding exdate:" << instance->recurrenceId().toString());
}
}
// Icalformat from kcalcore converts second-type duration into day-type duration
// whenever possible. We do the same to have consistent comparisons.
KCalCore::Alarm::List alarms = incidence->alarms();
Q_FOREACH (KCalCore::Alarm::Ptr alarm, alarms) {
if (!alarm->hasTime()) {
KCalCore::Duration offset(0);
if (alarm->hasEndOffset())
offset = alarm->endOffset();
else
offset = alarm->startOffset();
if (!offset.isDaily() && !(offset.value() % (60 * 60 * 24))) {
LOG_DEBUG("Converting to day-type duration " << offset.asDays());
if (alarm->hasEndOffset())
alarm->setEndOffset(KCalCore::Duration(offset.asDays(), KCalCore::Duration::Days));
else
alarm->setStartOffset(KCalCore::Duration(offset.asDays(), KCalCore::Duration::Days));
}
}
}
switch (incidence->type()) {
case KCalCore::IncidenceBase::TypeEvent: {
KCalCore::Event::Ptr event = incidence.staticCast<KCalCore::Event>();
bool eventIsAllDay = event->allDay();
if (eventIsAllDay) {
bool sendWithoutDtEnd = !event->customProperty("buteo", PROP_DTEND_ADDED_USING_DTSTART).isEmpty()
&& (event->dtStart() == event->dtEnd());
event->removeCustomProperty("buteo", PROP_DTEND_ADDED_USING_DTSTART);
if (sendWithoutDtEnd) {
// A single-day all-day event was received without a DTEND, and it is still a single-day
// all-day event, so remove the DTEND before upsyncing.
LOG_DEBUG("Removing DTEND from" << incidence->uid());
event->setDtEnd(KDateTime());
} else {
KDateTime dt;
if (event->hasEndDate()) {
// Event::dtEnd() is inclusive, but DTEND in iCalendar format is exclusive.
dt = KDateTime(event->dtEnd().addDays(1).date(), KDateTime::Spec::ClockTime());
LOG_DEBUG("Adding +1 day to DTEND to make exclusive DTEND for" << incidence->uid() << ":" << dt.toString());
} else {
// No DTEND exists in event, but it's all day. Set to DTSTART+1 to make exclusive DTEND.
dt = KDateTime(event->dtStart().addDays(1).date(), KDateTime::Spec::ClockTime());
LOG_DEBUG("Setting DTEND to DTSTART+1 for" << incidence->uid() << ":" << dt.toString());
}
dt.setDateOnly(true);
LOG_DEBUG("Stripping time from date-only DTEND:" << dt.toString());
event->setDtEnd(dt);
}
}
if (event->dtStart().isDateOnly()) {
KDateTime dt = KDateTime(event->dtStart().date(), KDateTime::Spec::ClockTime());
dt.setDateOnly(true);
event->setDtStart(dt);
LOG_DEBUG("Stripping time from date-only DTSTART:" << dt.toString());
}
// setting dtStart/End changes the allDay value, so ensure it is still set to true if needed.
if (eventIsAllDay) {
event->setAllDay(true);
}
break;
}
case KCalCore::IncidenceBase::TypeTodo:
break;
default:
LOG_DEBUG("Incidence type not supported; cannot create proper exportable version");
break;
}
return incidence;
}