davgroupwareresource.cpp 54 KB
Newer Older
1
2
/*
    Copyright (c) 2009 Grégory Oestreicher <greg@kamago.net>
3

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

9
10
11
12
    This program 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 General Public License for more details.
13

14
15
16
17
18
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

19
#include "davgroupwareresource.h"
20

Sandro Knauß's avatar
Sandro Knauß committed
21
#include "akonadietagcache.h"
22
#include "configdialog.h"
23
#include "ctagattribute.h"
24
#include "davfreebusyhandler.h"
25
#include "davprotocolattribute.h"
Sandro Knauß's avatar
Sandro Knauß committed
26
#include "utils.h"
Tobias Koenig's avatar
Tobias Koenig committed
27
28
#include "settings.h"
#include "settingsadaptor.h"
29
#include "setupwizard.h"
30

Sandro Knauß's avatar
Sandro Knauß committed
31
32
33
34
35
36
37
38
39
40
41
#include <KDAV/DavCollection>
#include <KDAV/DavCollectionDeleteJob>
#include <KDAV/DavCollectionsFetchJob>
#include <KDAV/DavCollectionsMultiFetchJob>
#include <KDAV/DavItem>
#include <KDAV/DavItemCreateJob>
#include <KDAV/DavItemDeleteJob>
#include <KDAV/DavItemFetchJob>
#include <KDAV/DavItemModifyJob>
#include <KDAV/DavItemsFetchJob>
#include <KDAV/DavItemsListJob>
Volker Krause's avatar
Volker Krause committed
42
#include <KDAV/ProtocolInfo>
Sandro Knauß's avatar
Sandro Knauß committed
43

44
45
46
47
48
#include <KCalendarCore/FreeBusy>
#include <KCalendarCore/Incidence>
#include <KCalendarCore/ICalFormat>
#include <KCalendarCore/MemoryCalendar>
#include <KCalendarCore/Todo>
49
#include <kjob.h>
50

51
#include <attributefactory.h>
52
#include <collectioncolorattribute.h>
53
54
55
56
57
58
#include <cachepolicy.h>
#include <changerecorder.h>
#include <collectionfetchscope.h>
#include <entitydisplayattribute.h>
#include <itemfetchjob.h>
#include <itemfetchscope.h>
Laurent Montel's avatar
Laurent Montel committed
59
60
#include <AkonadiCore/recursiveitemfetchjob.h>
#include <AkonadiCore/itemmodifyjob.h>
Laurent Montel's avatar
Laurent Montel committed
61
#include <AkonadiCore/itemdeletejob.h>
62
#include <AkonadiCore/collectionmodifyjob.h>
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
63
64
#include <kcontacts/addressee.h>
#include <kcontacts/vcardconverter.h>
Tobias Koenig's avatar
Tobias Koenig committed
65
#include <kwindowsystem.h>
66
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
67
#include "davresource_debug.h"
Tobias Koenig's avatar
Tobias Koenig committed
68

Laurent Montel's avatar
Laurent Montel committed
69
#include <QSet>
70
71
72

using namespace Akonadi;

73
typedef QSharedPointer<KCalendarCore::Incidence> IncidencePtr;
74

Laurent Montel's avatar
Laurent Montel committed
75
DavGroupwareResource::DavGroupwareResource(const QString &id)
Sandro Knauß's avatar
Sandro Knauß committed
76
77
78
    : ResourceBase(id)
    , FreeBusyProviderBase()
    , mSyncErrorNotified(false)
79
{
Laurent Montel's avatar
Laurent Montel committed
80
81
    AttributeFactory::registerAttribute<EntityDisplayAttribute>();
    AttributeFactory::registerAttribute<DavProtocolAttribute>();
82
    AttributeFactory::registerAttribute<CTagAttribute>();
83
    AttributeFactory::registerAttribute<CollectionColorAttribute>();
84

Laurent Montel's avatar
Laurent Montel committed
85
    setNeedsNetwork(true);
86

Laurent Montel's avatar
Laurent Montel committed
87
88
89
90
91
    mDavCollectionRoot.setParentCollection(Collection::root());
    mDavCollectionRoot.setName(identifier());
    mDavCollectionRoot.setRemoteId(identifier());
    mDavCollectionRoot.setContentMimeTypes(QStringList() << Collection::mimeType());
    mDavCollectionRoot.setRights(Collection::CanCreateCollection | Collection::CanDeleteCollection | Collection::CanChangeCollection);
92

Laurent Montel's avatar
Laurent Montel committed
93
    EntityDisplayAttribute *attribute = mDavCollectionRoot.attribute<EntityDisplayAttribute>(Collection::AddIfMissing);
Laurent Montel's avatar
Laurent Montel committed
94
    attribute->setIconName(QStringLiteral("folder-remote"));
95

Laurent Montel's avatar
Laurent Montel committed
96
97
98
99
    int refreshInterval = Settings::self()->refreshInterval();
    if (refreshInterval == 0) {
        refreshInterval = -1;
    }
100

Laurent Montel's avatar
Laurent Montel committed
101
102
103
104
105
    Akonadi::CachePolicy cachePolicy;
    cachePolicy.setInheritFromParent(false);
    cachePolicy.setSyncOnDemand(false);
    cachePolicy.setCacheTimeout(-1);
    cachePolicy.setIntervalCheckTime(refreshInterval);
Laurent Montel's avatar
Laurent Montel committed
106
    cachePolicy.setLocalParts(QStringList() << QStringLiteral("ALL"));
Laurent Montel's avatar
Laurent Montel committed
107
    mDavCollectionRoot.setCachePolicy(cachePolicy);
108

Laurent Montel's avatar
Laurent Montel committed
109
110
111
112
    changeRecorder()->fetchCollection(true);
    changeRecorder()->collectionFetchScope().setAncestorRetrieval(Akonadi::CollectionFetchScope::All);
    changeRecorder()->itemFetchScope().fetchFullPayload(true);
    changeRecorder()->itemFetchScope().setAncestorRetrieval(ItemFetchScope::All);
113

Laurent Montel's avatar
Laurent Montel committed
114
115
    Settings::self()->setWinId(winIdForDialogs());
    Settings::self()->setResourceIdentifier(identifier());
116

Laurent Montel's avatar
Laurent Montel committed
117
118
119
    mFreeBusyHandler = new DavFreeBusyHandler(this);
    connect(mFreeBusyHandler, &DavFreeBusyHandler::handlesFreeBusy, this, &DavGroupwareResource::onHandlesFreeBusy);
    connect(mFreeBusyHandler, &DavFreeBusyHandler::freeBusyRetrieved, this, &DavGroupwareResource::onFreeBusyRetrieved);
Gregory Oestreicher's avatar
Gregory Oestreicher committed
120

Laurent Montel's avatar
Laurent Montel committed
121
    connect(this, &DavGroupwareResource::reloadConfiguration, this, &DavGroupwareResource::onReloadConfig);
Grégory Oestreicher's avatar
Grégory Oestreicher committed
122

123
    scheduleCustomTask(this, "initialRetrieveCollections", QVariant(), ResourceBase::Prepend);
124
    scheduleCustomTask(this, "createInitialCache", QVariant(), ResourceBase::Prepend);
125
126
}

127
DavGroupwareResource::~DavGroupwareResource()
128
{
Laurent Montel's avatar
Laurent Montel committed
129
    delete mFreeBusyHandler;
130
131
}

Laurent Montel's avatar
Laurent Montel committed
132
void DavGroupwareResource::collectionRemoved(const Akonadi::Collection &collection)
133
{
Laurent Montel's avatar
Laurent Montel committed
134
    qCDebug(DAVRESOURCE_LOG) << "Removing collection " << collection.remoteId();
135

Laurent Montel's avatar
Laurent Montel committed
136
137
138
    if (!configurationIsValid()) {
        return;
    }
139

Sandro Knauß's avatar
Sandro Knauß committed
140
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(collection.remoteId());
141

Sandro Knauß's avatar
Sandro Knauß committed
142
    KDAV::DavCollectionDeleteJob *job = new KDAV::DavCollectionDeleteJob(davUrl);
Laurent Montel's avatar
Laurent Montel committed
143
    job->setProperty("collection", QVariant::fromValue(collection));
Sandro Knauß's avatar
Sandro Knauß committed
144
    connect(job, &KDAV::DavCollectionDeleteJob::result, this, &DavGroupwareResource::onCollectionRemovedFinished);
Laurent Montel's avatar
Laurent Montel committed
145
    job->start();
146
147
}

148
149
void DavGroupwareResource::cleanup()
{
Laurent Montel's avatar
Laurent Montel committed
150
    Settings::self()->cleanup();
151
    ResourceBase::cleanup();
152
153
}

154
QDateTime DavGroupwareResource::lastCacheUpdate() const
155
{
156
    return QDateTime::currentDateTime();
157
158
}

Laurent Montel's avatar
Laurent Montel committed
159
void DavGroupwareResource::canHandleFreeBusy(const QString &email) const
160
{
Laurent Montel's avatar
Laurent Montel committed
161
162
163
164
165
    if (!isOnline()) {
        handlesFreeBusy(email, false);
    } else {
        mFreeBusyHandler->canHandleFreeBusy(email);
    }
166
167
}

Laurent Montel's avatar
Laurent Montel committed
168
void DavGroupwareResource::onHandlesFreeBusy(const QString &email, bool handles)
169
{
Laurent Montel's avatar
Laurent Montel committed
170
    handlesFreeBusy(email, handles);
171
172
}

173
void DavGroupwareResource::retrieveFreeBusy(const QString &email, const QDateTime &start, const QDateTime &end)
174
{
Laurent Montel's avatar
Laurent Montel committed
175
176
177
178
179
    if (!isOnline()) {
        freeBusyRetrieved(email, QString(), false, i18n("Unable to retrieve free-busy info while offline"));
    } else {
        mFreeBusyHandler->retrieveFreeBusy(email, start, end);
    }
180
181
}

Laurent Montel's avatar
Laurent Montel committed
182
void DavGroupwareResource::onFreeBusyRetrieved(const QString &email, const QString &freeBusy, bool success, const QString &errorText)
183
{
Laurent Montel's avatar
Laurent Montel committed
184
    freeBusyRetrieved(email, freeBusy, success, errorText);
185
186
}

Laurent Montel's avatar
Laurent Montel committed
187
void DavGroupwareResource::configure(WId windowId)
188
{
Laurent Montel's avatar
Laurent Montel committed
189
190
191
192
193
194
195
    Settings::self()->setWinId(windowId);

    // On the initial configuration we start the setup wizard
    if (Settings::self()->configuredDavUrls().isEmpty()) {
        SetupWizard wizard;

        if (windowId) {
196
            wizard.setAttribute(Qt::WA_NativeWindow, true);
197
            KWindowSystem::setMainWindow(wizard.windowHandle(), windowId);
Laurent Montel's avatar
Laurent Montel committed
198
199
200
201
202
        }

        const int result = wizard.exec();
        if (result == QDialog::Accepted) {
            const SetupWizard::Url::List urls = wizard.urls();
Laurent Montel's avatar
Laurent Montel committed
203
            for (const SetupWizard::Url &url : urls) {
Laurent Montel's avatar
Laurent Montel committed
204
205
206
207
208
                Settings::UrlConfiguration *urlConfig = new Settings::UrlConfiguration();

                urlConfig->mUrl = url.url;
                urlConfig->mProtocol = url.protocol;
                urlConfig->mUser = url.userName;
Laurent Montel's avatar
Laurent Montel committed
209
                urlConfig->mPassword = wizard.field(QStringLiteral("credentialsPassword")).toString();
Laurent Montel's avatar
Laurent Montel committed
210
211
212
213
214
215
216
217

                Settings::self()->newUrlConfiguration(urlConfig);
            }

            if (!urls.isEmpty()) {
                Settings::self()->setDisplayName(wizard.displayName());
            }

Laurent Montel's avatar
Laurent Montel committed
218
            QString defaultUser = wizard.field(QStringLiteral("credentialsUserName")).toString();
Laurent Montel's avatar
Laurent Montel committed
219
220
            if (!defaultUser.isEmpty()) {
                Settings::self()->setDefaultUsername(defaultUser);
Laurent Montel's avatar
Laurent Montel committed
221
                Settings::self()->setDefaultPassword(wizard.field(QStringLiteral("credentialsPassword")).toString());
Laurent Montel's avatar
Laurent Montel committed
222
223
224
            }
        }
    }
225

Laurent Montel's avatar
Laurent Montel committed
226
227
    // continue with the normal config dialog
    ConfigDialog dialog;
228

Laurent Montel's avatar
Laurent Montel committed
229
    if (windowId) {
230
        dialog.setAttribute(Qt::WA_NativeWindow, true);
231
        KWindowSystem::setMainWindow(dialog.windowHandle(), windowId);
Laurent Montel's avatar
Laurent Montel committed
232
    }
233

Laurent Montel's avatar
Laurent Montel committed
234
235
236
    if (!Settings::self()->defaultUsername().isEmpty()) {
        dialog.setPassword(Settings::self()->defaultPassword());
    }
237

Laurent Montel's avatar
Laurent Montel committed
238
    const int result = dialog.exec();
239

Laurent Montel's avatar
Laurent Montel committed
240
241
242
243
    if (result == QDialog::Accepted) {
        Settings::self()->setSettingsVersion(3);
        Settings::self()->save();
        synchronize();
Laurent Montel's avatar
Laurent Montel committed
244
        Q_EMIT configurationDialogAccepted();
Laurent Montel's avatar
Laurent Montel committed
245
    } else {
Laurent Montel's avatar
Laurent Montel committed
246
        Q_EMIT configurationDialogRejected();
247
    }
Laurent Montel's avatar
Laurent Montel committed
248
}
249

250
KJob *DavGroupwareResource::createRetrieveCollectionsJob()
Laurent Montel's avatar
Laurent Montel committed
251
252
253
{
    qCDebug(DAVRESOURCE_LOG) << "Retrieving collections list";
    mSyncErrorNotified = false;
254

Laurent Montel's avatar
Laurent Montel committed
255
    if (!configurationIsValid()) {
256
        return nullptr;
Laurent Montel's avatar
Laurent Montel committed
257
    }
258

Laurent Montel's avatar
Laurent Montel committed
259
    Q_EMIT status(Running, i18n("Fetching collections"));
260

Sandro Knauß's avatar
Sandro Knauß committed
261
    KDAV::DavCollectionsMultiFetchJob *job = new KDAV::DavCollectionsMultiFetchJob(Settings::self()->configuredDavUrls());
Sandro Knauß's avatar
Sandro Knauß committed
262
263
    connect(job, &KDAV::DavCollectionsMultiFetchJob::result, this, &DavGroupwareResource::onRetrieveCollectionsFinished);
    connect(job, &KDAV::DavCollectionsMultiFetchJob::collectionDiscovered, this, &DavGroupwareResource::onCollectionDiscovered);
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    return job;
}

void DavGroupwareResource::initialRetrieveCollections()
{
    auto job = createRetrieveCollectionsJob();
    if (!job) {
        return;
    }
    job->setProperty("initialCacheSync", QVariant::fromValue(true));
    job->start();
}

void DavGroupwareResource::retrieveCollections()
{
    auto job = createRetrieveCollectionsJob();
    if (!job) {
        return;
    }
    job->setProperty("initialCacheSync", QVariant::fromValue(false));
Laurent Montel's avatar
Laurent Montel committed
284
    job->start();
285
286
}

Laurent Montel's avatar
Laurent Montel committed
287
void DavGroupwareResource::retrieveItems(const Akonadi::Collection &collection)
288
{
289
290
291
292
293
    if (!collection.isValid()) {
        itemsRetrievalDone();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
294
    qCDebug(DAVRESOURCE_LOG) << "Retrieving items for collection " << collection.remoteId();
295

Laurent Montel's avatar
Laurent Montel committed
296
297
298
    if (!configurationIsValid()) {
        return;
    }
299

Laurent Montel's avatar
Laurent Montel committed
300
301
302
303
304
305
306
    // As the resource root collection contains mime types for items we must
    // work around the fact that Akonadi will rightfully try to retrieve items
    // from it. So just return an empty list
    if (collection.remoteId() == identifier()) {
        itemsRetrievalDone();
        return;
    }
307

308
    if (!mEtagCaches.contains(collection.remoteId())) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
309
        qCDebug(DAVRESOURCE_LOG) << "Asked to retrieve items for a collection we don't have in the cache";
310
311
312
313
        itemsRetrievalDone();
        return;
    }

314
315
    // Only continue if the collection has changed or if
    // it's the first time we see it
316
    const CTagAttribute *CTagAttr = collection.attribute<CTagAttribute>();
317
318
319
320
321
322
    if (CTagAttr && mCTagCache.contains(collection.remoteId()) && mCTagCache.value(collection.remoteId()) == CTagAttr->CTag()) {
        qCDebug(DAVRESOURCE_LOG) << "CTag for collection" << collection.remoteId() << "didn't change: " << CTagAttr->CTag();
        itemsRetrievalDone();
        return;
    }

Sandro Knauß's avatar
Sandro Knauß committed
323
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(collection.remoteId());
324

Laurent Montel's avatar
Laurent Montel committed
325
    if (!davUrl.url().isValid()) {
Laurent Montel's avatar
Laurent Montel committed
326
        qCCritical(DAVRESOURCE_LOG) << "Can't find a configured URL, collection.remoteId() is " << collection.remoteId();
Laurent Montel's avatar
Laurent Montel committed
327
328
329
330
331
        cancelTask(i18n("Asked to retrieve items for an unknown collection: %1", collection.remoteId()));
        //Q_ASSERT_X( false, "DavGroupwareResource::retrieveItems", "Url is invalid" );
        return;
    }

Sandro Knauß's avatar
Sandro Knauß committed
332
    KDAV::DavItemsListJob *job = new KDAV::DavItemsListJob(davUrl, mEtagCaches.value(collection.remoteId()));
333
334
    if (Settings::self()->limitSyncRange()) {
        QDateTime start = Settings::self()->getSyncRangeStart();
335
        qCDebug(DAVRESOURCE_LOG) << "Start time for list job:" << start;
336
        if (start.isValid()) {
337
            job->setTimeRange(start.toString(QStringLiteral("yyyyMMddTHHMMssZ")), QString());
338
339
        }
    }
Laurent Montel's avatar
Laurent Montel committed
340
341
    job->setProperty("collection", QVariant::fromValue(collection));
    job->setContentMimeTypes(collection.contentMimeTypes());
Sandro Knauß's avatar
Sandro Knauß committed
342
    connect(job, &KDAV::DavItemsListJob::result, this, &DavGroupwareResource::onRetrieveItemsFinished);
Laurent Montel's avatar
Laurent Montel committed
343
    job->start();
344
345
}

Laurent Montel's avatar
Laurent Montel committed
346
bool DavGroupwareResource::retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &)
347
{
Laurent Montel's avatar
Laurent Montel committed
348
    qCDebug(DAVRESOURCE_LOG) << "Retrieving single item. Remote id = " << item.remoteId();
349

Laurent Montel's avatar
Laurent Montel committed
350
351
352
    if (!configurationIsValid()) {
        return false;
    }
353

Sandro Knauß's avatar
Sandro Knauß committed
354
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(item.parentCollection().remoteId(), item.remoteId());
Laurent Montel's avatar
Laurent Montel committed
355
    if (!davUrl.url().isValid()) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
356
        qCDebug(DAVRESOURCE_LOG) << "Failed to get a valid DavUrl. Parent collection remote ID is" << item.parentCollection().remoteId();
Laurent Montel's avatar
Laurent Montel committed
357
358
359
        cancelTask();
        return false;
    }
360

Sandro Knauß's avatar
Sandro Knauß committed
361
    KDAV::DavItem davItem;
Laurent Montel's avatar
Laurent Montel committed
362
    davItem.setContentType(QStringLiteral("text/calendar"));
Laurent Montel's avatar
Laurent Montel committed
363
    davItem.setEtag(item.remoteRevision());
364

Sandro Knauß's avatar
Sandro Knauß committed
365
    KDAV::DavItemFetchJob *job = new KDAV::DavItemFetchJob(davItem);
Laurent Montel's avatar
Laurent Montel committed
366
    job->setProperty("item", QVariant::fromValue(item));
Sandro Knauß's avatar
Sandro Knauß committed
367
    connect(job, &KDAV::DavItemFetchJob::result, this, &DavGroupwareResource::onRetrieveItemFinished);
Laurent Montel's avatar
Laurent Montel committed
368
    job->start();
369

Laurent Montel's avatar
Laurent Montel committed
370
    return true;
371
372
}

Laurent Montel's avatar
Laurent Montel committed
373
void DavGroupwareResource::itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection)
374
{
Laurent Montel's avatar
Laurent Montel committed
375
376
377
    qCDebug(DAVRESOURCE_LOG) << "Received notification for added item. Local id = "
                             << item.id() << ". Remote id = " << item.remoteId()
                             << ". Collection remote id = " << collection.remoteId();
378

Laurent Montel's avatar
Laurent Montel committed
379
380
    if (!configurationIsValid()) {
        return;
381
382
    }

Laurent Montel's avatar
Laurent Montel committed
383
    if (collection.remoteId().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
384
        qCCritical(DAVRESOURCE_LOG) << "Invalid remote id for collection " << collection.id() << " = " << collection.remoteId();
Laurent Montel's avatar
Laurent Montel committed
385
386
387
        cancelTask(i18n("Invalid collection for item %1.", item.id()));
        return;
    }
388

Sandro Knauß's avatar
Sandro Knauß committed
389
    KDAV::DavItem davItem = Utils::createDavItem(item, collection);
Laurent Montel's avatar
Laurent Montel committed
390
    if (davItem.data().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
391
        qCCritical(DAVRESOURCE_LOG) << "Item " << item.id() << " doesn't has a valid payload";
Laurent Montel's avatar
Laurent Montel committed
392
393
394
395
        cancelTask();
        return;
    }

Sandro Knauß's avatar
Sandro Knauß committed
396
397
398
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(collection.remoteId(), davItem.url().toDisplayString());
    qCDebug(DAVRESOURCE_LOG) << "Item " << item.id() << " will be put to " << davItem.url().toDisplayString();
    davItem.setUrl(davUrl);
Laurent Montel's avatar
Laurent Montel committed
399

Sandro Knauß's avatar
Sandro Knauß committed
400
    KDAV::DavItemCreateJob *job = new KDAV::DavItemCreateJob(davItem);
Laurent Montel's avatar
Laurent Montel committed
401
402
    job->setProperty("collection", QVariant::fromValue(collection));
    job->setProperty("item", QVariant::fromValue(item));
Sandro Knauß's avatar
Sandro Knauß committed
403
    connect(job, &KDAV::DavItemCreateJob::result, this, &DavGroupwareResource::onItemAddedFinished);
Laurent Montel's avatar
Laurent Montel committed
404
    job->start();
405
406
}

Laurent Montel's avatar
Laurent Montel committed
407
void DavGroupwareResource::itemChanged(const Akonadi::Item &item, const QSet<QByteArray> &)
408
{
Laurent Montel's avatar
Laurent Montel committed
409
410
    qCDebug(DAVRESOURCE_LOG) << "Received notification for changed item. Local id = " << item.id()
                             << ". Remote id = " << item.remoteId();
411

Laurent Montel's avatar
Laurent Montel committed
412
413
414
    if (!configurationIsValid()) {
        return;
    }
415

416
417
    const Akonadi::Collection collection = item.parentCollection();
    if (!mEtagCaches.contains(collection.remoteId())) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
418
        qCDebug(DAVRESOURCE_LOG) << "Changed item is in a collection we don't have in the cache";
419
420
421
422
423
        // TODO: display an error
        cancelTask();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
424
    QString ridBase = item.remoteId();
425
426
    if (ridBase.contains(QLatin1Char('#'))) {
        ridBase.truncate(ridBase.indexOf(QLatin1Char('#')));
427
    }
Laurent Montel's avatar
Laurent Montel committed
428

Sandro Knauß's avatar
Sandro Knauß committed
429
    auto cache = mEtagCaches.value(collection.remoteId());
Laurent Montel's avatar
Laurent Montel committed
430
    Akonadi::Item::List extraItems;
Laurent Montel's avatar
Laurent Montel committed
431
432
    const QStringList lstUrls = cache->urls();
    for (const QString &rid : lstUrls) {
Laurent Montel's avatar
Laurent Montel committed
433
434
435
436
437
        if (rid.startsWith(ridBase) && rid != item.remoteId()) {
            Akonadi::Item extraItem;
            extraItem.setRemoteId(rid);
            extraItems << extraItem;
        }
438
    }
Laurent Montel's avatar
Laurent Montel committed
439
440
441
442
443
444
445
446

    if (extraItems.isEmpty()) {
        doItemChange(item);
    } else {
        Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(extraItems);
        job->setCollection(item.parentCollection());
        job->fetchScope().fetchFullPayload();
        job->setProperty("item", QVariant::fromValue(item));
447
        connect(job, &Akonadi::ItemFetchJob::result, this, &DavGroupwareResource::onItemChangePrepared);
448
449
450
    }
}

Laurent Montel's avatar
Laurent Montel committed
451
void DavGroupwareResource::onItemChangePrepared(KJob *job)
452
{
Laurent Montel's avatar
Laurent Montel committed
453
454
455
456
    Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
    Akonadi::Item item = job->property("item").value<Akonadi::Item>();
    doItemChange(item, fetchJob->items());
}
457

Laurent Montel's avatar
Laurent Montel committed
458
459
void DavGroupwareResource::doItemChange(const Akonadi::Item &item, const Akonadi::Item::List &dependentItems)
{
Sandro Knauß's avatar
Sandro Knauß committed
460
    KDAV::DavItem davItem = Utils::createDavItem(item, item.parentCollection(), dependentItems);
Laurent Montel's avatar
Laurent Montel committed
461
    if (davItem.data().isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
462
        qCCritical(DAVRESOURCE_LOG) << "Item " << item.id() << " doesn't has a valid payload";
Laurent Montel's avatar
Laurent Montel committed
463
464
        cancelTask();
        return;
465
466
    }

Laurent Montel's avatar
Laurent Montel committed
467
    QString url = item.remoteId();
468
469
    if (url.contains(QLatin1Char('#'))) {
        url.truncate(url.indexOf(QLatin1Char('#')));
Laurent Montel's avatar
Laurent Montel committed
470
    }
Sandro Knauß's avatar
Sandro Knauß committed
471
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(item.parentCollection().remoteId(), url);
472

Laurent Montel's avatar
Laurent Montel committed
473
    // We have to re-set the URL as it's not necessarily valid after createDavItem()
Sandro Knauß's avatar
Sandro Knauß committed
474
    davItem.setUrl(davUrl);
Laurent Montel's avatar
Laurent Montel committed
475
    davItem.setEtag(item.remoteRevision());
476

Sandro Knauß's avatar
Sandro Knauß committed
477
    KDAV::DavItemModifyJob *modJob = new KDAV::DavItemModifyJob(davItem);
478
    modJob->setProperty("collection", QVariant::fromValue(item.parentCollection()));
Laurent Montel's avatar
Laurent Montel committed
479
480
    modJob->setProperty("item", QVariant::fromValue(item));
    modJob->setProperty("dependentItems", QVariant::fromValue(dependentItems));
Sandro Knauß's avatar
Sandro Knauß committed
481
    connect(modJob, &KDAV::DavItemModifyJob::result, this, &DavGroupwareResource::onItemChangedFinished);
482
483
484
    modJob->start();
}

Laurent Montel's avatar
Laurent Montel committed
485
486
void DavGroupwareResource::itemRemoved(const Akonadi::Item &item)
{
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
487
488
    qCDebug(DAVRESOURCE_LOG) << "Received notification for removed item. Remote id = " << item.remoteId();

Laurent Montel's avatar
Laurent Montel committed
489
490
491
492
    if (!configurationIsValid()) {
        return;
    }

493
494
    const Akonadi::Collection collection = item.parentCollection();
    if (!mEtagCaches.contains(collection.remoteId())) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
495
        qCDebug(DAVRESOURCE_LOG) << "Removed item is in a collection we don't have in the cache";
496
497
498
499
500
        // TODO: display an error
        cancelTask();
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
501
    QString ridBase = item.remoteId();
502
    if (ridBase.contains(QLatin1Char('#'))) {
Laurent Montel's avatar
Laurent Montel committed
503
504
        // A bit tricky: we must remove an incidence contained in a resource
        // containing multiple ones.
505
        ridBase.truncate(ridBase.indexOf(QLatin1Char('#')));
Laurent Montel's avatar
Laurent Montel committed
506

Sandro Knauß's avatar
Sandro Knauß committed
507
        auto cache = mEtagCaches.value(collection.remoteId());
Laurent Montel's avatar
Laurent Montel committed
508
        Akonadi::Item::List extraItems;
Laurent Montel's avatar
Laurent Montel committed
509
510
        const QStringList lstUrl = cache->urls();
        for (const QString &rid : lstUrl) {
Laurent Montel's avatar
Laurent Montel committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
            if (rid.startsWith(ridBase) && rid != item.remoteId()) {
                Akonadi::Item extraItem;
                extraItem.setRemoteId(rid);
                extraItems << extraItem;
            }
        }

        if (extraItems.isEmpty()) {
            // Urrrr?
            // Well, just delete the item.
            doItemRemoval(item);
        } else {
            Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(extraItems);
            job->setCollection(item.parentCollection());
            job->fetchScope().fetchFullPayload();
            job->setProperty("item", QVariant::fromValue(item));
527
            connect(job, &Akonadi::ItemFetchJob::result, this, &DavGroupwareResource::onItemRemovalPrepared);
Laurent Montel's avatar
Laurent Montel committed
528
529
530
531
532
533
534
535
        }
    } else {
        // easy as pie: just remove everything at the URL.
        doItemRemoval(item);
    }
}

void DavGroupwareResource::onItemRemovalPrepared(KJob *job)
536
{
Laurent Montel's avatar
Laurent Montel committed
537
538
    Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
    Akonadi::Item item = job->property("item").value<Akonadi::Item>();
Laurent Montel's avatar
Laurent Montel committed
539
    const Akonadi::Item::List keptItems = fetchJob->items();
Laurent Montel's avatar
Laurent Montel committed
540
541
542
543
544
545
546
547

    if (keptItems.isEmpty()) {
        // Urrrr? Not again!
        doItemRemoval(item);
    } else {
        Akonadi::Item mainItem;
        Akonadi::Item::List extraItems;
        QString ridBase = item.remoteId();
548
        ridBase.truncate(ridBase.indexOf(QLatin1Char('#')));
Laurent Montel's avatar
Laurent Montel committed
549

Laurent Montel's avatar
Laurent Montel committed
550
        for (const Akonadi::Item &kept : keptItems) {
Laurent Montel's avatar
Laurent Montel committed
551
552
553
554
555
556
557
558
559
560
561
            if (kept.remoteId() == ridBase && extraItems.isEmpty()) {
                mainItem = kept;
            } else {
                extraItems << kept;
            }
        }

        if (!mainItem.hasPayload()) {
            mainItem = extraItems.takeFirst();
        }

Sandro Knauß's avatar
Sandro Knauß committed
562
        const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(item.parentCollection().remoteId(), ridBase);
Laurent Montel's avatar
Laurent Montel committed
563

Sandro Knauß's avatar
Sandro Knauß committed
564
565
        KDAV::DavItem davItem = Utils::createDavItem(mainItem, mainItem.parentCollection(), extraItems);
        davItem.setUrl(davUrl);
Laurent Montel's avatar
Laurent Montel committed
566
567
        davItem.setEtag(item.remoteRevision());

Sandro Knauß's avatar
Sandro Knauß committed
568
        KDAV::DavItemModifyJob *modJob = new KDAV::DavItemModifyJob(davItem);
569
        modJob->setProperty("collection", QVariant::fromValue(mainItem.parentCollection()));
Laurent Montel's avatar
Laurent Montel committed
570
571
572
573
        modJob->setProperty("item", QVariant::fromValue(mainItem));
        modJob->setProperty("dependentItems", QVariant::fromValue(extraItems));
        modJob->setProperty("isRemoval", QVariant::fromValue(true));
        modJob->setProperty("removedItem", QVariant::fromValue(item));
Sandro Knauß's avatar
Sandro Knauß committed
574
        connect(modJob, &KDAV::DavItemModifyJob::result, this, &DavGroupwareResource::onItemChangedFinished);
Laurent Montel's avatar
Laurent Montel committed
575
576
577
        modJob->start();
    }
}
578

Laurent Montel's avatar
Laurent Montel committed
579
580
void DavGroupwareResource::doItemRemoval(const Akonadi::Item &item)
{
Sandro Knauß's avatar
Sandro Knauß committed
581
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(item.parentCollection().remoteId(), item.remoteId());
Tobias Koenig's avatar
Tobias Koenig committed
582

Sandro Knauß's avatar
Sandro Knauß committed
583
584
    KDAV::DavItem davItem;
    davItem.setUrl(davUrl);
Laurent Montel's avatar
Laurent Montel committed
585
586
    davItem.setEtag(item.remoteRevision());

Sandro Knauß's avatar
Sandro Knauß committed
587
    KDAV::DavItemDeleteJob *job = new KDAV::DavItemDeleteJob(davItem);
Laurent Montel's avatar
Laurent Montel committed
588
589
    job->setProperty("item", QVariant::fromValue(item));
    job->setProperty("collection", QVariant::fromValue(item.parentCollection()));
Sandro Knauß's avatar
Sandro Knauß committed
590
    connect(job, &KDAV::DavItemDeleteJob::result, this, &DavGroupwareResource::onItemRemovedFinished);
Laurent Montel's avatar
Laurent Montel committed
591
    job->start();
592
593
}

Laurent Montel's avatar
Laurent Montel committed
594
void DavGroupwareResource::doSetOnline(bool online)
595
{
Laurent Montel's avatar
Laurent Montel committed
596
    qCDebug(DAVRESOURCE_LOG) << "Resource changed online status to" << online;
597

Laurent Montel's avatar
Laurent Montel committed
598
599
600
    if (online) {
        synchronize();
    }
601

Laurent Montel's avatar
Laurent Montel committed
602
    ResourceBase::doSetOnline(online);
603
604
}

Grégory Oestreicher's avatar
Grégory Oestreicher committed
605
606
void DavGroupwareResource::createInitialCache()
{
Laurent Montel's avatar
Laurent Montel committed
607
608
609
    // Get all the items fetched by this resource
    Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob(mDavCollectionRoot, QStringList());
    job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
610
    connect(job, &Akonadi::RecursiveItemFetchJob::result, this, &DavGroupwareResource::onCreateInitialCacheReady);
Laurent Montel's avatar
Laurent Montel committed
611
    job->start();
Grégory Oestreicher's avatar
Grégory Oestreicher committed
612
613
}

Laurent Montel's avatar
Laurent Montel committed
614
void DavGroupwareResource::onCreateInitialCacheReady(KJob *job)
Grégory Oestreicher's avatar
Grégory Oestreicher committed
615
{
Laurent Montel's avatar
Laurent Montel committed
616
    Akonadi::RecursiveItemFetchJob *fetchJob = qobject_cast<Akonadi::RecursiveItemFetchJob *>(job);
Grégory Oestreicher's avatar
Grégory Oestreicher committed
617

Laurent Montel's avatar
Laurent Montel committed
618
619
    const Akonadi::Item::List itemsLst = fetchJob->items();
    for (const Akonadi::Item &item : itemsLst) {
Laurent Montel's avatar
Laurent Montel committed
620
621
        const QString rid = item.remoteId();
        if (rid.isEmpty()) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
622
            qCDebug(DAVRESOURCE_LOG) << "DavGroupwareResource::onCreateInitialCacheReady: Found an item without remote ID. " << item.id();
Laurent Montel's avatar
Laurent Montel committed
623
624
            continue;
        }
Grégory Oestreicher's avatar
Grégory Oestreicher committed
625

626
627
        const Akonadi::Collection collection = item.parentCollection();
        if (collection.remoteId().isEmpty()) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
628
629
            qCDebug(DAVRESOURCE_LOG) << "DavGroupwareResource::onCreateInitialCacheReady: Found an item in a collection without remote ID. "
                                     << item.remoteId();
Laurent Montel's avatar
Laurent Montel committed
630
631
            continue;
        }
Grégory Oestreicher's avatar
Grégory Oestreicher committed
632

Laurent Montel's avatar
Laurent Montel committed
633
634
        const QString etag = item.remoteRevision();
        if (etag.isEmpty()) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
635
            qCDebug(DAVRESOURCE_LOG) << "DavGroupwareResource::onCreateInitialCacheReady: Found an item without ETag. " << item.remoteId();
Laurent Montel's avatar
Laurent Montel committed
636
637
            continue;
        }
Grégory Oestreicher's avatar
Grégory Oestreicher committed
638

639
        if (!mEtagCaches.contains(collection.remoteId())) {
Sandro Knauß's avatar
Sandro Knauß committed
640
            auto cache = std::shared_ptr<KDAV::EtagCache>(new AkonadiEtagCache(collection));
641
            mEtagCaches.insert(collection.remoteId(), cache);
Laurent Montel's avatar
Laurent Montel committed
642
        }
Grégory Oestreicher's avatar
Grégory Oestreicher committed
643

644
        mEtagCaches[collection.remoteId()]->setEtag(rid, etag);
Laurent Montel's avatar
Laurent Montel committed
645
646
    }
    taskDone();
Grégory Oestreicher's avatar
Grégory Oestreicher committed
647
648
}

649
650
651
652
653
654
void DavGroupwareResource::onReloadConfig()
{
    Settings::self()->reloadConfig();
    synchronize();
}

Laurent Montel's avatar
Laurent Montel committed
655
void DavGroupwareResource::onCollectionRemovedFinished(KJob *job)
656
{
Laurent Montel's avatar
Laurent Montel committed
657
658
659
660
661
662
    if (job->error()) {
        cancelTask(i18n("Unable to remove collection: %1", job->errorText()));
        return;
    }

    Akonadi::Collection collection = job->property("collection").value<Akonadi::Collection>();
663
664
665
666

    if (mEtagCaches.contains(collection.remoteId())) {
        mEtagCaches[collection.remoteId()]->deleteLater();
        mEtagCaches.remove(collection.remoteId());
667
    }
668

Laurent Montel's avatar
Laurent Montel committed
669
    changeProcessed();
670
671
}

Laurent Montel's avatar
Laurent Montel committed
672
void DavGroupwareResource::onRetrieveCollectionsFinished(KJob *job)
673
{
Sandro Knauß's avatar
Sandro Knauß committed
674
    const KDAV::DavCollectionsMultiFetchJob *fetchJob = qobject_cast< KDAV::DavCollectionsMultiFetchJob *>(job);
Laurent Montel's avatar
Laurent Montel committed
675
676
677
678
679
680

    if (job->error()) {
        qCWarning(DAVRESOURCE_LOG) << "Unable to fetch collections" << job->error() << job->errorText();
        cancelTask(i18n("Unable to retrieve collections: %1", job->errorText()));
        mSyncErrorNotified = true;
        return;
681
    }
682

683
    bool initialCacheSync = job->property("initialCacheSync").toBool();
Laurent Montel's avatar
Laurent Montel committed
684
685
686
    Akonadi::Collection::List collections;
    collections << mDavCollectionRoot;
    QSet<QString> seenCollectionsUrls;
687

Sandro Knauß's avatar
Sandro Knauß committed
688
    const KDAV::DavCollection::List davCollections = fetchJob->collections();
689

Laurent Montel's avatar
Laurent Montel committed
690
    for (const KDAV::DavCollection &davCollection : davCollections) {
Sandro Knauß's avatar
Sandro Knauß committed
691
692
        if (seenCollectionsUrls.contains(davCollection.url().toDisplayString())) {
            qCDebug(DAVRESOURCE_LOG) << "DavGroupwareResource::onRetrieveCollectionsFinished: Duplicate collection reported. " << davCollection.url().toDisplayString();
Laurent Montel's avatar
Laurent Montel committed
693
694
            continue;
        } else {
Sandro Knauß's avatar
Sandro Knauß committed
695
            seenCollectionsUrls.insert(davCollection.url().toDisplayString());
Laurent Montel's avatar
Laurent Montel committed
696
        }
697

Laurent Montel's avatar
Laurent Montel committed
698
699
        Akonadi::Collection collection;
        collection.setParentCollection(mDavCollectionRoot);
Sandro Knauß's avatar
Sandro Knauß committed
700
        collection.setRemoteId(davCollection.url().toDisplayString());
Laurent Montel's avatar
Laurent Montel committed
701
        collection.setName(collection.remoteId());
702

703
704
705
706
707
        if (davCollection.color().isValid()) {
            CollectionColorAttribute *colorAttr = collection.attribute<CollectionColorAttribute>(Akonadi::Collection::AddIfMissing);
            colorAttr->setColor(davCollection.color());
        }

Laurent Montel's avatar
Laurent Montel committed
708
709
710
711
        if (!davCollection.displayName().isEmpty()) {
            EntityDisplayAttribute *attr = collection.attribute<EntityDisplayAttribute>(Collection::AddIfMissing);
            attr->setDisplayName(davCollection.displayName());
        }
712

Laurent Montel's avatar
Laurent Montel committed
713
714
        QStringList mimeTypes;
        mimeTypes << Collection::mimeType();
715

Sandro Knauß's avatar
Sandro Knauß committed
716
717
        const KDAV::DavCollection::ContentTypes contentTypes = davCollection.contentTypes();
        if (contentTypes & KDAV::DavCollection::Calendar) {
Laurent Montel's avatar
Laurent Montel committed
718
            mimeTypes << QStringLiteral("text/calendar");
Laurent Montel's avatar
Laurent Montel committed
719
        }
720

Sandro Knauß's avatar
Sandro Knauß committed
721
        if (contentTypes & KDAV::DavCollection::Events) {
722
            mimeTypes << KCalendarCore::Event::eventMimeType();
Laurent Montel's avatar
Laurent Montel committed
723
        }
724

Sandro Knauß's avatar
Sandro Knauß committed
725
        if (contentTypes & KDAV::DavCollection::Todos) {
726
            mimeTypes << KCalendarCore::Todo::todoMimeType();
Laurent Montel's avatar
Laurent Montel committed
727
        }
728

Sandro Knauß's avatar
Sandro Knauß committed
729
        if (contentTypes & KDAV::DavCollection::Contacts) {
Laurent Montel's avatar
Laurent Montel committed
730
731
            mimeTypes << KContacts::Addressee::mimeType();
        }
732

Sandro Knauß's avatar
Sandro Knauß committed
733
        if (contentTypes & KDAV::DavCollection::FreeBusy) {
734
            mimeTypes << KCalendarCore::FreeBusy::freeBusyMimeType();
Laurent Montel's avatar
Laurent Montel committed
735
        }
736

Sandro Knauß's avatar
Sandro Knauß committed
737
        if (contentTypes & KDAV::DavCollection::Journal) {
738
            mimeTypes << KCalendarCore::Journal::journalMimeType();
Laurent Montel's avatar
Laurent Montel committed
739
        }
740

Laurent Montel's avatar
Laurent Montel committed
741
742
        collection.setContentMimeTypes(mimeTypes);
        setCollectionIcon(collection /*by-ref*/);
