1 /*
2 * Copyright (C) 2020 Luciano Iam <oss@lucianoiam.com>
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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <iostream>
20 #include <sstream>
21
22 #include <glibmm/fileutils.h>
23 #include <glibmm/miscutils.h>
24
25 #include "pbd/xml++.h"
26
27 #include "manifest.h"
28 #include "resources.h"
29 #include "json.h"
30
31 using namespace ArdourSurface;
32
33 static const char* const manifest_filename = "manifest.xml";
34
SurfaceManifest(std::string path)35 SurfaceManifest::SurfaceManifest (std::string path)
36 : _path (path)
37 {
38 XMLTree tree;
39 std::string xml_path = Glib::build_filename (_path, manifest_filename);
40
41 if (!tree.read (xml_path.c_str())) {
42 #ifndef NDEBUG
43 std::cerr << "SurfaceManifest: could not parse " << xml_path << std::endl;
44 #endif
45 return;
46 }
47
48 XMLNodeList nlist = tree.root ()->children ();
49
50 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
51 XMLNode* node = *niter;
52 std::string name = node->name ();
53 std::string value;
54
55 node->get_property ("value", value);
56
57 if (name == "Name") {
58 _name = value;
59 } else if (name == "Description") {
60 _description = value;
61 } else if (name == "Version") {
62 _version = value;
63 }
64 }
65
66 if (_name.empty () || _description.empty () || _version.empty ()) {
67 #ifndef NDEBUG
68 std::cerr << "SurfaceManifest: missing properties in " << xml_path << std::endl;
69 #endif
70 return;
71 }
72
73 _valid = true;
74 }
75
76 std::string
to_json()77 SurfaceManifest::to_json ()
78 {
79 std::stringstream ss;
80
81 ss << "{"
82 << "\"path\":\"" << WebSocketsJSON::escape (Glib::path_get_basename (_path)) << "\""
83 << ",\"name\":\"" << WebSocketsJSON::escape (_name) << "\""
84 << ",\"description\":\"" << WebSocketsJSON::escape (_description) << "\""
85 << ",\"version\":\"" << WebSocketsJSON::escape (_version) << "\""
86 << "}";
87
88 return ss.str ();
89 }
90
91 bool
exists_at_path(std::string path)92 SurfaceManifest::exists_at_path (std::string path)
93 {
94 std::string xml_path = Glib::build_filename (path, manifest_filename);
95 return Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS);
96 }
97