Use a shared_ptr for SessionPlaylists so that it can be explicitly destroyed in ...
[ardour.git] / libs / ardour / audio_playlist_importer.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/audio_playlist_importer.h"
22
23 #include <sstream>
24
25 #include "pbd/failed_constructor.h"
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28
29 #include "ardour/audio_region_importer.h"
30 #include "ardour/session.h"
31 #include "ardour/playlist.h"
32 #include "ardour/playlist_factory.h"
33 #include "ardour/session_playlists.h"
34
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
38
39 /**** Handler ***/
40 AudioPlaylistImportHandler::AudioPlaylistImportHandler (XMLTree const & source, Session & session, AudioRegionImportHandler & region_handler, const char * nodename) :
41   ElementImportHandler (source, session),
42   region_handler (region_handler)
43 {
44         XMLNode const * root = source.root();
45         XMLNode const * playlists;
46
47         if (!(playlists = root->child (nodename))) {
48                 throw failed_constructor();
49         }
50
51         XMLNodeList const & pl_children = playlists->children();
52         for (XMLNodeList::const_iterator it = pl_children.begin(); it != pl_children.end(); ++it) {
53                 const XMLProperty* type = (*it)->property("type");
54                 if ( !type || type->value() == "audio" ) {
55                         try {
56                                 elements.push_back (ElementPtr ( new AudioPlaylistImporter (source, session, *this, **it)));
57                         } catch (failed_constructor err) {
58                                 set_dirty();
59                         }
60                 }
61         }
62 }
63
64 string
65 AudioPlaylistImportHandler::get_info () const
66 {
67         return _("Audio Playlists");
68 }
69
70 void
71 AudioPlaylistImportHandler::get_regions (XMLNode const & node, ElementList & list) const
72 {
73         region_handler.create_regions_from_children (node, list);
74 }
75
76 void
77 AudioPlaylistImportHandler::update_region_id (XMLProperty* id_prop)
78 {
79         PBD::ID old_id (id_prop->value());
80         PBD::ID new_id (region_handler.get_new_id (old_id));
81         id_prop->set_value (new_id.to_s());
82 }
83
84 void
85 AudioPlaylistImportHandler::playlists_by_diskstream (PBD::ID const & id, PlaylistList & list) const
86 {
87         for (ElementList::const_iterator it = elements.begin(); it != elements.end(); ++it) {
88                 boost::shared_ptr<AudioPlaylistImporter> pl = boost::dynamic_pointer_cast<AudioPlaylistImporter> (*it);
89                 if (pl && pl->orig_diskstream() == id) {
90                         list.push_back (PlaylistPtr (new AudioPlaylistImporter (*pl)));
91                 }
92         }
93 }
94
95 /*** AudioPlaylistImporter ***/
96 AudioPlaylistImporter::AudioPlaylistImporter (XMLTree const & source, Session & session, AudioPlaylistImportHandler & handler, XMLNode const & node) :
97   ElementImporter (source, session),
98   handler (handler),
99   orig_node (node),
100   xml_playlist (node),
101   diskstream_id ("0")
102 {
103         bool ds_ok = false;
104
105         populate_region_list ();
106
107         // Parse XML
108         XMLPropertyList const & props = xml_playlist.properties();
109         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
110                 string prop = (*it)->name();
111                 if (!prop.compare("type") || !prop.compare("frozen")) {
112                         // All ok
113                 } else if (!prop.compare("name")) {
114                         name = (*it)->value();
115                 } else if (!prop.compare("orig-diskstream-id")) {
116                         orig_diskstream_id = (*it)->value();
117                         ds_ok = true;
118                 } else {
119                         std::cerr << string_compose (X_("AudioPlaylistImporter did not recognise XML-property \"%1\""), prop) << endmsg;
120                 }
121         }
122
123         if (!ds_ok) {
124                 error << string_compose (X_("AudioPlaylistImporter (%1): did not find XML-property \"orig_diskstream_id\" which is mandatory"), name) << endmsg;
125                 throw failed_constructor();
126         }
127 }
128
129 AudioPlaylistImporter::AudioPlaylistImporter (AudioPlaylistImporter const & other) :
130   ElementImporter (other.source, other.session),
131   handler (other.handler),
132   orig_node (other.orig_node),
133   xml_playlist (other.xml_playlist),
134   orig_diskstream_id (other.orig_diskstream_id)
135 {
136         populate_region_list ();
137 }
138
139 AudioPlaylistImporter::~AudioPlaylistImporter ()
140 {
141
142 }
143
144 string
145 AudioPlaylistImporter::get_info () const
146 {
147         XMLNodeList children = xml_playlist.children();
148         unsigned int regions = 0;
149         std::ostringstream oss;
150
151         for (XMLNodeIterator it = children.begin(); it != children.end(); it++) {
152                 if ((*it)->name() == "Region") {
153                         ++regions;
154                 }
155         }
156
157         oss << regions << " ";
158
159         if (regions == 1) {
160                 oss << _("region");
161         } else {
162                 oss << _("regions");
163         }
164
165         return oss.str();
166 }
167
168 bool
169 AudioPlaylistImporter::_prepare_move ()
170 {
171         // Rename
172         while (session.playlists->by_name (name) || !handler.check_name (name)) {
173                 std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
174                 if (!rename_pair.first) {
175                         return false;
176                 }
177                 name = rename_pair.second;
178         }
179         xml_playlist.property ("name")->set_value (name);
180         handler.add_name (name);
181
182         return true;
183 }
184
185 void
186 AudioPlaylistImporter::_cancel_move ()
187 {
188         handler.remove_name (name);
189 }
190
191 void
192 AudioPlaylistImporter::_move ()
193 {
194         boost::shared_ptr<Playlist> playlist;
195
196         // Update diskstream id
197         xml_playlist.property ("orig-diskstream-id")->set_value (diskstream_id.to_s());
198
199         // Update region XML in playlist and prepare sources
200         xml_playlist.remove_nodes("Region");
201         for (RegionList::iterator it = regions.begin(); it != regions.end(); ++it) {
202                 xml_playlist.add_child_copy ((*it)->get_xml());
203                 (*it)->add_sources_to_session();
204                 if ((*it)->broken()) {
205                         handler.set_dirty();
206                         set_broken();
207                         return; // TODO clean up?
208                 }
209         }
210
211         // Update region ids in crossfades
212         XMLNodeList crossfades = xml_playlist.children("Crossfade");
213         for (XMLNodeIterator it = crossfades.begin(); it != crossfades.end(); ++it) {
214                 XMLProperty* in = (*it)->property("in");
215                 XMLProperty* out = (*it)->property("out");
216                 if (!in || !out) {
217                         error << string_compose (X_("AudioPlaylistImporter (%1): did not find the \"in\" or \"out\" property from a crossfade"), name) << endmsg;
218                 }
219
220                 handler.update_region_id (in);
221                 handler.update_region_id (out);
222
223                 // rate convert length and position
224                 XMLProperty* length = (*it)->property("length");
225                 if (length) {
226                         length->set_value (rate_convert_samples (length->value()));
227                 }
228
229                 XMLProperty* position = (*it)->property("position");
230                 if (position) {
231                         position->set_value (rate_convert_samples (position->value()));
232                 }
233         }
234
235         // Create playlist
236         playlist = PlaylistFactory::create (session, xml_playlist, false, true);
237 }
238
239 void
240 AudioPlaylistImporter::set_diskstream (PBD::ID const & id)
241 {
242         diskstream_id = id;
243 }
244
245 void
246 AudioPlaylistImporter::populate_region_list ()
247 {
248         ElementImportHandler::ElementList elements;
249         handler.get_regions (orig_node, elements);
250         for (ElementImportHandler::ElementList::iterator it = elements.begin(); it != elements.end(); ++it) {
251                 regions.push_back (boost::dynamic_pointer_cast<AudioRegionImporter> (*it));
252         }
253 }
254