From 35a3afc591c85999de856c21dbb493e341951d91 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 7 Jan 2017 17:56:30 +0100 Subject: Share query syntax --- sinksh/syntax_modules/sink_count.cpp | 16 ++++------- sinksh/syntax_modules/sink_list.cpp | 11 ++----- sinksh/syntax_modules/sink_sync.cpp | 56 ++++++------------------------------ 3 files changed, 16 insertions(+), 67 deletions(-) (limited to 'sinksh/syntax_modules') diff --git a/sinksh/syntax_modules/sink_count.cpp b/sinksh/syntax_modules/sink_count.cpp index 84bbabd..5ab9ca5 100644 --- a/sinksh/syntax_modules/sink_count.cpp +++ b/sinksh/syntax_modules/sink_count.cpp @@ -41,20 +41,14 @@ namespace SinkCount bool count(const QStringList &args, State &state) { - auto resources = args; - auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); - - if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { - state.printError(QObject::tr("Unknown type: %1").arg(type)); - return false; - } - Sink::Query query; - for (const auto &res : resources) { - query.resourceFilter(res.toLatin1()); + query.setId("count"); + if (!SinkshUtils::applyFilter(query, args)) { + state.printError(QObject::tr("Options: $type $filter")); + return false; } - auto model = SinkshUtils::loadModel(type, query); + auto model = SinkshUtils::loadModel(query.type(), query); QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector &roles) { if (roles.contains(Sink::Store::ChildrenFetchedRole)) { state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp index a609463..f9cda1a 100644 --- a/sinksh/syntax_modules/sink_list.cpp +++ b/sinksh/syntax_modules/sink_list.cpp @@ -74,16 +74,11 @@ bool list(const QStringList &args_, State &state) auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); - if (type.isEmpty() || !SinkshUtils::isValidStoreType(type)) { - state.printError(QObject::tr("Unknown type: %1").arg(type)); - return false; - } - Sink::Query query; query.setId("list"); - - if (options.options.contains("resource")) { - query.resourceFilter(baIfAvailable(options.options.value("resource"))); + if (!SinkshUtils::applyFilter(query, options)) { + state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--showall|--show $property]")); + return false; } if (options.options.contains("filter")) { for (const auto &f : options.options.value("filter")) { diff --git a/sinksh/syntax_modules/sink_sync.cpp b/sinksh/syntax_modules/sink_sync.cpp index f83f0d8..f90f055 100644 --- a/sinksh/syntax_modules/sink_sync.cpp +++ b/sinksh/syntax_modules/sink_sync.cpp @@ -38,56 +38,16 @@ namespace SinkSync { -bool isId(const QByteArray &value) +bool sync(const QStringList &args, State &state) { - return value.startsWith("{"); -} - -bool sync(const QStringList &args_, State &state) -{ - auto args = args_; Sink::Query query; - if (args.isEmpty()) { - state.printError(QObject::tr("Options: $type $resource/$folder/$subfolder")); - return false; - } - auto type = args.takeFirst().toLatin1(); - if (type != "*") { - query.setType(type); - } - if (!args.isEmpty()) { - auto resource = args.takeFirst().toLatin1(); - - if (resource.contains('/')) { - //The resource isn't an id but a path - auto list = resource.split('/'); - const auto resourceId = list.takeFirst(); - query.resourceFilter(resourceId); - if (type == Sink::ApplicationDomain::getTypeName() && !list.isEmpty()) { - auto value = list.takeFirst(); - if (isId(value)) { - query.filter(value); - } else { - Sink::Query folderQuery; - folderQuery.resourceFilter(resourceId); - folderQuery.filter(value); - folderQuery.filter(QVariant()); - qWarning() << "Looking for folder: " << value << " in " << resourceId; - auto folders = Sink::Store::read(folderQuery); - if (folders.size() == 1) { - query.filter(folders.first()); - qWarning() << "Synchronizing folder: " << folders.first().identifier(); - } else { - qWarning() << "Folder name did not match uniquely: " << folders.size(); - for (const auto &f : folders) { - qWarning() << f.getName(); - } - state.printError(QObject::tr("Folder name did not match uniquely.")); - } - } - } - } else { - query.resourceFilter(resource); + + if (!args.isEmpty() && !SinkshUtils::isValidStoreType(args.first())) { + query.resourceFilter(args.first().toLatin1()); + } else { + if (!SinkshUtils::applyFilter(query, args)) { + state.printError(QObject::tr("Options: $type $resource/$folder/$subfolder")); + return false; } } -- cgit v1.2.3