Changeset View
Changeset View
Standalone View
Standalone View
examples/carddavresource/carddavresource.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2015 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 "carddavresource.h" | ||||
21 | | ||||
22 | #include "../webdavcommon/webdav.h" | ||||
23 | | ||||
24 | #include "facade.h" | ||||
25 | #include "resourceconfig.h" | ||||
26 | #include "log.h" | ||||
27 | #include "definitions.h" | ||||
28 | #include "synchronizer.h" | ||||
29 | #include "inspector.h" | ||||
30 | | ||||
31 | #include "facadefactory.h" | ||||
32 | #include "adaptorfactoryregistry.h" | ||||
33 | | ||||
34 | #include "contactpreprocessor.h" | ||||
35 | | ||||
36 | //This is the resources entity type, and not the domain type | ||||
37 | #define ENTITY_TYPE_CONTACT "contact" | ||||
38 | #define ENTITY_TYPE_ADDRESSBOOK "addressbook" | ||||
39 | | ||||
40 | using namespace Sink; | ||||
41 | | ||||
42 | class ContactSynchronizer : public WebDavSynchronizer | ||||
43 | { | ||||
44 | public: | ||||
45 | ContactSynchronizer(const Sink::ResourceContext &resourceContext) | ||||
46 | : WebDavSynchronizer(resourceContext, KDAV2::CardDav, | ||||
47 | ApplicationDomain::getTypeName<ApplicationDomain::Addressbook>(), | ||||
48 | ApplicationDomain::getTypeName<ApplicationDomain::Contact>()) | ||||
49 | {} | ||||
50 | QByteArray createAddressbook(const QString &addressbookName, const QString &addressbookPath, const QString &parentAddressbookRid) | ||||
51 | { | ||||
52 | SinkTrace() << "Creating addressbook: " << addressbookName << parentAddressbookRid; | ||||
53 | const auto remoteId = addressbookPath.toUtf8(); | ||||
54 | const auto bufferType = ENTITY_TYPE_ADDRESSBOOK; | ||||
55 | Sink::ApplicationDomain::Addressbook addressbook; | ||||
56 | addressbook.setName(addressbookName); | ||||
57 | QHash<QByteArray, Query::Comparator> mergeCriteria; | ||||
58 | | ||||
59 | if (!parentAddressbookRid.isEmpty()) { | ||||
60 | addressbook.setParent(syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, parentAddressbookRid.toUtf8())); | ||||
61 | } | ||||
62 | createOrModify(bufferType, remoteId, addressbook, mergeCriteria); | ||||
63 | return remoteId; | ||||
64 | } | ||||
65 | | ||||
66 | protected: | ||||
67 | void updateLocalCollections(KDAV2::DavCollection::List addressbookList) Q_DECL_OVERRIDE | ||||
68 | { | ||||
69 | const QByteArray bufferType = ENTITY_TYPE_ADDRESSBOOK; | ||||
70 | SinkTrace() << "Found" << addressbookList.size() << "addressbooks"; | ||||
71 | | ||||
72 | for (const auto &f : addressbookList) { | ||||
73 | const auto &rid = resourceID(f); | ||||
74 | SinkLog() << "Found addressbook:" << rid << f.displayName(); | ||||
75 | createAddressbook(f.displayName(), rid, ""); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | void updateLocalItem(KDAV2::DavItem remoteContact, const QByteArray &addressbookLocalId) Q_DECL_OVERRIDE | ||||
80 | { | ||||
81 | Sink::ApplicationDomain::Contact localContact; | ||||
82 | | ||||
83 | localContact.setVcard(remoteContact.data()); | ||||
84 | localContact.setAddressbook(addressbookLocalId); | ||||
85 | | ||||
86 | QHash<QByteArray, Query::Comparator> mergeCriteria; | ||||
87 | createOrModify(ENTITY_TYPE_CONTACT, resourceID(remoteContact), localContact, mergeCriteria); | ||||
88 | } | ||||
89 | | ||||
90 | QByteArray collectionLocalResourceID(const KDAV2::DavCollection &addressbook) Q_DECL_OVERRIDE | ||||
91 | { | ||||
92 | return syncStore().resolveRemoteId(ENTITY_TYPE_ADDRESSBOOK, resourceID(addressbook)); | ||||
93 | } | ||||
94 | | ||||
95 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Contact &contact, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | ||||
96 | { | ||||
97 | return KAsync::null<QByteArray>(); | ||||
98 | } | ||||
99 | | ||||
100 | KAsync::Job<QByteArray> replay(const ApplicationDomain::Addressbook &addressbook, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE | ||||
101 | { | ||||
102 | return KAsync::null<QByteArray>(); | ||||
103 | } | ||||
104 | }; | ||||
105 | | ||||
106 | | ||||
107 | CardDavResource::CardDavResource(const Sink::ResourceContext &resourceContext) | ||||
108 | : Sink::GenericResource(resourceContext) | ||||
109 | { | ||||
110 | auto synchronizer = QSharedPointer<ContactSynchronizer>::create(resourceContext); | ||||
111 | setupSynchronizer(synchronizer); | ||||
112 | | ||||
113 | setupPreprocessors(ENTITY_TYPE_CONTACT, QVector<Sink::Preprocessor*>() << new ContactPropertyExtractor); | ||||
114 | } | ||||
115 | | ||||
116 | | ||||
117 | CardDavResourceFactory::CardDavResourceFactory(QObject *parent) | ||||
118 | : Sink::ResourceFactory(parent, | ||||
119 | {Sink::ApplicationDomain::ResourceCapabilities::Contact::contact, | ||||
120 | Sink::ApplicationDomain::ResourceCapabilities::Contact::addressbook, | ||||
121 | Sink::ApplicationDomain::ResourceCapabilities::Contact::storage | ||||
122 | } | ||||
123 | ) | ||||
124 | { | ||||
125 | } | ||||
126 | | ||||
127 | Sink::Resource *CardDavResourceFactory::createResource(const ResourceContext &context) | ||||
128 | { | ||||
129 | return new CardDavResource(context); | ||||
130 | } | ||||
131 | | ||||
132 | void CardDavResourceFactory::registerFacades(const QByteArray &name, Sink::FacadeFactory &factory) | ||||
133 | { | ||||
134 | factory.registerFacade<ApplicationDomain::Contact, DefaultFacade<ApplicationDomain::Contact>>(name); | ||||
135 | factory.registerFacade<ApplicationDomain::Addressbook, DefaultFacade<ApplicationDomain::Addressbook>>(name); | ||||
136 | } | ||||
137 | | ||||
138 | void CardDavResourceFactory::registerAdaptorFactories(const QByteArray &name, Sink::AdaptorFactoryRegistry ®istry) | ||||
139 | { | ||||
140 | registry.registerFactory<ApplicationDomain::Contact, DefaultAdaptorFactory<ApplicationDomain::Contact>>(name); | ||||
141 | registry.registerFactory<ApplicationDomain::Addressbook, DefaultAdaptorFactory<ApplicationDomain::Addressbook>>(name); | ||||
142 | } | ||||
143 | | ||||
144 | void CardDavResourceFactory::removeDataFromDisk(const QByteArray &instanceIdentifier) | ||||
145 | { | ||||
146 | CardDavResource::removeFromDisk(instanceIdentifier); | ||||
147 | } |