743

Laurent Montel's avatar
Laurent Montel committed
744
        DavProtocolAttribute *protoAttr = collection.attribute<DavProtocolAttribute>(Collection::AddIfMissing);
Sandro Knauß's avatar
Sandro Knauß committed
745
        protoAttr->setDavProtocol(davCollection.url().protocol());
746

747
748
749
750
        /*
         * We unfortunately have to update the CTag now in the cache
         * as this information will not be available when retrieveItems()
         * is called. We leave it untouched in the collection attribute
Yuri Chornoivan's avatar
Yuri Chornoivan committed
751
         * and will only update it there after successful sync.
752
         */
Laurent Montel's avatar
Laurent Montel committed
753
        if (!davCollection.CTag().isEmpty()) {
Sandro Knauß's avatar
Sandro Knauß committed
754
            mCTagCache.insert(davCollection.url().toDisplayString(), davCollection.CTag());
Laurent Montel's avatar
Laurent Montel committed
755
        }
756

Sandro Knauß's avatar
Sandro Knauß committed
757
        KDAV::Privileges privileges = davCollection.privileges();
Laurent Montel's avatar
Laurent Montel committed
758
        Akonadi::Collection::Rights rights;
759

Sandro Knauß's avatar
Sandro Knauß committed
760
        if (privileges & KDAV::All || privileges & KDAV::Write) {
Laurent Montel's avatar
Laurent Montel committed
761
762
            rights |= Akonadi::Collection::AllRights;
        }
