File indexing completed on 2024-11-10 04:40:32

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "subscriptionjob_p.h"
0008 
0009 #include "job_p.h"
0010 
0011 #include "collectionmodifyjob.h"
0012 
0013 using namespace Akonadi;
0014 
0015 class Akonadi::SubscriptionJobPrivate : public JobPrivate
0016 {
0017 public:
0018     explicit SubscriptionJobPrivate(SubscriptionJob *parent)
0019         : JobPrivate(parent)
0020     {
0021     }
0022 
0023     Q_DECLARE_PUBLIC(SubscriptionJob)
0024 
0025     Collection::List mSub, mUnsub;
0026 };
0027 
0028 SubscriptionJob::SubscriptionJob(QObject *parent)
0029     : Job(new SubscriptionJobPrivate(this), parent)
0030 {
0031 }
0032 
0033 SubscriptionJob::~SubscriptionJob()
0034 {
0035 }
0036 
0037 void SubscriptionJob::subscribe(const Collection::List &list)
0038 {
0039     Q_D(SubscriptionJob);
0040 
0041     d->mSub = list;
0042 }
0043 
0044 void SubscriptionJob::unsubscribe(const Collection::List &list)
0045 {
0046     Q_D(SubscriptionJob);
0047 
0048     d->mUnsub = list;
0049 }
0050 
0051 void SubscriptionJob::doStart()
0052 {
0053     Q_D(SubscriptionJob);
0054 
0055     if (d->mSub.isEmpty() && d->mUnsub.isEmpty()) {
0056         emitResult();
0057         return;
0058     }
0059 
0060     for (Collection col : std::as_const(d->mSub)) {
0061         col.setEnabled(true);
0062         new CollectionModifyJob(col, this);
0063     }
0064     for (Collection col : std::as_const(d->mUnsub)) {
0065         col.setEnabled(false);
0066         new CollectionModifyJob(col, this);
0067     }
0068 }
0069 
0070 void SubscriptionJob::slotResult(KJob *job)
0071 {
0072     if (job->error()) {
0073         setError(job->error());
0074         setErrorText(job->errorText());
0075         const auto subJobs = this->subjobs();
0076         for (KJob *subjob : subJobs) {
0077             removeSubjob(subjob);
0078         }
0079         emitResult();
0080     } else {
0081         Job::slotResult(job);
0082 
0083         if (!hasSubjobs()) {
0084             emitResult();
0085         }
0086     }
0087 }
0088 
0089 #include "moc_subscriptionjob_p.cpp"