File: calendarbase.cpp

package info (click to toggle)
akonadi-calendar 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,808 kB
  • sloc: cpp: 14,079; xml: 36; makefile: 8; sh: 4
file content (702 lines) | stat: -rw-r--r-- 24,603 bytes parent folder | download | duplicates (2)
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
   Copyright (C) 2011 Sérgio Martins <sergio.martins@kdab.com>
   Copyright (C) 2012 Sérgio Martins <iamsergio@gmail.com>

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   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 Library General Public
   License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include "calendarbase.h"
#include "calendarbase_p.h"
#include "incidencechanger.h"
#include "utils_p.h"
#include "akonadicalendar_debug.h"
#include <item.h>
#include <collectionfetchjob.h>
#include <collection.h>

#include <KLocalizedString>
#include <QTimeZone>

using namespace Akonadi;
using namespace KCalCore;

static QString itemToString(const Akonadi::Item &item)
{
    const KCalCore::Incidence::Ptr &incidence = CalendarUtils::incidence(item);
    QString str;
    QTextStream stream(&str);
    stream << item.id()
           << "; summary=" << incidence->summary() << "; uid=" << incidence->uid() << "; type="
           << incidence->type() << "; recurs=" << incidence->recurs() << "; recurrenceId="
           << incidence->recurrenceId().toString() << "; dtStart=" << incidence->dtStart().toString()
           << "; dtEnd=" << incidence->dateTime(Incidence::RoleEnd).toString()
           << "; parentCollection=" << item.storageCollectionId() << item.parentCollection().displayName();

    return str;
}

CalendarBasePrivate::CalendarBasePrivate(CalendarBase *qq)
    : QObject()
    , mIncidenceChanger(new IncidenceChanger())
    , mBatchInsertionCancelled(false)
    , mListensForNewItems(false)
    , mLastCreationCancelled(false)
    , q(qq)
{
    connect(mIncidenceChanger, &IncidenceChanger::createFinished,
            this, &CalendarBasePrivate::slotCreateFinished);

    connect(mIncidenceChanger, &IncidenceChanger::deleteFinished,
            this, &CalendarBasePrivate::slotDeleteFinished);

    connect(mIncidenceChanger, &IncidenceChanger::modifyFinished,
            this, &CalendarBasePrivate::slotModifyFinished);

    mIncidenceChanger->setDestinationPolicy(IncidenceChanger::DestinationPolicyAsk);
    mIncidenceChanger->setGroupwareCommunication(false);
    mIncidenceChanger->setHistoryEnabled(false);
}

CalendarBasePrivate::~CalendarBasePrivate()
{
    delete mIncidenceChanger;
    mIncidenceChanger = nullptr;
}

