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 boost::shared_ptr<Playlist> keeper (*i);
47 (*i)->drop_references ();
52 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
53 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
59 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
60 boost::shared_ptr<Playlist> keeper (*i);
61 (*i)->drop_references ();
67 unused_playlists.clear ();
71 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
73 Glib::Mutex::Lock lm (lock);
75 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
78 playlists.insert (playlists.begin(), playlist);
79 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
86 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
88 Glib::Mutex::Lock lm (lock);
92 i = find (playlists.begin(), playlists.end(), playlist);
93 if (i != playlists.end()) {
97 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
98 if (i != unused_playlists.end()) {
99 unused_playlists.erase (i);
105 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
107 boost::shared_ptr<Playlist> pl(wpl.lock());
116 /* its not supposed to be visible */
121 Glib::Mutex::Lock lm (lock);
125 unused_playlists.insert (pl);
127 if ((x = playlists.find (pl)) != playlists.end()) {
134 playlists.insert (pl);
136 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
137 unused_playlists.erase (x);
144 SessionPlaylists::n_playlists () const
146 Glib::Mutex::Lock lm (lock);
147 return playlists.size();
150 boost::shared_ptr<Playlist>
151 SessionPlaylists::by_name (string name)
153 Glib::Mutex::Lock lm (lock);
155 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
156 if ((*i)->name() == name) {
161 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
162 if ((*i)->name() == name) {
167 return boost::shared_ptr<Playlist>();
171 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
173 Glib::Mutex::Lock lm (lock);
175 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
176 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
181 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
182 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
189 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
191 Glib::Mutex::Lock lm (lock);
193 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
197 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
203 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
205 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
206 (*i)->get_region_list_equivalent_regions (region, result);
209 /** Return the number of playlists (not regions) that contain @a src */
211 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
214 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
215 for (Playlist::RegionList::const_iterator r = (*p)->region_list().begin();
216 r != (*p)->region_list().end(); ++r) {
217 if ((*r)->uses_source(src)) {
228 SessionPlaylists::update_after_tempo_map_change ()
230 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
231 (*i)->update_after_tempo_map_change ();
234 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
235 (*i)->update_after_tempo_map_change ();
240 SessionPlaylists::add_state (XMLNode* node, bool full_state)
242 XMLNode* child = node->add_child ("Playlists");
243 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
244 if (!(*i)->hidden()) {
245 if (!(*i)->empty()) {
247 child->add_child_nocopy ((*i)->get_state());
249 child->add_child_nocopy ((*i)->get_template());
255 child = node->add_child ("UnusedPlaylists");
256 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
257 if (!(*i)->hidden()) {
258 if (!(*i)->empty()) {
260 child->add_child_nocopy ((*i)->get_state());
262 child->add_child_nocopy ((*i)->get_template());
269 /** @return true for `stop cleanup', otherwise false */
271 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
273 vector<boost::shared_ptr<Playlist> > playlists_tbd;
275 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
277 int status = ask (*x);
284 playlists_tbd.push_back (*x);
293 /* now delete any that were marked for deletion */
295 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
296 boost::shared_ptr<Playlist> keeper (*x);
297 (*x)->drop_references ();
300 playlists_tbd.clear ();
306 SessionPlaylists::load (Session& session, const XMLNode& node)
309 XMLNodeConstIterator niter;
310 boost::shared_ptr<Playlist> playlist;
312 nlist = node.children();
314 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
316 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
317 error << _("Session: cannot create Playlist from XML description.") << endmsg;
325 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
328 XMLNodeConstIterator niter;
329 boost::shared_ptr<Playlist> playlist;
331 nlist = node.children();
333 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
335 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
336 error << _("Session: cannot create Playlist from XML description.") << endmsg;
340 // now manually untrack it
342 track (false, boost::weak_ptr<Playlist> (playlist));
348 boost::shared_ptr<Playlist>
349 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
352 return PlaylistFactory::create (session, node);
355 catch (failed_constructor& err) {
356 return boost::shared_ptr<Playlist>();