Wrong iterator used with IDSortedList
[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 namespace {
306 struct id_compare
307 {
308         bool operator()(const boost::shared_ptr<Playlist>& p1, const boost::shared_ptr<Playlist>& p2)
309         {
310                 return p1->id () < p2->id ();
311         }
312 };
313
314 typedef std::set<boost::shared_ptr<Playlist> > List;
315 typedef std::set<boost::shared_ptr<Playlist>, id_compare> IDSortedList;
316
317 static void
318 get_id_sorted_playlists (const List& playlists, IDSortedList& id_sorted_playlists)
319 {
320         for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
321                 id_sorted_playlists.insert(*i);
322         }
323 }
324
325 } // anonymous namespace
326
327 void
328 SessionPlaylists::add_state (XMLNode* node, bool full_state)
329 {
330         XMLNode* child = node->add_child ("Playlists");
331
332         IDSortedList id_sorted_playlists;
333         get_id_sorted_playlists (playlists, id_sorted_playlists);
334
335         for (IDSortedList::iterator i = id_sorted_playlists.begin (); i != id_sorted_playlists.end (); ++i) {
336                 if (!(*i)->hidden ()) {
337                         if (full_state) {
338                                 child->add_child_nocopy ((*i)->get_state ());
339                         } else {
340                                 child->add_child_nocopy ((*i)->get_template ());
341                         }
342                 }
343         }
344
345         child = node->add_child ("UnusedPlaylists");
346
347         IDSortedList id_sorted_unused_playlists;
348         get_id_sorted_playlists (unused_playlists, id_sorted_unused_playlists);
349
350         for (IDSortedList::iterator i = id_sorted_unused_playlists.begin ();
351              i != id_sorted_unused_playlists.end (); ++i) {
352                 if (!(*i)->hidden()) {
353                         if (!(*i)->empty()) {
354                                 if (full_state) {
355                                         child->add_child_nocopy ((*i)->get_state());
356                                 } else {
357                                         child->add_child_nocopy ((*i)->get_template());
358                                 }
359                         }
360                 }
361         }
362 }
363
364 /** @return true for `stop cleanup', otherwise false */
365 bool
366 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
367 {
368         vector<boost::shared_ptr<Playlist> > playlists_tbd;
369
370         bool delete_remaining = false;
371         bool keep_remaining = false;
372
373         for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
374
375                 if (keep_remaining) {
376                         break;
377                 }
378
379                 if (delete_remaining) {
380                         playlists_tbd.push_back (*x);
381                         continue;
382                 }
383
384                 int status = ask (*x);
385
386                 switch (status) {
387                 case -1:
388                         // abort
389                         return true;
390
391                 case -2:
392                         // keep this and all later
393                         keep_remaining = true;
394                         break;
395
396                 case 2:
397                         // delete this and all later
398                         delete_remaining = true;
399                         // no break;
400
401                 case 1:
402                         // delete this
403                         playlists_tbd.push_back (*x);
404                         break;
405
406                 default:
407                         /* leave it alone */
408                         break;
409                 }
410         }
411
412         /* now delete any that were marked for deletion */
413
414         for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
415                 boost::shared_ptr<Playlist> keeper (*x);
416                 (*x)->drop_references ();
417         }
418
419         playlists_tbd.clear ();
420
421         return false;
422 }
423
424 int
425 SessionPlaylists::load (Session& session, const XMLNode& node)
426 {
427         XMLNodeList nlist;
428         XMLNodeConstIterator niter;
429         boost::shared_ptr<Playlist> playlist;
430
431         nlist = node.children();
432
433         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
434
435                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
436                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
437                 }
438         }
439
440         return 0;
441 }
442
443 int
444 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
445 {
446         XMLNodeList nlist;
447         XMLNodeConstIterator niter;
448         boost::shared_ptr<Playlist> playlist;
449
450         nlist = node.children();
451
452         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
453
454                 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
455                         error << _("Session: cannot create Playlist from XML description.") << endmsg;
456                         continue;
457                 }
458
459                 // now manually untrack it
460
461                 track (false, boost::weak_ptr<Playlist> (playlist));
462         }
463
464         return 0;
465 }
466
467 boost::shared_ptr<Playlist>
468 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
469 {
470         try {
471                 return PlaylistFactory::create (session, node);
472         }
473
474         catch (failed_constructor& err) {
475                 return boost::shared_ptr<Playlist>();
476         }
477 }
478
479 boost::shared_ptr<Crossfade>
480 SessionPlaylists::find_crossfade (const PBD::ID& id)
481 {
482         Glib::Threads::Mutex::Lock lm (lock);
483
484         boost::shared_ptr<Crossfade> c;
485
486         for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
487                 c = (*i)->find_crossfade (id);
488                 if (c) {
489                         return c;
490                 }
491         }
492
493         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
494                 c = (*i)->find_crossfade (id);
495                 if (c) {
496                         return c;
497                 }
498         }
499
500         return boost::shared_ptr<Crossfade> ();
501 }
502
503 uint32_t
504 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
505 {
506         Glib::Threads::Mutex::Lock lm (lock);
507         uint32_t cnt = 0;
508
509         for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
510                 cnt += (*i)->region_use_count (region);
511         }
512
513         for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
514                 cnt += (*i)->region_use_count (region);
515         }
516
517         return cnt;
518 }
519
520 /** @return list of Playlists that are associated with a track */
521 vector<boost::shared_ptr<Playlist> >
522 SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
523 {
524         vector<boost::shared_ptr<Playlist> > pl;
525         get (pl);
526
527         vector<boost::shared_ptr<Playlist> > pl_tr;
528
529         for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
530                 if (((*i)->get_orig_track_id() == tr->id()) || (tr->playlist()->id() == (*i)->id())) {
531                         pl_tr.push_back (*i);
532                 }
533         }
534
535         return pl_tr;
536 }
537
538 void
539 SessionPlaylists::foreach (boost::function<void(boost::shared_ptr<const Playlist>)> functor, bool incl_unused)
540 {
541         Glib::Threads::Mutex::Lock lm (lock);
542         for (List::iterator i = playlists.begin(); i != playlists.end(); i++) {
543                 if (!(*i)->hidden()) {
544                         functor (*i);
545                 }
546         }
547         if (!incl_unused) {
548                 return;
549         }
550         for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); i++) {
551                 if (!(*i)->hidden()) {
552                         functor (*i);
553                 }
554         }
555 }