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