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
|
/*
* Copyright (C) 2015 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 "carddavresource.h"
#include "../webdavcommon/webdav.h"
#include "facade.h"
#include "resourceconfig.h"
#include "log.h"
#include "definitions.h"
#include "synchronizer.h"
#include "inspector.h"
#include "facadefactory.h"
#include "adaptorfactoryregistry.h"
#include "contactpreprocessor.h"
//This is the resources entity type, and not the domain type
#define ENTITY_TYPE_CONTACT "contact"
#define ENTITY_TYPE_ADDRESSBOOK "addressbook"
using namespace Sink;
class ContactSynchronizer : public WebDavSynchronizer
{
public:
ContactSynchronizer(const Sink::ResourceContext &resourceContext)
: WebDavSynchronizer(resourceContext, KDAV2::CardDav,
ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>(),
ApplicationDomain::getTypeName<ApplicationDomain::Contact>())
{}
QByteArray createAddressbook(const QString &addressbookName, const QString &addressbookPath, const QString &parentAddressbookRid)
{
SinkTrace() << "Creating addressbook: " << addressbookName << parentAddressbookRid;
const auto remoteId = addressbookPath.toUtf8();
const auto bufferType = ENTITY_TYPE_ADDRESSBOOK;
Sink::ApplicationDomain::Addressbook addressbook;
addressbook.setName(addressbookName);
QHash<QByteArray, Query::Comparator> mergeCriteria;
if (!parentAddressbookRid.isEmpty()) {
addressbook.setParent(syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, parentAddressbookRid.toUtf8()));
}
createOrModify(bufferType, remoteId, addressbook, mergeCriteria);
return remoteId;
}
protected:
void updateLocalCollections(KDAV2::DavCollection::List addressbookList) Q_DECL_OVERRIDE
{
const QByteArray bufferType = ENTITY_TYPE_ADDRESSBOOK;
SinkTrace() << "Found" << addressbookList.size() << "addressbooks";
for (const auto &f : addressbookList) {
const auto &rid = resourceID(f);
SinkLog() << "Found addressbook:" << rid << f.displayName();
createAddressbook(f.displayName(), rid, "");
}
}
void updateLocalItem(KDAV2::DavItem remoteContact, const QByteArray &addressbookLocalId) Q_DECL_OVERRIDE
{
Sink::ApplicationDomain::Contact localContact;
localContact.setVcard(remoteContact.data());
localContact.setAddressbook(addressbookLocalId);
QHash<QByteArray, Query::Comparator> mergeCriteria;
createOrModify(ENTITY_TYPE_CONTACT, resourceID(remoteContact), localContact, mergeCriteria);
}
QByteArray collectionLocalResourceID(const KDAV2::DavCollection &addressbook) Q_DECL_OVERRIDE
{
return syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, resourceID(addressbook));
}
KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE
{
SinkLog() << "Replaying to:" << serverUrl().url();
switch (operation) {
case Sink::Operation_Creation: {
const auto vcard = contact.getVcard();
if (vcard.isEmpty()) {
return KAsync::error<QByteArray>("No vcard in item for creation replay.");
}
auto collectionId = syncStore().resolveLocalId(ENTITY_TYPE_ADDRESSBOOK, contact.getAddressbook());
KDAV2::DavItem remoteItem;
remoteItem.setData(vcard);
remoteItem.setContentType("text/vcard");
remoteItem.setUrl(urlOf(collectionId, contact.getUid()));
SinkLog() << "Creating:" << contact.getUid() << remoteItem.url().url() << vcard;
return createItem(remoteItem).then([=] { return resourceID(remoteItem); });
}
case Sink::Operation_Removal: {
// We only need the URL in the DAV item for removal
KDAV2::DavItem remoteItem;
remoteItem.setUrl(urlOf(oldRemoteId));
SinkLog() << "Removing:" << oldRemoteId;
return removeItem(remoteItem).then([] { return QByteArray{}; });
}
case Sink::Operation_Modification:
const auto vcard = contact.getVcard();
if (vcard.isEmpty()) {
return KAsync::error<QByteArray>("No ICal in item for modification replay");
}
KDAV2::DavItem remoteItem;
remoteItem.setData(vcard);
remoteItem.setContentType("text/vcard");
remoteItem.setUrl(urlOf(oldRemoteId));
return modifyItem(remoteItem).then([=] { return oldRemoteId; });
}
}
KAsync::Job<QByteArray> replay(const ApplicationDomain::Addressbook &addressbook, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE
{
return KAsync::null<QByteArray>();
}
};
CardDavResource::CardDavResource(const Sink::ResourceContext &resourceContext)
: Sink::GenericResource(resourceContext)
{
auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext);
setupSynchronizer(synchronizer);
setupPreprocessors(ENTITY_TYPE_CONTACT, QVector<Sink::Preprocessor*>() << new ContactPropertyExtractor);
}
CardDavResourceFactory::CardDavResourceFactory(QObject *parent)
: Sink::ResourceFactory(parent,
{Sink::ApplicationDomain::ResourceCapabilities::Contact::contact,
Sink::ApplicationDomain::ResourceCapabilities::Contact::addressbook,
Sink::ApplicationDomain::ResourceCapabilities::Contact::storage
}
)
{
}
Sink::Resource *CardDavResourceFactory::createResource(const ResourceContext &context)
{
return new CardDavResource(context);
}
void CardDavResourceFactory::registerFacades(const QByteArray &name, Sink::FacadeFactory &factory)
{
factory.registerFacade<ApplicationDomain::Contact, DefaultFacade<ApplicationDomain::Contact>>(name);
factory.registerFacade<ApplicationDomain::Addressbook, DefaultFacade<ApplicationDomain::Addressbook>>(name);
}
void CardDavResourceFactory::registerAdaptorFactories(const QByteArray &name, Sink::AdaptorFactoryRegistry ®istry)
{
registry.registerFactory<ApplicationDomain::Contact, DefaultAdaptorFactory<ApplicationDomain::Contact>>(name);
registry.registerFactory<ApplicationDomain::Addressbook, DefaultAdaptorFactory<ApplicationDomain::Addressbook>>(name);
}
void CardDavResourceFactory::removeDataFromDisk(const QByteArray &instanceIdentifier)
{
CardDavResource::removeFromDisk(instanceIdentifier);
}
|