From d417f01e2eebeedfaae76b40667372bd0fb21fea Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 18 Jan 2015 14:29:08 +0100 Subject: Use jobs in queries, sync works again. --- common/clientapi.h | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'common/clientapi.h') diff --git a/common/clientapi.h b/common/clientapi.h index 2f1c127..dd11a0d 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -165,7 +165,7 @@ namespace Akonadi2 { /** * Standardized Domain Types * - * The don't adhere to any standard and can be freely extended + * They don't adhere to any standard and can be freely extended * Their sole purpose is providing a standardized interface to access data. * * This is necessary to decouple resource-backends from application domain containers (otherwise each resource would have to provide a faceade for each application domain container). @@ -297,6 +297,7 @@ using namespace async; class Query { public: + Query() : syncOnDemand(true) {} //Could also be a propertyFilter QStringList resources; //Could also be a propertyFilter @@ -305,6 +306,7 @@ public: QHash propertyFilter; //Properties to retrieve QSet requestedProperties; + bool syncOnDemand; }; @@ -324,7 +326,7 @@ public: virtual Async::Job create(const DomainType &domainObject) = 0; virtual Async::Job modify(const DomainType &domainObject) = 0; virtual Async::Job remove(const DomainType &domainObject) = 0; - virtual void load(const Query &query, const std::function &resultCallback, const std::function &completeCallback) = 0; + virtual Async::Job load(const Query &query, const std::function &resultCallback) = 0; }; @@ -418,21 +420,25 @@ public: // query tells us in which resources we're interested // TODO: queries to individual resources could be parallelized auto eventloop = QSharedPointer::create(); - int completeCounter = 0; + Async::Job job = Async::null(); for(const QString &resource : query.resources) { auto facade = FacadeFactory::instance().getFacade(resource); //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. std::function addCallback = std::bind(&ResultProvider::add, resultSet, std::placeholders::_1); //We copy the facade pointer to keep it alive - facade->load(query, addCallback, [&completeCounter, &query, resultSet, facade, eventloop]() { - //TODO use jobs instead of this counter - completeCounter++; - if (completeCounter == query.resources.size()) { - resultSet->complete(); - eventloop->quit(); - } + job = job.then([facade, query, addCallback](Async::Future &future) { + Async::Job j = facade->load(query, addCallback); + j.then([&future, facade](Async::Future &f) { + future.setFinished(); + f.setFinished(); + }).exec(); }); } + job.then([eventloop, resultSet](Async::Future &future) { + resultSet->complete(); + eventloop->quit(); + future.setFinished(); + }).exec(); //The thread contains no eventloop, so execute one here eventloop->exec(QEventLoop::ExcludeUserInputEvents); }); -- cgit v1.2.3