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.
20 #include "pbd/xml++.h"
21 #include "pbd/compose.h"
22 #include "ardour/debug.h"
23 #include "ardour/session_playlists.h"
24 #include "ardour/playlist.h"
25 #include "ardour/region.h"
26 #include "ardour/playlist_factory.h"
27 #include "ardour/session.h"
32 using namespace ARDOUR;
34 SessionPlaylists::~SessionPlaylists ()
36 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
38 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
39 SessionPlaylists::List::iterator tmp;
44 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
45 (*i)->drop_references ();
50 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
51 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
57 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
58 (*i)->drop_references ();
64 unused_playlists.clear ();
68 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
70 Glib::Mutex::Lock lm (lock);
72 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
75 playlists.insert (playlists.begin(), playlist);
76 playlist->InUse.connect (sigc::bind (mem_fun (*this, &SessionPlaylists::track), boost::weak_ptr<Playlist>(playlist)));
83 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
85 Glib::Mutex::Lock lm (lock);
89 i = find (playlists.begin(), playlists.end(), playlist);
90 if (i != playlists.end()) {
94 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
95 if (i != unused_playlists.end()) {
96 unused_playlists.erase (i);
102 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
104 boost::shared_ptr<Playlist> pl(wpl.lock());
113 /* its not supposed to be visible */
118 Glib::Mutex::Lock lm (lock);
122 unused_playlists.insert (pl);
124 if ((x = playlists.find (pl)) != playlists.end()) {
131 playlists.insert (pl);
133 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
134 unused_playlists.erase (x);
141 SessionPlaylists::n_playlists () const
143 Glib::Mutex::Lock lm (lock);
144 return playlists.size();
147 boost::shared_ptr<Playlist>
148 SessionPlaylists::by_name (string name)
150 Glib::Mutex::Lock lm (lock);
152 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
153 if ((*i)->name() == name) {
158 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
159 if ((*i)->name() == name) {
164 return boost::shared_ptr<Playlist>();
168 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
170 Glib::Mutex::Lock lm (lock);
172 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
173 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
178 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
179 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
186 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
188 Glib::Mutex::Lock lm (lock);
190 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
194 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
200 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
202 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
203 (*i)->get_region_list_equivalent_regions (region, result);
206 /** Return the number of playlists (not regions) that contain @a src */
208 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
211 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
212 for (Playlist::RegionList::const_iterator r = (*p)->region_list().begin();
213 r != (*p)->region_list().end(); ++r) {
214 if ((*r)->uses_source(src)) {
225 SessionPlaylists::update_after_tempo_map_change ()
227 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
228 (*i)->update_after_tempo_map_change ();
231 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
232 (*i)->update_after_tempo_map_change ();
237 SessionPlaylists::add_state (XMLNode* node, bool full_state)
239 XMLNode* child = node->add_child ("Playlists");
240 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
241 if (!(*i)->hidden()) {
242 if (!(*i)->empty()) {
244 child->add_child_nocopy ((*i)->get_state());
246 child->add_child_nocopy ((*i)->get_template());
252 child = node->add_child ("UnusedPlaylists");
253 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
254 if (!(*i)->hidden()) {
255 if (!(*i)->empty()) {
257 child->add_child_nocopy ((*i)->get_state());
259 child->add_child_nocopy ((*i)->get_template());
266 /** @return true for `stop cleanup', otherwise false */
268 SessionPlaylists::maybe_delete_unused (sigc::signal<int, boost::shared_ptr<Playlist> > ask)
270 vector<boost::shared_ptr<Playlist> > playlists_tbd;
272 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
274 int status = ask (*x);
281 playlists_tbd.push_back (*x);
290 /* now delete any that were marked for deletion */
292 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
293 (*x)->drop_references ();
296 playlists_tbd.clear ();
302 SessionPlaylists::load (Session& session, const XMLNode& node)
305 XMLNodeConstIterator niter;
306 boost::shared_ptr<Playlist> playlist;
308 nlist = node.children();
310 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
312 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
313 error << _("Session: cannot create Playlist from XML description.") << endmsg;
321 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
324 XMLNodeConstIterator niter;
325 boost::shared_ptr<Playlist> playlist;
327 nlist = node.children();
329 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
331 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
332 error << _("Session: cannot create Playlist from XML description.") << endmsg;
336 // now manually untrack it
338 track (false, boost::weak_ptr<Playlist> (playlist));
344 boost::shared_ptr<Playlist>
345 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
348 return PlaylistFactory::create (session, node);
351 catch (failed_constructor& err) {
352 return boost::shared_ptr<Playlist>();