2 Copyright (C) 2009 Paul Davis
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.
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.
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.
21 #include "pbd/xml++.h"
22 #include "pbd/compose.h"
23 #include "ardour/debug.h"
24 #include "ardour/session_playlists.h"
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/playlist_factory.h"
28 #include "ardour/session.h"
33 using namespace ARDOUR;
35 SessionPlaylists::~SessionPlaylists ()
37 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
39 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
40 SessionPlaylists::List::iterator tmp;
45 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
46 (*i)->drop_references ();
51 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
52 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
58 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
59 (*i)->drop_references ();
65 unused_playlists.clear ();
69 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
71 Glib::Mutex::Lock lm (lock);
73 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
76 playlists.insert (playlists.begin(), playlist);
77 scoped_connect (playlist->InUse, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
84 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
86 Glib::Mutex::Lock lm (lock);
90 i = find (playlists.begin(), playlists.end(), playlist);
91 if (i != playlists.end()) {
95 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
96 if (i != unused_playlists.end()) {
97 unused_playlists.erase (i);
103 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
105 boost::shared_ptr<Playlist> pl(wpl.lock());
114 /* its not supposed to be visible */
119 Glib::Mutex::Lock lm (lock);
123 unused_playlists.insert (pl);
125 if ((x = playlists.find (pl)) != playlists.end()) {
132 playlists.insert (pl);
134 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
135 unused_playlists.erase (x);
142 SessionPlaylists::n_playlists () const
144 Glib::Mutex::Lock lm (lock);
145 return playlists.size();
148 boost::shared_ptr<Playlist>
149 SessionPlaylists::by_name (string name)
151 Glib::Mutex::Lock lm (lock);
153 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
154 if ((*i)->name() == name) {
159 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
160 if ((*i)->name() == name) {
165 return boost::shared_ptr<Playlist>();
169 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
171 Glib::Mutex::Lock lm (lock);
173 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
174 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
179 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
180 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
187 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
189 Glib::Mutex::Lock lm (lock);
191 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
195 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
201 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
203 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
204 (*i)->get_region_list_equivalent_regions (region, result);
207 /** Return the number of playlists (not regions) that contain @a src */
209 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
212 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
213 for (Playlist::RegionList::const_iterator r = (*p)->region_list().begin();
214 r != (*p)->region_list().end(); ++r) {
215 if ((*r)->uses_source(src)) {
226 SessionPlaylists::update_after_tempo_map_change ()
228 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
229 (*i)->update_after_tempo_map_change ();
232 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
233 (*i)->update_after_tempo_map_change ();
238 SessionPlaylists::add_state (XMLNode* node, bool full_state)
240 XMLNode* child = node->add_child ("Playlists");
241 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
242 if (!(*i)->hidden()) {
243 if (!(*i)->empty()) {
245 child->add_child_nocopy ((*i)->get_state());
247 child->add_child_nocopy ((*i)->get_template());
253 child = node->add_child ("UnusedPlaylists");
254 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
255 if (!(*i)->hidden()) {
256 if (!(*i)->empty()) {
258 child->add_child_nocopy ((*i)->get_state());
260 child->add_child_nocopy ((*i)->get_template());
267 /** @return true for `stop cleanup', otherwise false */
269 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
271 vector<boost::shared_ptr<Playlist> > playlists_tbd;
273 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
275 int status = ask (*x);
282 playlists_tbd.push_back (*x);
291 /* now delete any that were marked for deletion */
293 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
294 (*x)->drop_references ();
297 playlists_tbd.clear ();
303 SessionPlaylists::load (Session& session, const XMLNode& node)
306 XMLNodeConstIterator niter;
307 boost::shared_ptr<Playlist> playlist;
309 nlist = node.children();
311 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
313 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
314 error << _("Session: cannot create Playlist from XML description.") << endmsg;
322 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
325 XMLNodeConstIterator niter;
326 boost::shared_ptr<Playlist> playlist;
328 nlist = node.children();
330 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
332 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
333 error << _("Session: cannot create Playlist from XML description.") << endmsg;
337 // now manually untrack it
339 track (false, boost::weak_ptr<Playlist> (playlist));
345 boost::shared_ptr<Playlist>
346 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
349 return PlaylistFactory::create (session, node);
352 catch (failed_constructor& err) {
353 return boost::shared_ptr<Playlist>();