summaryrefslogtreecommitdiffstats
path: root/common/clientapi.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/clientapi.h')
-rw-r--r--common/clientapi.h97
1 files changed, 9 insertions, 88 deletions
diff --git a/common/clientapi.h b/common/clientapi.h
index 9a32188..8f87562 100644
--- a/common/clientapi.h
+++ b/common/clientapi.h
@@ -22,28 +22,16 @@
22 22
23#include <QString> 23#include <QString>
24#include <QSharedPointer> 24#include <QSharedPointer>
25#include <QEventLoop>
26#include <functional>
27#include <memory>
28 25
29#include <Async/Async> 26#include <Async/Async>
30 27
31#include "query.h" 28#include "query.h"
32#include "resultprovider.h"
33#include "applicationdomaintype.h" 29#include "applicationdomaintype.h"
34#include "facadefactory.h"
35#include "log.h"
36 30
37namespace async { 31class QAbstractItemModel;
38 //This should abstract if we execute from eventloop or in thread.
39 //It supposed to allow the caller to finish the current method before executing the runner.
40 void run(const std::function<void()> &runner);
41}
42 32
43namespace Akonadi2 { 33namespace Akonadi2 {
44 34
45using namespace async;
46
47/** 35/**
48 * Store interface used in the client API. 36 * Store interface used in the client API.
49 */ 37 */
@@ -55,77 +43,22 @@ public:
55 static QString storageLocation(); 43 static QString storageLocation();
56 static QByteArray resourceName(const QByteArray &instanceIdentifier); 44 static QByteArray resourceName(const QByteArray &instanceIdentifier);
57 45
58 /** 46 enum Roles {
59 * Asynchronusly load a dataset 47 DomainObjectRole = Qt::UserRole + 1, //Must be the same as in ModelResult
60 */ 48 ChildrenFetchedRole
61 template <class DomainType> 49 };
62 static QSharedPointer<ResultEmitter<typename DomainType::Ptr> > load(Query query)
63 {
64 auto resultSet = QSharedPointer<ResultProvider<typename DomainType::Ptr> >::create();
65
66 //Execute the search in a thread.
67 //We must guarantee that the emitter is returned before the first result is emitted.
68 //The result provider must be threadsafe.
69 async::run([query, resultSet](){
70 QEventLoop eventLoop;
71 resultSet->onDone([&eventLoop](){
72 eventLoop.quit();
73 });
74 // Query all resources and aggregate results
75 KAsync::iterate(getResources(query.resources, ApplicationDomain::getTypeName<DomainType>()))
76 .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) {
77 auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceName(resource), resource);
78 if (facade) {
79 facade->load(query, resultSet).template then<void>([&future](){future.setFinished();}).exec();
80 //Keep the facade alive for the lifetime of the resultSet.
81 resultSet->setFacade(facade);
82 } else {
83 //Ignore the error and carry on
84 future.setFinished();
85 }
86 }).template then<void>([query, resultSet]() {
87 resultSet->initialResultSetComplete();
88 if (!query.liveQuery) {
89 resultSet->complete();
90 }
91 }).exec();
92
93 //Keep the thread alive until the result is ready
94 if (!resultSet->isDone()) {
95 eventLoop.exec();
96 }
97 });
98 return resultSet->emitter();
99 }
100 50
101 /** 51 /**
102 * Asynchronusly load a dataset with tree structure information 52 * Asynchronusly load a dataset with tree structure information
103 */ 53 */
104 // template <class DomainType>
105 // static TreeSet<DomainType> loadTree(Query)
106 // {
107
108 // }
109 template <class DomainType> 54 template <class DomainType>
110 static std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resourceInstanceIdentifier) 55 static QSharedPointer<QAbstractItemModel> loadModel(Query query);
111 {
112 if (auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceName(resourceInstanceIdentifier), resourceInstanceIdentifier)) {
113 return facade;
114 }
115 return std::make_shared<NullFacade<DomainType> >();
116 }
117 56
118 /** 57 /**
119 * Create a new entity. 58 * Create a new entity.
120 */ 59 */
121 template <class DomainType> 60 template <class DomainType>
122 static KAsync::Job<void> create(const DomainType &domainObject) { 61 static KAsync::Job<void> create(const DomainType &domainObject);
123 //Potentially move to separate thread as well
124 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
125 return facade->create(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
126 Warning() << "Failed to create";
127 });
128 }
129 62
130 /** 63 /**
131 * Modify an entity. 64 * Modify an entity.
@@ -133,25 +66,13 @@ public:
133 * This includes moving etc. since these are also simple settings on a property. 66 * This includes moving etc. since these are also simple settings on a property.
134 */ 67 */
135 template <class DomainType> 68 template <class DomainType>
136 static KAsync::Job<void> modify(const DomainType &domainObject) { 69 static KAsync::Job<void> modify(const DomainType &domainObject);
137 //Potentially move to separate thread as well
138 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
139 return facade->modify(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
140 Warning() << "Failed to modify";
141 });
142 }
143 70
144 /** 71 /**
145 * Remove an entity. 72 * Remove an entity.
146 */ 73 */
147 template <class DomainType> 74 template <class DomainType>
148 static KAsync::Job<void> remove(const DomainType &domainObject) { 75 static KAsync::Job<void> remove(const DomainType &domainObject);
149 //Potentially move to separate thread as well
150 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
151 return facade->remove(domainObject).template then<void>([facade](){}, [](int errorCode, const QString &error) {
152 Warning() << "Failed to remove";
153 });
154 }
155 76
156 /** 77 /**
157 * Shutdown resource. 78 * Shutdown resource.