13
0

WebSockets: improve surface manifest format and loader

This commit is contained in:
Luciano Iam 2020-04-11 09:31:06 +02:00 committed by Robin Gareus
parent 420137ea9d
commit e82171ea19
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
3 changed files with 31 additions and 22 deletions

View File

@ -18,15 +18,21 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h> #include <glibmm/miscutils.h>
#include "pbd/xml++.h" #include "pbd/xml++.h"
#include "manifest.h" #include "manifest.h"
SurfaceManifest::SurfaceManifest (std::string xml_path) static const char* const manifest_filename = "manifest.xml";
SurfaceManifest::SurfaceManifest (std::string path)
: _path (path)
{ {
XMLTree tree; XMLTree tree;
std::string xml_path = Glib::build_filename (_path, manifest_filename);
if (!tree.read (xml_path.c_str())) { if (!tree.read (xml_path.c_str())) {
#ifndef NDEBUG #ifndef NDEBUG
@ -44,25 +50,18 @@ SurfaceManifest::SurfaceManifest (std::string xml_path)
node->get_property ("value", value); node->get_property ("value", value);
if (name == "Id") { if (name == "Name") {
_id = value;
} else if (name == "Name") {
_name = value; _name = value;
} else if (name == "Description") { } else if (name == "Description") {
_description = value; _description = value;
} }
} }
#ifndef NDEBUG
if (_name.empty () || _description.empty ()) { if (_name.empty () || _description.empty ()) {
#ifndef NDEBUG
std::cerr << "SurfaceManifest: missing properties in " << xml_path << std::endl; std::cerr << "SurfaceManifest: missing properties in " << xml_path << std::endl;
return;
}
#endif #endif
return;
if (_id.empty ()) {
// default to manifest subdirectory name
_id = Glib::path_get_basename (Glib::path_get_dirname (xml_path));
} }
_valid = true; _valid = true;
@ -73,11 +72,21 @@ SurfaceManifest::to_json ()
{ {
std::stringstream ss; std::stringstream ss;
std::string rel_path = Glib::path_get_basename (Glib::path_get_dirname (_path));
ss << "{" ss << "{"
<< "\"id\":\"" << _id << "\"" << "\"diskPath\":\"" << _path << "\""
<< ",\"path\":\"" << rel_path << "\""
<< ",\"name\":\"" << _name << "\"" << ",\"name\":\"" << _name << "\""
<< ",\"description\":\"" << _description << "\"" << ",\"description\":\"" << _description << "\""
<< "}"; << "}";
return ss.str (); return ss.str ();
} }
bool
SurfaceManifest::exists_at_path (std::string path)
{
std::string xml_path = Glib::build_filename (path, manifest_filename);
return Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS);
}

View File

@ -29,16 +29,18 @@ public:
bool valid () { return _valid; } bool valid () { return _valid; }
std::string id () { return _id; } std::string path () { return _path; }
std::string name () { return _name; } std::string name () { return _name; }
std::string description () { return _description; } std::string description () { return _description; }
std::string to_json (); std::string to_json ();
static bool exists_at_path (std::string);
private: private:
bool _valid; bool _valid;
std::string _id; std::string _path;
std::string _name; std::string _name;
std::string _description; std::string _description;
}; };

View File

@ -30,7 +30,7 @@
static const char* const data_dir_env_var = "ARDOUR_WEBSURFACES_PATH"; static const char* const data_dir_env_var = "ARDOUR_WEBSURFACES_PATH";
static const char* const data_dir_name = "web_surfaces"; static const char* const data_dir_name = "web_surfaces";
static const char* const builtin_dir_name = "builtin"; static const char* const builtin_dir_name = "builtin";
static const char* const manifest_filename = "manifest.xml"; static const char* const user_dir_name = "user";
static bool static bool
dir_filter (const std::string &str, void* /*arg*/) dir_filter (const std::string &str, void* /*arg*/)
@ -80,7 +80,7 @@ ServerResources::scan ()
{ {
std::stringstream ss; std::stringstream ss;
ss << "{\"builtin\":["; ss << "[{\"path\":\"" << builtin_dir_name << "\",\"surfaces\":[";
SurfaceManifestVector builtin = read_manifests (builtin_dir ()); SurfaceManifestVector builtin = read_manifests (builtin_dir ());
@ -91,7 +91,7 @@ ServerResources::scan ()
} }
} }
ss << "],\"user\":["; ss << "]},{\"path\":\"" << user_dir_name << "\",\"surfaces\":[";
SurfaceManifestVector user = read_manifests (user_dir ()); SurfaceManifestVector user = read_manifests (user_dir ());
@ -102,7 +102,7 @@ ServerResources::scan ()
} }
} }
ss << "]}"; ss << "]}]";
return ss.str (); return ss.str ();
} }
@ -143,13 +143,11 @@ ServerResources::read_manifests (std::string dir)
0 /*arg*/, true /*pass_fullpath*/, true /*return_fullpath*/, false /*recurse*/); 0 /*arg*/, true /*pass_fullpath*/, true /*return_fullpath*/, false /*recurse*/);
for (std::vector<std::string>::const_iterator it = subdirs.begin (); it != subdirs.end (); ++it) { for (std::vector<std::string>::const_iterator it = subdirs.begin (); it != subdirs.end (); ++it) {
std::string xml_path = Glib::build_filename (*it, manifest_filename); if (!SurfaceManifest::exists_at_path (*it)) {
if (!Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
continue; continue;
} }
SurfaceManifest manifest (xml_path); SurfaceManifest manifest (*it);
if (manifest.valid ()) { if (manifest.valid ()) {
result.push_back (manifest); result.push_back (manifest);