diff options
Diffstat (limited to 'sinksh/syntax_modules/sink_create.cpp')
-rw-r--r-- | sinksh/syntax_modules/sink_create.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
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 | |||
39 | namespace SinkCreate | ||
40 | { | ||
41 | |||
42 | bool 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 | |||
76 | bool 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 | |||
105 | Syntax::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 | |||
116 | REGISTER_SYNTAX(SinkCreate) | ||
117 | |||
118 | } | ||