File indexing completed on 2025-01-19 04:46:48

0001 /* SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0002    SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0003 
0004    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "memorycalendarmemento.h"
0007 #include "text_calendar_debug.h"
0008 #include <Akonadi/ETMCalendar>
0009 #include <Akonadi/FetchJobCalendar>
0010 #include <CalendarSupport/CalendarSingleton>
0011 
0012 using namespace MessageViewer;
0013 using namespace Akonadi;
0014 
0015 MemoryCalendarMemento::MemoryCalendarMemento()
0016     : QObject(nullptr)
0017 {
0018     Akonadi::ETMCalendar::Ptr etmCalendar = CalendarSupport::calendarSingleton(/*createIfNull=*/false);
0019     if (etmCalendar && !etmCalendar->isLoading()) {
0020         // Good, either korganizer or kontact summary view are already running, so reuse ETM to save memory
0021         mCalendar = etmCalendar;
0022         QMetaObject::invokeMethod(this, "finalize", Qt::QueuedConnection);
0023     } else {
0024         FetchJobCalendar::Ptr calendar = FetchJobCalendar::Ptr(new FetchJobCalendar(this));
0025         mCalendar = calendar;
0026         connect(calendar.data(), &FetchJobCalendar::loadFinished, this, &MemoryCalendarMemento::slotCalendarLoaded);
0027     }
0028 }
0029 
0030 void MemoryCalendarMemento::slotCalendarLoaded(bool success, const QString &errorMessage)
0031 {
0032     qCDebug(TEXT_CALENDAR_LOG) << "MemoryCalendarMemento::slotCalendarLoaded: " << success;
0033     if (!success) {
0034         qCWarning(TEXT_CALENDAR_LOG) << "Unable to fetch incidences:" << errorMessage;
0035     }
0036 
0037     finalize();
0038 }
0039 
0040 void MemoryCalendarMemento::finalize()
0041 {
0042     mFinished = true;
0043     Q_EMIT update(MimeTreeParser::Delayed);
0044 }
0045 
0046 bool MemoryCalendarMemento::finished() const
0047 {
0048     return mFinished;
0049 }
0050 
0051 Akonadi::CalendarBase::Ptr MemoryCalendarMemento::calendar() const
0052 {
0053     Q_ASSERT(mFinished);
0054     return mCalendar;
0055 }
0056 
0057 void MemoryCalendarMemento::detach()
0058 {
0059     disconnect(this, &MemoryCalendarMemento::update, nullptr, nullptr);
0060 }
0061 
0062 #include "moc_memorycalendarmemento.cpp"