Fixup prev commit (LV2 X11 UI) -- #7837
[ardour.git] / libs / ardour / user_bundle.cc
1 /*
2  * Copyright (C) 2007-2010 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "ardour/user_bundle.h"
22 #include "ardour/types_convert.h"
23 #include "pbd/i18n.h"
24 #include "pbd/compose.h"
25 #include "pbd/error.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28
29 ARDOUR::UserBundle::UserBundle (std::string const & n)
30         : Bundle (n)
31 {
32
33 }
34
35 ARDOUR::UserBundle::UserBundle (XMLNode const & node, bool i)
36         : Bundle (i)
37 {
38         if (set_state (node, Stateful::loading_state_version)) {
39                 throw failed_constructor ();
40         }
41 }
42
43 int
44 ARDOUR::UserBundle::set_state (XMLNode const & node, int /*version*/)
45 {
46         std::string str;
47         if (!node.get_property ("name", str)) {
48                 PBD::error << _("Node for Bundle has no \"name\" property") << endmsg;
49                 return -1;
50         }
51
52         set_name (str);
53
54         XMLNodeList const channels = node.children ();
55
56         int n = 0;
57         for (XMLNodeConstIterator i = channels.begin(); i != channels.end(); ++i) {
58
59                 if ((*i)->name() != "Channel") {
60                         PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*i)->name()) << endmsg;
61                         return -1;
62                 }
63
64                 if (!(*i)->get_property ("name", str)) {
65                         PBD::error << _("Node for Channel has no \"name\" property") << endmsg;
66                         return -1;
67                 }
68
69                 DataType type(DataType::NIL);
70                 if (!(*i)->get_property ("type", type)) {
71                         PBD::error << _("Node for Channel has no \"type\" property") << endmsg;
72                         return -1;
73                 }
74
75                 add_channel (str, type);
76
77                 XMLNodeList const ports = (*i)->children ();
78
79                 for (XMLNodeConstIterator j = ports.begin(); j != ports.end(); ++j) {
80                         if ((*j)->name() != "Port") {
81                                 PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*j)->name()) << endmsg;
82                                 return -1;
83                         }
84
85                         if (!(*j)->get_property ("name", str)) {
86                                 PBD::error << _("Node for Port has no \"name\" property") << endmsg;
87                                 return -1;
88                         }
89
90                         add_port_to_channel (n, str);
91                 }
92
93                 ++n;
94         }
95
96         return 0;
97 }
98
99 XMLNode&
100 ARDOUR::UserBundle::get_state ()
101 {
102         XMLNode *node;
103
104         if (ports_are_inputs ()) {
105                 node = new XMLNode ("InputBundle");
106         } else {
107                 node = new XMLNode ("OutputBundle");
108         }
109
110         node->set_property ("name", name ());
111
112         {
113                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
114
115                 for (std::vector<Channel>::iterator i = _channel.begin(); i != _channel.end(); ++i) {
116                         XMLNode* c = new XMLNode ("Channel");
117                         c->set_property ("name", i->name);
118                         c->set_property ("type", i->type);
119
120                         for (PortList::iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
121                                 XMLNode* p = new XMLNode ("Port");
122                                 p->set_property ("name", *j);
123                                 c->add_child_nocopy (*p);
124                         }
125
126                         node->add_child_nocopy (*c);
127                 }
128         }
129
130         return *node;
131 }