diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-09 18:00:05 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-04-09 18:00:05 +0200 |
commit | 5004fdd4a0646767f8baa8c49d8da50843eac1ed (patch) | |
tree | 252c5df96e3c1da239b9256ca2886572b76954ed /sinksh/syntax_modules/sink_show.cpp | |
parent | 7a1eccc13dc0828c292dc1cc6d1556fa38011da3 (diff) | |
download | sink-5004fdd4a0646767f8baa8c49d8da50843eac1ed.tar.gz sink-5004fdd4a0646767f8baa8c49d8da50843eac1ed.zip |
sinksh show command
Diffstat (limited to 'sinksh/syntax_modules/sink_show.cpp')
-rw-r--r-- | sinksh/syntax_modules/sink_show.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/sink_show.cpp b/sinksh/syntax_modules/sink_show.cpp new file mode 100644 index 0000000..100b921 --- /dev/null +++ b/sinksh/syntax_modules/sink_show.cpp | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the | ||
17 | * Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include <QCoreApplication> | ||
22 | #include <QDebug> | ||
23 | #include <QObject> // tr() | ||
24 | #include <QModelIndex> | ||
25 | #include <QTime> | ||
26 | |||
27 | #include "common/resource.h" | ||
28 | #include "common/storage.h" | ||
29 | #include "common/domain/event.h" | ||
30 | #include "common/domain/folder.h" | ||
31 | #include "common/resourceconfig.h" | ||
32 | #include "common/log.h" | ||
33 | #include "common/storage.h" | ||
34 | #include "common/definitions.h" | ||
35 | |||
36 | #include "sinksh_utils.h" | ||
37 | #include "state.h" | ||
38 | #include "syntaxtree.h" | ||
39 | |||
40 | namespace SinkShow | ||
41 | { | ||
42 | |||
43 | bool show(const QStringList &args, State &state) | ||
44 | { | ||
45 | if (args.isEmpty()) { | ||
46 | state.printError(QObject::tr("Please provide at least one type to show (e.g. resource, ..")); | ||
47 | return false; | ||
48 | } | ||
49 | |||
50 | auto argList = args; | ||
51 | if (argList.size() < 2 || !SinkshUtils::isValidStoreType(argList.at(0))) { | ||
52 | state.printError(QObject::tr("Invalid command syntax. Supply type and resource at least.")); | ||
53 | return false; | ||
54 | } | ||
55 | auto type = argList.takeFirst(); | ||
56 | auto resource = argList.takeFirst(); | ||
57 | bool queryForResourceOrAgent = argList.isEmpty(); | ||
58 | |||
59 | Sink::Query query; | ||
60 | if (queryForResourceOrAgent) { | ||
61 | query.ids << resource.toLatin1(); | ||
62 | } else { | ||
63 | query.resources << resource.toLatin1(); | ||
64 | } | ||
65 | query.liveQuery = false; | ||
66 | |||
67 | QTime time; | ||
68 | time.start(); | ||
69 | auto model = SinkshUtils::loadModel(type, query); | ||
70 | if (state.debugLevel() > 0) { | ||
71 | state.printLine(QObject::tr("Folder type %1").arg(type)); | ||
72 | state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed())); | ||
73 | } | ||
74 | |||
75 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, state](const QModelIndex &index, int start, int end) { | ||
76 | for (int i = start; i <= end; i++) { | ||
77 | auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>(); | ||
78 | state.printLine("Resource: " + object->resourceInstanceIdentifier(), 1); | ||
79 | state.printLine("Identifier: " + object->identifier(), 1); | ||
80 | state.stageTableLine(QStringList() | ||
81 | << QObject::tr("Property:") | ||
82 | << QObject::tr("Value:")); | ||
83 | |||
84 | for (const auto &property : object->availableProperties()) { | ||
85 | state.stageTableLine(QStringList() | ||
86 | << property | ||
87 | << object->getProperty(property).toString()); | ||
88 | } | ||
89 | state.flushTable(); | ||
90 | } | ||
91 | }); | ||
92 | |||
93 | QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { | ||
94 | if (roles.contains(Sink::Store::ChildrenFetchedRole)) { | ||
95 | state.commandFinished(); | ||
96 | } | ||
97 | }); | ||
98 | |||
99 | if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) { | ||
100 | return true; | ||
101 | } | ||
102 | |||
103 | return false; | ||
104 | } | ||
105 | |||
106 | Syntax::List syntax() | ||
107 | { | ||
108 | Syntax show("show", QObject::tr("Show all properties of an entity."), &SinkShow::show, Syntax::EventDriven); | ||
109 | show.completer = &SinkshUtils::resourceOrTypeCompleter; | ||
110 | return Syntax::List() << show; | ||
111 | } | ||
112 | |||
113 | REGISTER_SYNTAX(SinkShow) | ||
114 | |||
115 | } | ||