diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-01-07 17:56:30 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-01-07 17:56:30 +0100 |
commit | 35a3afc591c85999de856c21dbb493e341951d91 (patch) | |
tree | f46b6a8c22e5bdb283148e48e0391c2691bcc343 | |
parent | 470203f1c09fa1811ae2859520c168901d5fa6cd (diff) | |
download | sink-35a3afc591c85999de856c21dbb493e341951d91.tar.gz sink-35a3afc591c85999de856c21dbb493e341951d91.zip |
Share query syntax
-rw-r--r-- | sinksh/sinksh_utils.cpp | 63 | ||||
-rw-r--r-- | sinksh/sinksh_utils.h | 3 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_count.cpp | 16 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_list.cpp | 11 | ||||
-rw-r--r-- | sinksh/syntax_modules/sink_sync.cpp | 56 |
5 files changed, 82 insertions, 67 deletions
diff --git a/sinksh/sinksh_utils.cpp b/sinksh/sinksh_utils.cpp index 82bd6c1..71a47b1 100644 --- a/sinksh/sinksh_utils.cpp +++ b/sinksh/sinksh_utils.cpp | |||
@@ -144,4 +144,67 @@ QMap<QString, QString> keyValueMapFromArgs(const QStringList &args) | |||
144 | } | 144 | } |
145 | return map; | 145 | return map; |
146 | } | 146 | } |
147 | |||
148 | bool isId(const QByteArray &value) | ||
149 | { | ||
150 | return value.startsWith("{"); | ||
151 | } | ||
152 | |||
153 | bool applyFilter(Sink::Query &query, const QStringList &args_) | ||
154 | { | ||
155 | if (args_.isEmpty()) { | ||
156 | return false; | ||
157 | } | ||
158 | auto args = args_; | ||
159 | |||
160 | auto type = args.takeFirst(); | ||
161 | |||
162 | if ((type.isEmpty() || !SinkshUtils::isValidStoreType(type)) && type != "*") { | ||
163 | qWarning() << "Unknown type: " << type; | ||
164 | return false; | ||
165 | } | ||
166 | if (type != "*") { | ||
167 | query.setType(type.toUtf8()); | ||
168 | } | ||
169 | if (!args.isEmpty()) { | ||
170 | auto resource = args.takeFirst().toLatin1(); | ||
171 | |||
172 | if (resource.contains('/')) { | ||
173 | //The resource isn't an id but a path | ||
174 | auto list = resource.split('/'); | ||
175 | const auto resourceId = list.takeFirst(); | ||
176 | query.resourceFilter(resourceId); | ||
177 | if (type == Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Mail>() && !list.isEmpty()) { | ||
178 | auto value = list.takeFirst(); | ||
179 | if (isId(value)) { | ||
180 | query.filter<Sink::ApplicationDomain::Mail::Folder>(value); | ||
181 | } else { | ||
182 | Sink::Query folderQuery; | ||
183 | folderQuery.resourceFilter(resourceId); | ||
184 | folderQuery.filter<Sink::ApplicationDomain::Folder::Name>(value); | ||
185 | folderQuery.filter<Sink::ApplicationDomain::Folder::Parent>(QVariant()); | ||
186 | auto folders = Sink::Store::read<Sink::ApplicationDomain::Folder>(folderQuery); | ||
187 | if (folders.size() == 1) { | ||
188 | query.filter<Sink::ApplicationDomain::Mail::Folder>(folders.first()); | ||
189 | } else { | ||
190 | qWarning() << "Folder name did not match uniquely: " << folders.size(); | ||
191 | for (const auto &f : folders) { | ||
192 | qWarning() << f.getName(); | ||
193 | } | ||
194 | return false; | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | } else { | ||
199 | query.resourceFilter(resource); | ||
200 | } | ||
201 | } | ||
202 | return true; | ||
203 | } | ||
204 | |||
205 | bool applyFilter(Sink::Query &query, const SyntaxTree::Options &options) | ||
206 | { | ||
207 | return applyFilter(query, options.positionalArguments); | ||
208 | } | ||
209 | |||
147 | } | 210 | } |
diff --git a/sinksh/sinksh_utils.h b/sinksh/sinksh_utils.h index 051c616..e251803 100644 --- a/sinksh/sinksh_utils.h +++ b/sinksh/sinksh_utils.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "common/store.h" | 27 | #include "common/store.h" |
28 | 28 | ||
29 | #include "state.h" | 29 | #include "state.h" |
30 | #include "syntaxtree.h" | ||
30 | 31 | ||
31 | namespace SinkshUtils { | 32 | namespace SinkshUtils { |
32 | 33 | ||
@@ -42,6 +43,8 @@ QStringList resourceOrTypeCompleter(const QStringList &commands, const QString & | |||
42 | QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state); | 43 | QStringList typeCompleter(const QStringList &commands, const QString &fragment, State &state); |
43 | QStringList debugareaCompleter(const QStringList &, const QString &fragment, State &state); | 44 | QStringList debugareaCompleter(const QStringList &, const QString &fragment, State &state); |
44 | QMap<QString, QString> keyValueMapFromArgs(const QStringList &args); | 45 | QMap<QString, QString> keyValueMapFromArgs(const QStringList &args); |
46 | bool applyFilter(Sink::Query &query, const QStringList &args); | ||
47 | bool applyFilter(Sink::Query &query, const SyntaxTree::Options &args); | ||
45 | 48 | ||
46 | /** | 49 | /** |
47 | * A small abstraction layer to use the sink store with the type available as string. | 50 | * A small abstraction layer to use the sink store with the type available as string. |
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 | |||
41 | 41 | ||
42 | bool count(const QStringList &args, State &state) | 42 | bool count(const QStringList &args, State &state) |
43 | { | 43 | { |
44 | auto resources = args; | ||
45 | auto type = !resources.isEmpty() ? resources.takeFirst() : QString(); | ||
46 | |||
47 | if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { | ||
48 | state.printError(QObject::tr("Unknown type: %1").arg(type)); | ||
49 | return false; | ||
50 | } | ||
51 | |||
52 | Sink::Query query; | 44 | Sink::Query query; |
53 | for (const auto &res : resources) { | 45 | query.setId("count"); |
54 | query.resourceFilter(res.toLatin1()); | 46 | if (!SinkshUtils::applyFilter(query, args)) { |
47 | state.printError(QObject::tr("Options: $type $filter")); | ||
48 | return false; | ||
55 | } | 49 | } |
56 | 50 | ||
57 | auto model = SinkshUtils::loadModel(type, query); | 51 | auto model = SinkshUtils::loadModel(query.type(), query); |
58 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | 52 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { |
59 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | 53 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { |
60 | state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex()))); | 54 | 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) | |||
74 | 74 | ||
75 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); | 75 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); |
76 | 76 | ||
77 | if (type.isEmpty() || !SinkshUtils::isValidStoreType(type)) { | ||
78 | state.printError(QObject::tr("Unknown type: %1").arg(type)); | ||
79 | return false; | ||
80 | } | ||
81 | |||
82 | Sink::Query query; | 77 | Sink::Query query; |
83 | query.setId("list"); | 78 | query.setId("list"); |
84 | 79 | if (!SinkshUtils::applyFilter(query, options)) { | |
85 | if (options.options.contains("resource")) { | 80 | state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--showall|--show $property]")); |
86 | query.resourceFilter(baIfAvailable(options.options.value("resource"))); | 81 | return false; |
87 | } | 82 | } |
88 | if (options.options.contains("filter")) { | 83 | if (options.options.contains("filter")) { |
89 | for (const auto &f : options.options.value("filter")) { | 84 | 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 @@ | |||
38 | namespace SinkSync | 38 | namespace SinkSync |
39 | { | 39 | { |
40 | 40 | ||
41 | bool isId(const QByteArray &value) | 41 | bool sync(const QStringList &args, State &state) |
42 | { | 42 | { |
43 | return value.startsWith("{"); | ||
44 | } | ||
45 | |||
46 | bool sync(const QStringList &args_, State &state) | ||
47 | { | ||
48 | auto args = args_; | ||
49 | Sink::Query query; | 43 | Sink::Query query; |
50 | if (args.isEmpty()) { | 44 | |
51 | state.printError(QObject::tr("Options: $type $resource/$folder/$subfolder")); | 45 | if (!args.isEmpty() && !SinkshUtils::isValidStoreType(args.first())) { |
52 | return false; | 46 | query.resourceFilter(args.first().toLatin1()); |
53 | } | 47 | } else { |
54 | auto type = args.takeFirst().toLatin1(); | 48 | if (!SinkshUtils::applyFilter(query, args)) { |
55 | if (type != "*") { | 49 | state.printError(QObject::tr("Options: $type $resource/$folder/$subfolder")); |
56 | query.setType(type); | 50 | return false; |
57 | } | ||
58 | if (!args.isEmpty()) { | ||
59 | auto resource = args.takeFirst().toLatin1(); | ||
60 | |||
61 | if (resource.contains('/')) { | ||
62 | //The resource isn't an id but a path | ||
63 | auto list = resource.split('/'); | ||
64 | const auto resourceId = list.takeFirst(); | ||
65 | query.resourceFilter(resourceId); | ||
66 | if (type == Sink::ApplicationDomain::getTypeName<Sink::ApplicationDomain::Mail>() && !list.isEmpty()) { | ||
67 | auto value = list.takeFirst(); | ||
68 | if (isId(value)) { | ||
69 | query.filter<Sink::ApplicationDomain::Mail::Folder>(value); | ||
70 | } else { | ||
71 | Sink::Query folderQuery; | ||
72 | folderQuery.resourceFilter(resourceId); | ||
73 | folderQuery.filter<Sink::ApplicationDomain::Folder::Name>(value); | ||
74 | folderQuery.filter<Sink::ApplicationDomain::Folder::Parent>(QVariant()); | ||
75 | qWarning() << "Looking for folder: " << value << " in " << resourceId; | ||
76 | auto folders = Sink::Store::read<Sink::ApplicationDomain::Folder>(folderQuery); | ||
77 | if (folders.size() == 1) { | ||
78 | query.filter<Sink::ApplicationDomain::Mail::Folder>(folders.first()); | ||
79 | qWarning() << "Synchronizing folder: " << folders.first().identifier(); | ||
80 | } else { | ||
81 | qWarning() << "Folder name did not match uniquely: " << folders.size(); | ||
82 | for (const auto &f : folders) { | ||
83 | qWarning() << f.getName(); | ||
84 | } | ||
85 | state.printError(QObject::tr("Folder name did not match uniquely.")); | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | } else { | ||
90 | query.resourceFilter(resource); | ||
91 | } | 51 | } |
92 | } | 52 | } |
93 | 53 | ||