diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-19 14:11:02 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-19 14:11:02 +0200 |
commit | f89e43b3603976bc0e6eb885b3b9a43a6caff1c2 (patch) | |
tree | 2ce617aa7a0f6b82b274f8fc9c0e74be5706ea31 /examples | |
parent | 9ce338c195bdc123633c3018a91908df26848da6 (diff) | |
download | sink-f89e43b3603976bc0e6eb885b3b9a43a6caff1c2.tar.gz sink-f89e43b3603976bc0e6eb885b3b9a43a6caff1c2.zip |
Moved client and dummyresource to examples/
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 5 | ||||
-rw-r--r-- | examples/client/CMakeLists.txt | 8 | ||||
-rw-r--r-- | examples/client/main.cpp | 52 | ||||
-rw-r--r-- | examples/dummyresource/CMakeLists.txt | 12 | ||||
-rw-r--r-- | examples/dummyresource/domainadaptor.cpp | 60 | ||||
-rw-r--r-- | examples/dummyresource/domainadaptor.h | 14 | ||||
-rw-r--r-- | examples/dummyresource/dummycalendar.fbs | 13 | ||||
-rw-r--r-- | examples/dummyresource/facade.cpp | 189 | ||||
-rw-r--r-- | examples/dummyresource/facade.h | 45 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 428 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.h | 65 |
11 files changed, 891 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..ea2b0ce --- /dev/null +++ b/examples/CMakeLists.txt | |||
@@ -0,0 +1,5 @@ | |||
1 | # the client | ||
2 | add_subdirectory(client) | ||
3 | |||
4 | # a simple dummy resource implementation | ||
5 | add_subdirectory(dummyresource) | ||
diff --git a/examples/client/CMakeLists.txt b/examples/client/CMakeLists.txt new file mode 100644 index 0000000..3555b3e --- /dev/null +++ b/examples/client/CMakeLists.txt | |||
@@ -0,0 +1,8 @@ | |||
1 | project(akonadi2_client) | ||
2 | |||
3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
4 | |||
5 | add_executable(${PROJECT_NAME} main.cpp) | ||
6 | target_link_libraries(${PROJECT_NAME} akonadi2common) | ||
7 | qt5_use_modules(${PROJECT_NAME} Widgets Network) | ||
8 | install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) | ||
diff --git a/examples/client/main.cpp b/examples/client/main.cpp new file mode 100644 index 0000000..b4cb081 --- /dev/null +++ b/examples/client/main.cpp | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
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 <QApplication> | ||
21 | #include <QCommandLineParser> | ||
22 | |||
23 | #include "common/commands.h" | ||
24 | #include "common/console.h" | ||
25 | #include "common/resourceaccess.h" | ||
26 | |||
27 | int main(int argc, char *argv[]) | ||
28 | { | ||
29 | QApplication app(argc, argv); | ||
30 | |||
31 | new Akonadi2::Console("Akonadi2 Client"); | ||
32 | Akonadi2::Console::main()->log(QString("PID: %1").arg(QCoreApplication::applicationPid())); | ||
33 | |||
34 | QCommandLineParser cliOptions; | ||
35 | cliOptions.addPositionalArgument(QObject::tr("[resource]"), | ||
36 | QObject::tr("A resource to connect to")); | ||
37 | cliOptions.process(app); | ||
38 | QStringList resources = cliOptions.positionalArguments(); | ||
39 | if (resources.isEmpty()) { | ||
40 | resources << "org.kde.dummy"; | ||
41 | } | ||
42 | |||
43 | for (const QString &resource: resources) { | ||
44 | Akonadi2::ResourceAccess *resAccess = new Akonadi2::ResourceAccess(resource.toLatin1()); | ||
45 | QObject::connect(&app, &QCoreApplication::aboutToQuit, | ||
46 | resAccess, &Akonadi2::ResourceAccess::close); | ||
47 | resAccess->sendCommand(Akonadi2::Commands::SynchronizeCommand); | ||
48 | resAccess->open(); | ||
49 | } | ||
50 | |||
51 | return app.exec(); | ||
52 | } | ||
diff --git a/examples/dummyresource/CMakeLists.txt b/examples/dummyresource/CMakeLists.txt new file mode 100644 index 0000000..abd315f --- /dev/null +++ b/examples/dummyresource/CMakeLists.txt | |||
@@ -0,0 +1,12 @@ | |||
1 | project(akonadi2_resource_dummy) | ||
2 | |||
3 | add_definitions(-DQT_PLUGIN) | ||
4 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | ||
5 | |||
6 | generate_flatbuffers(dummycalendar) | ||
7 | |||
8 | add_library(${PROJECT_NAME} SHARED facade.cpp resourcefactory.cpp domainadaptor.cpp) | ||
9 | qt5_use_modules(${PROJECT_NAME} Core Network) | ||
10 | target_link_libraries(${PROJECT_NAME} akonadi2common) | ||
11 | |||
12 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${AKONADI2_RESOURCE_PLUGINS_PATH}) | ||
diff --git a/examples/dummyresource/domainadaptor.cpp b/examples/dummyresource/domainadaptor.cpp new file mode 100644 index 0000000..8649bc3 --- /dev/null +++ b/examples/dummyresource/domainadaptor.cpp | |||
@@ -0,0 +1,60 @@ | |||
1 | |||
2 | #include "domainadaptor.h" | ||
3 | |||
4 | #include <QDebug> | ||
5 | #include <functional> | ||
6 | |||
7 | #include "dummycalendar_generated.h" | ||
8 | #include "event_generated.h" | ||
9 | #include "entity_generated.h" | ||
10 | #include "metadata_generated.h" | ||
11 | #include "domainadaptor.h" | ||
12 | #include "log.h" | ||
13 | #include <common/entitybuffer.h> | ||
14 | |||
15 | using namespace DummyCalendar; | ||
16 | using namespace flatbuffers; | ||
17 | |||
18 | |||
19 | |||
20 | |||
21 | DummyEventAdaptorFactory::DummyEventAdaptorFactory() | ||
22 | : DomainTypeAdaptorFactory() | ||
23 | { | ||
24 | //TODO turn this into initializeReadPropertyMapper as well? | ||
25 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { | ||
26 | return propertyToVariant<QString>(buffer->summary()); | ||
27 | }); | ||
28 | |||
29 | mResourceWriteMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(DummyEventBuilder &)> { | ||
30 | auto offset = variantToProperty<QString>(value, fbb); | ||
31 | return [offset](DummyEventBuilder &builder) { builder.add_summary(offset); }; | ||
32 | }); | ||
33 | } | ||
34 | |||
35 | |||
36 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) | ||
37 | { | ||
38 | flatbuffers::FlatBufferBuilder localFbb; | ||
39 | if (mLocalWriteMapper) { | ||
40 | auto pos = createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::Event>(event, localFbb, *mLocalWriteMapper); | ||
41 | Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, pos); | ||
42 | flatbuffers::Verifier verifier(localFbb.GetBufferPointer(), localFbb.GetSize()); | ||
43 | if (!verifier.VerifyBuffer<Akonadi2::ApplicationDomain::Buffer::Event>()) { | ||
44 | Warning() << "Created invalid local buffer"; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | flatbuffers::FlatBufferBuilder resFbb; | ||
49 | if (mResourceWriteMapper) { | ||
50 | auto pos = createBufferPart<DummyEventBuilder, DummyEvent>(event, resFbb, *mResourceWriteMapper); | ||
51 | DummyCalendar::FinishDummyEventBuffer(resFbb, pos); | ||
52 | flatbuffers::Verifier verifier(resFbb.GetBufferPointer(), resFbb.GetSize()); | ||
53 | if (!verifier.VerifyBuffer<DummyEvent>()) { | ||
54 | Warning() << "Created invalid resource buffer"; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); | ||
59 | } | ||
60 | |||
diff --git a/examples/dummyresource/domainadaptor.h b/examples/dummyresource/domainadaptor.h new file mode 100644 index 0000000..9d351e7 --- /dev/null +++ b/examples/dummyresource/domainadaptor.h | |||
@@ -0,0 +1,14 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "common/domainadaptor.h" | ||
4 | #include "event_generated.h" | ||
5 | #include "dummycalendar_generated.h" | ||
6 | #include "entity_generated.h" | ||
7 | |||
8 | class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder> | ||
9 | { | ||
10 | public: | ||
11 | DummyEventAdaptorFactory(); | ||
12 | virtual ~DummyEventAdaptorFactory() {}; | ||
13 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); | ||
14 | }; | ||
diff --git a/examples/dummyresource/dummycalendar.fbs b/examples/dummyresource/dummycalendar.fbs new file mode 100644 index 0000000..643c9b2 --- /dev/null +++ b/examples/dummyresource/dummycalendar.fbs | |||
@@ -0,0 +1,13 @@ | |||
1 | // example IDL file | ||
2 | |||
3 | namespace DummyCalendar; | ||
4 | |||
5 | table DummyEvent { | ||
6 | summary:string; | ||
7 | description:string; | ||
8 | attachment:[ubyte]; | ||
9 | remoteId:string; | ||
10 | } | ||
11 | |||
12 | root_type DummyEvent; | ||
13 | file_identifier "AKFB"; | ||
diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp new file mode 100644 index 0000000..e50e4f3 --- /dev/null +++ b/examples/dummyresource/facade.cpp | |||
@@ -0,0 +1,189 @@ | |||
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 | #include "facade.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | #include <functional> | ||
24 | |||
25 | #include "common/resourceaccess.h" | ||
26 | #include "common/commands.h" | ||
27 | #include "dummycalendar_generated.h" | ||
28 | #include "event_generated.h" | ||
29 | #include "entity_generated.h" | ||
30 | #include "metadata_generated.h" | ||
31 | #include "domainadaptor.h" | ||
32 | #include <common/entitybuffer.h> | ||
33 | #include <common/index.h> | ||
34 | #include <common/log.h> | ||
35 | |||
36 | using namespace DummyCalendar; | ||
37 | using namespace flatbuffers; | ||
38 | |||
39 | |||
40 | DummyResourceFacade::DummyResourceFacade() | ||
41 | : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>("org.kde.dummy"), | ||
42 | mFactory(new DummyEventAdaptorFactory) | ||
43 | { | ||
44 | } | ||
45 | |||
46 | DummyResourceFacade::~DummyResourceFacade() | ||
47 | { | ||
48 | } | ||
49 | |||
50 | Async::Job<void> DummyResourceFacade::create(const Akonadi2::ApplicationDomain::Event &domainObject) | ||
51 | { | ||
52 | flatbuffers::FlatBufferBuilder entityFbb; | ||
53 | mFactory->createBuffer(domainObject, entityFbb); | ||
54 | return sendCreateCommand("event", QByteArray::fromRawData(reinterpret_cast<const char*>(entityFbb.GetBufferPointer()), entityFbb.GetSize())); | ||
55 | } | ||
56 | |||
57 | Async::Job<void> DummyResourceFacade::modify(const Akonadi2::ApplicationDomain::Event &domainObject) | ||
58 | { | ||
59 | //Create message buffer and send to resource | ||
60 | return Async::null<void>(); | ||
61 | } | ||
62 | |||
63 | Async::Job<void> DummyResourceFacade::remove(const Akonadi2::ApplicationDomain::Event &domainObject) | ||
64 | { | ||
65 | //Create message buffer and send to resource | ||
66 | return Async::null<void>(); | ||
67 | } | ||
68 | |||
69 | static std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)> prepareQuery(const Akonadi2::Query &query) | ||
70 | { | ||
71 | //Compose some functions to make query matching fast. | ||
72 | //This way we can process the query once, and convert all values into something that can be compared quickly | ||
73 | std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)> preparedQuery; | ||
74 | if (!query.ids.isEmpty()) { | ||
75 | //Match by id | ||
76 | //TODO: for id's a direct lookup would be way faster | ||
77 | |||
78 | //We convert the id's to std::string so we don't have to convert each key during the scan. (This runs only once, and the query will be run for every key) | ||
79 | //Probably a premature optimization, but perhaps a useful technique to be investigated. | ||
80 | QVector<std::string> ids; | ||
81 | for (const auto &id : query.ids) { | ||
82 | ids << id.toStdString(); | ||
83 | } | ||
84 | preparedQuery = [ids](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
85 | if (ids.contains(key)) { | ||
86 | return true; | ||
87 | } | ||
88 | return false; | ||
89 | }; | ||
90 | } else if (!query.propertyFilter.isEmpty()) { | ||
91 | if (query.propertyFilter.contains("uid")) { | ||
92 | const QByteArray uid = query.propertyFilter.value("uid").toByteArray(); | ||
93 | preparedQuery = [uid](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
94 | if (local && local->uid() && (QByteArray::fromRawData(local->uid()->c_str(), local->uid()->size()) == uid)) { | ||
95 | return true; | ||
96 | } | ||
97 | return false; | ||
98 | }; | ||
99 | } | ||
100 | } else { | ||
101 | //Match everything | ||
102 | preparedQuery = [](const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local) { | ||
103 | return true; | ||
104 | }; | ||
105 | } | ||
106 | return preparedQuery; | ||
107 | } | ||
108 | |||
109 | void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)> preparedQuery) | ||
110 | { | ||
111 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
112 | |||
113 | //Skip internals | ||
114 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
115 | return true; | ||
116 | } | ||
117 | |||
118 | //Extract buffers | ||
119 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
120 | |||
121 | const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
122 | const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
123 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
124 | |||
125 | if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
126 | qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
127 | return true; | ||
128 | } | ||
129 | |||
130 | //We probably only want to create all buffers after the scan | ||
131 | //TODO use adapter for query and scan? | ||
132 | if (preparedQuery && preparedQuery(std::string(static_cast<char*>(keyValue), keySize), resourceBuffer, localBuffer)) { | ||
133 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
134 | //This only works for a 1:1 mapping of resource to domain types. | ||
135 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
136 | auto adaptor = mFactory->createAdaptor(buffer.entity()); | ||
137 | //TODO only copy requested properties | ||
138 | auto memoryAdaptor = QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create(*adaptor); | ||
139 | // here we could copy additional properties that don't have a 1:1 mapping, such as separately stored tags. | ||
140 | auto event = QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("org.kde.dummy", QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), revision, memoryAdaptor); | ||
141 | resultCallback(event); | ||
142 | } | ||
143 | return true; | ||
144 | }, | ||
145 | [](const Akonadi2::Storage::Error &error) { | ||
146 | qWarning() << "Error during query: " << error.message; | ||
147 | }); | ||
148 | } | ||
149 | |||
150 | Async::Job<qint64> DummyResourceFacade::load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) | ||
151 | { | ||
152 | return Async::start<qint64>([=](Async::Future<qint64> &future) { | ||
153 | //Now that the sync is complete we can execute the query | ||
154 | const auto preparedQuery = prepareQuery(query); | ||
155 | |||
156 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), "org.kde.dummy"); | ||
157 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
158 | Warning() << "Error during query: " << error.store << error.message; | ||
159 | }); | ||
160 | |||
161 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
162 | const qint64 revision = storage->maxRevision(); | ||
163 | |||
164 | //Index lookups | ||
165 | QVector<QByteArray> keys; | ||
166 | if (query.propertyFilter.contains("uid")) { | ||
167 | static Index uidIndex(Akonadi2::Store::storageLocation(), "org.kde.dummy.index.uid", Akonadi2::Storage::ReadOnly); | ||
168 | uidIndex.lookup(query.propertyFilter.value("uid").toByteArray(), [&](const QByteArray &value) { | ||
169 | keys << value; | ||
170 | }, | ||
171 | [](const Index::Error &error) { | ||
172 | Warning() << "Error in index: " << error.message; | ||
173 | }); | ||
174 | } | ||
175 | |||
176 | if (keys.isEmpty()) { | ||
177 | Log() << "Executing a full scan"; | ||
178 | readValue(storage, QByteArray(), resultCallback, preparedQuery); | ||
179 | } else { | ||
180 | for (const auto &key : keys) { | ||
181 | readValue(storage, key, resultCallback, preparedQuery); | ||
182 | } | ||
183 | } | ||
184 | storage->abortTransaction(); | ||
185 | future.setValue(revision); | ||
186 | future.setFinished(); | ||
187 | }); | ||
188 | } | ||
189 | |||
diff --git a/examples/dummyresource/facade.h b/examples/dummyresource/facade.h new file mode 100644 index 0000000..91ae351 --- /dev/null +++ b/examples/dummyresource/facade.h | |||
@@ -0,0 +1,45 @@ | |||
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 "common/facade.h" | ||
23 | |||
24 | #include "common/clientapi.h" | ||
25 | #include "common/storage.h" | ||
26 | #include "resourcefactory.h" | ||
27 | #include "entity_generated.h" | ||
28 | #include "event_generated.h" | ||
29 | #include "dummycalendar_generated.h" | ||
30 | #include "common/domainadaptor.h" | ||
31 | |||
32 | class DummyResourceFacade : public Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event> | ||
33 | { | ||
34 | public: | ||
35 | DummyResourceFacade(); | ||
36 | virtual ~DummyResourceFacade(); | ||
37 | Async::Job<void> create(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; | ||
38 | Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; | ||
39 | Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; | ||
40 | Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) Q_DECL_OVERRIDE; | ||
41 | |||
42 | private: | ||
43 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); | ||
44 | QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder> > mFactory; | ||
45 | }; | ||
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp new file mode 100644 index 0000000..d5765e2 --- /dev/null +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -0,0 +1,428 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
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 "resourcefactory.h" | ||
21 | #include "facade.h" | ||
22 | #include "entitybuffer.h" | ||
23 | #include "pipeline.h" | ||
24 | #include "dummycalendar_generated.h" | ||
25 | #include "metadata_generated.h" | ||
26 | #include "queuedcommand_generated.h" | ||
27 | #include "createentity_generated.h" | ||
28 | #include "domainadaptor.h" | ||
29 | #include "commands.h" | ||
30 | #include "clientapi.h" | ||
31 | #include "index.h" | ||
32 | #include "log.h" | ||
33 | #include <QUuid> | ||
34 | #include <assert.h> | ||
35 | |||
36 | |||
37 | /* | ||
38 | * Figure out how to implement various classes of processors: | ||
39 | * * read-only (index and such) => extractor function, probably using domain adaptor | ||
40 | * * filter => provide means to move entity elsewhere, and also reflect change in source (I guess?) | ||
41 | * * flag extractors? => like read-only? Or write to local portion of buffer? | ||
42 | * ** $ISSPAM should become part of domain object and is written to the local part of the mail. | ||
43 | * ** => value could be calculated by the server directly | ||
44 | */ | ||
45 | class SimpleProcessor : public Akonadi2::Preprocessor | ||
46 | { | ||
47 | public: | ||
48 | SimpleProcessor(const QString &id, const std::function<void(const Akonadi2::PipelineState &state, const Akonadi2::Entity &e)> &f) | ||
49 | : Akonadi2::Preprocessor(), | ||
50 | mFunction(f), | ||
51 | mId(id) | ||
52 | { | ||
53 | } | ||
54 | |||
55 | void process(const Akonadi2::PipelineState &state, const Akonadi2::Entity &e) Q_DECL_OVERRIDE | ||
56 | { | ||
57 | mFunction(state, e); | ||
58 | processingCompleted(state); | ||
59 | } | ||
60 | |||
61 | QString id() const | ||
62 | { | ||
63 | return mId; | ||
64 | } | ||
65 | |||
66 | protected: | ||
67 | std::function<void(const Akonadi2::PipelineState &state, const Akonadi2::Entity &e)> mFunction; | ||
68 | QString mId; | ||
69 | }; | ||
70 | |||
71 | |||
72 | |||
73 | static std::string createEvent() | ||
74 | { | ||
75 | static const size_t attachmentSize = 1024*2; // 2KB | ||
76 | static uint8_t rawData[attachmentSize]; | ||
77 | static flatbuffers::FlatBufferBuilder fbb; | ||
78 | fbb.Clear(); | ||
79 | { | ||
80 | uint8_t *rawDataPtr = Q_NULLPTR; | ||
81 | auto summary = fbb.CreateString("summary"); | ||
82 | auto data = fbb.CreateUninitializedVector<uint8_t>(attachmentSize, &rawDataPtr); | ||
83 | DummyCalendar::DummyEventBuilder eventBuilder(fbb); | ||
84 | eventBuilder.add_summary(summary); | ||
85 | eventBuilder.add_attachment(data); | ||
86 | auto eventLocation = eventBuilder.Finish(); | ||
87 | DummyCalendar::FinishDummyEventBuffer(fbb, eventLocation); | ||
88 | memcpy((void*)rawDataPtr, rawData, attachmentSize); | ||
89 | } | ||
90 | |||
91 | return std::string(reinterpret_cast<const char *>(fbb.GetBufferPointer()), fbb.GetSize()); | ||
92 | } | ||
93 | |||
94 | QMap<QString, QString> populate() | ||
95 | { | ||
96 | QMap<QString, QString> content; | ||
97 | for (int i = 0; i < 2; i++) { | ||
98 | auto event = createEvent(); | ||
99 | content.insert(QString("key%1").arg(i), QString::fromStdString(event)); | ||
100 | } | ||
101 | return content; | ||
102 | } | ||
103 | |||
104 | static QMap<QString, QString> s_dataSource = populate(); | ||
105 | |||
106 | //Drives the pipeline using the output from all command queues | ||
107 | class Processor : public QObject | ||
108 | { | ||
109 | Q_OBJECT | ||
110 | public: | ||
111 | Processor(Akonadi2::Pipeline *pipeline, QList<MessageQueue*> commandQueues) | ||
112 | : QObject(), | ||
113 | mPipeline(pipeline), | ||
114 | mCommandQueues(commandQueues), | ||
115 | mProcessingLock(false) | ||
116 | { | ||
117 | for (auto queue : mCommandQueues) { | ||
118 | const bool ret = connect(queue, &MessageQueue::messageReady, this, &Processor::process); | ||
119 | Q_UNUSED(ret); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | signals: | ||
124 | void error(int errorCode, const QString &errorMessage); | ||
125 | |||
126 | private: | ||
127 | bool messagesToProcessAvailable() | ||
128 | { | ||
129 | for (auto queue : mCommandQueues) { | ||
130 | if (!queue->isEmpty()) { | ||
131 | return true; | ||
132 | } | ||
133 | } | ||
134 | return false; | ||
135 | } | ||
136 | |||
137 | private slots: | ||
138 | void process() | ||
139 | { | ||
140 | if (mProcessingLock) { | ||
141 | return; | ||
142 | } | ||
143 | mProcessingLock = true; | ||
144 | auto job = processPipeline().then<void>([this]() { | ||
145 | mProcessingLock = false; | ||
146 | if (messagesToProcessAvailable()) { | ||
147 | process(); | ||
148 | } | ||
149 | }).exec(); | ||
150 | } | ||
151 | |||
152 | Async::Job<void> processQueuedCommand(const Akonadi2::QueuedCommand *queuedCommand) | ||
153 | { | ||
154 | Log() << "Processing command: " << Akonadi2::Commands::name(queuedCommand->commandId()); | ||
155 | //Throw command into appropriate pipeline | ||
156 | switch (queuedCommand->commandId()) { | ||
157 | case Akonadi2::Commands::DeleteEntityCommand: | ||
158 | //mPipeline->removedEntity | ||
159 | return Async::null<void>(); | ||
160 | case Akonadi2::Commands::ModifyEntityCommand: | ||
161 | //mPipeline->modifiedEntity | ||
162 | return Async::null<void>(); | ||
163 | case Akonadi2::Commands::CreateEntityCommand: | ||
164 | return mPipeline->newEntity(queuedCommand->command()->Data(), queuedCommand->command()->size()); | ||
165 | default: | ||
166 | return Async::error<void>(-1, "Unhandled command"); | ||
167 | } | ||
168 | return Async::null<void>(); | ||
169 | } | ||
170 | |||
171 | //Process all messages of this queue | ||
172 | Async::Job<void> processQueue(MessageQueue *queue) | ||
173 | { | ||
174 | //TODO use something like: | ||
175 | //Async::foreach("pass iterator here").each("process value here").join(); | ||
176 | //Async::foreach("pass iterator here").parallel("process value here").join(); | ||
177 | return Async::dowhile( | ||
178 | [this, queue](Async::Future<bool> &future) { | ||
179 | if (queue->isEmpty()) { | ||
180 | future.setValue(false); | ||
181 | future.setFinished(); | ||
182 | return; | ||
183 | } | ||
184 | queue->dequeue( | ||
185 | [this, &future](void *ptr, int size, std::function<void(bool success)> messageQueueCallback) { | ||
186 | auto callback = [messageQueueCallback, &future](bool success) { | ||
187 | messageQueueCallback(success); | ||
188 | future.setValue(!success); | ||
189 | future.setFinished(); | ||
190 | }; | ||
191 | |||
192 | flatbuffers::Verifier verifyer(reinterpret_cast<const uint8_t *>(ptr), size); | ||
193 | if (!Akonadi2::VerifyQueuedCommandBuffer(verifyer)) { | ||
194 | Warning() << "invalid buffer"; | ||
195 | callback(false); | ||
196 | return; | ||
197 | } | ||
198 | auto queuedCommand = Akonadi2::GetQueuedCommand(ptr); | ||
199 | Trace() << "Dequeued Command: " << Akonadi2::Commands::name(queuedCommand->commandId()); | ||
200 | //TODO JOBAPI: job lifetime management | ||
201 | //Right now we're just leaking jobs. In this case we'd like jobs that are heap allocated and delete | ||
202 | //themselves once done. In other cases we'd like jobs that only live as long as their handle though. | ||
203 | //FIXME this job is stack allocated and thus simply dies.... | ||
204 | processQueuedCommand(queuedCommand).then<void>( | ||
205 | [callback]() { | ||
206 | callback(true); | ||
207 | }, | ||
208 | [callback](int errorCode, QString errorMessage) { | ||
209 | Warning() << "Error while processing queue command: " << errorMessage; | ||
210 | callback(false); | ||
211 | } | ||
212 | ).exec(); | ||
213 | }, | ||
214 | [&future](const MessageQueue::Error &error) { | ||
215 | Warning() << "Error while getting message from messagequeue: " << error.message; | ||
216 | future.setValue(false); | ||
217 | future.setFinished(); | ||
218 | } | ||
219 | ); | ||
220 | } | ||
221 | ); | ||
222 | } | ||
223 | |||
224 | Async::Job<void> processPipeline() | ||
225 | { | ||
226 | //Go through all message queues | ||
227 | auto it = QSharedPointer<QListIterator<MessageQueue*> >::create(mCommandQueues); | ||
228 | return Async::dowhile( | ||
229 | [it]() { return it->hasNext(); }, | ||
230 | [it, this](Async::Future<void> &future) { | ||
231 | auto queue = it->next(); | ||
232 | processQueue(queue).then<void>([&future]() { | ||
233 | Trace() << "Queue processed"; | ||
234 | future.setFinished(); | ||
235 | }).exec(); | ||
236 | } | ||
237 | ); | ||
238 | } | ||
239 | |||
240 | private: | ||
241 | Akonadi2::Pipeline *mPipeline; | ||
242 | //Ordered by priority | ||
243 | QList<MessageQueue*> mCommandQueues; | ||
244 | bool mProcessingLock; | ||
245 | }; | ||
246 | |||
247 | DummyResource::DummyResource() | ||
248 | : Akonadi2::Resource(), | ||
249 | mUserQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde.dummy.userqueue"), | ||
250 | mSynchronizerQueue(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde.dummy.synchronizerqueue"), | ||
251 | mError(0) | ||
252 | { | ||
253 | } | ||
254 | |||
255 | void DummyResource::configurePipeline(Akonadi2::Pipeline *pipeline) | ||
256 | { | ||
257 | auto eventFactory = QSharedPointer<DummyEventAdaptorFactory>::create(); | ||
258 | //FIXME we should setup for each resource entity type, not for each domain type | ||
259 | //i.e. If a resource stores tags as part of each message it needs to update the tag index | ||
260 | //TODO setup preprocessors for each resource entity type and pipeline type allowing full customization | ||
261 | //Eventually the order should be self configuring, for now it's hardcoded. | ||
262 | auto eventIndexer = new SimpleProcessor("summaryprocessor", [eventFactory](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity) { | ||
263 | auto adaptor = eventFactory->createAdaptor(entity); | ||
264 | // Log() << "Summary preprocessor: " << adaptor->getProperty("summary").toString(); | ||
265 | }); | ||
266 | |||
267 | auto uidIndexer = new SimpleProcessor("uidIndexer", [eventFactory](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity) { | ||
268 | static Index uidIndex(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/akonadi2/storage", "org.kde.dummy.index.uid", Akonadi2::Storage::ReadWrite); | ||
269 | |||
270 | //TODO: Benchmark if this is performance wise acceptable, or if we have to access the buffer directly | ||
271 | auto adaptor = eventFactory->createAdaptor(entity); | ||
272 | const auto uid = adaptor->getProperty("uid"); | ||
273 | if (uid.isValid()) { | ||
274 | uidIndex.add(uid.toByteArray(), state.key()); | ||
275 | } | ||
276 | }); | ||
277 | |||
278 | //event is the entitytype and not the domain type | ||
279 | pipeline->setPreprocessors("event", Akonadi2::Pipeline::NewPipeline, QVector<Akonadi2::Preprocessor*>() << eventIndexer << uidIndexer); | ||
280 | mProcessor = new Processor(pipeline, QList<MessageQueue*>() << &mUserQueue << &mSynchronizerQueue); | ||
281 | QObject::connect(mProcessor, &Processor::error, [this](int errorCode, const QString &msg) { onProcessorError(errorCode, msg); }); | ||
282 | } | ||
283 | |||
284 | void DummyResource::onProcessorError(int errorCode, const QString &errorMessage) | ||
285 | { | ||
286 | Warning() << "Received error from Processor: " << errorCode << errorMessage; | ||
287 | mError = errorCode; | ||
288 | } | ||
289 | |||
290 | int DummyResource::error() const | ||
291 | { | ||
292 | return mError; | ||
293 | } | ||
294 | |||
295 | void findByRemoteId(QSharedPointer<Akonadi2::Storage> storage, const QString &rid, std::function<void(void *keyValue, int keySize, void *dataValue, int dataSize)> callback) | ||
296 | { | ||
297 | //TODO lookup in rid index instead of doing a full scan | ||
298 | const std::string ridString = rid.toStdString(); | ||
299 | storage->scan("", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
300 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
301 | return true; | ||
302 | } | ||
303 | |||
304 | Akonadi2::EntityBuffer::extractResourceBuffer(dataValue, dataSize, [&](const uint8_t *buffer, size_t size) { | ||
305 | flatbuffers::Verifier verifier(buffer, size); | ||
306 | if (DummyCalendar::VerifyDummyEventBuffer(verifier)) { | ||
307 | DummyCalendar::DummyEvent const *resourceBuffer = DummyCalendar::GetDummyEvent(buffer); | ||
308 | if (resourceBuffer && resourceBuffer->remoteId()) { | ||
309 | if (std::string(resourceBuffer->remoteId()->c_str(), resourceBuffer->remoteId()->size()) == ridString) { | ||
310 | callback(keyValue, keySize, dataValue, dataSize); | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | }); | ||
315 | return true; | ||
316 | }); | ||
317 | } | ||
318 | |||
319 | void DummyResource::enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data) | ||
320 | { | ||
321 | m_fbb.Clear(); | ||
322 | auto commandData = Akonadi2::EntityBuffer::appendAsVector(m_fbb, data.constData(), data.size()); | ||
323 | auto buffer = Akonadi2::CreateQueuedCommand(m_fbb, commandId, commandData); | ||
324 | Akonadi2::FinishQueuedCommandBuffer(m_fbb, buffer); | ||
325 | mq.enqueue(m_fbb.GetBufferPointer(), m_fbb.GetSize()); | ||
326 | } | ||
327 | |||
328 | Async::Job<void> DummyResource::synchronizeWithSource(Akonadi2::Pipeline *pipeline) | ||
329 | { | ||
330 | return Async::start<void>([this, pipeline](Async::Future<void> &f) { | ||
331 | //TODO use a read-only transaction during the complete sync to sync against a defined revision | ||
332 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), "org.kde.dummy"); | ||
333 | for (auto it = s_dataSource.constBegin(); it != s_dataSource.constEnd(); it++) { | ||
334 | bool isNew = true; | ||
335 | if (storage->exists()) { | ||
336 | findByRemoteId(storage, it.key(), [&](void *keyValue, int keySize, void *dataValue, int dataSize) { | ||
337 | isNew = false; | ||
338 | }); | ||
339 | } | ||
340 | if (isNew) { | ||
341 | m_fbb.Clear(); | ||
342 | |||
343 | const QByteArray data = it.value().toUtf8(); | ||
344 | auto eventBuffer = DummyCalendar::GetDummyEvent(data.data()); | ||
345 | |||
346 | //Map the source format to the buffer format (which happens to be an exact copy here) | ||
347 | auto summary = m_fbb.CreateString(eventBuffer->summary()->c_str()); | ||
348 | auto rid = m_fbb.CreateString(it.key().toStdString().c_str()); | ||
349 | auto description = m_fbb.CreateString(it.key().toStdString().c_str()); | ||
350 | static uint8_t rawData[100]; | ||
351 | auto attachment = Akonadi2::EntityBuffer::appendAsVector(m_fbb, rawData, 100); | ||
352 | |||
353 | auto builder = DummyCalendar::DummyEventBuilder(m_fbb); | ||
354 | builder.add_summary(summary); | ||
355 | builder.add_remoteId(rid); | ||
356 | builder.add_description(description); | ||
357 | builder.add_attachment(attachment); | ||
358 | auto buffer = builder.Finish(); | ||
359 | DummyCalendar::FinishDummyEventBuffer(m_fbb, buffer); | ||
360 | flatbuffers::FlatBufferBuilder entityFbb; | ||
361 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize(), 0, 0); | ||
362 | |||
363 | flatbuffers::FlatBufferBuilder fbb; | ||
364 | //This is the resource type and not the domain type | ||
365 | auto type = fbb.CreateString("event"); | ||
366 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); | ||
367 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
368 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
369 | |||
370 | enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); | ||
371 | } else { //modification | ||
372 | //TODO diff and create modification if necessary | ||
373 | } | ||
374 | } | ||
375 | //TODO find items to remove | ||
376 | f.setFinished(); | ||
377 | }); | ||
378 | } | ||
379 | |||
380 | Async::Job<void> DummyResource::processAllMessages() | ||
381 | { | ||
382 | //We have to wait for all items to be processed to ensure the synced items are available when a query gets executed. | ||
383 | //TODO: report errors while processing sync? | ||
384 | //TODO JOBAPI: A helper that waits for n events and then continues? | ||
385 | return Async::start<void>([this](Async::Future<void> &f) { | ||
386 | if (mSynchronizerQueue.isEmpty()) { | ||
387 | f.setFinished(); | ||
388 | } else { | ||
389 | QObject::connect(&mSynchronizerQueue, &MessageQueue::drained, [&f]() { | ||
390 | f.setFinished(); | ||
391 | }); | ||
392 | } | ||
393 | }).then<void>([this](Async::Future<void> &f) { | ||
394 | if (mUserQueue.isEmpty()) { | ||
395 | f.setFinished(); | ||
396 | } else { | ||
397 | QObject::connect(&mUserQueue, &MessageQueue::drained, [&f]() { | ||
398 | f.setFinished(); | ||
399 | }); | ||
400 | } | ||
401 | }); | ||
402 | } | ||
403 | |||
404 | void DummyResource::processCommand(int commandId, const QByteArray &data, uint size, Akonadi2::Pipeline *pipeline) | ||
405 | { | ||
406 | //TODO instead of copying the command including the full entity first into the command queue, we could directly | ||
407 | //create a new revision, only pushing a handle into the commandqueue with the relevant changeset (for changereplay). | ||
408 | //The problem is that we then require write access from multiple threads (or even processes to avoid sending the full entity over the wire). | ||
409 | enqueueCommand(mUserQueue, commandId, data); | ||
410 | } | ||
411 | |||
412 | DummyResourceFactory::DummyResourceFactory(QObject *parent) | ||
413 | : Akonadi2::ResourceFactory(parent) | ||
414 | { | ||
415 | |||
416 | } | ||
417 | |||
418 | Akonadi2::Resource *DummyResourceFactory::createResource() | ||
419 | { | ||
420 | return new DummyResource(); | ||
421 | } | ||
422 | |||
423 | void DummyResourceFactory::registerFacades(Akonadi2::FacadeFactory &factory) | ||
424 | { | ||
425 | factory.registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>(PLUGIN_NAME); | ||
426 | } | ||
427 | |||
428 | #include "resourcefactory.moc" | ||
diff --git a/examples/dummyresource/resourcefactory.h b/examples/dummyresource/resourcefactory.h new file mode 100644 index 0000000..3b99d5e --- /dev/null +++ b/examples/dummyresource/resourcefactory.h | |||
@@ -0,0 +1,65 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
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 "common/resource.h" | ||
23 | #include "async/src/async.h" | ||
24 | #include "common/messagequeue.h" | ||
25 | |||
26 | #include <flatbuffers/flatbuffers.h> | ||
27 | |||
28 | //TODO: a little ugly to have this in two places, once here and once in Q_PLUGIN_METADATA | ||
29 | #define PLUGIN_NAME "org.kde.dummy" | ||
30 | |||
31 | class Processor; | ||
32 | |||
33 | class DummyResource : public Akonadi2::Resource | ||
34 | { | ||
35 | public: | ||
36 | DummyResource(); | ||
37 | Async::Job<void> synchronizeWithSource(Akonadi2::Pipeline *pipeline); | ||
38 | Async::Job<void> processAllMessages(); | ||
39 | void processCommand(int commandId, const QByteArray &data, uint size, Akonadi2::Pipeline *pipeline); | ||
40 | void configurePipeline(Akonadi2::Pipeline *pipeline); | ||
41 | int error() const; | ||
42 | |||
43 | private: | ||
44 | void onProcessorError(int errorCode, const QString &errorMessage); | ||
45 | void enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data); | ||
46 | flatbuffers::FlatBufferBuilder m_fbb; | ||
47 | MessageQueue mUserQueue; | ||
48 | MessageQueue mSynchronizerQueue; | ||
49 | Processor *mProcessor; | ||
50 | int mError; | ||
51 | }; | ||
52 | |||
53 | class DummyResourceFactory : public Akonadi2::ResourceFactory | ||
54 | { | ||
55 | Q_OBJECT | ||
56 | Q_PLUGIN_METADATA(IID "org.kde.dummy") | ||
57 | Q_INTERFACES(Akonadi2::ResourceFactory) | ||
58 | |||
59 | public: | ||
60 | DummyResourceFactory(QObject *parent = 0); | ||
61 | |||
62 | Akonadi2::Resource *createResource(); | ||
63 | void registerFacades(Akonadi2::FacadeFactory &factory); | ||
64 | }; | ||
65 | |||