void CalendarBasePrivate::internalInsert(const Akonadi::Item &item)
{
    Q_ASSERT(item.isValid());
    Q_ASSERT(item.hasPayload<KCalCore::Incidence::Ptr>());
    KCalCore::Incidence::Ptr incidence = CalendarUtils::incidence(item);

    if (!incidence) {
        qCritical() << "Incidence is null. id=" << item.id()
                    << "; hasPayload()="  << item.hasPayload()
                    << "; has incidence=" << item.hasPayload<KCalCore::Incidence::Ptr>()
                    << "; mime type="     << item.mimeType();
        Q_ASSERT(false);
        return;
    }

    //qCDebug(AKONADICALENDAR_LOG) << "Inserting incidence in calendar. id=" << item.id() << "uid=" << incidence->uid();
    const QString uid = incidence->instanceIdentifier();

    if (uid.isEmpty()) {
        // This code path should never happen
        qCritical() << "Incidence has empty UID. id=" << item.id()
                    << "; summary=" << incidence->summary()
                    << "Please fix it. Ignoring this incidence.";
        return;
    }

    if (mItemIdByUid.contains(uid) && mItemIdByUid[uid] != item.id()) {
        // We only allow duplicate UIDs if they have the same item id, for example
        // when using virtual folders.
#if 0
        qCWarning(AKONADICALENDAR_LOG) << "Discarding duplicate incidence with instanceIdentifier=" << uid
                                       << "and summary " << incidence->summary()
                                       << "; recurrenceId() =" << incidence->recurrenceId()
                                       << "; new id=" << item.id()
                                       << "; existing id=" << mItemIdByUid[uid];
#endif
        return;
    }

    if (incidence->type() == KCalCore::Incidence::TypeEvent && !incidence->dtStart().isValid()) {
        // TODO: make the parser discard them would also be a good idea
        qCWarning(AKONADICALENDAR_LOG) << "Discarding event with invalid DTSTART. identifier="
                                       << incidence->instanceIdentifier() << "; summary=" << incidence->summary();
        return;
    }

    Akonadi::Collection collection = item.parentCollection();
    if (collection.isValid()) {
        // Some items don't have collection set
        if (item.storageCollectionId() != collection.id() && item.storageCollectionId() > -1) {
            if (mCollections.contains(item.storageCollectionId())) {
                collection = mCollections.value(item.storageCollectionId());
                incidence->setReadOnly(!(collection.rights() & Akonadi::Collection::CanChangeItem));
            } else if (!mCollectionJobs.key(item.storageCollectionId())) {
                collection = Akonadi::Collection(item.storageCollectionId());
                Akonadi::CollectionFetchJob *job = new Akonadi::CollectionFetchJob(collection, Akonadi::CollectionFetchJob::Base, this);
                QObject::connect(job, &KJob::result, this, &CalendarBasePrivate::collectionFetchResult);
                mCollectionJobs.insert(job,  collection.id());
            }
        } else {
            mCollections.insert(collection.id(), collection);
            incidence->setReadOnly(!(collection.rights() & Akonadi::Collection::CanChangeItem));
        }
    }

    mItemById.insert(item.id(), item);
    mItemIdByUid.insert(uid, item.id());
    mItemsByCollection.insert(item.storageCollectionId(), item);

    if (!incidence->hasRecurrenceId()) {
        // Insert parent relationships
        const QString parentUid = incidence->relatedTo();
        if (!parentUid.isEmpty()) {
            mParentUidToChildrenUid[parentUid].append(incidence->uid());
            mUidToParent.insert(uid, parentUid);
        }
    }

    incidence->setCustomProperty("VOLATILE", "AKONADI-ID", QString::number(item.id()));
    // Must be the last one due to re-entrancy
    const bool result = q->MemoryCalendar::addIncidence(incidence);
    if (!result) {
        qCritical() << "Error adding incidence " << itemToString(item);
        Q_ASSERT(false);
    }
}

void CalendarBasePrivate::collectionFetchResult(KJob *job)
{
    Akonadi::Collection::Id colid = mCollectionJobs.take(job);

    if (job->error()) {
        qWarning() << "Error occurred: " << job->errorString();
        return;
    }

    Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);

    const Akonadi::Collection collection = fetchJob->collections().at(0);
    if (collection.id() !=  colid) {
        qCritical() <<  "Fetched the wrong collection,  should fetch: " <<  colid << "fetched: " <<  collection.id();
    }

    bool isReadOnly = !(collection.rights() & Akonadi::Collection::CanChangeItem);
    foreach (const Akonadi::Item &item, mItemsByCollection.values(collection.id())) {
        KCalCore::Incidence::Ptr incidence = CalendarUtils::incidence(item);
        incidence->setReadOnly(isReadOnly);
    }

    mCollections.insert(collection.id(),  collection);

    if (mCollectionJobs.isEmpty()) {
        emit fetchFinished();
    }
}

void CalendarBasePrivate::internalRemove(const Akonadi::Item &item)
{
    Q_ASSERT(item.isValid());

    Incidence::Ptr tmp = CalendarUtils::incidence(item);
    if (!tmp) {
        qCritical() << "CalendarBase::internalRemove1: incidence is null, item.id=" << item.id();
        return;
    }

    // We want the one stored in the calendar
    Incidence::Ptr incidence = q->incidence(tmp->uid(), tmp->recurrenceId());

    // Null incidence means it was deleted via CalendarBase::deleteIncidence(), but then
    // the ETMCalendar received the monitor notification and tried to delete it again.
    if (incidence) {
        q->Calendar::notifyIncidenceAboutToBeDeleted(incidence);

        mItemById.remove(item.id());
        // qCDebug(AKONADICALENDAR_LOG) << "Deleting incidence from calendar .id=" << item.id() << "uid=" << incidence->uid();
        mItemIdByUid.remove(incidence->instanceIdentifier());

        mItemsByCollection.remove(item.storageCollectionId(), item);

        if (!incidence->hasRecurrenceId()) {
            const QString uid = incidence->uid();
            const QString parentUid = incidence->relatedTo();
            mParentUidToChildrenUid.remove(uid);
            if (!parentUid.isEmpty()) {
                mParentUidToChildrenUid[parentUid].removeAll(uid);
                mUidToParent.remove(uid);
            }
        }

        q->Calendar::setObserversEnabled(false);
        // Must be the last one due to re-entrancy
        const bool result = q->MemoryCalendar::deleteIncidence(incidence);
        q->Calendar::setObserversEnabled(true);
        q->Calendar::notifyIncidenceDeleted(incidence);
        if (!result) {
            qCritical() << "Error removing incidence " << itemToString(item);
            Q_ASSERT(false);
        }
    } else {
        qCWarning(AKONADICALENDAR_LOG) << "CalendarBase::internalRemove2: incidence is null, item.id=" << itemToString(item);
    }
}

