Use boost::shared_ptr instead of raw pointers for RouteUI track (etc) accessors ...
[ardour.git] / gtk2_ardour / export_dialog.cc
index bfe85f69a387972d6118316b1a5f39869c349390..0707dd8230d21a0f4086f5ebf001a7696576d291 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 
 */
 
 #include <unistd.h>
 #include <utility>
 #include <sys/stat.h>
-
 #include <fstream>
 
 #include <samplerate.h>
-#include <pbd/pthread_utils.h>
+
+#include <pbd/convert.h>
 #include <pbd/xml++.h>
-#include <pbd/dirname.h>
 
 #include <gtkmm2ext/utils.h>
+#include <gtkmm2ext/window_title.h>
+
 #include <ardour/export.h>
 #include <ardour/sndfile_helpers.h>
 #include <ardour/audio_track.h>
 #include "ardour_ui.h"
 #include "public_editor.h"
 #include "keyboard.h"
-#include "ardour_message.h"
 
 #include "i18n.h"
 
 #define FRAME_NAME "BaseFrame"
 
 using namespace std;
-
 using namespace ARDOUR;
+using namespace PBD;
 using namespace sigc;
 using namespace Gtk;
+using namespace Gtkmm2ext;
+
+using PBD::internationalize;
 
 static const gchar *sample_rates[] = {
        N_("22.05kHz"),
@@ -95,21 +97,20 @@ static const gchar* cue_file_types[] = {
        0
 };
 
-ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
+ExportDialog::ExportDialog(PublicEditor& e)
        : ArdourDialog ("export dialog"),
          editor (e),
          format_table (9, 2),
-         format_frame (_("FORMAT")),
-         cue_file_label (_("CD MARKER FILE TYPE"), 1.0, 0.5),
-         channel_count_label (_("CHANNELS"), 1.0, 0.5),
-         header_format_label (_("FILE TYPE"), 1.0, 0.5),
-         bitdepth_format_label (_("SAMPLE FORMAT"), 1.0, 0.5),
-         endian_format_label (_("SAMPLE ENDIANNESS"), 1.0, 0.5),
-         sample_rate_label (_("SAMPLE RATE"), 1.0, 0.5),
-         src_quality_label (_("CONVERSION QUALITY"), 1.0, 0.5),
-         dither_type_label (_("DITHER TYPE"), 1.0, 0.5),
-         cuefile_only_checkbox (_("EXPORT CD MARKER FILE ONLY")),
-         file_frame (_("EXPORT TO FILE")),
+         format_frame (_("Format")),
+         cue_file_label (_("CD Marker File Type"), 1.0, 0.5),
+         channel_count_label (_("Channels"), 1.0, 0.5),
+         header_format_label (_("File Type"), 1.0, 0.5),
+         bitdepth_format_label (_("Sample Format"), 1.0, 0.5),
+         endian_format_label (_("Sample Endianness"), 1.0, 0.5),
+         sample_rate_label (_("Sample Rate"), 1.0, 0.5),
+         src_quality_label (_("Conversion Quality"), 1.0, 0.5),
+         dither_type_label (_("Dither Type"), 1.0, 0.5),
+         cuefile_only_checkbox (_("Export CD Marker File Only")),
          file_browse_button (_("Browse")),
          track_selector_button (_("Specific tracks ..."))
 {
@@ -117,16 +118,19 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
        guint32 len;
        guint32 maxlen;
 
-       audio_region = r;
-
        session = 0;
+       track_and_master_selection_allowed = true;
+       channel_count_selection_allowed = true;
+       export_cd_markers_allowed = true;
+
+       WindowTitle title(Glib::get_application_name());
+       title += _("Export");
        
-       set_title (_("ardour: export"));
-       set_wmclass (_("ardour_export"), "Ardour");
+       set_title (title.get_string());
+       set_wmclass (X_("ardour_export"), "Ardour");
        set_name ("ExportWindow");
        add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
 
-       file_selector = 0;
        spec.running = false;
 
        file_entry.set_name ("ExportFileNameEntry");
@@ -177,23 +181,18 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
        hpacker.set_border_width (5);
        hpacker.pack_start (format_frame, false, false);
 
-       if (!audio_region) {
-
-               master_scroll.add (master_selector);
-               track_scroll.add (track_selector);
+       master_scroll.add (master_selector);
+       track_scroll.add (track_selector);
 
-               master_scroll.set_size_request (220, 100);
-               track_scroll.set_size_request (220, 100);
-
-               
+       master_scroll.set_size_request (220, 100);
+       track_scroll.set_size_request (220, 100);
                
-               /* we may hide some of these later */
-               track_vpacker.pack_start (master_scroll);
-               track_vpacker.pack_start (track_scroll);
-               track_vpacker.pack_start (track_selector_button, Gtk::PACK_EXPAND_PADDING);
+       /* we may hide some of these later */
+       track_vpacker.pack_start (master_scroll);
+       track_vpacker.pack_start (track_scroll);
+       track_vpacker.pack_start (track_selector_button, Gtk::PACK_EXPAND_PADDING);
 
-               hpacker.pack_start (track_vpacker);
-       }
+       hpacker.pack_start (track_vpacker);
 
        get_vbox()->pack_start (hpacker);
        
@@ -217,28 +216,28 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
           takes a reference. 
        */
 
-       vector<string> pop_strings = internationalize(sample_rates);
+       vector<string> pop_strings = I18N(sample_rates);
        Gtkmm2ext::set_popdown_strings (sample_rate_combo, pop_strings);
        sample_rate_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize(src_quality);
+       pop_strings = I18N(src_quality);
        Gtkmm2ext::set_popdown_strings (src_quality_combo, pop_strings);
        src_quality_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize(dither_types);
+       pop_strings = I18N(dither_types);
        Gtkmm2ext::set_popdown_strings (dither_type_combo, pop_strings);
        dither_type_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize(channel_strings);
+       pop_strings = I18N(channel_strings);
        Gtkmm2ext::set_popdown_strings (channel_count_combo, pop_strings);
        channel_count_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize((const char **) sndfile_header_formats_strings);
+       pop_strings = I18N((const char **) sndfile_header_formats_strings);
        Gtkmm2ext::set_popdown_strings (header_format_combo, pop_strings);
        header_format_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize((const char **) sndfile_bitdepth_formats_strings);
+       pop_strings = I18N((const char **) sndfile_bitdepth_formats_strings);
        Gtkmm2ext::set_popdown_strings (bitdepth_format_combo, pop_strings);
        bitdepth_format_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize((const char **) sndfile_endian_formats_strings);
+       pop_strings = I18N((const char **) sndfile_endian_formats_strings);
        Gtkmm2ext::set_popdown_strings (endian_format_combo, pop_strings);
        endian_format_combo.set_active_text (pop_strings.front());
-       pop_strings = internationalize(cue_file_types);
+       pop_strings = I18N(cue_file_types);
        Gtkmm2ext::set_popdown_strings (cue_file_combo, pop_strings);
        cue_file_combo.set_active_text (pop_strings.front());
 
@@ -250,10 +249,8 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
 
        /* determine longest strings at runtime */
 
-       const guint32 FUDGE = 10; // Combo's are stupid - they steal space from the entry for the button
-
        maxlen = 0;
-       const char *longest = "gl";
+       const char *longest = X_("gl"); /* translators: one ascender, one descender */
        string longest_str;
 
        for (n = 0; n < SNDFILE_HEADER_FORMATS; ++n) {
@@ -323,11 +320,9 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
        format_table.set_col_spacings (5);
        format_table.set_row_spacings (5);
 
-       if (!audio_region) {
-               format_table.attach (channel_count_label, 0, 1, 0, 1);
-               format_table.attach (channel_count_combo, 1, 2, 0, 1);
-       }
-
+       format_table.attach (channel_count_label, 0, 1, 0, 1);
+       format_table.attach (channel_count_combo, 1, 2, 0, 1);
+       
        format_table.attach (header_format_label, 0, 1, 1, 2);
        format_table.attach (header_format_combo, 1, 2, 1, 2);
 
@@ -354,13 +349,13 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
 
        signal_delete_event().connect (mem_fun(*this, &ExportDialog::window_closed));
 
-       ok_button = add_button (Stock::OK, RESPONSE_ACCEPT);
-       ok_button->signal_clicked().connect (mem_fun(*this, &ExportDialog::do_export));
        cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
        cancel_button->signal_clicked().connect (mem_fun(*this, &ExportDialog::end_dialog));
+       ok_button = add_button (_("Export"), RESPONSE_ACCEPT);
+       ok_button->signal_clicked().connect (mem_fun(*this, &ExportDialog::do_export));
        
        file_browse_button.set_name ("EditorGTKButton");
-       file_browse_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::initiate_browse));
+       file_browse_button.signal_clicked().connect (mem_fun(*this, &ExportDialog::browse));
 
        channel_count_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::channels_chosen));
        bitdepth_format_combo.signal_changed().connect (mem_fun(*this, &ExportDialog::bitdepth_chosen));
