diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-30 18:49:04 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-30 18:49:04 +0100 |
commit | 412563b7ff18684f9786f4e40b1a4d538f2d5233 (patch) | |
tree | 3190a317306cfb71b0d5d9bc4c0f06b260a92ce6 /examples/dummyresource/resourcefactory.cpp | |
parent | 790991aa1007d3271d80bc7e77f5b4f86c9bcef0 (diff) | |
parent | 6ad307dd846d07f1b55a1679a8d2eb47525af57d (diff) | |
download | sink-412563b7ff18684f9786f4e40b1a4d538f2d5233.tar.gz sink-412563b7ff18684f9786f4e40b1a4d538f2d5233.zip |
Merge branch 'feature/modelresult' into develop
Diffstat (limited to 'examples/dummyresource/resourcefactory.cpp')
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 397ca5f..e524c3f 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -40,6 +40,7 @@ | |||
40 | //This is the resources entity type, and not the domain type | 40 | //This is the resources entity type, and not the domain type |
41 | #define ENTITY_TYPE_EVENT "event" | 41 | #define ENTITY_TYPE_EVENT "event" |
42 | #define ENTITY_TYPE_MAIL "mail" | 42 | #define ENTITY_TYPE_MAIL "mail" |
43 | #define ENTITY_TYPE_FOLDER "folder" | ||
43 | 44 | ||
44 | /** | 45 | /** |
45 | * Index types: | 46 | * Index types: |
@@ -51,7 +52,6 @@ | |||
51 | * * range indexes like what date range an event affects. | 52 | * * range indexes like what date range an event affects. |
52 | * * group indexes like tree hierarchies as nested sets | 53 | * * group indexes like tree hierarchies as nested sets |
53 | */ | 54 | */ |
54 | template<typename DomainType> | ||
55 | class IndexUpdater : public Akonadi2::Preprocessor { | 55 | class IndexUpdater : public Akonadi2::Preprocessor { |
56 | public: | 56 | public: |
57 | IndexUpdater(const QByteArray &index, const QByteArray &type) | 57 | IndexUpdater(const QByteArray &index, const QByteArray &type) |
@@ -63,21 +63,17 @@ public: | |||
63 | 63 | ||
64 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 64 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
65 | { | 65 | { |
66 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
67 | add(newEntity.getProperty("remoteId"), uid, transaction); | 66 | add(newEntity.getProperty("remoteId"), uid, transaction); |
68 | } | 67 | } |
69 | 68 | ||
70 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 69 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
71 | { | 70 | { |
72 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
73 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
74 | remove(oldEntity.getProperty("remoteId"), uid, transaction); | 71 | remove(oldEntity.getProperty("remoteId"), uid, transaction); |
75 | add(newEntity.getProperty("remoteId"), uid, transaction); | 72 | add(newEntity.getProperty("remoteId"), uid, transaction); |
76 | } | 73 | } |
77 | 74 | ||
78 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | 75 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
79 | { | 76 | { |
80 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
81 | remove(oldEntity.getProperty("remoteId"), uid, transaction); | 77 | remove(oldEntity.getProperty("remoteId"), uid, transaction); |
82 | } | 78 | } |
83 | 79 | ||
@@ -91,6 +87,7 @@ private: | |||
91 | 87 | ||
92 | void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | 88 | void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) |
93 | { | 89 | { |
90 | //TODO hide notfound error | ||
94 | Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); | 91 | Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid); |
95 | } | 92 | } |
96 | 93 | ||
@@ -98,20 +95,49 @@ private: | |||
98 | QByteArray mBufferType; | 95 | QByteArray mBufferType; |
99 | }; | 96 | }; |
100 | 97 | ||
98 | template<typename DomainType> | ||
99 | class DefaultIndexUpdater : public Akonadi2::Preprocessor { | ||
100 | public: | ||
101 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
102 | { | ||
103 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
104 | } | ||
105 | |||
106 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
107 | { | ||
108 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
109 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); | ||
110 | } | ||
111 | |||
112 | void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
113 | { | ||
114 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction); | ||
115 | } | ||
116 | }; | ||
117 | |||
101 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) | 118 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) |
102 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) | 119 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) |
103 | { | 120 | { |
104 | { | 121 | { |
105 | auto eventFactory = QSharedPointer<DummyEventAdaptorFactory>::create(); | 122 | QVector<Akonadi2::Preprocessor*> eventPreprocessors; |
106 | auto eventIndexer = new IndexUpdater<Akonadi2::ApplicationDomain::Event>("event.index.rid", ENTITY_TYPE_EVENT); | 123 | eventPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>; |
107 | mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, QVector<Akonadi2::Preprocessor*>() << eventIndexer); | 124 | eventPreprocessors << new IndexUpdater("event.index.rid", ENTITY_TYPE_EVENT); |
108 | mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, eventFactory); | 125 | mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, eventPreprocessors); |
126 | mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, QSharedPointer<DummyEventAdaptorFactory>::create()); | ||
127 | } | ||
128 | { | ||
129 | QVector<Akonadi2::Preprocessor*> mailPreprocessors; | ||
130 | mailPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Mail>; | ||
131 | mailPreprocessors << new IndexUpdater("mail.index.rid", ENTITY_TYPE_MAIL); | ||
132 | mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, mailPreprocessors); | ||
133 | mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, QSharedPointer<DummyMailAdaptorFactory>::create()); | ||
109 | } | 134 | } |
110 | { | 135 | { |
111 | auto mailFactory = QSharedPointer<DummyMailAdaptorFactory>::create(); | 136 | QVector<Akonadi2::Preprocessor*> folderPreprocessors; |
112 | auto mailIndexer = new IndexUpdater<Akonadi2::ApplicationDomain::Mail>("mail.index.rid", ENTITY_TYPE_MAIL); | 137 | folderPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Folder>; |
113 | mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, QVector<Akonadi2::Preprocessor*>() << mailIndexer); | 138 | folderPreprocessors << new IndexUpdater("folder.index.rid", ENTITY_TYPE_FOLDER); |
114 | mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, mailFactory); | 139 | mPipeline->setPreprocessors(ENTITY_TYPE_FOLDER, folderPreprocessors); |
140 | mPipeline->setAdaptorFactory(ENTITY_TYPE_FOLDER, QSharedPointer<DummyFolderAdaptorFactory>::create()); | ||
115 | } | 141 | } |
116 | } | 142 | } |
117 | 143 | ||
@@ -156,6 +182,27 @@ void DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, | |||
156 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize()); | 182 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize()); |
157 | } | 183 | } |
158 | 184 | ||
185 | void DummyResource::createFolder(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) | ||
186 | { | ||
187 | //Map the source format to the buffer format (which happens to be an exact copy here) | ||
188 | auto name = m_fbb.CreateString(data.value("name").toString().toStdString()); | ||
189 | flatbuffers::Offset<flatbuffers::String> parent; | ||
190 | bool hasParent = false; | ||
191 | if (!data.value("parent").toString().isEmpty()) { | ||
192 | hasParent = true; | ||
193 | parent = m_fbb.CreateString(data.value("parent").toString().toStdString()); | ||
194 | } | ||
195 | |||
196 | auto builder = Akonadi2::ApplicationDomain::Buffer::FolderBuilder(m_fbb); | ||
197 | builder.add_name(name); | ||
198 | if (hasParent) { | ||
199 | builder.add_parent(parent); | ||
200 | } | ||
201 | auto buffer = builder.Finish(); | ||
202 | Akonadi2::ApplicationDomain::Buffer::FinishFolderBuffer(m_fbb, buffer); | ||
203 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize()); | ||
204 | } | ||
205 | |||
159 | void DummyResource::synchronize(const QString &bufferType, const QMap<QString, QMap<QString, QVariant> > &data, Akonadi2::Storage::Transaction &transaction, std::function<void(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)> createEntity) | 206 | void DummyResource::synchronize(const QString &bufferType, const QMap<QString, QMap<QString, QVariant> > &data, Akonadi2::Storage::Transaction &transaction, std::function<void(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)> createEntity) |
160 | { | 207 | { |
161 | Index uidIndex("index.uid", transaction); | 208 | Index uidIndex("index.uid", transaction); |
@@ -202,6 +249,9 @@ KAsync::Job<void> DummyResource::synchronizeWithSource() | |||
202 | synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) { | 249 | synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) { |
203 | createMail(ridBuffer, data, entityFbb); | 250 | createMail(ridBuffer, data, entityFbb); |
204 | }); | 251 | }); |
252 | synchronize(ENTITY_TYPE_FOLDER, DummyStore::instance().folders(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) { | ||
253 | createFolder(ridBuffer, data, entityFbb); | ||
254 | }); | ||
205 | 255 | ||
206 | f.setFinished(); | 256 | f.setFinished(); |
207 | }); | 257 | }); |
@@ -228,5 +278,6 @@ void DummyResourceFactory::registerFacades(Akonadi2::FacadeFactory &factory) | |||
228 | { | 278 | { |
229 | factory.registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>(PLUGIN_NAME); | 279 | factory.registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>(PLUGIN_NAME); |
230 | factory.registerFacade<Akonadi2::ApplicationDomain::Mail, DummyResourceMailFacade>(PLUGIN_NAME); | 280 | factory.registerFacade<Akonadi2::ApplicationDomain::Mail, DummyResourceMailFacade>(PLUGIN_NAME); |
281 | factory.registerFacade<Akonadi2::ApplicationDomain::Folder, DummyResourceFolderFacade>(PLUGIN_NAME); | ||
231 | } | 282 | } |
232 | 283 | ||