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

0001 /*
0002    SPDX-FileCopyrightText: 2011 Sérgio Martins <sergio.martins@kdab.com>
0003    SPDX-FileCopyrightText: 2012 Sérgio Martins <iamsergio@gmail.com>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "fetchjobcalendar.h"
0009 #include "akonadicalendar_debug.h"
0010 #include "fetchjobcalendar_p.h"
0011 #include "incidencefetchjob_p.h"
0012 #include <Akonadi/Collection>
0013 
0014 using namespace Akonadi;
0015 using namespace KCalendarCore;
0016 
0017 FetchJobCalendarPrivate::FetchJobCalendarPrivate(FetchJobCalendar *qq)
0018     : CalendarBasePrivate(qq)
0019     , q(qq)
0020 {
0021     auto job = new IncidenceFetchJob();
0022     connect(job, &KJob::result, this, &FetchJobCalendarPrivate::slotSearchJobFinished);
0023     connect(this, &CalendarBasePrivate::fetchFinished, this, &FetchJobCalendarPrivate::slotFetchJobFinished);
0024 }
0025 
0026 FetchJobCalendarPrivate::~FetchJobCalendarPrivate() = default;
0027 
0028 void FetchJobCalendarPrivate::slotSearchJobFinished(KJob *job)
0029 {
0030     auto searchJob = static_cast<Akonadi::IncidenceFetchJob *>(job);
0031     m_success = true;
0032     m_errorMessage = QString();
0033     if (searchJob->error()) {
0034         m_success = false;
0035         m_errorMessage = searchJob->errorText();
0036         qCWarning(AKONADICALENDAR_LOG) << "Unable to fetch incidences:" << searchJob->errorText();
0037     } else {
0038         const Akonadi::Item::List lstItem = searchJob->items();
0039         for (const Akonadi::Item &item : lstItem) {
0040             if (!item.isValid() || !item.hasPayload<KCalendarCore::Incidence::Ptr>()) {
0041                 m_success = false;
0042                 m_errorMessage = QStringLiteral("Invalid item or payload: %1").arg(item.id());
0043                 qCWarning(AKONADICALENDAR_LOG) << "Unable to fetch incidences:" << m_errorMessage;
0044                 continue;
0045             }
0046             internalInsert(item);
0047         }
0048     }
0049 
0050     if (mCollectionJobs.isEmpty()) {
0051         slotFetchJobFinished();
0052     }
0053 }
0054 
0055 void FetchJobCalendarPrivate::slotFetchJobFinished()
0056 {
0057     q->setIsLoading(false);
0058     // Q_EMIT loadFinished() in a delayed manner, due to freezes because of execs.
0059     QMetaObject::invokeMethod(q, "loadFinished", Qt::QueuedConnection, Q_ARG(bool, m_success), Q_ARG(QString, m_errorMessage));
0060 }
0061 
0062 FetchJobCalendar::FetchJobCalendar(QObject *parent)
0063     : CalendarBase(new FetchJobCalendarPrivate(this), parent)
0064 {
0065     setIsLoading(true);
0066 }
0067 
0068 FetchJobCalendar::~FetchJobCalendar() = default;
0069 
0070 #include "moc_fetchjobcalendar.cpp"
0071 #include "moc_fetchjobcalendar_p.cpp"