Changeset View
Changeset View
Standalone View
Standalone View
src/common/davcollectionsmultifetchjob.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org> | 2 | SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org> | ||
3 | 3 | | |||
4 | SPDX-License-Identifier: LGPL-2.0-or-later | 4 | SPDX-License-Identifier: LGPL-2.0-or-later | ||
5 | */ | 5 | */ | ||
6 | 6 | | |||
7 | #include "davcollectionsmultifetchjob.h" | 7 | #include "davcollectionsmultifetchjob.h" | ||
8 | 8 | | |||
9 | #include "davcollectionsfetchjob.h" | 9 | #include "davcollectionsfetchjob.h" | ||
10 | 10 | | |||
11 | using namespace KDAV; | 11 | using namespace KDAV; | ||
12 | 12 | | |||
13 | namespace KDAV { | 13 | namespace KDAV { | ||
14 | class DavCollectionsMultiFetchJobPrivate | 14 | class DavCollectionsMultiFetchJobPrivate | ||
15 | { | 15 | { | ||
16 | public: | 16 | public: | ||
17 | DavUrl::List mUrls; | | |||
18 | DavCollection::List mCollections; | 17 | DavCollection::List mCollections; | ||
19 | int mSubJobCount = -1; | | |||
20 | }; | 18 | }; | ||
21 | } | 19 | } | ||
22 | 20 | | |||
23 | DavCollectionsMultiFetchJob::DavCollectionsMultiFetchJob(const DavUrl::List &urls, QObject *parent) | 21 | DavCollectionsMultiFetchJob::DavCollectionsMultiFetchJob(const DavUrl::List &urls, QObject *parent) | ||
24 | : KJob(parent) | 22 | : KCompositeJob(parent) | ||
25 | , d(new DavCollectionsMultiFetchJobPrivate) | 23 | , d(new DavCollectionsMultiFetchJobPrivate) | ||
26 | { | 24 | { | ||
27 | d->mUrls = urls; | 25 | for (const DavUrl &url : qAsConst(urls)) { | ||
28 | d->mSubJobCount = urls.size(); | 26 | DavCollectionsFetchJob *job = new DavCollectionsFetchJob(url, this); | ||
27 | connect(job, &DavCollectionsFetchJob::collectionDiscovered, this, &DavCollectionsMultiFetchJob::collectionDiscovered); | ||||
28 | addSubjob(job); | ||||
29 | } | ||||
29 | } | 30 | } | ||
30 | 31 | | |||
31 | DavCollectionsMultiFetchJob::~DavCollectionsMultiFetchJob() = default; | 32 | DavCollectionsMultiFetchJob::~DavCollectionsMultiFetchJob() = default; | ||
32 | 33 | | |||
33 | void DavCollectionsMultiFetchJob::start() | 34 | void DavCollectionsMultiFetchJob::start() | ||
34 | { | 35 | { | ||
35 | if (d->mUrls.isEmpty()) { | 36 | if (!hasSubjobs()) { | ||
36 | emitResult(); | 37 | emitResult(); | ||
37 | } | 38 | } else { | ||
38 | 39 | for (KJob *job : subjobs()) { | |||
39 | for (const DavUrl &url : qAsConst(d->mUrls)) { | | |||
40 | DavCollectionsFetchJob *job = new DavCollectionsFetchJob(url, this); | | |||
41 | connect(job, &DavCollectionsFetchJob::result, this, &DavCollectionsMultiFetchJob::davJobFinished); | | |||
42 | connect(job, &DavCollectionsFetchJob::collectionDiscovered, this, &DavCollectionsMultiFetchJob::collectionDiscovered); | | |||
43 | job->start(); | 40 | job->start(); | ||
44 | } | 41 | } | ||
45 | } | 42 | } | ||
43 | } | ||||
46 | 44 | | |||
47 | DavCollection::List DavCollectionsMultiFetchJob::collections() const | 45 | DavCollection::List DavCollectionsMultiFetchJob::collections() const | ||
48 | { | 46 | { | ||
49 | return d->mCollections; | 47 | return d->mCollections; | ||
50 | } | 48 | } | ||
51 | 49 | | |||
52 | void DavCollectionsMultiFetchJob::davJobFinished(KJob *job) | 50 | void DavCollectionsMultiFetchJob::slotResult(KJob *job) | ||
53 | { | 51 | { | ||
54 | DavCollectionsFetchJob *fetchJob = qobject_cast<DavCollectionsFetchJob *>(job); | 52 | // If we use KCompositeJob::slotResult(job) we end up with behaviour that's very | ||
53 | // hard to unittest: the valid URLs might or might not get processed. | ||||
54 | // Let's wait until all subjobs are done before emitting result. | ||||
55 | 55 | | |||
56 | if (job->error()) { | 56 | if (job->error() && !error()) { | ||
57 | // Store error only if first error | ||||
57 | setError(job->error()); | 58 | setError(job->error()); | ||
58 | setErrorText(job->errorText()); | 59 | setErrorText(job->errorText()); | ||
59 | } else { | 60 | } | ||
61 | if (!job->error()) { | ||||
62 | DavCollectionsFetchJob *fetchJob = qobject_cast<DavCollectionsFetchJob *>(job); | ||||
60 | d->mCollections << fetchJob->collections(); | 63 | d->mCollections << fetchJob->collections(); | ||
61 | } | 64 | } | ||
62 | 65 | removeSubjob(job); | |||
63 | if (--d->mSubJobCount == 0) { | 66 | if (!hasSubjobs()) { | ||
64 | emitResult(); | 67 | emitResult(); | ||
65 | } | 68 | } | ||
66 | } | 69 | } |