+ } catch (exception& e) {
+ error << string_compose (_("MIDI file could not be written (best guess: %1)"), e.what()) << endmsg;
+ }
+
+ if (buf) {
+ free (buf);
+ }
+}
+
+static void
+remove_file_source (boost::shared_ptr<Source> source)
+{
+ boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
+
+ fs->DropReferences ();
+
+ if (fs) {
+ ::g_unlink (fs->path().c_str());
+ }
+}
+
+// This function is still unable to cleanly update an existing source, even though
+// it is possible to set the ImportStatus flag accordingly. The functinality
+// is disabled at the GUI until the Source implementations are able to provide
+// the necessary API.
+void
+Session::import_files (ImportStatus& status)
+{
+ typedef vector<boost::shared_ptr<Source> > Sources;
+ Sources all_new_sources;
+ boost::shared_ptr<AudioFileSource> afs;
+ boost::shared_ptr<SMFSource> smfs;
+ uint32_t channels = 0;
+ vector<string> smf_names;
+
+ status.sources.clear ();
+
+ for (vector<string>::const_iterator p = status.paths.begin();
+ p != status.paths.end() && !status.cancel;
+ ++p)
+ {
+ boost::shared_ptr<ImportableSource> source;
+ std::auto_ptr<Evoral::SMF> smf_reader;
+ const DataType type = SMFSource::safe_midi_file_extension (*p) ? DataType::MIDI : DataType::AUDIO;
+
+ if (type == DataType::AUDIO) {
+ try {
+ source = open_importable_source (*p, frame_rate(), status.quality);
+ channels = source->channels();
+ } catch (const failed_constructor& err) {
+ error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
+ status.done = status.cancel = true;
+ return;
+ }
+
+ } else {
+ try {
+ smf_reader = std::auto_ptr<Evoral::SMF>(new Evoral::SMF());
+
+ if (smf_reader->open(*p)) {
+ throw Evoral::SMF::FileError (*p);
+ }
+
+ if (smf_reader->is_type0 () && status.split_midi_channels) {
+ channels = smf_reader->channels().size();
+ } else {
+ channels = smf_reader->num_tracks();
+ switch (status.midi_track_name_source) {
+ case SMFTrackNumber:
+ break;
+ case SMFTrackName:
+ smf_reader->track_names (smf_names);
+ break;
+ case SMFInstrumentName:
+ smf_reader->instrument_names (smf_names);
+ break;
+ }
+ }
+ } catch (...) {
+ error << _("Import: error opening MIDI file") << endmsg;
+ status.done = status.cancel = true;
+ return;
+ }