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 From cc5363fe4b0cd4ea95ac98d9d37f2028fe226145 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Mon, 28 Dec 2015 22:00:19 +0100 Subject: typeCompleter and resourceOrTypeCompleter --- akonadish/akonadish_utils.cpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'akonadish/akonadish_utils.cpp') diff --git a/akonadish/akonadish_utils.cpp b/akonadish/akonadish_utils.cpp index 90847f6..070d788 100644 --- a/akonadish/akonadish_utils.cpp +++ b/akonadish/akonadish_utils.cpp @@ -25,10 +25,11 @@ namespace AkonadishUtils { +static QStringList s_types = QStringList() << "resource" << "folder" << "mail" << "event"; + bool isValidStoreType(const QString &type) { - static const QSet types = QSet() << "folder" << "mail" << "event" << "resource"; - return types.contains(type); + return s_types.contains(type); } StoreBase &getStore(const QString &type) @@ -97,23 +98,42 @@ QStringList resourceIds(State &state) return resources; } -QStringList resourceCompleter(const QStringList &commands, const QString &fragment, State &state) +QStringList filtered(const QStringList &list, const QString &fragment) { - QStringList resources = resourceIds(state); if (fragment.isEmpty()) { - return resources; + return list; } QStringList filtered; - for (auto resource: resources) { - if (resource.startsWith(fragment)) { - filtered << resource; + for (auto item: list) { + if (item.startsWith(fragment)) { + filtered << item; } } return filtered; } +QStringList resourceCompleter(const QStringList &, const QString &fragment, State &state) +{ + return filtered(resourceIds(state), fragment); +} + +QStringList resourceOrTypeCompleter(const QStringList &commands, const QString &fragment, State &state) +{ + static QStringList types = QStringList() << "resource" << "folder" << "mail" << "event"; + if (commands.count() == 1) { + return filtered(s_types, fragment); + } + + return filtered(resourceIds(state), fragment); +} + +QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state) +{ + return filtered(s_types, fragment); +} + QMap keyValueMapFromArgs(const QStringList &args) { //TODO: this is not the most clever of algorithms. preserved during the port of commands -- cgit v1.2.3