diff options
Diffstat (limited to 'examples/caldavresource/caldavresource.cpp')
-rw-r--r-- | examples/caldavresource/caldavresource.cpp | 105 |
1 files changed, 69 insertions, 36 deletions
diff --git a/examples/caldavresource/caldavresource.cpp b/examples/caldavresource/caldavresource.cpp index d9f8c69..d33f625 100644 --- a/examples/caldavresource/caldavresource.cpp +++ b/examples/caldavresource/caldavresource.cpp | |||
@@ -25,25 +25,29 @@ | |||
25 | #include "applicationdomaintype.h" | 25 | #include "applicationdomaintype.h" |
26 | #include "domainadaptor.h" | 26 | #include "domainadaptor.h" |
27 | #include "eventpreprocessor.h" | 27 | #include "eventpreprocessor.h" |
28 | #include "todopreprocessor.h" | ||
28 | #include "facade.h" | 29 | #include "facade.h" |
29 | #include "facadefactory.h" | 30 | #include "facadefactory.h" |
30 | 31 | ||
31 | #include <KCalCore/ICalFormat> | 32 | #include <KCalCore/ICalFormat> |
32 | 33 | ||
33 | #define ENTITY_TYPE_EVENT "event" | 34 | #define ENTITY_TYPE_EVENT "event" |
35 | #define ENTITY_TYPE_TODO "todo" | ||
34 | #define ENTITY_TYPE_CALENDAR "calendar" | 36 | #define ENTITY_TYPE_CALENDAR "calendar" |
35 | 37 | ||
36 | using Sink::ApplicationDomain::getTypeName; | 38 | using Sink::ApplicationDomain::getTypeName; |
37 | 39 | ||
38 | class EventSynchronizer : public WebDavSynchronizer | 40 | class CalDAVSynchronizer : public WebDavSynchronizer |
39 | { | 41 | { |
40 | using Event = Sink::ApplicationDomain::Event; | 42 | using Event = Sink::ApplicationDomain::Event; |
43 | using Todo = Sink::ApplicationDomain::Todo; | ||
41 | using Calendar = Sink::ApplicationDomain::Calendar; | 44 | using Calendar = Sink::ApplicationDomain::Calendar; |
42 | 45 | ||
43 | public: | 46 | public: |
44 | explicit EventSynchronizer(const Sink::ResourceContext &context) | 47 | explicit CalDAVSynchronizer(const Sink::ResourceContext &context) |
45 | : WebDavSynchronizer(context, KDAV2::CalDav, getTypeName<Calendar>(), getTypeName<Event>()) | 48 | : WebDavSynchronizer(context, KDAV2::CalDav, getTypeName<Calendar>(), getTypeName<Event>()) |
46 | {} | 49 | { |
50 | } | ||
47 | 51 | ||
48 | protected: | 52 | protected: |
49 | void updateLocalCollections(KDAV2::DavCollection::List calendarList) Q_DECL_OVERRIDE | 53 | void updateLocalCollections(KDAV2::DavCollection::List calendarList) Q_DECL_OVERRIDE |
@@ -74,21 +78,27 @@ protected: | |||
74 | 78 | ||
75 | switch (incidence->type()) { | 79 | switch (incidence->type()) { |
76 | case Type::TypeEvent: { | 80 | case Type::TypeEvent: { |
77 | auto remoteEvent = dynamic_cast<const KCalCore::Event &>(*incidence); | ||
78 | |||
79 | Event localEvent; | 81 | Event localEvent; |
80 | localEvent.setIcal(ical); | 82 | localEvent.setIcal(ical); |
81 | localEvent.setCalendar(calendarLocalId); | 83 | localEvent.setCalendar(calendarLocalId); |
82 | 84 | ||
83 | SinkTrace() << "Found an event:" << localEvent.getSummary() << "with id:" << rid; | 85 | SinkTrace() << "Found an event with id:" << rid; |
84 | 86 | ||
85 | createOrModify(ENTITY_TYPE_EVENT, rid, localEvent, | 87 | createOrModify(ENTITY_TYPE_EVENT, rid, localEvent, |
86 | /* mergeCriteria = */ QHash<QByteArray, Sink::Query::Comparator>{}); | 88 | /* mergeCriteria = */ QHash<QByteArray, Sink::Query::Comparator>{}); |
87 | break; | 89 | break; |
88 | } | 90 | } |
89 | case Type::TypeTodo: | 91 | case Type::TypeTodo: { |
90 | SinkWarning() << "Unimplemented add of a 'Todo' item in the Store"; | 92 | Todo localTodo; |
93 | localTodo.setIcal(ical); | ||
94 | localTodo.setCalendar(calendarLocalId); | ||
95 | |||
96 | SinkTrace() << "Found a Todo with id:" << rid; | ||
97 | |||
98 | createOrModify(ENTITY_TYPE_TODO, rid, localTodo, | ||
99 | /* mergeCriteria = */ QHash<QByteArray, Sink::Query::Comparator>{}); | ||
91 | break; | 100 | break; |
101 | } | ||
92 | case Type::TypeJournal: | 102 | case Type::TypeJournal: |
93 | SinkWarning() << "Unimplemented add of a 'Journal' item in the Store"; | 103 | SinkWarning() << "Unimplemented add of a 'Journal' item in the Store"; |
94 | break; | 104 | break; |
@@ -108,58 +118,74 @@ protected: | |||
108 | return syncStore().resolveRemoteId(ENTITY_TYPE_CALENDAR, resourceID(calendar)); | 118 | return syncStore().resolveRemoteId(ENTITY_TYPE_CALENDAR, resourceID(calendar)); |
109 | } | 119 | } |
110 | 120 | ||
111 | KAsync::Job<QByteArray> replay(const Event &event, Sink::Operation operation, | 121 | template<typename Item> |
112 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | 122 | KAsync::Job<QByteArray> replayItem(const Item &localItem, Sink::Operation operation, |
123 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties, | ||
124 | const QByteArray &entityType) | ||
113 | { | 125 | { |
114 | SinkLog() << "Replaying event"; | 126 | SinkLog() << "Replaying" << entityType; |
115 | 127 | ||
116 | KDAV2::DavItem item; | 128 | KDAV2::DavItem remoteItem; |
117 | 129 | ||
118 | switch (operation) { | 130 | switch (operation) { |
119 | case Sink::Operation_Creation: { | 131 | case Sink::Operation_Creation: { |
120 | auto rawIcal = event.getIcal(); | 132 | auto rawIcal = localItem.getIcal(); |
121 | if(rawIcal == "") { | 133 | if (rawIcal == "") { |
122 | return KAsync::error<QByteArray>("No ICal in event for creation replay"); | 134 | return KAsync::error<QByteArray>("No ICal in item for creation replay"); |
123 | } | 135 | } |
124 | 136 | ||
125 | auto collectionId = syncStore().resolveLocalId(ENTITY_TYPE_CALENDAR, event.getCalendar()); | 137 | auto collectionId = syncStore().resolveLocalId(ENTITY_TYPE_CALENDAR, localItem.getCalendar()); |
126 | 138 | ||
127 | item.setData(rawIcal); | 139 | remoteItem.setData(rawIcal); |
128 | item.setContentType("text/calendar"); | 140 | remoteItem.setContentType("text/calendar"); |
129 | item.setUrl(urlOf(collectionId, event.getUid())); | 141 | remoteItem.setUrl(urlOf(collectionId, localItem.getUid())); |
130 | 142 | ||
131 | SinkLog() << "Creating event:" << event.getSummary(); | 143 | SinkLog() << "Creating" << entityType << ":" << localItem.getSummary(); |
132 | return createItem(item).then([item] { return resourceID(item); }); | 144 | return createItem(remoteItem).then([remoteItem] { return resourceID(remoteItem); }); |
133 | } | 145 | } |
134 | case Sink::Operation_Removal: { | 146 | case Sink::Operation_Removal: { |
135 | // We only need the URL in the DAV item for removal | 147 | // We only need the URL in the DAV item for removal |
136 | item.setUrl(urlOf(oldRemoteId)); | 148 | remoteItem.setUrl(urlOf(oldRemoteId)); |
137 | 149 | ||
138 | SinkLog() << "Removing event:" << oldRemoteId; | 150 | SinkLog() << "Removing" << entityType << ":" << oldRemoteId; |
139 | return removeItem(item).then([] { return QByteArray{}; }); | 151 | return removeItem(remoteItem).then([] { return QByteArray{}; }); |
140 | } | 152 | } |
141 | case Sink::Operation_Modification: | 153 | case Sink::Operation_Modification: |
142 | auto rawIcal = event.getIcal(); | 154 | auto rawIcal = localItem.getIcal(); |
143 | if(rawIcal == "") { | 155 | if (rawIcal == "") { |
144 | return KAsync::error<QByteArray>("No ICal in event for modification replay"); | 156 | return KAsync::error<QByteArray>("No ICal in item for modification replay"); |
145 | } | 157 | } |
146 | 158 | ||
147 | item.setData(rawIcal); | 159 | remoteItem.setData(rawIcal); |
148 | item.setContentType("text/calendar"); | 160 | remoteItem.setContentType("text/calendar"); |
149 | item.setUrl(urlOf(oldRemoteId)); | 161 | remoteItem.setUrl(urlOf(oldRemoteId)); |
150 | 162 | ||
151 | SinkLog() << "Modifying event:" << event.getSummary(); | 163 | SinkLog() << "Modifying" << entityType << ":" << localItem.getSummary(); |
152 | 164 | ||
153 | // It would be nice to check that the URL of the item hasn't | 165 | // It would be nice to check that the URL of the item hasn't |
154 | // changed and move he item if it did, but since the URL is | 166 | // changed and move he item if it did, but since the URL is |
155 | // pretty much arbitrary, whoe does that anyway? | 167 | // pretty much arbitrary, whoe does that anyway? |
156 | return modifyItem(item).then([oldRemoteId] { return oldRemoteId; }); | 168 | return modifyItem(remoteItem).then([oldRemoteId] { return oldRemoteId; }); |
157 | } | 169 | } |
158 | } | 170 | } |
159 | 171 | ||
172 | KAsync::Job<QByteArray> replay(const Event &event, Sink::Operation operation, | ||
173 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | ||
174 | { | ||
175 | return replayItem(event, operation, oldRemoteId, changedProperties, ENTITY_TYPE_EVENT); | ||
176 | } | ||
177 | |||
178 | KAsync::Job<QByteArray> replay(const Todo &todo, Sink::Operation operation, | ||
179 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | ||
180 | { | ||
181 | return replayItem(todo, operation, oldRemoteId, changedProperties, ENTITY_TYPE_TODO); | ||
182 | } | ||
183 | |||
160 | KAsync::Job<QByteArray> replay(const Calendar &calendar, Sink::Operation operation, | 184 | KAsync::Job<QByteArray> replay(const Calendar &calendar, Sink::Operation operation, |
161 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | 185 | const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE |
162 | { | 186 | { |
187 | SinkLog() << "Replaying calendar"; | ||
188 | |||
163 | switch (operation) { | 189 | switch (operation) { |
164 | case Sink::Operation_Creation: | 190 | case Sink::Operation_Creation: |
165 | SinkWarning() << "Unimplemented replay of calendar creation"; | 191 | SinkWarning() << "Unimplemented replay of calendar creation"; |
@@ -180,19 +206,23 @@ protected: | |||
180 | CalDavResource::CalDavResource(const Sink::ResourceContext &context) | 206 | CalDavResource::CalDavResource(const Sink::ResourceContext &context) |
181 | : Sink::GenericResource(context) | 207 | : Sink::GenericResource(context) |
182 | { | 208 | { |
183 | auto synchronizer = QSharedPointer<EventSynchronizer>::create(context); | 209 | auto synchronizer = QSharedPointer<CalDAVSynchronizer>::create(context); |
184 | setupSynchronizer(synchronizer); | 210 | setupSynchronizer(synchronizer); |
185 | 211 | ||
186 | setupPreprocessors(ENTITY_TYPE_EVENT, QVector<Sink::Preprocessor*>() << new EventPropertyExtractor); | 212 | setupPreprocessors(ENTITY_TYPE_EVENT, QVector<Sink::Preprocessor *>() << new EventPropertyExtractor); |
213 | setupPreprocessors(ENTITY_TYPE_TODO, QVector<Sink::Preprocessor *>() << new TodoPropertyExtractor); | ||
187 | } | 214 | } |
188 | 215 | ||
189 | CalDavResourceFactory::CalDavResourceFactory(QObject *parent) | 216 | CalDavResourceFactory::CalDavResourceFactory(QObject *parent) |
190 | : Sink::ResourceFactory(parent, { | 217 | : Sink::ResourceFactory(parent, { |
191 | Sink::ApplicationDomain::ResourceCapabilities::Event::event, | ||
192 | Sink::ApplicationDomain::ResourceCapabilities::Event::calendar, | 218 | Sink::ApplicationDomain::ResourceCapabilities::Event::calendar, |
219 | Sink::ApplicationDomain::ResourceCapabilities::Event::event, | ||
193 | Sink::ApplicationDomain::ResourceCapabilities::Event::storage, | 220 | Sink::ApplicationDomain::ResourceCapabilities::Event::storage, |
221 | Sink::ApplicationDomain::ResourceCapabilities::Todo::todo, | ||
222 | Sink::ApplicationDomain::ResourceCapabilities::Todo::storage, | ||
194 | }) | 223 | }) |
195 | {} | 224 | { |
225 | } | ||
196 | 226 | ||
197 | Sink::Resource *CalDavResourceFactory::createResource(const Sink::ResourceContext &context) | 227 | Sink::Resource *CalDavResourceFactory::createResource(const Sink::ResourceContext &context) |
198 | { | 228 | { |
@@ -201,10 +231,12 @@ Sink::Resource *CalDavResourceFactory::createResource(const Sink::ResourceContex | |||
201 | 231 | ||
202 | using Sink::ApplicationDomain::Calendar; | 232 | using Sink::ApplicationDomain::Calendar; |
203 | using Sink::ApplicationDomain::Event; | 233 | using Sink::ApplicationDomain::Event; |
234 | using Sink::ApplicationDomain::Todo; | ||
204 | 235 | ||
205 | void CalDavResourceFactory::registerFacades(const QByteArray &resourceName, Sink::FacadeFactory &factory) | 236 | void CalDavResourceFactory::registerFacades(const QByteArray &resourceName, Sink::FacadeFactory &factory) |
206 | { | 237 | { |
207 | factory.registerFacade<Event, Sink::DefaultFacade<Event>>(resourceName); | 238 | factory.registerFacade<Event, Sink::DefaultFacade<Event>>(resourceName); |
239 | factory.registerFacade<Todo, Sink::DefaultFacade<Todo>>(resourceName); | ||
208 | factory.registerFacade<Calendar, Sink::DefaultFacade<Calendar>>(resourceName); | 240 | factory.registerFacade<Calendar, Sink::DefaultFacade<Calendar>>(resourceName); |
209 | } | 241 | } |
210 | 242 | ||
@@ -213,6 +245,7 @@ void CalDavResourceFactory::registerAdaptorFactories( | |||
213 | const QByteArray &resourceName, Sink::AdaptorFactoryRegistry ®istry) | 245 | const QByteArray &resourceName, Sink::AdaptorFactoryRegistry ®istry) |
214 | { | 246 | { |
215 | registry.registerFactory<Event, DefaultAdaptorFactory<Event>>(resourceName); | 247 | registry.registerFactory<Event, DefaultAdaptorFactory<Event>>(resourceName); |
248 | registry.registerFactory<Todo, DefaultAdaptorFactory<Todo>>(resourceName); | ||
216 | registry.registerFactory<Calendar, DefaultAdaptorFactory<Calendar>>(resourceName); | 249 | registry.registerFactory<Calendar, DefaultAdaptorFactory<Calendar>>(resourceName); |
217 | } | 250 | } |
218 | 251 | ||