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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "recursiveitemfetchjob.h"
0008 #include "collectionfetchjob.h"
0009 #include "collectionfetchscope.h"
0010 #include "itemfetchjob.h"
0011 #include "itemfetchscope.h"
0012 
0013 #include <QStringList>
0014 
0015 using namespace Akonadi;
0016 
0017 class Akonadi::RecursiveItemFetchJobPrivate
0018 {
0019 public:
0020     RecursiveItemFetchJobPrivate(const Collection &collection, const QStringList &mimeTypes, RecursiveItemFetchJob *parent)
0021         : mParent(parent)
0022         , mCollection(collection)
0023         , mMimeTypes(mimeTypes)
0024     {
0025     }
0026 
0027     void collectionFetchResult(KJob *job)
0028     {
0029         if (job->error()) {
0030             mParent->emitResult();
0031             return;
0032         }
0033 
0034         const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
0035 
0036         Collection::List collections = fetchJob->collections();
0037         collections.prepend(mCollection);
0038 
0039         for (const Collection &collection : std::as_const(collections)) {
0040             auto itemFetchJob = new ItemFetchJob(collection, mParent);
0041             itemFetchJob->setFetchScope(mFetchScope);
0042             mParent->connect(itemFetchJob, &KJob::result, mParent, [this](KJob *job) {
0043                 itemFetchResult(job);
0044             });
0045 
0046             mFetchCount++;
0047         }
0048     }
0049 
0050     void itemFetchResult(KJob *job)
0051     {
0052         if (!job->error()) {
0053             const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
0054 
0055             if (!mMimeTypes.isEmpty()) {
0056                 const Akonadi::Item::List lstItems = fetchJob->items();
0057                 for (const Item &item : lstItems) {
0058                     if (mMimeTypes.contains(item.mimeType())) {
0059                         mItems << item;
0060                     }
0061                 }
0062             } else {
0063                 mItems << fetchJob->items();
0064             }
0065         }
0066 
0067         mFetchCount--;
0068 
0069         if (mFetchCount == 0) {
0070             mParent->emitResult();
0071         }
0072     }
0073 
0074     RecursiveItemFetchJob *const mParent;
0075     const Collection mCollection;
0076     Item::List mItems;
0077     ItemFetchScope mFetchScope;
0078     const QStringList mMimeTypes;
0079 
0080     int mFetchCount = 0;
0081 };
0082 
0083 RecursiveItemFetchJob::RecursiveItemFetchJob(const Collection &collection, const QStringList &mimeTypes, QObject *parent)
0084     : KJob(parent)
0085     , d(new RecursiveItemFetchJobPrivate(collection, mimeTypes, this))
0086 {
0087 }
0088 
0089 RecursiveItemFetchJob::~RecursiveItemFetchJob() = default;
0090 
0091 void RecursiveItemFetchJob::setFetchScope(const ItemFetchScope &fetchScope)
0092 {
0093     d->mFetchScope = fetchScope;
0094 }
0095 
0096 ItemFetchScope &RecursiveItemFetchJob::fetchScope()
0097 {
0098     return d->mFetchScope;
0099 }
0100 
0101 void RecursiveItemFetchJob::start()
0102 {
0103     auto job = new CollectionFetchJob(d->mCollection, CollectionFetchJob::Recursive, this);
0104 
0105     if (!d->mMimeTypes.isEmpty()) {
0106         job->fetchScope().setContentMimeTypes(d->mMimeTypes);
0107     }
0108 
0109     connect(job, &CollectionFetchJob::result, this, [this](KJob *job) {
0110         d->collectionFetchResult(job);
0111     });
0112 }
0113 
0114 Akonadi::Item::List RecursiveItemFetchJob::items() const
0115 {
0116     return d->mItems;
0117 }
0118 
0119 #include "moc_recursiveitemfetchjob.cpp"