diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/domainadaptor.cpp | 40 | ||||
-rw-r--r-- | common/domainadaptor.h | 102 |
3 files changed, 131 insertions, 12 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 650691c..b06718f 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt | |||
@@ -30,6 +30,7 @@ set(command_SRCS | |||
30 | commands.cpp | 30 | commands.cpp |
31 | console.cpp | 31 | console.cpp |
32 | pipeline.cpp | 32 | pipeline.cpp |
33 | domainadaptor.cpp | ||
33 | resource.cpp | 34 | resource.cpp |
34 | resourceaccess.cpp | 35 | resourceaccess.cpp |
35 | storage_common.cpp | 36 | storage_common.cpp |
diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp new file mode 100644 index 0000000..5b7c427 --- /dev/null +++ b/common/domainadaptor.cpp | |||
@@ -0,0 +1,40 @@ | |||
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 "domainadaptor.h" | ||
21 | |||
22 | template <> | ||
23 | QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializePropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>() | ||
24 | { | ||
25 | auto propertyMapper = QSharedPointer<PropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); | ||
26 | propertyMapper->mReadAccessors.insert("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { | ||
27 | if (buffer->summary()) { | ||
28 | return QString::fromStdString(buffer->summary()->c_str()); | ||
29 | } | ||
30 | return QVariant(); | ||
31 | }); | ||
32 | propertyMapper->mReadAccessors.insert("uid", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { | ||
33 | if (buffer->uid()) { | ||
34 | return QString::fromStdString(buffer->uid()->c_str()); | ||
35 | } | ||
36 | return QVariant(); | ||
37 | }); | ||
38 | return propertyMapper; | ||
39 | } | ||
40 | |||
diff --git a/common/domainadaptor.h b/common/domainadaptor.h index df3f8e5..15e3067 100644 --- a/common/domainadaptor.h +++ b/common/domainadaptor.h | |||
@@ -25,11 +25,17 @@ | |||
25 | #include <functional> | 25 | #include <functional> |
26 | #include "clientapi.h" //for domain parts | 26 | #include "clientapi.h" //for domain parts |
27 | 27 | ||
28 | #include "event_generated.h" | ||
29 | #include "entity_generated.h" | ||
30 | #include "metadata_generated.h" | ||
31 | #include "entitybuffer.h" | ||
32 | |||
28 | /** | 33 | /** |
29 | * The property mapper holds accessor functions for all properties. | 34 | * The property mapper is a non-typesafe virtual dispatch. |
30 | * | 35 | * |
31 | * It is by default initialized with accessors that access the local-only buffer, | 36 | * Instead of using an interface and requring each implementation to override |
32 | * and resource simply have to overwrite those accessors. | 37 | * a virtual method per property, the property mapper can be filled with accessors |
38 | * that extract the properties from resource types. | ||
33 | */ | 39 | */ |
34 | template<typename BufferType> | 40 | template<typename BufferType> |
35 | class PropertyMapper | 41 | class PropertyMapper |
@@ -55,20 +61,92 @@ public: | |||
55 | QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors; | 61 | QHash<QByteArray, std::function<void(const QVariant &, BufferType*)> > mWriteAccessors; |
56 | }; | 62 | }; |
57 | 63 | ||
58 | //The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. | 64 | /** |
59 | //It defines how values are split accross local and resource buffer. | 65 | * A generic adaptor implementation that uses a property mapper to read/write values. |
60 | //This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. | 66 | */ |
61 | // template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> | 67 | template <class LocalBuffer, class ResourceBuffer> |
62 | // class DomainTypeAdaptorFactory | 68 | class GenericBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor |
63 | // { | 69 | { |
64 | // }; | 70 | public: |
71 | GenericBufferAdaptor() | ||
72 | : BufferAdaptor() | ||
73 | { | ||
74 | |||
75 | } | ||
76 | |||
77 | void setProperty(const QByteArray &key, const QVariant &value) | ||
78 | { | ||
79 | if (mResourceMapper && mResourceMapper->mWriteAccessors.contains(key)) { | ||
80 | // mResourceMapper->setProperty(key, value, mResourceBuffer); | ||
81 | } else { | ||
82 | // mLocalMapper.; | ||
83 | } | ||
84 | } | ||
85 | |||
86 | virtual QVariant getProperty(const QByteArray &key) const | ||
87 | { | ||
88 | if (mResourceBuffer && mResourceMapper->mReadAccessors.contains(key)) { | ||
89 | return mResourceMapper->getProperty(key, mResourceBuffer); | ||
90 | } else if (mLocalBuffer && mLocalMapper->mReadAccessors.contains(key)) { | ||
91 | return mLocalMapper->getProperty(key, mLocalBuffer); | ||
92 | } | ||
93 | qWarning() << "no mapping available for key " << key; | ||
94 | return QVariant(); | ||
95 | } | ||
96 | |||
97 | virtual QList<QByteArray> availableProperties() const | ||
98 | { | ||
99 | QList<QByteArray> props; | ||
100 | props << mResourceMapper->mReadAccessors.keys(); | ||
101 | props << mLocalMapper->mReadAccessors.keys(); | ||
102 | return props; | ||
103 | } | ||
104 | |||
105 | LocalBuffer const *mLocalBuffer; | ||
106 | ResourceBuffer const *mResourceBuffer; | ||
107 | QSharedPointer<PropertyMapper<LocalBuffer> > mLocalMapper; | ||
108 | QSharedPointer<PropertyMapper<ResourceBuffer> > mResourceMapper; | ||
109 | }; | ||
110 | |||
111 | /** | ||
112 | * Initializes the local property mapper. | ||
113 | * | ||
114 | * Provide an implementation for each application domain type. | ||
115 | */ | ||
116 | template <class T> | ||
117 | QSharedPointer<PropertyMapper<T> > initializePropertyMapper(); | ||
65 | 118 | ||
119 | /** | ||
120 | * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. | ||
121 | * It defines how values are split accross local and resource buffer. | ||
122 | * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. | ||
123 | */ | ||
66 | template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> | 124 | template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> |
67 | class DomainTypeAdaptorFactory/* <typename DomainType, LocalBuffer, ResourceBuffer> */ | 125 | class DomainTypeAdaptorFactory |
68 | { | 126 | { |
69 | public: | 127 | public: |
128 | DomainTypeAdaptorFactory() : mLocalMapper(initializePropertyMapper<LocalBuffer>()) {}; | ||
70 | virtual ~DomainTypeAdaptorFactory() {}; | 129 | virtual ~DomainTypeAdaptorFactory() {}; |
71 | virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) = 0; | 130 | |
131 | /** | ||
132 | * Creates an adaptor for the given domain and resource types. | ||
133 | * | ||
134 | * This returns by default a GenericBufferAdaptor initialized with the corresponding property mappers. | ||
135 | */ | ||
136 | virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) | ||
137 | { | ||
138 | const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<ResourceBuffer>(entity.resource()); | ||
139 | const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<LocalBuffer>(entity.local()); | ||
140 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
141 | |||
142 | auto adaptor = QSharedPointer<GenericBufferAdaptor<LocalBuffer, ResourceBuffer> >::create(); | ||
143 | adaptor->mLocalBuffer = localBuffer; | ||
144 | adaptor->mLocalMapper = mLocalMapper; | ||
145 | adaptor->mResourceBuffer = resourceBuffer; | ||
146 | adaptor->mResourceMapper = mResourceMapper; | ||
147 | return adaptor; | ||
148 | } | ||
149 | |||
72 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; | 150 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) {}; |
73 | 151 | ||
74 | protected: | 152 | protected: |