File indexing completed on 2024-05-12 05:10:46

0001 /*
0002   SPDX-FileCopyrightText: 2011 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003   SPDX-FileContributor: Volker Krause <vkrause@kde.org>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "incidencefetchjob_p.h"
0009 #include <Akonadi/CollectionFetchJob>
0010 #include <Akonadi/CollectionFetchScope>
0011 #include <Akonadi/ItemFetchJob>
0012 #include <Akonadi/ItemFetchScope>
0013 #include <KCalendarCore/Event>
0014 #include <KCalendarCore/Journal>
0015 #include <KCalendarCore/Todo>
0016 
0017 using namespace Akonadi;
0018 
0019 IncidenceFetchJob::IncidenceFetchJob(QObject *parent)
0020     : Job(parent)
0021 {
0022     m_mimeTypeChecker.addWantedMimeType(QStringLiteral("text/calendar"));
0023 }
0024 
0025 Item::List IncidenceFetchJob::items() const
0026 {
0027     return m_items;
0028 }
0029 
0030 void IncidenceFetchJob::doStart()
0031 {
0032     auto job = new CollectionFetchJob(Collection::root(), CollectionFetchJob::Recursive, this);
0033     job->fetchScope().setContentMimeTypes(QStringList() << QStringLiteral("text/calendar") << KCalendarCore::Event::eventMimeType()
0034                                                         << KCalendarCore::Todo::todoMimeType() << KCalendarCore::Journal::journalMimeType());
0035     connect(job, &CollectionFetchJob::result, this, &IncidenceFetchJob::collectionFetchResult);
0036 }
0037 
0038 void IncidenceFetchJob::collectionFetchResult(KJob *job)
0039 {
0040     if (job->error()) { // handled in base class
0041         return;
0042     }
0043     auto fetch = qobject_cast<CollectionFetchJob *>(job);
0044     Q_ASSERT(fetch);
0045 
0046     if (fetch->collections().isEmpty()) {
0047         emitResult();
0048         return;
0049     }
0050 
0051     const auto collections = fetch->collections();
0052     for (const Collection &col : collections) {
0053         if (!m_mimeTypeChecker.isWantedCollection(col) || col.isVirtual()) {
0054             continue;
0055         }
0056         auto itemFetch = new ItemFetchJob(col, this);
0057         itemFetch->fetchScope().fetchFullPayload(true);
0058         connect(itemFetch, &ItemFetchJob::result, this, &IncidenceFetchJob::itemFetchResult);
0059         ++m_jobCount;
0060     }
0061 }
0062 
0063 void IncidenceFetchJob::itemFetchResult(KJob *job)
0064 {
0065     if (job->error()) { // handled in base class
0066         return;
0067     }
0068     --m_jobCount;
0069     auto fetch = qobject_cast<ItemFetchJob *>(job);
0070     const auto items = fetch->items();
0071     for (const Item &item : items) {
0072         if (!m_mimeTypeChecker.isWantedItem(item)) {
0073             continue;
0074         }
0075         m_items.push_back(item);
0076     }
0077 
0078     if (m_jobCount <= 0) {
0079         emitResult();
0080     }
0081 }
0082 
0083 #include "moc_incidencefetchjob_p.cpp"