summaryrefslogtreecommitdiff
path: root/calendarsource.cpp
blob: 8a395db7ef5c4a7f0cdad751d345fac1dc652ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *  libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
 *  Copyright (C) 2016 Javier S. Pedro <dev.git@javispedro.com>
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

#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<CalendarEvent> CalendarSource::fetchEvents(const QDate &start, const QDate &end,
												 bool startInclusive, bool endInclusive)
{
	Q_D(CalendarSource);
	int count;
	count = d->calendarStorage->loadRecurringIncidences();
	qCDebug(calendarSourceCat) << "Loaded" << count << "recurring events";
	count = d->calendarStorage->load(start, end);
	qCDebug(calendarSourceCat) << "Loaded" << count << "normal events";

	QList<CalendarEvent> events;
	QVector<mKCal::ExtendedCalendar::ExpandedIncidence> 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;
}

}