From fa539e0228bb618516372d330b9e57f8f482d422 Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 28 Aug 2016 17:03:04 +0200 Subject: add calendar feature --- calendarsource.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 calendarsource.cpp (limited to 'calendarsource.cpp') diff --git a/calendarsource.cpp b/calendarsource.cpp new file mode 100644 index 0000000..8c3ad34 --- /dev/null +++ b/calendarsource.cpp @@ -0,0 +1,113 @@ +/* + * libwatchfish - library with common functionality for SailfishOS smartwatch connector programs. + * Copyright (C) 2016 Javier S. Pedro + * + * This program 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 program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "calendarsource.h" +#include "calendarsource_p.h" + +namespace watchfish +{ + +Q_LOGGING_CATEGORY(calendarSourceCat, "watchfish-CalendarSource") + +CalendarSourcePrivate::CalendarSourcePrivate(CalendarSource *q) + : calendar(new mKCal::ExtendedCalendar(KDateTime::Spec::LocalZone())), + calendarStorage(calendar->defaultStorage(calendar)), + q_ptr(q) +{ + calendarStorage->registerObserver(this); + if (!calendarStorage->open()) { + qCWarning(calendarSourceCat) << "Cannot open calendar database"; + } +} + +CalendarSourcePrivate::~CalendarSourcePrivate() +{ + calendarStorage->unregisterObserver(this); +} + +void CalendarSourcePrivate::storageModified(mKCal::ExtendedStorage *storage, const QString &info) +{ + Q_Q(CalendarSource); + Q_UNUSED(storage); + qCDebug(calendarSourceCat) << "Storage modified:" << info; + emit q->changed(); +} + +void CalendarSourcePrivate::storageProgress(mKCal::ExtendedStorage *storage, const QString &info) +{ + Q_UNUSED(storage); + Q_UNUSED(info); + // Nothing to do +} + +void CalendarSourcePrivate::storageFinished(mKCal::ExtendedStorage *storage, bool error, const QString &info) +{ + Q_UNUSED(storage); + Q_UNUSED(error); + Q_UNUSED(info); + // Nothing to do +} + +CalendarEvent CalendarSourcePrivate::convertToEvent(const mKCal::ExtendedCalendar::ExpandedIncidence &expanded) +{ + const KCalCore::Incidence::Ptr &incidence = expanded.second; + CalendarEvent event; + + event.setUid(incidence->uid()); + + event.setStart(expanded.first.dtStart); + event.setEnd(expanded.first.dtEnd); + + event.setAllDay(incidence->allDay()); + event.setTitle(incidence->summary()); + event.setLocation(incidence->location()); + + return event; +} + +CalendarSource::CalendarSource(QObject *parent) + : QObject(parent), d_ptr(new CalendarSourcePrivate(this)) +{ +} + +CalendarSource::~CalendarSource() +{ + delete d_ptr; +} + +QList CalendarSource::fetchEvents(const QDate &start, const QDate &end, + bool startInclusive, bool endInclusive) +{ + Q_D(CalendarSource); + int count = 0; + count += d->calendarStorage->loadRecurringIncidences(); + qCDebug(calendarSourceCat) << "Loaded" << count << "recurring events"; + count += d->calendarStorage->load(start, end); + qCDebug(calendarSourceCat) << "Loaded" << count << "events total"; + + QList events; + QVector incidences = d->calendar->rawExpandedEvents(start, end, startInclusive, endInclusive); + Q_FOREACH(const mKCal::ExtendedCalendar::ExpandedIncidence &expanded, incidences) { + events.append(d->convertToEvent(expanded)); + } + + qCDebug(calendarSourceCat) << "Returning" << events.size() << "events"; + return events; +} + +} -- cgit v1.2.3