From 7b380e9677678c0e3bcec981a2466ccb04f4e590 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Mon, 28 Dec 2015 20:21:02 +0100 Subject: methods to get resourceIds and a compelter built on that --- akonadish/akonadish_utils.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'akonadish/akonadish_utils.cpp') diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp index ffbdcb3..90847f6 100644 --- a/akonadish/akonadish_utils.cpp +++ b/akonadish/akonadish_utils.cpp @@ -70,6 +70,50 @@ QSharedPointer loadModel(const QString &type, Akonadi2::Quer return model; } +QStringList resourceIds(State &state) +{ + QStringList resources; + Akonadi2::Query query; + query.syncOnDemand = false; + query.processAll = false; + query.liveQuery = false; + auto model = AkonadishUtils::loadModel("resource", query); + + QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, &resources] (const QModelIndex &index, int start, int end) mutable { + for (int i = start; i <= end; i++) { + auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value(); + resources << object->identifier(); + } + }); + + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { + state.commandFinished(); + } + }); + + state.commandStarted(); + + return resources; +} + +QStringList resourceCompleter(const QStringList &commands, const QString &fragment, State &state) +{ + QStringList resources = resourceIds(state); + if (fragment.isEmpty()) { + return resources; + } + + QStringList filtered; + for (auto resource: resources) { + if (resource.startsWith(fragment)) { + filtered << resource; + } + } + + return filtered; +} + QMap keyValueMapFromArgs(const QStringList &args) { //TODO: this is not the most clever of algorithms. preserved during the port of commands -- cgit v1.2.3