diff options
author | Rémi Nicole <nicole@kolabsystems.com> | 2018-03-27 18:26:11 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-03-27 18:26:15 +0200 |
commit | 761328989492db9bd603c2d7f1134d20e485d2f6 (patch) | |
tree | 0e3b4517dd2000fb1cc2738bbb22a3e54dfffb6f /examples/caldavresource | |
parent | 80afd7070f2d8e57cab2fe55fef611623fdb75f0 (diff) | |
download | sink-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')
-rw-r--r-- | examples/caldavresource/CMakeLists.txt | 15 | ||||
-rw-r--r-- | examples/caldavresource/caldavresource.cpp | 155 | ||||
-rw-r--r-- | examples/caldavresource/caldavresource.h | 46 | ||||
-rw-r--r-- | examples/caldavresource/tests/CMakeLists.txt | 9 | ||||
-rw-r--r-- | examples/caldavresource/tests/caldavtest.cpp | 93 |
5 files changed, 318 insertions, 0 deletions
diff --git a/examples/caldavresource/CMakeLists.txt b/examples/caldavresource/CMakeLists.txt new file mode 100644 index 0000000..0057e8b --- /dev/null +++ b/examples/caldavresource/CMakeLists.txt | |||
@@ -0,0 +1,15 @@ | |||
1 | project(sink_resource_caldav) | ||
2 | |||
3 | add_definitions(-DQT_PLUGIN) | ||
4 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
5 | |||
6 | find_package(KPimKDAV2 REQUIRED) | ||
7 | find_package(KF5CalendarCore REQUIRED) | ||
8 | |||
9 | add_library(${PROJECT_NAME} SHARED caldavresource.cpp) | ||
10 | target_link_libraries(${PROJECT_NAME} sink_webdav_common sink Qt5::Core Qt5::Network KPim::KDAV2 | ||
11 | KF5::CalendarCore) | ||
12 | |||
13 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${SINK_RESOURCE_PLUGINS_PATH}) | ||
14 | |||
15 | add_subdirectory(tests) | ||
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 | } | ||
diff --git a/examples/caldavresource/caldavresource.h b/examples/caldavresource/caldavresource.h new file mode 100644 index 0000000..5822495 --- /dev/null +++ b/examples/caldavresource/caldavresource.h | |||
@@ -0,0 +1,46 @@ | |||
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 | #pragma once | ||
21 | |||
22 | #include "common/genericresource.h" | ||
23 | |||
24 | /** | ||
25 | * A CalDAV resource. | ||
26 | */ | ||
27 | class CalDavResource : public Sink::GenericResource | ||
28 | { | ||
29 | public: | ||
30 | CalDavResource(const Sink::ResourceContext &); | ||
31 | }; | ||
32 | |||
33 | class CalDavResourceFactory : public Sink::ResourceFactory | ||
34 | { | ||
35 | Q_OBJECT | ||
36 | Q_PLUGIN_METADATA(IID "sink.caldav") | ||
37 | Q_INTERFACES(Sink::ResourceFactory) | ||
38 | |||
39 | public: | ||
40 | CalDavResourceFactory(QObject *parent = nullptr); | ||
41 | |||
42 | Sink::Resource *createResource(const Sink::ResourceContext &context) Q_DECL_OVERRIDE; | ||
43 | void registerFacades(const QByteArray &resourceName, Sink::FacadeFactory &factory) Q_DECL_OVERRIDE; | ||
44 | void registerAdaptorFactories(const QByteArray &resourceName, Sink::AdaptorFactoryRegistry ®istry) Q_DECL_OVERRIDE; | ||
45 | void removeDataFromDisk(const QByteArray &instanceIdentifier) Q_DECL_OVERRIDE; | ||
46 | }; | ||
diff --git a/examples/caldavresource/tests/CMakeLists.txt b/examples/caldavresource/tests/CMakeLists.txt new file mode 100644 index 0000000..d2f9b50 --- /dev/null +++ b/examples/caldavresource/tests/CMakeLists.txt | |||
@@ -0,0 +1,9 @@ | |||
1 | set(CMAKE_AUTOMOC ON) | ||
2 | include_directories(${CMAKE_BINARY_DIR}) | ||
3 | |||
4 | include(SinkTest) | ||
5 | |||
6 | auto_tests ( | ||
7 | caldavtest | ||
8 | ) | ||
9 | target_link_libraries(caldavtest sink_resource_caldav) | ||
diff --git a/examples/caldavresource/tests/caldavtest.cpp b/examples/caldavresource/tests/caldavtest.cpp new file mode 100644 index 0000000..f999590 --- /dev/null +++ b/examples/caldavresource/tests/caldavtest.cpp | |||
@@ -0,0 +1,93 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include "../caldavresource.h" | ||
4 | |||
5 | #include "common/resourcecontrol.h" | ||
6 | #include "common/secretstore.h" | ||
7 | #include "common/store.h" | ||
8 | #include "common/test.h" | ||
9 | #include "tests/testutils.h" | ||
10 | |||
11 | using Sink::ApplicationDomain::Calendar; | ||
12 | using Sink::ApplicationDomain::DummyResource; | ||
13 | using Sink::ApplicationDomain::Event; | ||
14 | using Sink::ApplicationDomain::SinkResource; | ||
15 | |||
16 | class CalDavTest : public QObject | ||
17 | { | ||
18 | Q_OBJECT | ||
19 | |||
20 | SinkResource createResource() | ||
21 | { | ||
22 | auto resource = Sink::ApplicationDomain::CalDavResource::create("account1"); | ||
23 | resource.setProperty("server", "http://localhost/dav/calendars/users/doe"); | ||
24 | resource.setProperty("username", "doe"); | ||
25 | Sink::SecretStore::instance().insert(resource.identifier(), "doe"); | ||
26 | resource.setProperty("testmode", true); | ||
27 | return resource; | ||
28 | } | ||
29 | |||
30 | |||
31 | QByteArray mResourceInstanceIdentifier; | ||
32 | QByteArray mStorageResource; | ||
33 | |||
34 | private slots: | ||
35 | |||
36 | void initTestCase() | ||
37 | { | ||
38 | Sink::Test::initTest(); | ||
39 | auto resource = createResource(); | ||
40 | QVERIFY(!resource.identifier().isEmpty()); | ||
41 | VERIFYEXEC(Sink::Store::create(resource)); | ||
42 | mResourceInstanceIdentifier = resource.identifier(); | ||
43 | |||
44 | auto dummyResource = DummyResource::create("account1"); | ||
45 | VERIFYEXEC(Sink::Store::create(dummyResource)); | ||
46 | mStorageResource = dummyResource.identifier(); | ||
47 | QVERIFY(!mStorageResource.isEmpty()); | ||
48 | } | ||
49 | |||
50 | void cleanup() | ||
51 | { | ||
52 | VERIFYEXEC(Sink::Store::removeDataFromDisk(mResourceInstanceIdentifier)); | ||
53 | VERIFYEXEC(Sink::Store::removeDataFromDisk(mStorageResource)); | ||
54 | } | ||
55 | |||
56 | void init() | ||
57 | { | ||
58 | VERIFYEXEC(Sink::ResourceControl::start(mResourceInstanceIdentifier)); | ||
59 | } | ||
60 | |||
61 | void testSyncCal() | ||
62 | { | ||
63 | VERIFYEXEC(Sink::Store::synchronize(Sink::Query().resourceFilter(mResourceInstanceIdentifier))); | ||
64 | // Check in the logs that it doesn't synchronize events again because same CTag | ||
65 | VERIFYEXEC(Sink::Store::synchronize(Sink::Query().resourceFilter(mResourceInstanceIdentifier))); | ||
66 | } | ||
67 | |||
68 | void testSyncCalEmpty() | ||
69 | { | ||
70 | VERIFYEXEC(Sink::Store::synchronize(Sink::Query().resourceFilter(mResourceInstanceIdentifier))); | ||
71 | |||
72 | auto eventJob = | ||
73 | Sink::Store::fetchAll<Event>(Sink::Query().request<Event::Uid>()).then([](const QList<Event::Ptr> &events) { | ||
74 | QCOMPARE(events.size(), 14); | ||
75 | }); | ||
76 | VERIFYEXEC(eventJob); | ||
77 | |||
78 | auto calendarJob = | ||
79 | Sink::Store::fetchAll<Calendar>(Sink::Query().request<Calendar::Name>()).then([](const QList<Calendar::Ptr> &calendars) { | ||
80 | QCOMPARE(calendars.size(), 2); | ||
81 | for (const auto &calendar : calendars) { | ||
82 | QVERIFY(calendar->getName() == "Calendar" || calendar->getName() == "Tasks"); | ||
83 | } | ||
84 | }); | ||
85 | VERIFYEXEC(calendarJob); | ||
86 | |||
87 | SinkLog() << "Finished"; | ||
88 | } | ||
89 | }; | ||
90 | |||
91 | QTEST_MAIN(CalDavTest) | ||
92 | |||
93 | #include "caldavtest.moc" | ||