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

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "pimeventsplugin.h"
0009 #include "akonadipimdatasource.h"
0010 #include "eventdatavisitor.h"
0011 #include "pimeventsplugin_debug.h"
0012 
0013 PimEventsPlugin::PimEventsPlugin(QObject *parent)
0014     : PimEventsPlugin(new AkonadiPimDataSource(), parent)
0015 {
0016     static_cast<AkonadiPimDataSource *>(mDataSource)->setParent(this);
0017 }
0018 
0019 PimEventsPlugin::PimEventsPlugin(PimDataSource *dataSource, QObject *parent)
0020     : CalendarEvents::CalendarEventsPlugin(parent)
0021     , mDataSource(dataSource)
0022 {
0023     qCDebug(PIMEVENTSPLUGIN_LOG) << "PIM Events Plugin activated";
0024 
0025     dataSource->calendar()->registerObserver(this);
0026 }
0027 
0028 PimEventsPlugin::~PimEventsPlugin()
0029 {
0030     qCDebug(PIMEVENTSPLUGIN_LOG) << "PIM Events Plugin deactivated";
0031 }
0032 
0033 void PimEventsPlugin::loadEventsForDateRange(const QDate &startDate, const QDate &endDate)
0034 {
0035     mStart = startDate;
0036     mEnd = endDate;
0037 
0038     int eventsCount = 0;
0039     int eventDataCount = 0;
0040     {
0041         EventDataVisitor visitor(mDataSource, startDate, endDate);
0042         const KCalendarCore::Event::List events = mDataSource->calendar()->events(startDate, endDate);
0043         eventsCount = events.count();
0044         if (visitor.act(events)) {
0045             eventDataCount = visitor.results().count();
0046             Q_EMIT dataReady(visitor.results());
0047         }
0048     }
0049 
0050     int todosCount = 0;
0051     int todoDataCount = 0;
0052     {
0053         EventDataVisitor visitor(mDataSource, startDate, endDate);
0054         const KCalendarCore::Todo::List todos = mDataSource->calendar()->todos(startDate, endDate);
0055         todosCount = todos.count();
0056         if (visitor.act(todos)) {
0057             todoDataCount = visitor.results().count();
0058             Q_EMIT dataReady(visitor.results());
0059         }
0060     }
0061     qCDebug(PIMEVENTSPLUGIN_LOG) << "Range:" << startDate.toString(Qt::ISODate) << "-" << endDate.toString(Qt::ISODate) << "Events:" << eventsCount
0062                                  << "EventData:" << eventDataCount << "Todos:" << todosCount << "TodoData:" << todoDataCount;
0063 }
0064 
0065 void PimEventsPlugin::calendarIncidenceAdded(const KCalendarCore::Incidence::Ptr &incidence)
0066 {
0067     if (!mStart.isValid() || !mEnd.isValid()) {
0068         // Don't bother with changes that happen before the applet starts populating data
0069         return;
0070     }
0071     EventDataVisitor visitor(mDataSource, mStart, mEnd);
0072     if (visitor.act(incidence)) {
0073         Q_EMIT dataReady(visitor.results());
0074     }
0075 }
0076 
0077 void PimEventsPlugin::calendarIncidenceChanged(const KCalendarCore::Incidence::Ptr &incidence)
0078 {
0079     if (!mStart.isValid() || !mEnd.isValid()) {
0080         return;
0081     }
0082     EventDataVisitor visitor(mDataSource, mStart, mEnd);
0083     if (visitor.act(incidence)) {
0084         for (const auto &ed : visitor.results()) {
0085             Q_EMIT eventModified(ed);
0086         }
0087     }
0088 }
0089 
0090 void PimEventsPlugin::calendarIncidenceAboutToBeDeleted(const KCalendarCore::Incidence::Ptr &incidence)
0091 {
0092     if (!mStart.isValid() || !mEnd.isValid()) {
0093         return;
0094     }
0095     EventDataIdVisitor visitor(mDataSource, mStart, mEnd);
0096     if (visitor.act(incidence)) {
0097         for (const QString &uid : visitor.results()) {
0098             Q_EMIT eventRemoved(uid);
0099         }
0100     }
0101 }
0102 
0103 #include "moc_pimeventsplugin.cpp"