File indexing completed on 2024-05-12 05:26:07

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 "todopreprocessor.h"
0022 
0023 #include <KCalendarCore/ICalFormat>
0024 
0025 static QString statusString(const KCalendarCore::Todo &incidence)
0026 {
0027     switch(incidence.status()) {
0028         case KCalendarCore::Incidence::StatusCompleted:
0029             return "COMPLETED";
0030         case KCalendarCore::Incidence::StatusNeedsAction:
0031             return "NEEDSACTION";
0032         case KCalendarCore::Incidence::StatusCanceled:
0033             return "CANCELED";
0034         case KCalendarCore::Incidence::StatusInProcess:
0035             return "INPROCESS";
0036         default:
0037             break;
0038     }
0039     return incidence.customStatus();
0040 }
0041 
0042 void TodoPropertyExtractor::updatedIndexedProperties(Todo &todo, const QByteArray &rawIcal)
0043 {
0044     auto incidence = KCalendarCore::ICalFormat().readIncidence(rawIcal);
0045 
0046     if(!incidence) {
0047         SinkWarning() << "Invalid ICal to process, ignoring...";
0048         return;
0049     }
0050 
0051     if(incidence->type() != KCalendarCore::IncidenceBase::IncidenceType::TypeTodo) {
0052         SinkWarning() << "ICal to process is not of type `Todo`, ignoring...";
0053         return;
0054     }
0055 
0056     auto icalTodo = dynamic_cast<const KCalendarCore::Todo *>(incidence.data());
0057     // Should be guaranteed by the incidence->type() condition above.
0058     Q_ASSERT(icalTodo);
0059 
0060     SinkTrace() << "Extracting properties for todo:" << icalTodo->summary();
0061 
0062     todo.setExtractedUid(icalTodo->uid());
0063     todo.setExtractedSummary(icalTodo->summary());
0064     todo.setExtractedDescription(icalTodo->description());
0065 
0066     // Sets invalid QDateTime if not defined
0067     todo.setExtractedCompletedDate(icalTodo->completed());
0068     todo.setExtractedDueDate(icalTodo->dtDue());
0069     todo.setExtractedStartDate(icalTodo->dtStart());
0070 
0071     todo.setExtractedStatus(statusString(*icalTodo));
0072     todo.setExtractedPriority(icalTodo->priority());
0073     todo.setExtractedCategories(icalTodo->categories());
0074     todo.setExtractedParentUid(icalTodo->relatedTo());
0075 }
0076 
0077 void TodoPropertyExtractor::newEntity(Todo &todo)
0078 {
0079     updatedIndexedProperties(todo, todo.getIcal());
0080 }
0081 
0082 void TodoPropertyExtractor::modifiedEntity(const Todo &oldTodo, Todo &newTodo)
0083 {
0084     updatedIndexedProperties(newTodo, newTodo.getIcal());
0085 }