fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / session_playlists.cc
index ac3baf20081ae3f9faf0a1fd70af2689a68a52be..95982b6158932bfc0f0c842f7af7d79cebc2ebef 100644 (file)
@@ -23,7 +23,7 @@
 #include "ardour/playlist_factory.h"
 #include "ardour/session_playlists.h"
 #include "ardour/track.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include "pbd/compose.h"
 #include "pbd/xml++.h"
 
@@ -221,11 +221,11 @@ SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
 {
        Glib::Threads::Mutex::Lock lm (lock);
 
-       for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
                s.push_back (*i);
        }
 
-       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+       for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
                s.push_back (*i);
        }
 }
@@ -244,7 +244,6 @@ SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
        }
 }
 
-
 void
 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
 {
@@ -252,18 +251,32 @@ SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> re
                (*i)->get_region_list_equivalent_regions (region, result);
 }
 
-/** Return the number of playlists (not regions) that contain @a src */
+/** Return the number of playlists (not regions) that contain @a src
+ *  Important: this counts usage in both used and not-used playlists.
+ */
 uint32_t
 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
 {
        uint32_t count = 0;
 
+       /* XXXX this can go wildly wrong in the presence of circular references
+        * between compound regions.
+        */
+
        for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
                 if ((*p)->uses_source (src)) {
                         ++count;
                         break;
                 }
        }
+
+       for (List::const_iterator p = unused_playlists.begin(); p != unused_playlists.end(); ++p) {
+                if ((*p)->uses_source (src)) {
+                        ++count;
+                        break;
+                }
+       }
+
        return count;
 }
 
@@ -323,15 +336,39 @@ SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Pla
 {
        vector<boost::shared_ptr<Playlist> > playlists_tbd;
 
+       bool delete_remaining = false;
+       bool keep_remaining = false;
+
        for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
 
+               if (keep_remaining) {
+                       break;
+               }
+
+               if (delete_remaining) {
+                       playlists_tbd.push_back (*x);
+                       continue;
+               }
+
                int status = ask (*x);
 
                switch (status) {
                case -1:
+                       // abort
                        return true;
 
-               case 0:
+               case -2:
+                       // keep this and all later
+                       keep_remaining = true;
+                       break;
+
+               case 2:
+                       // delete this and all later
+                       delete_remaining = true;
+                       // no break;
+
+               case 1:
+                       // delete this
                        playlists_tbd.push_back (*x);
                        break;
 
@@ -438,11 +475,11 @@ SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
        Glib::Threads::Mutex::Lock lm (lock);
         uint32_t cnt = 0;
 
-       for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
                 cnt += (*i)->region_use_count (region);
        }
 
-       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+       for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
                 cnt += (*i)->region_use_count (region);
        }
 
@@ -455,7 +492,7 @@ SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
 {
        vector<boost::shared_ptr<Playlist> > pl;
        get (pl);
-       
+
        vector<boost::shared_ptr<Playlist> > pl_tr;
 
        for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
@@ -466,3 +503,19 @@ SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
 
        return pl_tr;
 }
+
+void
+SessionPlaylists::foreach (boost::function<void(boost::shared_ptr<const Playlist>)> functor)
+{
+       Glib::Threads::Mutex::Lock lm (lock);
+       for (List::iterator i = playlists.begin(); i != playlists.end(); i++) {
+               if (!(*i)->hidden()) {
+                       functor (*i);
+               }
+       }
+       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); i++) {
+               if (!(*i)->hidden()) {
+                       functor (*i);
+               }
+       }
+}