cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[ardour.git] / libs / ardour / session_playlists.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19 #include <vector>
20
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"
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace PBD;
33 using namespace ARDOUR;
34
35 SessionPlaylists::~SessionPlaylists ()
36 {
37         DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
38         
39         for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
40                 SessionPlaylists::List::iterator tmp;
41
42                 tmp = i;
43                 ++tmp;
44
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 ();
48
49                 i = tmp;
50         }
51
52         DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
53         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
54                 List::iterator tmp;
55
56                 tmp = i;
57                 ++tmp;
58
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 ();
62
63                 i = tmp;
64         }
65
66         playlists.clear ();
67         unused_playlists.clear ();
68 }
69
70 bool
71 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
72 {
73         Glib::Mutex::Lock lm (lock);
74
75         bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
76
77         if (!existing) {
78                 playlists.insert (playlists.begin(), playlist);
79                 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
80         }
81
82         return existing;
83 }
84
85 void
86 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
87 {
88         Glib::Mutex::Lock lm (lock);
89
90         List::iterator i;
91
92         i = find (playlists.begin(), playlists.end(), playlist);
93         if (i != playlists.end()) {
94                 playlists.erase (i);
95         }
96
97         i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
98         if (i != unused_playlists.end()) {
99                 unused_playlists.erase (i);
100         }
101 }
102         
103
104 void
105 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
106 {
107         boost::shared_ptr<Playlist> pl(wpl.lock());
108
109         if (!pl) {
110                 return;
111         }
112
113         List::iterator x;
114
115         if (pl->hidden()) {
116                 /* its not supposed to be visible */
117                 return;
118         }
119
120         {
121                 Glib::Mutex::Lock lm (lock);
122
123                 if (!inuse) {
124
125                         unused_playlists.insert (pl);
126
127                         if ((x = playlists.find (pl)) != playlists.end()) {
128                                 playlists.erase (x);
129                         }
130
131
132                 } else {
133
134                         playlists.insert (pl);
135
136                         if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
137                                 unused_playlists.erase (x);
138                         }
139                 }
140         }
141 }
142
143 uint32_t
144 SessionPlaylists::n_playlists () const
145 {
146         Glib::Mutex::Lock lm (lock);
147         return playlists.size();
148 }
149
150 boost::shared_ptr<Playlist>
151 SessionPlaylists::by_name (string name)
152 {
153         Glib::Mutex::Lock lm (lock);
154
155         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
156                 if ((*i)->name() == name) {
157                         return* i;
158                 }
159         }
160         
161         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
162                 if ((*i)->name() == name) {
163                         return* i;
164                 }
165         }
166
167         return boost::shared_ptr<Playlist>();
168 }
169
170 void
171 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
172 {
173         Glib::Mutex::Lock lm (lock);
174
175         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
176                 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
177                         list.push_back (*i);
178                 }
179         }
180         
181         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
182                 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
183                         list.push_back (*i);
184                 }
185         }
186 }
187
188 void
189 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
190 {
191         Glib::Mutex::Lock lm (lock);
192
193         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
194                 s.push_back (*i);
195         }
196         
197         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
198                 s.push_back (*i);
199         }
200 }
201
202 void
203 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
204 {
205         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
206                 (*i)->get_region_list_equivalent_regions (region, result);
207 }
208
209 /** Return the number of playlists (not regions) that contain @a src */
210 uint32_t
211 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
212 {
213         uint32_t count = 0;
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)) {
218                                 ++count;
219                                 break;
220                         }
221                 }
222         }
223         return count;
224 }
225
226
227 void
228 SessionPlaylists::update_after_tempo_map_change ()
229 {
230         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
231                 (*i)->update_after_tempo_map_change ();
232         }
233
234         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
235                 (*i)->update_after_tempo_map_change ();
236         }
237 }
238
239 void
240 SessionPlaylists::add_state (XMLNode* node, bool full_state)
241 {
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()) {
246                                 if (full_state) {
247                                         child->add_child_nocopy ((*i)->get_state());
248                                 } else {
249                                         child->add_child_nocopy ((*i)->get_template());
250                                 }
251                         }
252                 }
253         }
254
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()) {
259                                 if (full_state) {
260                                         child->add_child_nocopy ((*i)->get_state());
261                                 } else {
262                                         child->add_child_nocopy ((*i)->get_template());
263                                 }
264                         }
265                 }
266         }
267 }
268
269 /** @return true for `stop cleanup', otherwise false */
270 bool
271 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
272 {
273         vector<boost::shared_ptr<Playlist> > playlists_tbd;
274
275         for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
276
277                 int status = ask (*x);
278
279                 switch (status) {
280                 case -1:
281                         return true;
282
283                 case 0:
284                         playlists_tbd.push_back (*x);
285                         break;
286
287                 default:
288                         /* leave it alone */
289                         break;
290                 }
291         }
292
293         /* now delete any that were marked for deletion */
294
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 ();
298         }
299
300         playlists_tbd.clear ();
301
302         return false;
303 }
304
305 int
306 SessionPlaylists::load (Session& session, const XMLNode& node)
307 {
308         XMLNodeList nlist;
309         XMLNodeConstIterator niter;
310         boost::shared_ptr<Playlist> playlist;
311
312         nlist = node.children();
313
314         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
315
316                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
317                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
318                 }
319         }
320
321         return 0;
322 }
323
324 int
325 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
326 {
327         XMLNodeList nlist;
328         XMLNodeConstIterator niter;
329         boost::shared_ptr<Playlist> playlist;
330
331         nlist = node.children();
332
333         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
334
335                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
336                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
337                         continue;
338                 }
339
340                 // now manually untrack it
341
342                 track (false, boost::weak_ptr<Playlist> (playlist));
343         }
344
345         return 0;
346 }
347
348 boost::shared_ptr<Playlist>
349 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
350 {
351         try {
352                 return PlaylistFactory::create (session, node);
353         }
354
355         catch (failed_constructor& err) {
356                 return boost::shared_ptr<Playlist>();
357         }
358 }
359