1 /**
2 * File name: kit_state.cpp
3 * Project: Geonkick (A kick synthesizer)
4 *
5 * Copyright (C) 2020 Iurie Nistor <http://iuriepage.wordpress.com>
6 *
7 * This file is part of Geonkick.
8 *
9 * GeonKick is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "percussion_state.h"
25 #include "kit_state.h"
26
27
KitState()28 KitState::KitState()
29 : kitAppVersion{GEONKICK_VERSION}
30 , kitName{"Default"}
31 , kitAuthor{"Unknown"}
32 {
33 }
34
open(const std::string & fileName)35 bool KitState::open(const std::string &fileName)
36 {
37 if (fileName.size() < 6) {
38 RK_LOG_ERROR("can't open preset. File name empty or wrong format.");
39 return false;
40 }
41
42 std::filesystem::path filePath(fileName);
43 if (filePath.extension().empty()
44 || (filePath.extension() != ".gkit"
45 && filePath.extension() != ".GKIT")) {
46 RK_LOG_ERROR("can't open kit. Wrong file format.");
47 return false;
48 }
49
50 std::ifstream sfile;
51 sfile.open(std::filesystem::absolute(filePath));
52 if (!sfile.is_open()) {
53 RK_LOG_ERROR("can't open kit.");
54 return false;
55 }
56
57 std::string fileData((std::istreambuf_iterator<char>(sfile)),
58 (std::istreambuf_iterator<char>()));
59
60 sfile.close();
61 fromJson(fileData);
62 return false;
63 }
64
save(const std::string & fileName)65 bool KitState::save(const std::string &fileName)
66 {
67 if (fileName.size() < 6) {
68 RK_LOG_ERROR("can't save kit. Wrong file name");
69 return false;
70 }
71
72 std::filesystem::path filePath(fileName);
73 if (filePath.extension().empty()
74 || (filePath.extension() != ".gkit"
75 && filePath.extension() != ".GKIT")) {
76 filePath.replace_extension(".gkit");
77 }
78
79 std::ofstream file;
80 file.open(std::filesystem::absolute(filePath));
81 if (!file.is_open()) {
82 RK_LOG_ERROR("can't open file for saving: " << filePath);
83 return false;
84 }
85 file << toJson();
86 file.close();
87 auto path = filePath.has_parent_path() ? filePath.parent_path() : filePath;
88 return true;
89 }
90
setName(const std::string & name)91 void KitState::setName(const std::string &name)
92 {
93 kitName = name;
94 }
95
getName() const96 std::string KitState::getName() const
97 {
98 return kitName;
99 }
100
setAuthor(const std::string & author)101 void KitState::setAuthor(const std::string &author)
102 {
103 kitAuthor = author;
104 }
105
getAuthor() const106 std::string KitState::getAuthor() const
107 {
108 return kitAuthor;
109 }
110
setUrl(const std::string & url)111 void KitState::setUrl(const std::string &url)
112 {
113 kitUrl = url;
114 }
115
getUrl() const116 std::string KitState::getUrl() const
117 {
118 return kitUrl;
119 }
120
percussions()121 std::vector<std::shared_ptr<PercussionState>>& KitState::percussions()
122 {
123 return percussionsList;
124 }
125
fromJson(const std::string & jsonData)126 void KitState::fromJson(const std::string &jsonData)
127 {
128 rapidjson::Document document;
129 document.Parse(jsonData.c_str());
130 if (!document.IsObject())
131 return;
132 fromJsonObject(document);
133 }
134
fromJsonObject(const rapidjson::Value & obj)135 void KitState::fromJsonObject(const rapidjson::Value &obj)
136 {
137 for (const auto &m: obj.GetObject()) {
138 if (m.name == "KitAppVersion" && m.value.IsInt())
139 kitAppVersion = m.value.GetInt();
140 if (m.name == "name" && m.value.IsString())
141 setName(m.value.GetString());
142 if (m.name == "author" && m.value.IsString())
143 setAuthor(m.value.GetString());
144 if (m.name == "url" && m.value.IsString())
145 setUrl(m.value.GetString());
146 if (m.name == "percussions" && m.value.IsArray())
147 parsePercussions(m.value);
148 }
149 }
150
parsePercussions(const rapidjson::Value & percussionsArray)151 void KitState::parsePercussions(const rapidjson::Value &percussionsArray)
152 {
153 size_t i = 0;
154 for (const auto &per: percussionsArray.GetArray()) {
155 auto state = std::make_shared<PercussionState>();
156 state->setId(i++);
157 state->loadObject(per);
158 addPercussion(state);
159 }
160 }
161
toJson() const162 std::string KitState::toJson() const
163 {
164 std::ostringstream jsonStream;
165 jsonStream << "{" << std::endl;
166 jsonStream << "\"KitAppVersion\": " << GEONKICK_VERSION << "," << std::endl;
167 jsonStream << "\"name\": \"" << getName() << "\"," << std::endl;
168 jsonStream << "\"author\": \"" << getAuthor() << "\"," << std::endl;
169 jsonStream << "\"url\": \"" << getUrl() << "\"," << std::endl;
170 jsonStream << "\"percussions\": [" << std::endl;
171
172 size_t i = 0;
173 for (const auto &per: percussionsList) {
174 if (i < percussionsList.size() - 1)
175 jsonStream << per->toJson() << "," << std::endl;
176 else
177 jsonStream << per->toJson();
178 i++;
179 }
180 jsonStream << "]" << std::endl;
181 jsonStream << "}" << std::endl;
182 return jsonStream.str();
183 }
184
addPercussion(const std::shared_ptr<PercussionState> & percussion)185 void KitState::addPercussion(const std::shared_ptr<PercussionState> &percussion)
186 {
187 percussionsList.push_back(percussion);
188 }
189
getPercussion(size_t id)190 std::shared_ptr<PercussionState> KitState::getPercussion(size_t id)
191 {
192 if (id < percussionsList.size())
193 return percussionsList[id];
194 return nullptr;
195 }
196