correct name the event loop used by the GUI
[ardour.git] / gtk2_ardour / ardour_ui.cc
index ecdb549fd639e60eae9ecd88abe06ccf1df0e366..10be9afb5af2441b5fcd87f16db2fe91ef09ac68 100644 (file)
@@ -26,7 +26,8 @@
 #include <cmath>
 #include <iostream>
 #include <cerrno>
-#include <fstream>
+
+#include <stdarg.h>
 
 #ifndef PLATFORM_WINDOWS
 #include <sys/resource.h>
@@ -39,7 +40,7 @@
 #include <time.h>
 
 #include <glib.h>
-#include <glib/gstdio.h>
+#include "pbd/gstdio_compat.h"
 
 #include <gtkmm/messagedialog.h>
 #include <gtkmm/accelmap.h>
@@ -55,6 +56,8 @@
 #include "pbd/file_utils.h"
 #include "pbd/localtime_r.h"
 #include "pbd/pthread_utils.h"
+#include "pbd/replace_all.h"
+#include "pbd/xml++.h"
 
 #include "gtkmm2ext/application.h"
 #include "gtkmm2ext/bindings.h"
@@ -73,6 +76,7 @@
 #include "ardour/diskstream.h"
 #include "ardour/filename_extensions.h"
 #include "ardour/filesystem_paths.h"
+#include "ardour/ltc_file_reader.h"
 #include "ardour/port.h"
 #include "ardour/plugin_manager.h"
 #include "ardour/process_thread.h"
@@ -82,6 +86,7 @@
 #include "ardour/session_route.h"
 #include "ardour/session_state_utils.h"
 #include "ardour/session_utils.h"
+#include "ardour/source_factory.h"
 #include "ardour/slave.h"
 #include "ardour/system_exec.h"
 
@@ -103,8 +108,10 @@ typedef uint64_t microseconds_t;
 #include "ambiguous_file_dialog.h"
 #include "ardour_ui.h"
 #include "audio_clock.h"
+#include "audio_region_view.h"
 #include "big_clock_window.h"
 #include "bundle_manager.h"
+#include "duplicate_routes_dialog.h"
 #include "engine_dialog.h"
 #include "export_video_dialog.h"
 #include "export_video_infobox.h"
@@ -119,6 +126,7 @@ typedef uint64_t microseconds_t;
 #include "missing_file_dialog.h"
 #include "missing_plugin_dialog.h"
 #include "mixer_ui.h"
+#include "meterbridge.h"
 #include "mouse_cursors.h"
 #include "nsm.h"
 #include "opts.h"
@@ -160,8 +168,6 @@ ARDOUR_UI *ARDOUR_UI::theArdourUI = 0;
 sigc::signal<void, framepos_t, bool, framepos_t> ARDOUR_UI::Clock;
 sigc::signal<void>      ARDOUR_UI::CloseAllDialogs;
 
-float ARDOUR_UI::ui_scale = 1.0;
-
 static bool
 ask_about_configuration_copy (string const & old_dir, string const & new_dir, int version)
 {
@@ -181,10 +187,45 @@ ask_about_configuration_copy (string const & old_dir, string const & new_dir, in
        return (msg.run() == Gtk::RESPONSE_YES);
 }
 
+static void
+libxml_generic_error_func (void* /* parsing_context*/,
+                   const char* msg,
+                   ...)
+{
+       va_list ap;
+       char buf[2048];
+
+       va_start (ap, msg);
+       vsnprintf (buf, sizeof (buf), msg, ap);
+       error << buf << endmsg;
+       va_end (ap);
+}
+
+static void
+libxml_structured_error_func (void* /* parsing_context*/,
+                              xmlErrorPtr err)
+{
+       string msg;
+
+       if (err->message)
+               msg = err->message;
+
+       replace_all (msg, "\n", "");
+
+       if (err->file && err->line) {
+               error << X_("XML error: ") << msg << " in " << err->file << " at line " << err->line;
+
+               if (err->int2) {
+                       error << ':' << err->int2;
+               }
+       }
+       error << endmsg;
+}
+
+
 ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
 
-       : Gtkmm2ext::UI (PROGRAM_NAME, argcp, argvp)
-       , ui_config (new UIConfiguration)
+       : Gtkmm2ext::UI (X_("gui"), argcp, argvp)
        , session_loaded (false)
        , gui_object_state (new GUIObjectState)
        , primary_clock   (new MainClock (X_("primary"),   X_("transport"), true ))
@@ -197,6 +238,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        , nsm (0)
        , _was_dirty (false)
        , _mixer_on_top (false)
+       , _initial_verbose_plugin_scan (false)
        , first_time_engine_run (true)
        , roll_controllable (new TransportControllable ("transport roll", *this, TransportControllable::Roll))
        , stop_controllable (new TransportControllable ("transport stop", *this, TransportControllable::Stop))
@@ -214,7 +256,6 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        , error_alert_button ( ArdourButton::just_led_default_elements )
        , editor_meter(0)
        , editor_meter_peak_display()
-       , open_session_selector (0)
        , _numpad_locate_happening (false)
        , _session_is_new (false)
        , last_key_press_time (0)
@@ -244,23 +285,31 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        , _status_bar_visibility (X_("status-bar"))
        , _feedback_exists (false)
        , _log_not_acknowledged (LogLevelNone)
+       , duplicate_routes_dialog (0)
 {
        Gtkmm2ext::init (localedir);
 
+       UIConfiguration::instance().post_gui_init ();
+
        if (ARDOUR::handle_old_configuration_files (boost::bind (ask_about_configuration_copy, _1, _2, _3))) {
                MessageDialog msg (string_compose (_("Your configuration files were copied. You can now restart %1."), PROGRAM_NAME), true);
                msg.run ();
                /* configuration was modified, exit immediately */
                _exit (0);
        }
-       
+
        if (theArdourUI == 0) {
                theArdourUI = this;
        }
 
-       ui_config->ParameterChanged.connect (sigc::mem_fun (*this, &ARDOUR_UI::parameter_changed));
+       /* stop libxml from spewing to stdout/stderr */
+
+       xmlSetGenericErrorFunc (this, libxml_generic_error_func);
+       xmlSetStructuredErrorFunc (this, libxml_structured_error_func);
+
+       UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &ARDOUR_UI::parameter_changed));
        boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
-       ui_config->map_parameters (pc);
+       UIConfiguration::instance().map_parameters (pc);
 
        roll_button.set_controllable (roll_controllable);
        stop_button.set_controllable (stop_controllable);
@@ -331,7 +380,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        setup_gtk_ardour_enums ();
        setup_profile ();
 
-       SessionEvent::create_per_thread_pool ("GUI", 512);
+       SessionEvent::create_per_thread_pool ("GUI", 4096);
 
        /* we like keyboards */
 
@@ -345,7 +394,7 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
        /* we don't like certain modifiers */
        Bindings::set_ignored_state (GDK_LOCK_MASK|GDK_MOD2_MASK|GDK_MOD3_MASK);
 
-       reset_dpi();
+       UIConfiguration::instance().reset_dpi ();
 
        TimeAxisViewItem::set_constant_heights ();
 
@@ -392,12 +441,12 @@ ARDOUR_UI::ARDOUR_UI (int *argcp, char **argvp[], const char* localedir)
 
        /* Trigger setting up the color scheme and loading the GTK RC file */
 
-       ARDOUR_UI::config()->load_rc_file (false);
-       
+       UIConfiguration::instance().load_rc_file (false);
+
        _process_thread = new ProcessThread ();
        _process_thread->init ();
 
-       DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
+       UIConfiguration::instance().DPIReset.connect (sigc::mem_fun (*this, &ARDOUR_UI::resize_text_widgets));
 
        attach_to_engine ();
 }
@@ -424,16 +473,19 @@ ARDOUR_UI::engine_stopped ()
        ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_stopped)
        ActionManager::set_sensitive (ActionManager::engine_sensitive_actions, false);
        ActionManager::set_sensitive (ActionManager::engine_opposite_sensitive_actions, true);
+       update_sample_rate (0);
+       update_cpu_load ();
 }
 
 void
 ARDOUR_UI::engine_running ()
 {
+       ENSURE_GUI_THREAD (*this, &ARDOUR_UI::engine_running)
        if (first_time_engine_run) {
                post_engine();
                first_time_engine_run = false;
-       } 
-       
+       }
+
        if (_session) {
                _session->reset_xrun_count ();
        }
@@ -442,6 +494,9 @@ ARDOUR_UI::engine_running ()
        update_xrun_count ();
        update_sample_rate (AudioEngine::instance()->sample_rate());
        update_timecode_format ();
+       update_peak_thread_work ();
+       ActionManager::set_sensitive (ActionManager::engine_sensitive_actions, true);
+       ActionManager::set_sensitive (ActionManager::engine_opposite_sensitive_actions, false);
 }
 
 void
@@ -481,7 +536,7 @@ the audio backend and save the session."), PROGRAM_NAME);
        MessageDialog msg (*editor, msgstr);
        pop_back_splash (msg);
        msg.run ();
-       
+
        if (free_reason) {
                free (const_cast<char*> (reason));
        }
@@ -502,7 +557,7 @@ ARDOUR_UI::post_engine ()
 #endif
 
        ARDOUR::init_post_engine ();
-       
+
        /* connect to important signals */
 
        AudioEngine::instance()->Stopped.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::engine_stopped, this), gui_context());
@@ -511,8 +566,6 @@ ARDOUR_UI::post_engine ()
        AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
        AudioEngine::instance()->BecameSilent.connect (forever_connections, MISSING_INVALIDATOR, boost::bind (&ARDOUR_UI::audioengine_became_silent, this), gui_context());
 
