Commit 99794b71 authored by Dimitris Kardarakos's avatar Dimitris Kardarakos

Handle todo insert/update/delete through a dedicated controller component

parent c10b660f
Pipeline #3450 passed with stage
in 6 minutes and 57 seconds
......@@ -207,7 +207,9 @@ Kirigami.Page {
onTriggered: {
if(summary.text) {
console.log("Saving task");
root.calendar.addEditTask(root.uid, root.startdt, root.summary, root.description, root.startHour + (root.startPm ? 12 : 0), root.startMinute, root.allDay, root.location, root.completed); //TODO: Pass a Todo object
var controller = todoController.createObject(parent, { calendar: root.calendar });
controller.vtodo = { "uid": root.uid, "summary":root.summary, "startDate": root.startdt , "startHour": root.startHour + (root.startPm ? 12 : 0), "startMinute": root.startMinute, "allDay": root.allDay, "description": root.description,"location": root.location, "completed": root.completed};
controller.addEdit();
taskeditcompleted();
}
else {
......@@ -240,4 +242,11 @@ Kirigami.Page {
TimePickerSheet {
id: startTimePickerSheet
}
Component {
id: todoController
Calindori.TodoController {}
}
}
......@@ -78,7 +78,9 @@ Kirigami.Page {
icon.name: "delete"
onTriggered: {
root.calendar.deleteTask(model.uid);
var controller = todoController.createObject(parent, { calendar: root.calendar });
controller.vtodo = { "uid" : model.uid };
controller.remove();
tasksUpdated();
}
},
......@@ -114,4 +116,10 @@ Kirigami.Page {
}
}
}
Component {
id: todoController
Calindori.TodoController {}
}
}
......@@ -5,6 +5,7 @@ set(qmlplugin_SRCS
localcalendar.cpp
eventmodel.cpp
eventcontroller.cpp
todocontroller.cpp
)
add_library (calindoriqmlplugin SHARED ${qmlplugin_SRCS})
......
......@@ -49,6 +49,7 @@ QVariantMap EventController::vevent() const
void EventController::setVevent(const QVariantMap& event)
{
m_event = event;
emit veventChanged();
}
void EventController::remove()
......@@ -65,7 +66,7 @@ void EventController::remove()
qDebug() << "Event deleted: " << deleted;
emit veventChanged();
emit veventsUpdated();
}
void EventController::addEdit()
......@@ -123,4 +124,5 @@ void EventController::addEdit()
qDebug() << "Event added/updated: " << merged;
emit veventChanged();
emit veventsUpdated();
}
......@@ -46,6 +46,7 @@ public:
Q_SIGNALS:
void veventChanged();
void calendarChanged();
void veventsUpdated();
private:
QVariantMap m_event;
......
......@@ -91,59 +91,6 @@ void LocalCalendar::setCalendarstorage(FileStorage::Ptr calendarStorage)
}
}
void LocalCalendar::addEditTask(QString uid, QDate startDate, QString summary, QString description, int startHour, int startMinute, bool allDayFlg, QString location, bool completed)
{
if ( m_calendar == nullptr)
{
qDebug() << "Calendar not initialized, cannot add/edit tasks";
return;
}
qDebug() << "Creating todo" << "summary:" << summary << ", description:" << description << ", startDate:" << startDate.toString() << ", startHour: " << startHour << " , startMinute: " << startMinute << " , allDayFlg: " << allDayFlg;
QDateTime now = QDateTime::currentDateTime();
Todo::Ptr todo;
if (uid == "") {
todo = Todo::Ptr(new Todo());
todo->setUid(summary.left(1) + now.toString("yyyyMMddhhmmsszzz"));
}
else {
todo = m_calendar->todo(uid);
todo->setUid(uid);
}
QDateTime startDateTime;
if(allDayFlg) {
startDateTime = QDateTime(startDate);
}
else {
startDateTime = QDateTime(startDate, QTime(startHour, startMinute, 0, 0), QTimeZone::systemTimeZone());
}
todo->setDtStart(startDateTime);
todo->setDescription(description);
todo->setSummary(summary);
todo->setAllDay(allDayFlg);
todo->setLocation(location);
todo->setCompleted(completed);
m_calendar->addTodo(todo);
bool success = m_cal_storage->save();
qDebug() << "Storage save: " << success;
qDebug() << "Todo has been saved";
}
void LocalCalendar::deleteTask(QString uid) {
qDebug() << "Deleting task: " << uid;
Todo::Ptr todo = m_calendar->todo(uid);
m_calendar->deleteTodo(todo);
bool success = m_cal_storage->save();
qDebug() << "Task deleted? " << success;
}
int LocalCalendar::todosCount(const QDate &date) const {
if(m_calendar == nullptr)
{
......
......@@ -48,8 +48,6 @@ public:
void setName(QString calendarName);
public Q_SLOTS:
void addEditTask(QString uid, QDate startDate, QString summary, QString description, int startHour, int startMinute, bool allDayFlg, QString location, bool completed);
void deleteTask(QString uid);
int todosCount(const QDate &date) const;
void deleteCalendar();
int eventsCount(const QDate& date) const;
......
......@@ -23,6 +23,7 @@
#include "todosmodel.h"
#include "eventmodel.h"
#include "eventcontroller.h"
#include "todocontroller.h"
#include <QQmlEngine>
#include <QtQml/qqml.h>
......@@ -34,4 +35,5 @@ void QmlPlugins::registerTypes(const char *uri)
qmlRegisterType<LocalCalendar>(uri, 0, 1, "LocalCalendar");
qmlRegisterType<EventModel>(uri, 0, 1, "EventModel");
qmlRegisterType<EventController>(uri,0,1,"EventController");
qmlRegisterType<TodoController>(uri,0,1,"TodoController");
}
/*
* Copyright (C) 2019 Dimitris Kardarakos
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "todocontroller.h"
#include "localcalendar.h"
#include <KCalCore/Todo>
#include <KCalCore/MemoryCalendar>
#include <QDebug>
TodoController::TodoController(QObject* parent) : QObject(parent) {}
TodoController::~TodoController() = default;
QObject * TodoController::calendar() const
{
return m_calendar;
}
void TodoController::setCalendar(QObject *const calendarPtr)
{
if(m_calendar != calendarPtr)
{
m_calendar = calendarPtr;
emit calendarChanged();
}
}
QVariantMap TodoController::vtodo() const
{
return m_todo;
}
void TodoController::setVtodo(const QVariantMap& todo)
{
m_todo = todo;
emit vtodoChanged();
}
void TodoController::addEdit()
{
qDebug() << "Adding/updating todo";
auto localCalendar = qobject_cast<LocalCalendar*>(m_calendar);
MemoryCalendar::Ptr memoryCalendar = localCalendar->memorycalendar();
Todo::Ptr vtodo;
QDateTime now = QDateTime::currentDateTime();
QString uid = m_todo["uid"].value<QString>();
QString summary = m_todo["summary"].value<QString>();
QDate startDate = m_todo["startDate"].value<QDate>();
int startHour = m_todo["startHour"].value<int>();
int startMinute = m_todo["startMinute"].value<int>();
bool allDayFlg= m_todo["allDay"].value<bool>();
if(uid == "")
{
vtodo = Todo::Ptr(new Todo());
vtodo->setUid(summary.left(1) + now.toString("yyyyMMddhhmmsszzz"));
}
else
{
vtodo = memoryCalendar->todo(uid);
vtodo->setUid(uid);
}
QDateTime startDateTime;
if(allDayFlg) {
startDateTime = QDateTime(startDate);
}
else {
startDateTime = QDateTime(startDate, QTime(startHour, startMinute, 0, 0), QTimeZone::systemTimeZone());
}
vtodo->setDtStart(startDateTime);
vtodo->setDescription(m_todo["description"].value<QString>());
vtodo->setSummary(summary);
vtodo->setAllDay(allDayFlg);
vtodo->setLocation(m_todo["location"].value<QString>());
vtodo->setCompleted(m_todo["completed"].value<bool>());
memoryCalendar->addTodo(vtodo);
bool merged = localCalendar->save();
qDebug() << "Todo added/updated: " << merged;
emit vtodoChanged();
emit vtodosUpdated();
}
void TodoController::remove()
{
qDebug() << "Deleting todo";
auto localCalendar = qobject_cast<LocalCalendar*>(m_calendar);
MemoryCalendar::Ptr memoryCalendar = localCalendar->memorycalendar();
QString uid = m_todo["uid"].value<QString>();
Todo::Ptr vtodo = memoryCalendar->todo(uid);
memoryCalendar->deleteTodo(vtodo);
bool removed = localCalendar->save();
qDebug() << "Todo deleted: " << removed;
emit vtodoChanged();
emit vtodosUpdated();
}
/*
* Copyright (C) 2019 Dimitris Kardarakos
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TODOCONTROLLER_H
#define TODOCONTROLLER_H
#include <QObject>
#include <QVariantMap>
class TodoController : public QObject
{
Q_OBJECT
Q_PROPERTY(QObject* calendar READ calendar WRITE setCalendar NOTIFY calendarChanged);
Q_PROPERTY(QVariantMap vtodo READ vtodo WRITE setVtodo NOTIFY vtodoChanged);
public:
explicit TodoController(QObject* parent = nullptr);
~TodoController() override;
QObject* calendar() const;
void setCalendar(QObject* const calendarPtr );
QVariantMap vtodo() const;
void setVtodo(const QVariantMap& todo);
Q_INVOKABLE void remove();
Q_INVOKABLE void addEdit();
Q_SIGNALS:
void calendarChanged();
void vtodoChanged();
void vtodosUpdated();
private:
QObject* m_calendar;
QVariantMap m_todo;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment