+
+void
+ControlProtocolManager::foreach_known_protocol (boost::function<void(const ControlProtocolInfo*)> method)
+{
+ for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
+ method (*i);
+ }
+}
+
+ControlProtocolInfo*
+ControlProtocolManager::cpi_by_name (string name)
+{
+ for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
+ if (name == (*i)->name) {
+ return *i;
+ }
+ }
+ return 0;
+}
+
+int
+ControlProtocolManager::set_state (const XMLNode& node, int /*version*/)
+{
+ XMLNodeList clist;
+ XMLNodeConstIterator citer;
+ XMLProperty* prop;
+
+ Glib::Mutex::Lock lm (protocols_lock);
+
+ clist = node.children();
+
+ for (citer = clist.begin(); citer != clist.end(); ++citer) {
+ if ((*citer)->name() == X_("Protocol")) {
+
+ prop = (*citer)->property (X_("active"));
+
+ if (prop && string_is_affirmative (prop->value())) {
+ if ((prop = (*citer)->property (X_("name"))) != 0) {
+ ControlProtocolInfo* cpi = cpi_by_name (prop->value());
+ if (cpi) {
+ if (!(*citer)->children().empty()) {
+ cpi->state = (*citer)->children().front ();
+ } else {
+ cpi->state = 0;
+ }
+
+ if (_session) {
+ instantiate (*cpi);
+ } else {
+ cpi->requested = true;
+ }
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+XMLNode&
+ControlProtocolManager::get_state (void)
+{
+ XMLNode* root = new XMLNode (state_node_name);
+ Glib::Mutex::Lock lm (protocols_lock);
+
+ for (list<ControlProtocolInfo*>::iterator i = control_protocol_info.begin(); i != control_protocol_info.end(); ++i) {
+
+ XMLNode * child;
+
+ if ((*i)->protocol) {
+ child = &((*i)->protocol->get_state());
+ child->add_property (X_("active"), "yes");
+ // should we update (*i)->state here? probably.
+ root->add_child_nocopy (*child);
+ }
+ else if ((*i)->state) {
+ // keep ownership clear
+ root->add_child_copy (*(*i)->state);
+ }
+ else {
+ child = new XMLNode (X_("Protocol"));
+ child->add_property (X_("name"), (*i)->name);
+ child->add_property (X_("active"), "no");
+ root->add_child_nocopy (*child);
+ }
+ }
+
+ return *root;
+}
+
+void
+ControlProtocolManager::set_protocol_states (const XMLNode& node)
+{
+ XMLNodeList nlist;
+ XMLNodeConstIterator niter;
+ XMLProperty* prop;
+
+ nlist = node.children();
+
+ for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+
+ XMLNode* child = (*niter);
+
+ if ((prop = child->property ("name")) == 0) {
+ error << _("control protocol XML node has no name property. Ignored.") << endmsg;
+ continue;
+ }
+
+ ControlProtocolInfo* cpi = cpi_by_name (prop->value());
+
+ if (!cpi) {
+ warning << string_compose (_("control protocol \"%1\" is not known. Ignored"), prop->value()) << endmsg;
+ continue;
+ }
+
+ /* copy the node so that ownership is clear */
+
+ cpi->state = new XMLNode (*child);
+ }
+}
+
+ControlProtocolManager&
+ControlProtocolManager::instance ()
+{
+ if (_instance == 0) {
+ _instance = new ControlProtocolManager ();
+ }
+
+ return *_instance;
+}