void CalendarBasePrivate::slotDeleteFinished(int changeId,
        const QVector<Akonadi::Item::Id> &itemIds,
        IncidenceChanger::ResultCode resultCode,
        const QString &errorMessage)
{
    Q_UNUSED(changeId);
    if (resultCode == IncidenceChanger::ResultCodeSuccess) {
        for (const Akonadi::Item::Id &id : itemIds) {
            if (mItemById.contains(id)) {
                internalRemove(mItemById.value(id));
            }
        }
    }

    emit q->deleteFinished(resultCode == IncidenceChanger::ResultCodeSuccess, errorMessage);
}

void CalendarBasePrivate::slotCreateFinished(int changeId,
        const Akonadi::Item &item,
        IncidenceChanger::ResultCode resultCode,
        const QString &errorMessage)
{
    Q_UNUSED(changeId);
    Q_UNUSED(item);
    if (resultCode == IncidenceChanger::ResultCodeSuccess && !mListensForNewItems) {
        Q_ASSERT(item.isValid());
        Q_ASSERT(item.hasPayload<KCalCore::Incidence::Ptr>());
        internalInsert(item);
    }

    mLastCreationCancelled = (resultCode == IncidenceChanger::ResultCodeUserCanceled);

    emit q->createFinished(resultCode == IncidenceChanger::ResultCodeSuccess, errorMessage);
}

void CalendarBasePrivate::slotModifyFinished(int changeId,
        const Akonadi::Item &item,
        IncidenceChanger::ResultCode resultCode,
        const QString &errorMessage)
{
    Q_UNUSED(changeId);
    Q_UNUSED(item);
    QString message = errorMessage;
    if (resultCode == IncidenceChanger::ResultCodeSuccess) {
        KCalCore::Incidence::Ptr incidence = CalendarUtils::incidence(item);
        Q_ASSERT(incidence);
        KCalCore::Incidence::Ptr localIncidence = q->incidence(incidence->instanceIdentifier());

        if (localIncidence) {
            //update our local one
            *(static_cast<KCalCore::IncidenceBase *>(localIncidence.data())) = *(incidence.data());
        } else {
            // This shouldn't happen, unless the incidence gets deleted between event loops
            qCWarning(AKONADICALENDAR_LOG) << "CalendarBasePrivate::slotModifyFinished() Incidence was deleted already probably? id=" << item.id();
            message = i18n("Could not find incidence to update, it probably was deleted recently.");
            resultCode = IncidenceChanger::ResultCodeAlreadyDeleted;
        }
    }
    emit q->modifyFinished(resultCode == IncidenceChanger::ResultCodeSuccess, message);
}

