summaryrefslogtreecommitdiffstats
path: root/sinksh/syntax_modules
diff options
context:
space:
mode:
Diffstat (limited to 'sinksh/syntax_modules')
-rw-r--r--sinksh/syntax_modules/core_syntax.cpp204
-rw-r--r--sinksh/syntax_modules/sink_clear.cpp61
-rw-r--r--sinksh/syntax_modules/sink_count.cpp83
-rw-r--r--sinksh/syntax_modules/sink_create.cpp118
-rw-r--r--sinksh/syntax_modules/sink_list.cpp114
-rw-r--r--sinksh/syntax_modules/sink_modify.cpp120
-rw-r--r--sinksh/syntax_modules/sink_remove.cpp110
-rw-r--r--sinksh/syntax_modules/sink_stat.cpp120
-rw-r--r--sinksh/syntax_modules/sink_sync.cpp67
9 files changed, 997 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/core_syntax.cpp b/sinksh/syntax_modules/core_syntax.cpp
new file mode 100644
index 0000000..f5b6274
--- /dev/null
+++ b/sinksh/syntax_modules/core_syntax.cpp
@@ -0,0 +1,204 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QDebug>
21#include <QObject> // tr()
22#include <QSet>
23#include <QTextStream>
24
25#include "state.h"
26#include "syntaxtree.h"
27#include "utils.h"
28
29namespace CoreSyntax
30{
31
32bool exit(const QStringList &, State &)
33{
34 ::exit(0);
35 return true;
36}
37
38bool showHelp(const QStringList &commands, State &state)
39{
40 SyntaxTree::Command command = SyntaxTree::self()->match(commands);
41 if (commands.isEmpty()) {
42 state.printLine(QObject::tr("Welcome to the Sink command line tool!"));
43 state.printLine(QObject::tr("Top-level commands:"));
44
45 QSet<QString> sorted;
46 for (auto syntax: SyntaxTree::self()->syntax()) {
47 sorted.insert(syntax.keyword);
48 }
49
50 for (auto keyword: sorted) {
51 state.printLine(keyword, 1);
52 }
53 } else if (const Syntax *syntax = command.first) {
54 //TODO: get parent!
55 state.print(QObject::tr("Command `%1`").arg(syntax->keyword));
56
57 if (!syntax->help.isEmpty()) {
58 state.print(": " + syntax->help);
59 }
60 state.printLine();
61
62 if (!syntax->children.isEmpty()) {
63 state.printLine("Sub-commands:", 1);
64 QSet<QString> sorted;
65 for (auto childSyntax: syntax->children) {
66 sorted.insert(childSyntax.keyword);
67 }
68
69 for (auto keyword: sorted) {
70 state.printLine(keyword, 1);
71 }
72 }
73 } else {
74 state.printError("Unknown command: " + commands.join(" "));
75 }
76
77 return true;
78}
79
80QStringList showHelpCompleter(const QStringList &commands, const QString &fragment, State &)
81{
82 QStringList items;
83
84 for (auto syntax: SyntaxTree::self()->syntax()) {
85 if (syntax.keyword != QObject::tr("help") &&
86 (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) {
87 items << syntax.keyword;
88 }
89 }
90
91 qSort(items);
92 return items;
93}
94
95bool setDebugLevel(const QStringList &commands, State &state)
96{
97 if (commands.count() != 1) {
98 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
99 return false;
100 }
101
102 bool ok = false;
103 int level = commands[0].toUInt(&ok);
104
105 if (!ok) {
106 state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0]));
107 return false;
108 }
109
110 state.setDebugLevel(level);
111 return true;
112}
113
114bool printDebugLevel(const QStringList &, State &state)
115{
116 state.printLine(QString::number(state.debugLevel()));
117 return true;
118}
119
120bool printCommandTiming(const QStringList &, State &state)
121{
122 state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off"));
123 return true;
124}
125
126void printSyntaxBranch(State &state, const Syntax::List &list, int depth)
127{
128 if (list.isEmpty()) {
129 return;
130 }
131
132 if (depth > 0) {
133 state.printLine("\\", depth);
134 }
135
136 for (auto syntax: list) {
137 state.print("|-", depth);
138 state.printLine(syntax.keyword);
139 printSyntaxBranch(state, syntax.children, depth + 1);
140 }
141}
142
143bool printSyntaxTree(const QStringList &, State &state)
144{
145 printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0);
146 return true;
147}
148
149bool setLoggingLevel(const QStringList &commands, State &state)
150{
151 if (commands.count() != 1) {
152 state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count()));
153 return false;
154 }
155
156 state.setLoggingLevel(commands.at(0));
157 return true;
158}
159
160bool printLoggingLevel(const QStringList &commands, State &state)
161{
162 const QString level = state.loggingLevel();
163 state.printLine(level);
164 return true;
165}
166
167Syntax::List syntax()
168{
169 Syntax::List syntax;
170 syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit);
171
172 Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp);
173 help.completer = &CoreSyntax::showHelpCompleter;
174 syntax << help;
175
176 syntax << Syntax("syntaxtree", QString(), &printSyntaxTree);
177
178 Syntax set("set", QObject::tr("Sets settings for the session"));
179 set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel);
180
181 Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"));
182 setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; });
183 setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; });
184 set.children << setTiming;
185
186 Syntax logging("logging", QObject::tr("Set the logging level to one of Trace, Log, Warning or Error"), &CoreSyntax::setLoggingLevel);
187 logging.completer = [](const QStringList &, const QString &fragment, State &state) -> QStringList { return Utils::filteredCompletions(QStringList() << "trace" << "log" << "warning" << "error", fragment, Qt::CaseInsensitive); };
188 set.children << logging;
189
190 syntax << set;
191
192 Syntax get("get", QObject::tr("Gets settings for the session"));
193 get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel);
194 get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming);
195 get.children << Syntax("logging", QObject::tr("The current logging level"), &CoreSyntax::printLoggingLevel);
196 syntax << get;
197
198 return syntax;
199}
200
201REGISTER_SYNTAX(CoreSyntax)
202
203} // namespace CoreSyntax
204
diff --git a/sinksh/syntax_modules/sink_clear.cpp b/sinksh/syntax_modules/sink_clear.cpp
new file mode 100644
index 0000000..d02c638
--- /dev/null
+++ b/sinksh/syntax_modules/sink_clear.cpp
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23
24#include "common/resource.h"
25#include "common/storage.h"
26#include "common/domain/event.h"
27#include "common/domain/folder.h"
28#include "common/resourceconfig.h"
29#include "common/log.h"
30#include "common/storage.h"
31#include "common/definitions.h"
32
33#include "sinksh_utils.h"
34#include "state.h"
35#include "syntaxtree.h"
36
37namespace SinkClear
38{
39
40bool clear(const QStringList &args, State &state)
41{
42 for (const auto &resource : args) {
43 state.print(QObject::tr("Removing local cache for '%1' ...").arg(resource));
44 Sink::Store::removeFromDisk(resource.toLatin1());
45 state.printLine(QObject::tr("done"));
46 }
47
48 return true;
49}
50
51Syntax::List syntax()
52{
53 Syntax clear("clear", QObject::tr("Clears the local cache of one or more resources (be careful!)"), &SinkClear::clear);
54 clear.completer = &SinkshUtils::resourceCompleter;
55
56 return Syntax::List() << clear;
57}
58
59REGISTER_SYNTAX(SinkClear)
60
61}
diff --git a/sinksh/syntax_modules/sink_count.cpp b/sinksh/syntax_modules/sink_count.cpp
new file mode 100644
index 0000000..fde7c33
--- /dev/null
+++ b/sinksh/syntax_modules/sink_count.cpp
@@ -0,0 +1,83 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkCount
40{
41
42bool count(const QStringList &args, State &state)
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;
53 for (const auto &res : resources) {
54 query.resources << res.toLatin1();
55 }
56 query.liveQuery = false;
57
58 auto model = SinkshUtils::loadModel(type, query);
59 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
60 if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
61 state.printLine(QObject::tr("Counted results %1").arg(model->rowCount(QModelIndex())));
62 state.commandFinished();
63 }
64 });
65
66 if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
67 return true;
68 }
69
70 return true;
71}
72
73Syntax::List syntax()
74{
75 Syntax count("count", QObject::tr("Returns the number of items of a given type in a resource. Usage: count <type> <resource>"), &SinkCount::count, Syntax::EventDriven);
76 count.completer = &SinkshUtils::typeCompleter;
77
78 return Syntax::List() << count;
79}
80
81REGISTER_SYNTAX(SinkCount)
82
83}
diff --git a/sinksh/syntax_modules/sink_create.cpp b/sinksh/syntax_modules/sink_create.cpp
new file mode 100644
index 0000000..cd2cd80
--- /dev/null
+++ b/sinksh/syntax_modules/sink_create.cpp
@@ -0,0 +1,118 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkCreate
40{
41
42bool create(const QStringList &allArgs, State &state)
43{
44 if (allArgs.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "sinkcreate/02");
46 return false;
47 }
48
49 if (allArgs.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to create items"), "sinkcreate/03");
51 return false;
52 }
53
54 auto args = allArgs;
55 auto type = args.takeFirst();
56 auto &store = SinkshUtils::getStore(type);
57 Sink::ApplicationDomain::ApplicationDomainType::Ptr object;
58 auto resource = args.takeFirst().toLatin1();
59 object = store.getObject(resource);
60
61 auto map = SinkshUtils::keyValueMapFromArgs(args);
62 for (auto i = map.begin(); i != map.end(); ++i) {
63 object->setProperty(i.key().toLatin1(), i.value());
64 }
65
66 auto result = store.create(*object).exec();
67 result.waitForFinished();
68 if (result.errorCode()) {
69 state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
70 "akonaid_create_e" + QString::number(result.errorCode()));
71 }
72
73 return true;
74}
75
76bool resource(const QStringList &args, State &state)
77{
78 if (args.isEmpty()) {
79 state.printError(QObject::tr("A resource can not be created without a type"), "sinkcreate/01");
80 return false;
81 }
82
83 auto &store = SinkshUtils::getStore("resource");
84
85 auto resourceType = args.at(0);
86 Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("");
87 object->setProperty("type", resourceType);
88
89 auto map = SinkshUtils::keyValueMapFromArgs(args);
90 for (auto i = map.begin(); i != map.end(); ++i) {
91 object->setProperty(i.key().toLatin1(), i.value());
92 }
93
94 auto result = store.create(*object).exec();
95 result.waitForFinished();
96 if (result.errorCode()) {
97 state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
98 "akonaid_create_e" + QString::number(result.errorCode()));
99 }
100
101 return true;
102}
103
104
105Syntax::List syntax()
106{
107 Syntax::List syntax;
108
109 Syntax create("create", QObject::tr("Create items in a resource"), &SinkCreate::create);
110 create.children << Syntax("resource", QObject::tr("Creates a new resource"), &SinkCreate::resource);
111
112 syntax << create;
113 return syntax;
114}
115
116REGISTER_SYNTAX(SinkCreate)
117
118}
diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp
new file mode 100644
index 0000000..9712b6f
--- /dev/null
+++ b/sinksh/syntax_modules/sink_list.cpp
@@ -0,0 +1,114 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkList
40{
41
42bool list(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("Please provide at least one type to list (e.g. resource, .."));
46 return false;
47 }
48
49 auto resources = args;
50 auto type = !resources.isEmpty() ? resources.takeFirst() : QString();
51
52 if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) {
53 state.printError(QObject::tr("Unknown type: %1").arg(type));
54 return false;
55 }
56
57 Sink::Query query;
58 for (const auto &res : resources) {
59 query.resources << res.toLatin1();
60 }
61 query.liveQuery = false;
62
63 QTime time;
64 time.start();
65 auto model = SinkshUtils::loadModel(type, query);
66 if (state.debugLevel() > 0) {
67 state.printLine(QObject::tr("Folder type %1").arg(type));
68 state.printLine(QObject::tr("Loaded model in %1 ms").arg(time.elapsed()));
69 }
70
71 //qDebug() << "Listing";
72 int colSize = 38; //Necessary to display a complete UUID
73 state.print(QObject::tr("Resource").leftJustified(colSize, ' ', true) +
74 QObject::tr("Identifier").leftJustified(colSize, ' ', true));
75 for (int i = 0; i < model->columnCount(QModelIndex()); i++) {
76 state.print(" | " + model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true));
77 }
78 state.printLine();
79
80 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize, state](const QModelIndex &index, int start, int end) {
81 for (int i = start; i <= end; i++) {
82 auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>();
83 state.print(object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true));
84 state.print(object->identifier().leftJustified(colSize, ' ', true));
85 for (int col = 0; col < model->columnCount(QModelIndex()); col++) {
86 state.print(" | " + model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true));
87 }
88 state.printLine();
89 }
90 });
91
92 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
93 if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
94 state.commandFinished();
95 }
96 });
97
98 if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
99 return true;
100 }
101
102 return false;
103}
104
105Syntax::List syntax()
106{
107 Syntax list("list", QObject::tr("List all resources, or the contents of one or more resources"), &SinkList::list, Syntax::EventDriven);
108 list.completer = &SinkshUtils::resourceOrTypeCompleter;
109 return Syntax::List() << list;
110}
111
112REGISTER_SYNTAX(SinkList)
113
114}
diff --git a/sinksh/syntax_modules/sink_modify.cpp b/sinksh/syntax_modules/sink_modify.cpp
new file mode 100644
index 0000000..4d637d8
--- /dev/null
+++ b/sinksh/syntax_modules/sink_modify.cpp
@@ -0,0 +1,120 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkModify
40{
41
42bool modify(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "sink_modify/02");
46 return false;
47 }
48
49 if (args.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to remove items"), "sink_modify/03");
51 return false;
52 }
53
54 if (args.count() < 3) {
55 state.printError(QObject::tr("An object ID is required to remove items"), "sink_modify/03");
56 return false;
57 }
58
59 auto type = args[0];
60 auto resourceId = args[1];
61 auto identifier = args[2];
62
63 auto &store = SinkshUtils::getStore(type);
64 Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8());
65
66 auto map = SinkshUtils::keyValueMapFromArgs(args);
67 for (auto i = map.begin(); i != map.end(); ++i) {
68 object->setProperty(i.key().toLatin1(), i.value());
69 }
70
71 auto result = store.modify(*object).exec();
72 result.waitForFinished();
73 if (result.errorCode()) {
74 state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()),
75 "akonaid__modify_e" + QString::number(result.errorCode()));
76 }
77
78 return true;
79}
80
81bool resource(const QStringList &args, State &state)
82{
83 if (args.isEmpty()) {
84 state.printError(QObject::tr("A resource can not be modified without an id"), "sink_modify/01");
85 }
86
87 auto &store = SinkshUtils::getStore("resource");
88
89 auto resourceId = args.at(0);
90 Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1());
91
92 auto map = SinkshUtils::keyValueMapFromArgs(args);
93 for (auto i = map.begin(); i != map.end(); ++i) {
94 object->setProperty(i.key().toLatin1(), i.value());
95 }
96
97 auto result = store.modify(*object).exec();
98 result.waitForFinished();
99 if (result.errorCode()) {
100 state.printError(QObject::tr("An error occurred while modifying the resource %1: %2").arg(resourceId).arg(result.errorMessage()),
101 "akonaid_modify_e" + QString::number(result.errorCode()));
102 }
103
104 return true;
105}
106
107
108Syntax::List syntax()
109{
110 Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify);
111 Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven);
112 resource.completer = &SinkshUtils::resourceOrTypeCompleter;
113 modify.children << resource;
114
115 return Syntax::List() << modify;
116}
117
118REGISTER_SYNTAX(SinkModify)
119
120}
diff --git a/sinksh/syntax_modules/sink_remove.cpp b/sinksh/syntax_modules/sink_remove.cpp
new file mode 100644
index 0000000..b374824
--- /dev/null
+++ b/sinksh/syntax_modules/sink_remove.cpp
@@ -0,0 +1,110 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QCoreApplication>
21#include <QDebug>
22#include <QObject> // tr()
23#include <QModelIndex>
24#include <QTime>
25
26#include "common/resource.h"
27#include "common/storage.h"
28#include "common/domain/event.h"
29#include "common/domain/folder.h"
30#include "common/resourceconfig.h"
31#include "common/log.h"
32#include "common/storage.h"
33#include "common/definitions.h"
34
35#include "sinksh_utils.h"
36#include "state.h"
37#include "syntaxtree.h"
38
39namespace SinkRemove
40{
41
42bool remove(const QStringList &args, State &state)
43{
44 if (args.isEmpty()) {
45 state.printError(QObject::tr("A type is required"), "sink_remove/02");
46 return false;
47 }
48
49 if (args.count() < 2) {
50 state.printError(QObject::tr("A resource ID is required to remove items"), "sink_remove/03");
51 return false;
52 }
53
54 if (args.count() < 3) {
55 state.printError(QObject::tr("An object ID is required to remove items"), "sink_remove/03");
56 return false;
57 }
58
59 auto type = args[0];
60 auto resourceId = args[1];
61 auto identifier = args[2];
62
63 auto &store = SinkshUtils::getStore(type);
64 Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject(resourceId.toUtf8(), identifier.toUtf8());
65
66 auto result = store.remove(*object).exec();
67 result.waitForFinished();
68 if (result.errorCode()) {
69 state.printError(QObject::tr("An error occurred while removing %1 from %1: %2").arg(identifier).arg(resourceId).arg(result.errorMessage()),
70 "akonaid_remove_e" + QString::number(result.errorCode()));
71 }
72
73 return true;
74}
75
76bool resource(const QStringList &args, State &state)
77{
78 if (args.isEmpty()) {
79 state.printError(QObject::tr("A resource can not be removed without an id"), "sink_remove/01");
80 }
81
82 auto &store = SinkshUtils::getStore("resource");
83
84 auto resourceId = args.at(0);
85 Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1());
86
87 auto result = store.remove(*object).exec();
88 result.waitForFinished();
89 if (result.errorCode()) {
90 state.printError(QObject::tr("An error occurred while removing the resource %1: %2").arg(resourceId).arg(result.errorMessage()),
91 "akonaid_remove_e" + QString::number(result.errorCode()));
92 }
93
94 return true;
95}
96
97
98Syntax::List syntax()
99{
100 Syntax remove("remove", QObject::tr("Remove items in a resource"), &SinkRemove::remove);
101 Syntax resource("resource", QObject::tr("Removes a resource"), &SinkRemove::resource);//, Syntax::EventDriven);
102 resource.completer = &SinkshUtils::resourceCompleter;
103 remove.children << resource;
104
105 return Syntax::List() << remove;
106}
107
108REGISTER_SYNTAX(SinkRemove)
109
110}
diff --git a/sinksh/syntax_modules/sink_stat.cpp b/sinksh/syntax_modules/sink_stat.cpp
new file mode 100644
index 0000000..06586d9
--- /dev/null
+++ b/sinksh/syntax_modules/sink_stat.cpp
@@ -0,0 +1,120 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23#include <QDir>
24
25#include "common/resource.h"
26#include "common/storage.h"
27#include "common/domain/event.h"
28#include "common/domain/folder.h"
29#include "common/resourceconfig.h"
30#include "common/log.h"
31#include "common/storage.h"
32#include "common/definitions.h"
33
34#include "sinksh_utils.h"
35#include "state.h"
36#include "syntaxtree.h"
37
38namespace SinkStat
39{
40
41void statResources(const QStringList &resources, const State &state)
42{
43 qint64 total = 0;
44 for (const auto &resource : resources) {
45 Sink::Storage storage(Sink::storageLocation(), resource, Sink::Storage::ReadOnly);
46 auto transaction = storage.createTransaction(Sink::Storage::ReadOnly);
47
48 QList<QByteArray> databases = transaction.getDatabaseNames();
49 for (const auto &databaseName : databases) {
50 state.printLine(QObject::tr("Database: %1").arg(QString(databaseName)), 1);
51 auto db = transaction.openDatabase(databaseName);
52 qint64 size = db.getSize() / 1024;
53 state.printLine(QObject::tr("Size [kb]: %1").arg(size), 1);
54 total += size;
55 }
56 int diskUsage = 0;
57
58 QDir dir(Sink::storageLocation());
59 for (const auto &folder : dir.entryList(QStringList() << resource + "*")) {
60 diskUsage += Sink::Storage(Sink::storageLocation(), folder, Sink::Storage::ReadOnly).diskUsage();
61 }
62 auto size = diskUsage / 1024;
63 state.printLine(QObject::tr("Disk usage [kb]: %1").arg(size), 1);
64 }
65
66 state.printLine(QObject::tr("Total [kb]: %1").arg(total));
67}
68
69bool statAllResources(State &state)
70{
71 Sink::Query query;
72 query.liveQuery = false;
73 auto model = SinkshUtils::loadModel("resource", query);
74
75 //SUUUPER ugly, but can't think of a better way with 2 glasses of wine in me on Christmas day
76 static QStringList resources;
77 resources.clear();
78
79 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) mutable {
80 for (int i = start; i <= end; i++) {
81 auto object = model->data(model->index(i, 0, index), Sink::Store::DomainObjectBaseRole).value<Sink::ApplicationDomain::ApplicationDomainType::Ptr>();
82 resources << object->identifier();
83 }
84 });
85
86 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, state](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
87 if (roles.contains(Sink::Store::ChildrenFetchedRole)) {
88 statResources(resources, state);
89 state.commandFinished();
90 }
91 });
92
93 if (!model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()) {
94 return true;
95 }
96
97 return false;
98}
99
100bool stat(const QStringList &args, State &state)
101{
102 if (args.isEmpty()) {
103 return statAllResources(state);
104 }
105
106 statResources(args, state);
107 return false;
108}
109
110Syntax::List syntax()
111{
112 Syntax state("stat", QObject::tr("Shows database usage for the resources requested"), &SinkStat::stat, Syntax::EventDriven);
113 state.completer = &SinkshUtils::resourceCompleter;
114
115 return Syntax::List() << state;
116}
117
118REGISTER_SYNTAX(SinkStat)
119
120}
diff --git a/sinksh/syntax_modules/sink_sync.cpp b/sinksh/syntax_modules/sink_sync.cpp
new file mode 100644
index 0000000..3006202
--- /dev/null
+++ b/sinksh/syntax_modules/sink_sync.cpp
@@ -0,0 +1,67 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <QDebug>
21#include <QObject> // tr()
22#include <QTimer>
23
24#include "common/resource.h"
25#include "common/storage.h"
26#include "common/domain/event.h"
27#include "common/domain/folder.h"
28#include "common/resourceconfig.h"
29#include "common/log.h"
30#include "common/storage.h"
31#include "common/definitions.h"
32
33#include "sinksh_utils.h"
34#include "state.h"
35#include "syntaxtree.h"
36
37namespace SinkSync
38{
39
40bool sync(const QStringList &args, State &state)
41{
42 Sink::Query query;
43 for (const auto &res : args) {
44 query.resources << res.toLatin1();
45 }
46
47 QTimer::singleShot(0, [query, state]() {
48 Sink::Store::synchronize(query).then<void>([state]() {
49 state.printLine("Synchronization complete!");
50 state.commandFinished();
51 }).exec();
52 });
53
54 return true;
55}
56
57Syntax::List syntax()
58{
59 Syntax sync("sync", QObject::tr("Syncronizes all resources that are listed; and empty list triggers a syncronizaton on all resources"), &SinkSync::sync, Syntax::EventDriven );
60 sync.completer = &SinkshUtils::resourceCompleter;
61
62 return Syntax::List() << sync;
63}
64
65REGISTER_SYNTAX(SinkSync)
66
67}