diff options
Diffstat (limited to 'examples/caldavresource/caldavresource.cpp')
-rw-r--r-- | examples/caldavresource/caldavresource.cpp | 155 |
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 | |||
35 | using Sink::ApplicationDomain::getTypeName; | ||
36 | |||
37 | class EventSynchronizer : public WebDavSynchronizer | ||
38 | { | ||
39 | using Event = Sink::ApplicationDomain::Event; | ||
40 | using Calendar = Sink::ApplicationDomain::Calendar; | ||
41 | |||
42 | public: | ||
43 | explicit EventSynchronizer(const Sink::ResourceContext &context) | ||
44 | : WebDavSynchronizer(context, KDAV2::CalDav, getTypeName<Calendar>(), getTypeName<Event>()) | ||
45 | {} | ||
46 | |||
47 | protected: | ||
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 | |||
113 | CalDavResource::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 | |||
122 | CalDavResourceFactory::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 | |||
130 | Sink::Resource *CalDavResourceFactory::createResource(const Sink::ResourceContext &context) | ||
131 | { | ||
132 | return new CalDavResource(context); | ||
133 | } | ||
134 | |||
135 | using Sink::ApplicationDomain::Calendar; | ||
136 | using Sink::ApplicationDomain::Event; | ||
137 | |||
138 | void 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 | |||
145 | void CalDavResourceFactory::registerAdaptorFactories( | ||
146 | const QByteArray &resourceName, Sink::AdaptorFactoryRegistry ®istry) | ||
147 | { | ||
148 | registry.registerFactory<Event, DefaultAdaptorFactory<Event>>(resourceName); | ||
149 | registry.registerFactory<Calendar, DefaultAdaptorFactory<Calendar>>(resourceName); | ||
150 | } | ||
151 | |||
152 | void CalDavResourceFactory::removeDataFromDisk(const QByteArray &instanceIdentifier) | ||
153 | { | ||
154 | CalDavResource::removeFromDisk(instanceIdentifier); | ||
155 | } | ||