fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / user_bundle.cc
1 /*
2     Copyright (C) 2012 Paul Davis
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 Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "ardour/user_bundle.h"
21 #include "pbd/i18n.h"
22 #include "pbd/compose.h"
23 #include "pbd/error.h"
24 #include "pbd/failed_constructor.h"
25 #include "pbd/xml++.h"
26
27 ARDOUR::UserBundle::UserBundle (std::string const & n)
28         : Bundle (n)
29 {
30
31 }
32
33 ARDOUR::UserBundle::UserBundle (XMLNode const & node, bool i)
34         : Bundle (i)
35 {
36         if (set_state (node, Stateful::loading_state_version)) {
37                 throw failed_constructor ();
38         }
39 }
40
41 int
42 ARDOUR::UserBundle::set_state (XMLNode const & node, int /*version*/)
43 {
44         XMLProperty const * name;
45
46         if ((name = node.property ("name")) == 0) {
47                 PBD::error << _("Node for Bundle has no \"name\" property") << endmsg;
48                 return -1;
49         }
50
51         set_name (name->value ());
52
53         XMLNodeList const channels = node.children ();
54
55         int n = 0;
56         for (XMLNodeConstIterator i = channels.begin(); i != channels.end(); ++i) {
57
58                 if ((*i)->name() != "Channel") {
59                         PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*i)->name()) << endmsg;
60                         return -1;
61                 }
62
63                 if ((name = (*i)->property ("name")) == 0) {
64                         PBD::error << _("Node for Channel has no \"name\" property") << endmsg;
65                         return -1;
66                 }
67
68                 XMLProperty const * type;
69                 if ((type = (*i)->property ("type")) == 0) {
70                         PBD::error << _("Node for Channel has no \"type\" property") << endmsg;
71                         return -1;
72                 }
73
74                 add_channel (name->value (), DataType (type->value()));
75
76                 XMLNodeList const ports = (*i)->children ();
77
78                 for (XMLNodeConstIterator j = ports.begin(); j != ports.end(); ++j) {
79                         if ((*j)->name() != "Port") {
80                                 PBD::error << string_compose (_("Unknown node \"%s\" in Bundle"), (*j)->name()) << endmsg;
81                                 return -1;
82                         }
83
84                         if ((name = (*j)->property ("name")) == 0) {
85                                 PBD::error << _("Node for Port has no \"name\" property") << endmsg;
86                                 return -1;
87                         }
88
89                         add_port_to_channel (n, name->value ());
90                 }
91
92                 ++n;
93         }
94
95         return 0;
96 }
97
98 XMLNode&
99 ARDOUR::UserBundle::get_state ()
100 {
101         XMLNode *node;
102
103         if (ports_are_inputs ()) {
104                 node = new XMLNode ("InputBundle");
105         } else {
106                 node = new XMLNode ("OutputBundle");
107         }
108
109         node->add_property ("name", name ());
110
111         {
112                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
113
114                 for (std::vector<Channel>::iterator i = _channel.begin(); i != _channel.end(); ++i) {
115                         XMLNode* c = new XMLNode ("Channel");
116                         c->add_property ("name", i->name);
117                         c->add_property ("type", i->type.to_string());
118
119                         for (PortList::iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
120                                 XMLNode* p = new XMLNode ("Port");
121                                 p->add_property ("name", *j);
122                                 c->add_child_nocopy (*p);
123                         }
124
125                         node->add_child_nocopy (*c);
126                 }
127         }
128
129         return *node;
130 }