1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* Copyright (C) 2018 Christian Mollekopf <chrigi_1@fastmail.fm>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "webdav.h"
#include "applicationdomaintype.h"
#include "resourceconfig.h"
#include <KDAV2/DavCollectionDeleteJob>
#include <KDAV2/DavCollectionModifyJob>
#include <KDAV2/DavCollectionsFetchJob>
#include <KDAV2/DavItemCreateJob>
#include <KDAV2/DavItemDeleteJob>
#include <KDAV2/DavItemFetchJob>
#include <KDAV2/DavItemModifyJob>
#include <KDAV2/DavItemsListJob>
#include <KDAV2/EtagCache>
#include <QNetworkReply>
static int translateDavError(KJob *job)
{
using Sink::ApplicationDomain::ErrorCode;
const int responseCode = dynamic_cast<KDAV2::DavJobBase *>(job)->latestResponseCode();
switch (responseCode) {
case QNetworkReply::HostNotFoundError:
return ErrorCode::NoServerError;
// Since we don't login we will just not have the necessary permissions ot view the object
case QNetworkReply::OperationCanceledError:
return ErrorCode::LoginError;
}
return ErrorCode::UnknownError;
}
static KAsync::Job<void> runJob(KJob *job)
{
return KAsync::start<void>([job](KAsync::Future<void> &future) {
QObject::connect(job, &KJob::result, [&future](KJob *job) {
SinkTrace() << "Job done: " << job->metaObject()->className();
if (job->error()) {
SinkWarning() << "Job failed: " << job->errorString() << job->metaObject()->className() << job->error();
auto proxyError = translateDavError(job);
future.setError(proxyError, job->errorString());
} else {
future.setFinished();
}
});
SinkTrace() << "Starting job: " << job->metaObject()->className();
job->start();
});
}
template <typename T>
static KAsync::Job<T> runJob(KJob *job, const std::function<T(KJob *)> &func)
{
return KAsync::start<T>([job, func](KAsync::Future<T> &future) {
QObject::connect(job, &KJob::result, [&future, func](KJob *job) {
SinkTrace() << "Job done: " << job->metaObject()->className();
if (job->error()) {
SinkWarning() << "Job failed: " << job->errorString() << job->metaObject()->className() << job->error();
auto proxyError = translateDavError(job);
future.setError(proxyError, job->errorString());
} else {
future.setValue(func(job));
future.setFinished();
}
});
SinkTrace() << "Starting job: " << job->metaObject()->className();
job->start();
});
}
WebDavSynchronizer::WebDavSynchronizer(const Sink::ResourceContext &context,
KDAV2::Protocol protocol, QByteArray mCollectionType, QByteArray mEntityType)
: Sink::Synchronizer(context),
protocol(protocol),
mCollectionType(std::move(mCollectionType)),
mEntityType(std::move(mEntityType))
{
auto config = ResourceConfig::getConfiguration(context.instanceId());
mServer = QUrl::fromUserInput(config.value("server").toString());
mUsername = config.value("username").toString();
}
QList<Sink::Synchronizer::SyncRequest> WebDavSynchronizer::getSyncRequests(const Sink::QueryBase &query)
{
QList<Synchronizer::SyncRequest> list;
if (!query.type().isEmpty()) {
// We want to synchronize something specific
list << Synchronizer::SyncRequest{ query };
} else {
// We want to synchronize everything
// Item synchronization does the collections anyway
// list << Synchronizer::SyncRequest{ Sink::QueryBase(mCollectionType) };
list << Synchronizer::SyncRequest{ Sink::QueryBase(mEntityType) };
}
return list;
}
KAsync::Job<void> WebDavSynchronizer::synchronizeWithSource(const Sink::QueryBase &query)
{
if (query.type() != mCollectionType && query.type() != mEntityType) {
SinkWarning() << "Received synchronization reuqest with unkown type" << query;
return KAsync::null<void>();
}
SinkLog() << "Synchronizing" << query.type() << "through WebDAV at:" << serverUrl().url();
auto collectionsFetchJob = new KDAV2::DavCollectionsFetchJob{ serverUrl() };
auto job = runJob<KDAV2::DavCollection::List>(collectionsFetchJob,
[](KJob *job) { return static_cast<KDAV2::DavCollectionsFetchJob *>(job)->collections(); })
.then([this](const KDAV2::DavCollection::List &collections) {
updateLocalCollections(collections);
return collections;
});
if (query.type() == mCollectionType) {
// Do nothing more
return job;
} else if (query.type() == mEntityType) {
auto progress = QSharedPointer<int>::create(0);
auto total = QSharedPointer<int>::create(0);
// Will contain the resource Id of all collections to be able to scan
// for collections to be removed.
auto collectionResourceIDs = QSharedPointer<QSet<QByteArray>>::create();
return job
.serialEach([=](const KDAV2::DavCollection &collection) {
auto collectionResourceID = resourceID(collection);
collectionResourceIDs->insert(collectionResourceID);
if (unchanged(collection)) {
SinkTrace() << "Collection unchanged:" << collectionResourceID;
return KAsync::null<void>();
}
SinkTrace() << "Syncing collection:" << collectionResourceID;
auto itemsResourceIDs = QSharedPointer<QSet<QByteArray>>::create();
return synchronizeCollection(collection, progress, total, itemsResourceIDs)
.then([=] {
scanForRemovals(mEntityType, [&itemsResourceIDs](const QByteArray &remoteId) {
return itemsResourceIDs->contains(remoteId);
});
});
})
.then([=]() {
scanForRemovals(mCollectionType, [&collectionResourceIDs](const QByteArray &remoteId) {
return collectionResourceIDs->contains(remoteId);
});
});
} else {
SinkWarning() << "Unknown query type";
return KAsync::null<void>();
}
}
KAsync::Job<void> WebDavSynchronizer::synchronizeCollection(const KDAV2::DavCollection &collection,
QSharedPointer<int> progress, QSharedPointer<int> total,
QSharedPointer<QSet<QByteArray>> itemsResourceIDs)
{
auto collectionRid = resourceID(collection);
auto ctag = collection.CTag().toLatin1();
auto localRid = collectionLocalResourceID(collection);
auto cache = std::make_shared<KDAV2::EtagCache>();
auto davItemsListJob = new KDAV2::DavItemsListJob(collection.url(), std::move(cache));
return runJob<KDAV2::DavItem::List>(davItemsListJob,
[](KJob *job) { return static_cast<KDAV2::DavItemsListJob *>(job)->items(); })
.then([this, total](const KDAV2::DavItem::List &items) {
*total += items.size();
return items;
})
.serialEach([this, collectionRid, localRid, progress(std::move(progress)), total(std::move(total)),
itemsResourceIDs(std::move(itemsResourceIDs))](const KDAV2::DavItem &item) {
auto itemRid = resourceID(item);
itemsResourceIDs->insert(itemRid);
if (unchanged(item)) {
SinkTrace() << "Item unchanged:" << itemRid;
return KAsync::null<void>();
}
SinkTrace() << "Syncing item:" << itemRid;
return synchronizeItem(item, localRid, progress, total);
})
.then([this, collectionRid, ctag] {
// Update the local CTag to be able to tell if the collection is unchanged
syncStore().writeValue(collectionRid + "_ctag", ctag);
});
}
KAsync::Job<void> WebDavSynchronizer::synchronizeItem(const KDAV2::DavItem &item,
const QByteArray &collectionLocalRid, QSharedPointer<int> progress, QSharedPointer<int> total)
{
auto etag = item.etag().toLatin1();
auto itemFetchJob = new KDAV2::DavItemFetchJob(item);
return runJob<KDAV2::DavItem>(
itemFetchJob, [](KJob *job) { return static_cast<KDAV2::DavItemFetchJob *>(job)->item(); })
.then([this, collectionLocalRid](const KDAV2::DavItem &item) {
updateLocalItem(item, collectionLocalRid);
return item;
})
.then([this, etag, progress(std::move(progress)), total(std::move(total))](const KDAV2::DavItem &item) {
// Update the local ETag to be able to tell if the item is unchanged
syncStore().writeValue(resourceID(item) + "_etag", etag);
*progress += 1;
reportProgress(*progress, *total);
if ((*progress % 5) == 0) {
commit();
}
});
}
KAsync::Job<void> WebDavSynchronizer::createItem(const KDAV2::DavItem &item)
{
auto job = new KDAV2::DavItemCreateJob(item);
return runJob(job).then([] { SinkTrace() << "Done creating item"; });
}
KAsync::Job<void> WebDavSynchronizer::removeItem(const KDAV2::DavItem &item)
{
auto job = new KDAV2::DavItemDeleteJob(item);
return runJob(job).then([] { SinkTrace() << "Done removing item"; });
}
KAsync::Job<void> WebDavSynchronizer::modifyItem(const KDAV2::DavItem &item)
{
auto job = new KDAV2::DavItemModifyJob(item);
return runJob(job).then([] { SinkTrace() << "Done modifying item"; });
}
// There is no "DavCollectionCreateJob"
/*
KAsync::Job<void> WebDavSynchronizer::createCollection(const KDAV2::DavCollection &collection)
{
auto job = new KDAV2::DavCollectionCreateJob(collection);
return runJob(job);
}
*/
KAsync::Job<void> WebDavSynchronizer::removeCollection(const KDAV2::DavUrl &url)
{
auto job = new KDAV2::DavCollectionDeleteJob(url);
return runJob(job).then([] { SinkLog() << "Done removing collection"; });
}
// Useless without using the `setProperty` method of DavCollectionModifyJob
/*
KAsync::Job<void> WebDavSynchronizer::modifyCollection(const KDAV2::DavUrl &url)
{
auto job = new KDAV2::DavCollectionModifyJob(url);
return runJob(job).then([] { SinkLog() << "Done modifying collection"; });
}
*/
QByteArray WebDavSynchronizer::resourceID(const KDAV2::DavCollection &collection)
{
return collection.url().url().path().toUtf8();
}
QByteArray WebDavSynchronizer::resourceID(const KDAV2::DavItem &item)
{
return item.url().url().path().toUtf8();
}
KDAV2::DavUrl WebDavSynchronizer::urlOf(const QByteArray &remoteId)
{
auto davurl = serverUrl();
auto url = davurl.url();
url.setPath(remoteId);
davurl.setUrl(url);
return davurl;
}
KDAV2::DavUrl WebDavSynchronizer::urlOf(const QByteArray &collectionRemoteId, const QString &itemPath)
{
return urlOf(collectionRemoteId + itemPath.toUtf8());
}
bool WebDavSynchronizer::unchanged(const KDAV2::DavCollection &collection)
{
auto ctag = collection.CTag().toLatin1();
return ctag == syncStore().readValue(resourceID(collection) + "_ctag");
}
bool WebDavSynchronizer::unchanged(const KDAV2::DavItem &item)
{
auto etag = item.etag().toLatin1();
return etag == syncStore().readValue(resourceID(item) + "_etag");
}
KDAV2::DavUrl WebDavSynchronizer::serverUrl() const
{
if (secret().isEmpty()) {
return {};
}
auto result = mServer;
result.setUserName(mUsername);
result.setPassword(secret());
return KDAV2::DavUrl{ result, protocol };
}
|