@@ -371,38 +366,59 @@ ExportDialog::ExportDialog(PublicEditor& e, AudioRegion* r)
 
 ExportDialog::~ExportDialog()
 {
-       if (file_selector) {
-               delete file_selector;
-       }
+}
+
+void
+ExportDialog::do_not_allow_track_and_master_selection()
+{
+       track_and_master_selection_allowed = false;
+       track_vpacker.set_no_show_all();
+}
+
+void
+ExportDialog::do_not_allow_channel_count_selection()
+{
+       channel_count_selection_allowed = false;
+       channel_count_combo.set_no_show_all();
+       channel_count_label.set_no_show_all();
+}
+
+void
+ExportDialog::do_not_allow_export_cd_markers()
+{
+       export_cd_markers_allowed = false;
+       cue_file_label.set_no_show_all();
+       cue_file_combo.set_no_show_all();
+       cuefile_only_checkbox.set_no_show_all();
 }
 
 void
 ExportDialog::connect_to_session (Session *s)
 {
        session = s;
-       session->going_away.connect (mem_fun(*this, &Window::hide_all));
+       session->GoingAway.connect (mem_fun(*this, &Window::hide_all));
 
        switch (session->frame_rate()) {
        case 22050:
-               sample_rate_combo.set_active_text (N_("22.05kHz"));
+               sample_rate_combo.set_active_text (_("22.05kHz"));
                break;
        case 44100:
-               sample_rate_combo.set_active_text (N_("44.1kHz"));
+               sample_rate_combo.set_active_text (_("44.1kHz"));
                break;
        case 48000:
-               sample_rate_combo.set_active_text (N_("48kHz"));
+               sample_rate_combo.set_active_text (_("48kHz"));
                break;
        case 88200:
-               sample_rate_combo.set_active_text (N_("88.2kHz"));
+               sample_rate_combo.set_active_text (_("88.2kHz"));
                break;
        case 96000:
-               sample_rate_combo.set_active_text (N_("96kHz"));
+               sample_rate_combo.set_active_text (_("96kHz"));
                break;
        case 192000:
-               sample_rate_combo.set_active_text (N_("192kHz"));
+               sample_rate_combo.set_active_text (_("192kHz"));
                break;
        default:
-               sample_rate_combo.set_active_text (N_("44.1kHz"));
+               sample_rate_combo.set_active_text (_("44.1kHz"));
                break;
        }
 
@@ -478,7 +494,7 @@ ExportDialog::set_state()
                        }
 
                        TreeModel::Children rows = master_selector.get_model()->children();