-       _tooltips.enable();
-
        if (setup_windows ()) {
                throw failed_constructor ();
        }
@@ -522,7 +575,7 @@ ARDOUR_UI::post_engine ()
        if (n) {
                _status_bar_visibility.set_state (*n);
        }
-       
+
        check_memory_locking();
 
        /* this is the first point at which all the keybindings are available */
@@ -577,13 +630,13 @@ ARDOUR_UI::post_engine ()
                boost::function<void (string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
                Config->map_parameters (pc);
 
-               ui_config->map_parameters (pc);
+               UIConfiguration::instance().map_parameters (pc);
        }
 }
 
 ARDOUR_UI::~ARDOUR_UI ()
 {
-       ui_config->save_state();
+       UIConfiguration::instance().save_state();
 
        stop_video_server();
 
@@ -593,9 +646,23 @@ ARDOUR_UI::~ARDOUR_UI ()
                delete primary_clock;
                delete secondary_clock;
                delete _process_thread;
+               delete meterbridge;
+               delete editor;
+               delete mixer;
+               delete nsm;
                delete gui_object_state;
                FastMeter::flush_pattern_cache ();
+               PixFader::flush_pattern_cache ();
        }
+
+#ifndef NDEBUG
+       /* Small trick to flush main-thread event pool.
+        * Other thread-pools are destroyed at pthread_exit(),
+        * but tmain thread termination is too late to trigger Pool::~Pool()
+        */
+       SessionEvent* ev = new SessionEvent (SessionEvent::SetTransportSpeed, SessionEvent::Clear, SessionEvent::Immediate, 0, 0); // get the pool reference, values don't matter since the event is never queued.
+       delete ev->event_pool();
+#endif
 }
 
 void
@@ -754,19 +821,27 @@ ARDOUR_UI::check_announcements ()
 
 #ifdef __APPLE__
        _annc_filename = PROGRAM_NAME "_announcements_osx_";
+#elif defined PLATFORM_WINDOWS
+       _annc_filename = PROGRAM_NAME "_announcements_windows_";
 #else
        _annc_filename = PROGRAM_NAME "_announcements_linux_";
 #endif
        _annc_filename.append (VERSIONSTRING);
 
+       _announce_string = "";
+
        std::string path = Glib::build_filename (user_config_directory(), _annc_filename);
