Templateify MidiBuffer iterators (avoid code duplication since they're about to get...
[ardour.git] / libs / ardour / audio_track_importer.cc
index 016f133bb00b3211c19f45e15d7e808840ecbf76..32b90e00dc8710959979e4dfb348e997874d286a 100644 (file)
 #include <ardour/audio_track_importer.h>
 
 #include <ardour/audio_playlist_importer.h>
+#include <ardour/audio_diskstream.h>
 #include <ardour/session.h>
 
 #include <pbd/failed_constructor.h>
 #include <pbd/convert.h>
 
 #include <sstream>
+#include <algorithm>
 
 #include "i18n.h"
 
@@ -70,7 +72,7 @@ AudioTrackImportHandler::get_info () const
 
 AudioTrackImporter::AudioTrackImporter (XMLTree const & source,
                                         Session & session,
-                                        AudioTrackImportHandler & handler,
+                                        AudioTrackImportHandler & track_handler,
                                         XMLNode const & node,
                                         AudioPlaylistImportHandler & pl_handler) :
   ElementImporter (source, session),
@@ -102,6 +104,11 @@ AudioTrackImporter::AudioTrackImporter (XMLTree const & source,
        xml_track.remove_nodes_and_delete ("extra");
 }
 
+AudioTrackImporter::~AudioTrackImporter ()
+{
+       playlists.clear ();
+}
+
 bool
 AudioTrackImporter::parse_route_xml ()
 {
@@ -162,9 +169,18 @@ AudioTrackImporter::parse_io ()
                        (*it)->set_value (id.to_s());
                        id_ok = true;
                } else if (!prop.compare("inputs")) {
-                       // TODO Let the IO class do it's thing for now...
+                       // TODO Handle this properly!
+                       /* Input and output ports are counted and added empty, so that no in/output connecting function fails. */
+                       uint32_t num_inputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
+                       std::string value;
+                       for (uint32_t i = 0; i < num_inputs; i++) { value += "{}"; }
+                       (*it)->set_value (value);
                } else if (!prop.compare("outputs")) {
-                       // TODO Let the IO class do it's thing for now...
+                       // TODO See comments above
+                       uint32_t num_outputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
+                       std::string value;
+                       for (uint32_t i = 0; i < num_outputs; i++) { value += "{}"; }
+                       (*it)->set_value (value);
                } else {
                        std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
                }
@@ -232,7 +248,6 @@ AudioTrackImporter::_prepare_move ()
        xml_track.child ("IO")->property ("name")->set_value (name);
        track_handler.add_name (name);
        
-       // TODO
        return true;
 }
 
@@ -241,13 +256,40 @@ AudioTrackImporter::_cancel_move ()
 {
        track_handler.remove_name (name);
        playlists.clear ();
-       // TODO
 }
 
 void
 AudioTrackImporter::_move ()
-{
-       // TODO
+{      
+       /* Add diskstream */
+       
+       boost::shared_ptr<XMLSharedNodeList> ds_node_list;
+       string xpath = "/Session/DiskStreams/AudioDiskstream[@id='" + old_ds_id.to_s() + "']";
+       ds_node_list = source.root()->find (xpath);
+       
+       if (ds_node_list->size() != 1) {
+               error << string_compose (_("Error Importing Audio track %1"), name) << endmsg;
+               return;
+       }
+       
+       boost::shared_ptr<XMLNode> ds_node = ds_node_list->front();
+       ds_node->property ("id")->set_value (new_ds_id.to_s());
+       
+       boost::shared_ptr<Diskstream> new_ds (new AudioDiskstream (session, *ds_node));
+       new_ds->set_name (name);
+       session.add_diskstream (new_ds);
+
+       /* Import playlists */
+
+       for (PlaylistList::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
+               (*it)->move ();
+       }
+
+       /* Import track */
+
+       XMLNode routes ("Routes");
+       routes.add_child_copy (xml_track);
+       session.load_routes (routes);
 }
 
 bool