summaryrefslogtreecommitdiffstats
path: root/examples/caldavresource/caldavresource.cpp
diff options
context:
space:
mode:
authorRémi Nicole <nicole@kolabsystems.com>2018-03-27 18:26:11 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-03-27 18:26:15 +0200
commit761328989492db9bd603c2d7f1134d20e485d2f6 (patch)
tree0e3b4517dd2000fb1cc2738bbb22a3e54dfffb6f /examples/caldavresource/caldavresource.cpp
parent80afd7070f2d8e57cab2fe55fef611623fdb75f0 (diff)
downloadsink-761328989492db9bd603c2d7f1134d20e485d2f6.tar.gz
sink-761328989492db9bd603c2d7f1134d20e485d2f6.zip
Add CalDAV support
Summary: Notes: - Add a `webdavcommon` folder for WebDAV generic resource code - Move `davresource` to `carddaveresource` and make it use the WebDAV code - For now it tests the CalDAV resource directly on KolabNow (to be changed) - Only synchronization, not adding / changing / removing WebDAV collections or items (to be implemented) - Only events are currently supported (todo, freebusy, etc. are to be implemented but should be straightforward) Fixes T8224 Reviewers: cmollekopf Tags: #sink Maniphest Tasks: T8224 Differential Revision: https://phabricator.kde.org/D11741
Diffstat (limited to 'examples/caldavresource/caldavresource.cpp')
-rw-r--r--examples/caldavresource/caldavresource.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/examples/caldavresource/caldavresource.cpp b/examples/caldavresource/caldavresource.cpp
new file mode 100644
index 0000000..2bcdfa1
--- /dev/null
+++ b/examples/caldavresource/caldavresource.cpp
@@ -0,0 +1,155 @@
1/*
2 * Copyright (C) 2018 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "caldavresource.h"
21
22#include "../webdavcommon/webdav.h"
23
24#include "adaptorfactoryregistry.h"
25#include "applicationdomaintype.h"
26#include "domainadaptor.h"
27#include "facade.h"
28#include "facadefactory.h"
29
30#include <KCalCore/ICalFormat>
31
32#define ENTITY_TYPE_EVENT "event"
33#define ENTITY_TYPE_CALENDAR "calendar"
34
35using Sink::ApplicationDomain::getTypeName;
36
37class EventSynchronizer : public WebDavSynchronizer
38{
39 using Event = Sink::ApplicationDomain::Event;
40 using Calendar = Sink::ApplicationDomain::Calendar;
41
42public:
43 explicit EventSynchronizer(const Sink::ResourceContext &context)
44 : WebDavSynchronizer(context, KDAV2::CalDav, getTypeName<Calendar>(), getTypeName<Event>())
45 {}
46
47protected:
48 void updateLocalCollections(KDAV2::DavCollection::List calendarList) Q_DECL_OVERRIDE
49 {
50 SinkLog() << "Found" << calendarList.size() << "calendar(s)";
51
52 QVector<QByteArray> ridList;
53 for (const auto &remoteCalendar : calendarList) {
54 const auto &rid = resourceID(remoteCalendar);
55 SinkLog() << "Found calendar:" << remoteCalendar.displayName() << "[" << rid << "]";
56
57 Calendar localCalendar;
58 localCalendar.setName(remoteCalendar.displayName());
59
60 createOrModify(ENTITY_TYPE_CALENDAR, rid, localCalendar,
61 /* mergeCriteria = */ QHash<QByteArray, Sink::Query::Comparator>{});
62 }
63 }
64
65 void updateLocalItem(KDAV2::DavItem remoteItem, const QByteArray &calendarLocalId) Q_DECL_OVERRIDE
66 {
67 const auto &rid = resourceID(remoteItem);
68
69 auto incidence = KCalCore::ICalFormat().fromString(remoteItem.data());
70
71 using Type = KCalCore::IncidenceBase::IncidenceType;
72
73 switch (incidence->type()) {
74 case Type::TypeEvent: {
75 auto remoteEvent = dynamic_cast<const KCalCore::Event &>(*incidence);
76
77 Event localEvent;
78 localEvent.setUid(remoteEvent.uid());
79 localEvent.setSummary(remoteEvent.summary());
80 localEvent.setDescription(remoteEvent.description());
81 localEvent.setStartTime(remoteEvent.dtStart());
82 localEvent.setCalendar(calendarLocalId);
83
84 SinkTrace() << "Found an event:" << localEvent.getSummary() << "with id:" << rid;
85
86 createOrModify(ENTITY_TYPE_EVENT, rid, localEvent,
87 /* mergeCriteria = */ QHash<QByteArray, Sink::Query::Comparator>{});
88 break;
89 }
90 case Type::TypeTodo:
91 SinkWarning() << "Unimplemented add of a 'Todo' item in the Store";
92 break;
93 case Type::TypeJournal:
94 SinkWarning() << "Unimplemented add of a 'Journal' item in the Store";
95 break;
96 case Type::TypeFreeBusy:
97 SinkWarning() << "Unimplemented add of a 'FreeBusy' item in the Store";
98 break;
99 case Type::TypeUnknown:
100 SinkWarning() << "Trying to add a 'Unknown' item";
101 break;
102 default:
103 break;
104 }
105 }
106
107 QByteArray collectionLocalResourceID(const KDAV2::DavCollection &calendar) Q_DECL_OVERRIDE
108 {
109 return syncStore().resolveRemoteId(ENTITY_TYPE_CALENDAR, resourceID(calendar));
110 }
111};
112
113CalDavResource::CalDavResource(const Sink::ResourceContext &context)
114 : Sink::GenericResource(context)
115{
116 auto synchronizer = QSharedPointer<EventSynchronizer>::create(context);
117 setupSynchronizer(synchronizer);
118
119 // setupPreprocessors(ENTITY_TYPE_EVENT, QVector<Sink::Preprocessor*>() << new EventPropertyExtractor);
120}
121
122CalDavResourceFactory::CalDavResourceFactory(QObject *parent)
123 : Sink::ResourceFactory(parent, {
124 Sink::ApplicationDomain::ResourceCapabilities::Event::event,
125 Sink::ApplicationDomain::ResourceCapabilities::Event::calendar,
126 Sink::ApplicationDomain::ResourceCapabilities::Event::storage,
127 })
128{}
129
130Sink::Resource *CalDavResourceFactory::createResource(const Sink::ResourceContext &context)
131{
132 return new CalDavResource(context);
133}
134
135using Sink::ApplicationDomain::Calendar;
136using Sink::ApplicationDomain::Event;
137
138void CalDavResourceFactory::registerFacades(const QByteArray &resourceName, Sink::FacadeFactory &factory)
139{
140 factory.registerFacade<Event, Sink::DefaultFacade<Event>>(resourceName);
141 factory.registerFacade<Calendar, Sink::DefaultFacade<Calendar>>(resourceName);
142}
143
144
145void CalDavResourceFactory::registerAdaptorFactories(
146 const QByteArray &resourceName, Sink::AdaptorFactoryRegistry &registry)
147{
148 registry.registerFactory<Event, DefaultAdaptorFactory<Event>>(resourceName);
149 registry.registerFactory<Calendar, DefaultAdaptorFactory<Calendar>>(resourceName);
150}
151
152void CalDavResourceFactory::removeDataFromDisk(const QByteArray &instanceIdentifier)
153{
154 CalDavResource::removeFromDisk(instanceIdentifier);
155}