*** NEW CODING POLICY ***
[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
34 using namespace PBD;
35 using namespace ARDOUR;
36
37 /**** Handler ***/
38 AudioPlaylistImportHandler::AudioPlaylistImportHandler (XMLTree const & source, Session & session, AudioRegionImportHandler & region_handler, const char * nodename) :
39   ElementImportHandler (source, session),
40   region_handler (region_handler)
41 {
42         XMLNode const * root = source.root();
43         XMLNode const * playlists;
44         
45         if (!(playlists = root->child (nodename))) {
46                 throw failed_constructor();
47         }
48         
49         XMLNodeList const & pl_children = playlists->children();
50         for (XMLNodeList::const_iterator it = pl_children.begin(); it != pl_children.end(); ++it) {
51                 const XMLProperty* type = (*it)->property("type");
52                 if ( !type || type->value() == "audio" ) {
53                         try {
54                                 elements.push_back (ElementPtr ( new AudioPlaylistImporter (source, session, *this, **it)));
55                         } catch (failed_constructor err) {
56                                 set_dirty();
57                         }
58                 }
59         }
60 }
61
62 string
63 AudioPlaylistImportHandler::get_info () const
64 {
65         return _("Audio Playlists");
66 }
67
68 void
69 AudioPlaylistImportHandler::get_regions (XMLNode const & node, ElementList & list) const
70 {
71         region_handler.create_regions_from_children (node, list);
72 }
73
74 void
75 AudioPlaylistImportHandler::update_region_id (XMLProperty* id_prop)
76 {
77         PBD::ID old_id (id_prop->value());
78         PBD::ID new_id (region_handler.get_new_id (old_id));
79         id_prop->set_value (new_id.to_s());
80 }
81
82 void
83 AudioPlaylistImportHandler::playlists_by_diskstream (PBD::ID const & id, PlaylistList & list) const
84 {
85         for (ElementList::const_iterator it = elements.begin(); it != elements.end(); ++it) {
86                 boost::shared_ptr<AudioPlaylistImporter> pl = boost::dynamic_pointer_cast<AudioPlaylistImporter> (*it);
87                 if (pl && pl->orig_diskstream() == id) {
88                         list.push_back (PlaylistPtr (new AudioPlaylistImporter (*pl)));
89                 }
90         }
91 }
92
93 /*** AudioPlaylistImporter ***/
94 AudioPlaylistImporter::AudioPlaylistImporter (XMLTree const & source, Session & session, AudioPlaylistImportHandler & handler, XMLNode const & node) : 
95   ElementImporter (source, session),
96   handler (handler),
97   orig_node (node),
98   xml_playlist (node),
99   diskstream_id ("0")
100 {
101         bool ds_ok = false;
102         
103         populate_region_list ();
104         
105         // Parse XML
106         XMLPropertyList const & props = xml_playlist.properties();
107         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
108                 string prop = (*it)->name();
109                 if (!prop.compare("type") || !prop.compare("frozen")) {
110                         // All ok
111                 } else if (!prop.compare("name")) {
112                         name = (*it)->value();
113                 } else if (!prop.compare("orig-diskstream-id")) {
114                         orig_diskstream_id = (*it)->value();
115                         ds_ok = true;
116                 } else {
117                         std::cerr << string_compose (X_("AudioPlaylistImporter did not recognise XML-property \"%1\""), prop) << endmsg;
118                 }
119         }
120         
121         if (!ds_ok) {
122                 error << string_compose (X_("AudioPlaylistImporter (%1): did not find XML-property \"orig_diskstream_id\" which is mandatory"), name) << endmsg;
123                 throw failed_constructor();
124         }
125 }
126
127 AudioPlaylistImporter::AudioPlaylistImporter (AudioPlaylistImporter const & other) :
128   ElementImporter (other.source, other.session),
129   handler (other.handler),
130   orig_node (other.orig_node),
131   xml_playlist (other.xml_playlist),
132   orig_diskstream_id (other.orig_diskstream_id)
133 {
134         populate_region_list ();
135 }
136
137 AudioPlaylistImporter::~AudioPlaylistImporter ()
138 {
139         
140 }
141
142 string
143 AudioPlaylistImporter::get_info () const
144 {
145         XMLNodeList children = xml_playlist.children();
146         unsigned int regions = 0;
147         std::ostringstream oss;
148         
149         for (XMLNodeIterator it = children.begin(); it != children.end(); it++) {
150                 if ((*it)->name() == "Region") {
151                         ++regions;
152                 }
153         }
154         
155         oss << regions << " ";
156         
157         if (regions == 1) {
158                 oss << _("region");
159         } else {
160                 oss << _("regions");
161         }
162         
163         return oss.str();
164 }
165
166 bool
167 AudioPlaylistImporter::_prepare_move ()
168 {
169         // Rename
170         while (session.playlist_by_name (name) || !handler.check_name (name)) {
171                 std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
172                 if (!rename_pair.first) {
173                         return false;
174                 }
175                 name = rename_pair.second;
176         }
177         xml_playlist.property ("name")->set_value (name);
178         handler.add_name (name);
179         
180         return true;
181 }
182
183 void
184 AudioPlaylistImporter::_cancel_move ()
185 {
186         handler.remove_name (name);
187 }
188
189 void
190 AudioPlaylistImporter::_move ()
191 {
192         boost::shared_ptr<Playlist> playlist;
193         
194         // Update diskstream id
195         xml_playlist.property ("orig-diskstream-id")->set_value (diskstream_id.to_s());
196         
197         // Update region XML in playlist and prepare sources
198         xml_playlist.remove_nodes("Region");
199         for (RegionList::iterator it = regions.begin(); it != regions.end(); ++it) {
200                 xml_playlist.add_child_copy ((*it)->get_xml());
201                 (*it)->add_sources_to_session();
202                 if ((*it)->broken()) {
203                         handler.set_dirty();
204                         set_broken();
205                         return; // TODO clean up?
206                 }
207         }
208         
209         // Update region ids in crossfades
210         XMLNodeList crossfades = xml_playlist.children("Crossfade");
211         for (XMLNodeIterator it = crossfades.begin(); it != crossfades.end(); ++it) {
212                 XMLProperty* in = (*it)->property("in");
213                 XMLProperty* out = (*it)->property("out");
214                 if (!in || !out) {
215                         error << string_compose (X_("AudioPlaylistImporter (%1): did not find the \"in\" or \"out\" property from a crossfade"), name) << endmsg;
216                 }
217                 
218                 handler.update_region_id (in);
219                 handler.update_region_id (out);
220                 
221                 // rate convert length and position
222                 XMLProperty* length = (*it)->property("length");
223                 if (length) {
224                         length->set_value (rate_convert_samples (length->value()));
225                 }
226                 
227                 XMLProperty* position = (*it)->property("position");
228                 if (position) {
229                         position->set_value (rate_convert_samples (position->value()));
230                 }
231         }
232         
233         // Create playlist
234         playlist = PlaylistFactory::create (session, xml_playlist, false, true);
235 }
236
237 void
238 AudioPlaylistImporter::set_diskstream (PBD::ID const & id)
239 {
240         diskstream_id = id;
241 }
242
243 void
244 AudioPlaylistImporter::populate_region_list ()
245 {
246         ElementImportHandler::ElementList elements;
247         handler.get_regions (orig_node, elements);
248         for (ElementImportHandler::ElementList::iterator it = elements.begin(); it != elements.end(); ++it) {
249                 regions.push_back (boost::dynamic_pointer_cast<AudioRegionImporter> (*it));
250         }
251 }
252