fix crash when copy'ing latent plugins
[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 "ardour/debug.h"
22 #include "ardour/playlist.h"
23 #include "ardour/playlist_factory.h"
24 #include "ardour/session_playlists.h"
25 #include "ardour/track.h"
26 #include "pbd/i18n.h"
27 #include "pbd/compose.h"
28 #include "pbd/xml++.h"
29
30 using namespace std;
31 using namespace PBD;
32 using namespace ARDOUR;
33
34 SessionPlaylists::~SessionPlaylists ()
35 {
36         DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
37
38         for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
39                 SessionPlaylists::List::iterator tmp;
40
41                 tmp = i;
42                 ++tmp;
43
44                 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
45                 boost::shared_ptr<Playlist> keeper (*i);
46                 (*i)->drop_references ();
47
48                 i = tmp;
49         }
50
51         DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
52         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
53                 List::iterator tmp;
54
55                 tmp = i;
56                 ++tmp;
57
58                 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
59                 boost::shared_ptr<Playlist> keeper (*i);
60                 (*i)->drop_references ();
61
62                 i = tmp;
63         }
64
65         playlists.clear ();
66         unused_playlists.clear ();
67 }
68
69 bool
70 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
71 {
72         Glib::Threads::Mutex::Lock lm (lock);
73
74         bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
75
76         if (!existing) {
77                 playlists.insert (playlists.begin(), playlist);
78                 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
79                 playlist->DropReferences.connect_same_thread (
80                         *this, boost::bind (&SessionPlaylists::remove_weak, this, boost::weak_ptr<Playlist> (playlist))
81                         );
82         }
83
84         return existing;
85 }
86
87 void
88 SessionPlaylists::remove_weak (boost::weak_ptr<Playlist> playlist)
89 {
90         boost::shared_ptr<Playlist> p = playlist.lock ();
91         if (p) {
92                 remove (p);
93         }
94 }
95
96 void
97 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
98 {
99         Glib::Threads::Mutex::Lock lm (lock);
100
101         List::iterator i;
102
103         i = find (playlists.begin(), playlists.end(), playlist);
104         if (i != playlists.end()) {
105                 playlists.erase (i);
106         }
107
108         i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
109         if (i != unused_playlists.end()) {
110                 unused_playlists.erase (i);
111         }
112 }
113
114
115 void
116 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
117 {
118         boost::shared_ptr<Playlist> pl(wpl.lock());
119
120         if (!pl) {
121                 return;
122         }
123
124         List::iterator x;
125
126         if (pl->hidden()) {
127                 /* its not supposed to be visible */
128                 return;
129         }
130
131         {
132                 Glib::Threads::Mutex::Lock lm (lock);
133
134                 if (!inuse) {
135
136                         unused_playlists.insert (pl);
137
138                         if ((x = playlists.find (pl)) != playlists.end()) {
139                                 playlists.erase (x);
140                         }
141
142
143                 } else {
144
145                         playlists.insert (pl);
146
147                         if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
148                                 unused_playlists.erase (x);
149                         }
150                 }
151         }
152 }
153
154 uint32_t
155 SessionPlaylists::n_playlists () const
156 {
157         Glib::Threads::Mutex::Lock lm (lock);
158         return playlists.size();
159 }
160
161 boost::shared_ptr<Playlist>
162 SessionPlaylists::by_name (string name)
163 {
164         Glib::Threads::Mutex::Lock lm (lock);
165
166         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
167                 if ((*i)->name() == name) {
168                         return* i;
169                 }
170         }
171
172         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
173                 if ((*i)->name() == name) {
174                         return* i;
175                 }
176         }
177
178         return boost::shared_ptr<Playlist>();
179 }
180
181 boost::shared_ptr<Playlist>
182 SessionPlaylists::by_id (const PBD::ID& id)
183 {
184         Glib::Threads::Mutex::Lock lm (lock);
185
186         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
187                 if ((*i)->id() == id) {
188                         return* i;
189                 }
190         }
191
192         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
193                 if ((*i)->id() == id) {
194                         return* i;
195                 }
196         }
197
198         return boost::shared_ptr<Playlist>();
199 }
200
201 void
202 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
203 {
204         Glib::Threads::Mutex::Lock lm (lock);
205
206         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
207                 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
208                         list.push_back (*i);
209                 }
210         }
211
212         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
213                 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
214                         list.push_back (*i);
215                 }
216         }
217 }
218
219 void
220 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
221 {
222         Glib::Threads::Mutex::Lock lm (lock);
223
224         for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
225                 s.push_back (*i);
226         }
227
228         for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
229                 s.push_back (*i);
230         }
231 }
232
233 void
234 SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
235 {
236         Glib::Threads::Mutex::Lock lm (lock);
237
238         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
239                 (*i)->destroy_region (r);
240         }
241
242         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
243                 (*i)->destroy_region (r);
244         }
245 }
246
247 void
248 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
249 {
250         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
251                 (*i)->get_region_list_equivalent_regions (region, result);
252 }
253
254 /** Return the number of playlists (not regions) that contain @a src
255  *  Important: this counts usage in both used and not-used playlists.
256  */
257 uint32_t
258 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
259 {
260         uint32_t count = 0;
261
262         /* XXXX this can go wildly wrong in the presence of circular references
263          * between compound regions.
264          */
265
266         for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
267                 if ((*p)->uses_source (src)) {
268                         ++count;
269                         break;
270                 }
271         }
272
273         for (List::const_iterator p = unused_playlists.begin(); p != unused_playlists.end(); ++p) {
274                 if ((*p)->uses_source (src)) {
275                         ++count;
276                         break;
277                 }
278         }
279
280         return count;
281 }
282
283 void
284 SessionPlaylists::sync_all_regions_with_regions ()
285 {
286         Glib::Threads::Mutex::Lock lm (lock);
287
288         for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
289                 (*p)->sync_all_regions_with_regions ();
290         }
291 }
292
293 void
294 SessionPlaylists::update_after_tempo_map_change ()
295 {
296         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
297                 (*i)->update_after_tempo_map_change ();
298         }
299
300         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
301                 (*i)->update_after_tempo_map_change ();
302         }
303 }
304
305 void
306 SessionPlaylists::add_state (XMLNode* node, bool full_state)
307 {
308         XMLNode* child = node->add_child ("Playlists");
309         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
310                 if (!(*i)->hidden()) {
311                         if (full_state) {
312                                 child->add_child_nocopy ((*i)->get_state());
313                         } else {
314                                 child->add_child_nocopy ((*i)->get_template());
315                         }
316                 }
317         }
318
319         child = node->add_child ("UnusedPlaylists");
320         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
321                 if (!(*i)->hidden()) {
322                         if (!(*i)->empty()) {
323                                 if (full_state) {
324                                         child->add_child_nocopy ((*i)->get_state());
325                                 } else {
326                                         child->add_child_nocopy ((*i)->get_template());
327                                 }
328                         }
329                 }
330         }
331 }
332
333 /** @return true for `stop cleanup', otherwise false */
334 bool
335 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
336 {
337         vector<boost::shared_ptr<Playlist> > playlists_tbd;
338
339         bool delete_remaining = false;
340         bool keep_remaining = false;
341
342         for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
343
344                 if (keep_remaining) {
345                         break;
346                 }
347
348                 if (delete_remaining) {
349                         playlists_tbd.push_back (*x);
350                         continue;
351                 }
352
353                 int status = ask (*x);
354
355                 switch (status) {
356                 case -1:
357                         // abort
358                         return true;
359
360                 case -2:
361                         // keep this and all later
362                         keep_remaining = true;
363                         break;
364
365                 case 2:
366                         // delete this and all later
367                         delete_remaining = true;
368                         // no break;
369
370                 case 1:
371                         // delete this
372                         playlists_tbd.push_back (*x);
373                         break;
374
375                 default:
376                         /* leave it alone */
377                         break;
378                 }
379         }
380
381         /* now delete any that were marked for deletion */
382
383         for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
384                 boost::shared_ptr<Playlist> keeper (*x);
385                 (*x)->drop_references ();
386         }
387
388         playlists_tbd.clear ();
389
390         return false;
391 }
392
393 int
394 SessionPlaylists::load (Session& session, const XMLNode& node)
395 {
396         XMLNodeList nlist;
397         XMLNodeConstIterator niter;
398         boost::shared_ptr<Playlist> playlist;
399
400         nlist = node.children();
401
402         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
403
404                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
405                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
406                 }
407         }
408
409         return 0;
410 }
411
412 int
413 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
414 {
415         XMLNodeList nlist;
416         XMLNodeConstIterator niter;
417         boost::shared_ptr<Playlist> playlist;
418
419         nlist = node.children();
420
421         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
422
423                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
424                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
425                         continue;
426                 }
427
428                 // now manually untrack it
429
430                 track (false, boost::weak_ptr<Playlist> (playlist));
431         }
432
433         return 0;
434 }
435
436 boost::shared_ptr<Playlist>
437 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
438 {
439         try {
440                 return PlaylistFactory::create (session, node);
441         }
442
443         catch (failed_constructor& err) {
444                 return boost::shared_ptr<Playlist>();
445         }
446 }
447
448 boost::shared_ptr<Crossfade>
449 SessionPlaylists::find_crossfade (const PBD::ID& id)
450 {
451         Glib::Threads::Mutex::Lock lm (lock);
452
453         boost::shared_ptr<Crossfade> c;
454
455         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
456                 c = (*i)->find_crossfade (id);
457                 if (c) {
458                         return c;
459                 }
460         }
461
462         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
463                 c = (*i)->find_crossfade (id);
464                 if (c) {
465                         return c;
466                 }
467         }
468
469         return boost::shared_ptr<Crossfade> ();
470 }
471
472 uint32_t
473 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
474 {
475         Glib::Threads::Mutex::Lock lm (lock);
476         uint32_t cnt = 0;
477
478         for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
479                 cnt += (*i)->region_use_count (region);
480         }
481
482         for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
483                 cnt += (*i)->region_use_count (region);
484         }
485
486         return cnt;
487 }
488
489 /** @return list of Playlists that are associated with a track */
490 vector<boost::shared_ptr<Playlist> >
491 SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
492 {
493         vector<boost::shared_ptr<Playlist> > pl;
494         get (pl);
495
496         vector<boost::shared_ptr<Playlist> > pl_tr;
497
498         for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
499                 if (((*i)->get_orig_track_id() == tr->id()) || (tr->playlist()->id() == (*i)->id())) {
500                         pl_tr.push_back (*i);
501                 }
502         }
503
504         return pl_tr;
505 }
506
507 void
508 SessionPlaylists::foreach (boost::function<void(boost::shared_ptr<const Playlist>)> functor)
509 {
510         Glib::Threads::Mutex::Lock lm (lock);
511         for (List::iterator i = playlists.begin(); i != playlists.end(); i++) {
512                 if (!(*i)->hidden()) {
513                         functor (*i);
514                 }
515         }
516         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); i++) {
517                 if (!(*i)->hidden()) {
518                         functor (*i);
519                 }
520         }
521 }