File indexing completed on 2025-01-19 04:52:00

0001 /*
0002  *  Copyright (C) 2017 Michael Bohldueer, <michael.bohldueer@kdemail.net>
0003  *  Copyright (C) 2018 Christian Mollekopf, <mollekopf@kolabsys.com>
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 along
0016  *  with this program; if not, write to the Free Software Foundation, Inc.,
0017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "todocontroller.h"
0021 
0022 #include <sink/applicationdomaintype.h>
0023 #include <sink/store.h>
0024 #include <sink/log.h>
0025 
0026 #include <KCalendarCore/ICalFormat>
0027 #include <KCalendarCore/Todo>
0028 #include <QUuid>
0029 
0030 using namespace Sink::ApplicationDomain;
0031 
0032 TodoController::TodoController()
0033     : Kube::Controller(),
0034     action_save{new Kube::ControllerAction{this, &TodoController::save}}
0035 {
0036     updateSaveAction();
0037 }
0038 
0039 void TodoController::save()
0040 {
0041     using namespace Sink;
0042     using namespace Sink::ApplicationDomain;
0043 
0044     const auto calendar = getCalendar();
0045     if (!calendar) {
0046         qWarning() << "No calendar selected";
0047         return;
0048     }
0049 
0050     auto populateTodo = [this](KCalendarCore::Todo& todo) {
0051         todo.setSummary(getSummary());
0052         todo.setRelatedTo(getParentUid());
0053         todo.setDescription(getDescription());
0054         todo.setDtStart(getStart());
0055         todo.setDtDue(getDue());
0056         if (getComplete()) {
0057             todo.setCompleted(true);
0058         } else if (getDoing()) {
0059             todo.setCompleted(false);
0060             todo.setStatus(KCalendarCore::Incidence::StatusInProcess);
0061         } else {
0062             todo.setCompleted(false);
0063         }
0064     };
0065 
0066     if (auto e = mTodo.value<Sink::ApplicationDomain::Todo::Ptr>()) {
0067         Todo todo = *e;
0068 
0069         //Apply the changed properties on top of what's existing
0070         auto calcoreTodo = KCalendarCore::ICalFormat().readIncidence(todo.getIcal()).dynamicCast<KCalendarCore::Todo>();
0071         if(!calcoreTodo) {
0072             SinkWarning() << "Invalid ICal to process, ignoring...";
0073             return;
0074         }
0075         populateTodo(*calcoreTodo);
0076 
0077         todo.setIcal(KCalendarCore::ICalFormat().toICalString(calcoreTodo).toUtf8());
0078         todo.setCalendar(*calendar);
0079 
0080         auto job = Store::modify(todo)
0081             .then([&] (const KAsync::Error &error) {
0082                 if (error) {
0083                     SinkWarning() << "Failed to save the todo: " << error;
0084                 }
0085                 emit done();
0086             });
0087 
0088         run(job);
0089     } else {
0090         Todo todo(calendar->resourceInstanceIdentifier());
0091 
0092         auto calcoreTodo = QSharedPointer<KCalendarCore::Todo>::create();
0093         calcoreTodo->setUid(QUuid::createUuid().toString());
0094         populateTodo(*calcoreTodo);
0095 
0096         todo.setIcal(KCalendarCore::ICalFormat().toICalString(calcoreTodo).toUtf8());
0097         todo.setCalendar(*calendar);
0098 
0099         auto job = Store::create(todo)
0100             .then([&] (const KAsync::Error &error) {
0101                 if (error) {
0102                     SinkWarning() << "Failed to save the todo: " << error;
0103                 }
0104                 emit done();
0105             });
0106 
0107         run(job);
0108     }
0109 }
0110 
0111 void TodoController::updateSaveAction()
0112 {
0113     saveAction()->setEnabled(!getSummary().isEmpty());
0114 }
0115 
0116 void TodoController::reload()
0117 {
0118     loadTodo(mTodo);
0119 }
0120 
0121 void TodoController::loadTodo(const QVariant &variant)
0122 {
0123     using namespace Sink;
0124 
0125     mTodo = variant;
0126     if (auto todo = variant.value<ApplicationDomain::Todo::Ptr>()) {
0127         setCalendar(ApplicationDomainType::Ptr::create(ApplicationDomainType::createEntity<ApplicationDomain::Calendar>(todo->resourceInstanceIdentifier(), todo->getCalendar())));
0128         setCalendarId(todo->getCalendar());
0129 
0130         auto icalTodo = KCalendarCore::ICalFormat().readIncidence(todo->getIcal()).dynamicCast<KCalendarCore::Todo>();
0131         if(!icalTodo) {
0132             SinkWarning() << "Invalid ICal to process, ignoring...";
0133             return;
0134         }
0135         setUid(icalTodo->uid().toUtf8());
0136         setParentUid(icalTodo->relatedTo().toUtf8());
0137         setSummary(icalTodo->summary());
0138         setDescription(icalTodo->description());
0139         setLocation(icalTodo->location());
0140         setStart(icalTodo->dtStart());
0141         setDue(icalTodo->dtDue());
0142         if (icalTodo->status() == KCalendarCore::Incidence::StatusCompleted) {
0143             setComplete(true);
0144             setDoing(false);
0145         } else if (icalTodo->status() == KCalendarCore::Incidence::StatusInProcess) {
0146             setComplete(false);
0147             setDoing(true);
0148         } else {
0149             setComplete(false);
0150             setDoing(false);
0151         }
0152     } else {
0153         qWarning() << "Not a todo" << variant;
0154     }
0155     setModified(false);
0156 }
0157 
0158 void TodoController::remove()
0159 {
0160     if (auto c = mTodo.value<Sink::ApplicationDomain::Todo::Ptr>()) {
0161         run(Sink::Store::remove(*c));
0162     }
0163 }
0164 
0165 QVariant TodoController::getTodo() const
0166 {
0167     return mTodo;
0168 }