diff options
-rw-r--r-- | common/facade.h | 88 | ||||
-rw-r--r-- | dummyresource/facade.cpp | 34 | ||||
-rw-r--r-- | dummyresource/facade.h | 11 |
3 files changed, 94 insertions, 39 deletions
diff --git a/common/facade.h b/common/facade.h new file mode 100644 index 0000000..98bcb38 --- /dev/null +++ b/common/facade.h | |||
@@ -0,0 +1,88 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 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 | #pragma once | ||
21 | |||
22 | #include "clientapi.h" | ||
23 | |||
24 | #include <QByteArray> | ||
25 | |||
26 | #include "async/src/async.h" | ||
27 | #include "resourceaccess.h" | ||
28 | #include "commands.h" | ||
29 | #include "createentity_generated.h" | ||
30 | #include "domainadaptor.h" | ||
31 | #include "entitybuffer.h" | ||
32 | |||
33 | namespace Akonadi2 { | ||
34 | class ResourceAccess; | ||
35 | /** | ||
36 | * Default facade implementation for resources that are implemented in a separate process using the ResourceAccess class. | ||
37 | */ | ||
38 | template <typename DomainType> | ||
39 | class GenericFacade: public Akonadi2::StoreFacade<DomainType> | ||
40 | { | ||
41 | public: | ||
42 | GenericFacade(const QByteArray &resourceIdentifier) | ||
43 | : Akonadi2::StoreFacade<DomainType>(), | ||
44 | mResourceAccess(new ResourceAccess(resourceIdentifier)) | ||
45 | { | ||
46 | } | ||
47 | |||
48 | ~GenericFacade() | ||
49 | { | ||
50 | } | ||
51 | |||
52 | protected: | ||
53 | Async::Job<void> sendCreateCommand(const QByteArray &t, const QByteArray &buffer) | ||
54 | { | ||
55 | flatbuffers::FlatBufferBuilder fbb; | ||
56 | //This is the resource buffer type and not the domain type | ||
57 | auto type = fbb.CreateString(t.constData()); | ||
58 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, buffer.constData(), buffer.size()); | ||
59 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
60 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
61 | mResourceAccess->open(); | ||
62 | return mResourceAccess->sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb); | ||
63 | } | ||
64 | |||
65 | Async::Job<void> synchronizeResource(bool sync, bool processAll) | ||
66 | { | ||
67 | //TODO check if a sync is necessary | ||
68 | //TODO Only sync what was requested | ||
69 | //TODO timeout | ||
70 | //TODO the synchronization should normally not be necessary: We just return what is already available. | ||
71 | |||
72 | if (sync || processAll) { | ||
73 | return Async::start<void>([=](Async::Future<void> &future) { | ||
74 | mResourceAccess->open(); | ||
75 | mResourceAccess->synchronizeResource(sync, processAll).then<void>([&future]() { | ||
76 | future.setFinished(); | ||
77 | }).exec(); | ||
78 | }); | ||
79 | } | ||
80 | return Async::null<void>(); | ||
81 | } | ||
82 | |||
83 | private: | ||
84 | //TODO use one resource access instance per application => make static | ||
85 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | ||
86 | }; | ||
87 | |||
88 | } | ||
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index 1af735e..209ad0d 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp | |||
@@ -28,7 +28,6 @@ | |||
28 | #include "event_generated.h" | 28 | #include "event_generated.h" |
29 | #include "entity_generated.h" | 29 | #include "entity_generated.h" |
30 | #include "metadata_generated.h" | 30 | #include "metadata_generated.h" |
31 | #include "createentity_generated.h" | ||
32 | #include "domainadaptor.h" | 31 | #include "domainadaptor.h" |
33 | #include <common/entitybuffer.h> | 32 | #include <common/entitybuffer.h> |
34 | #include <common/index.h> | 33 | #include <common/index.h> |
@@ -37,9 +36,9 @@ | |||
37 | using namespace DummyCalendar; | 36 | using namespace DummyCalendar; |
38 | using namespace flatbuffers; | 37 | using namespace flatbuffers; |
39 | 38 | ||
39 | |||
40 | DummyResourceFacade::DummyResourceFacade() | 40 | DummyResourceFacade::DummyResourceFacade() |
41 | : Akonadi2::StoreFacade<Akonadi2::Domain::Event>(), | 41 | : Akonadi2::GenericFacade<Akonadi2::Domain::Event>("org.kde.dummy"), |
42 | mResourceAccess(new Akonadi2::ResourceAccess("org.kde.dummy")), | ||
43 | mFactory(new DummyEventAdaptorFactory) | 42 | mFactory(new DummyEventAdaptorFactory) |
44 | { | 43 | { |
45 | } | 44 | } |
@@ -52,15 +51,7 @@ Async::Job<void> DummyResourceFacade::create(const Akonadi2::Domain::Event &doma | |||
52 | { | 51 | { |
53 | flatbuffers::FlatBufferBuilder entityFbb; | 52 | flatbuffers::FlatBufferBuilder entityFbb; |
54 | mFactory->createBuffer(domainObject, entityFbb); | 53 | mFactory->createBuffer(domainObject, entityFbb); |
55 | 54 | return sendCreateCommand("event", QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize())); | |
56 | flatbuffers::FlatBufferBuilder fbb; | ||
57 | //This is the resource buffer type and not the domain type | ||
58 | auto type = fbb.CreateString("event"); | ||
59 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); | ||
60 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
61 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
62 | mResourceAccess->open(); | ||
63 | return mResourceAccess->sendCommand(Akonadi2::Commands::CreateEntityCommand, fbb); | ||
64 | } | 55 | } |
65 | 56 | ||
66 | Async::Job<void> DummyResourceFacade::modify(const Akonadi2::Domain::Event &domainObject) | 57 | Async::Job<void> DummyResourceFacade::modify(const Akonadi2::Domain::Event &domainObject) |
@@ -115,25 +106,6 @@ static std::function<bool(const std::string &key, DummyEvent const *buffer, Akon | |||
115 | return preparedQuery; | 106 | return preparedQuery; |
116 | } | 107 | } |
117 | 108 | ||
118 | Async::Job<void> DummyResourceFacade::synchronizeResource(bool sync, bool processAll) | ||
119 | { | ||
120 | //TODO check if a sync is necessary | ||
121 | //TODO Only sync what was requested | ||
122 | //TODO timeout | ||
123 | //TODO the synchronization should normally not be necessary: We just return what is already available. | ||
124 | |||
125 | if (sync || processAll) { | ||
126 | return Async::start<void>([=](Async::Future<void> &future) { | ||
127 | mResourceAccess->open(); | ||
128 | mResourceAccess->synchronizeResource(sync, processAll).then<void>([&future](Async::Future<void> &f) { | ||
129 | future.setFinished(); | ||
130 | f.setFinished(); | ||
131 | }).exec(); | ||
132 | }); | ||
133 | } | ||
134 | return Async::null<void>(); | ||
135 | } | ||
136 | |||
137 | void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::Domain::Buffer::Event const *local)> preparedQuery) | 109 | void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::Domain::Buffer::Event const *local)> preparedQuery) |
138 | { | 110 | { |
139 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | 111 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { |
diff --git a/dummyresource/facade.h b/dummyresource/facade.h index 1f69161..195c50f 100644 --- a/dummyresource/facade.h +++ b/dummyresource/facade.h | |||
@@ -19,6 +19,8 @@ | |||
19 | 19 | ||
20 | #pragma once | 20 | #pragma once |
21 | 21 | ||
22 | #include "common/facade.h" | ||
23 | |||
22 | #include "common/clientapi.h" | 24 | #include "common/clientapi.h" |
23 | #include "common/storage.h" | 25 | #include "common/storage.h" |
24 | #include "resourcefactory.h" | 26 | #include "resourcefactory.h" |
@@ -27,12 +29,7 @@ | |||
27 | #include "dummycalendar_generated.h" | 29 | #include "dummycalendar_generated.h" |
28 | #include "common/domainadaptor.h" | 30 | #include "common/domainadaptor.h" |
29 | 31 | ||
30 | namespace Akonadi2 { | 32 | class DummyResourceFacade : public Akonadi2::GenericFacade<Akonadi2::Domain::Event> |
31 | class ResourceAccess; | ||
32 | } | ||
33 | |||
34 | |||
35 | class DummyResourceFacade : public Akonadi2::StoreFacade<Akonadi2::Domain::Event> | ||
36 | { | 33 | { |
37 | public: | 34 | public: |
38 | DummyResourceFacade(); | 35 | DummyResourceFacade(); |
@@ -44,7 +41,5 @@ public: | |||
44 | 41 | ||
45 | private: | 42 | private: |
46 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::Domain::Buffer::Event const *local)>); | 43 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::Domain::Buffer::Event const *local)>); |
47 | Async::Job<void> synchronizeResource(bool sync, bool processAll); | ||
48 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | ||
49 | QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::Domain::Event, Akonadi2::Domain::Buffer::Event, DummyCalendar::DummyEvent> > mFactory; | 44 | QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::Domain::Event, Akonadi2::Domain::Buffer::Event, DummyCalendar::DummyEvent> > mFactory; |
50 | }; | 45 | }; |