do not attempt to save undo history if it is empty, and do not do any part of history...
[ardour.git] / libs / ardour / session_state.cc
1 /*
2   Copyright (C) 1999-2013 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20
21 #ifdef WAF_BUILD
22 #include "libardour-config.h"
23 #endif
24
25 #include <stdint.h>
26
27 #include <algorithm>
28 #include <fstream>
29 #include <string>
30 #include <cerrno>
31 #include <cstdio> /* snprintf(3) ... grrr */
32 #include <cmath>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <climits>
36 #include <signal.h>
37 #include <sys/time.h>
38
39 #ifdef HAVE_SYS_VFS_H
40 #include <sys/vfs.h>
41 #endif
42
43 #ifdef __APPLE__
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #endif
47
48 #ifdef HAVE_SYS_STATVFS_H
49 #include <sys/statvfs.h>
50 #endif
51
52 #include <glib.h>
53 #include <glib/gstdio.h>
54
55 #include <glibmm.h>
56 #include <glibmm/threads.h>
57
58 #include <boost/algorithm/string.hpp>
59
60 #include "midi++/mmc.h"
61 #include "midi++/port.h"
62
63 #include "evoral/SMF.hpp"
64
65 #include "pbd/boost_debug.h"
66 #include "pbd/basename.h"
67 #include "pbd/controllable_descriptor.h"
68 #include "pbd/enumwriter.h"
69 #include "pbd/error.h"
70 #include "pbd/file_utils.h"
71 #include "pbd/pathexpand.h"
72 #include "pbd/pthread_utils.h"
73 #include "pbd/stacktrace.h"
74 #include "pbd/convert.h"
75 #include "pbd/localtime_r.h"
76
77 #include "ardour/amp.h"
78 #include "ardour/async_midi_port.h"
79 #include "ardour/audio_diskstream.h"
80 #include "ardour/audio_track.h"
81 #include "ardour/audioengine.h"
82 #include "ardour/audiofilesource.h"
83 #include "ardour/audioregion.h"
84 #include "ardour/automation_control.h"
85 #include "ardour/butler.h"
86 #include "ardour/control_protocol_manager.h"
87 #include "ardour/directory_names.h"
88 #include "ardour/filename_extensions.h"
89 #include "ardour/graph.h"
90 #include "ardour/location.h"
91 #include "ardour/midi_model.h"
92 #include "ardour/midi_patch_manager.h"
93 #include "ardour/midi_region.h"
94 #include "ardour/midi_scene_changer.h"
95 #include "ardour/midi_source.h"
96 #include "ardour/midi_track.h"
97 #include "ardour/pannable.h"
98 #include "ardour/playlist_factory.h"
99 #include "ardour/port.h"
100 #include "ardour/processor.h"
101 #include "ardour/proxy_controllable.h"
102 #include "ardour/recent_sessions.h"
103 #include "ardour/region_factory.h"
104 #include "ardour/route_group.h"
105 #include "ardour/send.h"
106 #include "ardour/session.h"
107 #include "ardour/session_directory.h"
108 #include "ardour/session_metadata.h"
109 #include "ardour/session_playlists.h"
110 #include "ardour/session_state_utils.h"
111 #include "ardour/silentfilesource.h"
112 #include "ardour/sndfilesource.h"
113 #include "ardour/source_factory.h"
114 #include "ardour/speakers.h"
115 #include "ardour/template_utils.h"
116 #include "ardour/tempo.h"
117 #include "ardour/ticker.h"
118 #include "ardour/user_bundle.h"
119
120 #include "control_protocol/control_protocol.h"
121
122 #include "i18n.h"
123 #include <locale.h>
124
125 using namespace std;
126 using namespace ARDOUR;
127 using namespace PBD;
128
129 void
130 Session::pre_engine_init (string fullpath)
131 {
132         if (fullpath.empty()) {
133                 destroy ();
134                 throw failed_constructor();
135         }
136
137         /* discover canonical fullpath */
138
139         _path = canonical_path(fullpath);
140
141         /* we require _path to end with a dir separator */
142
143         if (_path[_path.length()-1] != G_DIR_SEPARATOR) {
144                 _path += G_DIR_SEPARATOR;
145         }
146
147         /* is it new ? */
148
149         _is_new = !Glib::file_test (_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
150
151         /* finish initialization that can't be done in a normal C++ constructor
152            definition.
153         */
154
155         timerclear (&last_mmc_step);
156         g_atomic_int_set (&processing_prohibited, 0);
157         g_atomic_int_set (&_record_status, Disabled);
158         g_atomic_int_set (&_playback_load, 100);
159         g_atomic_int_set (&_capture_load, 100);
160         set_next_event ();
161         _all_route_group->set_active (true, this);
162         interpolation.add_channel_to (0, 0);
163
164         if (config.get_use_video_sync()) {
165                 waiting_for_sync_offset = true;
166         } else {
167                 waiting_for_sync_offset = false;
168         }
169
170         last_rr_session_dir = session_dirs.begin();
171
172         set_history_depth (Config->get_history_depth());
173         
174         /* default: assume simple stereo speaker configuration */
175
176         _speakers->setup_default_speakers (2);
177
178         _solo_cut_control.reset (new ProxyControllable (_("solo cut control (dB)"), PBD::Controllable::GainLike,
179                                                         boost::bind (&RCConfiguration::set_solo_mute_gain, Config, _1),
180                                                         boost::bind (&RCConfiguration::get_solo_mute_gain, Config)));
181         add_controllable (_solo_cut_control);
182
183         /* These are all static "per-class" signals */
184
185         SourceFactory::SourceCreated.connect_same_thread (*this, boost::bind (&Session::add_source, this, _1));
186         PlaylistFactory::PlaylistCreated.connect_same_thread (*this, boost::bind (&Session::add_playlist, this, _1, _2));
187         AutomationList::AutomationListCreated.connect_same_thread (*this, boost::bind (&Session::add_automation_list, this, _1));
188         Controllable::Destroyed.connect_same_thread (*this, boost::bind (&Session::remove_controllable, this, _1));
189         IO::PortCountChanged.connect_same_thread (*this, boost::bind (&Session::ensure_buffers, this, _1));
190
191         /* stop IO objects from doing stuff until we're ready for them */
192
193         Delivery::disable_panners ();
194         IO::disable_connecting ();
195
196         AudioFileSource::set_peak_dir (_session_dir->peak_path());
197 }
198
199 int
200 Session::post_engine_init ()
201 {
202         BootMessage (_("Set block size and sample rate"));
203
204         set_block_size (_engine.samples_per_cycle());
205         set_frame_rate (_engine.sample_rate());
206
207         BootMessage (_("Using configuration"));
208
209         _midi_ports = new MidiPortManager;
210         
211         MIDISceneChanger* msc;
212
213         _scene_changer = msc = new MIDISceneChanger (*this);
214         msc->set_input_port (scene_input_port());
215         msc->set_output_port (scene_out());
216
217         boost::function<framecnt_t(void)> timer_func (boost::bind (&Session::audible_frame, this));
218         boost::dynamic_pointer_cast<AsyncMIDIPort>(scene_in())->set_timer (timer_func);
219
220         setup_midi_machine_control ();
221         
222         if (_butler->start_thread()) {
223                 return -1;
224         }
225         
226         if (start_midi_thread ()) {
227                 return -1;
228         }
229         
230         setup_click_sounds (0);
231         setup_midi_control ();
232
233         _engine.Halted.connect_same_thread (*this, boost::bind (&Session::engine_halted, this));
234         _engine.Xrun.connect_same_thread (*this, boost::bind (&Session::xrun_recovery, this));
235
236         try {
237                 /* tempo map requires sample rate knowledge */
238
239                 _tempo_map = new TempoMap (_current_frame_rate);
240                 _tempo_map->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::tempo_map_changed, this, _1));
241                 
242                 /* MidiClock requires a tempo map */
243
244                 midi_clock = new MidiClockTicker ();
245                 midi_clock->set_session (this);
246
247                 /* crossfades require sample rate knowledge */
248
249                 SndFileSource::setup_standard_crossfades (*this, frame_rate());
250                 _engine.GraphReordered.connect_same_thread (*this, boost::bind (&Session::graph_reordered, this));
251                 
252                 AudioDiskstream::allocate_working_buffers();
253                 refresh_disk_space ();
254                 
255                 /* we're finally ready to call set_state() ... all objects have
256                  * been created, the engine is running.
257                  */
258                 
259                 if (state_tree) {
260                         if (set_state (*state_tree->root(), Stateful::loading_state_version)) {
261                                 return -1;
262                         }
263                 } else {
264                         // set_state() will call setup_raid_path(), but if it's a new session we need
265                         // to call setup_raid_path() here.
266                         setup_raid_path (_path);
267                 }
268
269                 /* ENGINE */
270
271                 boost::function<void (std::string)> ff (boost::bind (&Session::config_changed, this, _1, false));
272                 boost::function<void (std::string)> ft (boost::bind (&Session::config_changed, this, _1, true));
273                 
274                 Config->map_parameters (ff);
275                 config.map_parameters (ft);
276
277                 /* Reset all panners */
278                 
279                 Delivery::reset_panners ();
280                 
281                 /* this will cause the CPM to instantiate any protocols that are in use
282                  * (or mandatory), which will pass it this Session, and then call
283                  * set_state() on each instantiated protocol to match stored state.
284                  */
285                 
286                 ControlProtocolManager::instance().set_session (this);
287                 
288                 /* This must be done after the ControlProtocolManager set_session above,
289                    as it will set states for ports which the ControlProtocolManager creates.
290                 */
291                 
292                 // XXX set state of MIDI::Port's
293                 // MidiPortManager::instance()->set_port_states (Config->midi_port_states ());
294                 
295                 /* And this must be done after the MIDI::Manager::set_port_states as
296                  * it will try to make connections whose details are loaded by set_port_states.
297                  */
298                 
299                 hookup_io ();
300                 
301                 /* Let control protocols know that we are now all connected, so they
302                  * could start talking to surfaces if they want to.
303                  */
304                 
305                 ControlProtocolManager::instance().midi_connectivity_established ();
306                 
307                 if (_is_new && !no_auto_connect()) {
308                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
309                         auto_connect_master_bus ();
310                 }
311                 
312                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
313                 
314                 /* update latencies */
315                 
316                 initialize_latencies ();
317                 
318                 _locations->changed.connect_same_thread (*this, boost::bind (&Session::locations_changed, this));
319                 _locations->added.connect_same_thread (*this, boost::bind (&Session::locations_added, this, _1));
320                 
321         } catch (AudioEngine::PortRegistrationFailure& err) {
322                 /* handle this one in a different way than all others, so that its clear what happened */
323                 error << err.what() << endmsg;
324                 return -1;
325         } catch (...) {
326                 return -1;
327         }
328
329         BootMessage (_("Reset Remote Controls"));
330
331         // send_full_time_code (0);
332         _engine.transport_locate (0);
333
334         _mmc->send (MIDI::MachineControlCommand (MIDI::MachineControl::cmdMmcReset));
335         _mmc->send (MIDI::MachineControlCommand (Timecode::Time ()));
336
337         MIDI::Name::MidiPatchManager::instance().set_session (this);
338
339         ltc_tx_initialize();
340         /* initial program change will be delivered later; see ::config_changed() */
341
342         _state_of_the_state = Clean;
343
344         Port::set_connecting_blocked (false);
345
346         DirtyChanged (); /* EMIT SIGNAL */
347
348         if (_is_new) {
349                 save_state ("");
350         } else if (state_was_pending) {
351                 save_state ("");
352                 remove_pending_capture_state ();
353                 state_was_pending = false;
354         }
355
356         return 0;
357 }
358
359 string
360 Session::raid_path () const
361 {
362         Searchpath raid_search_path;
363
364         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
365                 raid_search_path += (*i).path;
366         }
367
368         return raid_search_path.to_string ();
369 }
370
371 void
372 Session::setup_raid_path (string path)
373 {
374         if (path.empty()) {
375                 return;
376         }
377
378         space_and_path sp;
379         string fspath;
380
381         session_dirs.clear ();
382
383         Searchpath search_path(path);
384         Searchpath sound_search_path;
385         Searchpath midi_search_path;
386
387         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
388                 sp.path = *i;
389                 sp.blocks = 0; // not needed
390                 session_dirs.push_back (sp);
391
392                 SessionDirectory sdir(sp.path);
393
394                 sound_search_path += sdir.sound_path ();
395                 midi_search_path += sdir.midi_path ();
396         }
397
398         // reset the round-robin soundfile path thingie
399         last_rr_session_dir = session_dirs.begin();
400 }
401
402 bool
403 Session::path_is_within_session (const std::string& path)
404 {
405         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
406                 if (PBD::path_is_within (i->path, path)) {
407                         return true;
408                 }
409         }
410         return false;
411 }
412
413 int
414 Session::ensure_subdirs ()
415 {
416         string dir;
417
418         dir = session_directory().peak_path();
419
420         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
421                 error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
422                 return -1;
423         }
424
425         dir = session_directory().sound_path();
426
427         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
428                 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
429                 return -1;
430         }
431
432         dir = session_directory().midi_path();
433
434         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
435                 error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
436                 return -1;
437         }
438
439         dir = session_directory().dead_path();
440
441         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
442                 error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
443                 return -1;
444         }
445
446         dir = session_directory().export_path();
447
448         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
449                 error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
450                 return -1;
451         }
452
453         dir = analysis_dir ();
454
455         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
456                 error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
457                 return -1;
458         }
459
460         dir = plugins_dir ();
461
462         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
463                 error << string_compose(_("Session: cannot create session plugins folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
464                 return -1;
465         }
466
467         dir = externals_dir ();
468
469         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
470                 error << string_compose(_("Session: cannot create session externals folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
471                 return -1;
472         }
473
474         return 0;
475 }
476
477 /** @param session_template directory containing session template, or empty.
478  *  Caller must not hold process lock.
479  */
480 int
481 Session::create (const string& session_template, BusProfile* bus_profile)
482 {
483         if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
484                 error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
485                 return -1;
486         }
487
488         if (ensure_subdirs ()) {
489                 return -1;
490         }
491
492         _writable = exists_and_writable (_path);
493
494         if (!session_template.empty()) {
495                 std::string in_path = session_template_dir_to_file (session_template);
496
497                 ifstream in(in_path.c_str());
498
499                 if (in) {
500                         /* no need to call legalize_for_path() since the string
501                          * in session_template is already a legal path name
502                          */
503                         string out_path = Glib::build_filename (_session_dir->root_path(), _name + statefile_suffix);
504
505                         ofstream out(out_path.c_str());
506
507                         if (out) {
508                                 out << in.rdbuf();
509                                 _is_new = false;
510
511                                 /* Copy plugin state files from template to new session */
512                                 std::string template_plugins = Glib::build_filename (session_template, X_("plugins"));
513                                 copy_files (template_plugins, plugins_dir ());
514                                 
515                                 return 0;
516
517                         } else {
518                                 error << string_compose (_("Could not open %1 for writing session template"), out_path)
519                                         << endmsg;
520                                 return -1;
521                         }
522
523                 } else {
524                         error << string_compose (_("Could not open session template %1 for reading"), in_path)
525                                 << endmsg;
526                         return -1;
527                 }
528
529         }
530
531         /* set initial start + end point */
532
533         _state_of_the_state = Clean;
534
535         /* set up Master Out and Control Out if necessary */
536
537         if (bus_profile) {
538
539                 RouteList rl;
540                 ChanCount count(DataType::AUDIO, bus_profile->master_out_channels);
541
542                 if (bus_profile->master_out_channels) {
543                         boost::shared_ptr<Route> r (new Route (*this, _("master"), Route::MasterOut, DataType::AUDIO));
544                         if (r->init ()) {
545                                 return -1;
546                         }
547 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
548                         // boost_debug_shared_ptr_mark_interesting (r.get(), "Route");
549 #endif
550                         {
551                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
552                                 r->input()->ensure_io (count, false, this);
553                                 r->output()->ensure_io (count, false, this);
554                         }
555
556                         rl.push_back (r);
557
558                 } else {
559                         /* prohibit auto-connect to master, because there isn't one */
560                         bus_profile->output_ac = AutoConnectOption (bus_profile->output_ac & ~AutoConnectMaster);
561                 }
562
563                 if (!rl.empty()) {
564                         add_routes (rl, false, false, false);
565                 }
566
567                 /* this allows the user to override settings with an environment variable.
568                  */
569
570                 if (no_auto_connect()) {
571                         bus_profile->input_ac = AutoConnectOption (0);
572                         bus_profile->output_ac = AutoConnectOption (0);
573                 }
574
575                 Config->set_input_auto_connect (bus_profile->input_ac);
576                 Config->set_output_auto_connect (bus_profile->output_ac);
577         }
578
579         if (Config->get_use_monitor_bus() && bus_profile) {
580                 add_monitor_section ();
581         }
582
583         return 0;
584 }
585
586 void
587 Session::maybe_write_autosave()
588 {
589         if (dirty() && record_status() != Recording) {
590                 save_state("", true);
591         }
592 }
593
594 void
595 Session::remove_pending_capture_state ()
596 {
597         std::string pending_state_file_path(_session_dir->root_path());
598
599         pending_state_file_path = Glib::build_filename (pending_state_file_path, legalize_for_path (_current_snapshot_name) + pending_suffix);
600
601         if (!Glib::file_test (pending_state_file_path, Glib::FILE_TEST_EXISTS)) return;
602
603         if (g_remove (pending_state_file_path.c_str()) != 0) {
604                 error << string_compose(_("Could not remove pending capture state at path \"%1\" (%2)"),
605                                 pending_state_file_path, g_strerror (errno)) << endmsg;
606         }
607 }
608
609 /** Rename a state file.
610  *  @param old_name Old snapshot name.
611  *  @param new_name New snapshot name.
612  */
613 void
614 Session::rename_state (string old_name, string new_name)
615 {
616         if (old_name == _current_snapshot_name || old_name == _name) {
617                 /* refuse to rename the current snapshot or the "main" one */
618                 return;
619         }
620
621         const string old_xml_filename = legalize_for_path (old_name) + statefile_suffix;
622         const string new_xml_filename = legalize_for_path (new_name) + statefile_suffix;
623
624         const std::string old_xml_path(Glib::build_filename (_session_dir->root_path(), old_xml_filename));
625         const std::string new_xml_path(Glib::build_filename (_session_dir->root_path(), new_xml_filename));
626
627         if (::g_rename (old_xml_path.c_str(), new_xml_path.c_str()) != 0) {
628                 error << string_compose(_("could not rename snapshot %1 to %2 (%3)"),
629                                 old_name, new_name, g_strerror(errno)) << endmsg;
630         }
631 }
632
633 /** Remove a state file.
634  *  @param snapshot_name Snapshot name.
635  */
636 void
637 Session::remove_state (string snapshot_name)
638 {
639         if (!_writable || snapshot_name == _current_snapshot_name || snapshot_name == _name) {
640                 // refuse to remove the current snapshot or the "main" one
641                 return;
642         }
643
644         std::string xml_path(_session_dir->root_path());
645
646         xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
647
648         if (!create_backup_file (xml_path)) {
649                 // don't remove it if a backup can't be made
650                 // create_backup_file will log the error.
651                 return;
652         }
653
654         // and delete it
655         if (g_remove (xml_path.c_str()) != 0) {
656                 error << string_compose(_("Could not remove session file at path \"%1\" (%2)"),
657                                 xml_path, g_strerror (errno)) << endmsg;
658         }
659 }
660
661 /** @param snapshot_name Name to save under, without .ardour / .pending prefix */
662 int
663 Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot)
664 {
665         XMLTree tree;
666         std::string xml_path(_session_dir->root_path());
667
668         if (!_writable || (_state_of_the_state & CannotSave)) {
669                 return 1;
670         }
671
672         if (g_atomic_int_get(&_suspend_save)) {
673                 _save_queued = true;
674                 return 1;
675         }
676         _save_queued = false;
677
678         if (!_engine.connected ()) {
679                 error << string_compose (_("the %1 audio engine is not connected and state saving would lose all I/O connections. Session not saved"),
680                                          PROGRAM_NAME)
681                       << endmsg;
682                 return 1;
683         }
684
685         /* tell sources we're saving first, in case they write out to a new file
686          * which should be saved with the state rather than the old one */
687         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
688                 try {
689                         i->second->session_saved();
690                 } catch (Evoral::SMF::FileError& e) {
691                         error << string_compose ("Could not write to MIDI file %1; MIDI data not saved.", e.file_name ()) << endmsg;
692                 }
693         }
694
695         SaveSession (); /* EMIT SIGNAL */
696
697         tree.set_root (&get_state());
698
699         if (snapshot_name.empty()) {
700                 snapshot_name = _current_snapshot_name;
701         } else if (switch_to_snapshot) {
702                 _current_snapshot_name = snapshot_name;
703         }
704
705         if (!pending) {
706
707                 /* proper save: use statefile_suffix (.ardour in English) */
708
709                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
710
711                 /* make a backup copy of the old file */
712
713                 if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS) && !create_backup_file (xml_path)) {
714                         // create_backup_file will log the error
715                         return -1;
716                 }
717
718         } else {
719
720                 /* pending save: use pending_suffix (.pending in English) */
721                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + pending_suffix);
722         }
723
724         std::string tmp_path(_session_dir->root_path());
725         tmp_path = Glib::build_filename (tmp_path, legalize_for_path (snapshot_name) + temp_suffix);
726
727         // cerr << "actually writing state to " << xml_path << endl;
728
729         if (!tree.write (tmp_path)) {
730                 error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
731                 if (g_remove (tmp_path.c_str()) != 0) {
732                         error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
733                                         tmp_path, g_strerror (errno)) << endmsg;
734                 }
735                 return -1;
736
737         } else {
738
739                 if (::g_rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
740                         error << string_compose (_("could not rename temporary session file %1 to %2 (%3)"),
741                                         tmp_path, xml_path, g_strerror(errno)) << endmsg;
742                         if (g_remove (tmp_path.c_str()) != 0) {
743                                 error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
744                                                 tmp_path, g_strerror (errno)) << endmsg;
745                         }
746                         return -1;
747                 }
748         }
749
750         if (!pending) {
751
752                 save_history (snapshot_name);
753
754                 bool was_dirty = dirty();
755
756                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
757
758                 if (was_dirty) {
759                         DirtyChanged (); /* EMIT SIGNAL */
760                 }
761
762                 StateSaved (snapshot_name); /* EMIT SIGNAL */
763         }
764
765         return 0;
766 }
767
768 int
769 Session::restore_state (string snapshot_name)
770 {
771         if (load_state (snapshot_name) == 0) {
772                 set_state (*state_tree->root(), Stateful::loading_state_version);
773         }
774
775         return 0;
776 }
777
778 int
779 Session::load_state (string snapshot_name)
780 {
781         delete state_tree;
782         state_tree = 0;
783
784         state_was_pending = false;
785
786         /* check for leftover pending state from a crashed capture attempt */
787
788         std::string xmlpath(_session_dir->root_path());
789         xmlpath = Glib::build_filename (xmlpath, legalize_for_path (snapshot_name) + pending_suffix);
790
791         if (Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
792
793                 /* there is pending state from a crashed capture attempt */
794
795                 boost::optional<int> r = AskAboutPendingState();
796                 if (r.get_value_or (1)) {
797                         state_was_pending = true;
798                 }
799         }
800
801         if (!state_was_pending) {
802                 xmlpath = Glib::build_filename (_session_dir->root_path(), snapshot_name);
803         }
804
805         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
806                 xmlpath = Glib::build_filename (_session_dir->root_path(), legalize_for_path (snapshot_name) + statefile_suffix);
807                 if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
808                         error << string_compose(_("%1: session file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
809                         return 1;
810                 }
811         }
812
813         state_tree = new XMLTree;
814
815         set_dirty();
816
817         _writable = exists_and_writable (xmlpath) && exists_and_writable(Glib::path_get_dirname(xmlpath));
818
819         if (!state_tree->read (xmlpath)) {
820                 error << string_compose(_("Could not understand session file %1"), xmlpath) << endmsg;
821                 delete state_tree;
822                 state_tree = 0;
823                 return -1;
824         }
825
826         XMLNode& root (*state_tree->root());
827
828         if (root.name() != X_("Session")) {
829                 error << string_compose (_("Session file %1 is not a session"), xmlpath) << endmsg;
830                 delete state_tree;
831                 state_tree = 0;
832                 return -1;
833         }
834
835         const XMLProperty* prop;
836
837         if ((prop = root.property ("version")) == 0) {
838                 /* no version implies very old version of Ardour */
839                 Stateful::loading_state_version = 1000;
840         } else {
841                 if (prop->value().find ('.') != string::npos) {
842                         /* old school version format */
843                         if (prop->value()[0] == '2') {
844                                 Stateful::loading_state_version = 2000;
845                         } else {
846                                 Stateful::loading_state_version = 3000;
847                         }
848                 } else {
849                         Stateful::loading_state_version = atoi (prop->value());
850                 }
851         }
852
853         if (Stateful::loading_state_version < CURRENT_SESSION_FILE_VERSION && _writable) {
854
855                 std::string backup_path(_session_dir->root_path());
856                 std::string backup_filename = string_compose ("%1-%2%3", legalize_for_path (snapshot_name), Stateful::loading_state_version, statefile_suffix);
857                 backup_path = Glib::build_filename (backup_path, backup_filename);
858
859                 // only create a backup for a given statefile version once
860
861                 if (!Glib::file_test (backup_path, Glib::FILE_TEST_EXISTS)) {
862                         
863                         VersionMismatch (xmlpath, backup_path);
864                         
865                         if (!copy_file (xmlpath, backup_path)) {;
866                                 return -1;
867                         }
868                 }
869         }
870
871         return 0;
872 }
873
874 int
875 Session::load_options (const XMLNode& node)
876 {
877         LocaleGuard lg (X_("POSIX"));
878         config.set_variables (node);
879         return 0;
880 }
881
882 bool
883 Session::save_default_options ()
884 {
885         return config.save_state();
886 }
887
888 XMLNode&
889 Session::get_state()
890 {
891         return state(true);
892 }
893
894 XMLNode&
895 Session::get_template()
896 {
897         /* if we don't disable rec-enable, diskstreams
898            will believe they need to store their capture
899            sources in their state node.
900         */
901
902         disable_record (false);
903
904         return state(false);
905 }
906
907 XMLNode&
908 Session::state (bool full_state)
909 {
910         XMLNode* node = new XMLNode("Session");
911         XMLNode* child;
912
913         char buf[16];
914         snprintf(buf, sizeof(buf), "%d", CURRENT_SESSION_FILE_VERSION);
915         node->add_property("version", buf);
916
917         /* store configuration settings */
918
919         if (full_state) {
920
921                 node->add_property ("name", _name);
922                 snprintf (buf, sizeof (buf), "%" PRId64, _nominal_frame_rate);
923                 node->add_property ("sample-rate", buf);
924
925                 if (session_dirs.size() > 1) {
926
927                         string p;
928
929                         vector<space_and_path>::iterator i = session_dirs.begin();
930                         vector<space_and_path>::iterator next;
931
932                         ++i; /* skip the first one */
933                         next = i;
934                         ++next;
935
936                         while (i != session_dirs.end()) {
937
938                                 p += (*i).path;
939
940                                 if (next != session_dirs.end()) {
941                                         p += G_SEARCHPATH_SEPARATOR;
942                                 } else {
943                                         break;
944                                 }
945
946                                 ++next;
947                                 ++i;
948                         }
949
950                         child = node->add_child ("Path");
951                         child->add_content (p);
952                 }
953         }
954
955         /* save the ID counter */
956
957         snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
958         node->add_property ("id-counter", buf);
959
960         /* save the event ID counter */
961
962         snprintf (buf, sizeof (buf), "%d", Evoral::event_id_counter());
963         node->add_property ("event-counter", buf);
964
965         /* various options */
966
967         list<XMLNode*> midi_port_nodes = _midi_ports->get_midi_port_states();
968         if (!midi_port_nodes.empty()) {
969                 XMLNode* midi_port_stuff = new XMLNode ("MIDIPorts");
970                 for (list<XMLNode*>::const_iterator n = midi_port_nodes.begin(); n != midi_port_nodes.end(); ++n) {
971                         midi_port_stuff->add_child_nocopy (**n);
972                 }
973                 node->add_child_nocopy (*midi_port_stuff);
974         }
975
976         node->add_child_nocopy (config.get_variables ());
977
978         node->add_child_nocopy (ARDOUR::SessionMetadata::Metadata()->get_state());
979
980         child = node->add_child ("Sources");
981
982         if (full_state) {
983                 Glib::Threads::Mutex::Lock sl (source_lock);
984
985                 for (SourceMap::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
986
987                         /* Don't save information about non-file Sources, or
988                          * about non-destructive file sources that are empty
989                          * and unused by any regions.
990                         */
991
992                         boost::shared_ptr<FileSource> fs;
993
994                         if ((fs = boost::dynamic_pointer_cast<FileSource> (siter->second)) != 0) {
995
996                                 if (!fs->destructive()) {
997                                         if (fs->empty() && !fs->used()) {
998                                                 continue;
999                                         }
1000                                 }
1001
1002                                 child->add_child_nocopy (siter->second->get_state());
1003                         }
1004                 }
1005         }
1006
1007         child = node->add_child ("Regions");
1008
1009         if (full_state) {
1010                 Glib::Threads::Mutex::Lock rl (region_lock);
1011                 const RegionFactory::RegionMap& region_map (RegionFactory::all_regions());
1012                 for (RegionFactory::RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
1013                         boost::shared_ptr<Region> r = i->second;
1014                         /* only store regions not attached to playlists */
1015                         if (r->playlist() == 0) {
1016                                 if (boost::dynamic_pointer_cast<AudioRegion>(r)) {
1017                                         child->add_child_nocopy ((boost::dynamic_pointer_cast<AudioRegion>(r))->get_basic_state ());
1018                                 } else {
1019                                         child->add_child_nocopy (r->get_state ());
1020                                 }
1021                         }
1022                 }
1023
1024                 RegionFactory::CompoundAssociations& cassocs (RegionFactory::compound_associations());
1025
1026                 if (!cassocs.empty()) {
1027                         XMLNode* ca = node->add_child (X_("CompoundAssociations"));
1028
1029                         for (RegionFactory::CompoundAssociations::iterator i = cassocs.begin(); i != cassocs.end(); ++i) {
1030                                 char buf[64];
1031                                 XMLNode* can = new XMLNode (X_("CompoundAssociation"));
1032                                 i->first->id().print (buf, sizeof (buf));
1033                                 can->add_property (X_("copy"), buf);
1034                                 i->second->id().print (buf, sizeof (buf));
1035                                 can->add_property (X_("original"), buf);
1036                                 ca->add_child_nocopy (*can);
1037                         }
1038                 }
1039         }
1040
1041         if (full_state) {
1042                 node->add_child_nocopy (_locations->get_state());
1043         } else {
1044                 // for a template, just create a new Locations, populate it
1045                 // with the default start and end, and get the state for that.
1046                 Locations loc (*this);
1047                 Location* range = new Location (*this, 0, 0, _("session"), Location::IsSessionRange);
1048                 range->set (max_framepos, 0);
1049                 loc.add (range);
1050                 node->add_child_nocopy (loc.get_state());
1051         }
1052
1053         child = node->add_child ("Bundles");
1054         {
1055                 boost::shared_ptr<BundleList> bundles = _bundles.reader ();
1056                 for (BundleList::iterator i = bundles->begin(); i != bundles->end(); ++i) {
1057                         boost::shared_ptr<UserBundle> b = boost::dynamic_pointer_cast<UserBundle> (*i);
1058                         if (b) {
1059                                 child->add_child_nocopy (b->get_state());
1060                         }
1061                 }
1062         }
1063
1064         child = node->add_child ("Routes");
1065         {
1066                 boost::shared_ptr<RouteList> r = routes.reader ();
1067
1068                 RoutePublicOrderSorter cmp;
1069                 RouteList public_order (*r);
1070                 public_order.sort (cmp);
1071
1072                 /* the sort should have put control outs first */
1073
1074                 if (_monitor_out) {
1075                         assert (_monitor_out == public_order.front());
1076                 }
1077
1078                 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1079                         if (!(*i)->is_auditioner()) {
1080                                 if (full_state) {
1081                                         child->add_child_nocopy ((*i)->get_state());
1082                                 } else {
1083                                         child->add_child_nocopy ((*i)->get_template());
1084                                 }
1085                         }
1086                 }
1087         }
1088
1089         playlists->add_state (node, full_state);
1090
1091         child = node->add_child ("RouteGroups");
1092         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
1093                 child->add_child_nocopy ((*i)->get_state());
1094         }
1095
1096         if (_click_io) {
1097                 XMLNode* gain_child = node->add_child ("Click");
1098                 gain_child->add_child_nocopy (_click_io->state (full_state));
1099                 gain_child->add_child_nocopy (_click_gain->state (full_state));
1100         }
1101
1102         if (_ltc_input) {
1103                 XMLNode* ltc_input_child = node->add_child ("LTC-In");
1104                 ltc_input_child->add_child_nocopy (_ltc_input->state (full_state));
1105         }
1106
1107         if (_ltc_input) {
1108                 XMLNode* ltc_output_child = node->add_child ("LTC-Out");
1109                 ltc_output_child->add_child_nocopy (_ltc_output->state (full_state));
1110         }
1111
1112         node->add_child_nocopy (_speakers->get_state());
1113         node->add_child_nocopy (_tempo_map->get_state());
1114         node->add_child_nocopy (get_control_protocol_state());
1115
1116         if (_extra_xml) {
1117                 node->add_child_copy (*_extra_xml);
1118         }
1119
1120         return *node;
1121 }
1122
1123 XMLNode&
1124 Session::get_control_protocol_state ()
1125 {
1126         ControlProtocolManager& cpm (ControlProtocolManager::instance());
1127         return cpm.get_state();
1128 }
1129
1130 int
1131 Session::set_state (const XMLNode& node, int version)
1132 {
1133         XMLNodeList nlist;
1134         XMLNode* child;
1135         const XMLProperty* prop;
1136         int ret = -1;
1137
1138         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1139
1140         if (node.name() != X_("Session")) {
1141                 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1142                 return -1;
1143         }
1144
1145         if ((prop = node.property ("name")) != 0) {
1146                 _name = prop->value ();
1147         }
1148
1149         if ((prop = node.property (X_("sample-rate"))) != 0) {
1150
1151                 _nominal_frame_rate = atoi (prop->value());
1152
1153                 if (_nominal_frame_rate != _current_frame_rate) {
1154                         boost::optional<int> r = AskAboutSampleRateMismatch (_nominal_frame_rate, _current_frame_rate);
1155                         if (r.get_value_or (0)) {
1156                                 return -1;
1157                         }
1158                 }
1159         }
1160
1161         setup_raid_path(_session_dir->root_path());
1162
1163         if ((prop = node.property (X_("id-counter"))) != 0) {
1164                 uint64_t x;
1165                 sscanf (prop->value().c_str(), "%" PRIu64, &x);
1166                 ID::init_counter (x);
1167         } else {
1168                 /* old sessions used a timebased counter, so fake
1169                    the startup ID counter based on a standard
1170                    timestamp.
1171                 */
1172                 time_t now;
1173                 time (&now);
1174                 ID::init_counter (now);
1175         }
1176
1177         if ((prop = node.property (X_("event-counter"))) != 0) {
1178                 Evoral::init_event_id_counter (atoi (prop->value()));
1179         }
1180
1181
1182         if ((child = find_named_node (node, "MIDIPorts")) != 0) {
1183                 _midi_ports->set_midi_port_states (child->children());
1184         }
1185
1186         IO::disable_connecting ();
1187
1188         Stateful::save_extra_xml (node);
1189
1190         if (((child = find_named_node (node, "Options")) != 0)) { /* old style */
1191                 load_options (*child);
1192         } else if ((child = find_named_node (node, "Config")) != 0) { /* new style */
1193                 load_options (*child);
1194         } else {
1195                 error << _("Session: XML state has no options section") << endmsg;
1196         }
1197
1198         if (version >= 3000) {
1199                 if ((child = find_named_node (node, "Metadata")) == 0) {
1200                         warning << _("Session: XML state has no metadata section") << endmsg;
1201                 } else if ( ARDOUR::SessionMetadata::Metadata()->set_state (*child, version) ) {
1202                         goto out;
1203                 }
1204         }
1205
1206         if ((child = find_named_node (node, X_("Speakers"))) != 0) {
1207                 _speakers->set_state (*child, version);
1208         }
1209
1210         if ((child = find_named_node (node, "Sources")) == 0) {
1211                 error << _("Session: XML state has no sources section") << endmsg;
1212                 goto out;
1213         } else if (load_sources (*child)) {
1214                 goto out;
1215         }
1216
1217         if ((child = find_named_node (node, "TempoMap")) == 0) {
1218                 error << _("Session: XML state has no Tempo Map section") << endmsg;
1219                 goto out;
1220         } else if (_tempo_map->set_state (*child, version)) {
1221                 goto out;
1222         }
1223
1224         if ((child = find_named_node (node, "Locations")) == 0) {
1225                 error << _("Session: XML state has no locations section") << endmsg;
1226                 goto out;
1227         } else if (_locations->set_state (*child, version)) {
1228                 goto out;
1229         }
1230
1231         Location* location;
1232
1233         if ((location = _locations->auto_loop_location()) != 0) {
1234                 set_auto_loop_location (location);
1235         }
1236
1237         if ((location = _locations->auto_punch_location()) != 0) {
1238                 set_auto_punch_location (location);
1239         }
1240
1241         if ((location = _locations->session_range_location()) != 0) {
1242                 delete _session_range_location;
1243                 _session_range_location = location;
1244         }
1245
1246         if (_session_range_location) {
1247                 AudioFileSource::set_header_position_offset (_session_range_location->start());
1248         }
1249
1250         if ((child = find_named_node (node, "Regions")) == 0) {
1251                 error << _("Session: XML state has no Regions section") << endmsg;
1252                 goto out;
1253         } else if (load_regions (*child)) {
1254                 goto out;
1255         }
1256
1257         if ((child = find_named_node (node, "Playlists")) == 0) {
1258                 error << _("Session: XML state has no playlists section") << endmsg;
1259                 goto out;
1260         } else if (playlists->load (*this, *child)) {
1261                 goto out;
1262         }
1263
1264         if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1265                 // this is OK
1266         } else if (playlists->load_unused (*this, *child)) {
1267                 goto out;
1268         }
1269
1270         if ((child = find_named_node (node, "CompoundAssociations")) != 0) {
1271                 if (load_compounds (*child)) {
1272                         goto out;
1273                 }
1274         }
1275
1276         if (version >= 3000) {
1277                 if ((child = find_named_node (node, "Bundles")) == 0) {
1278                         warning << _("Session: XML state has no bundles section") << endmsg;
1279                         //goto out;
1280                 } else {
1281                         /* We can't load Bundles yet as they need to be able
1282                            to convert from port names to Port objects, which can't happen until
1283                            later */
1284                         _bundle_xml_node = new XMLNode (*child);
1285                 }
1286         }
1287
1288         if (version < 3000) {
1289                 if ((child = find_named_node (node, X_("DiskStreams"))) == 0) {
1290                         error << _("Session: XML state has no diskstreams section") << endmsg;
1291                         goto out;
1292                 } else if (load_diskstreams_2X (*child, version)) {
1293                         goto out;
1294                 }
1295         }
1296
1297         if ((child = find_named_node (node, "Routes")) == 0) {
1298                 error << _("Session: XML state has no routes section") << endmsg;
1299                 goto out;
1300         } else if (load_routes (*child, version)) {
1301                 goto out;
1302         }
1303
1304         /* our diskstreams list is no longer needed as they are now all owned by their Route */
1305         _diskstreams_2X.clear ();
1306
1307         if (version >= 3000) {
1308
1309                 if ((child = find_named_node (node, "RouteGroups")) == 0) {
1310                         error << _("Session: XML state has no route groups section") << endmsg;
1311                         goto out;
1312                 } else if (load_route_groups (*child, version)) {
1313                         goto out;
1314                 }
1315
1316         } else if (version < 3000) {
1317
1318                 if ((child = find_named_node (node, "EditGroups")) == 0) {
1319                         error << _("Session: XML state has no edit groups section") << endmsg;
1320                         goto out;
1321                 } else if (load_route_groups (*child, version)) {
1322                         goto out;
1323                 }
1324
1325                 if ((child = find_named_node (node, "MixGroups")) == 0) {
1326                         error << _("Session: XML state has no mix groups section") << endmsg;
1327                         goto out;
1328                 } else if (load_route_groups (*child, version)) {
1329                         goto out;
1330                 }
1331         }
1332
1333         if ((child = find_named_node (node, "Click")) == 0) {
1334                 warning << _("Session: XML state has no click section") << endmsg;
1335         } else if (_click_io) {
1336                 setup_click_state (&node);
1337         }
1338
1339         if ((child = find_named_node (node, ControlProtocolManager::state_node_name)) != 0) {
1340                 ControlProtocolManager::instance().set_state (*child, version);
1341         }
1342
1343         update_have_rec_enabled_track ();
1344
1345         /* here beginneth the second phase ... */
1346
1347         StateReady (); /* EMIT SIGNAL */
1348
1349         return 0;
1350
1351   out:
1352         return ret;
1353 }
1354
1355 int
1356 Session::load_routes (const XMLNode& node, int version)
1357 {
1358         XMLNodeList nlist;
1359         XMLNodeConstIterator niter;
1360         RouteList new_routes;
1361
1362         nlist = node.children();
1363
1364         set_dirty();
1365
1366         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1367
1368                 boost::shared_ptr<Route> route;
1369                 if (version < 3000) {
1370                         route = XMLRouteFactory_2X (**niter, version);
1371                 } else {
1372                         route = XMLRouteFactory (**niter, version);
1373                 }
1374
1375                 if (route == 0) {
1376                         error << _("Session: cannot create Route from XML description.") << endmsg;
1377                         return -1;
1378                 }
1379
1380                 BootMessage (string_compose (_("Loaded track/bus %1"), route->name()));
1381
1382                 new_routes.push_back (route);
1383         }
1384
1385         add_routes (new_routes, false, false, false);
1386
1387         return 0;
1388 }
1389
1390 boost::shared_ptr<Route>
1391 Session::XMLRouteFactory (const XMLNode& node, int version)
1392 {
1393         boost::shared_ptr<Route> ret;
1394
1395         if (node.name() != "Route") {
1396                 return ret;
1397         }
1398
1399         XMLNode* ds_child = find_named_node (node, X_("Diskstream"));
1400
1401         DataType type = DataType::AUDIO;
1402         const XMLProperty* prop = node.property("default-type");
1403
1404         if (prop) {
1405                 type = DataType (prop->value());
1406         }
1407
1408         assert (type != DataType::NIL);
1409
1410         if (ds_child) {
1411
1412                 boost::shared_ptr<Track> track;
1413
1414                 if (type == DataType::AUDIO) {
1415                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1416                 } else {
1417                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1418                 }
1419
1420                 if (track->init()) {
1421                         return ret;
1422                 }
1423
1424                 if (track->set_state (node, version)) {
1425                         return ret;
1426                 }
1427
1428 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1429                 // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
1430 #endif
1431                 ret = track;
1432
1433         } else {
1434                 enum Route::Flag flags = Route::Flag(0);
1435                 const XMLProperty* prop = node.property("flags");
1436                 if (prop) {
1437                         flags = Route::Flag (string_2_enum (prop->value(), flags));
1438                 }
1439
1440                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1441
1442                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1443 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1444                         // boost_debug_shared_ptr_mark_interesting (r.get(), "Route");
1445 #endif
1446                         ret = r;
1447                 }
1448         }
1449
1450         return ret;
1451 }
1452
1453 boost::shared_ptr<Route>
1454 Session::XMLRouteFactory_2X (const XMLNode& node, int version)
1455 {
1456         boost::shared_ptr<Route> ret;
1457
1458         if (node.name() != "Route") {
1459                 return ret;
1460         }
1461
1462         XMLProperty const * ds_prop = node.property (X_("diskstream-id"));
1463         if (!ds_prop) {
1464                 ds_prop = node.property (X_("diskstream"));
1465         }
1466
1467         DataType type = DataType::AUDIO;
1468         const XMLProperty* prop = node.property("default-type");
1469
1470         if (prop) {
1471                 type = DataType (prop->value());
1472         }
1473
1474         assert (type != DataType::NIL);
1475
1476         if (ds_prop) {
1477
1478                 list<boost::shared_ptr<Diskstream> >::iterator i = _diskstreams_2X.begin ();
1479                 while (i != _diskstreams_2X.end() && (*i)->id() != ds_prop->value()) {
1480                         ++i;
1481                 }
1482
1483                 if (i == _diskstreams_2X.end()) {
1484                         error << _("Could not find diskstream for route") << endmsg;
1485                         return boost::shared_ptr<Route> ();
1486                 }
1487
1488                 boost::shared_ptr<Track> track;
1489
1490                 if (type == DataType::AUDIO) {
1491                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1492                 } else {
1493                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1494                 }
1495
1496                 if (track->init()) {
1497                         return ret;
1498                 }
1499
1500                 if (track->set_state (node, version)) {
1501                         return ret;
1502                 }
1503
1504                 track->set_diskstream (*i);
1505
1506 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1507                 // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
1508 #endif
1509                 ret = track;
1510
1511         } else {
1512                 enum Route::Flag flags = Route::Flag(0);
1513                 const XMLProperty* prop = node.property("flags");
1514                 if (prop) {
1515                         flags = Route::Flag (string_2_enum (prop->value(), flags));
1516                 }
1517
1518                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1519
1520                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1521 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
1522                         // boost_debug_shared_ptr_mark_interesting (r.get(), "Route");
1523 #endif
1524                         ret = r;
1525                 }
1526         }
1527
1528         return ret;
1529 }
1530
1531 int
1532 Session::load_regions (const XMLNode& node)
1533 {
1534         XMLNodeList nlist;
1535         XMLNodeConstIterator niter;
1536         boost::shared_ptr<Region> region;
1537
1538         nlist = node.children();
1539
1540         set_dirty();
1541
1542         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1543                 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1544                         error << _("Session: cannot create Region from XML description.");
1545                         const XMLProperty *name = (**niter).property("name");
1546
1547                         if (name) {
1548                                 error << " " << string_compose (_("Can not load state for region '%1'"), name->value());
1549                         }
1550
1551                         error << endmsg;
1552                 }
1553         }
1554
1555         return 0;
1556 }
1557
1558 int
1559 Session::load_compounds (const XMLNode& node)
1560 {
1561         XMLNodeList calist = node.children();
1562         XMLNodeConstIterator caiter;
1563         XMLProperty *caprop;
1564
1565         for (caiter = calist.begin(); caiter != calist.end(); ++caiter) {
1566                 XMLNode* ca = *caiter;
1567                 ID orig_id;
1568                 ID copy_id;
1569
1570                 if ((caprop = ca->property (X_("original"))) == 0) {
1571                         continue;
1572                 }
1573                 orig_id = caprop->value();
1574
1575                 if ((caprop = ca->property (X_("copy"))) == 0) {
1576                         continue;
1577                 }
1578                 copy_id = caprop->value();
1579
1580                 boost::shared_ptr<Region> orig = RegionFactory::region_by_id (orig_id);
1581                 boost::shared_ptr<Region> copy = RegionFactory::region_by_id (copy_id);
1582
1583                 if (!orig || !copy) {
1584                         warning << string_compose (_("Regions in compound description not found (ID's %1 and %2): ignored"),
1585                                                    orig_id, copy_id)
1586                                 << endmsg;
1587                         continue;
1588                 }
1589
1590                 RegionFactory::add_compound_association (orig, copy);
1591         }
1592
1593         return 0;
1594 }
1595
1596 void
1597 Session::load_nested_sources (const XMLNode& node)
1598 {
1599         XMLNodeList nlist;
1600         XMLNodeConstIterator niter;
1601
1602         nlist = node.children();
1603
1604         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1605                 if ((*niter)->name() == "Source") {
1606
1607                         /* it may already exist, so don't recreate it unnecessarily 
1608                          */
1609
1610                         XMLProperty* prop = (*niter)->property (X_("id"));
1611                         if (!prop) {
1612                                 error << _("Nested source has no ID info in session file! (ignored)") << endmsg;
1613                                 continue;
1614                         }
1615
1616                         ID source_id (prop->value());
1617
1618                         if (!source_by_id (source_id)) {
1619
1620                                 try {
1621                                         SourceFactory::create (*this, **niter, true);
1622                                 }
1623                                 catch (failed_constructor& err) {
1624                                         error << string_compose (_("Cannot reconstruct nested source for region %1"), name()) << endmsg;
1625                                 }
1626                         }
1627                 }
1628         }
1629 }
1630
1631 boost::shared_ptr<Region>
1632 Session::XMLRegionFactory (const XMLNode& node, bool full)
1633 {
1634         const XMLProperty* type = node.property("type");
1635
1636         try {
1637
1638                 const XMLNodeList& nlist = node.children();
1639
1640                 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
1641                         XMLNode *child = (*niter);
1642                         if (child->name() == "NestedSource") {
1643                                 load_nested_sources (*child);
1644                         }
1645                 }
1646
1647                 if (!type || type->value() == "audio") {
1648                         return boost::shared_ptr<Region>(XMLAudioRegionFactory (node, full));
1649                 } else if (type->value() == "midi") {
1650                         return boost::shared_ptr<Region>(XMLMidiRegionFactory (node, full));
1651                 }
1652
1653         } catch (failed_constructor& err) {
1654                 return boost::shared_ptr<Region> ();
1655         }
1656
1657         return boost::shared_ptr<Region> ();
1658 }
1659
1660 boost::shared_ptr<AudioRegion>
1661 Session::XMLAudioRegionFactory (const XMLNode& node, bool /*full*/)
1662 {
1663         const XMLProperty* prop;
1664         boost::shared_ptr<Source> source;
1665         boost::shared_ptr<AudioSource> as;
1666         SourceList sources;
1667         SourceList master_sources;
1668         uint32_t nchans = 1;
1669         char buf[128];
1670
1671         if (node.name() != X_("Region")) {
1672                 return boost::shared_ptr<AudioRegion>();
1673         }
1674
1675         if ((prop = node.property (X_("channels"))) != 0) {
1676                 nchans = atoi (prop->value().c_str());
1677         }
1678
1679         if ((prop = node.property ("name")) == 0) {
1680                 cerr << "no name for this region\n";
1681                 abort ();
1682         }
1683
1684         if ((prop = node.property (X_("source-0"))) == 0) {
1685                 if ((prop = node.property ("source")) == 0) {
1686                         error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1687                         return boost::shared_ptr<AudioRegion>();
1688                 }
1689         }
1690
1691         PBD::ID s_id (prop->value());
1692
1693         if ((source = source_by_id (s_id)) == 0) {
1694                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1695                 return boost::shared_ptr<AudioRegion>();
1696         }
1697
1698         as = boost::dynamic_pointer_cast<AudioSource>(source);
1699         if (!as) {
1700                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
1701                 return boost::shared_ptr<AudioRegion>();
1702         }
1703
1704         sources.push_back (as);
1705
1706         /* pickup other channels */
1707
1708         for (uint32_t n=1; n < nchans; ++n) {
1709                 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1710                 if ((prop = node.property (buf)) != 0) {
1711
1712                         PBD::ID id2 (prop->value());
1713
1714                         if ((source = source_by_id (id2)) == 0) {
1715                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1716                                 return boost::shared_ptr<AudioRegion>();
1717                         }
1718
1719                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1720                         if (!as) {
1721                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1722                                 return boost::shared_ptr<AudioRegion>();
1723                         }
1724                         sources.push_back (as);
1725                 }
1726         }
1727
1728         for (uint32_t n = 0; n < nchans; ++n) {
1729                 snprintf (buf, sizeof(buf), X_("master-source-%d"), n);
1730                 if ((prop = node.property (buf)) != 0) {
1731
1732                         PBD::ID id2 (prop->value());
1733
1734                         if ((source = source_by_id (id2)) == 0) {
1735                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1736                                 return boost::shared_ptr<AudioRegion>();
1737                         }
1738
1739                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1740                         if (!as) {
1741                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1742                                 return boost::shared_ptr<AudioRegion>();
1743                         }
1744                         master_sources.push_back (as);
1745                 }
1746         }
1747
1748         try {
1749                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
1750
1751                 /* a final detail: this is the one and only place that we know how long missing files are */
1752
1753                 if (region->whole_file()) {
1754                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1755                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1756                                 if (sfp) {
1757                                         sfp->set_length (region->length());
1758                                 }
1759                         }
1760                 }
1761
1762                 if (!master_sources.empty()) {
1763                         if (master_sources.size() != nchans) {
1764                                 error << _("Session: XMLNode describing an AudioRegion is missing some master sources; ignored") << endmsg;
1765                         } else {
1766                                 region->set_master_sources (master_sources);
1767                         }
1768                 }
1769
1770                 return region;
1771
1772         }
1773
1774         catch (failed_constructor& err) {
1775                 return boost::shared_ptr<AudioRegion>();
1776         }
1777 }
1778
1779 boost::shared_ptr<MidiRegion>
1780 Session::XMLMidiRegionFactory (const XMLNode& node, bool /*full*/)
1781 {
1782         const XMLProperty* prop;
1783         boost::shared_ptr<Source> source;
1784         boost::shared_ptr<MidiSource> ms;
1785         SourceList sources;
1786
1787         if (node.name() != X_("Region")) {
1788                 return boost::shared_ptr<MidiRegion>();
1789         }
1790
1791         if ((prop = node.property ("name")) == 0) {
1792                 cerr << "no name for this region\n";
1793                 abort ();
1794         }
1795
1796         if ((prop = node.property (X_("source-0"))) == 0) {
1797                 if ((prop = node.property ("source")) == 0) {
1798                         error << _("Session: XMLNode describing a MidiRegion is incomplete (no source)") << endmsg;
1799                         return boost::shared_ptr<MidiRegion>();
1800                 }
1801         }
1802
1803         PBD::ID s_id (prop->value());
1804
1805         if ((source = source_by_id (s_id)) == 0) {
1806                 error << string_compose(_("Session: XMLNode describing a MidiRegion references an unknown source id =%1"), s_id) << endmsg;
1807                 return boost::shared_ptr<MidiRegion>();
1808         }
1809
1810         ms = boost::dynamic_pointer_cast<MidiSource>(source);
1811         if (!ms) {
1812                 error << string_compose(_("Session: XMLNode describing a MidiRegion references a non-midi source id =%1"), s_id) << endmsg;
1813                 return boost::shared_ptr<MidiRegion>();
1814         }
1815
1816         sources.push_back (ms);
1817
1818         try {
1819                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (sources, node)));
1820                 /* a final detail: this is the one and only place that we know how long missing files are */
1821
1822                 if (region->whole_file()) {
1823                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1824                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1825                                 if (sfp) {
1826                                         sfp->set_length (region->length());
1827                                 }
1828                         }
1829                 }
1830
1831                 return region;
1832         }
1833
1834         catch (failed_constructor& err) {
1835                 return boost::shared_ptr<MidiRegion>();
1836         }
1837 }
1838
1839 XMLNode&
1840 Session::get_sources_as_xml ()
1841
1842 {
1843         XMLNode* node = new XMLNode (X_("Sources"));
1844         Glib::Threads::Mutex::Lock lm (source_lock);
1845
1846         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
1847                 node->add_child_nocopy (i->second->get_state());
1848         }
1849
1850         return *node;
1851 }
1852
1853 int
1854 Session::load_sources (const XMLNode& node)
1855 {
1856         XMLNodeList nlist;
1857         XMLNodeConstIterator niter;
1858         boost::shared_ptr<Source> source;
1859
1860         nlist = node.children();
1861
1862         set_dirty();
1863
1864         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1865           retry:
1866                 try {
1867                         if ((source = XMLSourceFactory (**niter)) == 0) {
1868                                 error << _("Session: cannot create Source from XML description.") << endmsg;
1869                         }
1870
1871                 } catch (MissingSource& err) {
1872
1873                         int user_choice;
1874
1875                         if (!no_questions_about_missing_files) {
1876                                 user_choice = MissingFile (this, err.path, err.type).get_value_or (-1);
1877                         } else {
1878                                 user_choice = -2;
1879                         }
1880
1881                         switch (user_choice) {
1882                         case 0:
1883                                 /* user added a new search location, so try again */
1884                                 goto retry;
1885
1886
1887                         case 1:
1888                                 /* user asked to quit the entire session load
1889                                  */
1890                                 return -1;
1891
1892                         case 2:
1893                                 no_questions_about_missing_files = true;
1894                                 goto retry;
1895
1896                         case 3:
1897                                 no_questions_about_missing_files = true;
1898                                 /* fallthru */
1899
1900                         case -1:
1901                         default:
1902                                 warning << _("A sound file is missing. It will be replaced by silence.") << endmsg;
1903                                 source = SourceFactory::createSilent (*this, **niter, max_framecnt, _current_frame_rate);
1904                                 break;
1905                         }
1906                 }
1907         }
1908
1909         return 0;
1910 }
1911
1912 boost::shared_ptr<Source>
1913 Session::XMLSourceFactory (const XMLNode& node)
1914 {
1915         if (node.name() != "Source") {
1916                 return boost::shared_ptr<Source>();
1917         }
1918
1919         try {
1920                 /* note: do peak building in another thread when loading session state */
1921                 return SourceFactory::create (*this, node, true);
1922         }
1923
1924         catch (failed_constructor& err) {
1925                 error << string_compose (_("Found a sound file that cannot be used by %1. Talk to the progammers."), PROGRAM_NAME) << endmsg;
1926                 return boost::shared_ptr<Source>();
1927         }
1928 }
1929
1930 int
1931 Session::save_template (string template_name)
1932 {
1933         XMLTree tree;
1934
1935         if (_state_of_the_state & CannotSave) {
1936                 return -1;
1937         }
1938
1939         std::string user_template_dir(user_template_directory());
1940
1941         if (g_mkdir_with_parents (user_template_dir.c_str(), 0755) != 0) {
1942                 error << string_compose(_("Could not create templates directory \"%1\" (%2)"),
1943                                 user_template_dir, g_strerror (errno)) << endmsg;
1944                 return -1;
1945         }
1946
1947         tree.set_root (&get_template());
1948
1949         std::string template_dir_path(user_template_dir);
1950         
1951         /* directory to put the template in */
1952         template_dir_path = Glib::build_filename (template_dir_path, template_name);
1953
1954         if (Glib::file_test (template_dir_path, Glib::FILE_TEST_EXISTS)) {
1955                 warning << string_compose(_("Template \"%1\" already exists - new version not created"),
1956                                 template_dir_path) << endmsg;
1957                 return -1;
1958         }
1959         
1960         if (g_mkdir_with_parents (template_dir_path.c_str(), 0755) != 0) {
1961                 error << string_compose(_("Could not create directory for Session template\"%1\" (%2)"),
1962                                 template_dir_path, g_strerror (errno)) << endmsg;
1963                 return -1;
1964         }
1965
1966         /* file to write */
1967         std::string template_file_path(template_dir_path);
1968         template_file_path = Glib::build_filename (template_file_path, template_name + template_suffix);
1969
1970         if (!tree.write (template_file_path)) {
1971                 error << _("template not saved") << endmsg;
1972                 return -1;
1973         }
1974
1975         /* copy plugin state directory */
1976
1977         std::string template_plugin_state_path(template_dir_path);
1978         template_plugin_state_path = Glib::build_filename (template_plugin_state_path, X_("plugins"));
1979
1980         if (g_mkdir_with_parents (template_plugin_state_path.c_str(), 0755) != 0) {
1981                 error << string_compose(_("Could not create directory for Session template plugin state\"%1\" (%2)"),
1982                                 template_plugin_state_path, g_strerror (errno)) << endmsg;
1983                 return -1;
1984         }
1985
1986         copy_files (plugins_dir(), template_plugin_state_path);
1987
1988         return 0;
1989 }
1990
1991 void
1992 Session::refresh_disk_space ()
1993 {
1994 #if __APPLE__ || (HAVE_SYS_VFS_H && HAVE_SYS_STATVFS_H)
1995         
1996         Glib::Threads::Mutex::Lock lm (space_lock);
1997
1998         /* get freespace on every FS that is part of the session path */
1999
2000         _total_free_4k_blocks = 0;
2001         _total_free_4k_blocks_uncertain = false;
2002
2003         for (vector<space_and_path>::iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2004
2005                 struct statfs statfsbuf;
2006                 statfs (i->path.c_str(), &statfsbuf);
2007
2008                 double const scale = statfsbuf.f_bsize / 4096.0;
2009
2010                 /* See if this filesystem is read-only */
2011                 struct statvfs statvfsbuf;
2012                 statvfs (i->path.c_str(), &statvfsbuf);
2013
2014                 /* f_bavail can be 0 if it is undefined for whatever
2015                    filesystem we are looking at; Samba shares mounted
2016                    via GVFS are an example of this.
2017                 */
2018                 if (statfsbuf.f_bavail == 0) {
2019                         /* block count unknown */
2020                         i->blocks = 0;
2021                         i->blocks_unknown = true;
2022                 } else if (statvfsbuf.f_flag & ST_RDONLY) {
2023                         /* read-only filesystem */
2024                         i->blocks = 0;
2025                         i->blocks_unknown = false;
2026                 } else {
2027                         /* read/write filesystem with known space */
2028                         i->blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
2029                         i->blocks_unknown = false;
2030                 }
2031
2032                 _total_free_4k_blocks += i->blocks;
2033                 if (i->blocks_unknown) {
2034                         _total_free_4k_blocks_uncertain = true;
2035                 }
2036         }
2037 #elif defined (COMPILER_MSVC)
2038         vector<string> scanned_volumes;
2039         vector<string>::iterator j;
2040         vector<space_and_path>::iterator i;
2041     DWORD nSectorsPerCluster, nBytesPerSector,
2042           nFreeClusters, nTotalClusters;
2043     char disk_drive[4];
2044         bool volume_found;
2045
2046         _total_free_4k_blocks = 0;
2047
2048         for (i = session_dirs.begin(); i != session_dirs.end(); i++) {
2049                 strncpy (disk_drive, (*i).path.c_str(), 3);
2050                 disk_drive[3] = 0;
2051                 strupr(disk_drive);
2052
2053                 volume_found = false;
2054                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2055                 {
2056                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2057                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2058                         i->blocks = (uint32_t)(nFreeBytes / 4096);
2059
2060                         for (j = scanned_volumes.begin(); j != scanned_volumes.end(); j++) {
2061                                 if (0 == j->compare(disk_drive)) {
2062                                         volume_found = true;
2063                                         break;
2064                                 }
2065                         }
2066
2067                         if (!volume_found) {
2068                                 scanned_volumes.push_back(disk_drive);
2069                                 _total_free_4k_blocks += i->blocks;
2070                         }
2071                 }
2072         }
2073
2074         if (0 == _total_free_4k_blocks) {
2075                 strncpy (disk_drive, path().c_str(), 3);
2076                 disk_drive[3] = 0;
2077
2078                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2079                 {
2080                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2081                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2082                         _total_free_4k_blocks = (uint32_t)(nFreeBytes / 4096);
2083                 }
2084         }
2085 #endif
2086 }
2087
2088 string
2089 Session::get_best_session_directory_for_new_audio ()
2090 {
2091         vector<space_and_path>::iterator i;
2092         string result = _session_dir->root_path();
2093
2094         /* handle common case without system calls */
2095
2096         if (session_dirs.size() == 1) {
2097                 return result;
2098         }
2099
2100         /* OK, here's the algorithm we're following here:
2101
2102         We want to select which directory to use for
2103         the next file source to be created. Ideally,
2104         we'd like to use a round-robin process so as to
2105         get maximum performance benefits from splitting
2106         the files across multiple disks.
2107
2108         However, in situations without much diskspace, an
2109         RR approach may end up filling up a filesystem
2110         with new files while others still have space.
2111         Its therefore important to pay some attention to
2112         the freespace in the filesystem holding each
2113         directory as well. However, if we did that by
2114         itself, we'd keep creating new files in the file
2115         system with the most space until it was as full
2116         as all others, thus negating any performance
2117         benefits of this RAID-1 like approach.
2118
2119         So, we use a user-configurable space threshold. If
2120         there are at least 2 filesystems with more than this
2121         much space available, we use RR selection between them.
2122         If not, then we pick the filesystem with the most space.
2123
2124         This gets a good balance between the two
2125         approaches.
2126         */
2127
2128         refresh_disk_space ();
2129
2130         int free_enough = 0;
2131
2132         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2133                 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2134                         free_enough++;
2135                 }
2136         }
2137
2138         if (free_enough >= 2) {
2139                 /* use RR selection process, ensuring that the one
2140                    picked works OK.
2141                 */
2142
2143                 i = last_rr_session_dir;
2144
2145                 do {
2146                         if (++i == session_dirs.end()) {
2147                                 i = session_dirs.begin();
2148                         }
2149
2150                         if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2151                                 SessionDirectory sdir(i->path);
2152                                 if (sdir.create ()) {
2153                                         result = (*i).path;
2154                                         last_rr_session_dir = i;
2155                                         return result;
2156                                 }
2157                         }
2158
2159                 } while (i != last_rr_session_dir);
2160
2161         } else {
2162
2163                 /* pick FS with the most freespace (and that
2164                    seems to actually work ...)
2165                 */
2166
2167                 vector<space_and_path> sorted;
2168                 space_and_path_ascending_cmp cmp;
2169
2170                 sorted = session_dirs;
2171                 sort (sorted.begin(), sorted.end(), cmp);
2172
2173                 for (i = sorted.begin(); i != sorted.end(); ++i) {
2174                         SessionDirectory sdir(i->path);
2175                         if (sdir.create ()) {
2176                                 result = (*i).path;
2177                                 last_rr_session_dir = i;
2178                                 return result;
2179                         }
2180                 }
2181         }
2182
2183         return result;
2184 }
2185
2186 string
2187 Session::automation_dir () const
2188 {
2189         return Glib::build_filename (_path, "automation");
2190 }
2191
2192 string
2193 Session::analysis_dir () const
2194 {
2195         return Glib::build_filename (_path, "analysis");
2196 }
2197
2198 string
2199 Session::plugins_dir () const
2200 {
2201         return Glib::build_filename (_path, "plugins");
2202 }
2203
2204 string
2205 Session::externals_dir () const
2206 {
2207         return Glib::build_filename (_path, "externals");
2208 }
2209
2210 int
2211 Session::load_bundles (XMLNode const & node)
2212 {
2213         XMLNodeList nlist = node.children();
2214         XMLNodeConstIterator niter;
2215
2216         set_dirty();
2217
2218         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2219                 if ((*niter)->name() == "InputBundle") {
2220                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, true)));
2221                 } else if ((*niter)->name() == "OutputBundle") {
2222                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, false)));
2223                 } else {
2224                         error << string_compose(_("Unknown node \"%1\" found in Bundles list from session file"), (*niter)->name()) << endmsg;
2225                         return -1;
2226                 }
2227         }
2228
2229         return 0;
2230 }
2231
2232 int
2233 Session::load_route_groups (const XMLNode& node, int version)
2234 {
2235         XMLNodeList nlist = node.children();
2236         XMLNodeConstIterator niter;
2237
2238         set_dirty ();
2239
2240         if (version >= 3000) {
2241
2242                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2243                         if ((*niter)->name() == "RouteGroup") {
2244                                 RouteGroup* rg = new RouteGroup (*this, "");
2245                                 add_route_group (rg);
2246                                 rg->set_state (**niter, version);
2247                         }
2248                 }
2249
2250         } else if (version < 3000) {
2251
2252                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2253                         if ((*niter)->name() == "EditGroup" || (*niter)->name() == "MixGroup") {
2254                                 RouteGroup* rg = new RouteGroup (*this, "");
2255                                 add_route_group (rg);
2256                                 rg->set_state (**niter, version);
2257                         }
2258                 }
2259         }
2260
2261         return 0;
2262 }
2263
2264 static bool
2265 state_file_filter (const string &str, void* /*arg*/)
2266 {
2267         return (str.length() > strlen(statefile_suffix) &&
2268                 str.find (statefile_suffix) == (str.length() - strlen (statefile_suffix)));
2269 }
2270
2271 static string
2272 remove_end(string state)
2273 {
2274         string statename(state);
2275
2276         string::size_type start,end;
2277         if ((start = statename.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
2278                 statename = statename.substr (start+1);
2279         }
2280
2281         if ((end = statename.rfind(".ardour")) == string::npos) {
2282                 end = statename.length();
2283         }
2284
2285         return string(statename.substr (0, end));
2286 }
2287
2288 vector<string>
2289 Session::possible_states (string path)
2290 {
2291         vector<string> states;
2292         find_files_matching_filter (states, path, state_file_filter, 0, false, false);
2293
2294         transform(states.begin(), states.end(), states.begin(), remove_end);
2295
2296         sort (states.begin(), states.end());
2297
2298         return states;
2299 }
2300
2301 vector<string>
2302 Session::possible_states () const
2303 {
2304         return possible_states(_path);
2305 }
2306
2307 void
2308 Session::add_route_group (RouteGroup* g)
2309 {
2310         _route_groups.push_back (g);
2311         route_group_added (g); /* EMIT SIGNAL */
2312
2313         g->RouteAdded.connect_same_thread (*this, boost::bind (&Session::route_added_to_route_group, this, _1, _2));
2314         g->RouteRemoved.connect_same_thread (*this, boost::bind (&Session::route_removed_from_route_group, this, _1, _2));
2315         g->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::route_group_property_changed, this, g));
2316
2317         set_dirty ();
2318 }
2319
2320 void
2321 Session::remove_route_group (RouteGroup& rg)
2322 {
2323         list<RouteGroup*>::iterator i;
2324
2325         if ((i = find (_route_groups.begin(), _route_groups.end(), &rg)) != _route_groups.end()) {
2326                 _route_groups.erase (i);
2327                 delete &rg;
2328
2329                 route_group_removed (); /* EMIT SIGNAL */
2330         }
2331 }
2332
2333 /** Set a new order for our route groups, without adding or removing any.
2334  *  @param groups Route group list in the new order.
2335  */
2336 void
2337 Session::reorder_route_groups (list<RouteGroup*> groups)
2338 {
2339         _route_groups = groups;
2340
2341         route_groups_reordered (); /* EMIT SIGNAL */
2342         set_dirty ();
2343 }
2344
2345
2346 RouteGroup *
2347 Session::route_group_by_name (string name)
2348 {
2349         list<RouteGroup *>::iterator i;
2350
2351         for (i = _route_groups.begin(); i != _route_groups.end(); ++i) {
2352                 if ((*i)->name() == name) {
2353                         return* i;
2354                 }
2355         }
2356         return 0;
2357 }
2358
2359 RouteGroup&
2360 Session::all_route_group() const
2361 {
2362         return *_all_route_group;
2363 }
2364
2365 void
2366 Session::add_commands (vector<Command*> const & cmds)
2367 {
2368         for (vector<Command*>::const_iterator i = cmds.begin(); i != cmds.end(); ++i) {
2369                 add_command (*i);
2370         }
2371 }
2372
2373 void
2374 Session::begin_reversible_command (const string& name)
2375 {
2376         begin_reversible_command (g_quark_from_string (name.c_str ()));
2377 }
2378
2379 /** Begin a reversible command using a GQuark to identify it.
2380  *  begin_reversible_command() and commit_reversible_command() calls may be nested,
2381  *  but there must be as many begin...()s as there are commit...()s.
2382  */
2383 void
2384 Session::begin_reversible_command (GQuark q)
2385 {
2386         /* If nested begin/commit pairs are used, we create just one UndoTransaction
2387            to hold all the commands that are committed.  This keeps the order of
2388            commands correct in the history.
2389         */
2390
2391         if (_current_trans == 0) {
2392                 /* start a new transaction */
2393                 assert (_current_trans_quarks.empty ());
2394                 _current_trans = new UndoTransaction();
2395                 _current_trans->set_name (g_quark_to_string (q));
2396         }
2397
2398         _current_trans_quarks.push_front (q);
2399 }
2400
2401 void
2402 Session::commit_reversible_command (Command *cmd)
2403 {
2404         assert (_current_trans);
2405         assert (!_current_trans_quarks.empty ());
2406
2407         struct timeval now;
2408
2409         if (cmd) {
2410                 _current_trans->add_command (cmd);
2411         }
2412
2413         _current_trans_quarks.pop_front ();
2414
2415         if (!_current_trans_quarks.empty ()) {
2416                 /* the transaction we're committing is not the top-level one */
2417                 return;
2418         }
2419
2420         if (_current_trans->empty()) {
2421                 /* no commands were added to the transaction, so just get rid of it */
2422                 delete _current_trans;
2423                 _current_trans = 0;
2424                 return;
2425         }
2426
2427         gettimeofday (&now, 0);
2428         _current_trans->set_timestamp (now);
2429
2430         _history.add (_current_trans);
2431         _current_trans = 0;
2432 }
2433
2434 static bool
2435 accept_all_audio_files (const string& path, void* /*arg*/)
2436 {
2437         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2438                 return false;
2439         }
2440
2441         if (!AudioFileSource::safe_audio_file_extension (path)) {
2442                 return false;
2443         }
2444
2445         return true;
2446 }
2447
2448 static bool
2449 accept_all_midi_files (const string& path, void* /*arg*/)
2450 {
2451         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2452                 return false;
2453         }
2454
2455         return ((path.length() > 4 && path.find (".mid") != (path.length() - 4)) ||
2456                 (path.length() > 4 && path.find (".smf") != (path.length() - 4)) ||
2457                 (path.length() > 5 && path.find (".midi") != (path.length() - 5)));
2458 }
2459
2460 static bool
2461 accept_all_state_files (const string& path, void* /*arg*/)
2462 {
2463         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2464                 return false;
2465         }
2466
2467         return (path.length() > 7 && path.find (".ardour") == (path.length() - 7));
2468 }
2469
2470 int
2471 Session::find_all_sources (string path, set<string>& result)
2472 {
2473         XMLTree tree;
2474         XMLNode* node;
2475
2476         if (!tree.read (path)) {
2477                 return -1;
2478         }
2479
2480         if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2481                 return -2;
2482         }
2483
2484         XMLNodeList nlist;
2485         XMLNodeConstIterator niter;
2486
2487         nlist = node->children();
2488
2489         set_dirty();
2490
2491         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2492
2493                 XMLProperty* prop;
2494
2495                 if ((prop = (*niter)->property (X_("type"))) == 0) {
2496                         continue;
2497                 }
2498
2499                 DataType type (prop->value());
2500
2501                 if ((prop = (*niter)->property (X_("name"))) == 0) {
2502                         continue;
2503                 }
2504
2505                 if (Glib::path_is_absolute (prop->value())) {
2506                         /* external file, ignore */
2507                         continue;
2508                 }
2509
2510                 string found_path;
2511                 bool is_new;
2512                 uint16_t chan;
2513
2514                 if (FileSource::find (*this, type, prop->value(), true, is_new, chan, found_path)) {
2515                         result.insert (found_path);
2516                 }
2517         }
2518
2519         return 0;
2520 }
2521
2522 int
2523 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2524 {
2525         vector<string> state_files;
2526         string ripped;
2527         string this_snapshot_path;
2528
2529         result.clear ();
2530
2531         ripped = _path;
2532
2533         if (ripped[ripped.length()-1] == G_DIR_SEPARATOR) {
2534                 ripped = ripped.substr (0, ripped.length() - 1);
2535         }
2536
2537         find_files_matching_filter (state_files, ripped, accept_all_state_files, (void *) 0, true, true);
2538
2539         if (state_files.empty()) {
2540                 /* impossible! */
2541                 return 0;
2542         }
2543
2544         this_snapshot_path = _path;
2545         this_snapshot_path += legalize_for_path (_current_snapshot_name);
2546         this_snapshot_path += statefile_suffix;
2547
2548         for (vector<string>::iterator i = state_files.begin(); i != state_files.end(); ++i) {
2549
2550                 if (exclude_this_snapshot && *i == this_snapshot_path) {
2551                         continue;
2552                 }
2553
2554                 if (find_all_sources (*i, result) < 0) {
2555                         return -1;
2556                 }
2557         }
2558
2559         return 0;
2560 }
2561
2562 struct RegionCounter {
2563     typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
2564     AudioSourceList::iterator iter;
2565     boost::shared_ptr<Region> region;
2566     uint32_t count;
2567
2568     RegionCounter() : count (0) {}
2569 };
2570
2571 int
2572 Session::ask_about_playlist_deletion (boost::shared_ptr<Playlist> p)
2573 {
2574         boost::optional<int> r = AskAboutPlaylistDeletion (p);
2575         return r.get_value_or (1);
2576 }
2577
2578 void
2579 Session::cleanup_regions ()
2580 {
2581         const RegionFactory::RegionMap& regions (RegionFactory::regions());
2582
2583         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end(); ++i) {
2584
2585                 uint32_t used = playlists->region_use_count (i->second);
2586
2587                 if (used == 0 && !i->second->automatic ()) {
2588                         RegionFactory::map_remove (i->second);
2589                 }
2590         }
2591
2592         /* dump the history list */
2593         _history.clear ();
2594
2595         save_state ("");
2596 }
2597
2598 int
2599 Session::cleanup_sources (CleanupReport& rep)
2600 {
2601         // FIXME: needs adaptation to midi
2602
2603         vector<boost::shared_ptr<Source> > dead_sources;
2604         string audio_path;
2605         string midi_path;
2606         vector<string> candidates;
2607         vector<string> unused;
2608         set<string> all_sources;
2609         bool used;
2610         string spath;
2611         int ret = -1;
2612         string tmppath1;
2613         string tmppath2;
2614         Searchpath asp;
2615         Searchpath msp;
2616
2617         _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
2618
2619         /* consider deleting all unused playlists */
2620
2621         if (playlists->maybe_delete_unused (boost::bind (Session::ask_about_playlist_deletion, _1))) {
2622                 ret = 0;
2623                 goto out;
2624         }
2625
2626         /* sync the "all regions" property of each playlist with its current state
2627          */
2628
2629         playlists->sync_all_regions_with_regions ();
2630
2631         /* find all un-used sources */
2632
2633         rep.paths.clear ();
2634         rep.space = 0;
2635
2636         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
2637
2638                 SourceMap::iterator tmp;
2639
2640                 tmp = i;
2641                 ++tmp;
2642
2643                 /* do not bother with files that are zero size, otherwise we remove the current "nascent"
2644                    capture files.
2645                 */
2646
2647                 if (!i->second->used() && (i->second->length(i->second->timeline_position() > 0))) {
2648                         dead_sources.push_back (i->second);
2649                         i->second->drop_references ();
2650                 }
2651
2652                 i = tmp;
2653         }
2654
2655         /* build a list of all the possible audio directories for the session */
2656
2657         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2658                 SessionDirectory sdir ((*i).path);
2659                 asp += sdir.sound_path();
2660         }
2661         audio_path += asp.to_string();
2662
2663
2664         /* build a list of all the possible midi directories for the session */
2665
2666         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2667                 SessionDirectory sdir ((*i).path);
2668                 msp += sdir.midi_path();
2669         }
2670         midi_path += msp.to_string();
2671
2672         find_files_matching_filter (candidates, audio_path, accept_all_audio_files, (void *) 0, true, true);
2673         find_files_matching_filter (candidates, midi_path, accept_all_midi_files, (void *) 0, true, true);
2674
2675         /* find all sources, but don't use this snapshot because the
2676            state file on disk still references sources we may have already
2677            dropped.
2678         */
2679
2680         find_all_sources_across_snapshots (all_sources, true);
2681
2682         /*  add our current source list
2683          */
2684
2685         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
2686                 boost::shared_ptr<FileSource> fs;
2687                 SourceMap::iterator tmp = i;
2688                 ++tmp;
2689
2690                 if ((fs = boost::dynamic_pointer_cast<FileSource> (i->second)) != 0) {
2691
2692                         if (!fs->is_stub()) {
2693
2694                                 if (playlists->source_use_count (fs) != 0) {
2695                                         all_sources.insert (fs->path());
2696                                 } else {
2697                                         
2698                                         /* we might not remove this source from disk, because it may be used
2699                                            by other snapshots, but its not being used in this version
2700                                            so lets get rid of it now, along with any representative regions
2701                                            in the region list.
2702                                         */
2703                                         
2704                                         RegionFactory::remove_regions_using_source (i->second);
2705                                         sources.erase (i);
2706                                 }
2707                         }
2708                 }
2709
2710                 i = tmp;
2711         }
2712
2713         for (vector<string>::iterator x = candidates.begin(); x != candidates.end(); ++x) {
2714
2715                 used = false;
2716                 spath = *x;
2717
2718                 for (set<string>::iterator i = all_sources.begin(); i != all_sources.end(); ++i) {
2719
2720                         tmppath1 = canonical_path (spath);
2721                         tmppath2 = canonical_path ((*i));
2722
2723                         if (tmppath1 == tmppath2) {
2724                                 used = true;
2725                                 break;
2726                         }
2727                 }
2728
2729                 if (!used) {
2730                         unused.push_back (spath);
2731                 }
2732         }
2733
2734         /* now try to move all unused files into the "dead" directory(ies) */
2735
2736         for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
2737                 struct stat statbuf;
2738
2739                 string newpath;
2740
2741                 /* don't move the file across filesystems, just
2742                    stick it in the `dead_dir_name' directory
2743                    on whichever filesystem it was already on.
2744                 */
2745
2746                 if ((*x).find ("/sounds/") != string::npos) {
2747
2748                         /* old school, go up 1 level */
2749
2750                         newpath = Glib::path_get_dirname (*x);      // "sounds"
2751                         newpath = Glib::path_get_dirname (newpath); // "session-name"
2752
2753                 } else {
2754
2755                         /* new school, go up 4 levels */
2756
2757                         newpath = Glib::path_get_dirname (*x);      // "audiofiles" or "midifiles"
2758                         newpath = Glib::path_get_dirname (newpath); // "session-name"
2759                         newpath = Glib::path_get_dirname (newpath); // "interchange"
2760                         newpath = Glib::path_get_dirname (newpath); // "session-dir"
2761                 }
2762
2763                 newpath = Glib::build_filename (newpath, dead_dir_name);
2764
2765                 if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
2766                         error << string_compose(_("Session: cannot create dead file folder \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
2767                         return -1;
2768                 }
2769
2770                 newpath = Glib::build_filename (newpath, Glib::path_get_basename ((*x)));
2771
2772                 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
2773
2774                         /* the new path already exists, try versioning */
2775
2776                         char buf[PATH_MAX+1];
2777                         int version = 1;
2778                         string newpath_v;
2779
2780                         snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
2781                         newpath_v = buf;
2782
2783                         while (Glib::file_test (newpath_v.c_str(), Glib::FILE_TEST_EXISTS) && version < 999) {
2784                                 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
2785                                 newpath_v = buf;
2786                         }
2787
2788                         if (version == 999) {
2789                                 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
2790                                                   newpath)
2791                                       << endmsg;
2792                         } else {
2793                                 newpath = newpath_v;
2794                         }
2795
2796                 } else {
2797
2798                         /* it doesn't exist, or we can't read it or something */
2799
2800                 }
2801
2802                 stat ((*x).c_str(), &statbuf);
2803
2804                 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
2805                         error << string_compose (_("cannot rename unused file source from %1 to %2 (%3)"),
2806                                           (*x), newpath, strerror (errno))
2807                               << endmsg;
2808                         goto out;
2809                 }
2810
2811                 /* see if there an easy to find peakfile for this file, and remove it.
2812                  */
2813
2814                 string base = basename_nosuffix (*x);
2815                 base += "%A"; /* this is what we add for the channel suffix of all native files,
2816                                  or for the first channel of embedded files. it will miss
2817                                  some peakfiles for other channels
2818                               */
2819                 string peakpath = peak_path (base);
2820
2821                 if (Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
2822                         if (::g_unlink (peakpath.c_str()) != 0) {
2823                                 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
2824                                                          peakpath, _path, strerror (errno))
2825                                       << endmsg;
2826                                 /* try to back out */
2827                                 ::rename (newpath.c_str(), _path.c_str());
2828                                 goto out;
2829                         }
2830                 }
2831
2832                 rep.paths.push_back (*x);
2833                 rep.space += statbuf.st_size;
2834         }
2835
2836         /* dump the history list */
2837
2838         _history.clear ();
2839
2840         /* save state so we don't end up a session file
2841            referring to non-existent sources.
2842         */
2843
2844         save_state ("");
2845         ret = 0;
2846
2847   out:
2848         _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
2849
2850         return ret;
2851 }
2852
2853 int
2854 Session::cleanup_trash_sources (CleanupReport& rep)
2855 {
2856         // FIXME: needs adaptation for MIDI
2857
2858         vector<space_and_path>::iterator i;
2859         string dead_dir;
2860
2861         rep.paths.clear ();
2862         rep.space = 0;
2863
2864         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2865
2866                 dead_dir = Glib::build_filename ((*i).path, dead_dir_name);
2867
2868                 clear_directory (dead_dir, &rep.space, &rep.paths);
2869         }
2870
2871         return 0;
2872 }
2873
2874 void
2875 Session::set_dirty ()
2876 {
2877         bool was_dirty = dirty();
2878
2879         _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
2880
2881
2882         if (!was_dirty) {
2883                 DirtyChanged(); /* EMIT SIGNAL */
2884         }
2885 }
2886
2887
2888 void
2889 Session::set_clean ()
2890 {
2891         bool was_dirty = dirty();
2892
2893         _state_of_the_state = Clean;
2894
2895
2896         if (was_dirty) {
2897                 DirtyChanged(); /* EMIT SIGNAL */
2898         }
2899 }
2900
2901 void
2902 Session::set_deletion_in_progress ()
2903 {
2904         _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
2905 }
2906
2907 void
2908 Session::clear_deletion_in_progress ()
2909 {
2910         _state_of_the_state = StateOfTheState (_state_of_the_state & (~Deletion));
2911 }
2912
2913 void
2914 Session::add_controllable (boost::shared_ptr<Controllable> c)
2915 {
2916         /* this adds a controllable to the list managed by the Session.
2917            this is a subset of those managed by the Controllable class
2918            itself, and represents the only ones whose state will be saved
2919            as part of the session.
2920         */
2921
2922         Glib::Threads::Mutex::Lock lm (controllables_lock);
2923         controllables.insert (c);
2924 }
2925
2926 struct null_deleter { void operator()(void const *) const {} };
2927
2928 void
2929 Session::remove_controllable (Controllable* c)
2930 {
2931         if (_state_of_the_state & Deletion) {
2932                 return;
2933         }
2934
2935         Glib::Threads::Mutex::Lock lm (controllables_lock);
2936
2937         Controllables::iterator x = controllables.find (boost::shared_ptr<Controllable>(c, null_deleter()));
2938
2939         if (x != controllables.end()) {
2940                 controllables.erase (x);
2941         }
2942 }
2943
2944 boost::shared_ptr<Controllable>
2945 Session::controllable_by_id (const PBD::ID& id)
2946 {
2947         Glib::Threads::Mutex::Lock lm (controllables_lock);
2948
2949         for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
2950                 if ((*i)->id() == id) {
2951                         return *i;
2952                 }
2953         }
2954
2955         return boost::shared_ptr<Controllable>();
2956 }
2957
2958 boost::shared_ptr<Controllable>
2959 Session::controllable_by_descriptor (const ControllableDescriptor& desc)
2960 {
2961         boost::shared_ptr<Controllable> c;
2962         boost::shared_ptr<Route> r;
2963
2964         switch (desc.top_level_type()) {
2965         case ControllableDescriptor::NamedRoute:
2966         {
2967                 std::string str = desc.top_level_name();
2968                 if (str == "master") {
2969                         r = _master_out;
2970                 } else if (str == "control" || str == "listen") {
2971                         r = _monitor_out;
2972                 } else {
2973                         r = route_by_name (desc.top_level_name());
2974                 }
2975                 break;
2976         }
2977
2978         case ControllableDescriptor::RemoteControlID:
2979                 r = route_by_remote_id (desc.rid());
2980                 break;
2981         }
2982
2983         if (!r) {
2984                 return c;
2985         }
2986
2987         switch (desc.subtype()) {
2988         case ControllableDescriptor::Gain:
2989                 c = r->gain_control ();
2990                 break;
2991
2992         case ControllableDescriptor::Solo:
2993                 c = r->solo_control();
2994                 break;
2995
2996         case ControllableDescriptor::Mute:
2997                 c = r->mute_control();
2998                 break;
2999
3000         case ControllableDescriptor::Recenable:
3001         {
3002                 boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track>(r);
3003
3004                 if (t) {
3005                         c = t->rec_enable_control ();
3006                 }
3007                 break;
3008         }
3009
3010         case ControllableDescriptor::PanDirection:
3011         {
3012                 c = r->pannable()->pan_azimuth_control;
3013                 break;
3014         }
3015
3016         case ControllableDescriptor::PanWidth:
3017         {
3018                 c = r->pannable()->pan_width_control;
3019                 break;
3020         }
3021
3022         case ControllableDescriptor::PanElevation:
3023         {
3024                 c = r->pannable()->pan_elevation_control;
3025                 break;
3026         }
3027
3028         case ControllableDescriptor::Balance:
3029                 /* XXX simple pan control */
3030                 break;
3031
3032         case ControllableDescriptor::PluginParameter:
3033         {
3034                 uint32_t plugin = desc.target (0);
3035                 uint32_t parameter_index = desc.target (1);
3036
3037                 /* revert to zero based counting */
3038
3039                 if (plugin > 0) {
3040                         --plugin;
3041                 }
3042
3043                 if (parameter_index > 0) {
3044                         --parameter_index;
3045                 }
3046
3047                 boost::shared_ptr<Processor> p = r->nth_plugin (plugin);
3048
3049                 if (p) {
3050                         c = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
3051                                 p->control(Evoral::Parameter(PluginAutomation, 0, parameter_index)));
3052                 }
3053                 break;
3054         }
3055
3056         case ControllableDescriptor::SendGain:
3057         {
3058                 uint32_t send = desc.target (0);
3059
3060                 /* revert to zero-based counting */
3061
3062                 if (send > 0) {
3063                         --send;
3064                 }
3065
3066                 boost::shared_ptr<Processor> p = r->nth_send (send);
3067
3068                 if (p) {
3069                         boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
3070                         boost::shared_ptr<Amp> a = s->amp();
3071                         
3072                         if (a) {
3073                                 c = s->amp()->gain_control();
3074                         }
3075                 }
3076                 break;
3077         }
3078
3079         default:
3080                 /* relax and return a null pointer */
3081                 break;
3082         }
3083
3084         return c;
3085 }
3086
3087 void
3088 Session::add_instant_xml (XMLNode& node, bool write_to_config)
3089 {
3090         if (_writable) {
3091                 Stateful::add_instant_xml (node, _path);
3092         }
3093
3094         if (write_to_config) {
3095                 Config->add_instant_xml (node);
3096         }
3097 }
3098
3099 XMLNode*
3100 Session::instant_xml (const string& node_name)
3101 {
3102         return Stateful::instant_xml (node_name, _path);
3103 }
3104
3105 int
3106 Session::save_history (string snapshot_name)
3107 {
3108         XMLTree tree;
3109
3110         if (!_writable) {
3111                 return 0;
3112         }
3113
3114         if (!Config->get_save_history() || Config->get_saved_history_depth() < 0 || 
3115             (_history.undo_depth() == 0 && _history.redo_depth() == 0)) {
3116                 return 0;
3117         }
3118
3119         if (snapshot_name.empty()) {
3120                 snapshot_name = _current_snapshot_name;
3121         }
3122
3123         const string history_filename = legalize_for_path (snapshot_name) + history_suffix;
3124         const string backup_filename = history_filename + backup_suffix;
3125         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), history_filename));
3126         const std::string backup_path(Glib::build_filename (_session_dir->root_path(), backup_filename));
3127
3128         if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3129                 if (::g_rename (xml_path.c_str(), backup_path.c_str()) != 0) {
3130                         error << _("could not backup old history file, current history not saved") << endmsg;
3131                         return -1;
3132                 }
3133         }
3134
3135         tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
3136
3137         if (!tree.write (xml_path))
3138         {
3139                 error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
3140
3141                 if (g_remove (xml_path.c_str()) != 0) {
3142                         error << string_compose(_("Could not remove history file at path \"%1\" (%2)"),
3143                                         xml_path, g_strerror (errno)) << endmsg;
3144                 }
3145                 if (::g_rename (backup_path.c_str(), xml_path.c_str()) != 0) {
3146                         error << string_compose (_("could not restore history file from backup %1 (%2)"),
3147                                         backup_path, g_strerror (errno)) << endmsg;
3148                 }
3149
3150                 return -1;
3151         }
3152
3153         return 0;
3154 }
3155
3156 int
3157 Session::restore_history (string snapshot_name)
3158 {
3159         XMLTree tree;
3160
3161         if (snapshot_name.empty()) {
3162                 snapshot_name = _current_snapshot_name;
3163         }
3164
3165         const std::string xml_filename = legalize_for_path (snapshot_name) + history_suffix;
3166         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), xml_filename));
3167
3168         info << "Loading history from " << xml_path << endmsg;
3169
3170         if (!Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3171                 info << string_compose (_("%1: no history file \"%2\" for this session."),
3172                                 _name, xml_path) << endmsg;
3173                 return 1;
3174         }
3175
3176         if (!tree.read (xml_path)) {
3177                 error << string_compose (_("Could not understand session history file \"%1\""),
3178                                 xml_path) << endmsg;
3179                 return -1;
3180         }
3181
3182         // replace history
3183         _history.clear();
3184
3185         for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
3186
3187                 XMLNode *t = *it;
3188                 UndoTransaction* ut = new UndoTransaction ();
3189                 struct timeval tv;
3190
3191                 ut->set_name(t->property("name")->value());
3192                 stringstream ss(t->property("tv-sec")->value());
3193                 ss >> tv.tv_sec;
3194                 ss.str(t->property("tv-usec")->value());
3195                 ss >> tv.tv_usec;
3196                 ut->set_timestamp(tv);
3197
3198                 for (XMLNodeConstIterator child_it  = t->children().begin();
3199                                 child_it != t->children().end(); child_it++)
3200                 {
3201                         XMLNode *n = *child_it;
3202                         Command *c;
3203
3204                         if (n->name() == "MementoCommand" ||
3205                                         n->name() == "MementoUndoCommand" ||
3206                                         n->name() == "MementoRedoCommand") {
3207
3208                                 if ((c = memento_command_factory(n))) {
3209                                         ut->add_command(c);
3210                                 }
3211
3212                         } else if (n->name() == "NoteDiffCommand") {
3213                                 PBD::ID id (n->property("midi-source")->value());
3214                                 boost::shared_ptr<MidiSource> midi_source =
3215                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3216                                 if (midi_source) {
3217                                         ut->add_command (new MidiModel::NoteDiffCommand(midi_source->model(), *n));
3218                                 } else {
3219                                         error << _("Failed to downcast MidiSource for NoteDiffCommand") << endmsg;
3220                                 }
3221
3222                         } else if (n->name() == "SysExDiffCommand") {
3223
3224                                 PBD::ID id (n->property("midi-source")->value());
3225                                 boost::shared_ptr<MidiSource> midi_source =
3226                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3227                                 if (midi_source) {
3228                                         ut->add_command (new MidiModel::SysExDiffCommand (midi_source->model(), *n));
3229                                 } else {
3230                                         error << _("Failed to downcast MidiSource for SysExDiffCommand") << endmsg;
3231                                 }
3232
3233                         } else if (n->name() == "PatchChangeDiffCommand") {
3234
3235                                 PBD::ID id (n->property("midi-source")->value());
3236                                 boost::shared_ptr<MidiSource> midi_source =
3237                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3238                                 if (midi_source) {
3239                                         ut->add_command (new MidiModel::PatchChangeDiffCommand (midi_source->model(), *n));
3240                                 } else {
3241                                         error << _("Failed to downcast MidiSource for PatchChangeDiffCommand") << endmsg;
3242                                 }
3243
3244                         } else if (n->name() == "StatefulDiffCommand") {
3245                                 if ((c = stateful_diff_command_factory (n))) {
3246                                         ut->add_command (c);
3247                                 }
3248                         } else {
3249                                 error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
3250                         }
3251                 }
3252
3253                 _history.add (ut);
3254         }
3255
3256         return 0;
3257 }
3258
3259 void
3260 Session::config_changed (std::string p, bool ours)
3261 {
3262         if (ours) {
3263                 set_dirty ();
3264         }
3265
3266         if (p == "seamless-loop") {
3267
3268         } else if (p == "rf-speed") {
3269
3270         } else if (p == "auto-loop") {
3271
3272         } else if (p == "auto-input") {
3273
3274                 if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
3275                         /* auto-input only makes a difference if we're rolling */
3276                         set_track_monitor_input_status (!config.get_auto_input());
3277                 }
3278
3279         } else if (p == "punch-in") {
3280
3281                 Location* location;
3282
3283                 if ((location = _locations->auto_punch_location()) != 0) {
3284
3285                         if (config.get_punch_in ()) {
3286                                 replace_event (SessionEvent::PunchIn, location->start());
3287                         } else {
3288                                 remove_event (location->start(), SessionEvent::PunchIn);
3289                         }
3290                 }
3291
3292         } else if (p == "punch-out") {
3293
3294                 Location* location;
3295
3296                 if ((location = _locations->auto_punch_location()) != 0) {
3297
3298                         if (config.get_punch_out()) {
3299                                 replace_event (SessionEvent::PunchOut, location->end());
3300                         } else {
3301                                 clear_events (SessionEvent::PunchOut);
3302                         }
3303                 }
3304
3305         } else if (p == "edit-mode") {
3306
3307                 Glib::Threads::Mutex::Lock lm (playlists->lock);
3308
3309                 for (SessionPlaylists::List::iterator i = playlists->playlists.begin(); i != playlists->playlists.end(); ++i) {
3310                         (*i)->set_edit_mode (Config->get_edit_mode ());
3311                 }
3312
3313         } else if (p == "use-video-sync") {
3314
3315                 waiting_for_sync_offset = config.get_use_video_sync();
3316
3317         } else if (p == "mmc-control") {
3318
3319                 //poke_midi_thread ();
3320
3321         } else if (p == "mmc-device-id" || p == "mmc-receive-id" || p == "mmc-receive-device-id") {
3322
3323                 _mmc->set_receive_device_id (Config->get_mmc_receive_device_id());
3324
3325         } else if (p == "mmc-send-id" || p == "mmc-send-device-id") {
3326
3327                 _mmc->set_send_device_id (Config->get_mmc_send_device_id());
3328
3329         } else if (p == "midi-control") {
3330
3331                 //poke_midi_thread ();
3332
3333         } else if (p == "raid-path") {
3334
3335                 setup_raid_path (config.get_raid_path());
3336
3337         } else if (p == "timecode-format") {
3338
3339                 sync_time_vars ();
3340
3341         } else if (p == "video-pullup") {
3342
3343                 sync_time_vars ();
3344
3345         } else if (p == "seamless-loop") {
3346
3347                 if (play_loop && transport_rolling()) {
3348                         // to reset diskstreams etc
3349                         request_play_loop (true);
3350                 }
3351
3352         } else if (p == "rf-speed") {
3353
3354                 cumulative_rf_motion = 0;
3355                 reset_rf_scale (0);
3356
3357         } else if (p == "click-sound") {
3358
3359                 setup_click_sounds (1);
3360
3361         } else if (p == "click-emphasis-sound") {
3362
3363                 setup_click_sounds (-1);
3364
3365         } else if (p == "clicking") {
3366
3367                 if (Config->get_clicking()) {
3368                         if (_click_io && click_data) { // don't require emphasis data
3369                                 _clicking = true;
3370                         }
3371                 } else {
3372                         _clicking = false;
3373                 }
3374
3375         } else if (p == "click-gain") {
3376                 
3377                 if (_click_gain) {
3378                         _click_gain->set_gain (Config->get_click_gain(), this);
3379                 }
3380
3381         } else if (p == "send-mtc") {
3382
3383                 if (Config->get_send_mtc ()) {
3384                         /* mark us ready to send */
3385                         next_quarter_frame_to_send = 0;
3386                 }
3387
3388         } else if (p == "send-mmc") {
3389
3390                 _mmc->enable_send (Config->get_send_mmc ());
3391
3392         } else if (p == "midi-feedback") {
3393
3394                 session_midi_feedback = Config->get_midi_feedback();
3395
3396         } else if (p == "jack-time-master") {
3397
3398                 engine().reset_timebase ();
3399
3400         } else if (p == "native-file-header-format") {
3401
3402                 if (!first_file_header_format_reset) {
3403                         reset_native_file_format ();
3404                 }
3405
3406                 first_file_header_format_reset = false;
3407
3408         } else if (p == "native-file-data-format") {
3409
3410                 if (!first_file_data_format_reset) {
3411                         reset_native_file_format ();
3412                 }
3413
3414                 first_file_data_format_reset = false;
3415
3416         } else if (p == "external-sync") {
3417                 if (!config.get_external_sync()) {
3418                         drop_sync_source ();
3419                 } else {
3420                         switch_to_sync_source (Config->get_sync_source());
3421                 }
3422         }  else if (p == "denormal-model") {
3423                 setup_fpu ();
3424         } else if (p == "history-depth") {
3425                 set_history_depth (Config->get_history_depth());
3426         } else if (p == "remote-model") {
3427                 /* XXX DO SOMETHING HERE TO TELL THE GUI THAT WE NEED
3428                    TO SET REMOTE ID'S
3429                 */
3430         } else if (p == "initial-program-change") {
3431
3432                 if (_mmc->output_port() && Config->get_initial_program_change() >= 0) {
3433                         MIDI::byte buf[2];
3434
3435                         buf[0] = MIDI::program; // channel zero by default
3436                         buf[1] = (Config->get_initial_program_change() & 0x7f);
3437
3438                         _mmc->output_port()->midimsg (buf, sizeof (buf), 0);
3439                 }
3440         } else if (p == "solo-mute-override") {
3441                 // catch_up_on_solo_mute_override ();
3442         } else if (p == "listen-position" || p == "pfl-position") {
3443                 listen_position_changed ();
3444         } else if (p == "solo-control-is-listen-control") {
3445                 solo_control_mode_changed ();
3446         } else if (p == "timecode-offset" || p == "timecode-offset-negative") {
3447                 last_timecode_valid = false;
3448         } else if (p == "playback-buffer-seconds") {
3449                 AudioSource::allocate_working_buffers (frame_rate());
3450         } else if (p == "automation-thinning-factor") {
3451                 Evoral::ControlList::set_thinning_factor (Config->get_automation_thinning_factor());
3452         } else if (p == "ltc-source-port") {
3453                 reconnect_ltc_input ();
3454         } else if (p == "ltc-sink-port") {
3455                 reconnect_ltc_output ();
3456         } else if (p == "timecode-generator-offset") {
3457                 ltc_tx_parse_offset();
3458         }
3459
3460         set_dirty ();
3461 }
3462
3463 void
3464 Session::set_history_depth (uint32_t d)
3465 {
3466         _history.set_depth (d);
3467 }
3468
3469 int
3470 Session::load_diskstreams_2X (XMLNode const & node, int)
3471 {
3472         XMLNodeList          clist;
3473         XMLNodeConstIterator citer;
3474
3475         clist = node.children();
3476
3477         for (citer = clist.begin(); citer != clist.end(); ++citer) {
3478
3479                 try {
3480                         /* diskstreams added automatically by DiskstreamCreated handler */
3481                         if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
3482                                 boost::shared_ptr<AudioDiskstream> dsp (new AudioDiskstream (*this, **citer));
3483                                 _diskstreams_2X.push_back (dsp);
3484                         } else {
3485                                 error << _("Session: unknown diskstream type in XML") << endmsg;
3486                         }
3487                 }
3488
3489                 catch (failed_constructor& err) {
3490                         error << _("Session: could not load diskstream via XML state") << endmsg;
3491                         return -1;
3492                 }
3493         }
3494
3495         return 0;
3496 }
3497
3498 /** Connect things to the MMC object */
3499 void
3500 Session::setup_midi_machine_control ()
3501 {
3502         _mmc = new MIDI::MachineControl;
3503         _mmc->set_ports (_midi_ports->mmc_input_port(), _midi_ports->mmc_output_port());
3504
3505         _mmc->Play.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
3506         _mmc->DeferredPlay.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
3507         _mmc->Stop.connect_same_thread (*this, boost::bind (&Session::mmc_stop, this, _1));
3508         _mmc->FastForward.connect_same_thread (*this, boost::bind (&Session::mmc_fast_forward, this, _1));
3509         _mmc->Rewind.connect_same_thread (*this, boost::bind (&Session::mmc_rewind, this, _1));
3510         _mmc->Pause.connect_same_thread (*this, boost::bind (&Session::mmc_pause, this, _1));
3511         _mmc->RecordPause.connect_same_thread (*this, boost::bind (&Session::mmc_record_pause, this, _1));
3512         _mmc->RecordStrobe.connect_same_thread (*this, boost::bind (&Session::mmc_record_strobe, this, _1));
3513         _mmc->RecordExit.connect_same_thread (*this, boost::bind (&Session::mmc_record_exit, this, _1));
3514         _mmc->Locate.connect_same_thread (*this, boost::bind (&Session::mmc_locate, this, _1, _2));
3515         _mmc->Step.connect_same_thread (*this, boost::bind (&Session::mmc_step, this, _1, _2));
3516         _mmc->Shuttle.connect_same_thread (*this, boost::bind (&Session::mmc_shuttle, this, _1, _2, _3));
3517         _mmc->TrackRecordStatusChange.connect_same_thread (*this, boost::bind (&Session::mmc_record_enable, this, _1, _2, _3));
3518
3519         /* also handle MIDI SPP because its so common */
3520
3521         _mmc->SPPStart.connect_same_thread (*this, boost::bind (&Session::spp_start, this));
3522         _mmc->SPPContinue.connect_same_thread (*this, boost::bind (&Session::spp_continue, this));
3523         _mmc->SPPStop.connect_same_thread (*this, boost::bind (&Session::spp_stop, this));
3524 }
3525
3526 boost::shared_ptr<Controllable>
3527 Session::solo_cut_control() const
3528 {
3529         /* the solo cut control is a bit of an anomaly, at least as of Febrary 2011. There are no other
3530            controls in Ardour that currently get presented to the user in the GUI that require
3531            access as a Controllable and are also NOT owned by some SessionObject (e.g. Route, or MonitorProcessor).
3532
3533            its actually an RCConfiguration parameter, so we use a ProxyControllable to wrap
3534            it up as a Controllable. Changes to the Controllable will just map back to the RCConfiguration
3535            parameter.
3536         */
3537
3538         return _solo_cut_control;
3539 }
3540
3541 int
3542 Session::rename (const std::string& new_name)
3543 {
3544         string legal_name = legalize_for_path (new_name);
3545         string newpath;
3546         string oldstr;
3547         string newstr;
3548         bool first = true;
3549
3550         string const old_sources_root = _session_dir->sources_root();
3551
3552         /* Rename:
3553
3554          * session directory
3555          * interchange subdirectory
3556          * session file
3557          * session history
3558          
3559          * Backup files are left unchanged and not renamed.
3560          */
3561
3562         /* pass one: not 100% safe check that the new directory names don't
3563          * already exist ...
3564          */
3565
3566         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3567                 vector<string> v;
3568
3569                 oldstr = (*i).path;
3570
3571                 /* this is a stupid hack because Glib::path_get_dirname() is
3572                  * lexical-only, and so passing it /a/b/c/ gives a different
3573                  * result than passing it /a/b/c ...
3574                  */
3575
3576                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
3577                         oldstr = oldstr.substr (0, oldstr.length() - 1);
3578                 }
3579
3580                 string base = Glib::path_get_dirname (oldstr);
3581                 string p = Glib::path_get_basename (oldstr);
3582
3583                 newstr = Glib::build_filename (base, legal_name);
3584                 
3585                 if (Glib::file_test (newstr, Glib::FILE_TEST_EXISTS)) {
3586                         return -1;
3587                 }
3588         }
3589
3590         /* Session dirs */
3591         
3592         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3593                 vector<string> v;
3594
3595                 oldstr = (*i).path;
3596
3597                 /* this is a stupid hack because Glib::path_get_dirname() is
3598                  * lexical-only, and so passing it /a/b/c/ gives a different
3599                  * result than passing it /a/b/c ...
3600                  */
3601
3602                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
3603                         oldstr = oldstr.substr (0, oldstr.length() - 1);
3604                 }
3605
3606                 string base = Glib::path_get_dirname (oldstr);
3607                 string p = Glib::path_get_basename (oldstr);
3608
3609                 newstr = Glib::build_filename (base, legal_name);
3610
3611                 cerr << "Rename " << oldstr << " => " << newstr << endl;                
3612
3613                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
3614                         error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
3615                         return 1;
3616                 }
3617
3618                 if (first) {
3619                         (*_session_dir) = newstr;
3620                         newpath = newstr;
3621                         first = 1;
3622                 }
3623
3624                 /* directory below interchange */
3625
3626                 v.push_back (newstr);
3627                 v.push_back (interchange_dir_name);
3628                 v.push_back (p);
3629
3630                 oldstr = Glib::build_filename (v);
3631
3632                 v.clear ();
3633                 v.push_back (newstr);
3634                 v.push_back (interchange_dir_name);
3635                 v.push_back (legal_name);
3636
3637                 newstr = Glib::build_filename (v);
3638                 
3639                 cerr << "Rename " << oldstr << " => " << newstr << endl;
3640                 
3641                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
3642                         error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
3643                         return 1;
3644                 }
3645         }
3646
3647         /* state file */
3648         
3649         oldstr = Glib::build_filename (newpath, _current_snapshot_name) + statefile_suffix;
3650         newstr= Glib::build_filename (newpath, legal_name) + statefile_suffix;
3651         
3652         cerr << "Rename " << oldstr << " => " << newstr << endl;                
3653
3654         if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
3655                 error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
3656                 return 1;
3657         }
3658
3659         /* history file */
3660
3661         
3662         oldstr = Glib::build_filename (newpath, _current_snapshot_name) + history_suffix;
3663
3664         if (Glib::file_test (oldstr, Glib::FILE_TEST_EXISTS))  {
3665                 newstr = Glib::build_filename (newpath, legal_name) + history_suffix;
3666                 
3667                 cerr << "Rename " << oldstr << " => " << newstr << endl;                
3668                 
3669                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
3670                         error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
3671                         return 1;
3672                 }
3673         }
3674
3675         /* update file source paths */
3676         
3677         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
3678                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
3679                 if (fs) {
3680                         string p = fs->path ();
3681                         boost::replace_all (p, old_sources_root, _session_dir->sources_root());
3682                         fs->set_path (p);
3683                 }
3684         }
3685
3686         /* remove old name from recent sessions */
3687
3688         remove_recent_sessions (_path);
3689
3690         _path = newpath;
3691         _current_snapshot_name = new_name;
3692         _name = new_name;
3693
3694         /* re-add directory separator - reverse hack to oldstr above */
3695         if (_path[_path.length()-1] != G_DIR_SEPARATOR) {
3696                 _path += G_DIR_SEPARATOR;
3697         }
3698
3699         set_dirty ();
3700
3701         /* save state again to get everything just right */
3702
3703         save_state (_current_snapshot_name);
3704
3705
3706         /* add to recent sessions */
3707
3708         store_recent_sessions (new_name, _path);
3709
3710         return 0;
3711 }
3712
3713 int
3714 Session::get_session_info_from_path (XMLTree& tree, const string& xmlpath)
3715 {
3716         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
3717                 return -1;
3718         }
3719
3720         if (!tree.read (xmlpath)) {
3721                 return -1;
3722         }
3723
3724         return 0;
3725 }
3726
3727 int
3728 Session::get_info_from_path (const string& xmlpath, float& sample_rate, SampleFormat& data_format)
3729 {
3730         XMLTree tree;
3731         bool found_sr = false;
3732         bool found_data_format = false;
3733
3734         if (get_session_info_from_path (tree, xmlpath)) {
3735                 return -1;
3736         }
3737
3738         /* sample rate */
3739
3740         const XMLProperty* prop;
3741         if ((prop = tree.root()->property (X_("sample-rate"))) != 0) {          
3742                 sample_rate = atoi (prop->value());
3743                 found_sr = true;
3744         }
3745
3746         const XMLNodeList& children (tree.root()->children());
3747         for (XMLNodeList::const_iterator c = children.begin(); c != children.end(); ++c) {
3748                 const XMLNode* child = *c;
3749                 if (child->name() == "Config") {
3750                         const XMLNodeList& options (child->children());
3751                         for (XMLNodeList::const_iterator oc = options.begin(); oc != options.end(); ++oc) {
3752                                 const XMLNode* option = *oc;
3753                                 const XMLProperty* name = option->property("name");
3754
3755                                 if (!name) {
3756                                         continue;
3757                                 }
3758
3759                                 if (name->value() == "native-file-data-format") {
3760                                         const XMLProperty* value = option->property ("value");
3761                                         if (value) {
3762                                                 SampleFormat fmt = (SampleFormat) string_2_enum (option->property ("value")->value(), fmt);
3763                                                 data_format = fmt;
3764                                                 found_data_format = true;
3765                                                 break;
3766                                         }
3767                                 }
3768                         }
3769                 }
3770                 if (found_data_format) {
3771                         break;
3772                 }
3773         }
3774
3775         return !(found_sr && found_data_format); // zero if they are both found
3776 }
3777
3778 typedef std::vector<boost::shared_ptr<FileSource> > SeveralFileSources;
3779 typedef std::map<std::string,SeveralFileSources> SourcePathMap;
3780
3781 int
3782 Session::bring_all_sources_into_session (boost::function<void(uint32_t,uint32_t,string)> callback)
3783 {
3784         uint32_t total = 0;
3785         uint32_t n = 0;
3786         SourcePathMap source_path_map;
3787         string new_path;
3788         boost::shared_ptr<AudioFileSource> afs;
3789         int ret = 0;
3790
3791         {
3792
3793                 Glib::Threads::Mutex::Lock lm (source_lock);
3794                 
3795                 cerr << " total sources = " << sources.size();
3796                 
3797                 for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
3798                         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
3799                         
3800                         if (!fs) {
3801                                 continue;
3802                         }
3803                         
3804                         if (fs->within_session()) {
3805                                 cerr << "skip " << fs->name() << endl;
3806                                 continue;
3807                         }
3808                         
3809                         if (source_path_map.find (fs->path()) != source_path_map.end()) {
3810                                 source_path_map[fs->path()].push_back (fs);
3811                         } else {
3812                                 SeveralFileSources v;
3813                                 v.push_back (fs);
3814                                 source_path_map.insert (make_pair (fs->path(), v));
3815                         }
3816                         
3817                         total++;
3818                 }
3819                 
3820                 cerr << " fsources = " << total << endl;
3821                 
3822                 for (SourcePathMap::iterator i = source_path_map.begin(); i != source_path_map.end(); ++i) {
3823                         
3824                         /* tell caller where we are */
3825                         
3826                         string old_path = i->first;
3827                         
3828                         callback (n, total, old_path);
3829                         
3830                         cerr << old_path << endl;
3831                         
3832                         new_path.clear ();
3833                         
3834                         switch (i->second.front()->type()) {
3835                         case DataType::AUDIO:
3836                                 new_path = new_audio_source_path_for_embedded (old_path);
3837                                 break;
3838                                 
3839                         case DataType::MIDI:
3840                                 break;
3841                         }
3842                         
3843                         cerr << "Move " << old_path << " => " << new_path << endl;
3844                         
3845                         if (!copy_file (old_path, new_path)) {
3846                                 cerr << "failed !\n";
3847                                 ret = -1;
3848                         }
3849                         
3850                         /* make sure we stop looking in the external
3851                            dir/folder. Remember, this is an all-or-nothing
3852                            operations, it doesn't merge just some files.
3853                         */
3854                         remove_dir_from_search_path (Glib::path_get_dirname (old_path), i->second.front()->type());
3855
3856                         for (SeveralFileSources::iterator f = i->second.begin(); f != i->second.end(); ++f) {
3857                                 (*f)->set_path (new_path);
3858                         }
3859                 }
3860         }
3861
3862         save_state ("", false, false);
3863
3864         return ret;
3865 }