-
Notifications
You must be signed in to change notification settings - Fork 6
/
calendarsource.cpp
159 lines (131 loc) · 4.29 KB
/
calendarsource.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
* Copyright (C) 2016 Javier S. Pedro <[email protected]>
*
* 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 <QDateTime>
#ifdef MER_EDITION_SAILFISH
#include "calendarsource_p.h"
#include <KCalendarCore/OccurrenceIterator>
#include <KCalendarCore/Event>
#endif
namespace watchfish
{
Q_LOGGING_CATEGORY(calendarSourceCat, "watchfish-CalendarSource")
#ifdef MER_EDITION_SAILFISH
CalendarSourcePrivate::CalendarSourcePrivate(CalendarSource *q)
#ifdef KF5CALENDARCORE
: calendar(new mKCal::ExtendedCalendar(QTimeZone::systemTimeZone())),
#else
: calendar(new mKCal::ExtendedCalendar(KDateTime::Spec::LocalZone())),
#endif
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(
#ifdef KF5CALENDARCORE
const KCalendarCore::Incidence::Ptr &incidence
#else
const KCalCore::Incidence::Ptr &incidence
#endif
, const QDateTime &startDate, const QDateTime &endDate) {
CalendarEvent event;
event.setUid(incidence->uid());
event.setStart(startDate);
event.setEnd(endDate);
auto alarmList = incidence->alarms();
if (!alarmList.empty() && alarmList[0]->enabled()) {
QDateTime nextAlarm = alarmList[0]->nextRepetition(QDateTime::currentDateTime());
if (nextAlarm.isValid())
event.setAlertTime(nextAlarm);
}
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->load(start, end);
qCDebug(calendarSourceCat) << "Loaded" << count << "normal events";
QList<CalendarEvent> events;
KCalendarCore::OccurrenceIterator it(*d->calendar, QDateTime(start), QDateTime(end));
while (it.hasNext()) {
it.next();
if (!d->calendar->isVisible(it.incidence()) || it.incidence()->type() != KCalendarCore::IncidenceBase::TypeEvent) {
continue;
}
events.append(d->convertToEvent(it.incidence(), it.occurrenceStartDate(), it.occurrenceStartDate())); //!TODO end date is newer api
}
qCDebug(calendarSourceCat) << "Returning" << events.size() << "events";
return events;
}
#else
CalendarSource::CalendarSource(QObject *parent)
: QObject(parent)
{
}
CalendarSource::~CalendarSource()
{
}
QList<CalendarEvent> CalendarSource::fetchEvents(const QDate &start, const QDate &end,
bool startInclusive, bool endInclusive)
{
QList<CalendarEvent> events;
return events;
}
#endif
}