void CalendarBasePrivate::handleUidChange(const Akonadi::Item &oldItem,
        const Akonadi::Item &newItem, const QString &newIdentifier)
{
    Q_ASSERT(oldItem.isValid());
    Incidence::Ptr newIncidence = CalendarUtils::incidence(newItem);
    Q_ASSERT(newIncidence);
    Incidence::Ptr oldIncidence = CalendarUtils::incidence(oldItem);
    Q_ASSERT(oldIncidence);

    const QString newUid = newIncidence->uid();
    if (mItemIdByUid.contains(newIdentifier)) {
        Incidence::Ptr oldIncidence = CalendarUtils::incidence(oldItem);
#if 0
        qCWarning(AKONADICALENDAR_LOG) << "New uid shouldn't be known: "  << newIdentifier << "; id="
                                       << newItem.id() << "; oldItem.id=" << mItemIdByUid[newIdentifier]
                                       << "; new summary= " << newIncidence->summary()
                                       << "; new recurrenceId=" << newIncidence->recurrenceId()
                                       << "; oldIncidence" << oldIncidence;
#endif
        if (oldIncidence) {
#if 0
            qCWarning(AKONADICALENDAR_LOG) << "; oldIncidence uid=" << oldIncidence->uid()
                                           << "; oldIncidence recurrenceId = " << oldIncidence->recurrenceId()
                                           << "; oldIncidence summary = " << oldIncidence->summary();
#endif
        }
        Q_ASSERT(false);
        return;
    }

    mItemIdByUid[newIdentifier] = newItem.id();

    // Get the real pointer
    oldIncidence = q->MemoryCalendar::incidence(oldIncidence->uid());

    if (!oldIncidence) {
        // How can this happen ?
        qCWarning(AKONADICALENDAR_LOG) << "Couldn't find old incidence";
        Q_ASSERT(false);
        return;
    }

    if (newIncidence->instanceIdentifier() == oldIncidence->instanceIdentifier()) {
#if 0
        qCWarning(AKONADICALENDAR_LOG) << "New uid=" << newIncidence->uid() << "; old uid=" << oldIncidence->uid()
                                       << "; new recurrenceId="
                                       << newIncidence->recurrenceId()
                                       << "; old recurrenceId=" << oldIncidence->recurrenceId()
                                       << "; new summary = " << newIncidence->summary()
                                       << "; old summary = " << oldIncidence->summary()
                                       << "; id = " << newItem.id();
#endif
        Q_ASSERT(false);   // The reason we're here in the first place
        return;
    }

    mItemIdByUid.remove(oldIncidence->instanceIdentifier());
    const QString oldUid = oldIncidence->uid();

    if (mParentUidToChildrenUid.contains(oldUid)) {
        Q_ASSERT(!mParentUidToChildrenUid.contains(newIdentifier));
        QStringList children = mParentUidToChildrenUid.value(oldUid);
        mParentUidToChildrenUid.insert(newIdentifier, children);
        mParentUidToChildrenUid.remove(oldUid);
    }

    // Update internal maps of the base class, MemoryCalendar
    q->setObserversEnabled(false);
    q->MemoryCalendar::deleteIncidence(oldIncidence);
    q->MemoryCalendar::addIncidence(newIncidence);

    newIncidence->setUid(oldUid); // We set and unset just to notify observers of a change.
    q->setObserversEnabled(true);
    newIncidence->setUid(newUid);
}

void CalendarBasePrivate::handleParentChanged(const KCalCore::Incidence::Ptr &newIncidence)
{
    Q_ASSERT(newIncidence);

    if (newIncidence->hasRecurrenceId()) {   // These ones don't/shouldn't have a parent
        return;
    }

    const QString originalParentUid = mUidToParent.value(newIncidence->uid());
    const QString newParentUid = newIncidence->relatedTo();

    if (originalParentUid == newParentUid) {
        return; // nothing changed
    }

    if (!originalParentUid.isEmpty()) {
        // Remove this child from it's old parent:
        Q_ASSERT(mParentUidToChildrenUid.contains(originalParentUid));
        mParentUidToChildrenUid[originalParentUid].removeAll(newIncidence->uid());
    }

    mUidToParent.remove(newIncidence->uid());

    if (!newParentUid.isEmpty()) {
        // Deliver this child to it's new parent:
        Q_ASSERT(!mParentUidToChildrenUid[newParentUid].contains(newIncidence->uid()));
        mParentUidToChildrenUid[newParentUid].append(newIncidence->uid());
        mUidToParent.insert(newIncidence->uid(), newParentUid);
    }
}

CalendarBase::CalendarBase(QObject *parent)
    : MemoryCalendar(QTimeZone::systemTimeZone())
    , d_ptr(new CalendarBasePrivate(this))
{
    setParent(parent);
    setDeletionTracking(false);
}

CalendarBase::CalendarBase(CalendarBasePrivate *const dd, QObject *parent)
    : MemoryCalendar(QTimeZone::systemTimeZone())
    , d_ptr(dd)
{
    setParent(parent);
    setDeletionTracking(false);
}

