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