summaryrefslogtreecommitdiffstats
path: root/sinksh/syntax_modules/sink_modify.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sinksh/syntax_modules/sink_modify.cpp')
-rw-r--r--sinksh/syntax_modules/sink_modify.cpp120
1 files changed, 120 insertions, 0 deletions
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}