CalendarBase::~CalendarBase()
{
}

Akonadi::Item CalendarBase::item(Akonadi::Item::Id id) const
{
    Q_D(const CalendarBase);
    Akonadi::Item i;
    auto it = d->mItemById.constFind(id);
    if (it != d->mItemById.cend()) {
        i = *it;
    } else {
        qCDebug(AKONADICALENDAR_LOG) << "Can't find any item with id " << id;
    }
    return i;
}

Akonadi::Item CalendarBase::item(const QString &uid) const
{
    Q_D(const CalendarBase);
    Akonadi::Item i;

    if (uid.isEmpty()) {
        return i;
    }

    auto it = d->mItemIdByUid.constFind(uid);
    if (it != d->mItemIdByUid.cend()) {
        const Akonadi::Item::Id id = *it;
        auto it2 = d->mItemById.constFind(id);
        if (it2 == d->mItemById.cend()) {
            qCritical() << "Item with id " << id << "(uid=" << uid << ") not found, but in uid map";
            Q_ASSERT_X(false, "CalendarBase::item", "not in mItemById");
        }
        i = *it2;
    } else {
        qCDebug(AKONADICALENDAR_LOG) << "Can't find any incidence with uid " << uid;
    }
    return i;
}

Item CalendarBase::item(const Incidence::Ptr &incidence) const
{
    return incidence ? item(incidence->instanceIdentifier()) : Item();
}

Akonadi::Item::List CalendarBase::items(Akonadi::Collection::Id id) const
{
    Q_D(const CalendarBase);

    Akonadi::Item::List result;
    if (id == -1)
        result.reserve(d->mItemsByCollection.size());

    auto it = id == -1 ? d->mItemsByCollection.cbegin() : d->mItemsByCollection.constFind(id);
    while (it != d->mItemsByCollection.cend() && (id == -1 || it.key() == id)) {
        result.push_back(*it);
        ++it;
    }

    return result;
}

Akonadi::Item::List CalendarBase::itemList(const KCalCore::Incidence::List &incidences) const
{
    Akonadi::Item::List items;
    items.reserve(incidences.size());

    for (const KCalCore::Incidence::Ptr &incidence : incidences) {
        if (incidence) {
            items << item(incidence->instanceIdentifier());
        } else {
            items << Akonadi::Item();
        }
    }

    return items;
}

KCalCore::Incidence::List CalendarBase::childIncidences(Akonadi::Item::Id parentId) const
{
    Q_D(const CalendarBase);
    KCalCore::Incidence::List childs;

    if (d->mItemById.contains(parentId)) {
        const Akonadi::Item item = d->mItemById.value(parentId);
        Q_ASSERT(item.isValid());
        KCalCore::Incidence::Ptr parent = CalendarUtils::incidence(item);

        if (parent) {
            childs = childIncidences(parent->uid());
        } else {
            Q_ASSERT(false);
        }
    }

    return childs;
}

KCalCore::Incidence::List CalendarBase::childIncidences(const QString &parentUid) const
{
    Q_D(const CalendarBase);
    KCalCore::Incidence::List children;
    const QStringList uids = d->mParentUidToChildrenUid.value(parentUid);
    for (const QString &uid : uids) {
        Incidence::Ptr child = incidence(uid);
        if (child) {
            children.append(child);
        } else {
            qCWarning(AKONADICALENDAR_LOG) << "Invalid child with uid " << uid;
        }
    }
    return children;
}

Akonadi::Item::List CalendarBase::childItems(const Akonadi::Item::Id &parentId) const
{
    Q_D(const CalendarBase);
    Akonadi::Item::List childs;

    if (d->mItemById.contains(parentId)) {
        const Akonadi::Item item = d->mItemById.value(parentId);
        Q_ASSERT(item.isValid());
        KCalCore::Incidence::Ptr parent = CalendarUtils::incidence(item);

        if (parent) {
            childs = childItems(parent->uid());
        } else {
            Q_ASSERT(false);
        }
    }

    return childs;
}

