diff options
-rw-r--r-- | examples/carddavresource/carddavresource.cpp | 39 | ||||
-rw-r--r-- | examples/carddavresource/tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | examples/carddavresource/tests/carddavtest.cpp | 48 |
3 files changed, 85 insertions, 4 deletions
diff --git a/examples/carddavresource/carddavresource.cpp b/examples/carddavresource/carddavresource.cpp index fc2b946..0bf851b 100644 --- a/examples/carddavresource/carddavresource.cpp +++ b/examples/carddavresource/carddavresource.cpp | |||
@@ -94,7 +94,44 @@ protected: | |||
94 | 94 | ||
95 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | 95 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE |
96 | { | 96 | { |
97 | return KAsync::null<QByteArray>(); | 97 | SinkLog() << "Replaying to:" << serverUrl().url(); |
98 | switch (operation) { | ||
99 | case Sink::Operation_Creation: { | ||
100 | const auto vcard = contact.getVcard(); | ||
101 | if (vcard.isEmpty()) { | ||
102 | return KAsync::error<QByteArray>("No vcard in item for creation replay."); | ||
103 | } | ||
104 | |||
105 | auto collectionId = syncStore().resolveLocalId(ENTITY_TYPE_ADDRESSBOOK, contact.getAddressbook()); | ||
106 | |||
107 | KDAV2::DavItem remoteItem; | ||
108 | remoteItem.setData(vcard); | ||
109 | remoteItem.setContentType("text/vcard"); | ||
110 | remoteItem.setUrl(urlOf(collectionId, contact.getUid())); | ||
111 | SinkLog() << "Creating:" << contact.getUid() << remoteItem.url().url() << vcard; | ||
112 | return createItem(remoteItem).then([=] { return resourceID(remoteItem); }); | ||
113 | } | ||
114 | case Sink::Operation_Removal: { | ||
115 | // We only need the URL in the DAV item for removal | ||
116 | KDAV2::DavItem remoteItem; | ||
117 | remoteItem.setUrl(urlOf(oldRemoteId)); | ||
118 | |||
119 | SinkLog() << "Removing:" << oldRemoteId; | ||
120 | return removeItem(remoteItem).then([] { return QByteArray{}; }); | ||
121 | } | ||
122 | case Sink::Operation_Modification: | ||
123 | const auto vcard = contact.getVcard(); | ||
124 | if (vcard.isEmpty()) { | ||
125 | return KAsync::error<QByteArray>("No ICal in item for modification replay"); | ||
126 | } | ||
127 | |||
128 | KDAV2::DavItem remoteItem; | ||
129 | remoteItem.setData(vcard); | ||
130 | remoteItem.setContentType("text/vcard"); | ||
131 | remoteItem.setUrl(urlOf(oldRemoteId)); | ||
132 | |||
133 | return modifyItem(remoteItem).then([=] { return oldRemoteId; }); | ||
134 | } | ||
98 | } | 135 | } |
99 | 136 | ||
100 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Addressbook &addressbook, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | 137 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Addressbook &addressbook, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE |
diff --git a/examples/carddavresource/tests/CMakeLists.txt b/examples/carddavresource/tests/CMakeLists.txt index e7e8c18..e59665e 100644 --- a/examples/carddavresource/tests/CMakeLists.txt +++ b/examples/carddavresource/tests/CMakeLists.txt | |||
@@ -6,4 +6,4 @@ include(SinkTest) | |||
6 | auto_tests ( | 6 | auto_tests ( |
7 | carddavtest | 7 | carddavtest |
8 | ) | 8 | ) |
9 | target_link_libraries(carddavtest sink_resource_carddav) | 9 | target_link_libraries(carddavtest sink_resource_carddav KF5::Contacts) |
diff --git a/examples/carddavresource/tests/carddavtest.cpp b/examples/carddavresource/tests/carddavtest.cpp index 6e7cf01..b0f41d1 100644 --- a/examples/carddavresource/tests/carddavtest.cpp +++ b/examples/carddavresource/tests/carddavtest.cpp | |||
@@ -12,10 +12,12 @@ | |||
12 | #include <KDAV2/DavItemCreateJob> | 12 | #include <KDAV2/DavItemCreateJob> |
13 | #include <KDAV2/DavCollectionsFetchJob> | 13 | #include <KDAV2/DavCollectionsFetchJob> |
14 | #include <KDAV2/DavCollection> | 14 | #include <KDAV2/DavCollection> |
15 | #include <KContacts/Addressee> | ||
16 | #include <KContacts/VCardConverter> | ||
15 | 17 | ||
16 | 18 | ||
17 | using Sink::ApplicationDomain::Calendar; | 19 | using Sink::ApplicationDomain::Addressbook; |
18 | using Sink::ApplicationDomain::Event; | 20 | using Sink::ApplicationDomain::Contact; |
19 | using Sink::ApplicationDomain::SinkResource; | 21 | using Sink::ApplicationDomain::SinkResource; |
20 | 22 | ||
21 | class CardDavTest : public QObject | 23 | class CardDavTest : public QObject |
@@ -109,6 +111,48 @@ private slots: | |||
109 | QCOMPARE(contacts.size(), 2); | 111 | QCOMPARE(contacts.size(), 2); |
110 | } | 112 | } |
111 | } | 113 | } |
114 | |||
115 | void testSyncAddressbooks() | ||
116 | { | ||
117 | Sink::SyncScope scope; | ||
118 | scope.setType<Addressbook>(); | ||
119 | scope.resourceFilter(mResourceInstanceIdentifier); | ||
120 | |||
121 | VERIFYEXEC(Sink::Store::synchronize(scope)); | ||
122 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); | ||
123 | const auto addressbooks = Sink::Store::read<Addressbook>(Sink::Query().resourceFilter(mResourceInstanceIdentifier)); | ||
124 | QCOMPARE(addressbooks.size(), 1); | ||
125 | } | ||
126 | |||
127 | void testAddContact() | ||
128 | { | ||
129 | Sink::SyncScope scope; | ||
130 | scope.setType<Addressbook>(); | ||
131 | scope.resourceFilter(mResourceInstanceIdentifier); | ||
132 | |||
133 | VERIFYEXEC(Sink::Store::synchronize(scope)); | ||
134 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(mResourceInstanceIdentifier)); | ||
135 | |||
136 | auto addressbooks = Sink::Store::read<Addressbook>(Sink::Query().resourceFilter(mResourceInstanceIdentifier)); | ||
137 | QVERIFY(!addressbooks.isEmpty()); | ||
138 | |||
139 | KContacts::Addressee addressee; | ||
140 | addressee.setGivenName("John"); | ||
141 | addressee.setFamilyName("Doe"); | ||
142 | addressee.setFormattedName("John Doe"); | ||
143 | KContacts::VCardConverter converter; | ||
144 | const auto vcard = converter.createVCard(addressee, KContacts::VCardConverter::v3_0); | ||
145 | |||
146 | Contact contact(mResourceInstanceIdentifier); | ||
147 | contact.setVcard(vcard); | ||
148 | contact.setAddressbook(addressbooks.first()); | ||
149 | |||
150 | VERIFYEXEC(Sink::Store::create(contact)); | ||
151 | VERIFYEXEC(Sink::ResourceControl::flushReplayQueue(mResourceInstanceIdentifier)); | ||
152 | |||
153 | auto contacts = Sink::Store::read<Contact>({}); | ||
154 | QVERIFY(!contacts.isEmpty()); | ||
155 | } | ||
112 | }; | 156 | }; |
113 | 157 | ||
114 | QTEST_MAIN(CardDavTest) | 158 | QTEST_MAIN(CardDavTest) |