diff options
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 117 | ||||
-rw-r--r-- | examples/dummyresource/resourcefactory.h | 1 |
2 files changed, 46 insertions, 72 deletions
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 9bb20e5..65b3c66 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -35,6 +35,7 @@ | |||
35 | #include "dummystore.h" | 35 | #include "dummystore.h" |
36 | #include "definitions.h" | 36 | #include "definitions.h" |
37 | #include "facadefactory.h" | 37 | #include "facadefactory.h" |
38 | #include <QDate> | ||
38 | 39 | ||
39 | //This is the resources entity type, and not the domain type | 40 | //This is the resources entity type, and not the domain type |
40 | #define ENTITY_TYPE_EVENT "event" | 41 | #define ENTITY_TYPE_EVENT "event" |
@@ -104,18 +105,17 @@ void DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, | |||
104 | { | 105 | { |
105 | //Map the source format to the buffer format (which happens to be an exact copy here) | 106 | //Map the source format to the buffer format (which happens to be an exact copy here) |
106 | auto subject = m_fbb.CreateString(data.value("subject").toString().toStdString()); | 107 | auto subject = m_fbb.CreateString(data.value("subject").toString().toStdString()); |
107 | auto rid = m_fbb.CreateString(std::string(ridBuffer.constData(), ridBuffer.size())); | ||
108 | auto sender = m_fbb.CreateString(data.value("sender").toString().toStdString()); | 108 | auto sender = m_fbb.CreateString(data.value("sender").toString().toStdString()); |
109 | auto senderName = m_fbb.CreateString(data.value("senderName").toString().toStdString()); | 109 | auto senderName = m_fbb.CreateString(data.value("senderName").toString().toStdString()); |
110 | auto date = m_fbb.CreateString(data.value("date").toString().toStdString()); | 110 | auto date = m_fbb.CreateString(data.value("date").toDate().toString().toStdString()); |
111 | auto folder = m_fbb.CreateString(std::string("inbox")); | 111 | auto folder = m_fbb.CreateString(std::string("inbox")); |
112 | 112 | ||
113 | auto builder = Akonadi2::ApplicationDomain::Buffer::MailBuilder(m_fbb); | 113 | auto builder = Akonadi2::ApplicationDomain::Buffer::MailBuilder(m_fbb); |
114 | builder.add_subject(subject); | 114 | builder.add_subject(subject); |
115 | builder.add_sender(sender); | 115 | builder.add_sender(sender); |
116 | builder.add_senderName(senderName); | 116 | builder.add_senderName(senderName); |
117 | builder.add_unread(true); | 117 | builder.add_unread(data.value("unread").toBool()); |
118 | builder.add_important(false); | 118 | builder.add_important(data.value("important").toBool()); |
119 | builder.add_date(date); | 119 | builder.add_date(date); |
120 | builder.add_folder(folder); | 120 | builder.add_folder(folder); |
121 | auto buffer = builder.Finish(); | 121 | auto buffer = builder.Finish(); |
@@ -123,78 +123,51 @@ void DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, | |||
123 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize()); | 123 | Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize()); |
124 | } | 124 | } |
125 | 125 | ||
126 | 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) | ||
127 | { | ||
128 | Index uidIndex("index.uid", transaction); | ||
129 | for (auto it = data.constBegin(); it != data.constEnd(); it++) { | ||
130 | bool isNew = true; | ||
131 | uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { | ||
132 | isNew = false; | ||
133 | }, | ||
134 | [](const Index::Error &error) { | ||
135 | if (error.code != Index::IndexNotAvailable) { | ||
136 | Warning() << "Error in uid index: " << error.message; | ||
137 | } | ||
138 | }); | ||
139 | if (isNew) { | ||
140 | m_fbb.Clear(); | ||
141 | |||
142 | flatbuffers::FlatBufferBuilder entityFbb; | ||
143 | createEntity(it.key().toUtf8(), it.value(), entityFbb); | ||
144 | |||
145 | flatbuffers::FlatBufferBuilder fbb; | ||
146 | //This is the resource type and not the domain type | ||
147 | auto type = fbb.CreateString(bufferType.toStdString()); | ||
148 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); | ||
149 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
150 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
151 | |||
152 | enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); | ||
153 | } else { //modification | ||
154 | //TODO diff and create modification if necessary | ||
155 | } | ||
156 | } | ||
157 | //TODO find items to remove | ||
158 | } | ||
159 | |||
126 | KAsync::Job<void> DummyResource::synchronizeWithSource() | 160 | KAsync::Job<void> DummyResource::synchronizeWithSource() |
127 | { | 161 | { |
128 | return KAsync::start<void>([this](KAsync::Future<void> &f) { | 162 | return KAsync::start<void>([this](KAsync::Future<void> &f) { |
129 | auto transaction = Akonadi2::Storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier, Akonadi2::Storage::ReadOnly).createTransaction(Akonadi2::Storage::ReadOnly); | 163 | auto transaction = Akonadi2::Storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier, Akonadi2::Storage::ReadOnly).createTransaction(Akonadi2::Storage::ReadOnly); |
130 | Index uidIndex("index.uid", transaction); | 164 | |
131 | 165 | synchronize(ENTITY_TYPE_EVENT, DummyStore::instance().events(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) { | |
132 | const auto data = DummyStore::instance().events(); | 166 | createEvent(ridBuffer, data, entityFbb); |
133 | for (auto it = data.constBegin(); it != data.constEnd(); it++) { | 167 | }); |
134 | bool isNew = true; | 168 | synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) { |
135 | uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { | 169 | createMail(ridBuffer, data, entityFbb); |
136 | isNew = false; | 170 | }); |
137 | }, | ||
138 | [](const Index::Error &error) { | ||
139 | if (error.code != Index::IndexNotAvailable) { | ||
140 | Warning() << "Error in uid index: " << error.message; | ||
141 | } | ||
142 | }); | ||
143 | if (isNew) { | ||
144 | m_fbb.Clear(); | ||
145 | |||
146 | flatbuffers::FlatBufferBuilder entityFbb; | ||
147 | createEvent(it.key().toUtf8(), it.value(), entityFbb); | ||
148 | |||
149 | flatbuffers::FlatBufferBuilder fbb; | ||
150 | //This is the resource type and not the domain type | ||
151 | auto type = fbb.CreateString(ENTITY_TYPE_EVENT); | ||
152 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); | ||
153 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
154 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
155 | |||
156 | enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); | ||
157 | } else { //modification | ||
158 | //TODO diff and create modification if necessary | ||
159 | } | ||
160 | } | ||
161 | //TODO find items to remove | ||
162 | |||
163 | const auto mails = DummyStore::instance().mails(); | ||
164 | for (auto it = mails.constBegin(); it != mails.constEnd(); it++) { | ||
165 | bool isNew = true; | ||
166 | uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) { | ||
167 | isNew = false; | ||
168 | }, | ||
169 | [](const Index::Error &error) { | ||
170 | if (error.code != Index::IndexNotAvailable) { | ||
171 | Warning() << "Error in uid index: " << error.message; | ||
172 | } | ||
173 | }); | ||
174 | if (isNew) { | ||
175 | m_fbb.Clear(); | ||
176 | |||
177 | flatbuffers::FlatBufferBuilder entityFbb; | ||
178 | createMail(it.key().toUtf8(), it.value(), entityFbb); | ||
179 | |||
180 | flatbuffers::Verifier verifyer(reinterpret_cast<const uint8_t *>(entityFbb.GetBufferPointer()), entityFbb.GetSize()); | ||
181 | if (!Akonadi2::ApplicationDomain::Buffer::VerifyMailBuffer(verifyer)) { | ||
182 | Warning() << "invalid buffer, not a mail buffer"; | ||
183 | } | ||
184 | |||
185 | flatbuffers::FlatBufferBuilder fbb; | ||
186 | //This is the resource type and not the domain type | ||
187 | auto type = fbb.CreateString(ENTITY_TYPE_MAIL); | ||
188 | auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); | ||
189 | auto location = Akonadi2::Commands::CreateCreateEntity(fbb, type, delta); | ||
190 | Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); | ||
191 | |||
192 | enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); | ||
193 | } else { //modification | ||
194 | //TODO diff and create modification if necessary | ||
195 | } | ||
196 | } | ||
197 | //TODO find items to remove | ||
198 | 171 | ||
199 | f.setFinished(); | 172 | f.setFinished(); |
200 | }); | 173 | }); |
diff --git a/examples/dummyresource/resourcefactory.h b/examples/dummyresource/resourcefactory.h index 55d84d3..55cfbd3 100644 --- a/examples/dummyresource/resourcefactory.h +++ b/examples/dummyresource/resourcefactory.h | |||
@@ -37,6 +37,7 @@ public: | |||
37 | private: | 37 | private: |
38 | void createEvent(const QByteArray &rid, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb); | 38 | void createEvent(const QByteArray &rid, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb); |
39 | void createMail(const QByteArray &rid, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb); | 39 | void createMail(const QByteArray &rid, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb); |
40 | void 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); | ||
40 | }; | 41 | }; |
41 | 42 | ||
42 | class DummyResourceFactory : public Akonadi2::ResourceFactory | 43 | class DummyResourceFactory : public Akonadi2::ResourceFactory |