Akonadi::Item::List CalendarBase::childItems(const QString &parentUid) const
{
    Q_D(const CalendarBase);
    Akonadi::Item::List children;
    const QStringList uids = d->mParentUidToChildrenUid.value(parentUid);
    for (const QString &uid : uids) {
        Akonadi::Item child = item(uid);
        if (child.isValid() && child.hasPayload<KCalCore::Incidence::Ptr>()) {
            children.append(child);
        } else {
            qCWarning(AKONADICALENDAR_LOG) << "Invalid child with uid " << uid;
        }
    }
    return children;
}

bool CalendarBase::addEvent(const KCalCore::Event::Ptr &event)
{
    return addIncidence(event);
}

bool CalendarBase::deleteEvent(const KCalCore::Event::Ptr &event)
{
    return deleteIncidence(event);
}

bool CalendarBase::addTodo(const KCalCore::Todo::Ptr &todo)
{
    return addIncidence(todo);
}

bool CalendarBase::deleteTodo(const KCalCore::Todo::Ptr &todo)
{
    return deleteIncidence(todo);
}

bool CalendarBase::addJournal(const KCalCore::Journal::Ptr &journal)
{
    return addIncidence(journal);
}

bool CalendarBase::deleteJournal(const KCalCore::Journal::Ptr &journal)
{
    return deleteIncidence(journal);
}

bool CalendarBase::addIncidence(const KCalCore::Incidence::Ptr &incidence)
{
    //TODO: Parent for dialogs
    Q_D(CalendarBase);

    // User canceled on the collection selection dialog
    if (batchAdding() && d->mBatchInsertionCancelled) {
        return false;
    }

    d->mLastCreationCancelled = false;

    Akonadi::Collection collection;

    if (batchAdding() && d->mCollectionForBatchInsertion.isValid()) {
        collection = d->mCollectionForBatchInsertion;
    }

    if (incidence->hasRecurrenceId() && !collection.isValid()) {
        // We are creating an exception, reuse the same collection that the main incidence uses
        Item mainItem = item(incidence->uid());
        if (mainItem.isValid()) {
            collection = Collection(mainItem.storageCollectionId());
        }
    }

    const int changeId = d->mIncidenceChanger->createIncidence(incidence, collection);

    if (batchAdding()) {
        const Akonadi::Collection lastCollection = d->mIncidenceChanger->lastCollectionUsed();
        if (changeId != -1 && !lastCollection.isValid()) {
            d->mBatchInsertionCancelled = true;
        } else if (lastCollection.isValid() && !d->mCollectionForBatchInsertion.isValid()) {
            d->mCollectionForBatchInsertion = d->mIncidenceChanger->lastCollectionUsed();
        }
    }

    return changeId != -1;
}

bool CalendarBase::deleteIncidence(const KCalCore::Incidence::Ptr &incidence)
{
    Q_D(CalendarBase);
    Q_ASSERT(incidence);
    Akonadi::Item item_ = item(incidence->instanceIdentifier());
    return -1 != d->mIncidenceChanger->deleteIncidence(item_);
}

bool CalendarBase::modifyIncidence(const KCalCore::Incidence::Ptr &newIncidence)
{
    Q_D(CalendarBase);
    Q_ASSERT(newIncidence);
    Akonadi::Item item_ = item(newIncidence->instanceIdentifier());
    item_.setPayload<KCalCore::Incidence::Ptr>(newIncidence);
    return -1 != d->mIncidenceChanger->modifyIncidence(item_);
}

void CalendarBase::setWeakPointer(const QWeakPointer<CalendarBase> &pointer)
{
    Q_D(CalendarBase);
    d->mWeakPointer = pointer;
}

QWeakPointer<CalendarBase> CalendarBase::weakPointer() const
{
    Q_D(const CalendarBase);
    return d->mWeakPointer;
}

IncidenceChanger *CalendarBase::incidenceChanger() const
{
    Q_D(const CalendarBase);
    return d->mIncidenceChanger;
}

void CalendarBase::startBatchAdding()
{
    KCalCore::MemoryCalendar::startBatchAdding();
}

void CalendarBase::endBatchAdding()
{
    Q_D(CalendarBase);
    d->mCollectionForBatchInsertion = Akonadi::Collection();
    d->mBatchInsertionCancelled = false;
    KCalCore::MemoryCalendar::endBatchAdding();
}

bool CalendarBase::isLoaded() const
{
    return true;
}

#include "moc_calendarbase.cpp"
#include "moc_calendarbase_p.cpp"