File indexing completed on 2024-05-12 05:25:59

0001 /*
0002  *   Copyright (C) 2018 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *   Copyright (C) 2018 RĂ©mi Nicole <minijackson@riseup.net>
0004  *
0005  *   This program is free software; you can redistribute it and/or modify
0006  *   it under the terms of the GNU General Public License as published by
0007  *   the Free Software Foundation; either version 2 of the License, or
0008  *   (at your option) any later version.
0009  *
0010  *   This program is distributed in the hope that it will be useful,
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *   GNU General Public License for more details.
0014  *
0015  *   You should have received a copy of the GNU General Public License
0016  *   along with this program; if not, write to the
0017  *   Free Software Foundation, Inc.,
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0019  */
0020 
0021 #include "eventpreprocessor.h"
0022 
0023 #include <KCalendarCore/ICalFormat>
0024 #include <QDateTime>
0025 
0026 void EventPropertyExtractor::updatedIndexedProperties(Event &event, const QByteArray &rawIcal)
0027 {
0028     auto icalEvent = KCalendarCore::ICalFormat().readIncidence(rawIcal).dynamicCast<KCalendarCore::Event>();
0029     if(!icalEvent) {
0030         SinkWarning() << "Invalid ICal to process, ignoring: " << rawIcal;
0031         return;
0032     }
0033     SinkTrace() << "Extracting properties for event:" << icalEvent->summary();
0034 
0035     event.setExtractedUid(icalEvent->uid());
0036     event.setExtractedSummary(icalEvent->summary());
0037     event.setExtractedDescription(icalEvent->description());
0038     event.setExtractedStartTime(icalEvent->dtStart());
0039     event.setExtractedEndTime(icalEvent->dtEnd());
0040     event.setExtractedAllDay(icalEvent->allDay());
0041     event.setExtractedRecurring(icalEvent->recurs());
0042 
0043     if (icalEvent->recurs() && icalEvent->recurrence()) {
0044         QList<QPair<QDateTime, QDateTime>> ranges;
0045         const auto duration = icalEvent->hasDuration() ? icalEvent->duration().asSeconds() : 0;
0046         const auto occurrences = icalEvent->recurrence()->timesInInterval(icalEvent->dtStart(), icalEvent->dtStart().addYears(10));
0047         for (const auto &start : occurrences) {
0048             ranges.append(qMakePair(start, start.addSecs(duration)));
0049         }
0050         if (!ranges.isEmpty()) {
0051             event.setExtractedEndTime(ranges.last().second);
0052             event.setProperty("indexRanges", QVariant::fromValue(ranges));
0053         }
0054     }
0055     if (icalEvent->hasRecurrenceId()) {
0056         const auto duration = icalEvent->hasDuration() ? icalEvent->duration().asSeconds() : 0;
0057         QList<QPair<QDateTime, QDateTime>> ranges;
0058 
0059         const auto start = icalEvent->dtStart();
0060         ranges.append(qMakePair(start, start.addSecs(duration)));
0061 
0062         const auto recurrenceId = icalEvent->recurrenceId();
0063         ranges.append(qMakePair(recurrenceId, recurrenceId.addSecs(duration)));
0064 
0065         //recurrenceId can be earlier or later and we need to cover both cases
0066         std::sort(ranges.begin(), ranges.end());
0067         event.setExtractedStartTime(ranges.first().first);
0068         event.setExtractedEndTime(ranges.last().second);
0069 
0070         event.setProperty("indexRanges", QVariant::fromValue(ranges));
0071     }
0072 }
0073 
0074 void EventPropertyExtractor::newEntity(Event &event)
0075 {
0076     updatedIndexedProperties(event, event.getIcal());
0077 }
0078 
0079 void EventPropertyExtractor::modifiedEntity(const Event &oldEvent, Event &newEvent)
0080 {
0081     updatedIndexedProperties(newEvent, newEvent.getIcal());
0082 }