763

Sandro Knauß's avatar
Sandro Knauß committed
764
        if (privileges & KDAV::WriteContent) {
Laurent Montel's avatar
Laurent Montel committed
765
766
            rights |= Akonadi::Collection::CanChangeItem;
        }
767

Sandro Knauß's avatar
Sandro Knauß committed
768
        if (privileges & KDAV::Bind) {
Laurent Montel's avatar
Laurent Montel committed
769
770
            rights |= Akonadi::Collection::CanCreateItem;
        }
771

Sandro Knauß's avatar
Sandro Knauß committed
772
        if (privileges & KDAV::Unbind) {
Laurent Montel's avatar
Laurent Montel committed
773
774
775
            rights |= Akonadi::Collection::CanDeleteItem;
        }

Sandro Knauß's avatar
Sandro Knauß committed
776
        if (privileges == KDAV::Read) {
Laurent Montel's avatar
Laurent Montel committed
777
778
779
780
781
            rights |= Akonadi::Collection::ReadOnly;
        }

        collection.setRights(rights);
        collections << collection;
782
783

        if (!mEtagCaches.contains(collection.remoteId())) {
Sandro Knauß's avatar
Sandro Knauß committed
784
            auto cache = std::shared_ptr<KDAV::EtagCache>(new AkonadiEtagCache(collection));
785
786
            mEtagCaches.insert(collection.remoteId(), cache);
        }
