summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--client/clientapi.h149
-rw-r--r--client/test/clientapitest.cpp10
2 files changed, 89 insertions, 70 deletions
diff --git a/client/clientapi.h b/client/clientapi.h
index 4ec90e2..44fc98b 100644
--- a/client/clientapi.h
+++ b/client/clientapi.h
@@ -3,6 +3,8 @@
3#include <QString> 3#include <QString>
4#include <QSet> 4#include <QSet>
5#include <QSharedPointer> 5#include <QSharedPointer>
6#include <QTimer>
7#include <QDebug>
6#include <functional> 8#include <functional>
7 9
8namespace async { 10namespace async {
@@ -100,28 +102,45 @@ namespace Domain {
100 102
101class AkonadiDomainType { 103class AkonadiDomainType {
102public: 104public:
105 AkonadiDomainType(const QString &resource, const QString &identifier, qint64 revision)
106 : mResource(resource),
107 mIdentifier(identifier),
108 mRevision(revision)
109 {
110 }
111
112 virtual QVariant getProperty(const QString &key){ return QVariant(); }
113
114private:
103 /* 115 /*
104 * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location. 116 * Each domain object needs to store the resource, identifier, revision triple so we can link back to the storage location.
105 */ 117 */
106 QString identifier; 118 QString mResource;
107 QString resource; 119 QString mIdentifier;
108 qint64 revision; 120 qint64 mRevision;
109}; 121};
110 122
111class Event : public AkonadiDomainType { 123class Event : public AkonadiDomainType {
124public:
125 typedef QSharedPointer<Event> Ptr;
126 Event(const QString &resource, const QString &identifier, qint64 revision):AkonadiDomainType(resource, identifier, revision){};
112 127
113}; 128};
114class Todo : public AkonadiDomainType {
115 129
130class Todo : public AkonadiDomainType {
131public:
132 typedef QSharedPointer<Todo> Ptr;
116}; 133};
117class Calendar : public AkonadiDomainType {
118 134
135class Calendar : public AkonadiDomainType {
136public:
137 typedef QSharedPointer<Calendar> Ptr;
119}; 138};
120class Mail : public AkonadiDomainType {
121 139
140class Mail : public AkonadiDomainType {
122}; 141};
123class Folder : public AkonadiDomainType {
124 142
143class Folder : public AkonadiDomainType {
125}; 144};
126 145
127/** 146/**
@@ -185,7 +204,7 @@ public:
185 virtual void create(const DomainType &domainObject) = 0; 204 virtual void create(const DomainType &domainObject) = 0;
186 virtual void modify(const DomainType &domainObject) = 0; 205 virtual void modify(const DomainType &domainObject) = 0;
187 virtual void remove(const DomainType &domainObject) = 0; 206 virtual void remove(const DomainType &domainObject) = 0;
188 virtual void load(const Query &query, const std::function<void(const DomainType &)> &resultCallback) = 0; 207 virtual void load(const Query &query, const std::function<void(const typename DomainType::Ptr &)> &resultCallback) = 0;
189}; 208};
190 209
191 210
@@ -262,9 +281,9 @@ public:
262 * Asynchronusly load a dataset 281 * Asynchronusly load a dataset
263 */ 282 */
264 template <class DomainType> 283 template <class DomainType>
265 static QSharedPointer<ResultEmitter<DomainType> > load(Query query) 284 static QSharedPointer<ResultEmitter<typename DomainType::Ptr> > load(Query query)
266 { 285 {
267 QSharedPointer<ResultProvider<DomainType> > resultSet(new ResultProvider<DomainType>); 286 QSharedPointer<ResultProvider<typename DomainType::Ptr> > resultSet(new ResultProvider<typename DomainType::Ptr>);
268 287
269 //Execute the search in a thread. 288 //Execute the search in a thread.
270 //We must guarantee that the emitter is returned before the first result is emitted. 289 //We must guarantee that the emitter is returned before the first result is emitted.
@@ -275,7 +294,7 @@ public:
275 for(const QString &resource : query.resources) { 294 for(const QString &resource : query.resources) {
276 auto facade = FacadeFactory::instance().getFacade<DomainType>(resource); 295 auto facade = FacadeFactory::instance().getFacade<DomainType>(resource);
277 //We have to bind an instance to the function callback. Since we use a shared pointer this keeps the result provider instance (and thus also the emitter) alive. 296 //We have to bind an instance to the function callback. Since we use a shared pointer this keeps the result provider instance (and thus also the emitter) alive.
278 std::function<void(const DomainType &)> addCallback = std::bind(&ResultProvider<DomainType>::add, resultSet, std::placeholders::_1); 297 std::function<void(const typename DomainType::Ptr &)> addCallback = std::bind(&ResultProvider<typename DomainType::Ptr>::add, resultSet, std::placeholders::_1);
279 facade->load(query, addCallback); 298 facade->load(query, addCallback);
280 } 299 }
281 resultSet->complete(); 300 resultSet->complete();
@@ -363,58 +382,58 @@ class EventDomainAdapter : public Akonadi2::Domain::Event {
363 * 382 *
364 * TODO: perhaps we should also allow async access and leave the thread/non-thread decision up to the implementation? 383 * TODO: perhaps we should also allow async access and leave the thread/non-thread decision up to the implementation?
365 */ 384 */
366template<typename DomainType> 385// template<typename DomainType>
367class StoreFacadeImpl : public Akonadi2::StoreFacade<Akonadi2::Domain::Event> { 386// class StoreFacadeImpl : public Akonadi2::StoreFacade<Akonadi2::Domain::Event> {
368}; 387// };
369 388//
370template<> 389// template<>
371class StoreFacadeImpl<Akonadi2::Domain::Event> : public Akonadi2::StoreFacade<Akonadi2::Domain::Event> { 390// class StoreFacadeImpl<Akonadi2::Domain::Event> : public Akonadi2::StoreFacade<Akonadi2::Domain::Event> {
372public: 391// public:
373 StoreFacadeImpl():StoreFacade() {}; 392// StoreFacadeImpl():StoreFacade() {};
374 393//
375 void create(const Akonadi2::Domain::Event &domainObject) { 394// void create(const Akonadi2::Domain::Event &domainObject) {
376 //FIXME here we would need to cast to DomainAdapter 395// //FIXME here we would need to cast to DomainAdapter
377 //Do actual work 396// //Do actual work
378 //transformFromDomainType(domainObject); 397// //transformFromDomainType(domainObject);
379 //Ideally we have an adapter 398// //Ideally we have an adapter
380 //getAdater(domainObject).buffer(); 399// //getAdater(domainObject).buffer();
381 //domainObject.key(); => The domain object needs to provide the id 400// //domainObject.key(); => The domain object needs to provide the id
382 //writeToDb(); 401// //writeToDb();
383 } 402// }
384 403//
385 void modify(const Akonadi2::Domain::Event &domainObject) { 404// void modify(const Akonadi2::Domain::Event &domainObject) {
386 //Do actual work 405// //Do actual work
387 } 406// }
388 407//
389 void remove(const Akonadi2::Domain::Event &domainObject) { 408// void remove(const Akonadi2::Domain::Event &domainObject) {
390 //Do actual work 409// //Do actual work
391 } 410// }
392 411//
393 class EventBuffer { 412// class EventBuffer {
394 QString value; 413// QString value;
395 }; 414// };
396 415//
397 static Akonadi2::Domain::Event transformToDomainType(const EventBuffer &buffer) { 416// static Akonadi2::Domain::Event transformToDomainType(const EventBuffer &buffer) {
398 //We may want to avoid copies here 417// //We may want to avoid copies here
399 Akonadi2::Domain::Event event; 418// Akonadi2::Domain::Event event;
400 // //Ideally we don't have to copy and can use an adaptor instead 419// // //Ideally we don't have to copy and can use an adaptor instead
401 // return DomainAdaptor 420// // return DomainAdaptor
402 return event; 421// return event;
403 }; 422// };
404 423//
405 void load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event &)> &resultCallback) { 424// void load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event &)> &resultCallback) {
406 //retrieve buffers from storage 425// //retrieve buffers from storage
407 QList<EventBuffer> queryresult; 426// QList<EventBuffer> queryresult;
408 for(const EventBuffer &buffer : queryresult) { 427// for(const EventBuffer &buffer : queryresult) {
409 resultCallback(transformToDomainType(buffer)); 428// resultCallback(transformToDomainType(buffer));
410 } 429// }
411 } 430// }
412 431//
413private: 432// private:
414 //Dummy implementation 433// //Dummy implementation
415 class ResourceImpl {}; 434// class ResourceImpl {};
416 ResourceImpl resource; 435// ResourceImpl resource;
417 class DatabaseImpl {}; 436// class DatabaseImpl {};
418 DatabaseImpl mDb; 437// DatabaseImpl mDb;
419}; 438// };
420 439
diff --git a/client/test/clientapitest.cpp b/client/test/clientapitest.cpp
index 8d8f552..bff910b 100644
--- a/client/test/clientapitest.cpp
+++ b/client/test/clientapitest.cpp
@@ -11,7 +11,7 @@ public:
11 virtual void create(const Akonadi2::Domain::Event &domainObject){}; 11 virtual void create(const Akonadi2::Domain::Event &domainObject){};
12 virtual void modify(const Akonadi2::Domain::Event &domainObject){}; 12 virtual void modify(const Akonadi2::Domain::Event &domainObject){};
13 virtual void remove(const Akonadi2::Domain::Event &domainObject){}; 13 virtual void remove(const Akonadi2::Domain::Event &domainObject){};
14 virtual void load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event &)> &resultCallback) 14 virtual void load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::Domain::Event::Ptr &)> &resultCallback)
15 { 15 {
16 qDebug() << "load called"; 16 qDebug() << "load called";
17 for(const auto &result : results) { 17 for(const auto &result : results) {
@@ -19,7 +19,7 @@ public:
19 } 19 }
20 } 20 }
21 21
22 QList<Akonadi2::Domain::Event> results; 22 QList<Akonadi2::Domain::Event::Ptr> results;
23}; 23};
24 24
25class ClientAPITest : public QObject 25class ClientAPITest : public QObject
@@ -30,7 +30,7 @@ private Q_SLOTS:
30 void testLoad() 30 void testLoad()
31 { 31 {
32 DummyResourceFacade facade; 32 DummyResourceFacade facade;
33 facade.results << Akonadi2::Domain::Event(); 33 facade.results << QSharedPointer<Akonadi2::Domain::Event>::create("resource", "id", 0);
34 34
35 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::Domain::Event, DummyResourceFacade>("dummyresource", [facade](){ return new DummyResourceFacade(facade); }); 35 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::Domain::Event, DummyResourceFacade>("dummyresource", [facade](){ return new DummyResourceFacade(facade); });
36 36
@@ -39,8 +39,8 @@ private Q_SLOTS:
39 39
40 auto result = Akonadi2::Store::load<Akonadi2::Domain::Event>(query); 40 auto result = Akonadi2::Store::load<Akonadi2::Domain::Event>(query);
41 41
42 QList<Akonadi2::Domain::Event> resultSet; 42 QList<Akonadi2::Domain::Event::Ptr> resultSet;
43 result->onAdded([&resultSet](const Akonadi2::Domain::Event &event){ resultSet << event; qDebug() << "result added";}); 43 result->onAdded([&resultSet](const Akonadi2::Domain::Event::Ptr &event){ resultSet << event; qDebug() << "result added";});
44 44
45 bool complete; 45 bool complete;
46 result->onComplete([&complete]{ complete = true; qDebug() << "complete";}); 46 result->onComplete([&complete]{ complete = true; qDebug() << "complete";});