-                       for (uint32_t r = 0; r < session->master_out()->n_outputs(); ++r) {
+                       for (uint32_t r = 0; r < session->master_out()->n_outputs().n_audio(); ++r) {
                                if (nchns == 2) {
                                        if (r % 2) {
                                                rows[r][exp_cols.right] = true;
@@ -559,7 +575,7 @@ ExportDialog::save_state()
 
                row = *ri;
                track->add_property(X_("channel1"), row[exp_cols.left] ? X_("on") : X_("off"));
-               track->add_property(X_("channel1"), row[exp_cols.right] ? X_("on") : X_("off"));
+               track->add_property(X_("channel2"), row[exp_cols.right] ? X_("on") : X_("off"));
 
                tracks->add_child_nocopy(*track);
        }
@@ -569,41 +585,21 @@ ExportDialog::save_state()
 }
 
 void
-ExportDialog::set_range (jack_nframes_t start, jack_nframes_t end)
+ExportDialog::set_range (nframes_t start, nframes_t end)
 {
        spec.start_frame = start;
        spec.end_frame = end;
-
-       if (!audio_region) {
-               // XXX: this is a hack until we figure out what is really wrong
-               session->request_locate (spec.start_frame, false);
-       }
 }
 
 gint
 ExportDialog::progress_timeout ()
 {
-       progress_bar.set_fraction (spec.progress/100);
+       progress_bar.set_fraction (spec.progress);
        return TRUE;
 }
 
-void*
-ExportDialog::_export_region_thread (void *arg)
-{
-       PBD::ThreadCreated (pthread_self(), X_("Export Region"));
-
-       static_cast<ExportDialog*>(arg)->export_region ();
-       return 0;
-}
-
-void
-ExportDialog::export_region ()
-{
-       audio_region->exportme (*session, spec);
-}
-
 void
-frames_to_cd_frames_string (char* buf, jack_nframes_t when, jack_nframes_t fr)
+frames_to_cd_frames_string (char* buf, nframes_t when, nframes_t fr)
 {
 
   long unsigned int remainder;
@@ -627,8 +623,11 @@ struct LocationSortByStart {
 void
 ExportDialog::export_toc_file (Locations::LocationList& locations, const string& path)
 {
+       if(!export_cd_markers_allowed){
+               return;
+       }
        
-        string filepath = path + ".toc";
+    string filepath = path + ".toc";
        ofstream out (filepath.c_str());
        long unsigned int last_end_time = spec.start_frame, last_start_time = spec.start_frame;
        int numtracks = 0;
@@ -750,7 +749,11 @@ ExportDialog::export_toc_file (Locations::LocationList& locations, const string&
 void
 ExportDialog::export_cue_file (Locations::LocationList& locations, const string& path)
 {
-        string filepath = path + ".cue";
+       if(!export_cd_markers_allowed){
+               return;
+       }
+       
+    string filepath = path + ".cue";
        ofstream out (filepath.c_str());
        gchar buf[18];
        long unsigned int last_track_end = spec.start_frame;
@@ -869,11 +872,11 @@ ExportDialog::export_cue_file (Locations::LocationList& locations, const string&
        }
        
 }
-
+       
 void
 ExportDialog::do_export_cd_markers (const string& path,const string& cuefile_type)
 {
-       if (cuefile_type == "TOC") {
+       if (cuefile_type == _("TOC")) {
                session->locations()->apply (*this, &ExportDialog::export_toc_file, path);      
        } else {
                session->locations()->apply (*this, &ExportDialog::export_cue_file, path);
@@ -884,52 +887,21 @@ ExportDialog::do_export_cd_markers (const string& path,const string& cuefile_typ
 void
 ExportDialog::do_export ()
 {
-       // sanity check file name first
-       string filepath = file_entry.get_text();
-       struct stat statbuf;
-  
-       if (filepath.empty()) {
-               // warning dialog
-               string txt = _("Please enter a valid filename.");
-               MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
-               msg.run();
-               return;
-       }
+       string filepath = file_entry.get_text();
        
-       // check if file exists already and warn
-       if (stat (filepath.c_str(), &statbuf) == 0) {
-               if (S_ISDIR (statbuf.st_mode)) {
-                       string txt = _("Please specify a complete filename for the audio file.");
-                       MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
-                       msg.run();
-                       return;
-               }
-               else {
-                       string txt = _("File already exists, do you want to overwrite it?");
-                       MessageDialog msg (*this, txt, false, MESSAGE_QUESTION, BUTTONS_YES_NO, true);
-                       //ArdourMessage msg (this, X_("exportoverwrite"), txt, true, false, Gtk::BUTTONS_YES_NO);
-                       if ((ResponseType) msg.run() == Gtk::RESPONSE_NO) {
-                               return;
-                       }
-               }
-       }
-       
-       // directory needs to exist and be writable
-       string dirpath = PBD::dirname (filepath);
-       if (::access (dirpath.c_str(), W_OK) != 0) {
-               string txt = _("Cannot write file in: ") + dirpath;
-               MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
-               msg.run();
-               return;
-       }
-       
-       if (cue_file_combo.get_active_text () != _("None")) {
-               do_export_cd_markers (file_entry.get_text(), cue_file_combo.get_active_text ());
+       if(!is_filepath_valid(filepath)){
+               return;
        }
 
-       if (cuefile_only_checkbox.get_active()) {
-               end_dialog ();
-               return;
+       if (export_cd_markers_allowed) {
+               if (cue_file_combo.get_active_text () != _("None")) {
+                       do_export_cd_markers (file_entry.get_text(), cue_file_combo.get_active_text ());
+               }
+
+               if (cuefile_only_checkbox.get_active()) {
+                       end_dialog ();
+                       return;
+               }
        }
 
        ok_button->set_sensitive(false);
@@ -937,156 +909,21 @@ ExportDialog::do_export ()
 
        set_modal (true);
        
-       spec.path = filepath;
-       spec.progress = 0;
-       spec.running = true;
-       spec.stop = false;
-       spec.port_map.clear();
-       
-       if (channel_count_combo.get_active_text() == _("mono")) {
-               spec.channels = 1;
-       } else {
-               spec.channels = 2;
-       }
-
-       spec.format = 0;
-
-       spec.format |= sndfile_header_format_from_string (header_format_combo.get_active_text ());
+       // read user input into spec
+       initSpec(filepath);
        
-       if ((spec.format & SF_FORMAT_WAV) == 0) {
-               /* RIFF/WAV specifies endianess */
-               spec.format |= sndfile_endian_format_from_string (endian_format_combo.get_active_text ());
-       }
-
-       spec.format |= sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());
-
-       string sr_str = sample_rate_combo.get_active_text();
-       if (sr_str == N_("22.05kHz")) {
-               spec.sample_rate = 22050;
-       } else if (sr_str == N_("44.1kHz")) {
-               spec.sample_rate = 44100;
-       } else if (sr_str == N_("48kHz")) {
-               spec.sample_rate = 48000;
-       } else if (sr_str == N_("88.2kHz")) {
-               spec.sample_rate = 88200;
-       } else if (sr_str == N_("96kHz")) {
-               spec.sample_rate = 96000;
-       } else if (sr_str == N_("192kHz")) {
-               spec.sample_rate = 192000;
-       } else {
-               spec.sample_rate = session->frame_rate();
-       }
-       
-       string src_str = src_quality_combo.get_active_text();
-       if (src_str == _("fastest")) {
-               spec.src_quality = SRC_ZERO_ORDER_HOLD;
-       } else if (src_str == _("linear")) {
-               spec.src_quality = SRC_LINEAR;
-       } else if (src_str == _("better")) {
-               spec.src_quality = SRC_SINC_FASTEST;
-       } else if (src_str == _("intermediate")) {
-               spec.src_quality = SRC_SINC_MEDIUM_QUALITY;
-       } else {
-               spec.src_quality = SRC_SINC_BEST_QUALITY;
-       }
-
-       string dither_str = dither_type_combo.get_active_text();
-       if (dither_str == _("None")) {
-               spec.dither_type = GDitherNone;
-       } else if (dither_str == _("Rectangular")) {
-               spec.dither_type = GDitherRect;
-       } else if (dither_str == _("Triangular")) {
-               spec.dither_type = GDitherTri;
-       } else {
-               spec.dither_type = GDitherShaped;
-       } 
-
-       if (!audio_region) {
-
-               uint32_t chan=0;
-               Port *last_port = 0;
-               
-               TreeModel::Children rows = master_selector.get_model()->children();
-               TreeModel::Children::iterator ri;
-               TreeModel::Row row;
-               for (ri = rows.begin(); ri != rows.end(); ++ri) {
-                       row = *ri;
-                       Port* port = row[exp_cols.port];
-                       
-                       if (last_port != port) {
-                               chan = 0;
-                       }
-                       
-                       if (row[exp_cols.left]) {
-                               spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
-                       } 
-                       
-                       if (spec.channels == 2) {
-                               if (row[exp_cols.right]) {
-                                       spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
-                               }
-                       }
-               }
-
-               chan = 0;
-
-               rows = track_selector.get_model()->children();
-               for (ri = rows.begin(); ri != rows.end(); ++ri) {
-                       row = *ri;
-                       
-                       Port* port = row[exp_cols.port];
-                       
-                       if (last_port != port) {
-                               chan = 0;
-                       }
-                       
-                       if (row[exp_cols.left]) {
-                               spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
-                       } 
-                       
-                       if (spec.channels == 2) {
-                               if (row[exp_cols.right]) {
-                                       spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
-                               }
-                               
-                       }
-                       
-                       last_port = port;
-                       ++chan;
-               }
-       }
-
        progress_connection = Glib::signal_timeout().connect (mem_fun(*this, &ExportDialog::progress_timeout), 100);
        cancel_label.set_text (_("Stop Export"));
 
-       if (!audio_region) {
-               if (session->start_audio_export (spec)) {
-                       goto out;
-               }
-       } else {
-               pthread_t thr;
-               pthread_create_and_store ("region export", &thr, 0, ExportDialog::_export_region_thread, this);
-       }
-
-       gtk_main_iteration ();
-       while (spec.running) {
-               if (gtk_events_pending()) {
-                       gtk_main_iteration ();
-               } else {
-                       usleep (10000);
-               }
-       }
+       export_audio_data();
        
-  out:
-       progress_connection.disconnect ();
+       progress_connection.disconnect ();
        end_dialog ();
 }
        
-
 void
 ExportDialog::end_dialog ()
 {
-
        if (spec.running) {
                spec.stop = true;
 
@@ -1099,14 +936,10 @@ ExportDialog::end_dialog ()
                }
        }
 
-       session->engine().freewheel (false);
+       session->finalize_audio_export ();
 
        hide_all ();
 
-       if (file_selector) {
-               file_selector->hide_all ();
-       }
-
        set_modal (false);
        ok_button->set_sensitive(true);
 }
@@ -1118,20 +951,22 @@ ExportDialog::start_export ()
                return;
        }
 
-       /* If it the filename hasn't been set before, use the
-          directory above the current session as a default
+       /* If the filename hasn't been set before, use the
+          current session's export directory as a default
           location for the export.  
        */
        
        if (file_entry.get_text().length() == 0) {
-               string dir = session->path();
+               string dir = session->export_dir();
                string::size_type last_slash;
                
-               if ((last_slash = dir.find_last_of ('/')) != string::npos) {
+               if ((last_slash = dir.find_last_of ('/')) != string::npos && last_slash != 0) {
                        dir = dir.substr (0, last_slash+1);
                }
 
-               dir = dir + "export.wav";
+               if (!wants_dir()) {
+                       dir = dir + "export.wav";
+               }
                
                file_entry.set_text (dir);
        }
@@ -1191,19 +1026,19 @@ void
 ExportDialog::sample_rate_chosen ()
 {
        string sr_str = sample_rate_combo.get_active_text();
-       jack_nframes_t rate;
+       nframes_t rate;
 
        if (sr_str == N_("22.05kHz")) {
                rate = 22050;
-       } else if (sr_str == N_("44.1kHz")) {
+       } else if (sr_str == _("44.1kHz")) {
                rate = 44100;
-       } else if (sr_str == N_("48kHz")) {
+       } else if (sr_str == _("48kHz")) {
                rate = 48000;
-       } else if (sr_str == N_("88.2kHz")) {
+       } else if (sr_str == _("88.2kHz")) {
                rate = 88200;
-       } else if (sr_str == N_("96kHz")) {
+       } else if (sr_str == _("96kHz")) {
                rate = 96000;
-       } else if (sr_str == N_("192kHz")) {
+       } else if (sr_str == _("192kHz")) {
                rate = 192000;
        } else {
                rate = session->frame_rate();
@@ -1251,19 +1086,19 @@ ExportDialog::fill_lists ()
        track_list->clear();
        master_list->clear();
        
-       Session::RouteList routes = session->get_routes ();
-
-       for (Session::RouteList::iterator ri = routes.begin(); ri != routes.end(); ++ri) {
+       boost::shared_ptr<Session::RouteList> routes = session->get_routes ();
 
-               Route* route = (*ri);
+       for (Session::RouteList::iterator ri = routes->begin(); ri != routes->end(); ++ri) {
+               
+               boost::shared_ptr<Route> route = (*ri);
                
                if (route->hidden()) {
                        continue;
                }
 
-               for (uint32_t i=0; i < route->n_outputs(); ++i) {
+               for (uint32_t i=0; i < route->n_outputs().n_audio(); ++i) {
                        string name;
-                       if (route->n_outputs() == 1) {
+                       if (route->n_outputs().n_audio() == 1) {
                                name = route->name();
                        } else {
                                name = string_compose("%1: out-%2", route->name(), i+1);
@@ -1288,38 +1123,209 @@ ExportDialog::fill_lists ()
        }
 }
 
-gint
-ExportDialog::window_closed (GdkEventAny *ignored)
+
+bool
+ExportDialog::is_filepath_valid(string &filepath)
 {
-       end_dialog ();
-       return TRUE;
+       // sanity check file name first
+
+       struct stat statbuf;
+  
+       if (filepath.empty()) {
+               string txt = _("Please enter a valid filename.");
+               MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
+               msg.run();
+               return false;
+       }
+       
+       // check if file exists already and warn
+
+       if (stat (filepath.c_str(), &statbuf) == 0) {
+               if (S_ISDIR (statbuf.st_mode)) {
+                       string txt = _("Please specify a complete filename for the audio file.");
+                       MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
+                       msg.run();
+                       return false;
+               }
+               else {
+                       string txt = _("File already exists, do you want to overwrite it?");
+                       MessageDialog msg (*this, txt, false, MESSAGE_QUESTION, BUTTONS_YES_NO, true);
+                       if ((ResponseType) msg.run() == Gtk::RESPONSE_NO) {
+                               return false;
+                       }
+               }
+       }
+       
+       // directory needs to exist and be writable
+
+       string dirpath = Glib::path_get_dirname (filepath);
+       if (::access (dirpath.c_str(), W_OK) != 0) {
+               string txt = _("Cannot write file in: ") + dirpath;
+               MessageDialog msg (*this, txt, false, MESSAGE_ERROR, BUTTONS_OK, true);
+               msg.run();
+               return false;
+       }
+       
+       return true;
 }
+
 void
-ExportDialog::initiate_browse ()
+ExportDialog::initSpec(string &filepath)
 {
-       if (file_selector == 0) {
-               file_selector = new FileSelection;
-               file_selector->set_modal (true);
+       spec.path = filepath;
+       spec.progress = 0;
+       spec.running = true;
+       spec.stop = false;
+       spec.port_map.clear();
+       
+       if (channel_count_combo.get_active_text() == _("mono")) {
+               spec.channels = 1;
+       } else {
+               spec.channels = 2;
+       }
+
+       spec.format = 0;
 
-               file_selector->get_cancel_button()->signal_clicked().connect (bind (mem_fun(*this, &ExportDialog::finish_browse), -1));
-               file_selector->get_ok_button()->signal_clicked().connect (bind (mem_fun(*this, &ExportDialog::finish_browse), 1));
+       spec.format |= sndfile_header_format_from_string (header_format_combo.get_active_text ());
+       
+       if ((spec.format & SF_FORMAT_WAV) == 0) {
+               /* RIFF/WAV specifies endianess */
+               spec.format |= sndfile_endian_format_from_string (endian_format_combo.get_active_text ());
        }
-       file_selector->set_filename (file_entry.get_text());
-       file_selector->show_all ();
+
+       spec.format |= sndfile_bitdepth_format_from_string (bitdepth_format_combo.get_active_text ());
+
+       string sr_str = sample_rate_combo.get_active_text();
+       if (sr_str == N_("22.05kHz")) {
+               spec.sample_rate = 22050;
+       } else if (sr_str == _("44.1kHz")) {
+               spec.sample_rate = 44100;
+       } else if (sr_str == _("48kHz")) {
+               spec.sample_rate = 48000;
+       } else if (sr_str == _("88.2kHz")) {
+               spec.sample_rate = 88200;
+       } else if (sr_str == _("96kHz")) {
+               spec.sample_rate = 96000;
+       } else if (sr_str == _("192kHz")) {
+               spec.sample_rate = 192000;
+       } else {
+               spec.sample_rate = session->frame_rate();
+       }
+       
+       string src_str = src_quality_combo.get_active_text();
+       if (src_str == _("fastest")) {
+               spec.src_quality = SRC_ZERO_ORDER_HOLD;
+       } else if (src_str == _("linear")) {
+               spec.src_quality = SRC_LINEAR;
+       } else if (src_str == _("better")) {
+               spec.src_quality = SRC_SINC_FASTEST;
+       } else if (src_str == _("intermediate")) {
+               spec.src_quality = SRC_SINC_MEDIUM_QUALITY;
+       } else {
+               spec.src_quality = SRC_SINC_BEST_QUALITY;
+       }
+
+       string dither_str = dither_type_combo.get_active_text();
+       if (dither_str == _("None")) {
+               spec.dither_type = GDitherNone;
+       } else if (dither_str == _("Rectangular")) {
+               spec.dither_type = GDitherRect;
+       } else if (dither_str == _("Triangular")) {
+               spec.dither_type = GDitherTri;
+       } else {
+               spec.dither_type = GDitherShaped;
+       } 
+
+       write_track_and_master_selection_to_spec();
 }
 
+
 void
-ExportDialog::finish_browse (int status)
+ExportDialog::write_track_and_master_selection_to_spec()
 {
-       if (file_selector) {
-               if (status > 0) {
-                       string result = file_selector->get_filename();
-                       
-                       if (result.length()) {
-                               file_entry.set_text (result);
+       if(!track_and_master_selection_allowed){
+               return;
+       }
+
+       uint32_t chan=0;
+       Port *last_port = 0;
+               
+       TreeModel::Children rows = master_selector.get_model()->children();
+       TreeModel::Children::iterator ri;
+       TreeModel::Row row;
+       for (ri = rows.begin(); ri != rows.end(); ++ri) {
+               row = *ri;
+               Port* port = row[exp_cols.port];
+               
+               if (last_port != port) {
+                       chan = 0;
+               }
+               
+               if (row[exp_cols.left]) {
+                       spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
+               } 
+               
+               if (spec.channels == 2) {
+                       if (row[exp_cols.right]) {
+                               spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
+                       }
+               }
+       }
+       
+       chan = 0;
+       rows = track_selector.get_model()->children();
+
+       for (ri = rows.begin(); ri != rows.end(); ++ri) {
+               row = *ri;
+               
+               Port* port = row[exp_cols.port];
+               
+               if (last_port != port) {
+                       chan = 0;
+               }
+               
+               if (row[exp_cols.left]) {
+                       spec.port_map[0].push_back (std::pair<Port*,uint32_t>(port, chan));
+               } 
+               
+               if (spec.channels == 2) {
+                       if (row[exp_cols.right]) {
+                               spec.port_map[1].push_back (std::pair<Port*,uint32_t>(port, chan));
                        }
+                       
+               }
+               
+               last_port = port;
+               ++chan;
+       }
+}
+
+
+gint
+ExportDialog::window_closed (GdkEventAny *ignored)
+{
+       end_dialog ();
+       return TRUE;
+}
+
+void
+ExportDialog::browse ()
+{
+       FileChooserDialog dialog("Export to file", browse_action());
+       dialog.set_transient_for(*this);
+       dialog.set_filename (file_entry.get_text());
+
+       dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+       dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
+  
+       int result = dialog.run();
+
+       if (result == Gtk::RESPONSE_OK) {
+               string filename = dialog.get_filename();
+       
+               if (filename.length()) {
+                       file_entry.set_text (filename);
                }
-               file_selector->hide_all();
        }
 }