787
    }
Laurent Montel's avatar
Laurent Montel committed
788

789
    foreach (const QString &rid, mEtagCaches.keys()) {
Laurent Montel's avatar
Laurent Montel committed
790
        if (!seenCollectionsUrls.contains(rid)) {
Grégory Oestreicher's avatar
Debug++    
Grégory Oestreicher committed
791
            qCDebug(DAVRESOURCE_LOG) << "DavGroupwareResource::onRetrieveCollectionsFinished: Collection disappeared. " << rid;
792
793
            mEtagCaches[rid]->deleteLater();
            mEtagCaches.remove(rid);
Laurent Montel's avatar
Laurent Montel committed
794
        }
795
    }
Laurent Montel's avatar
Laurent Montel committed
796

797
798
799
800
801
    if (!initialCacheSync) {
        collectionsRetrieved(collections);
    } else {
        taskDone();
    }
802
803
}

Laurent Montel's avatar
Laurent Montel committed
804
void DavGroupwareResource::onRetrieveItemsFinished(KJob *job)
805
{
Laurent Montel's avatar
Laurent Montel committed
806
807
808
809
810
811
812
813
    if (job->error()) {
        if (mSyncErrorNotified) {
            cancelTask();
        } else {
            cancelTask(i18n("Unable to retrieve items: %1", job->errorText()));
            mSyncErrorNotified = true;
        }
        return;
814
    }
815

816
    Collection collection = job->property("collection").value<Collection>();
Sandro Knauß's avatar
Sandro Knauß committed
817
    const KDAV::DavUrl davUrl = Settings::self()->davUrlFromCollectionUrl(collection.remoteId());
Volker Krause's avatar
Volker Krause committed
818
    const bool protocolSupportsMultiget = KDAV::ProtocolInfo::useMultiget(davUrl.protocol());
Laurent Montel's avatar
Laurent Montel committed
819

Sandro Knauß's avatar
Sandro Knauß committed
820
821
    const KDAV::DavItemsListJob *listJob = qobject_cast< KDAV::DavItemsListJob *>(job);
    auto cache = mEtagCaches.value(collection.remoteId());
822
823
824
825
826
    if (!cache) {
        qCDebug(DAVRESOURCE_LOG) << "Collection has disappeared during item fetch!";
        cancelTask();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
827
828
829
830

    Akonadi::Item::List changedItems;
    QSet<QString> seenRids;
    QStringList changedRids;
Laurent Montel's avatar
Laurent Montel committed
831
    changedItems.reserve(listJob->changedItems().count());
Sandro Knauß's avatar
Sandro Knauß committed
832
833
    foreach (const KDAV::DavItem &davItem, listJob->changedItems()) {
        seenRids.insert(davItem.url().toDisplayString());