summaryrefslogtreecommitdiffstats
path: root/examples/davresource/davresource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/davresource/davresource.cpp')
-rw-r--r--examples/davresource/davresource.cpp47
1 files changed, 20 insertions, 27 deletions
diff --git a/examples/davresource/davresource.cpp b/examples/davresource/davresource.cpp
index 50471ed..465220f 100644
--- a/examples/davresource/davresource.cpp
+++ b/examples/davresource/davresource.cpp
@@ -31,19 +31,17 @@
31 31
32#include "contactpreprocessor.h" 32#include "contactpreprocessor.h"
33 33
34#include <KDAV/DavCollection> 34#include <KDAV2/DavCollection>
35#include <KDAV/DavCollectionsFetchJob> 35#include <KDAV2/DavCollectionsFetchJob>
36#include <KDAV/DavItem> 36#include <KDAV2/DavItem>
37#include <KDAV/DavItemsListJob> 37#include <KDAV2/DavItemsListJob>
38#include <KDAV/DavItemFetchJob> 38#include <KDAV2/DavItemFetchJob>
39#include <KDAV/EtagCache> 39#include <KDAV2/EtagCache>
40 40
41//This is the resources entity type, and not the domain type 41//This is the resources entity type, and not the domain type
42#define ENTITY_TYPE_CONTACT "contact" 42#define ENTITY_TYPE_CONTACT "contact"
43#define ENTITY_TYPE_ADDRESSBOOK "addressbook" 43#define ENTITY_TYPE_ADDRESSBOOK "addressbook"
44 44
45SINK_DEBUG_AREA("davresource")
46
47using namespace Sink; 45using namespace Sink;
48 46
49static KAsync::Job<void> runJob(KJob *job) 47static KAsync::Job<void> runJob(KJob *job)
@@ -87,7 +85,7 @@ public:
87 return remoteId; 85 return remoteId;
88 } 86 }
89 87
90 void synchronizeAddressbooks(const KDAV::DavCollection::List &addressbookList) 88 void synchronizeAddressbooks(const KDAV2::DavCollection::List &addressbookList)
91 { 89 {
92 const QByteArray bufferType = ENTITY_TYPE_ADDRESSBOOK; 90 const QByteArray bufferType = ENTITY_TYPE_ADDRESSBOOK;
93 SinkTrace() << "Found addressbooks " << addressbookList.size(); 91 SinkTrace() << "Found addressbooks " << addressbookList.size();
@@ -121,12 +119,12 @@ public:
121 return list; 119 return list;
122 } 120 }
123 121
124 static QByteArray getRid(const KDAV::DavItem &item) 122 static QByteArray getRid(const KDAV2::DavItem &item)
125 { 123 {
126 return item.url().toDisplayString().toUtf8(); 124 return item.url().toDisplayString().toUtf8();
127 } 125 }
128 126
129 static QByteArray getRid(const KDAV::DavCollection &item) 127 static QByteArray getRid(const KDAV2::DavCollection &item)
130 { 128 {
131 return item.url().toDisplayString().toUtf8(); 129 return item.url().toDisplayString().toUtf8();
132 } 130 }
@@ -135,7 +133,7 @@ public:
135 { 133 {
136 if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>()) { 134 if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>()) {
137 SinkLogCtx(mLogCtx) << "Synchronizing addressbooks:" << mResourceUrl.url(); 135 SinkLogCtx(mLogCtx) << "Synchronizing addressbooks:" << mResourceUrl.url();
138 auto collectionsFetchJob = new KDAV::DavCollectionsFetchJob(mResourceUrl); 136 auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(mResourceUrl);
139 auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] (const KAsync::Error &error) { 137 auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] (const KAsync::Error &error) {
140 if (error) { 138 if (error) {
141 SinkWarningCtx(mLogCtx) << "Failed to synchronize addressbooks." << collectionsFetchJob->errorString(); 139 SinkWarningCtx(mLogCtx) << "Failed to synchronize addressbooks." << collectionsFetchJob->errorString();
@@ -147,29 +145,29 @@ public:
147 } else if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Contact>()) { 145 } else if (query.type() == ApplicationDomain::getTypeName<ApplicationDomain::Contact>()) {
148 SinkLogCtx(mLogCtx) << "Synchronizing contacts."; 146 SinkLogCtx(mLogCtx) << "Synchronizing contacts.";
149 auto ridList = QSharedPointer<QByteArrayList>::create(); 147 auto ridList = QSharedPointer<QByteArrayList>::create();
150 auto collectionsFetchJob = new KDAV::DavCollectionsFetchJob(mResourceUrl); 148 auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob(mResourceUrl);
151 auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] { 149 auto job = runJob(collectionsFetchJob).then([this, collectionsFetchJob] {
152 synchronizeAddressbooks(collectionsFetchJob ->collections()); 150 synchronizeAddressbooks(collectionsFetchJob ->collections());
153 return collectionsFetchJob->collections(); 151 return collectionsFetchJob->collections();
154 }) 152 })
155 .serialEach([this, ridList](const KDAV::DavCollection &collection) { 153 .serialEach([this, ridList](const KDAV2::DavCollection &collection) {
156 auto collId = getRid(collection); 154 auto collId = getRid(collection);
157 const auto addressbookLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, collId); 155 const auto addressbookLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, collId);
158 auto ctag = collection.CTag().toLatin1(); 156 auto ctag = collection.CTag().toLatin1();
159 if (ctag != syncStore().readValue(collId + "_ctagXX")) { 157 if (ctag != syncStore().readValue(collId + "_ctagXX")) {
160 SinkTraceCtx(mLogCtx) << "Syncing " << collId; 158 SinkTraceCtx(mLogCtx) << "Syncing " << collId;
161 auto cache = std::shared_ptr<KDAV::EtagCache>(new KDAV::EtagCache()); 159 auto cache = std::shared_ptr<KDAV2::EtagCache>(new KDAV2::EtagCache());
162 auto davItemsListJob = new KDAV::DavItemsListJob(collection.url(), cache); 160 auto davItemsListJob = new KDAV2::DavItemsListJob(collection.url(), cache);
163 const QByteArray bufferType = ENTITY_TYPE_CONTACT; 161 const QByteArray bufferType = ENTITY_TYPE_CONTACT;
164 QHash<QByteArray, Query::Comparator> mergeCriteria; 162 QHash<QByteArray, Query::Comparator> mergeCriteria;
165 auto colljob = runJob(davItemsListJob).then([davItemsListJob] { 163 auto colljob = runJob(davItemsListJob).then([davItemsListJob] {
166 return KAsync::value(davItemsListJob->items()); 164 return KAsync::value(davItemsListJob->items());
167 }) 165 })
168 .serialEach([=] (const KDAV::DavItem &item) { 166 .serialEach([=] (const KDAV2::DavItem &item) {
169 QByteArray rid = getRid(item); 167 QByteArray rid = getRid(item);
170 if (item.etag().toLatin1() != syncStore().readValue(rid + "_etag")){ 168 if (item.etag().toLatin1() != syncStore().readValue(rid + "_etag")){
171 SinkTrace() << "Updating " << rid; 169 SinkTrace() << "Updating " << rid;
172 auto davItemFetchJob = new KDAV::DavItemFetchJob(item); 170 auto davItemFetchJob = new KDAV2::DavItemFetchJob(item);
173 auto itemjob = runJob(davItemFetchJob) 171 auto itemjob = runJob(davItemFetchJob)
174 .then([=] { 172 .then([=] {
175 const auto item = davItemFetchJob->item(); 173 const auto item = davItemFetchJob->item();
@@ -180,7 +178,7 @@ public:
180 createOrModify(bufferType, rid, contact, mergeCriteria); 178 createOrModify(bufferType, rid, contact, mergeCriteria);
181 return item; 179 return item;
182 }) 180 })
183 .then([this, ridList] (const KDAV::DavItem &item) { 181 .then([this, ridList] (const KDAV2::DavItem &item) {
184 const auto rid = getRid(item); 182 const auto rid = getRid(item);
185 syncStore().writeValue(rid + "_etag", item.etag().toLatin1()); 183 syncStore().writeValue(rid + "_etag", item.etag().toLatin1());
186 ridList->append(rid); 184 ridList->append(rid);
@@ -227,24 +225,19 @@ KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink::
227 } 225 }
228 226
229public: 227public:
230 KDAV::DavUrl mResourceUrl; 228 KDAV2::DavUrl mResourceUrl;
231}; 229};
232 230
233 231
234DavResource::DavResource(const Sink::ResourceContext &resourceContext) 232DavResource::DavResource(const Sink::ResourceContext &resourceContext)
235 : Sink::GenericResource(resourceContext) 233 : Sink::GenericResource(resourceContext)
236{ 234{
237 /*
238 * Fork KIO slaves (used in kdav), instead of starting them via klauncher.
239 * Otherwise we have yet another runtime dependency that will i.e. not work in the docker container.
240 */
241 qputenv("KDE_FORK_SLAVES", "TRUE");
242 auto config = ResourceConfig::getConfiguration(resourceContext.instanceId()); 235 auto config = ResourceConfig::getConfiguration(resourceContext.instanceId());
243 auto resourceUrl = QUrl::fromUserInput(config.value("server").toString()); 236 auto resourceUrl = QUrl::fromUserInput(config.value("server").toString());
244 resourceUrl.setUserName(config.value("username").toString()); 237 resourceUrl.setUserName(config.value("username").toString());
245 resourceUrl.setPassword(config.value("password").toString()); 238 resourceUrl.setPassword(config.value("password").toString());
246 239
247 mResourceUrl = KDAV::DavUrl(resourceUrl, KDAV::CardDav); 240 mResourceUrl = KDAV2::DavUrl(resourceUrl, KDAV2::CardDav);
248 241
249 auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext); 242 auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext);
250 synchronizer->mResourceUrl = mResourceUrl; 243 synchronizer->mResourceUrl = mResourceUrl;
@@ -271,7 +264,7 @@ Sink::Resource *DavResourceFactory::createResource(const ResourceContext &contex
271void DavResourceFactory::registerFacades(const QByteArray &name, Sink::FacadeFactory &factory) 264void DavResourceFactory::registerFacades(const QByteArray &name, Sink::FacadeFactory &factory)
272{ 265{
273 factory.registerFacade<ApplicationDomain::Contact, DefaultFacade<ApplicationDomain::Contact>>(name); 266 factory.registerFacade<ApplicationDomain::Contact, DefaultFacade<ApplicationDomain::Contact>>(name);
274 factory.registerFacade<ApplicationDomain::Addressbook, DefaultFacade<ApplicationDomain::Contact>>(name); 267 factory.registerFacade<ApplicationDomain::Addressbook, DefaultFacade<ApplicationDomain::Addressbook>>(name);
275} 268}
276 269
277void DavResourceFactory::registerAdaptorFactories(const QByteArray &name, Sink::AdaptorFactoryRegistry &registry) 270void DavResourceFactory::registerAdaptorFactories(const QByteArray &name, Sink::AdaptorFactoryRegistry &registry)