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