Commit 734a7b27 authored by Dimitris Kardarakos's avatar Dimitris Kardarakos

Handle event adding/editing/removing through a controller component

parent 076a9e58
......@@ -239,7 +239,9 @@ Kirigami.Page {
onTriggered: {
if(summary.text) {
console.log("Saving event, root.startdt:" + startdt);
root.calendar.addEditEvent( { "uid" : root.uid, "startDate": root.startdt, "summary": root.summary, "description": root.description, "startHour": root.startHour + (root.startPm ? 12 : 0), "startMinute": root.startMinute, "allDay": root.allDay, "location": root.location, "endDate": root.enddt, "endHour": root.endHour + (root.endPm ? 12 : 0), "endMinute": root.endMinute, }) ;
var controller = eventController.createObject(parent, {calendar: root.calendar});
controller.vevent = { "uid" : root.uid, "startDate": root.startdt, "summary": root.summary, "description": root.description, "startHour": root.startHour + (root.startPm ? 12 : 0), "startMinute": root.startMinute, "allDay": root.allDay, "location": root.location, "endDate": root.enddt, "endHour": root.endHour + (root.endPm ? 12 : 0), "endMinute": root.endMinute };
controller.addEdit();
editcompleted();
}
else {
......@@ -296,4 +298,10 @@ Kirigami.Page {
}
}
Component {
id: eventController
Calindori.EventController {
}
}
}
......@@ -45,7 +45,6 @@ Kirigami.Page {
onTriggered: pageStack.push(eventEditor, {startdt: eventStartDt, enddt: eventStartDt})
}
Component {
id: eventEditor
EventEditor {
......@@ -78,7 +77,9 @@ Kirigami.Page {
icon.name: "delete"
onTriggered: {
root.calendar.deleteEvent(model.uid);
var controller = eventController.createObject(parent, { calendar: root.calendar });
controller.vevent = { uid: model.uid } ;
controller.remove();
eventsUpdated();
}
},
......@@ -115,4 +116,12 @@ Kirigami.Page {
}
}
}
Component {
id: eventController
Calindori.EventController {
}
}
}
......@@ -4,6 +4,7 @@ set(qmlplugin_SRCS
todosmodel.cpp
localcalendar.cpp
eventmodel.cpp
eventcontroller.cpp
)
add_library (calindoriqmlplugin SHARED ${qmlplugin_SRCS})
......
/*
* 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 "eventcontroller.h"
#include "localcalendar.h"
#include <QDebug>
EventController::EventController(QObject* parent) : QObject(parent) {}
EventController::~EventController() = default;
QObject* EventController::calendar() const
{
return m_calendar;
}
void EventController::setCalendar(QObject * const calendarPtr)
{
if(calendarPtr != m_calendar)
{
m_calendar = calendarPtr;
}
emit calendarChanged();
}
QVariantMap EventController::vevent() const
{
return m_event;
}
void EventController::setVevent(const QVariantMap& event)
{
m_event = event;
}
void EventController::remove()
{
qDebug() << "Deleting event";
auto localcalendar = qobject_cast<LocalCalendar*>(m_calendar);
MemoryCalendar::Ptr memoryCalendar = localcalendar->memorycalendar();
QString uid = m_event["uid"].value<QString>();
Event::Ptr event = memoryCalendar->event(uid);
memoryCalendar->deleteEvent(event);
bool deleted = localcalendar->save();
qDebug() << "Event deleted: " << deleted;
emit veventChanged();
}
void EventController::addEdit()
{
qDebug() << "Creating event";
auto localcalendar = qobject_cast<LocalCalendar*>(m_calendar);
MemoryCalendar::Ptr memoryCalendar = localcalendar->memorycalendar();
QDateTime now = QDateTime::currentDateTime();
QString uid = m_event["uid"].value<QString>();
QString summary = m_event["summary"].value<QString>();
Event::Ptr event;
if (uid == "") {
event = Event::Ptr(new Event());
event->setUid(summary.left(1) + now.toString("yyyyMMddhhmmsszzz"));
}
else {
event = memoryCalendar->event(uid);
event->setUid(uid);
}
QDate startDate = m_event["startDate"].value<QDate>();
int startHour = m_event["startHour"].value<int>();
int startMinute = m_event["startMinute"].value<int>();
QDate endDate = m_event["endDate"].value<QDate>();
int endHour = m_event["endHour"].value<int>();
int endMinute = m_event["endMinute"].value<int>();
QDateTime startDateTime;
QDateTime endDateTime;
bool allDayFlg= m_event["allDay"].value<bool>();
if(allDayFlg) {
startDateTime = QDateTime(startDate);
endDateTime = QDateTime(endDate);
}
else {
startDateTime = QDateTime(startDate, QTime(startHour, startMinute, 0, 0), QTimeZone::systemTimeZone());
endDateTime = QDateTime(endDate, QTime(endHour, endMinute, 0, 0), QTimeZone::systemTimeZone());
}
event->setDtStart(startDateTime);
event->setDtEnd(endDateTime);
event->setDescription(m_event["description"].value<QString>());
event->setSummary(summary);
event->setAllDay(allDayFlg);
event->setLocation(m_event["location"].value<QString>());
memoryCalendar->addEvent(event);
bool merged = localcalendar->save();
qDebug() << "Event added/updated: " << merged;
emit veventChanged();
}
/*
* 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 EVENTCONTROLLER_H
#define EVENTCONTROLLER_H
#include <QObject>
#include <KCalCore/Event>
#include <KCalCore/MemoryCalendar>
#include <KCalCore/FileStorage>
class EventController : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantMap vevent READ vevent WRITE setVevent NOTIFY veventChanged)
Q_PROPERTY(QObject* calendar READ calendar WRITE setCalendar NOTIFY calendarChanged)
public:
explicit EventController(QObject* parent = nullptr);
~EventController() override;
QVariantMap vevent() const;
void setVevent(const QVariantMap& event);
QObject* calendar() const;
void setCalendar(QObject* const calendarPtr);
Q_INVOKABLE void remove();
Q_INVOKABLE void addEdit();
Q_SIGNALS:
void veventChanged();
void calendarChanged();
private:
QVariantMap m_event;
QObject* m_calendar;
};
#endif
......@@ -170,74 +170,6 @@ QDateTime LocalCalendar::nulldate() const
return QDateTime();
}
void LocalCalendar::addEditEvent(const QVariantMap& eventObject)
{
if ( m_calendar == nullptr)
{
qDebug() << "Calendar not initialized, cannot add/edit events";
return;
}
qDebug() << "Creating event";
QDateTime now = QDateTime::currentDateTime();
Event::Ptr event;
QString uid = eventObject["uid"].value<QString>();
QString summary = eventObject["summary"].value<QString>();
if (uid == "") {
event = Event::Ptr(new Event());
event->setUid(summary.left(1) + now.toString("yyyyMMddhhmmsszzz"));
}
else {
event = m_calendar->event(uid);
event->setUid(uid);
}
QDate startDate = eventObject["startDate"].value<QDate>();
int startHour = eventObject["startHour"].value<int>();
int startMinute = eventObject["startMinute"].value<int>();
QDate endDate = eventObject["endDate"].value<QDate>();
int endHour = eventObject["endHour"].value<int>();
int endMinute = eventObject["endMinute"].value<int>();
QDateTime startDateTime;
QDateTime endDateTime;
bool allDayFlg= eventObject["allDay"].value<bool>();
if(allDayFlg) {
startDateTime = QDateTime(startDate);
endDateTime = QDateTime(endDate);
}
else {
startDateTime = QDateTime(startDate, QTime(startHour, startMinute, 0, 0), QTimeZone::systemTimeZone());
endDateTime = QDateTime(endDate, QTime(endHour, endMinute, 0, 0), QTimeZone::systemTimeZone());
}
event->setDtStart(startDateTime);
event->setDtEnd(endDateTime);
event->setDescription(eventObject["description"].value<QString>());
event->setSummary(summary);
event->setAllDay(allDayFlg);
event->setLocation(eventObject["location"].value<QString>());
m_calendar->addEvent(event);
bool success = m_cal_storage->save();
qDebug() << "Saving event object: " << success;
}
void LocalCalendar::deleteEvent(QString uid) {
qDebug() << "Deleting event: " << uid;
Event::Ptr event = m_calendar->event(uid);
m_calendar->deleteEvent(event);
bool success = m_cal_storage->save();
qDebug() << "Event deleted? " << success;
}
int LocalCalendar::eventsCount(const QDate& date) const {
if(m_calendar == nullptr)
{
......@@ -246,3 +178,8 @@ int LocalCalendar::eventsCount(const QDate& date) const {
Event::List eventList = m_calendar->rawEvents(date,date);
return eventList.count();
}
bool LocalCalendar::save()
{
return m_cal_storage->save();
}
......@@ -52,10 +52,8 @@ public Q_SLOTS:
void deleteTask(QString uid);
int todosCount(const QDate &date) const;
void deleteCalendar();
void addEditEvent(const QVariantMap& eventObject);
void deleteEvent(QString uid);
int eventsCount(const QDate& date) const;
bool save();
Q_SIGNALS:
void memorycalendarChanged();
void calendarstorageChanged();
......
......@@ -22,6 +22,7 @@
#include "localcalendar.h"
#include "todosmodel.h"
#include "eventmodel.h"
#include "eventcontroller.h"
#include <QQmlEngine>
#include <QtQml/qqml.h>
......@@ -32,4 +33,5 @@ void QmlPlugins::registerTypes(const char *uri)
qmlRegisterType<TodosModel>(uri, 0, 1, "TodosModel");
qmlRegisterType<LocalCalendar>(uri, 0, 1, "LocalCalendar");
qmlRegisterType<EventModel>(uri, 0, 1, "EventModel");
qmlRegisterType<EventController>(uri,0,1,"EventController");
}
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