-       std::ifstream announce_file (path.c_str());
-       if ( announce_file.fail() )
-               _announce_string = "";
-       else {
-               std::stringstream oss;
-               oss << announce_file.rdbuf();
-               _announce_string = oss.str();
+       FILE* fin = g_fopen (path.c_str(), "rb");
+       if (fin) {
+               while (!feof (fin)) {
+                       char tmp[1024];
+                       size_t len;
+                       if ((len = fread (tmp, sizeof(char), 1024, fin)) == 0 || ferror (fin)) {
+                               break;
+                       }
+                       _announce_string.append (tmp, len);
+               }
+               fclose (fin);
        }
 
        pingback (VERSIONSTRING, path);
@@ -792,7 +867,7 @@ ARDOUR_UI::starting ()
        /* we need to create this early because it may need to set the
         *  audio backend end up.
         */
-       
+
        try {
                audio_midi_setup.get (true);
        } catch (...) {
@@ -803,13 +878,16 @@ ARDOUR_UI::starting ()
        if ((nsm_url = g_getenv ("NSM_URL")) != 0) {
                nsm = new NSM_Client;
                if (!nsm->init (nsm_url)) {
-                       /* TODO this needs fixing!
+                       /* the ardour executable may have different names:
+                        *
                         * waf's obj.target for distro versions: eg ardour4, ardourvst4
                         * Ardour4, Mixbus3 for bundled versions + full path on OSX & windows
                         * argv[0] does not apply since we need the wrapper-script (not the binary itself)
-                        * No idea how to address all these.
+                        *
+                        * The wrapper startup script should set the environment variable 'ARDOUR_SELF'
                         */
-                       nsm->announce (PROGRAM_NAME, ":dirty:", "ardour4");
+                       const char *process_name = g_getenv ("ARDOUR_SELF");
+                       nsm->announce (PROGRAM_NAME, ":dirty:", process_name ? process_name : "ardour4");
 
                        unsigned int i = 0;
                        // wait for announce reply from nsm server
@@ -870,12 +948,14 @@ ARDOUR_UI::starting ()
                }
 
        } else  {
-               
+
                if (brand_new_user) {
+                       _initial_verbose_plugin_scan = true;
                        ArdourStartup s;
                        s.present ();
                        main().run();
                        s.hide ();
+                       _initial_verbose_plugin_scan = false;
                        switch (s.response ()) {
                        case Gtk::RESPONSE_OK:
                                break;
@@ -890,16 +970,16 @@ ARDOUR_UI::starting ()
                ARDOUR::read_recent_sessions (rs);
 
                string path = Glib::build_filename (user_config_directory(), ".iknowaboutfreeversion");
-               
+
                if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS) && !rs.empty()) {
-                       
+
                        /* already used Ardour, have sessions ... warn about plugin state */
-                       
+
                        ArdourDialog d (_("Free/Demo Version Warning"), true);
                        Label l;
                        Button b (string_compose (_("Subscribe and support development of %1"), PROGRAM_NAME));
                        CheckButton c (_("Don't warn me about this again"));
-                       
+
                        l.set_markup (string_compose (_("<span weight=\"bold\" size=\"large\">%1</span>\n\n<b>%2</b>\n\n<i>%3</i>\n\n%4"),
                                                      string_compose (_("This is a free/demo version of %1"), PROGRAM_NAME),
                                                      _("It will not restore OR save any plugin settings"),
@@ -908,26 +988,26 @@ ARDOUR_UI::starting ()
                                                      _("To get full access to updates without this limitation\n"
                                                        "consider becoming a subscriber for a low cost every month.")));
                        l.set_justify (JUSTIFY_CENTER);
-                       
+
                        b.signal_clicked().connect (mem_fun(*this, &ARDOUR_UI::launch_subscribe));
-                       
+
                        d.get_vbox()->pack_start (l, true, true);
                        d.get_vbox()->pack_start (b, false, false, 12);
                        d.get_vbox()->pack_start (c, false, false, 12);
-                       
+
                        d.add_button (_("Quit now"), RESPONSE_CANCEL);
                        d.add_button (string_compose (_("Continue using %1"), PROGRAM_NAME), RESPONSE_OK);
-                       
+
                        d.show_all ();
 
                        c.signal_toggled().connect (sigc::hide_return (sigc::bind (sigc::ptr_fun (toggle_file_existence), path)));
-                       
+
                        if (d.run () != RESPONSE_OK) {
                                _exit (0);
                        }
                }
 #endif
-                       
+
                /* go get a session */
 
                const bool new_session_required = (ARDOUR_COMMAND_LINE::new_session || brand_new_user);
@@ -996,7 +1076,7 @@ ARDOUR_UI::check_memory_locking ()
                                                  "runs out of memory. \n\n"
                                                  "You can view the memory limit with 'ulimit -l', "
                                                  "and it is normally controlled by %2"),
-                                               PROGRAM_NAME, 
+                                               PROGRAM_NAME,
 #ifdef __FreeBSD__
                                                X_("/etc/login.conf")
 #else
@@ -1187,6 +1267,7 @@ ARDOUR_UI::every_second ()
        update_buffer_load ();
        update_disk_space ();
        update_timecode_format ();
+       update_peak_thread_work ();
 
        if (nsm && nsm->is_active ()) {
                nsm->check ();
@@ -1214,10 +1295,10 @@ ARDOUR_UI::every_point_zero_something_seconds ()
 {
        // august 2007: actual update frequency: 25Hz (40ms), not 100Hz
 
-       if (editor_meter && ARDOUR_UI::config()->get_show_editor_meter()) {
+       if (editor_meter && UIConfiguration::instance().get_show_editor_meter()) {
                float mpeak = editor_meter->update_meters();
                if (mpeak > editor_meter_max_peak) {
-                       if (mpeak >= ARDOUR_UI::config()->get_meter_peak()) {
+                       if (mpeak >= UIConfiguration::instance().get_meter_peak()) {
                                editor_meter_peak_display.set_active_state ( Gtkmm2ext::ExplicitActive );
                        }
                }
@@ -1323,10 +1404,16 @@ ARDOUR_UI::update_format ()
        case RF64:
                s << _("RF64");
                break;
+       case RF64_WAV:
+               s << _("RF64/WAV");
+               break;
+       case MBWF:
+               s << _("MBWF");
+               break;
        }
 
        s << " ";
-       
+
        switch (_session->config.get_native_file_data_format ()) {
        case FormatFloat:
                s << _("32-float");
@@ -1381,6 +1468,19 @@ ARDOUR_UI::update_cpu_load ()
        cpu_load_label.set_markup (buf);
 }
 
+void
+ARDOUR_UI::update_peak_thread_work ()
+{
+       char buf[64];
+       const int c = SourceFactory::peak_work_queue_length ();
+       if (c > 0) {
+               snprintf (buf, sizeof (buf), _("PkBld: <span foreground=\"%s\">%d</span>"), c >= 2 ? X_("red") : X_("green"), c);
+               peak_thread_work_label.set_markup (buf);
+       } else {
+               peak_thread_work_label.set_markup (X_(""));
+       }
+}
+
 void
 ARDOUR_UI::update_buffer_load ()
 {
@@ -1392,7 +1492,7 @@ ARDOUR_UI::update_buffer_load ()
        /* If this text is changed, the set_size_request_to_display_given_text call in ARDOUR_UI::resize_text_widgets
           should also be changed.
        */
-       
+
        if (_session) {
                snprintf (
                        buf, sizeof (buf),
@@ -1463,9 +1563,9 @@ ARDOUR_UI::update_disk_space()
                        mins = frames / (fr * 60);
                        frames -= mins * fr * 60;
                        secs = frames / fr;
-                       
+
                        bool const low = (hrs == 0 && mins <= 30);
-                       
+
                        snprintf (
                                buf, sizeof(buf),
                                _("Disk: <span foreground=\"%s\">%02dh:%02dm:%02ds</span>"),
@@ -1493,7 +1593,7 @@ ARDOUR_UI::update_timecode_format ()
                } else {
                        matching = true;
                }
-                       
+
                snprintf (buf, sizeof (buf), S_("Timecode|TC: <span foreground=\"%s\">%s</span>"),
                          matching ? X_("green") : X_("red"),
                          Timecode::timecode_format_name (_session->config.get_timecode_format()).c_str());
@@ -1502,7 +1602,7 @@ ARDOUR_UI::update_timecode_format ()
        }
 
        timecode_format_label.set_markup (buf);
-}      
+}
 
 gint
 ARDOUR_UI::update_wall_clock ()
@@ -1523,171 +1623,40 @@ ARDOUR_UI::update_wall_clock ()
        return TRUE;
 }
 
-void
-ARDOUR_UI::redisplay_recent_sessions ()
-{
-       std::vector<std::string> session_directories;
-       RecentSessionsSorter cmp;
-
-       recent_session_display.set_model (Glib::RefPtr<TreeModel>(0));
-       recent_session_model->clear ();
-
-       ARDOUR::RecentSessions rs;
-       ARDOUR::read_recent_sessions (rs);
-
-       if (rs.empty()) {
-               recent_session_display.set_model (recent_session_model);
-               return;
-       }
-
-       // sort them alphabetically
-       sort (rs.begin(), rs.end(), cmp);
-
-       for (ARDOUR::RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
-               session_directories.push_back ((*i).second);
-       }
-
-       for (vector<std::string>::const_iterator i = session_directories.begin();
-                       i != session_directories.end(); ++i)
-       {
-               std::vector<std::string> state_file_paths;
-
-               // now get available states for this session
-
-               get_state_files_in_directory (*i, state_file_paths);
-
-               vector<string> states;
-               vector<const gchar*> item;
-               string fullpath = *i;
-
-               /* remove any trailing / */
-
-               if (fullpath[fullpath.length() - 1] == '/') {
-                       fullpath = fullpath.substr (0, fullpath.length() - 1);
-               }
-
-               /* check whether session still exists */
-               if (!Glib::file_test(fullpath.c_str(), Glib::FILE_TEST_EXISTS)) {
-                       /* session doesn't exist */
-                       continue;
-               }
-
-               /* now get available states for this session */
-               states = Session::possible_states (fullpath);
-
-               if (states.empty()) {
-                       /* no state file? */
-                       continue;
-               }
-
-               std::vector<string> state_file_names(get_file_names_no_extension (state_file_paths));
-
-               Gtk::TreeModel::Row row = *(recent_session_model->append());
-
-               row[recent_session_columns.fullpath] = fullpath;
-               row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
-
-               if (state_file_names.size() > 1) {
-                       // multiple session files in the session directory - show the directory name.
-                       row[recent_session_columns.visible_name] = Glib::path_get_basename (fullpath);
-
-                       // add the children
-                       for (std::vector<std::string>::iterator i2 = state_file_names.begin();
-                                       i2 != state_file_names.end(); ++i2)
-                       {
-
-                               Gtk::TreeModel::Row child_row = *(recent_session_model->append (row.children()));
-
-                               child_row[recent_session_columns.visible_name] = *i2;
-                               child_row[recent_session_columns.fullpath] = fullpath;
-                               child_row[recent_session_columns.tip] = Glib::Markup::escape_text (fullpath);
-                       }
-               } else {
-                       // only a single session file in the directory - show its actual name.
-                       row[recent_session_columns.visible_name] = state_file_names.front ();
-               }
-       }
-
-       recent_session_display.set_tooltip_column(1); // recent_session_columns.tip
-       recent_session_display.set_model (recent_session_model);
-}
-
-void
-ARDOUR_UI::build_session_selector ()
-{
-       session_selector_window = new ArdourDialog (_("Recent Sessions"));
-
-       Gtk::ScrolledWindow *scroller = manage (new Gtk::ScrolledWindow);
-
-       session_selector_window->add_button (Stock::CANCEL, RESPONSE_CANCEL);
-       session_selector_window->add_button (Stock::OPEN, RESPONSE_ACCEPT);
-       session_selector_window->set_default_response (RESPONSE_ACCEPT);
-       recent_session_model = TreeStore::create (recent_session_columns);
-       recent_session_display.set_model (recent_session_model);
-       recent_session_display.append_column (_("Recent Sessions"), recent_session_columns.visible_name);
-       recent_session_display.set_headers_visible (false);
-       recent_session_display.get_selection()->set_mode (SELECTION_BROWSE);
-       recent_session_display.signal_row_activated().connect (sigc::mem_fun (*this, &ARDOUR_UI::recent_session_row_activated));
-
-       scroller->add (recent_session_display);
-       scroller->set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
-
-       session_selector_window->set_name ("SessionSelectorWindow");
-       session_selector_window->set_size_request (200, 400);
-       session_selector_window->get_vbox()->pack_start (*scroller);
-
-       recent_session_display.show();
-       scroller->show();
-}
-
-void
-ARDOUR_UI::recent_session_row_activated (const TreePath& /*path*/, TreeViewColumn* /*col*/)
-{
-       session_selector_window->response (RESPONSE_ACCEPT);
-}
-
 void
 ARDOUR_UI::open_recent_session ()
 {
        bool can_return = (_session != 0);
 
-       if (session_selector_window == 0) {
-               build_session_selector ();
-       }
-
-       redisplay_recent_sessions ();
+       SessionDialog recent_session_dialog;
 
        while (true) {
 
-               ResponseType r = (ResponseType) session_selector_window->run ();
+               ResponseType r = (ResponseType) recent_session_dialog.run ();
 
                switch (r) {
                case RESPONSE_ACCEPT:
                        break;
                default:
                        if (can_return) {
-                               session_selector_window->hide();
+                               recent_session_dialog.hide();
                                return;
                        } else {
                                exit (1);
                        }
                }
 
-               if (recent_session_display.get_selection()->count_selected_rows() == 0) {
-                       continue;
-               }
+               recent_session_dialog.hide();
 
-               session_selector_window->hide();
+               bool should_be_new;
 
-               Gtk::TreeModel::iterator i = recent_session_display.get_selection()->get_selected();
+               std::string path = recent_session_dialog.session_folder();
+               std::string state = recent_session_dialog.session_name (should_be_new);
 
-               if (i == recent_session_model->children().end()) {
-                       return;
+               if (should_be_new == true) {
+                       continue;
                }
 
-               std::string path = (*i)[recent_session_columns.fullpath];
-               std::string state = (*i)[recent_session_columns.visible_name];
-
                _session_is_new = false;
 
                if (load_session (path, state) == 0) {
@@ -1699,10 +1668,10 @@ ARDOUR_UI::open_recent_session ()
 }
 
 bool
-ARDOUR_UI::check_audioengine ()
+ARDOUR_UI::check_audioengine (Gtk::Window& parent)
 {
        if (!AudioEngine::instance()->connected()) {
-               MessageDialog msg (string_compose (
+               MessageDialog msg (parent, string_compose (
                                           _("%1 is not connected to any audio backend.\n"
                                             "You cannot open or close sessions in this condition"),
                                           PROGRAM_NAME));
@@ -1716,60 +1685,47 @@ ARDOUR_UI::check_audioengine ()
 void
 ARDOUR_UI::open_session ()
 {
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
-
        }
 
-       /* popup selector window */
-
-       if (open_session_selector == 0) {
+       /* ardour sessions are folders */
+       Gtk::FileChooserDialog open_session_selector(_("Open Session"), FILE_CHOOSER_ACTION_OPEN);
+       open_session_selector.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+       open_session_selector.add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
+       open_session_selector.set_default_response(Gtk::RESPONSE_ACCEPT);
 
-               /* ardour sessions are folders */
-
-               open_session_selector = new Gtk::FileChooserDialog (_("Open Session"), FILE_CHOOSER_ACTION_OPEN);
-               open_session_selector->add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-               open_session_selector->add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
-               open_session_selector->set_default_response(Gtk::RESPONSE_ACCEPT);
-               
-               if (_session) {
-                       string session_parent_dir = Glib::path_get_dirname(_session->path());
-                       string::size_type last_dir_sep = session_parent_dir.rfind(G_DIR_SEPARATOR);
-                       session_parent_dir = session_parent_dir.substr(0, last_dir_sep);
-                       open_session_selector->set_current_folder(session_parent_dir);
-               } else {
-                       open_session_selector->set_current_folder(Config->get_default_session_parent_dir());
-               }
+       if (_session) {
+               string session_parent_dir = Glib::path_get_dirname(_session->path());
+               open_session_selector.set_current_folder(session_parent_dir);
+       } else {
+               open_session_selector.set_current_folder(Config->get_default_session_parent_dir());
+       }
 
+       Gtkmm2ext::add_volume_shortcuts (open_session_selector);
+       try {
+               /* add_shortcut_folder throws an exception if the folder being added already has a shortcut */
                string default_session_folder = Config->get_default_session_parent_dir();
-               try {
-                       /* add_shortcut_folder throws an exception if the folder being added already has a shortcut */
-                       open_session_selector->add_shortcut_folder (default_session_folder);
-               }
-               catch (Glib::Error & e) {
-                       std::cerr << "open_session_selector->add_shortcut_folder (" << default_session_folder << ") threw Glib::Error " << e.what() << std::endl;
-               }
+               open_session_selector.add_shortcut_folder (default_session_folder);
+       }
+       catch (Glib::Error & e) {
+               std::cerr << "open_session_selector.add_shortcut_folder() threw Glib::Error " << e.what() << std::endl;
+       }
 
-               FileFilter session_filter;
-               session_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::statefile_suffix));
-               session_filter.set_name (string_compose (_("%1 sessions"), PROGRAM_NAME));
-               open_session_selector->add_filter (session_filter);
-               open_session_selector->set_filter (session_filter);
-       }
+       FileFilter session_filter;
+       session_filter.add_pattern (string_compose(X_("*%1"), ARDOUR::statefile_suffix));
+       session_filter.set_name (string_compose (_("%1 sessions"), PROGRAM_NAME));
+       open_session_selector.add_filter (session_filter);
+       open_session_selector.set_filter (session_filter);
 
-       int response = open_session_selector->run();
-       open_session_selector->hide ();
+       int response = open_session_selector.run();
+       open_session_selector.hide ();
 
-       switch (response) {
-       case RESPONSE_ACCEPT:
-               break;
-       default:
-               open_session_selector->hide();
+       if (response == Gtk::RESPONSE_CANCEL) {
                return;
        }
 
-       open_session_selector->hide();
-       string session_path = open_session_selector->get_filename();
+       string session_path = open_session_selector.get_filename();
        string path, name;
        bool isnew;
 
@@ -1783,7 +1739,7 @@ ARDOUR_UI::open_session ()
 
 
 void
-ARDOUR_UI::session_add_mixed_track (const ChanCount& input, const ChanCount& output, RouteGroup* route_group, 
+ARDOUR_UI::session_add_mixed_track (const ChanCount& input, const ChanCount& output, RouteGroup* route_group,
                                    uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
 {
        list<boost::shared_ptr<MidiTrack> > tracks;
@@ -1795,7 +1751,7 @@ ARDOUR_UI::session_add_mixed_track (const ChanCount& input, const ChanCount& out
 
        try {
                tracks = _session->new_midi_track (input, output, instrument, ARDOUR::Normal, route_group, how_many, name_template);
-               
+
                if (tracks.size() != how_many) {
                        error << string_compose(P_("could not create %1 new mixed track", "could not create %1 new mixed tracks", how_many), how_many) << endmsg;
                }
@@ -1810,7 +1766,7 @@ restart with more ports."), PROGRAM_NAME));
                msg.run ();
        }
 }
-       
+
 
 void
 ARDOUR_UI::session_add_midi_route (bool disk, RouteGroup* route_group, uint32_t how_many, const string& name_template, PluginInfoPtr instrument)
@@ -1847,7 +1803,7 @@ ARDOUR_UI::session_add_audio_route (
                        tracks = _session->new_audio_track (input_channels, output_channels, mode, route_group, how_many, name_template);
 
                        if (tracks.size() != how_many) {
-                               error << string_compose (P_("could not create %1 new audio track", "could not create %1 new audio tracks", how_many), how_many) 
+                               error << string_compose (P_("could not create %1 new audio track", "could not create %1 new audio tracks", how_many), how_many)
                                      << endmsg;
                        }
 
@@ -1916,9 +1872,9 @@ ARDOUR_UI::transport_goto_wallclock ()
 
                time (&now);
                localtime_r (&now, &tmnow);
-               
-               int frame_rate = _session->frame_rate();
-               
+
+               framecnt_t frame_rate = _session->frame_rate();
+
                if (frame_rate == 0) {
                        /* no frame rate available */
                        return;
@@ -1974,7 +1930,7 @@ ARDOUR_UI::transport_stop ()
 
 /** Check if any tracks are record enabled. If none are, record enable all of them.
  * @return true if track record-enabled status was changed, false otherwise.
- */  
+ */
 bool
 ARDOUR_UI::trx_record_enable_all_tracks ()
 {
@@ -1997,7 +1953,7 @@ ARDOUR_UI::trx_record_enable_all_tracks ()
 
        if (none_record_enabled) {
                _session->set_record_enabled (rl, true, Session::rt_cleanup);
-       } 
+       }
 
        return none_record_enabled;
 }
@@ -2078,7 +2034,7 @@ ARDOUR_UI::transport_roll ()
                                /* stop loop playback but keep rolling */
                                _session->request_play_loop (false, false);
                        }
-               } 
+               }
 
        } else if (_session->get_play_range () ) {
                /* stop playing a range if we currently are */
@@ -2100,7 +2056,6 @@ ARDOUR_UI::get_smart_mode() const
 void
 ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
 {
-
        if (!_session) {
                return;
        }
@@ -2126,17 +2081,26 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
        if (rolling && roll_out_of_bounded_mode) {
                /* drop out of loop/range playback but leave transport rolling */
                if (_session->get_play_loop()) {
-                       if (Config->get_seamless_loop()) {
-                               /* the disk buffers contain copies of the loop - we can't
-                                  just keep playing, so stop the transport. the user
-                                  can restart as they wish.
-                               */
-                               affect_transport = true;
+                       if (_session->actively_recording()) {
+
+                               /* just stop using the loop, then actually stop
+                                * below
+                                */
+                               _session->request_play_loop (false, affect_transport);
+
                        } else {
-                               /* disk buffers are normal, so we can keep playing */
-                               affect_transport = false;
+                               if (Config->get_seamless_loop()) {
+                                       /* the disk buffers contain copies of the loop - we can't
+                                          just keep playing, so stop the transport. the user
+                                          can restart as they wish.
+                                       */
+                                       affect_transport = true;
+                               } else {
+                                       /* disk buffers are normal, so we can keep playing */
+                                       affect_transport = false;
+                               }
+                               _session->request_play_loop (false, affect_transport);
                        }
-                       _session->request_play_loop (false, affect_transport);
                } else if (_session->get_play_range ()) {
                        affect_transport = false;
                        _session->request_play_range (0, true);
@@ -2146,8 +2110,8 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
        if (affect_transport) {
                if (rolling) {
                        _session->request_stop (with_abort, true);
-               } else {
-                       if (ARDOUR_UI::config()->get_follow_edits() && ( editor->get_selection().time.front().start == _session->transport_frame() ) ) {  //if playhead is exactly at the start of a range, we can assume it was placed there by follow_edits
+               } else if (!_session->config.get_external_sync()) {
+                       if (UIConfiguration::instance().get_follow_edits() && ( editor->get_selection().time.front().start == _session->transport_frame() ) ) {  //if playhead is exactly at the start of a range, we can assume it was placed there by follow_edits
                                _session->request_play_range (&editor->get_selection().time, true);
                                _session->set_requested_return_frame( editor->get_selection().time.front().start );  //force an auto-return here
                        }
@@ -2159,9 +2123,13 @@ ARDOUR_UI::toggle_roll (bool with_abort, bool roll_out_of_bounded_mode)
 void
 ARDOUR_UI::toggle_session_auto_loop ()
 {
+       if (!_session) {
+               return;
+       }
+
        Location * looploc = _session->locations()->auto_loop_location();
 
-       if (!_session || !looploc) {
+       if (!looploc) {
                return;
        }
 
@@ -2185,7 +2153,7 @@ ARDOUR_UI::toggle_session_auto_loop ()
                        _session->request_play_loop (true, true);
                }
        }
-       
+
        //show the loop markers
        looploc->set_hidden (false, this);
 }
@@ -2214,7 +2182,7 @@ ARDOUR_UI::transport_rewind (int option)
 {
        float current_transport_speed;
 
-               if (_session) {
+       if (_session) {
                current_transport_speed = _session->transport_speed();
 
                if (current_transport_speed >= 0.0f) {
@@ -2242,9 +2210,9 @@ ARDOUR_UI::transport_forward (int option)
        if (!_session) {
                return;
        }
-       
+
        float current_transport_speed = _session->transport_speed();
-       
+
        if (current_transport_speed <= 0.0f) {
                switch (option) {
                case 0:
@@ -2324,7 +2292,7 @@ ARDOUR_UI::map_transport_state ()
                        auto_loop_button.set_active (false);
                }
 
-               if (ARDOUR_UI::config()->get_follow_edits()) {
+               if (UIConfiguration::instance().get_follow_edits() && !_session->config.get_external_sync()) {
                        /* light up both roll and play-selection if they are joined */
                        roll_button.set_active (true);
                        play_selection_button.set_active (true);
@@ -2370,7 +2338,7 @@ ARDOUR_UI::update_clocks ()
 void
 ARDOUR_UI::start_clocking ()
 {
-       if (ui_config->get_super_rapid_clock_update()) {
+       if (UIConfiguration::instance().get_super_rapid_clock_update()) {
                clock_signal_connection = Timers::fps_connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
        } else {
                clock_signal_connection = Timers::rapid_connect (sigc::mem_fun(*this, &ARDOUR_UI::update_clocks));
@@ -2394,7 +2362,7 @@ ARDOUR_UI::save_as_progress_update (float fraction, int64_t cnt, int64_t total,
        bar->set_fraction (fraction);
 
        /* process events, redraws, etc. */
-       
+
        while (gtk_events_pending()) {
                gtk_main_iteration ();
        }
@@ -2411,58 +2379,116 @@ ARDOUR_UI::save_session_as ()
 
        if (!save_as_dialog) {
                save_as_dialog = new SaveAsDialog;
-       } else {
-               save_as_dialog->clear_name ();
        }
 
+       save_as_dialog->set_name (_session->name());
+
        int response = save_as_dialog->run ();
 
        save_as_dialog->hide ();
-       
+
        switch (response) {
        case Gtk::RESPONSE_OK:
                break;
        default:
                return;
        }
-       
-       ArdourDialog progress_dialog(_("Save As"), true);
-       Gtk::Label label;
-       Gtk::ProgressBar progress_bar;
 
-       progress_dialog.get_vbox()->pack_start (label);
-       progress_dialog.get_vbox()->pack_start (progress_bar);
-       label.show ();
-       progress_bar.show ();
-       
-       Session::SaveAs sa;
+
+       Session::SaveAs sa;
 
        sa.new_parent_folder = save_as_dialog->new_parent_folder ();
        sa.new_name = save_as_dialog->new_name ();
        sa.switch_to = save_as_dialog->switch_to();
        sa.copy_media = save_as_dialog->copy_media();
        sa.copy_external = save_as_dialog->copy_external();
+       sa.include_media = save_as_dialog->include_media ();
 
-       /* this signal will be emitted from within this, the calling thread,
-        * after every file is copied. It provides information on percentage
-        * complete (in terms of total data to copy), the number of files
-        * copied so far, and the total number to copy.
-        */
+       /* Only bother with a progress dialog if we're going to copy
+          media into the save-as target. Without that choice, this
+          will be very fast because we're only talking about a few kB's to
+          perhaps a couple of MB's of data.
+       */
+
+       ArdourDialog progress_dialog (_("Save As"), true);
+
+       if (sa.include_media && sa.copy_media) {
+
+               Gtk::Label label;
+               Gtk::ProgressBar progress_bar;
+
+               progress_dialog.get_vbox()->pack_start (label);
+               progress_dialog.get_vbox()->pack_start (progress_bar);
+               label.show ();
+               progress_bar.show ();
+
+               /* this signal will be emitted from within this, the calling thread,
+                * after every file is copied. It provides information on percentage
+                * complete (in terms of total data to copy), the number of files
+                * copied so far, and the total number to copy.
+                */
+
+               ScopedConnection c;
 
-       ScopedConnection c;
+               sa.Progress.connect_same_thread (c, boost::bind (&ARDOUR_UI::save_as_progress_update, this, _1, _2, _3, &label, &progress_bar));
+
+               progress_dialog.show_all ();
+               progress_dialog.present ();
+       }
 
-       sa.Progress.connect_same_thread (c, boost::bind (&ARDOUR_UI::save_as_progress_update, this, _1, _2, _3, &label, &progress_bar));
-       
-       progress_dialog.show_all ();
-       progress_dialog.present ();
-       
        if (_session->save_as (sa)) {
                /* ERROR MESSAGE */
                MessageDialog msg (string_compose (_("Save As failed: %1"), sa.failure_message));
                msg.run ();
        }
+
+       if (!sa.include_media) {
+               unload_session (false);
+               load_session (sa.final_session_folder_name, sa.new_name);
+       }
 }
 
+bool
+ARDOUR_UI::process_snapshot_session_prompter (ArdourPrompter& prompter, bool switch_to_it)
+{
+       string snapname;
+
+       prompter.get_result (snapname);
+
+       bool do_save = (snapname.length() != 0);
+
+       if (do_save) {
+               char illegal = Session::session_name_is_legal(snapname);
+               if (illegal) {
+                       MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
+                                            "snapshot names may not contain a '%1' character"), illegal));
+                       msg.run ();
+                       return false;
+               }
+       }
+
+       vector<std::string> p;
+       get_state_files_in_directory (_session->session_directory().root_path(), p);
+       vector<string> n = get_file_names_no_extension (p);
+
+       if (find (n.begin(), n.end(), snapname) != n.end()) {
+
+               do_save = overwrite_file_dialog (prompter,
+                                                _("Confirm Snapshot Overwrite"),
+                                                _("A snapshot already exists with that name. Do you want to overwrite it?"));
+       }
+
+       if (do_save) {
+               save_state (snapname, switch_to_it);
+       }
+       else {
+               return false;
+       }
+
+       return true;
+}
+
+
 /** Ask the user for the name of a new snapshot and then take it.
  */
 
@@ -2470,7 +2496,6 @@ void
 ARDOUR_UI::snapshot_session (bool switch_to_it)
 {
        ArdourPrompter prompter (true);
-       string snapname;
 
        prompter.set_name ("Prompter");
        prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
@@ -2493,49 +2518,19 @@ ARDOUR_UI::snapshot_session (bool switch_to_it)
                prompter.set_initial_text (timebuf);
        }
 
-  again:
-       switch (prompter.run()) {
-       case RESPONSE_ACCEPT:
-       {
-               prompter.get_result (snapname);
-
-               bool do_save = (snapname.length() != 0);
-
-               if (do_save) {
-                       char illegal = Session::session_name_is_legal(snapname);
-                       if (illegal) {
-                               MessageDialog msg (string_compose (_("To ensure compatibility with various systems\n"
-                                                    "snapshot names may not contain a '%1' character"), illegal));
-                               msg.run ();
-                               goto again;
-                       }
-               }
-
-               vector<std::string> p;
-               get_state_files_in_directory (_session->session_directory().root_path(), p);
-               vector<string> n = get_file_names_no_extension (p);
-               if (find (n.begin(), n.end(), snapname) != n.end()) {
-
-                       ArdourDialog confirm (_("Confirm Snapshot Overwrite"), true);
-                       Label m (_("A snapshot already exists with that name.  Do you want to overwrite it?"));
-                       confirm.get_vbox()->pack_start (m, true, true);
-                       confirm.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
-                       confirm.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
-                       confirm.show_all ();
-                       switch (confirm.run()) {
-                       case RESPONSE_CANCEL:
-                               do_save = false;
-                       }
+       bool finished = false;
+       while (!finished) {
+               switch (prompter.run()) {
+               case RESPONSE_ACCEPT:
+               {
+                       finished = process_snapshot_session_prompter (prompter, switch_to_it);
+                       break;
                }
 
-               if (do_save) {
-                       save_state (snapname, switch_to_it);
+               default:
+                       finished = true;
+                       break;
                }
-               break;
-       }
-
-       default:
-               break;
        }
 }
 
@@ -2593,7 +2588,7 @@ ARDOUR_UI::rename_session ()
                        }
                        }
                }
-               
+
                break;
        }
 
@@ -2605,6 +2600,10 @@ ARDOUR_UI::rename_session ()
 void
 ARDOUR_UI::save_state (const string & name, bool switch_to_it)
 {
+       if (!_session || _session->deletion_in_progress()) {
+               return;
+       }
+
        XMLNode* node = new XMLNode (X_("UI"));
 
        WM::Manager::instance().add_state (*node);
@@ -2626,10 +2625,6 @@ ARDOUR_UI::save_state_canfail (string name, bool switch_to_it)
        if (_session) {
                int ret;
 
-               if (name.length() == 0) {
-                       name = _session->snap_name();
-               }
-
                if ((ret = _session->save_state (name, false, switch_to_it)) != 0) {
                        return ret;
                }
@@ -2690,13 +2685,39 @@ ARDOUR_UI::transport_rec_enable_blink (bool onoff)
        }
 }
 
+bool
+ARDOUR_UI::process_save_template_prompter (ArdourPrompter& prompter)
+{
+       string name;
+
+       prompter.get_result (name);
+
+       if (name.length()) {
+               int failed = _session->save_template (name);
+
+               if (failed == -2) { /* file already exists. */
+                       bool overwrite = overwrite_file_dialog (prompter,
+                                                               _("Confirm Template Overwrite"),
+                                                               _("A template already exists with that name. Do you want to overwrite it?"));
+
+                       if (overwrite) {
+                               _session->save_template (name, true);
+                       }
+                       else {
+                               return false;
+                       }
+               }
+       }
+
+       return true;
+}
+
 void
 ARDOUR_UI::save_template ()
 {
        ArdourPrompter prompter (true);
-       string name;
 
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
        }
 
@@ -2706,17 +2727,17 @@ ARDOUR_UI::save_template ()
        prompter.set_initial_text(_session->name() + _("-template"));
        prompter.add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
 
-       switch (prompter.run()) {
-       case RESPONSE_ACCEPT:
-               prompter.get_result (name);
+       bool finished = false;
+       while (!finished) {
+               switch (prompter.run()) {
+               case RESPONSE_ACCEPT:
+                       finished = process_save_template_prompter (prompter);
+                       break;
 
-               if (name.length()) {
-                       _session->save_template (name);
+               default:
+                       finished = true;
+                       break;
                }
-               break;
-
-       default:
-               break;
        }
 }
 
@@ -2816,14 +2837,49 @@ ARDOUR_UI::build_session_from_dialog (SessionDialog& sd, const std::string& sess
 void
 ARDOUR_UI::load_from_application_api (const std::string& path)
 {
+       printf("ARDOUR_UI::load_from_application_api\n");
        ARDOUR_COMMAND_LINE::session_name = path;
+       /* Cancel SessionDialog if it's visible to make OSX delegates work.
+        *
+        * ARDOUR_UI::starting connects app->ShouldLoad signal and then shows a SessionDialog
+        * race-condition:
+        *  - ShouldLoad does not arrive in time, ARDOUR_COMMAND_LINE::session_name is empty:
+        *    -> ARDOUR_UI::get_session_parameters starts a SessionDialog.
+        *  - ShouldLoad signal arrives, this function is called and sets ARDOUR_COMMAND_LINE::session_name
+        *    -> SessionDialog is not displayed
+        */
 
+       if (_session_dialog) {
+               std::string session_name = basename_nosuffix (ARDOUR_COMMAND_LINE::session_name);
+               std::string session_path = path;
+               if (Glib::file_test (session_path, Glib::FILE_TEST_IS_REGULAR)) {
+                       session_path = Glib::path_get_dirname (session_path);
+               }
+               // signal the existing dialog in ARDOUR_UI::get_session_parameters()
+               _session_dialog->set_provided_session (session_name, session_path);
+               _session_dialog->response (RESPONSE_NONE);
+               _session_dialog->hide();
+               return;
+       }
+
+       int rv;
        if (Glib::file_test (path, Glib::FILE_TEST_IS_DIR)) {
                /* /path/to/foo => /path/to/foo, foo */
-               load_session (path, basename_nosuffix (path));
+               rv = load_session (path, basename_nosuffix (path));
        } else {
                /* /path/to/foo/foo.ardour => /path/to/foo, foo */
-               load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
+               rv =load_session (Glib::path_get_dirname (path), basename_nosuffix (path));
+       }
+
+       // if load_session fails -> pop up SessionDialog.
+       if (rv) {
+               ARDOUR_COMMAND_LINE::session_name = "";
+
+               if (get_session_parameters (true, false)) {
+                       exit (1);
+               }
+
+               goto_editor_window ();
        }
 }
 
@@ -2839,7 +2895,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
        bool cancel_not_quit;
 
        /* deal with any existing DIRTY session now, rather than later. don't
-        * treat a non-dirty session this way, so that it stays visible 
+        * treat a non-dirty session this way, so that it stays visible
         * as we bring up the new session dialog.
         */
 
@@ -2867,7 +2923,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
 
        session_name = basename_nosuffix (ARDOUR_COMMAND_LINE::session_name);
        session_path = ARDOUR_COMMAND_LINE::session_name;
-       
+
        if (!session_path.empty()) {
                if (Glib::file_test (session_path.c_str(), Glib::FILE_TEST_EXISTS)) {
                        if (Glib::file_test (session_path.c_str(), Glib::FILE_TEST_IS_REGULAR)) {
@@ -2879,6 +2935,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
 
        SessionDialog session_dialog (should_be_new, session_name, session_path, load_template, cancel_not_quit);
 
+       _session_dialog = &session_dialog;
        while (ret != 0) {
 
                if (!ARDOUR_COMMAND_LINE::session_name.empty()) {
@@ -2903,7 +2960,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                        session_name = "";
                        session_dialog.clear_given ();
                }
-               
+
                if (should_be_new || session_name.empty()) {
                        /* need the dialog to get info from user */
 
@@ -2912,6 +2969,10 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                        switch (session_dialog.run()) {
                        case RESPONSE_ACCEPT:
                                break;
+                       case RESPONSE_NONE:
+                               /* this is used for async * app->ShouldLoad(). */
+                               continue; // while loop
+                               break;
                        default:
                                if (quit_on_cancel) {
                                        // JE - Currently (July 2014) this section can only get reached if the
@@ -2931,9 +2992,9 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                }
 
                /* if we run the startup dialog again, offer more than just "new session" */
-               
+
                should_be_new = false;
-               
+
                session_name = session_dialog.session_name (likely_new);
                session_path = session_dialog.session_folder ();
 
@@ -2942,22 +3003,22 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                }
 
                string::size_type suffix = session_name.find (statefile_suffix);
-               
+
                if (suffix != string::npos) {
                        session_name = session_name.substr (0, suffix);
                }
-               
+
                /* this shouldn't happen, but we catch it just in case it does */
-               
+
                if (session_name.empty()) {
                        continue;
                }
-               
+
                if (session_dialog.use_session_template()) {
                        template_name = session_dialog.session_template_name();
                        _session_is_new = true;
                }
-               
+
                if (session_name[0] == G_DIR_SEPARATOR ||
 #ifdef PLATFORM_WINDOWS
                    (session_name.length() > 3 && session_name[1] == ':' && session_name[2] == G_DIR_SEPARATOR)
@@ -2967,20 +3028,20 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
 #endif
                         )
                {
-                       
+
                        /* absolute path or cwd-relative path specified for session name: infer session folder
                           from what was given.
                        */
-                       
+
                        session_path = Glib::path_get_dirname (session_name);
                        session_name = Glib::path_get_basename (session_name);
-                       
+
                } else {
 
                        session_path = session_dialog.session_folder();
-                       
+
                        char illegal = Session::session_name_is_legal (session_name);
-                       
+
                        if (illegal) {
                                MessageDialog msg (session_dialog,
                                                   string_compose (_("To ensure compatibility with various systems\n"
@@ -2991,7 +3052,7 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                                continue;
                        }
                }
-       
+
                if (Glib::file_test (session_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
 
 
@@ -3057,13 +3118,15 @@ ARDOUR_UI::get_session_parameters (bool quit_on_cancel, bool should_be_new, stri
                }
        }
 
+       _session_dialog = NULL;
+
        return ret;
 }
 
 void
 ARDOUR_UI::close_session()
 {
-       if (!check_audioengine()) {
+       if (!check_audioengine(*editor)) {
                return;
        }
 
@@ -3092,7 +3155,7 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
 
        if (_session) {
                unload_status = unload_session ();
-               
+
                if (unload_status < 0) {
                        goto out;
                } else if (unload_status > 0) {
@@ -3136,7 +3199,26 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
                }
                goto out;
        }
+       catch (SessionException e) {
+               MessageDialog msg (string_compose(
+                                          _("Session \"%1 (snapshot %2)\" did not load successfully: %3"),
+                                          path, snap_name, e.what()),
+                                  true,
+                                  Gtk::MESSAGE_INFO,
+                                  BUTTONS_OK);
 
+               msg.set_title (_("Loading Error"));
+               msg.set_position (Gtk::WIN_POS_CENTER);
+               pop_back_splash (msg);
+               msg.present ();
+
+               dump_errors (cerr);
+
+               (void) msg.run ();
+               msg.hide ();
+
+               goto out;
+       }
        catch (...) {
 
                MessageDialog msg (string_compose(
@@ -3150,6 +3232,9 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
                msg.set_position (Gtk::WIN_POS_CENTER);
                pop_back_splash (msg);
                msg.present ();
+
+               dump_errors (cerr);
+
                (void) msg.run ();
                msg.hide ();
 
@@ -3177,7 +3262,7 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
                (void) msg.run ();
                msg.hide ();
        }
-       
+
 
        /* Now the session been created, add the transport controls */
        new_session->add_controllable(roll_controllable);
@@ -3202,7 +3287,10 @@ ARDOUR_UI::load_session (const std::string& path, const std::string& snap_name,
        fst_stop_threading();
 #endif
 
-       flush_pending ();
+       {
+               Timers::TimerSuspender t;
+               flush_pending ();
+       }
 
 #ifdef WINDOWS_VST_SUPPORT
        fst_start_threading();
@@ -3234,9 +3322,20 @@ ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name,
                new_session = new Session (*AudioEngine::instance(), path, snap_name, &bus_profile);
        }
 
+       catch (SessionException e) {
+               dump_errors (cerr);
+               MessageDialog msg (string_compose(_("Could not create session in \"%1\": %2"), path, e.what()));
+               msg.set_title (_("Loading Error"));
+               msg.set_position (Gtk::WIN_POS_CENTER);
+               pop_back_splash (msg);
+               msg.run ();
+               return -1;
+       }
        catch (...) {
-
+               dump_errors (cerr);
                MessageDialog msg (string_compose(_("Could not create session in \"%1\""), path));
+               msg.set_title (_("Loading Error"));
+               msg.set_position (Gtk::WIN_POS_CENTER);
                pop_back_splash (msg);
                msg.run ();
                return -1;
@@ -3274,13 +3373,24 @@ ARDOUR_UI::build_session (const std::string& path, const std::string& snap_name,
 void
 ARDOUR_UI::launch_chat ()
 {
+       MessageDialog dialog(_("<b>Just ask and wait for an answer.\nIt may take from minutes to hours.</b>"), true);
+
+       dialog.set_title (_("About the Chat"));
+       dialog.set_secondary_text (_("When you're inside the chat just ask your question and wait for an answer. The chat is occupied by real people with real lives so many of them are passively online and might not read your question before minutes or hours later.\nSo please be patient and wait for an answer.\n\nYou should just leave the chat window open and check back regularly until someone has answered your question."));
+
+       switch (dialog.run()) {
+       case RESPONSE_OK:
 #ifdef __APPLE__
-       open_uri("http://webchat.freenode.net/?channels=ardour-osx");
+               open_uri("http://webchat.freenode.net/?channels=ardour-osx");
 #elif defined PLATFORM_WINDOWS
-       open_uri("http://webchat.freenode.net/?channels=ardour-windows");
+               open_uri("http://webchat.freenode.net/?channels=ardour-windows");
 #else
-       open_uri("http://webchat.freenode.net/?channels=ardour");
+               open_uri("http://webchat.freenode.net/?channels=ardour");
 #endif
+               break;
+       default:
+               break;
+       }
 }
 
 void
@@ -3298,7 +3408,7 @@ ARDOUR_UI::launch_reference ()
 void
 ARDOUR_UI::launch_tracker ()
 {
-       PBD::open_uri ("http://tracker.ardour.org/bug_report_page.php");
+       PBD::open_uri ("http://tracker.ardour.org");
 }
 
 void
@@ -3465,7 +3575,7 @@ The following file was deleted from %2,\n\
 releasing %3 %4bytes of disk space", "\
 The following %1 files were deleted from %2,\n\
 releasing %3 %4bytes of disk space", removed),
-                                       removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
+                                       removed, Gtkmm2ext::markup_escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
        } else {
                txt.set_markup (string_compose (P_("\
 The following file was not in use and \n\
@@ -3478,7 +3588,7 @@ have been moved to: %2\n\n\
 After a restart of %5\n\n\
 <span face=\"mono\">Session -> Clean-up -> Flush Wastebasket</span>\n\n\
 will release an additional %3 %4bytes of disk space.\n", removed),
-                                       removed, Glib::Markup::escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
+                                       removed, Gtkmm2ext::markup_escape_text (dead_directory), space_adjusted, bprefix, PROGRAM_NAME));
        }
 
        dhbox.pack_start (*dimage, true, false, 5);
@@ -3592,6 +3702,47 @@ ARDOUR_UI::flush_trash ()
        display_cleanup_results (rep, _("deleted file"), true);
 }
 
+void
+ARDOUR_UI::cleanup_peakfiles ()
+{
+       if (_session == 0) {
+               /* shouldn't happen: menu item is insensitive */
+               return;
+       }
+
+       if (! _session->can_cleanup_peakfiles ()) {
+               return;
+       }
+
+       // get all region-views in this session
+       RegionSelection rs;
+       TrackViewList empty;
+       empty.clear();
+       editor->get_regions_after(rs, (framepos_t) 0, empty);
+       std::list<RegionView*> views = rs.by_layer();
+
+       // remove displayed audio-region-views waveforms
+       for (list<RegionView*>::iterator i = views.begin(); i != views.end(); ++i) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
+               if (!arv) { continue ; }
+               arv->delete_waves();
+       }
+
+       // cleanup peak files:
+       // - stop pending peakfile threads
+       // - close peakfiles if any
+       // - remove peak dir in session
+       // - setup peakfiles (background thread)
+       _session->cleanup_peakfiles ();
+
+       // re-add waves to ARV
+       for (list<RegionView*>::iterator i = views.begin(); i != views.end(); ++i) {
+               AudioRegionView* arv = dynamic_cast<AudioRegionView*> (*i);
+               if (!arv) { continue ; }
+               arv->create_waves();
+       }
+}
+
 void
 ARDOUR_UI::setup_order_hint (AddRouteDialog::InsertAt place)
 {
@@ -3600,12 +3751,12 @@ ARDOUR_UI::setup_order_hint (AddRouteDialog::InsertAt place)
        if (editor->get_selection().tracks.empty()) {
                return;
        }
-       
+
        /*
-         we want the new routes to have their order keys set starting from 
+         we want the new routes to have their order keys set starting from
          the highest order key in the selection + 1 (if available).
        */
-       
+
        if (place == AddRouteDialog::AfterSelection) {
                RouteTimeAxisView *rtav = dynamic_cast<RouteTimeAxisView*> (editor->get_selection().tracks.back());
                if (rtav) {
@@ -3647,6 +3798,20 @@ ARDOUR_UI::setup_order_hint (AddRouteDialog::InsertAt place)
        }
 }
 
+void
+ARDOUR_UI::start_duplicate_routes ()
+{
+       if (!duplicate_routes_dialog) {
+               duplicate_routes_dialog = new DuplicateRouteDialog;
+       }
+
+       if (duplicate_routes_dialog->restart (_session)) {
+               return;
+       }
+
+       duplicate_routes_dialog->present ();
+}
+
 void
 ARDOUR_UI::add_route (Gtk::Window* /* ignored */)
 {
@@ -3835,7 +4000,7 @@ ARDOUR_UI::start_video_server (Gtk::Window* float_window, bool popup_msg)
                        Config->set_video_advanced_setup(false);
                } else {
                        std::ostringstream osstream;
-                       osstream << "http://localhost:" << video_server_dialog->get_listenport() << "/";
+                       osstream << "http://127.0.0.1:" << video_server_dialog->get_listenport() << "/";
                        Config->set_video_server_url(osstream.str());
                        Config->set_video_server_docroot(icsd_docroot);
                        Config->set_video_advanced_setup(true);
@@ -3910,6 +4075,9 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                return;
        }
 
+       std::string audio_from_video;
+       bool detect_ltc = false;
+
        switch (add_video_dialog->import_option()) {
                case VTL_IMPORT_TRANSCODE:
                        {
@@ -3921,9 +4089,15 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                                        delete transcode_video_dialog;
                                        return;
                                }
-                               if (!transcode_video_dialog->get_audiofile().empty()) {
+
+                               audio_from_video = transcode_video_dialog->get_audiofile();
+
+                               if (!audio_from_video.empty() && transcode_video_dialog->detect_ltc()) {
+                                       detect_ltc = true;
+                               }
+                               else if (!audio_from_video.empty()) {
                                        editor->embed_audio_from_video(
-                                                       transcode_video_dialog->get_audiofile(),
+                                                       audio_from_video,
                                                        video_timeline->get_offset(),
                                                        (transcode_video_dialog->import_option() != VTL_IMPORT_NO_VIDEO)
                                                        );
@@ -3956,6 +4130,7 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
        }
 
        video_timeline->set_update_session_fps(auto_set_session_fps);
+
        if (video_timeline->video_file_info(path, local_file)) {
                XMLNode* node = new XMLNode(X_("Videotimeline"));
                node->add_property (X_("Filename"), path);
@@ -3969,6 +4144,40 @@ ARDOUR_UI::add_video (Gtk::Window* float_window)
                _session->add_extra_xml (*node);
                _session->set_dirty ();
 
+               if (!audio_from_video.empty() && detect_ltc) {
+                       std::vector<LTCFileReader::LTCMap> ltc_seq;
+
+                       try {
+                               /* TODO ask user about TV standard (LTC alignment if any) */
+                               LTCFileReader ltcr (audio_from_video, video_timeline->get_video_file_fps());
+                               /* TODO ASK user which channel:  0 .. ltcr->channels() - 1 */
+
+                               ltc_seq = ltcr.read_ltc (/*channel*/ 0, /*max LTC frames to decode*/ 15);
+
+                               /* TODO seek near end of file, and read LTC until end.
+                                * if it fails to find any LTC frames, scan complete file
+                                *
+                                * calculate drift of LTC compared to video-duration,
+                                * ask user for reference (timecode from start/mid/end)
+                                */
+                       } catch (...) {
+                               // LTCFileReader will have written error messages
+                       }
+
+                       ::g_unlink(audio_from_video.c_str());
+
+                       if (ltc_seq.size() == 0) {
+                               PBD::error << _("No LTC detected, video will not be aligned.") << endmsg;
+                       } else {
+                               /* the very first TC in the file is somteimes not aligned properly */
+                               int i = ltc_seq.size() -1;
+                               ARDOUR::frameoffset_t video_start_offset =
+                                       _session->nominal_frame_rate() * (ltc_seq[i].timecode_sec - ltc_seq[i].framepos_sec);
+                               PBD::info << string_compose (_("Align video-start to %1 [samples]"), video_start_offset) << endmsg;
+                               video_timeline->set_offset(video_start_offset);
+                       }
+               }
+
                _session->maybe_update_session_range(
                        std::max(video_timeline->get_offset(), (ARDOUR::frameoffset_t) 0),
                        std::max(video_timeline->get_offset() + video_timeline->get_duration(), (ARDOUR::frameoffset_t) 0));
@@ -4152,6 +4361,7 @@ quickly enough to keep up with recording.\n"), PROGRAM_NAME));
 static MessageDialog *scan_dlg = NULL;
 static ProgressBar   *scan_pbar = NULL;
 static HBox          *scan_tbox = NULL;
+static Gtk::Button   *scan_timeout_button;
 
 void
 ARDOUR_UI::cancel_plugin_scan ()
@@ -4163,7 +4373,7 @@ void
 ARDOUR_UI::cancel_plugin_timeout ()
 {
        PluginManager::instance().cancel_plugin_timeout();
-       scan_tbox->hide();
+       scan_timeout_button->set_sensitive (false);
 }
 
 void
@@ -4173,10 +4383,13 @@ ARDOUR_UI::plugin_scan_timeout (int timeout)
                return;
        }
        if (timeout > 0) {
+               scan_pbar->set_sensitive (false);
+               scan_timeout_button->set_sensitive (true);
                scan_pbar->set_fraction ((float) timeout / (float) Config->get_vst_scan_timeout());
                scan_tbox->show();
        } else {
-               scan_tbox->hide();
+               scan_pbar->set_sensitive (false);
+               scan_timeout_button->set_sensitive (false);
        }
        gui_idle_handler();
 }
@@ -4189,7 +4402,7 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
        }
 
        const bool cancelled = PluginManager::instance().cancelled();
-       if (type != X_("closeme") && !ui_config->get_show_plugin_scan_window()) {
+       if (type != X_("closeme") && (!UIConfiguration::instance().get_show_plugin_scan_window()) && !_initial_verbose_plugin_scan) {
                if (cancelled && scan_dlg->is_mapped()) {
                        scan_dlg->hide();
                        gui_idle_handler();
@@ -4201,7 +4414,6 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
        }
 
        static Gtk::Button *cancel_button;
-       static Gtk::Button *timeout_button;
        if (!scan_dlg) {
                scan_dlg = new MessageDialog("", false, MESSAGE_INFO, BUTTONS_NONE); // TODO manage
                VBox* vbox = scan_dlg->get_vbox();
@@ -4217,10 +4429,10 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
 
                scan_tbox = manage( new HBox() );
 
-               timeout_button = manage(new Gtk::Button(_("Stop Timeout")));
-               timeout_button->set_name ("EditorGTKButton");
-               timeout_button->signal_clicked().connect ( mem_fun (*this, &ARDOUR_UI::cancel_plugin_timeout) );
-               timeout_button->show();
+               scan_timeout_button = manage(new Gtk::Button(_("Stop Timeout")));
+               scan_timeout_button->set_name ("EditorGTKButton");
+               scan_timeout_button->signal_clicked().connect ( mem_fun (*this, &ARDOUR_UI::cancel_plugin_timeout) );
+               scan_timeout_button->show();
 
                scan_pbar = manage(new ProgressBar());
                scan_pbar->set_orientation(Gtk::PROGRESS_RIGHT_TO_LEFT);
@@ -4228,7 +4440,7 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
                scan_pbar->show();
 
                scan_tbox->pack_start (*scan_pbar, PACK_EXPAND_WIDGET, 4);
-               scan_tbox->pack_start (*timeout_button, PACK_SHRINK, 4);
+               scan_tbox->pack_start (*scan_timeout_button, PACK_SHRINK, 4);
 
                scan_dlg->get_vbox()->pack_start (*scan_tbox, PACK_SHRINK, 4);
        }
@@ -4236,13 +4448,14 @@ ARDOUR_UI::plugin_scan_dialog (std::string type, std::string plugin, bool can_ca
        assert(scan_dlg && scan_tbox && cancel_button);
 
        if (type == X_("closeme")) {
+               scan_tbox->hide();
                scan_dlg->hide();
        } else {
                scan_dlg->set_message(type + ": " + Glib::path_get_basename(plugin));
                scan_dlg->show();
        }
        if (!can_cancel || !cancelled) {
-               scan_tbox->hide();
+               scan_timeout_button->set_sensitive(false);
        }
        cancel_button->set_sensitive(can_cancel && !cancelled);
 
@@ -4339,7 +4552,7 @@ what you would like to do.\n"), PROGRAM_NAME));
 int
 ARDOUR_UI::sr_mismatch_dialog (framecnt_t desired, framecnt_t actual)
 {
-       HBox* hbox = new HBox();
+       HBox* hbox = new HBox();
        Image* image = new Image (Stock::DIALOG_WARNING, ICON_SIZE_DIALOG);
        ArdourDialog dialog (_("Sample Rate Mismatch"), true);
        Label  message (string_compose (_("\
@@ -4369,46 +4582,6 @@ audio may be played at the wrong sample rate.\n"), desired, PROGRAM_NAME, actual
         return 1;
 }
 
-int
-ARDOUR_UI::disconnect_from_engine ()
-{
-       /* drop connection to AudioEngine::Halted so that we don't act
-        *  as if the engine unexpectedly shut down
-        */
-
-       halt_connection.disconnect ();
-       
-       if (AudioEngine::instance()->stop ()) {
-               MessageDialog msg (*editor, _("Could not disconnect from Audio/MIDI engine"));
-               msg.run ();
-               return -1;
-       } else {
-               AudioEngine::instance()->Halted.connect_same_thread (halt_connection, boost::bind (&ARDOUR_UI::engine_halted, this, _1, false));
-       }
-       
-       update_sample_rate (0);
-       return 0;
-}
-
-int
-ARDOUR_UI::reconnect_to_engine ()
-{
-       if (AudioEngine::instance()->start ()) {
-               // TODO somehow make this the topmost window (above any dialogs currently visible)
-               if (editor) {
-                       MessageDialog msg (*editor,  _("Could not reconnect to the Audio/MIDI engine"));
-                       msg.run ();
-               } else {
-                       MessageDialog msg (_("Could not reconnect to the Audio/MIDI engine"));
-                       msg.run ();
-               }
-               return -1;
-       }
-       
-       update_sample_rate (0);
-       return 0;
-}
-
 void
 ARDOUR_UI::use_config ()
 {
@@ -4421,13 +4594,13 @@ ARDOUR_UI::use_config ()
 void
 ARDOUR_UI::update_transport_clocks (framepos_t pos)
 {
-       if (ui_config->get_primary_clock_delta_edit_cursor()) {
+       if (UIConfiguration::instance().get_primary_clock_delta_edit_cursor()) {
                primary_clock->set (pos, false, editor->get_preferred_edit_position (EDIT_IGNORE_PHEAD));
        } else {
                primary_clock->set (pos);
        }
 
-       if (ui_config->get_secondary_clock_delta_edit_cursor()) {
+       if (UIConfiguration::instance().get_secondary_clock_delta_edit_cursor()) {
                secondary_clock->set (pos, false, editor->get_preferred_edit_position (EDIT_IGNORE_PHEAD));
        } else {
                secondary_clock->set (pos);
@@ -4493,7 +4666,7 @@ ARDOUR_UI::store_clock_modes ()
 
        for (vector<AudioClock*>::iterator x = AudioClock::clocks.begin(); x != AudioClock::clocks.end(); ++x) {
                XMLNode* child = new XMLNode (X_("Clock"));
-               
+
                child->add_property (X_("name"), (*x)->name());
                child->add_property (X_("mode"), enum_2_string ((*x)->mode()));
                child->add_property (X_("on"), ((*x)->off() ? X_("no") : X_("yes")));
@@ -4638,6 +4811,7 @@ ARDOUR_UI::ambiguous_file (std::string file, std::vector<std::string> hits)
        dialog.present ();
 
        dialog.run ();
+
        return dialog.get_which ();
 }
 
@@ -4727,13 +4901,21 @@ ARDOUR_UI::do_audio_midi_setup (uint32_t desired_sample_rate)
        audio_midi_setup->set_desired_sample_rate (desired_sample_rate);
        audio_midi_setup->set_position (WIN_POS_CENTER);
 
-       switch (audio_midi_setup->run()) {
-       case Gtk::RESPONSE_OK:
-               return 0;
-       case Gtk::RESPONSE_APPLY:
-               return 0;
-       default:
-               return -1;
+       int response;
+
+       while (true) {
+               response = audio_midi_setup->run();
+               switch (response) {
+               case Gtk::RESPONSE_OK:
+                       if (!AudioEngine::instance()->running()) {
+                               continue;
+                       } else {
+                               return 0;
+                       }
+                       return 0;
+               default:
+                       return -1;
+               }
        }
 }
 
@@ -4768,15 +4950,15 @@ ARDOUR_UI::transport_numpad_event (int num)
        if ( _numpad_locate_happening ) {
                _pending_locate_num = _pending_locate_num*10 + num;
        } else {
-               switch (num) {          
-                       case 0:  toggle_roll(false, false);             break;
-                       case 1:  transport_rewind(1);                           break;
-                       case 2:  transport_forward(1);                          break;
-                       case 3:  transport_record(true);                        break;
+               switch (num) {
+                       case 0:  toggle_roll(false, false);             break;
+                       case 1:  transport_rewind(1);                           break;
+                       case 2:  transport_forward(1);                          break;
+                       case 3:  transport_record(true);                        break;
                        case 4:  toggle_session_auto_loop();            break;
-                       case 5:  transport_record(false); toggle_session_auto_loop();   break;
-                       case 6:  toggle_punch();                                        break;
-                       case 7:  toggle_click();                                break;
+                       case 5:  transport_record(false); toggle_session_auto_loop();   break;
+                       case 6:  toggle_punch();                                        break;
+                       case 7:  toggle_click();                                break;
                        case 8:  toggle_auto_return();                  break;
                        case 9:  toggle_follow_edits();         break;
                }
@@ -4786,7 +4968,7 @@ ARDOUR_UI::transport_numpad_event (int num)
 void
 ARDOUR_UI::set_flat_buttons ()
 {
-       CairoWidget::set_flat_buttons( config()->get_flat_buttons() );
+       CairoWidget::set_flat_buttons( UIConfiguration::instance().get_flat_buttons() );
 }
 
 void
@@ -4811,21 +4993,21 @@ ARDOUR_UI::audioengine_became_silent ()
        subscribe_button_box.pack_start (subscribe_button, true, false);
 
        bool (*openuri)(const char*) = PBD::open_uri; /* this forces selection of the const char* variant of PBD::open_uri(), which we need to avoid ambiguity below */
-       
+
        pay_button.signal_clicked().connect (sigc::hide_return (sigc::bind (sigc::ptr_fun (openuri), (const char*) "https://ardour.org/download")));
        subscribe_button.signal_clicked().connect (sigc::hide_return (sigc::bind (sigc::ptr_fun (openuri), (const char*) "https://community.ardour.org/s/subscribe")));
-       
+
        msg.get_vbox()->pack_start (pay_label);
        msg.get_vbox()->pack_start (pay_button_box);
        msg.get_vbox()->pack_start (subscribe_label);
        msg.get_vbox()->pack_start (subscribe_button_box);
 
        msg.get_vbox()->show_all ();
-       
+
        msg.add_button (_("Remain silent"), Gtk::RESPONSE_CANCEL);
        msg.add_button (_("Save and quit"), Gtk::RESPONSE_NO);
        msg.add_button (_("Give me more time"), Gtk::RESPONSE_YES);
-       
+
        int r = msg.run ();
 
        switch (r) {
@@ -4845,3 +5027,23 @@ ARDOUR_UI::audioengine_became_silent ()
                break;
        }
 }
+
+void
+ARDOUR_UI::hide_application ()
+{
+    Application::instance ()-> hide ();
+}
+
+void
+ARDOUR_UI::cancel_solo ()
+{
+       if (_session) {
+               if (_session->soloing()) {
+                       _session->set_solo (_session->get_routes(), false);
+               } else if (_session->listening()) {
+                       _session->set_listen (_session->get_routes(), false);
+               }
+
+               _session->clear_all_solo_state (_session->get_routes()); // safeguard, ideally this won't do anything, check the log-window
+       }
+}