+ for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+ if (!extra_system[*i].empty()) {
+ boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
+ system->add_bundle (b);
+ }
+ }
+
+ for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+ if (!extra_other[*i].empty()) {
+ boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
+ other->add_bundle (b);
+ }
+ }
+
+ if (!allow_dups) {
+ system->remove_duplicates ();
+ }
+
+ add_group_if_not_empty (other);
+ if (type != DataType::MIDI) {
+ add_group_if_not_empty (bus);
+ }
+ add_group_if_not_empty (track);
+ add_group_if_not_empty (ardour);
+ add_group_if_not_empty (system);
+
+ emit_changed ();
+}
+
+boost::shared_ptr<Bundle>
+PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs) const
+{
+ boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
+
+ std::string const pre = common_prefix (p);
+ if (!pre.empty()) {
+ b->set_name (pre.substr (0, pre.length() - 1));
+ }
+
+ for (uint32_t j = 0; j < p.size(); ++j) {
+ b->add_channel (p[j].substr (pre.length()), type);
+ b->set_port (j, p[j]);
+ }
+
+ return b;
+}
+
+bool
+PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
+{
+ return n.substr (0, p.length()) == p;
+}
+
+std::string
+PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
+{
+ /* we must have some strings and the first must contain the separator string */
+ if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
+ return "";
+ }
+
+ /* prefix of the first string */
+ std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
+
+ /* see if the other strings also start with fp */
+ uint32_t j = 1;
+ while (j < p.size()) {
+ if (p[j].substr (0, fp.length()) != fp) {
+ break;
+ }
+ ++j;
+ }
+
+ if (j != p.size()) {
+ return "";
+ }
+
+ return fp;
+}
+
+
+std::string
+PortGroupList::common_prefix (std::vector<std::string> const & p) const
+{
+ /* common prefix before '/' ? */
+ std::string cp = common_prefix_before (p, "/");
+ if (!cp.empty()) {
+ return cp;
+ }
+
+ cp = common_prefix_before (p, ":");
+ if (!cp.empty()) {
+ return cp;
+ }
+
+ return "";
+}
+
+void
+PortGroupList::clear ()
+{
+ _groups.clear ();
+ _bundle_changed_connections.drop_connections ();
+ emit_changed ();
+}
+
+
+PortGroup::BundleList const &
+PortGroupList::bundles () const
+{
+ _bundles.clear ();
+
+ for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
+ std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
+ }
+
+ return _bundles;
+}
+
+ChanCount
+PortGroupList::total_channels () const
+{
+ ChanCount n;
+
+ for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
+ n += (*i)->total_channels ();
+ }
+
+ return n;
+}
+
+void
+PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
+{
+ if (!g->bundles().empty ()) {
+ add_group (g);
+ }
+}
+
+void
+PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
+{
+ _groups.push_back (g);
+
+ g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
+ g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
+
+ emit_changed ();