summaryrefslogtreecommitdiffstats
path: root/common/clientapi.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-14 18:22:36 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-14 18:22:36 +0100
commitfd9703a6f990d965d1c7fba21fee36b2beef644e (patch)
tree1587bb9080ec6de0ead8d8cfc0f69b8ed3445cbd /common/clientapi.cpp
parentc4ec8fbc9f95a67079bc011c6455c3de72fa8266 (diff)
downloadsink-fd9703a6f990d965d1c7fba21fee36b2beef644e.tar.gz
sink-fd9703a6f990d965d1c7fba21fee36b2beef644e.zip
An imperative query API
Diffstat (limited to 'common/clientapi.cpp')
-rw-r--r--common/clientapi.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/common/clientapi.cpp b/common/clientapi.cpp
index e7ca99d..2c25220 100644
--- a/common/clientapi.cpp
+++ b/common/clientapi.cpp
@@ -212,10 +212,74 @@ KAsync::Job<void> Store::synchronize(const Akonadi2::Query &query)
212 .template then<void>([](){}); 212 .template then<void>([](){});
213} 213}
214 214
215template <class DomainType>
216KAsync::Job<DomainType> Store::fetchOne(const Akonadi2::Query &query)
217{
218 return KAsync::start<DomainType>([query](KAsync::Future<DomainType> &future) {
219 //FIXME We could do this more elegantly if composed jobs would have the correct type (In that case we'd simply return the value from then continuation, and could avoid the outer job entirely)
220 fetch<DomainType>(query, 1)
221 .template then<void, QList<typename DomainType::Ptr> >([&future](const QList<typename DomainType::Ptr> &list){
222 future.setValue(*list.first());
223 future.setFinished();
224 }, [&future](int errorCode, const QString &errorMessage) {
225 future.setError(errorCode, errorMessage);
226 future.setFinished();
227 }).exec();
228 });
229}
230
231template <class DomainType>
232KAsync::Job<QList<typename DomainType::Ptr> > Store::fetchAll(const Akonadi2::Query &query)
233{
234 return fetch<DomainType>(query);
235}
236
237template <class DomainType>
238KAsync::Job<QList<typename DomainType::Ptr> > Store::fetch(const Akonadi2::Query &query, int minimumAmount)
239{
240 auto model = loadModel<DomainType>(query);
241 auto list = QSharedPointer<QList<typename DomainType::Ptr> >::create();
242 auto context = QSharedPointer<QObject>::create();
243 return KAsync::start<QList<typename DomainType::Ptr> >([model, list, context, minimumAmount](KAsync::Future<QList<typename DomainType::Ptr> > &future) {
244 if (model->rowCount() >= 1) {
245 for (int i = 0; i < model->rowCount(); i++) {
246 list->append(model->index(i, 0, QModelIndex()).data(Akonadi2::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
247 }
248 } else {
249 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, context.data(), [model, &future, list](const QModelIndex &index, int start, int end) {
250 for (int i = start; i <= end; i++) {
251 list->append(model->index(i, 0, QModelIndex()).data(Akonadi2::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
252 }
253 });
254 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, context.data(), [model, &future, list, minimumAmount](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
255 if (roles.contains(ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole)) {
256 if (list->size() < minimumAmount) {
257 future.setError(1, "Not enough values.");
258 } else {
259 future.setValue(*list);
260 }
261 future.setFinished();
262 }
263 });
264 }
265 if (model->data(QModelIndex(), ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole).toBool()) {
266 if (list->size() < minimumAmount) {
267 future.setError(1, "Not enough values.");
268 } else {
269 future.setValue(*list);
270 }
271 future.setFinished();
272 }
273 });
274}
275
215#define REGISTER_TYPE(T) template KAsync::Job<void> Store::remove<T>(const T &domainObject); \ 276#define REGISTER_TYPE(T) template KAsync::Job<void> Store::remove<T>(const T &domainObject); \
216 template KAsync::Job<void> Store::create<T>(const T &domainObject); \ 277 template KAsync::Job<void> Store::create<T>(const T &domainObject); \
217 template KAsync::Job<void> Store::modify<T>(const T &domainObject); \ 278 template KAsync::Job<void> Store::modify<T>(const T &domainObject); \
218 template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \ 279 template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \
280 template KAsync::Job<T> Store::fetchOne<T>(const Query &); \
281 template KAsync::Job<QList<T::Ptr> > Store::fetchAll<T>(const Query &); \
282 template KAsync::Job<QList<T::Ptr> > Store::fetch<T>(const Query &, int); \
219 283
220REGISTER_TYPE(ApplicationDomain::Event); 284REGISTER_TYPE(ApplicationDomain::Event);
221REGISTER_TYPE(ApplicationDomain::Mail); 285REGISTER_TYPE(ApplicationDomain::Mail);