+ region_list_connections.drop_connections ();
+
+ {
+ Glib::Mutex::Lock lm (region_map_lock);
+ region_map.clear ();
+ }
+
+}
+
+void
+RegionFactory::delete_all_regions ()
+{
+ RegionMap copy;
+
+ /* copy region list */
+ {
+ Glib::Mutex::Lock lm (region_map_lock);
+ copy = region_map;
+ }
+
+ /* clear existing map */
+ clear_map ();
+
+ /* tell everyone to drop references */
+ for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
+ i->second->drop_references ();
+ }
+
+ /* the copy should now hold the only references, which will
+ vanish as we leave this scope, thus calling all destructors.
+ */
+}
+
+uint32_t
+RegionFactory::nregions ()
+{
+ Glib::Mutex::Lock lm (region_map_lock);
+ return region_map.size ();
+}
+
+void
+RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
+{
+ string::size_type const last_period = region->name().find_last_of ('.');
+
+ if (last_period != string::npos && last_period < region->name().length() - 1) {
+
+ string const base = region->name().substr (0, last_period);
+ string const number = region->name().substr (last_period + 1);
+
+ /* note that if there is no number, we get zero from atoi,
+ which is just fine
+ */
+
+ Glib::Mutex::Lock lm (region_name_map_lock);
+ region_name_map[base] = atoi (number.c_str ());
+ }
+}
+
+void
+RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
+{
+ boost::shared_ptr<Region> r = w.lock ();
+ if (!r) {
+ return;
+ }
+
+ if (what_changed.contains (Properties::name)) {
+ update_region_name_map (r);
+ }
+}
+
+int
+RegionFactory::region_name (string& result, string base, bool newlevel)
+{
+ char buf[16];
+ string subbase;
+
+ if (base.find("/") != string::npos) {
+ base = base.substr(base.find_last_of("/") + 1);
+ }
+
+ if (base == "") {
+
+ snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
+ result = "region.";
+ result += buf;
+
+ } else {
+
+ if (newlevel) {
+ subbase = base;
+ } else {
+ string::size_type pos;
+
+ pos = base.find_last_of ('.');
+
+ /* pos may be npos, but then we just use entire base */
+
+ subbase = base.substr (0, pos);
+
+ }
+
+ {
+ Glib::Mutex::Lock lm (region_name_map_lock);
+
+ map<string,uint32_t>::iterator x;
+
+ result = subbase;
+
+ if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
+ result += ".1";
+ region_name_map[subbase] = 1;
+ } else {
+ x->second++;
+ snprintf (buf, sizeof (buf), ".%d", x->second);
+
+ result += buf;
+ }
+ }
+ }
+
+ return 0;
+}
+
+string
+RegionFactory::new_region_name (string old)
+{
+ string::size_type last_period;
+ uint32_t number;
+ string::size_type len = old.length() + 64;
+ char buf[len];
+
+ if ((last_period = old.find_last_of ('.')) == string::npos) {
+
+ /* no period present - add one explicitly */
+
+ old += '.';
+ last_period = old.length() - 1;
+ number = 0;
+
+ } else {
+
+ number = atoi (old.substr (last_period+1).c_str());
+
+ }
+
+ while (number < (UINT_MAX-1)) {
+
+ const RegionMap& regions (RegionFactory::regions());
+ RegionMap::const_iterator i;
+ string sbuf;
+
+ number++;
+
+ snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
+ sbuf = buf;
+
+ for (i = regions.begin(); i != regions.end(); ++i) {
+ if (i->second->name() == sbuf) {
+ break;
+ }
+ }
+
+ if (i == regions.end()) {
+ break;
+ }
+ }
+
+ if (number != (UINT_MAX-1)) {
+ return buf;
+ }
+
+ error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
+ return old;
+}
+
+void
+RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
+{
+ Glib::Mutex::Lock lm (region_map_lock);
+
+ for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+ if (i->second->uses_source (s)) {
+ r.insert (i->second);
+ }
+ }