summaryrefslogtreecommitdiffstats
path: root/common/facade.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/facade.h')
-rw-r--r--common/facade.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/common/facade.h b/common/facade.h
index f9b5a83..6c1ad67 100644
--- a/common/facade.h
+++ b/common/facade.h
@@ -29,6 +29,7 @@
29#include "createentity_generated.h" 29#include "createentity_generated.h"
30#include "domainadaptor.h" 30#include "domainadaptor.h"
31#include "entitybuffer.h" 31#include "entitybuffer.h"
32#include "log.h"
32 33
33/** 34/**
34 * A QueryRunner runs a query and updates the corresponding result set. 35 * A QueryRunner runs a query and updates the corresponding result set.
@@ -98,6 +99,48 @@ public:
98 { 99 {
99 } 100 }
100 101
102 //TODO JOBAPI return job from sync continuation to execute it as subjob?
103 Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) Q_DECL_OVERRIDE
104 {
105 auto runner = QSharedPointer<QueryRunner>::create(query);
106 QWeakPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > weakResultProvider = resultProvider;
107 runner->setQuery([this, weakResultProvider, query] (qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> {
108 return Async::start<qint64>([this, weakResultProvider, query](Async::Future<qint64> &future) {
109 auto resultProvider = weakResultProvider.toStrongRef();
110 if (!resultProvider) {
111 Warning() << "Tried executing query after result provider is already gone";
112 future.setError(0, QString());
113 future.setFinished();
114 return;
115 }
116 //TODO only emit changes and don't replace everything
117 resultProvider->clear();
118 //rerun query
119 auto addCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1);
120 load(query, addCallback).template then<void, qint64>([resultProvider, &future](qint64 queriedRevision) {
121 //TODO set revision in result provider?
122 //TODO update all existing results with new revision
123 resultProvider->complete();
124 future.setValue(queriedRevision);
125 future.setFinished();
126 }).exec();
127 });
128 });
129
130 //In case of a live query we keep the runner for as long alive as the result provider exists
131 if (query.liveQuery) {
132 resultProvider->setQueryRunner(runner);
133 QObject::connect(mResourceAccess.data(), &Akonadi2::ResourceAccess::revisionChanged, runner.data(), &QueryRunner::revisionChanged);
134 }
135
136 //We have to capture the runner to keep it alive
137 return synchronizeResource(query.syncOnDemand, query.processAll).template then<void>([runner](Async::Future<void> &future) {
138 runner->run().then<void>([&future]() {
139 future.setFinished();
140 }).exec();
141 });
142 }
143
101protected: 144protected:
102 Async::Job<void> sendCreateCommand(const QByteArray &t, const QByteArray &buffer) 145 Async::Job<void> sendCreateCommand(const QByteArray &t, const QByteArray &buffer)
103 { 146 {
@@ -129,6 +172,8 @@ protected:
129 return Async::null<void>(); 172 return Async::null<void>();
130 } 173 }
131 174
175 virtual Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const typename DomainType::Ptr &)> &resultCallback) { return Async::null<qint64>(); };
176
132protected: 177protected:
133 //TODO use one resource access instance per application => make static 178 //TODO use one resource access instance per application => make static
134 QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; 179 QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess;