diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-05-25 23:54:36 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-05-25 23:54:36 +0200 |
commit | b25c71e0a42b18b02764719d32714f07ca241b3f (patch) | |
tree | c780b3cdca0fd6495d2285a8876220de3b9a51f8 | |
parent | 3601ee575f833bf204540f4fac41d87a0d977a79 (diff) | |
download | sink-b25c71e0a42b18b02764719d32714f07ca241b3f.tar.gz sink-b25c71e0a42b18b02764719d32714f07ca241b3f.zip |
Moved ApplicationDomainType and BufferAdaptor to separate files
-rw-r--r-- | common/bufferadaptor.h | 67 | ||||
-rw-r--r-- | common/clientapi.h | 152 | ||||
-rw-r--r-- | common/domain/applicationdomaintype.h | 134 |
3 files changed, 202 insertions, 151 deletions
diff --git a/common/bufferadaptor.h b/common/bufferadaptor.h new file mode 100644 index 0000000..121ef9d --- /dev/null +++ b/common/bufferadaptor.h | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #pragma once | ||
21 | |||
22 | #include <QVariant> | ||
23 | #include <QByteArray> | ||
24 | #include <QHash> | ||
25 | |||
26 | namespace Akonadi2 { | ||
27 | |||
28 | namespace ApplicationDomain { | ||
29 | |||
30 | /** | ||
31 | * This class has to be implemented by resources and can be used as generic interface to access the buffer properties | ||
32 | */ | ||
33 | class BufferAdaptor { | ||
34 | public: | ||
35 | virtual ~BufferAdaptor() {} | ||
36 | virtual QVariant getProperty(const QByteArray &key) const { return QVariant(); } | ||
37 | virtual void setProperty(const QByteArray &key, const QVariant &value) {} | ||
38 | virtual QList<QByteArray> availableProperties() const { return QList<QByteArray>(); } | ||
39 | }; | ||
40 | |||
41 | class MemoryBufferAdaptor : public BufferAdaptor { | ||
42 | public: | ||
43 | MemoryBufferAdaptor() | ||
44 | : BufferAdaptor() | ||
45 | { | ||
46 | } | ||
47 | |||
48 | MemoryBufferAdaptor(const BufferAdaptor &buffer) | ||
49 | : BufferAdaptor() | ||
50 | { | ||
51 | for(const auto &property : buffer.availableProperties()) { | ||
52 | mValues.insert(property, buffer.getProperty(property)); | ||
53 | } | ||
54 | } | ||
55 | |||
56 | virtual ~MemoryBufferAdaptor() {} | ||
57 | |||
58 | virtual QVariant getProperty(const QByteArray &key) const { return mValues.value(key); } | ||
59 | virtual void setProperty(const QByteArray &key, const QVariant &value) { mValues.insert(key, value); } | ||
60 | virtual QByteArrayList availableProperties() const { return mValues.keys(); } | ||
61 | |||
62 | private: | ||
63 | QHash<QByteArray, QVariant> mValues; | ||
64 | }; | ||
65 | |||
66 | } | ||
67 | } | ||
diff --git a/common/clientapi.h b/common/clientapi.h index d26a2ad..38ec1ee 100644 --- a/common/clientapi.h +++ b/common/clientapi.h | |||
@@ -33,6 +33,7 @@ | |||
33 | 33 | ||
34 | #include "threadboundary.h" | 34 | #include "threadboundary.h" |
35 | #include "resultprovider.h" | 35 | #include "resultprovider.h" |
36 | #include "domain/applicationdomaintype.h" | ||
36 | 37 | ||
37 | namespace async { | 38 | namespace async { |
38 | //This should abstract if we execute from eventloop or in thread. | 39 | //This should abstract if we execute from eventloop or in thread. |
@@ -42,157 +43,6 @@ namespace async { | |||
42 | 43 | ||
43 | namespace Akonadi2 { | 44 | namespace Akonadi2 { |
44 | 45 | ||
45 | /** | ||
46 | * Standardized Application Domain Types | ||
47 | * | ||
48 | * They don't adhere to any standard and can be freely extended | ||
49 | * Their sole purpose is providing a standardized interface to access data. | ||
50 | * | ||
51 | * This is necessary to decouple resource-backends from application domain containers (otherwise each resource would have to provide a faceade for each application domain container). | ||
52 | * | ||
53 | * These types will be frequently modified (for every new feature that should be exposed to the any client) | ||
54 | */ | ||
55 | namespace ApplicationDomain { | ||
56 | |||
57 | /** | ||
58 | * This class has to be implemented by resources and can be used as generic interface to access the buffer properties | ||
59 | */ | ||
60 | class BufferAdaptor { | ||
61 | public: | ||
62 | virtual ~BufferAdaptor() {} | ||
63 | virtual QVariant getProperty(const QByteArray &key) const { return QVariant(); } | ||
64 | virtual void setProperty(const QByteArray &key, const QVariant &value) {} | ||
65 | virtual QList<QByteArray> availableProperties() const { return QList<QByteArray>(); } | ||
66 | }; | ||
67 | |||
68 | class MemoryBufferAdaptor : public BufferAdaptor { | ||
69 | public: | ||
70 | MemoryBufferAdaptor() | ||
71 | : BufferAdaptor() | ||
72 | { | ||
73 | } | ||
74 | |||
75 | MemoryBufferAdaptor(const BufferAdaptor &buffer) | ||
76 | : BufferAdaptor() | ||
77 | { | ||
78 | for(const auto &property : buffer.availableProperties()) { | ||
79 | mValues.insert(property, buffer.getProperty(property)); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | virtual ~MemoryBufferAdaptor() {} | ||
84 | |||
85 | virtual QVariant getProperty(const QByteArray &key) const { return mValues.value(key); } | ||
86 | virtual void setProperty(const QByteArray &key, const QVariant &value) { mValues.insert(key, value); } | ||
87 | virtual QByteArrayList availableProperties() const { return mValues.keys(); } | ||
88 | |||
89 | private: | ||
90 | QHash<QByteArray, QVariant> mValues; | ||
91 | }; | ||
92 | |||
93 | /** | ||
94 | * The domain type interface has two purposes: | ||
95 | * * provide a unified interface to read buffers (for zero-copy reading) | ||
96 | * * record changes to generate changesets for modifications | ||
97 | */ | ||
98 | class ApplicationDomainType { | ||
99 | public: | ||
100 | typedef QSharedPointer<ApplicationDomainType> Ptr; | ||
101 | |||
102 | ApplicationDomainType() | ||
103 | :mAdaptor(new MemoryBufferAdaptor()) | ||
104 | { | ||
105 | |||
106 | } | ||
107 | |||
108 | ApplicationDomainType(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier, qint64 revision, const QSharedPointer<BufferAdaptor> &adaptor) | ||
109 | : mAdaptor(adaptor), | ||
110 | mResourceInstanceIdentifier(resourceInstanceIdentifier), | ||
111 | mIdentifier(identifier), | ||
112 | mRevision(revision) | ||
113 | { | ||
114 | } | ||
115 | |||
116 | template <typename DomainType> | ||
117 | static typename DomainType::Ptr getInMemoryRepresentation(const ApplicationDomainType::Ptr &domainType) | ||
118 | { | ||
119 | //TODO only copy requested properties | ||
120 | auto memoryAdaptor = QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create(*(domainType->mAdaptor)); | ||
121 | return QSharedPointer<DomainType>::create(domainType->mResourceInstanceIdentifier, domainType->mIdentifier, domainType->mRevision, memoryAdaptor); | ||
122 | } | ||
123 | |||
124 | virtual ~ApplicationDomainType() {} | ||
125 | |||
126 | virtual QVariant getProperty(const QByteArray &key) const { return mAdaptor->getProperty(key); } | ||
127 | virtual void setProperty(const QByteArray &key, const QVariant &value){ mChangeSet.insert(key, value); mAdaptor->setProperty(key, value); } | ||
128 | virtual QByteArrayList changedProperties() const { return mChangeSet.keys(); } | ||
129 | qint64 revision() const { return mRevision; } | ||
130 | QByteArray resourceInstanceIdentifier() const { return mResourceInstanceIdentifier; } | ||
131 | QByteArray identifier() const { return mIdentifier; } | ||
132 | |||
133 | private: | ||
134 | QSharedPointer<BufferAdaptor> mAdaptor; | ||
135 | QHash<QByteArray, QVariant> mChangeSet; | ||
136 | /* | ||
137 | * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location. | ||
138 | */ | ||
139 | QByteArray mResourceInstanceIdentifier; | ||
140 | QByteArray mIdentifier; | ||
141 | qint64 mRevision; | ||
142 | }; | ||
143 | |||
144 | struct Event : public ApplicationDomainType { | ||
145 | typedef QSharedPointer<Event> Ptr; | ||
146 | using ApplicationDomainType::ApplicationDomainType; | ||
147 | }; | ||
148 | |||
149 | struct Todo : public ApplicationDomainType { | ||
150 | typedef QSharedPointer<Todo> Ptr; | ||
151 | using ApplicationDomainType::ApplicationDomainType; | ||
152 | }; | ||
153 | |||
154 | struct Calendar : public ApplicationDomainType { | ||
155 | typedef QSharedPointer<Calendar> Ptr; | ||
156 | using ApplicationDomainType::ApplicationDomainType; | ||
157 | }; | ||
158 | |||
159 | class Mail : public ApplicationDomainType { | ||
160 | }; | ||
161 | |||
162 | class Folder : public ApplicationDomainType { | ||
163 | }; | ||
164 | |||
165 | /** | ||
166 | * Represents an akonadi resource. | ||
167 | * | ||
168 | * This type is used for configuration of resources, | ||
169 | * and for creating and removing resource instances. | ||
170 | */ | ||
171 | struct AkonadiResource : public ApplicationDomainType { | ||
172 | typedef QSharedPointer<AkonadiResource> Ptr; | ||
173 | using ApplicationDomainType::ApplicationDomainType; | ||
174 | }; | ||
175 | |||
176 | /** | ||
177 | * All types need to be registered here an MUST return a different name. | ||
178 | * | ||
179 | * Do not store these types to disk, they may change over time. | ||
180 | */ | ||
181 | |||
182 | template<class DomainType> | ||
183 | QByteArray getTypeName(); | ||
184 | |||
185 | template<> | ||
186 | QByteArray getTypeName<Event>(); | ||
187 | |||
188 | template<> | ||
189 | QByteArray getTypeName<Todo>(); | ||
190 | |||
191 | template<> | ||
192 | QByteArray getTypeName<AkonadiResource>(); | ||
193 | |||
194 | } | ||
195 | |||
196 | using namespace async; | 46 | using namespace async; |
197 | 47 | ||
198 | /** | 48 | /** |
diff --git a/common/domain/applicationdomaintype.h b/common/domain/applicationdomaintype.h new file mode 100644 index 0000000..2de1460 --- /dev/null +++ b/common/domain/applicationdomaintype.h | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #pragma once | ||
21 | |||
22 | #include <QSharedPointer> | ||
23 | #include <QVariant> | ||
24 | #include <QByteArray> | ||
25 | #include "../bufferadaptor.h" | ||
26 | |||
27 | namespace Akonadi2 { | ||
28 | |||
29 | namespace ApplicationDomain { | ||
30 | |||
31 | /** | ||
32 | * The domain type interface has two purposes: | ||
33 | * * provide a unified interface to read buffers (for zero-copy reading) | ||
34 | * * record changes to generate changesets for modifications | ||
35 | * | ||
36 | * ApplicationDomainTypes don't adhere to any standard and are meant to be extended frequently (hence the non-typesafe interface). | ||
37 | */ | ||
38 | class ApplicationDomainType { | ||
39 | public: | ||
40 | typedef QSharedPointer<ApplicationDomainType> Ptr; | ||
41 | |||
42 | ApplicationDomainType() | ||
43 | :mAdaptor(new MemoryBufferAdaptor()) | ||
44 | { | ||
45 | |||
46 | } | ||
47 | |||
48 | ApplicationDomainType(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier, qint64 revision, const QSharedPointer<BufferAdaptor> &adaptor) | ||
49 | : mAdaptor(adaptor), | ||
50 | mResourceInstanceIdentifier(resourceInstanceIdentifier), | ||
51 | mIdentifier(identifier), | ||
52 | mRevision(revision) | ||
53 | { | ||
54 | } | ||
55 | |||
56 | template <typename DomainType> | ||
57 | static typename DomainType::Ptr getInMemoryRepresentation(const ApplicationDomainType::Ptr &domainType) | ||
58 | { | ||
59 | //TODO only copy requested properties | ||
60 | auto memoryAdaptor = QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create(*(domainType->mAdaptor)); | ||
61 | return QSharedPointer<DomainType>::create(domainType->mResourceInstanceIdentifier, domainType->mIdentifier, domainType->mRevision, memoryAdaptor); | ||
62 | } | ||
63 | |||
64 | virtual ~ApplicationDomainType() {} | ||
65 | |||
66 | virtual QVariant getProperty(const QByteArray &key) const { return mAdaptor->getProperty(key); } | ||
67 | virtual void setProperty(const QByteArray &key, const QVariant &value){ mChangeSet.insert(key, value); mAdaptor->setProperty(key, value); } | ||
68 | virtual QByteArrayList changedProperties() const { return mChangeSet.keys(); } | ||
69 | qint64 revision() const { return mRevision; } | ||
70 | QByteArray resourceInstanceIdentifier() const { return mResourceInstanceIdentifier; } | ||
71 | QByteArray identifier() const { return mIdentifier; } | ||
72 | |||
73 | private: | ||
74 | QSharedPointer<BufferAdaptor> mAdaptor; | ||
75 | QHash<QByteArray, QVariant> mChangeSet; | ||
76 | /* | ||
77 | * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location. | ||
78 | */ | ||
79 | QByteArray mResourceInstanceIdentifier; | ||
80 | QByteArray mIdentifier; | ||
81 | qint64 mRevision; | ||
82 | }; | ||
83 | |||
84 | struct Event : public ApplicationDomainType { | ||
85 | typedef QSharedPointer<Event> Ptr; | ||
86 | using ApplicationDomainType::ApplicationDomainType; | ||
87 | }; | ||
88 | |||
89 | struct Todo : public ApplicationDomainType { | ||
90 | typedef QSharedPointer<Todo> Ptr; | ||
91 | using ApplicationDomainType::ApplicationDomainType; | ||
92 | }; | ||
93 | |||
94 | struct Calendar : public ApplicationDomainType { | ||
95 | typedef QSharedPointer<Calendar> Ptr; | ||
96 | using ApplicationDomainType::ApplicationDomainType; | ||
97 | }; | ||
98 | |||
99 | class Mail : public ApplicationDomainType { | ||
100 | }; | ||
101 | |||
102 | class Folder : public ApplicationDomainType { | ||
103 | }; | ||
104 | |||
105 | /** | ||
106 | * Represents an akonadi resource. | ||
107 | * | ||
108 | * This type is used for configuration of resources, | ||
109 | * and for creating and removing resource instances. | ||
110 | */ | ||
111 | struct AkonadiResource : public ApplicationDomainType { | ||
112 | typedef QSharedPointer<AkonadiResource> Ptr; | ||
113 | using ApplicationDomainType::ApplicationDomainType; | ||
114 | }; | ||
115 | |||
116 | /** | ||
117 | * All types need to be registered here an MUST return a different name. | ||
118 | * | ||
119 | * Do not store these types to disk, they may change over time. | ||
120 | */ | ||
121 | template<class DomainType> | ||
122 | QByteArray getTypeName(); | ||
123 | |||
124 | template<> | ||
125 | QByteArray getTypeName<Event>(); | ||
126 | |||
127 | template<> | ||
128 | QByteArray getTypeName<Todo>(); | ||
129 | |||
130 | template<> | ||
131 | QByteArray getTypeName<AkonadiResource>(); | ||
132 | |||
133 | } | ||
134 | } | ||