fix crash when copy'ing latent plugins
[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 <string>
29 #include <cerrno>
30 #include <cstdio> /* snprintf(3) ... grrr */
31 #include <cmath>
32
33 #include <unistd.h>
34 #include <climits>
35 #include <signal.h>
36 #include <sys/time.h>
37
38 #ifdef HAVE_SYS_VFS_H
39 #include <sys/vfs.h>
40 #endif
41
42 #if defined(__APPLE__) || defined(__FreeBSD__)
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #endif
46
47 #ifdef HAVE_SYS_STATVFS_H
48 #include <sys/statvfs.h>
49 #endif
50
51 #include <glib.h>
52 #include "pbd/gstdio_compat.h"
53
54 #include <glibmm.h>
55 #include <glibmm/threads.h>
56 #include <glibmm/fileutils.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/basename.h"
66 #include "pbd/debug.h"
67 #include "pbd/enumwriter.h"
68 #include "pbd/error.h"
69 #include "pbd/file_utils.h"
70 #include "pbd/pathexpand.h"
71 #include "pbd/pthread_utils.h"
72 #include "pbd/stacktrace.h"
73 #include "pbd/convert.h"
74 #include "pbd/localtime_r.h"
75 #include "pbd/unwind.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/auditioner.h"
85 #include "ardour/automation_control.h"
86 #include "ardour/boost_debug.h"
87 #include "ardour/butler.h"
88 #include "ardour/controllable_descriptor.h"
89 #include "ardour/control_protocol_manager.h"
90 #include "ardour/directory_names.h"
91 #include "ardour/filename_extensions.h"
92 #include "ardour/graph.h"
93 #include "ardour/location.h"
94 #include "ardour/midi_model.h"
95 #include "ardour/midi_patch_manager.h"
96 #include "ardour/midi_region.h"
97 #include "ardour/midi_scene_changer.h"
98 #include "ardour/midi_source.h"
99 #include "ardour/midi_track.h"
100 #include "ardour/pannable.h"
101 #include "ardour/playlist_factory.h"
102 #include "ardour/playlist_source.h"
103 #include "ardour/port.h"
104 #include "ardour/processor.h"
105 #include "ardour/profile.h"
106 #include "ardour/proxy_controllable.h"
107 #include "ardour/recent_sessions.h"
108 #include "ardour/region_factory.h"
109 #include "ardour/revision.h"
110 #include "ardour/route_group.h"
111 #include "ardour/send.h"
112 #include "ardour/session.h"
113 #include "ardour/session_directory.h"
114 #include "ardour/session_metadata.h"
115 #include "ardour/session_playlists.h"
116 #include "ardour/session_state_utils.h"
117 #include "ardour/silentfilesource.h"
118 #include "ardour/sndfilesource.h"
119 #include "ardour/source_factory.h"
120 #include "ardour/speakers.h"
121 #include "ardour/template_utils.h"
122 #include "ardour/tempo.h"
123 #include "ardour/ticker.h"
124 #include "ardour/user_bundle.h"
125 #include "ardour/vca.h"
126 #include "ardour/vca_manager.h"
127
128 #include "control_protocol/control_protocol.h"
129
130 #include "LuaBridge/LuaBridge.h"
131
132 #include "pbd/i18n.h"
133 #include <locale.h>
134
135 using namespace std;
136 using namespace ARDOUR;
137 using namespace PBD;
138
139 #define DEBUG_UNDO_HISTORY(msg) DEBUG_TRACE (PBD::DEBUG::UndoHistory, string_compose ("%1: %2\n", __LINE__, msg));
140
141 void
142 Session::pre_engine_init (string fullpath)
143 {
144         if (fullpath.empty()) {
145                 destroy ();
146                 throw failed_constructor();
147         }
148
149         /* discover canonical fullpath */
150
151         _path = canonical_path(fullpath);
152
153         /* is it new ? */
154         if (Profile->get_trx() ) {
155                 // Waves TracksLive has a usecase of session replacement with a new one.
156                 // We should check session state file (<session_name>.ardour) existance
157                 // to determine if the session is new or not
158
159                 string full_session_name = Glib::build_filename( fullpath, _name );
160                 full_session_name += statefile_suffix;
161
162                 _is_new = !Glib::file_test (full_session_name, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
163         } else {
164                 _is_new = !Glib::file_test (_path, Glib::FileTest (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR));
165         }
166
167         /* finish initialization that can't be done in a normal C++ constructor
168            definition.
169         */
170
171         timerclear (&last_mmc_step);
172         g_atomic_int_set (&processing_prohibited, 0);
173         g_atomic_int_set (&_record_status, Disabled);
174         g_atomic_int_set (&_playback_load, 100);
175         g_atomic_int_set (&_capture_load, 100);
176         set_next_event ();
177         _all_route_group->set_active (true, this);
178         interpolation.add_channel_to (0, 0);
179
180         if (config.get_use_video_sync()) {
181                 waiting_for_sync_offset = true;
182         } else {
183                 waiting_for_sync_offset = false;
184         }
185
186         last_rr_session_dir = session_dirs.begin();
187
188         set_history_depth (Config->get_history_depth());
189
190         /* default: assume simple stereo speaker configuration */
191
192         _speakers->setup_default_speakers (2);
193
194         _solo_cut_control.reset (new ProxyControllable (_("solo cut control (dB)"), PBD::Controllable::GainLike,
195                                                         boost::bind (&RCConfiguration::set_solo_mute_gain, Config, _1),
196                                                         boost::bind (&RCConfiguration::get_solo_mute_gain, Config)));
197         add_controllable (_solo_cut_control);
198
199         /* These are all static "per-class" signals */
200
201         SourceFactory::SourceCreated.connect_same_thread (*this, boost::bind (&Session::add_source, this, _1));
202         PlaylistFactory::PlaylistCreated.connect_same_thread (*this, boost::bind (&Session::add_playlist, this, _1, _2));
203         AutomationList::AutomationListCreated.connect_same_thread (*this, boost::bind (&Session::add_automation_list, this, _1));
204         Controllable::Destroyed.connect_same_thread (*this, boost::bind (&Session::remove_controllable, this, _1));
205         IO::PortCountChanged.connect_same_thread (*this, boost::bind (&Session::ensure_buffers, this, _1));
206
207         /* stop IO objects from doing stuff until we're ready for them */
208
209         Delivery::disable_panners ();
210         IO::disable_connecting ();
211 }
212
213 int
214 Session::post_engine_init ()
215 {
216         BootMessage (_("Set block size and sample rate"));
217
218         set_block_size (_engine.samples_per_cycle());
219         set_frame_rate (_engine.sample_rate());
220
221         BootMessage (_("Using configuration"));
222
223         _midi_ports = new MidiPortManager;
224
225         MIDISceneChanger* msc;
226
227         _scene_changer = msc = new MIDISceneChanger (*this);
228         msc->set_input_port (boost::dynamic_pointer_cast<MidiPort>(scene_input_port()));
229         msc->set_output_port (boost::dynamic_pointer_cast<MidiPort>(scene_output_port()));
230
231         boost::function<framecnt_t(void)> timer_func (boost::bind (&Session::audible_frame, this));
232         boost::dynamic_pointer_cast<AsyncMIDIPort>(scene_input_port())->set_timer (timer_func);
233
234         setup_midi_machine_control ();
235
236         if (_butler->start_thread()) {
237                 return -1;
238         }
239
240         if (start_midi_thread ()) {
241                 return -1;
242         }
243
244         setup_click_sounds (0);
245         setup_midi_control ();
246
247         _engine.Halted.connect_same_thread (*this, boost::bind (&Session::engine_halted, this));
248         _engine.Xrun.connect_same_thread (*this, boost::bind (&Session::xrun_recovery, this));
249
250         try {
251                 /* tempo map requires sample rate knowledge */
252
253                 delete _tempo_map;
254                 _tempo_map = new TempoMap (_current_frame_rate);
255                 _tempo_map->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::tempo_map_changed, this, _1));
256                 _tempo_map->MetricPositionChanged.connect_same_thread (*this, boost::bind (&Session::gui_tempo_map_changed, this));
257
258                 /* MidiClock requires a tempo map */
259
260                 delete midi_clock;
261                 midi_clock = new MidiClockTicker ();
262                 midi_clock->set_session (this);
263
264                 /* crossfades require sample rate knowledge */
265
266                 SndFileSource::setup_standard_crossfades (*this, frame_rate());
267                 _engine.GraphReordered.connect_same_thread (*this, boost::bind (&Session::graph_reordered, this));
268
269                 AudioDiskstream::allocate_working_buffers();
270                 refresh_disk_space ();
271
272                 /* we're finally ready to call set_state() ... all objects have
273                  * been created, the engine is running.
274                  */
275
276                 if (state_tree) {
277                         if (set_state (*state_tree->root(), Stateful::loading_state_version)) {
278                                 return -1;
279                         }
280                 } else {
281                         // set_state() will call setup_raid_path(), but if it's a new session we need
282                         // to call setup_raid_path() here.
283                         setup_raid_path (_path);
284                 }
285
286                 /* ENGINE */
287
288                 boost::function<void (std::string)> ff (boost::bind (&Session::config_changed, this, _1, false));
289                 boost::function<void (std::string)> ft (boost::bind (&Session::config_changed, this, _1, true));
290
291                 Config->map_parameters (ff);
292                 config.map_parameters (ft);
293                 _butler->map_parameters ();
294
295                 /* Reset all panners */
296
297                 Delivery::reset_panners ();
298
299                 /* this will cause the CPM to instantiate any protocols that are in use
300                  * (or mandatory), which will pass it this Session, and then call
301                  * set_state() on each instantiated protocol to match stored state.
302                  */
303
304                 ControlProtocolManager::instance().set_session (this);
305
306                 /* This must be done after the ControlProtocolManager set_session above,
307                    as it will set states for ports which the ControlProtocolManager creates.
308                 */
309
310                 // XXX set state of MIDI::Port's
311                 // MidiPortManager::instance()->set_port_states (Config->midi_port_states ());
312
313                 /* And this must be done after the MIDI::Manager::set_port_states as
314                  * it will try to make connections whose details are loaded by set_port_states.
315                  */
316
317                 hookup_io ();
318
319                 /* Let control protocols know that we are now all connected, so they
320                  * could start talking to surfaces if they want to.
321                  */
322
323                 ControlProtocolManager::instance().midi_connectivity_established ();
324
325                 if (_is_new && !no_auto_connect()) {
326                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
327                         auto_connect_master_bus ();
328                 }
329
330                 _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
331
332                 /* update latencies */
333
334                 initialize_latencies ();
335
336                 _locations->added.connect_same_thread (*this, boost::bind (&Session::location_added, this, _1));
337                 _locations->removed.connect_same_thread (*this, boost::bind (&Session::location_removed, this, _1));
338                 _locations->changed.connect_same_thread (*this, boost::bind (&Session::locations_changed, this));
339
340         } catch (AudioEngine::PortRegistrationFailure& err) {
341                 /* handle this one in a different way than all others, so that its clear what happened */
342                 error << err.what() << endmsg;
343                 return -1;
344         } catch (...) {
345                 return -1;
346         }
347
348         BootMessage (_("Reset Remote Controls"));
349
350         // send_full_time_code (0);
351         _engine.transport_locate (0);
352
353         send_immediate_mmc (MIDI::MachineControlCommand (MIDI::MachineControl::cmdMmcReset));
354         send_immediate_mmc (MIDI::MachineControlCommand (Timecode::Time ()));
355
356         MIDI::Name::MidiPatchManager::instance().add_search_path (session_directory().midi_patch_path() );
357
358         ltc_tx_initialize();
359         /* initial program change will be delivered later; see ::config_changed() */
360
361         _state_of_the_state = Clean;
362
363         Port::set_connecting_blocked (false);
364
365         DirtyChanged (); /* EMIT SIGNAL */
366
367         if (_is_new) {
368                 save_state ("");
369         } else if (state_was_pending) {
370                 save_state ("");
371                 remove_pending_capture_state ();
372                 state_was_pending = false;
373         }
374
375         /* Now, finally, we can fill the playback buffers */
376
377         BootMessage (_("Filling playback buffers"));
378
379         boost::shared_ptr<RouteList> rl = routes.reader();
380         for (RouteList::iterator r = rl->begin(); r != rl->end(); ++r) {
381                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (*r);
382                 if (trk && !trk->hidden()) {
383                         trk->seek (_transport_frame, true);
384                 }
385         }
386
387         return 0;
388 }
389
390 void
391 Session::session_loaded ()
392 {
393         SessionLoaded();
394
395         _state_of_the_state = Clean;
396
397         DirtyChanged (); /* EMIT SIGNAL */
398
399         if (_is_new) {
400                 save_state ("");
401         } else if (state_was_pending) {
402                 save_state ("");
403                 remove_pending_capture_state ();
404                 state_was_pending = false;
405         }
406
407         /* Now, finally, we can fill the playback buffers */
408
409         BootMessage (_("Filling playback buffers"));
410         force_locate (_transport_frame, false);
411 }
412
413 string
414 Session::raid_path () const
415 {
416         Searchpath raid_search_path;
417
418         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
419                 raid_search_path += (*i).path;
420         }
421
422         return raid_search_path.to_string ();
423 }
424
425 void
426 Session::setup_raid_path (string path)
427 {
428         if (path.empty()) {
429                 return;
430         }
431
432         space_and_path sp;
433         string fspath;
434
435         session_dirs.clear ();
436
437         Searchpath search_path(path);
438         Searchpath sound_search_path;
439         Searchpath midi_search_path;
440
441         for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
442                 sp.path = *i;
443                 sp.blocks = 0; // not needed
444                 session_dirs.push_back (sp);
445
446                 SessionDirectory sdir(sp.path);
447
448                 sound_search_path += sdir.sound_path ();
449                 midi_search_path += sdir.midi_path ();
450         }
451
452         // reset the round-robin soundfile path thingie
453         last_rr_session_dir = session_dirs.begin();
454 }
455
456 bool
457 Session::path_is_within_session (const std::string& path)
458 {
459         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
460                 if (PBD::path_is_within (i->path, path)) {
461                         return true;
462                 }
463         }
464         return false;
465 }
466
467 int
468 Session::ensure_subdirs ()
469 {
470         string dir;
471
472         dir = session_directory().peak_path();
473
474         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
475                 error << string_compose(_("Session: cannot create session peakfile folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
476                 return -1;
477         }
478
479         dir = session_directory().sound_path();
480
481         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
482                 error << string_compose(_("Session: cannot create session sounds dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
483                 return -1;
484         }
485
486         dir = session_directory().midi_path();
487
488         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
489                 error << string_compose(_("Session: cannot create session midi dir \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
490                 return -1;
491         }
492
493         dir = session_directory().dead_path();
494
495         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
496                 error << string_compose(_("Session: cannot create session dead sounds folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
497                 return -1;
498         }
499
500         dir = session_directory().export_path();
501
502         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
503                 error << string_compose(_("Session: cannot create session export folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
504                 return -1;
505         }
506
507         dir = analysis_dir ();
508
509         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
510                 error << string_compose(_("Session: cannot create session analysis folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
511                 return -1;
512         }
513
514         dir = plugins_dir ();
515
516         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
517                 error << string_compose(_("Session: cannot create session plugins folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
518                 return -1;
519         }
520
521         dir = externals_dir ();
522
523         if (g_mkdir_with_parents (dir.c_str(), 0755) < 0) {
524                 error << string_compose(_("Session: cannot create session externals folder \"%1\" (%2)"), dir, strerror (errno)) << endmsg;
525                 return -1;
526         }
527
528         return 0;
529 }
530
531 /** @param session_template directory containing session template, or empty.
532  *  Caller must not hold process lock.
533  */
534 int
535 Session::create (const string& session_template, BusProfile* bus_profile)
536 {
537         if (g_mkdir_with_parents (_path.c_str(), 0755) < 0) {
538                 error << string_compose(_("Session: cannot create session folder \"%1\" (%2)"), _path, strerror (errno)) << endmsg;
539                 return -1;
540         }
541
542         if (ensure_subdirs ()) {
543                 return -1;
544         }
545
546         _writable = exists_and_writable (_path);
547
548         if (!session_template.empty()) {
549                 string in_path = (ARDOUR::Profile->get_trx () ? session_template : session_template_dir_to_file (session_template));
550
551                 FILE* in = g_fopen (in_path.c_str(), "rb");
552
553                 if (in) {
554                         /* no need to call legalize_for_path() since the string
555                          * in session_template is already a legal path name
556                          */
557                         string out_path = Glib::build_filename (_session_dir->root_path(), _name + statefile_suffix);
558
559                         FILE* out = g_fopen (out_path.c_str(), "wb");
560
561                         if (out) {
562                                 char buf[1024];
563                                 stringstream new_session;
564
565                                 while (!feof (in)) {
566                                         size_t charsRead = fread (buf, sizeof(char), 1024, in);
567
568                                         if (ferror (in)) {
569                                                 error << string_compose (_("Error reading session template file %1 (%2)"), in_path, strerror (errno)) << endmsg;
570                                                 fclose (in);
571                                                 fclose (out);
572                                                 return -1;
573                                         }
574                                         if (charsRead == 0) {
575                                                 break;
576                                         }
577                                         new_session.write (buf, charsRead);
578                                 }
579                                 fclose (in);
580
581                                 string file_contents = new_session.str();
582                                 size_t writeSize = file_contents.length();
583                                 if (fwrite (file_contents.c_str(), sizeof(char), writeSize, out) != writeSize) {
584                                         error << string_compose (_("Error writing session template file %1 (%2)"), out_path, strerror (errno)) << endmsg;
585                                         fclose (out);
586                                         return -1;
587                                 }
588                                 fclose (out);
589
590                                 _is_new = false;
591
592                                 if (!ARDOUR::Profile->get_trx()) {
593                                         /* Copy plugin state files from template to new session */
594                                         std::string template_plugins = Glib::build_filename (session_template, X_("plugins"));
595                                         copy_recurse (template_plugins, plugins_dir ());
596                                 }
597
598                                 return 0;
599
600                         } else {
601                                 error << string_compose (_("Could not open %1 for writing session template"), out_path)
602                                         << endmsg;
603                                 fclose(in);
604                                 return -1;
605                         }
606
607                 } else {
608                         error << string_compose (_("Could not open session template %1 for reading"), in_path)
609                                 << endmsg;
610                         return -1;
611                 }
612
613         }
614
615         if (Profile->get_trx()) {
616
617                 /* set initial start + end point : ARDOUR::Session::session_end_shift long.
618                    Remember that this is a brand new session. Sessions
619                    loaded from saved state will get this range from the saved state.
620                 */
621
622                 set_session_range_location (0, 0);
623
624                 /* Initial loop location, from absolute zero, length 10 seconds  */
625
626                 Location* loc = new Location (*this, 0, 10.0 * _engine.sample_rate(), _("Loop"),  Location::IsAutoLoop);
627                 _locations->add (loc, true);
628                 set_auto_loop_location (loc);
629         }
630
631         _state_of_the_state = Clean;
632
633         /* set up Master Out and Monitor Out if necessary */
634
635         if (bus_profile) {
636
637                 RouteList rl;
638                 ChanCount count(DataType::AUDIO, bus_profile->master_out_channels);
639
640                 // Waves Tracks: always create master bus for Tracks
641                 if (ARDOUR::Profile->get_trx() || bus_profile->master_out_channels) {
642                         boost::shared_ptr<Route> r (new Route (*this, _("Master"), PresentationInfo::MasterOut, DataType::AUDIO));
643                         if (r->init ()) {
644                                 return -1;
645                         }
646
647                         BOOST_MARK_ROUTE(r);
648
649                         {
650                                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
651                                 r->input()->ensure_io (count, false, this);
652                                 r->output()->ensure_io (count, false, this);
653                         }
654
655                         rl.push_back (r);
656
657                 } else {
658                         /* prohibit auto-connect to master, because there isn't one */
659                         bus_profile->output_ac = AutoConnectOption (bus_profile->output_ac & ~AutoConnectMaster);
660                 }
661
662                 if (!rl.empty()) {
663                         add_routes (rl, false, false, false, PresentationInfo::max_order);
664                 }
665
666                 // Waves Tracks: Skip this. Always use autoconnection for Tracks
667                 if (!ARDOUR::Profile->get_trx()) {
668
669                         /* this allows the user to override settings with an environment variable.
670                          */
671
672                         if (no_auto_connect()) {
673                                 bus_profile->input_ac = AutoConnectOption (0);
674                                 bus_profile->output_ac = AutoConnectOption (0);
675                         }
676
677                         Config->set_input_auto_connect (bus_profile->input_ac);
678                         Config->set_output_auto_connect (bus_profile->output_ac);
679                 }
680         }
681
682         if (Config->get_use_monitor_bus() && bus_profile) {
683                 add_monitor_section ();
684         }
685
686         return 0;
687 }
688
689 void
690 Session::maybe_write_autosave()
691 {
692         if (dirty() && record_status() != Recording) {
693                 save_state("", true);
694         }
695 }
696
697 void
698 Session::remove_pending_capture_state ()
699 {
700         std::string pending_state_file_path(_session_dir->root_path());
701
702         pending_state_file_path = Glib::build_filename (pending_state_file_path, legalize_for_path (_current_snapshot_name) + pending_suffix);
703
704         if (!Glib::file_test (pending_state_file_path, Glib::FILE_TEST_EXISTS)) return;
705
706         if (g_remove (pending_state_file_path.c_str()) != 0) {
707                 error << string_compose(_("Could not remove pending capture state at path \"%1\" (%2)"),
708                                 pending_state_file_path, g_strerror (errno)) << endmsg;
709         }
710 }
711
712 /** Rename a state file.
713  *  @param old_name Old snapshot name.
714  *  @param new_name New snapshot name.
715  */
716 void
717 Session::rename_state (string old_name, string new_name)
718 {
719         if (old_name == _current_snapshot_name || old_name == _name) {
720                 /* refuse to rename the current snapshot or the "main" one */
721                 return;
722         }
723
724         const string old_xml_filename = legalize_for_path (old_name) + statefile_suffix;
725         const string new_xml_filename = legalize_for_path (new_name) + statefile_suffix;
726
727         const std::string old_xml_path(Glib::build_filename (_session_dir->root_path(), old_xml_filename));
728         const std::string new_xml_path(Glib::build_filename (_session_dir->root_path(), new_xml_filename));
729
730         if (::g_rename (old_xml_path.c_str(), new_xml_path.c_str()) != 0) {
731                 error << string_compose(_("could not rename snapshot %1 to %2 (%3)"),
732                                 old_name, new_name, g_strerror(errno)) << endmsg;
733         }
734 }
735
736 /** Remove a state file.
737  *  @param snapshot_name Snapshot name.
738  */
739 void
740 Session::remove_state (string snapshot_name)
741 {
742         if (!_writable || snapshot_name == _current_snapshot_name || snapshot_name == _name) {
743                 // refuse to remove the current snapshot or the "main" one
744                 return;
745         }
746
747         std::string xml_path(_session_dir->root_path());
748
749         xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
750
751         if (!create_backup_file (xml_path)) {
752                 // don't remove it if a backup can't be made
753                 // create_backup_file will log the error.
754                 return;
755         }
756
757         // and delete it
758         if (g_remove (xml_path.c_str()) != 0) {
759                 error << string_compose(_("Could not remove session file at path \"%1\" (%2)"),
760                                 xml_path, g_strerror (errno)) << endmsg;
761         }
762 }
763
764 /** @param snapshot_name Name to save under, without .ardour / .pending prefix */
765 int
766 Session::save_state (string snapshot_name, bool pending, bool switch_to_snapshot, bool template_only)
767 {
768         DEBUG_TRACE (DEBUG::Locale, string_compose ("Session::save_state locale '%1'\n", setlocale (LC_NUMERIC, NULL)));
769
770         XMLTree tree;
771         std::string xml_path(_session_dir->root_path());
772
773         /* prevent concurrent saves from different threads */
774
775         Glib::Threads::Mutex::Lock lm (save_state_lock);
776
777         if (!_writable || (_state_of_the_state & CannotSave)) {
778                 return 1;
779         }
780
781         if (g_atomic_int_get(&_suspend_save)) {
782                 _save_queued = true;
783                 return 1;
784         }
785         _save_queued = false;
786
787         if (!_engine.connected ()) {
788                 error << string_compose (_("the %1 audio engine is not connected and state saving would lose all I/O connections. Session not saved"),
789                                          PROGRAM_NAME)
790                       << endmsg;
791                 return 1;
792         }
793
794 #ifndef NDEBUG
795         const int64_t save_start_time = g_get_monotonic_time();
796 #endif
797
798         /* tell sources we're saving first, in case they write out to a new file
799          * which should be saved with the state rather than the old one */
800         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
801                 try {
802                         i->second->session_saved();
803                 } catch (Evoral::SMF::FileError& e) {
804                         error << string_compose ("Could not write to MIDI file %1; MIDI data not saved.", e.file_name ()) << endmsg;
805                 }
806         }
807
808         SessionSaveUnderway (); /* EMIT SIGNAL */
809
810         bool mark_as_clean = true;
811
812         if (!snapshot_name.empty() && !switch_to_snapshot) {
813                 mark_as_clean = false;
814         }
815
816         if (template_only) {
817                 mark_as_clean = false;
818                 tree.set_root (&get_template());
819         } else {
820                 tree.set_root (&get_state());
821         }
822
823         if (snapshot_name.empty()) {
824                 snapshot_name = _current_snapshot_name;
825         } else if (switch_to_snapshot) {
826                 set_snapshot_name (snapshot_name);
827         }
828
829         assert (!snapshot_name.empty());
830
831         if (!pending) {
832
833                 /* proper save: use statefile_suffix (.ardour in English) */
834
835                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + statefile_suffix);
836
837                 /* make a backup copy of the old file */
838
839                 if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS) && !create_backup_file (xml_path)) {
840                         // create_backup_file will log the error
841                         return -1;
842                 }
843
844         } else {
845
846                 /* pending save: use pending_suffix (.pending in English) */
847                 xml_path = Glib::build_filename (xml_path, legalize_for_path (snapshot_name) + pending_suffix);
848         }
849
850         std::string tmp_path(_session_dir->root_path());
851         tmp_path = Glib::build_filename (tmp_path, legalize_for_path (snapshot_name) + temp_suffix);
852
853         cerr << "actually writing state to " << tmp_path << endl;
854
855         if (!tree.write (tmp_path)) {
856                 error << string_compose (_("state could not be saved to %1"), tmp_path) << endmsg;
857                 if (g_remove (tmp_path.c_str()) != 0) {
858                         error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
859                                         tmp_path, g_strerror (errno)) << endmsg;
860                 }
861                 return -1;
862
863         } else {
864
865                 cerr << "renaming state to " << xml_path << endl;
866
867                 if (::g_rename (tmp_path.c_str(), xml_path.c_str()) != 0) {
868                         error << string_compose (_("could not rename temporary session file %1 to %2 (%3)"),
869                                         tmp_path, xml_path, g_strerror(errno)) << endmsg;
870                         if (g_remove (tmp_path.c_str()) != 0) {
871                                 error << string_compose(_("Could not remove temporary session file at path \"%1\" (%2)"),
872                                                 tmp_path, g_strerror (errno)) << endmsg;
873                         }
874                         return -1;
875                 }
876         }
877
878         if (!pending) {
879
880                 save_history (snapshot_name);
881
882                 if (mark_as_clean) {
883                         bool was_dirty = dirty();
884
885                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~Dirty);
886
887                         if (was_dirty) {
888                                 DirtyChanged (); /* EMIT SIGNAL */
889                         }
890                 }
891
892                 StateSaved (snapshot_name); /* EMIT SIGNAL */
893         }
894
895 #ifndef NDEBUG
896         const int64_t elapsed_time_us = g_get_monotonic_time() - save_start_time;
897         cerr << "saved state in " << fixed << setprecision (1) << elapsed_time_us / 1000. << " ms\n";
898 #endif
899         return 0;
900 }
901
902 int
903 Session::restore_state (string snapshot_name)
904 {
905         if (load_state (snapshot_name) == 0) {
906                 set_state (*state_tree->root(), Stateful::loading_state_version);
907         }
908
909         return 0;
910 }
911
912 int
913 Session::load_state (string snapshot_name)
914 {
915         delete state_tree;
916         state_tree = 0;
917
918         state_was_pending = false;
919
920         /* check for leftover pending state from a crashed capture attempt */
921
922         std::string xmlpath(_session_dir->root_path());
923         xmlpath = Glib::build_filename (xmlpath, legalize_for_path (snapshot_name) + pending_suffix);
924
925         if (Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
926
927                 /* there is pending state from a crashed capture attempt */
928
929                 boost::optional<int> r = AskAboutPendingState();
930                 if (r.get_value_or (1)) {
931                         state_was_pending = true;
932                 }
933         }
934
935         if (!state_was_pending) {
936                 xmlpath = Glib::build_filename (_session_dir->root_path(), snapshot_name);
937         }
938
939         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
940                 xmlpath = Glib::build_filename (_session_dir->root_path(), legalize_for_path (snapshot_name) + statefile_suffix);
941                 if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
942                         error << string_compose(_("%1: session file \"%2\" doesn't exist!"), _name, xmlpath) << endmsg;
943                         return 1;
944                 }
945         }
946
947         state_tree = new XMLTree;
948
949         set_dirty();
950
951         _writable = exists_and_writable (xmlpath) && exists_and_writable(Glib::path_get_dirname(xmlpath));
952
953         if (!state_tree->read (xmlpath)) {
954                 error << string_compose(_("Could not understand session file %1"), xmlpath) << endmsg;
955                 delete state_tree;
956                 state_tree = 0;
957                 return -1;
958         }
959
960         XMLNode const & root (*state_tree->root());
961
962         if (root.name() != X_("Session")) {
963                 error << string_compose (_("Session file %1 is not a session"), xmlpath) << endmsg;
964                 delete state_tree;
965                 state_tree = 0;
966                 return -1;
967         }
968
969         XMLProperty const * prop;
970
971         if ((prop = root.property ("version")) == 0) {
972                 /* no version implies very old version of Ardour */
973                 Stateful::loading_state_version = 1000;
974         } else {
975                 if (prop->value().find ('.') != string::npos) {
976                         /* old school version format */
977                         if (prop->value()[0] == '2') {
978                                 Stateful::loading_state_version = 2000;
979                         } else {
980                                 Stateful::loading_state_version = 3000;
981                         }
982                 } else {
983                         Stateful::loading_state_version = atoi (prop->value());
984                 }
985         }
986
987         if (Stateful::loading_state_version < CURRENT_SESSION_FILE_VERSION && _writable) {
988
989                 std::string backup_path(_session_dir->root_path());
990                 std::string backup_filename = string_compose ("%1-%2%3", legalize_for_path (snapshot_name), Stateful::loading_state_version, statefile_suffix);
991                 backup_path = Glib::build_filename (backup_path, backup_filename);
992
993                 // only create a backup for a given statefile version once
994
995                 if (!Glib::file_test (backup_path, Glib::FILE_TEST_EXISTS)) {
996
997                         VersionMismatch (xmlpath, backup_path);
998
999                         if (!copy_file (xmlpath, backup_path)) {;
1000                                 return -1;
1001                         }
1002                 }
1003         }
1004
1005         save_snapshot_name (snapshot_name);
1006
1007         return 0;
1008 }
1009
1010 int
1011 Session::load_options (const XMLNode& node)
1012 {
1013         LocaleGuard lg;
1014         config.set_variables (node);
1015         return 0;
1016 }
1017
1018 bool
1019 Session::save_default_options ()
1020 {
1021         return config.save_state();
1022 }
1023
1024 XMLNode&
1025 Session::get_state()
1026 {
1027         return state(true);
1028 }
1029
1030 XMLNode&
1031 Session::get_template()
1032 {
1033         /* if we don't disable rec-enable, diskstreams
1034            will believe they need to store their capture
1035            sources in their state node.
1036         */
1037
1038         disable_record (false);
1039
1040         return state(false);
1041 }
1042
1043 XMLNode&
1044 Session::state (bool full_state)
1045 {
1046         LocaleGuard lg;
1047         XMLNode* node = new XMLNode("Session");
1048         XMLNode* child;
1049
1050         char buf[16];
1051         snprintf(buf, sizeof(buf), "%d", CURRENT_SESSION_FILE_VERSION);
1052         node->add_property("version", buf);
1053
1054         child = node->add_child ("ProgramVersion");
1055         child->add_property("created-with", created_with);
1056
1057         std::string modified_with = string_compose ("%1 %2", PROGRAM_NAME, revision);
1058         child->add_property("modified-with", modified_with);
1059
1060         /* store configuration settings */
1061
1062         if (full_state) {
1063
1064                 node->add_property ("name", _name);
1065                 snprintf (buf, sizeof (buf), "%" PRId64, _base_frame_rate);
1066                 node->add_property ("sample-rate", buf);
1067
1068                 if (session_dirs.size() > 1) {
1069
1070                         string p;
1071
1072                         vector<space_and_path>::iterator i = session_dirs.begin();
1073                         vector<space_and_path>::iterator next;
1074
1075                         ++i; /* skip the first one */
1076                         next = i;
1077                         ++next;
1078
1079                         while (i != session_dirs.end()) {
1080
1081                                 p += (*i).path;
1082
1083                                 if (next != session_dirs.end()) {
1084                                         p += G_SEARCHPATH_SEPARATOR;
1085                                 } else {
1086                                         break;
1087                                 }
1088
1089                                 ++next;
1090                                 ++i;
1091                         }
1092
1093                         child = node->add_child ("Path");
1094                         child->add_content (p);
1095                 }
1096         }
1097
1098         node->add_property ("end-is-free", _session_range_end_is_free ? X_("yes") : X_("no"));
1099
1100         /* save the ID counter */
1101
1102         snprintf (buf, sizeof (buf), "%" PRIu64, ID::counter());
1103         node->add_property ("id-counter", buf);
1104
1105         snprintf (buf, sizeof (buf), "%u", name_id_counter ());
1106         node->add_property ("name-counter", buf);
1107
1108         /* save the event ID counter */
1109
1110         snprintf (buf, sizeof (buf), "%d", Evoral::event_id_counter());
1111         node->add_property ("event-counter", buf);
1112
1113         /* save the VCA counter */
1114
1115         snprintf (buf, sizeof (buf), "%" PRIu32, VCA::get_next_vca_number());
1116         node->add_property ("vca-counter", buf);
1117
1118         /* various options */
1119
1120         list<XMLNode*> midi_port_nodes = _midi_ports->get_midi_port_states();
1121         if (!midi_port_nodes.empty()) {
1122                 XMLNode* midi_port_stuff = new XMLNode ("MIDIPorts");
1123                 for (list<XMLNode*>::const_iterator n = midi_port_nodes.begin(); n != midi_port_nodes.end(); ++n) {
1124                         midi_port_stuff->add_child_nocopy (**n);
1125                 }
1126                 node->add_child_nocopy (*midi_port_stuff);
1127         }
1128
1129         node->add_child_nocopy (config.get_variables ());
1130
1131         node->add_child_nocopy (ARDOUR::SessionMetadata::Metadata()->get_state());
1132
1133         child = node->add_child ("Sources");
1134
1135         if (full_state) {
1136                 Glib::Threads::Mutex::Lock sl (source_lock);
1137
1138                 for (SourceMap::iterator siter = sources.begin(); siter != sources.end(); ++siter) {
1139
1140                         /* Don't save information about non-file Sources, or
1141                          * about non-destructive file sources that are empty
1142                          * and unused by any regions.
1143                         */
1144
1145                         boost::shared_ptr<FileSource> fs;
1146
1147                         if ((fs = boost::dynamic_pointer_cast<FileSource> (siter->second)) != 0) {
1148
1149                                 if (!fs->destructive()) {
1150                                         if (fs->empty() && !fs->used()) {
1151                                                 continue;
1152                                         }
1153                                 }
1154
1155                                 child->add_child_nocopy (siter->second->get_state());
1156                         }
1157                 }
1158         }
1159
1160         child = node->add_child ("Regions");
1161
1162         if (full_state) {
1163                 Glib::Threads::Mutex::Lock rl (region_lock);
1164                 const RegionFactory::RegionMap& region_map (RegionFactory::all_regions());
1165                 for (RegionFactory::RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
1166                         boost::shared_ptr<Region> r = i->second;
1167                         /* only store regions not attached to playlists */
1168                         if (r->playlist() == 0) {
1169                                 if (boost::dynamic_pointer_cast<AudioRegion>(r)) {
1170                                         child->add_child_nocopy ((boost::dynamic_pointer_cast<AudioRegion>(r))->get_basic_state ());
1171                                 } else {
1172                                         child->add_child_nocopy (r->get_state ());
1173                                 }
1174                         }
1175                 }
1176
1177                 RegionFactory::CompoundAssociations& cassocs (RegionFactory::compound_associations());
1178
1179                 if (!cassocs.empty()) {
1180                         XMLNode* ca = node->add_child (X_("CompoundAssociations"));
1181
1182                         for (RegionFactory::CompoundAssociations::iterator i = cassocs.begin(); i != cassocs.end(); ++i) {
1183                                 char buf[64];
1184                                 XMLNode* can = new XMLNode (X_("CompoundAssociation"));
1185                                 i->first->id().print (buf, sizeof (buf));
1186                                 can->add_property (X_("copy"), buf);
1187                                 i->second->id().print (buf, sizeof (buf));
1188                                 can->add_property (X_("original"), buf);
1189                                 ca->add_child_nocopy (*can);
1190                         }
1191                 }
1192         }
1193
1194
1195
1196         if (full_state) {
1197
1198                 if (_locations) {
1199                         node->add_child_nocopy (_locations->get_state());
1200                 }
1201         } else {
1202                 Locations loc (*this);
1203                 // for a template, just create a new Locations, populate it
1204                 // with the default start and end, and get the state for that.
1205                 Location* range = new Location (*this, 0, 0, _("session"), Location::IsSessionRange);
1206                 range->set (max_framepos, 0);
1207                 loc.add (range);
1208                 XMLNode& locations_state = loc.get_state();
1209
1210                 if (ARDOUR::Profile->get_trx() && _locations) {
1211                         // For tracks we need stored the Auto Loop Range and all MIDI markers.
1212                         for (Locations::LocationList::const_iterator i = _locations->list ().begin (); i != _locations->list ().end (); ++i) {
1213                                 if ((*i)->is_mark () || (*i)->is_auto_loop ()) {
1214                                         locations_state.add_child_nocopy ((*i)->get_state ());
1215                                 }
1216                         }
1217                 }
1218                 node->add_child_nocopy (locations_state);
1219         }
1220
1221         child = node->add_child ("Bundles");
1222         {
1223                 boost::shared_ptr<BundleList> bundles = _bundles.reader ();
1224                 for (BundleList::iterator i = bundles->begin(); i != bundles->end(); ++i) {
1225                         boost::shared_ptr<UserBundle> b = boost::dynamic_pointer_cast<UserBundle> (*i);
1226                         if (b) {
1227                                 child->add_child_nocopy (b->get_state());
1228                         }
1229                 }
1230         }
1231
1232         node->add_child_nocopy (_vca_manager->get_state());
1233
1234         child = node->add_child ("Routes");
1235         {
1236                 boost::shared_ptr<RouteList> r = routes.reader ();
1237
1238                 RoutePublicOrderSorter cmp;
1239                 RouteList public_order (*r);
1240                 public_order.sort (cmp);
1241
1242                 /* the sort should have put the monitor out first */
1243
1244                 if (_monitor_out) {
1245                         assert (_monitor_out == public_order.front());
1246                 }
1247
1248                 for (RouteList::iterator i = public_order.begin(); i != public_order.end(); ++i) {
1249                         if (!(*i)->is_auditioner()) {
1250                                 if (full_state) {
1251                                         child->add_child_nocopy ((*i)->get_state());
1252                                 } else {
1253                                         child->add_child_nocopy ((*i)->get_template());
1254                                 }
1255                         }
1256                 }
1257         }
1258
1259         playlists->add_state (node, full_state);
1260
1261         child = node->add_child ("RouteGroups");
1262         for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
1263                 child->add_child_nocopy ((*i)->get_state());
1264         }
1265
1266         if (_click_io) {
1267                 XMLNode* gain_child = node->add_child ("Click");
1268                 gain_child->add_child_nocopy (_click_io->state (full_state));
1269                 gain_child->add_child_nocopy (_click_gain->state (full_state));
1270         }
1271
1272         if (_ltc_input) {
1273                 XMLNode* ltc_input_child = node->add_child ("LTC-In");
1274                 ltc_input_child->add_child_nocopy (_ltc_input->state (full_state));
1275         }
1276
1277         if (_ltc_input) {
1278                 XMLNode* ltc_output_child = node->add_child ("LTC-Out");
1279                 ltc_output_child->add_child_nocopy (_ltc_output->state (full_state));
1280         }
1281
1282         node->add_child_nocopy (_speakers->get_state());
1283         node->add_child_nocopy (_tempo_map->get_state());
1284         node->add_child_nocopy (get_control_protocol_state());
1285
1286         if (_extra_xml) {
1287                 node->add_child_copy (*_extra_xml);
1288         }
1289
1290         {
1291                 Glib::Threads::Mutex::Lock lm (lua_lock);
1292                 std::string saved;
1293                 {
1294                         luabridge::LuaRef savedstate ((*_lua_save)());
1295                         saved = savedstate.cast<std::string>();
1296                 }
1297                 lua.collect_garbage ();
1298                 lm.release ();
1299
1300                 gchar* b64 = g_base64_encode ((const guchar*)saved.c_str (), saved.size ());
1301                 std::string b64s (b64);
1302                 g_free (b64);
1303
1304                 XMLNode* script_node = new XMLNode (X_("Script"));
1305                 script_node->add_property (X_("lua"), LUA_VERSION);
1306                 script_node->add_content (b64s);
1307                 node->add_child_nocopy (*script_node);
1308         }
1309
1310         return *node;
1311 }
1312
1313 XMLNode&
1314 Session::get_control_protocol_state ()
1315 {
1316         ControlProtocolManager& cpm (ControlProtocolManager::instance());
1317         return cpm.get_state();
1318 }
1319
1320 int
1321 Session::set_state (const XMLNode& node, int version)
1322 {
1323         LocaleGuard lg;
1324         XMLNodeList nlist;
1325         XMLNode* child;
1326         XMLProperty const * prop;
1327         int ret = -1;
1328
1329         _state_of_the_state = StateOfTheState (_state_of_the_state|CannotSave);
1330
1331         if (node.name() != X_("Session")) {
1332                 fatal << _("programming error: Session: incorrect XML node sent to set_state()") << endmsg;
1333                 goto out;
1334         }
1335
1336         if ((prop = node.property ("name")) != 0) {
1337                 _name = prop->value ();
1338         }
1339
1340         if ((prop = node.property (X_("sample-rate"))) != 0) {
1341
1342                 _base_frame_rate = atoi (prop->value());
1343                 _nominal_frame_rate = _base_frame_rate;
1344
1345                 assert (AudioEngine::instance()->running ());
1346                 if (_base_frame_rate != AudioEngine::instance()->sample_rate ()) {
1347                         boost::optional<int> r = AskAboutSampleRateMismatch (_base_frame_rate, _current_frame_rate);
1348                         if (r.get_value_or (0)) {
1349                                 goto out;
1350                         }
1351                 }
1352         }
1353
1354         created_with = "unknown";
1355         if ((child = find_named_node (node, "ProgramVersion")) != 0) {
1356                 if ((prop = child->property (X_("created-with"))) != 0) {
1357                         created_with = prop->value ();
1358                 }
1359         }
1360
1361         setup_raid_path(_session_dir->root_path());
1362
1363         if ((prop = node.property (X_("end-is-free"))) != 0) {
1364                 _session_range_end_is_free = string_is_affirmative (prop->value());
1365         }
1366
1367         if ((prop = node.property (X_("id-counter"))) != 0) {
1368                 uint64_t x;
1369                 sscanf (prop->value().c_str(), "%" PRIu64, &x);
1370                 ID::init_counter (x);
1371         } else {
1372                 /* old sessions used a timebased counter, so fake
1373                    the startup ID counter based on a standard
1374                    timestamp.
1375                 */
1376                 time_t now;
1377                 time (&now);
1378                 ID::init_counter (now);
1379         }
1380
1381         if ((prop = node.property (X_("name-counter"))) != 0) {
1382                 init_name_id_counter (atoi (prop->value()));
1383         }
1384
1385         if ((prop = node.property (X_("event-counter"))) != 0) {
1386                 Evoral::init_event_id_counter (atoi (prop->value()));
1387         }
1388
1389         if ((prop = node.property (X_("vca-counter"))) != 0) {
1390                 uint32_t x;
1391                 sscanf (prop->value().c_str(), "%" PRIu32, &x);
1392                 VCA::set_next_vca_number (x);
1393         } else {
1394                 VCA::set_next_vca_number (1);
1395         }
1396
1397         if ((child = find_named_node (node, "MIDIPorts")) != 0) {
1398                 _midi_ports->set_midi_port_states (child->children());
1399         }
1400
1401         IO::disable_connecting ();
1402
1403         Stateful::save_extra_xml (node);
1404
1405         if (((child = find_named_node (node, "Options")) != 0)) { /* old style */
1406                 load_options (*child);
1407         } else if ((child = find_named_node (node, "Config")) != 0) { /* new style */
1408                 load_options (*child);
1409         } else {
1410                 error << _("Session: XML state has no options section") << endmsg;
1411         }
1412
1413         if (version >= 3000) {
1414                 if ((child = find_named_node (node, "Metadata")) == 0) {
1415                         warning << _("Session: XML state has no metadata section") << endmsg;
1416                 } else if ( ARDOUR::SessionMetadata::Metadata()->set_state (*child, version) ) {
1417                         goto out;
1418                 }
1419         }
1420
1421         if ((child = find_named_node (node, X_("Speakers"))) != 0) {
1422                 _speakers->set_state (*child, version);
1423         }
1424
1425         if ((child = find_named_node (node, "Sources")) == 0) {
1426                 error << _("Session: XML state has no sources section") << endmsg;
1427                 goto out;
1428         } else if (load_sources (*child)) {
1429                 goto out;
1430         }
1431
1432         if ((child = find_named_node (node, "TempoMap")) == 0) {
1433                 error << _("Session: XML state has no Tempo Map section") << endmsg;
1434                 goto out;
1435         } else if (_tempo_map->set_state (*child, version)) {
1436                 goto out;
1437         }
1438
1439         if ((child = find_named_node (node, "Locations")) == 0) {
1440                 error << _("Session: XML state has no locations section") << endmsg;
1441                 goto out;
1442         } else if (_locations->set_state (*child, version)) {
1443                 goto out;
1444         }
1445
1446         locations_changed ();
1447
1448         if (_session_range_location) {
1449                 AudioFileSource::set_header_position_offset (_session_range_location->start());
1450         }
1451
1452         if ((child = find_named_node (node, "Regions")) == 0) {
1453                 error << _("Session: XML state has no Regions section") << endmsg;
1454                 goto out;
1455         } else if (load_regions (*child)) {
1456                 goto out;
1457         }
1458
1459         if ((child = find_named_node (node, "Playlists")) == 0) {
1460                 error << _("Session: XML state has no playlists section") << endmsg;
1461                 goto out;
1462         } else if (playlists->load (*this, *child)) {
1463                 goto out;
1464         }
1465
1466         if ((child = find_named_node (node, "UnusedPlaylists")) == 0) {
1467                 // this is OK
1468         } else if (playlists->load_unused (*this, *child)) {
1469                 goto out;
1470         }
1471
1472         if ((child = find_named_node (node, "CompoundAssociations")) != 0) {
1473                 if (load_compounds (*child)) {
1474                         goto out;
1475                 }
1476         }
1477
1478         if (version >= 3000) {
1479                 if ((child = find_named_node (node, "Bundles")) == 0) {
1480                         warning << _("Session: XML state has no bundles section") << endmsg;
1481                         //goto out;
1482                 } else {
1483                         /* We can't load Bundles yet as they need to be able
1484                            to convert from port names to Port objects, which can't happen until
1485                            later */
1486                         _bundle_xml_node = new XMLNode (*child);
1487                 }
1488         }
1489
1490         if (version < 3000) {
1491                 if ((child = find_named_node (node, X_("DiskStreams"))) == 0) {
1492                         error << _("Session: XML state has no diskstreams section") << endmsg;
1493                         goto out;
1494                 } else if (load_diskstreams_2X (*child, version)) {
1495                         goto out;
1496                 }
1497         }
1498
1499         if ((child = find_named_node (node, VCAManager::xml_node_name)) != 0) {
1500                 _vca_manager->set_state (*child, version);
1501         }
1502
1503         if ((child = find_named_node (node, "Routes")) == 0) {
1504                 error << _("Session: XML state has no routes section") << endmsg;
1505                 goto out;
1506         } else if (load_routes (*child, version)) {
1507                 goto out;
1508         }
1509
1510         /* Now that we have Routes and masters loaded, connect them if appropriate */
1511
1512         Slavable::Assign (_vca_manager); /* EMIT SIGNAL */
1513
1514         /* our diskstreams list is no longer needed as they are now all owned by their Route */
1515         _diskstreams_2X.clear ();
1516
1517         if (version >= 3000) {
1518
1519                 if ((child = find_named_node (node, "RouteGroups")) == 0) {
1520                         error << _("Session: XML state has no route groups section") << endmsg;
1521                         goto out;
1522                 } else if (load_route_groups (*child, version)) {
1523                         goto out;
1524                 }
1525
1526         } else if (version < 3000) {
1527
1528                 if ((child = find_named_node (node, "EditGroups")) == 0) {
1529                         error << _("Session: XML state has no edit groups section") << endmsg;
1530                         goto out;
1531                 } else if (load_route_groups (*child, version)) {
1532                         goto out;
1533                 }
1534
1535                 if ((child = find_named_node (node, "MixGroups")) == 0) {
1536                         error << _("Session: XML state has no mix groups section") << endmsg;
1537                         goto out;
1538                 } else if (load_route_groups (*child, version)) {
1539                         goto out;
1540                 }
1541         }
1542
1543         if ((child = find_named_node (node, "Click")) == 0) {
1544                 warning << _("Session: XML state has no click section") << endmsg;
1545         } else if (_click_io) {
1546                 setup_click_state (&node);
1547         }
1548
1549         if ((child = find_named_node (node, ControlProtocolManager::state_node_name)) != 0) {
1550                 ControlProtocolManager::instance().set_state (*child, version);
1551         }
1552
1553         if ((child = find_named_node (node, "Script"))) {
1554                 for (XMLNodeList::const_iterator n = child->children ().begin (); n != child->children ().end (); ++n) {
1555                         if (!(*n)->is_content ()) { continue; }
1556                         gsize size;
1557                         guchar* buf = g_base64_decode ((*n)->content ().c_str (), &size);
1558                         try {
1559                                 Glib::Threads::Mutex::Lock lm (lua_lock);
1560                                 (*_lua_load)(std::string ((const char*)buf, size));
1561                         } catch (luabridge::LuaException const& e) {
1562                                 cerr << "LuaException:" << e.what () << endl;
1563                         }
1564                         g_free (buf);
1565                 }
1566         }
1567
1568         update_route_record_state ();
1569
1570         /* here beginneth the second phase ... */
1571         set_snapshot_name (_current_snapshot_name);
1572
1573         StateReady (); /* EMIT SIGNAL */
1574
1575         delete state_tree;
1576         state_tree = 0;
1577         return 0;
1578
1579   out:
1580         delete state_tree;
1581         state_tree = 0;
1582         return ret;
1583 }
1584
1585 int
1586 Session::load_routes (const XMLNode& node, int version)
1587 {
1588         XMLNodeList nlist;
1589         XMLNodeConstIterator niter;
1590         RouteList new_routes;
1591
1592         nlist = node.children();
1593
1594         set_dirty();
1595
1596         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1597
1598                 boost::shared_ptr<Route> route;
1599                 if (version < 3000) {
1600                         route = XMLRouteFactory_2X (**niter, version);
1601                 } else {
1602                         route = XMLRouteFactory (**niter, version);
1603                 }
1604
1605                 if (route == 0) {
1606                         error << _("Session: cannot create Route from XML description.") << endmsg;
1607                         return -1;
1608                 }
1609
1610                 BootMessage (string_compose (_("Loaded track/bus %1"), route->name()));
1611
1612                 new_routes.push_back (route);
1613         }
1614
1615         BootMessage (_("Tracks/busses loaded;  Adding to Session"));
1616
1617         add_routes (new_routes, false, false, false, PresentationInfo::max_order);
1618
1619         BootMessage (_("Finished adding tracks/busses"));
1620
1621         return 0;
1622 }
1623
1624 boost::shared_ptr<Route>
1625 Session::XMLRouteFactory (const XMLNode& node, int version)
1626 {
1627         boost::shared_ptr<Route> ret;
1628
1629         if (node.name() != "Route") {
1630                 return ret;
1631         }
1632
1633         XMLNode* ds_child = find_named_node (node, X_("Diskstream"));
1634
1635         DataType type = DataType::AUDIO;
1636         XMLProperty const * prop = node.property("default-type");
1637
1638         if (prop) {
1639                 type = DataType (prop->value());
1640         }
1641
1642         assert (type != DataType::NIL);
1643
1644         if (ds_child) {
1645
1646                 boost::shared_ptr<Track> track;
1647
1648                 if (type == DataType::AUDIO) {
1649                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1650                 } else {
1651                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1652                 }
1653
1654                 if (track->init()) {
1655                         return ret;
1656                 }
1657
1658                 if (track->set_state (node, version)) {
1659                         return ret;
1660                 }
1661
1662                 BOOST_MARK_TRACK (track);
1663                 ret = track;
1664
1665         } else {
1666                 PresentationInfo::Flag flags = PresentationInfo::get_flags (node);
1667                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1668
1669                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1670                         BOOST_MARK_ROUTE (r);
1671                         ret = r;
1672                 }
1673         }
1674
1675         return ret;
1676 }
1677
1678 boost::shared_ptr<Route>
1679 Session::XMLRouteFactory_2X (const XMLNode& node, int version)
1680 {
1681         boost::shared_ptr<Route> ret;
1682
1683         if (node.name() != "Route") {
1684                 return ret;
1685         }
1686
1687         XMLProperty const * ds_prop = node.property (X_("diskstream-id"));
1688         if (!ds_prop) {
1689                 ds_prop = node.property (X_("diskstream"));
1690         }
1691
1692         DataType type = DataType::AUDIO;
1693         XMLProperty const * prop = node.property("default-type");
1694
1695         if (prop) {
1696                 type = DataType (prop->value());
1697         }
1698
1699         assert (type != DataType::NIL);
1700
1701         if (ds_prop) {
1702
1703                 list<boost::shared_ptr<Diskstream> >::iterator i = _diskstreams_2X.begin ();
1704                 while (i != _diskstreams_2X.end() && (*i)->id() != ds_prop->value()) {
1705                         ++i;
1706                 }
1707
1708                 if (i == _diskstreams_2X.end()) {
1709                         error << _("Could not find diskstream for route") << endmsg;
1710                         return boost::shared_ptr<Route> ();
1711                 }
1712
1713                 boost::shared_ptr<Track> track;
1714
1715                 if (type == DataType::AUDIO) {
1716                         track.reset (new AudioTrack (*this, X_("toBeResetFroXML")));
1717                 } else {
1718                         track.reset (new MidiTrack (*this, X_("toBeResetFroXML")));
1719                 }
1720
1721                 if (track->init()) {
1722                         return ret;
1723                 }
1724
1725                 if (track->set_state (node, version)) {
1726                         return ret;
1727                 }
1728
1729                 track->set_diskstream (*i);
1730
1731                 BOOST_MARK_TRACK (track);
1732                 ret = track;
1733
1734         } else {
1735                 PresentationInfo::Flag flags = PresentationInfo::get_flags (node);
1736                 boost::shared_ptr<Route> r (new Route (*this, X_("toBeResetFroXML"), flags));
1737
1738                 if (r->init () == 0 && r->set_state (node, version) == 0) {
1739                         BOOST_MARK_ROUTE (r);
1740                         ret = r;
1741                 }
1742         }
1743
1744         return ret;
1745 }
1746
1747 int
1748 Session::load_regions (const XMLNode& node)
1749 {
1750         XMLNodeList nlist;
1751         XMLNodeConstIterator niter;
1752         boost::shared_ptr<Region> region;
1753
1754         nlist = node.children();
1755
1756         set_dirty();
1757
1758         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1759                 if ((region = XMLRegionFactory (**niter, false)) == 0) {
1760                         error << _("Session: cannot create Region from XML description.");
1761                         XMLProperty const * name = (**niter).property("name");
1762
1763                         if (name) {
1764                                 error << " " << string_compose (_("Can not load state for region '%1'"), name->value());
1765                         }
1766
1767                         error << endmsg;
1768                 }
1769         }
1770
1771         return 0;
1772 }
1773
1774 int
1775 Session::load_compounds (const XMLNode& node)
1776 {
1777         XMLNodeList calist = node.children();
1778         XMLNodeConstIterator caiter;
1779         XMLProperty const * caprop;
1780
1781         for (caiter = calist.begin(); caiter != calist.end(); ++caiter) {
1782                 XMLNode* ca = *caiter;
1783                 ID orig_id;
1784                 ID copy_id;
1785
1786                 if ((caprop = ca->property (X_("original"))) == 0) {
1787                         continue;
1788                 }
1789                 orig_id = caprop->value();
1790
1791                 if ((caprop = ca->property (X_("copy"))) == 0) {
1792                         continue;
1793                 }
1794                 copy_id = caprop->value();
1795
1796                 boost::shared_ptr<Region> orig = RegionFactory::region_by_id (orig_id);
1797                 boost::shared_ptr<Region> copy = RegionFactory::region_by_id (copy_id);
1798
1799                 if (!orig || !copy) {
1800                         warning << string_compose (_("Regions in compound description not found (ID's %1 and %2): ignored"),
1801                                                    orig_id, copy_id)
1802                                 << endmsg;
1803                         continue;
1804                 }
1805
1806                 RegionFactory::add_compound_association (orig, copy);
1807         }
1808
1809         return 0;
1810 }
1811
1812 void
1813 Session::load_nested_sources (const XMLNode& node)
1814 {
1815         XMLNodeList nlist;
1816         XMLNodeConstIterator niter;
1817
1818         nlist = node.children();
1819
1820         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1821                 if ((*niter)->name() == "Source") {
1822
1823                         /* it may already exist, so don't recreate it unnecessarily
1824                          */
1825
1826                         XMLProperty const * prop = (*niter)->property (X_("id"));
1827                         if (!prop) {
1828                                 error << _("Nested source has no ID info in session file! (ignored)") << endmsg;
1829                                 continue;
1830                         }
1831
1832                         ID source_id (prop->value());
1833
1834                         if (!source_by_id (source_id)) {
1835
1836                                 try {
1837                                         SourceFactory::create (*this, **niter, true);
1838                                 }
1839                                 catch (failed_constructor& err) {
1840                                         error << string_compose (_("Cannot reconstruct nested source for region %1"), name()) << endmsg;
1841                                 }
1842                         }
1843                 }
1844         }
1845 }
1846
1847 boost::shared_ptr<Region>
1848 Session::XMLRegionFactory (const XMLNode& node, bool full)
1849 {
1850         XMLProperty const * type = node.property("type");
1851
1852         try {
1853
1854                 const XMLNodeList& nlist = node.children();
1855
1856                 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
1857                         XMLNode *child = (*niter);
1858                         if (child->name() == "NestedSource") {
1859                                 load_nested_sources (*child);
1860                         }
1861                 }
1862
1863                 if (!type || type->value() == "audio") {
1864                         return boost::shared_ptr<Region>(XMLAudioRegionFactory (node, full));
1865                 } else if (type->value() == "midi") {
1866                         return boost::shared_ptr<Region>(XMLMidiRegionFactory (node, full));
1867                 }
1868
1869         } catch (failed_constructor& err) {
1870                 return boost::shared_ptr<Region> ();
1871         }
1872
1873         return boost::shared_ptr<Region> ();
1874 }
1875
1876 boost::shared_ptr<AudioRegion>
1877 Session::XMLAudioRegionFactory (const XMLNode& node, bool /*full*/)
1878 {
1879         XMLProperty const * prop;
1880         boost::shared_ptr<Source> source;
1881         boost::shared_ptr<AudioSource> as;
1882         SourceList sources;
1883         SourceList master_sources;
1884         uint32_t nchans = 1;
1885         char buf[128];
1886
1887         if (node.name() != X_("Region")) {
1888                 return boost::shared_ptr<AudioRegion>();
1889         }
1890
1891         if ((prop = node.property (X_("channels"))) != 0) {
1892                 nchans = atoi (prop->value().c_str());
1893         }
1894
1895         if ((prop = node.property ("name")) == 0) {
1896                 cerr << "no name for this region\n";
1897                 abort ();
1898         }
1899
1900         if ((prop = node.property (X_("source-0"))) == 0) {
1901                 if ((prop = node.property ("source")) == 0) {
1902                         error << _("Session: XMLNode describing a AudioRegion is incomplete (no source)") << endmsg;
1903                         return boost::shared_ptr<AudioRegion>();
1904                 }
1905         }
1906
1907         PBD::ID s_id (prop->value());
1908
1909         if ((source = source_by_id (s_id)) == 0) {
1910                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), s_id) << endmsg;
1911                 return boost::shared_ptr<AudioRegion>();
1912         }
1913
1914         as = boost::dynamic_pointer_cast<AudioSource>(source);
1915         if (!as) {
1916                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), s_id) << endmsg;
1917                 return boost::shared_ptr<AudioRegion>();
1918         }
1919
1920         sources.push_back (as);
1921
1922         /* pickup other channels */
1923
1924         for (uint32_t n=1; n < nchans; ++n) {
1925                 snprintf (buf, sizeof(buf), X_("source-%d"), n);
1926                 if ((prop = node.property (buf)) != 0) {
1927
1928                         PBD::ID id2 (prop->value());
1929
1930                         if ((source = source_by_id (id2)) == 0) {
1931                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1932                                 return boost::shared_ptr<AudioRegion>();
1933                         }
1934
1935                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1936                         if (!as) {
1937                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1938                                 return boost::shared_ptr<AudioRegion>();
1939                         }
1940                         sources.push_back (as);
1941                 }
1942         }
1943
1944         for (uint32_t n = 0; n < nchans; ++n) {
1945                 snprintf (buf, sizeof(buf), X_("master-source-%d"), n);
1946                 if ((prop = node.property (buf)) != 0) {
1947
1948                         PBD::ID id2 (prop->value());
1949
1950                         if ((source = source_by_id (id2)) == 0) {
1951                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references an unknown source id =%1"), id2) << endmsg;
1952                                 return boost::shared_ptr<AudioRegion>();
1953                         }
1954
1955                         as = boost::dynamic_pointer_cast<AudioSource>(source);
1956                         if (!as) {
1957                                 error << string_compose(_("Session: XMLNode describing a AudioRegion references a non-audio source id =%1"), id2) << endmsg;
1958                                 return boost::shared_ptr<AudioRegion>();
1959                         }
1960                         master_sources.push_back (as);
1961                 }
1962         }
1963
1964         try {
1965                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (sources, node)));
1966
1967                 /* a final detail: this is the one and only place that we know how long missing files are */
1968
1969                 if (region->whole_file()) {
1970                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
1971                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
1972                                 if (sfp) {
1973                                         sfp->set_length (region->length());
1974                                 }
1975                         }
1976                 }
1977
1978                 if (!master_sources.empty()) {
1979                         if (master_sources.size() != nchans) {
1980                                 error << _("Session: XMLNode describing an AudioRegion is missing some master sources; ignored") << endmsg;
1981                         } else {
1982                                 region->set_master_sources (master_sources);
1983                         }
1984                 }
1985
1986                 return region;
1987
1988         }
1989
1990         catch (failed_constructor& err) {
1991                 return boost::shared_ptr<AudioRegion>();
1992         }
1993 }
1994
1995 boost::shared_ptr<MidiRegion>
1996 Session::XMLMidiRegionFactory (const XMLNode& node, bool /*full*/)
1997 {
1998         XMLProperty const * prop;
1999         boost::shared_ptr<Source> source;
2000         boost::shared_ptr<MidiSource> ms;
2001         SourceList sources;
2002
2003         if (node.name() != X_("Region")) {
2004                 return boost::shared_ptr<MidiRegion>();
2005         }
2006
2007         if ((prop = node.property ("name")) == 0) {
2008                 cerr << "no name for this region\n";
2009                 abort ();
2010         }
2011
2012         if ((prop = node.property (X_("source-0"))) == 0) {
2013                 if ((prop = node.property ("source")) == 0) {
2014                         error << _("Session: XMLNode describing a MidiRegion is incomplete (no source)") << endmsg;
2015                         return boost::shared_ptr<MidiRegion>();
2016                 }
2017         }
2018
2019         PBD::ID s_id (prop->value());
2020
2021         if ((source = source_by_id (s_id)) == 0) {
2022                 error << string_compose(_("Session: XMLNode describing a MidiRegion references an unknown source id =%1"), s_id) << endmsg;
2023                 return boost::shared_ptr<MidiRegion>();
2024         }
2025
2026         ms = boost::dynamic_pointer_cast<MidiSource>(source);
2027         if (!ms) {
2028                 error << string_compose(_("Session: XMLNode describing a MidiRegion references a non-midi source id =%1"), s_id) << endmsg;
2029                 return boost::shared_ptr<MidiRegion>();
2030         }
2031
2032         sources.push_back (ms);
2033
2034         try {
2035                 boost::shared_ptr<MidiRegion> region (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (sources, node)));
2036                 /* a final detail: this is the one and only place that we know how long missing files are */
2037
2038                 if (region->whole_file()) {
2039                         for (SourceList::iterator sx = sources.begin(); sx != sources.end(); ++sx) {
2040                                 boost::shared_ptr<SilentFileSource> sfp = boost::dynamic_pointer_cast<SilentFileSource> (*sx);
2041                                 if (sfp) {
2042                                         sfp->set_length (region->length());
2043                                 }
2044                         }
2045                 }
2046
2047                 return region;
2048         }
2049
2050         catch (failed_constructor& err) {
2051                 return boost::shared_ptr<MidiRegion>();
2052         }
2053 }
2054
2055 XMLNode&
2056 Session::get_sources_as_xml ()
2057
2058 {
2059         XMLNode* node = new XMLNode (X_("Sources"));
2060         Glib::Threads::Mutex::Lock lm (source_lock);
2061
2062         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
2063                 node->add_child_nocopy (i->second->get_state());
2064         }
2065
2066         return *node;
2067 }
2068
2069 void
2070 Session::reset_write_sources (bool mark_write_complete, bool force)
2071 {
2072         boost::shared_ptr<RouteList> rl = routes.reader();
2073         for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
2074                 boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
2075                 if (tr) {
2076                         _state_of_the_state = StateOfTheState (_state_of_the_state|InCleanup);
2077                         tr->reset_write_sources(mark_write_complete, force);
2078                         _state_of_the_state = StateOfTheState (_state_of_the_state & ~InCleanup);
2079                 }
2080         }
2081 }
2082
2083 int
2084 Session::load_sources (const XMLNode& node)
2085 {
2086         XMLNodeList nlist;
2087         XMLNodeConstIterator niter;
2088         boost::shared_ptr<Source> source; /* don't need this but it stops some
2089                                            * versions of gcc complaining about
2090                                            * discarded return values.
2091                                            */
2092
2093         nlist = node.children();
2094
2095         set_dirty();
2096
2097         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2098           retry:
2099                 try {
2100                         if ((source = XMLSourceFactory (**niter)) == 0) {
2101                                 error << _("Session: cannot create Source from XML description.") << endmsg;
2102                         }
2103
2104                 } catch (MissingSource& err) {
2105
2106                         int user_choice;
2107
2108                         if (err.type == DataType::MIDI && Glib::path_is_absolute (err.path)) {
2109                                 error << string_compose (_("A external MIDI file is missing. %1 cannot currently recover from missing external MIDI files"),
2110                                                          PROGRAM_NAME) << endmsg;
2111                                 return -1;
2112                         }
2113
2114                         if (!no_questions_about_missing_files) {
2115                                 user_choice = MissingFile (this, err.path, err.type).get_value_or (-1);
2116                         } else {
2117                                 user_choice = -2;
2118                         }
2119
2120                         switch (user_choice) {
2121                         case 0:
2122                                 /* user added a new search location, so try again */
2123                                 goto retry;
2124
2125
2126                         case 1:
2127                                 /* user asked to quit the entire session load
2128                                  */
2129                                 return -1;
2130
2131                         case 2:
2132                                 no_questions_about_missing_files = true;
2133                                 goto retry;
2134
2135                         case 3:
2136                                 no_questions_about_missing_files = true;
2137                                 /* fallthru */
2138
2139                         case -1:
2140                         default:
2141                                 switch (err.type) {
2142
2143                                 case DataType::AUDIO:
2144                                         source = SourceFactory::createSilent (*this, **niter, max_framecnt, _current_frame_rate);
2145                                         break;
2146
2147                                 case DataType::MIDI:
2148                                         /* The MIDI file is actually missing so
2149                                          * just create a new one in the same
2150                                          * location. Do not announce its
2151                                          */
2152                                         string fullpath;
2153
2154                                         if (!Glib::path_is_absolute (err.path)) {
2155                                                 fullpath = Glib::build_filename (source_search_path (DataType::MIDI).front(), err.path);
2156                                         } else {
2157                                                 /* this should be an unrecoverable error: we would be creating a MIDI file outside
2158                                                    the session tree.
2159                                                 */
2160                                                 return -1;
2161                                         }
2162                                         /* Note that we do not announce the source just yet - we need to reset its ID before we do that */
2163                                         source = SourceFactory::createWritable (DataType::MIDI, *this, fullpath, false, _current_frame_rate, false, false);
2164                                         /* reset ID to match the missing one */
2165                                         source->set_id (**niter);
2166                                         /* Now we can announce it */
2167                                         SourceFactory::SourceCreated (source);
2168                                         break;
2169                                 }
2170                                 break;
2171                         }
2172                 }
2173         }
2174
2175         return 0;
2176 }
2177
2178 boost::shared_ptr<Source>
2179 Session::XMLSourceFactory (const XMLNode& node)
2180 {
2181         if (node.name() != "Source") {
2182                 return boost::shared_ptr<Source>();
2183         }
2184
2185         try {
2186                 /* note: do peak building in another thread when loading session state */
2187                 return SourceFactory::create (*this, node, true);
2188         }
2189
2190         catch (failed_constructor& err) {
2191                 error << string_compose (_("Found a sound file that cannot be used by %1. Talk to the programmers."), PROGRAM_NAME) << endmsg;
2192                 return boost::shared_ptr<Source>();
2193         }
2194 }
2195
2196 int
2197 Session::save_template (string template_name, bool replace_existing)
2198 {
2199         if ((_state_of_the_state & CannotSave) || template_name.empty ()) {
2200                 return -1;
2201         }
2202
2203         bool absolute_path = Glib::path_is_absolute (template_name);
2204
2205         /* directory to put the template in */
2206         std::string template_dir_path;
2207
2208         if (!absolute_path) {
2209                 std::string user_template_dir(user_template_directory());
2210
2211                 if (g_mkdir_with_parents (user_template_dir.c_str(), 0755) != 0) {
2212                         error << string_compose(_("Could not create templates directory \"%1\" (%2)"),
2213                                         user_template_dir, g_strerror (errno)) << endmsg;
2214                         return -1;
2215                 }
2216
2217                 template_dir_path = Glib::build_filename (user_template_dir, template_name);
2218         } else {
2219                 template_dir_path = template_name;
2220         }
2221
2222         if (!ARDOUR::Profile->get_trx()) {
2223                 if (!replace_existing && Glib::file_test (template_dir_path, Glib::FILE_TEST_EXISTS)) {
2224                         warning << string_compose(_("Template \"%1\" already exists - new version not created"),
2225                                                                           template_dir_path) << endmsg;
2226                         return -2;
2227                 }
2228
2229                 if (g_mkdir_with_parents (template_dir_path.c_str(), 0755) != 0) {
2230                         error << string_compose(_("Could not create directory for Session template\"%1\" (%2)"),
2231                                                                         template_dir_path, g_strerror (errno)) << endmsg;
2232                         return -1;
2233                 }
2234         }
2235
2236         /* file to write */
2237         std::string template_file_path;
2238
2239         if (ARDOUR::Profile->get_trx()) {
2240                 template_file_path = template_name;
2241         } else {
2242                 if (absolute_path) {
2243                         template_file_path = Glib::build_filename (template_dir_path, Glib::path_get_basename (template_dir_path) + template_suffix);
2244                 } else {
2245                         template_file_path = Glib::build_filename (template_dir_path, template_name + template_suffix);
2246                 }
2247         }
2248
2249         SessionSaveUnderway (); /* EMIT SIGNAL */
2250
2251         XMLTree tree;
2252
2253         {
2254                 PBD::Unwinder<std::string> uw (_template_state_dir, template_dir_path);
2255                 tree.set_root (&get_template());
2256         }
2257
2258         if (!tree.write (template_file_path)) {
2259                 error << _("template not saved") << endmsg;
2260                 return -1;
2261         }
2262
2263         store_recent_templates (template_file_path);
2264
2265         return 0;
2266 }
2267
2268 void
2269 Session::refresh_disk_space ()
2270 {
2271 #if __APPLE__ || __FreeBSD__ || (HAVE_SYS_VFS_H && HAVE_SYS_STATVFS_H)
2272
2273         Glib::Threads::Mutex::Lock lm (space_lock);
2274
2275         /* get freespace on every FS that is part of the session path */
2276
2277         _total_free_4k_blocks = 0;
2278         _total_free_4k_blocks_uncertain = false;
2279
2280         for (vector<space_and_path>::iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2281
2282                 struct statfs statfsbuf;
2283                 statfs (i->path.c_str(), &statfsbuf);
2284
2285                 double const scale = statfsbuf.f_bsize / 4096.0;
2286
2287                 /* See if this filesystem is read-only */
2288                 struct statvfs statvfsbuf;
2289                 statvfs (i->path.c_str(), &statvfsbuf);
2290
2291                 /* f_bavail can be 0 if it is undefined for whatever
2292                    filesystem we are looking at; Samba shares mounted
2293                    via GVFS are an example of this.
2294                 */
2295                 if (statfsbuf.f_bavail == 0) {
2296                         /* block count unknown */
2297                         i->blocks = 0;
2298                         i->blocks_unknown = true;
2299                 } else if (statvfsbuf.f_flag & ST_RDONLY) {
2300                         /* read-only filesystem */
2301                         i->blocks = 0;
2302                         i->blocks_unknown = false;
2303                 } else {
2304                         /* read/write filesystem with known space */
2305                         i->blocks = (uint32_t) floor (statfsbuf.f_bavail * scale);
2306                         i->blocks_unknown = false;
2307                 }
2308
2309                 _total_free_4k_blocks += i->blocks;
2310                 if (i->blocks_unknown) {
2311                         _total_free_4k_blocks_uncertain = true;
2312                 }
2313         }
2314 #elif defined PLATFORM_WINDOWS
2315         vector<string> scanned_volumes;
2316         vector<string>::iterator j;
2317         vector<space_and_path>::iterator i;
2318     DWORD nSectorsPerCluster, nBytesPerSector,
2319           nFreeClusters, nTotalClusters;
2320     char disk_drive[4];
2321         bool volume_found;
2322
2323         _total_free_4k_blocks = 0;
2324
2325         for (i = session_dirs.begin(); i != session_dirs.end(); i++) {
2326                 strncpy (disk_drive, (*i).path.c_str(), 3);
2327                 disk_drive[3] = 0;
2328                 strupr(disk_drive);
2329
2330                 volume_found = false;
2331                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2332                 {
2333                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2334                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2335                         i->blocks = (uint32_t)(nFreeBytes / 4096);
2336
2337                         for (j = scanned_volumes.begin(); j != scanned_volumes.end(); j++) {
2338                                 if (0 == j->compare(disk_drive)) {
2339                                         volume_found = true;
2340                                         break;
2341                                 }
2342                         }
2343
2344                         if (!volume_found) {
2345                                 scanned_volumes.push_back(disk_drive);
2346                                 _total_free_4k_blocks += i->blocks;
2347                         }
2348                 }
2349         }
2350
2351         if (0 == _total_free_4k_blocks) {
2352                 strncpy (disk_drive, path().c_str(), 3);
2353                 disk_drive[3] = 0;
2354
2355                 if (0 != (GetDiskFreeSpace(disk_drive, &nSectorsPerCluster, &nBytesPerSector, &nFreeClusters, &nTotalClusters)))
2356                 {
2357                         int64_t nBytesPerCluster = nBytesPerSector * nSectorsPerCluster;
2358                         int64_t nFreeBytes = nBytesPerCluster * (int64_t)nFreeClusters;
2359                         _total_free_4k_blocks = (uint32_t)(nFreeBytes / 4096);
2360                 }
2361         }
2362 #endif
2363 }
2364
2365 string
2366 Session::get_best_session_directory_for_new_audio ()
2367 {
2368         vector<space_and_path>::iterator i;
2369         string result = _session_dir->root_path();
2370
2371         /* handle common case without system calls */
2372
2373         if (session_dirs.size() == 1) {
2374                 return result;
2375         }
2376
2377         /* OK, here's the algorithm we're following here:
2378
2379         We want to select which directory to use for
2380         the next file source to be created. Ideally,
2381         we'd like to use a round-robin process so as to
2382         get maximum performance benefits from splitting
2383         the files across multiple disks.
2384
2385         However, in situations without much diskspace, an
2386         RR approach may end up filling up a filesystem
2387         with new files while others still have space.
2388         Its therefore important to pay some attention to
2389         the freespace in the filesystem holding each
2390         directory as well. However, if we did that by
2391         itself, we'd keep creating new files in the file
2392         system with the most space until it was as full
2393         as all others, thus negating any performance
2394         benefits of this RAID-1 like approach.
2395
2396         So, we use a user-configurable space threshold. If
2397         there are at least 2 filesystems with more than this
2398         much space available, we use RR selection between them.
2399         If not, then we pick the filesystem with the most space.
2400
2401         This gets a good balance between the two
2402         approaches.
2403         */
2404
2405         refresh_disk_space ();
2406
2407         int free_enough = 0;
2408
2409         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
2410                 if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2411                         free_enough++;
2412                 }
2413         }
2414
2415         if (free_enough >= 2) {
2416                 /* use RR selection process, ensuring that the one
2417                    picked works OK.
2418                 */
2419
2420                 i = last_rr_session_dir;
2421
2422                 do {
2423                         if (++i == session_dirs.end()) {
2424                                 i = session_dirs.begin();
2425                         }
2426
2427                         if ((*i).blocks * 4096 >= Config->get_disk_choice_space_threshold()) {
2428                                 SessionDirectory sdir(i->path);
2429                                 if (sdir.create ()) {
2430                                         result = (*i).path;
2431                                         last_rr_session_dir = i;
2432                                         return result;
2433                                 }
2434                         }
2435
2436                 } while (i != last_rr_session_dir);
2437
2438         } else {
2439
2440                 /* pick FS with the most freespace (and that
2441                    seems to actually work ...)
2442                 */
2443
2444                 vector<space_and_path> sorted;
2445                 space_and_path_ascending_cmp cmp;
2446
2447                 sorted = session_dirs;
2448                 sort (sorted.begin(), sorted.end(), cmp);
2449
2450                 for (i = sorted.begin(); i != sorted.end(); ++i) {
2451                         SessionDirectory sdir(i->path);
2452                         if (sdir.create ()) {
2453                                 result = (*i).path;
2454                                 last_rr_session_dir = i;
2455                                 return result;
2456                         }
2457                 }
2458         }
2459
2460         return result;
2461 }
2462
2463 string
2464 Session::automation_dir () const
2465 {
2466         return Glib::build_filename (_path, automation_dir_name);
2467 }
2468
2469 string
2470 Session::analysis_dir () const
2471 {
2472         return Glib::build_filename (_path, analysis_dir_name);
2473 }
2474
2475 string
2476 Session::plugins_dir () const
2477 {
2478         return Glib::build_filename (_path, plugins_dir_name);
2479 }
2480
2481 string
2482 Session::externals_dir () const
2483 {
2484         return Glib::build_filename (_path, externals_dir_name);
2485 }
2486
2487 int
2488 Session::load_bundles (XMLNode const & node)
2489 {
2490         XMLNodeList nlist = node.children();
2491         XMLNodeConstIterator niter;
2492
2493         set_dirty();
2494
2495         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2496                 if ((*niter)->name() == "InputBundle") {
2497                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, true)));
2498                 } else if ((*niter)->name() == "OutputBundle") {
2499                         add_bundle (boost::shared_ptr<UserBundle> (new UserBundle (**niter, false)));
2500                 } else {
2501                         error << string_compose(_("Unknown node \"%1\" found in Bundles list from session file"), (*niter)->name()) << endmsg;
2502                         return -1;
2503                 }
2504         }
2505
2506         return 0;
2507 }
2508
2509 int
2510 Session::load_route_groups (const XMLNode& node, int version)
2511 {
2512         XMLNodeList nlist = node.children();
2513         XMLNodeConstIterator niter;
2514
2515         set_dirty ();
2516
2517         if (version >= 3000) {
2518
2519                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2520                         if ((*niter)->name() == "RouteGroup") {
2521                                 RouteGroup* rg = new RouteGroup (*this, "");
2522                                 add_route_group (rg);
2523                                 rg->set_state (**niter, version);
2524                         }
2525                 }
2526
2527         } else if (version < 3000) {
2528
2529                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2530                         if ((*niter)->name() == "EditGroup" || (*niter)->name() == "MixGroup") {
2531                                 RouteGroup* rg = new RouteGroup (*this, "");
2532                                 add_route_group (rg);
2533                                 rg->set_state (**niter, version);
2534                         }
2535                 }
2536         }
2537
2538         return 0;
2539 }
2540
2541 static bool
2542 state_file_filter (const string &str, void* /*arg*/)
2543 {
2544         return (str.length() > strlen(statefile_suffix) &&
2545                 str.find (statefile_suffix) == (str.length() - strlen (statefile_suffix)));
2546 }
2547
2548 static string
2549 remove_end(string state)
2550 {
2551         string statename(state);
2552
2553         string::size_type start,end;
2554         if ((start = statename.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
2555                 statename = statename.substr (start+1);
2556         }
2557
2558         if ((end = statename.rfind(statefile_suffix)) == string::npos) {
2559                 end = statename.length();
2560         }
2561
2562         return string(statename.substr (0, end));
2563 }
2564
2565 vector<string>
2566 Session::possible_states (string path)
2567 {
2568         vector<string> states;
2569         find_files_matching_filter (states, path, state_file_filter, 0, false, false);
2570
2571         transform(states.begin(), states.end(), states.begin(), remove_end);
2572
2573         sort (states.begin(), states.end());
2574
2575         return states;
2576 }
2577
2578 vector<string>
2579 Session::possible_states () const
2580 {
2581         return possible_states(_path);
2582 }
2583
2584 void
2585 Session::add_route_group (RouteGroup* g)
2586 {
2587         _route_groups.push_back (g);
2588         route_group_added (g); /* EMIT SIGNAL */
2589
2590         g->RouteAdded.connect_same_thread (*this, boost::bind (&Session::route_added_to_route_group, this, _1, _2));
2591         g->RouteRemoved.connect_same_thread (*this, boost::bind (&Session::route_removed_from_route_group, this, _1, _2));
2592         g->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::route_group_property_changed, this, g));
2593
2594         set_dirty ();
2595 }
2596
2597 void
2598 Session::remove_route_group (RouteGroup& rg)
2599 {
2600         list<RouteGroup*>::iterator i;
2601
2602         if ((i = find (_route_groups.begin(), _route_groups.end(), &rg)) != _route_groups.end()) {
2603                 _route_groups.erase (i);
2604                 delete &rg;
2605
2606                 route_group_removed (); /* EMIT SIGNAL */
2607         }
2608 }
2609
2610 /** Set a new order for our route groups, without adding or removing any.
2611  *  @param groups Route group list in the new order.
2612  */
2613 void
2614 Session::reorder_route_groups (list<RouteGroup*> groups)
2615 {
2616         _route_groups = groups;
2617
2618         route_groups_reordered (); /* EMIT SIGNAL */
2619         set_dirty ();
2620 }
2621
2622
2623 RouteGroup *
2624 Session::route_group_by_name (string name)
2625 {
2626         list<RouteGroup *>::iterator i;
2627
2628         for (i = _route_groups.begin(); i != _route_groups.end(); ++i) {
2629                 if ((*i)->name() == name) {
2630                         return* i;
2631                 }
2632         }
2633         return 0;
2634 }
2635
2636 RouteGroup&
2637 Session::all_route_group() const
2638 {
2639         return *_all_route_group;
2640 }
2641
2642 void
2643 Session::add_commands (vector<Command*> const & cmds)
2644 {
2645         for (vector<Command*>::const_iterator i = cmds.begin(); i != cmds.end(); ++i) {
2646                 add_command (*i);
2647         }
2648 }
2649
2650 void
2651 Session::add_command (Command* const cmd)
2652 {
2653         assert (_current_trans);
2654         DEBUG_UNDO_HISTORY (
2655             string_compose ("Current Undo Transaction %1, adding command: %2",
2656                             _current_trans->name (),
2657                             cmd->name ()));
2658         _current_trans->add_command (cmd);
2659 }
2660
2661 PBD::StatefulDiffCommand*
2662 Session::add_stateful_diff_command (boost::shared_ptr<PBD::StatefulDestructible> sfd)
2663 {
2664         PBD::StatefulDiffCommand* cmd = new PBD::StatefulDiffCommand (sfd);
2665         add_command (cmd);
2666         return cmd;
2667 }
2668
2669 void
2670 Session::begin_reversible_command (const string& name)
2671 {
2672         begin_reversible_command (g_quark_from_string (name.c_str ()));
2673 }
2674
2675 /** Begin a reversible command using a GQuark to identify it.
2676  *  begin_reversible_command() and commit_reversible_command() calls may be nested,
2677  *  but there must be as many begin...()s as there are commit...()s.
2678  */
2679 void
2680 Session::begin_reversible_command (GQuark q)
2681 {
2682         /* If nested begin/commit pairs are used, we create just one UndoTransaction
2683            to hold all the commands that are committed.  This keeps the order of
2684            commands correct in the history.
2685         */
2686
2687         if (_current_trans == 0) {
2688                 DEBUG_UNDO_HISTORY (string_compose (
2689                     "Begin Reversible Command, new transaction: %1", g_quark_to_string (q)));
2690
2691                 /* start a new transaction */
2692                 assert (_current_trans_quarks.empty ());
2693                 _current_trans = new UndoTransaction();
2694                 _current_trans->set_name (g_quark_to_string (q));
2695         } else {
2696                 DEBUG_UNDO_HISTORY (
2697                     string_compose ("Begin Reversible Command, current transaction: %1",
2698                                     _current_trans->name ()));
2699         }
2700
2701         _current_trans_quarks.push_front (q);
2702 }
2703
2704 void
2705 Session::abort_reversible_command ()
2706 {
2707         if (_current_trans != 0) {
2708                 DEBUG_UNDO_HISTORY (
2709                     string_compose ("Abort Reversible Command: %1", _current_trans->name ()));
2710                 _current_trans->clear();
2711                 delete _current_trans;
2712                 _current_trans = 0;
2713                 _current_trans_quarks.clear();
2714         }
2715 }
2716
2717 void
2718 Session::commit_reversible_command (Command *cmd)
2719 {
2720         assert (_current_trans);
2721         assert (!_current_trans_quarks.empty ());
2722
2723         struct timeval now;
2724
2725         if (cmd) {
2726                 DEBUG_UNDO_HISTORY (
2727                     string_compose ("Current Undo Transaction %1, adding command: %2",
2728                                     _current_trans->name (),
2729                                     cmd->name ()));
2730                 _current_trans->add_command (cmd);
2731         }
2732
2733         DEBUG_UNDO_HISTORY (
2734             string_compose ("Commit Reversible Command, current transaction: %1",
2735                             _current_trans->name ()));
2736
2737         _current_trans_quarks.pop_front ();
2738
2739         if (!_current_trans_quarks.empty ()) {
2740                 DEBUG_UNDO_HISTORY (
2741                     string_compose ("Commit Reversible Command, transaction is not "
2742                                     "top-level, current transaction: %1",
2743                                     _current_trans->name ()));
2744                 /* the transaction we're committing is not the top-level one */
2745                 return;
2746         }
2747
2748         if (_current_trans->empty()) {
2749                 /* no commands were added to the transaction, so just get rid of it */
2750                 DEBUG_UNDO_HISTORY (
2751                     string_compose ("Commit Reversible Command, No commands were "
2752                                     "added to current transaction: %1",
2753                                     _current_trans->name ()));
2754                 delete _current_trans;
2755                 _current_trans = 0;
2756                 return;
2757         }
2758
2759         gettimeofday (&now, 0);
2760         _current_trans->set_timestamp (now);
2761
2762         _history.add (_current_trans);
2763         _current_trans = 0;
2764 }
2765
2766 static bool
2767 accept_all_audio_files (const string& path, void* /*arg*/)
2768 {
2769         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2770                 return false;
2771         }
2772
2773         if (!AudioFileSource::safe_audio_file_extension (path)) {
2774                 return false;
2775         }
2776
2777         return true;
2778 }
2779
2780 static bool
2781 accept_all_midi_files (const string& path, void* /*arg*/)
2782 {
2783         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2784                 return false;
2785         }
2786
2787         return ((path.length() > 4 && path.find (".mid") != (path.length() - 4)) ||
2788                 (path.length() > 4 && path.find (".smf") != (path.length() - 4)) ||
2789                 (path.length() > 5 && path.find (".midi") != (path.length() - 5)));
2790 }
2791
2792 static bool
2793 accept_all_state_files (const string& path, void* /*arg*/)
2794 {
2795         if (!Glib::file_test (path, Glib::FILE_TEST_IS_REGULAR)) {
2796                 return false;
2797         }
2798
2799         std::string const statefile_ext (statefile_suffix);
2800         if (path.length() >= statefile_ext.length()) {
2801                 return (0 == path.compare (path.length() - statefile_ext.length(), statefile_ext.length(), statefile_ext));
2802         } else {
2803                 return false;
2804         }
2805 }
2806
2807 int
2808 Session::find_all_sources (string path, set<string>& result)
2809 {
2810         XMLTree tree;
2811         XMLNode* node;
2812
2813         if (!tree.read (path)) {
2814                 return -1;
2815         }
2816
2817         if ((node = find_named_node (*tree.root(), "Sources")) == 0) {
2818                 return -2;
2819         }
2820
2821         XMLNodeList nlist;
2822         XMLNodeConstIterator niter;
2823
2824         nlist = node->children();
2825
2826         set_dirty();
2827
2828         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2829
2830                 XMLProperty const * prop;
2831
2832                 if ((prop = (*niter)->property (X_("type"))) == 0) {
2833                         continue;
2834                 }
2835
2836                 DataType type (prop->value());
2837
2838                 if ((prop = (*niter)->property (X_("name"))) == 0) {
2839                         continue;
2840                 }
2841
2842                 if (Glib::path_is_absolute (prop->value())) {
2843                         /* external file, ignore */
2844                         continue;
2845                 }
2846
2847                 string found_path;
2848                 bool is_new;
2849                 uint16_t chan;
2850
2851                 if (FileSource::find (*this, type, prop->value(), true, is_new, chan, found_path)) {
2852                         result.insert (found_path);
2853                 }
2854         }
2855
2856         return 0;
2857 }
2858
2859 int
2860 Session::find_all_sources_across_snapshots (set<string>& result, bool exclude_this_snapshot)
2861 {
2862         vector<string> state_files;
2863         string ripped;
2864         string this_snapshot_path;
2865
2866         result.clear ();
2867
2868         ripped = _path;
2869
2870         if (ripped[ripped.length()-1] == G_DIR_SEPARATOR) {
2871                 ripped = ripped.substr (0, ripped.length() - 1);
2872         }
2873
2874         find_files_matching_filter (state_files, ripped, accept_all_state_files, (void *) 0, true, true);
2875
2876         if (state_files.empty()) {
2877                 /* impossible! */
2878                 return 0;
2879         }
2880
2881         this_snapshot_path = Glib::build_filename (_path, legalize_for_path (_current_snapshot_name));
2882         this_snapshot_path += statefile_suffix;
2883
2884         for (vector<string>::iterator i = state_files.begin(); i != state_files.end(); ++i) {
2885
2886                 cerr << "Looking at snapshot " << (*i) << " ( with this = [" << this_snapshot_path << "])\n";
2887
2888                 if (exclude_this_snapshot && *i == this_snapshot_path) {
2889                         cerr << "\texcluded\n";
2890                         continue;
2891
2892                 }
2893
2894                 if (find_all_sources (*i, result) < 0) {
2895                         return -1;
2896                 }
2897         }
2898
2899         return 0;
2900 }
2901
2902 struct RegionCounter {
2903     typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
2904     AudioSourceList::iterator iter;
2905     boost::shared_ptr<Region> region;
2906     uint32_t count;
2907
2908     RegionCounter() : count (0) {}
2909 };
2910
2911 int
2912 Session::ask_about_playlist_deletion (boost::shared_ptr<Playlist> p)
2913 {
2914         boost::optional<int> r = AskAboutPlaylistDeletion (p);
2915         return r.get_value_or (1);
2916 }
2917
2918 void
2919 Session::cleanup_regions ()
2920 {
2921         bool removed = false;
2922         const RegionFactory::RegionMap& regions (RegionFactory::regions());
2923
2924         for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end();) {
2925
2926                 uint32_t used = playlists->region_use_count (i->second);
2927
2928                 if (used == 0 && !i->second->automatic ()) {
2929                         boost::weak_ptr<Region> w = i->second;
2930                         ++i;
2931                         removed = true;
2932                         RegionFactory::map_remove (w);
2933                 } else {
2934                         ++i;
2935                 }
2936         }
2937
2938         if (removed) {
2939                 // re-check to remove parent references of compound regions
2940                 for (RegionFactory::RegionMap::const_iterator i = regions.begin(); i != regions.end();) {
2941                         if (!(i->second->whole_file() && i->second->max_source_level() > 0)) {
2942                                 ++i;
2943                                 continue;
2944                         }
2945                         assert(boost::dynamic_pointer_cast<PlaylistSource>(i->second->source (0)) != 0);
2946                         if (0 == playlists->region_use_count (i->second)) {
2947                                 boost::weak_ptr<Region> w = i->second;
2948                                 ++i;
2949                                 RegionFactory::map_remove (w);
2950                         } else {
2951                                 ++i;
2952                         }
2953                 }
2954         }
2955
2956         /* dump the history list */
2957         _history.clear ();
2958
2959         save_state ("");
2960 }
2961
2962 bool
2963 Session::can_cleanup_peakfiles () const
2964 {
2965         if (deletion_in_progress()) {
2966                 return false;
2967         }
2968         if (!_writable || (_state_of_the_state & CannotSave)) {
2969                 warning << _("Cannot cleanup peak-files for read-only session.") << endmsg;
2970                 return false;
2971         }
2972         if (record_status() == Recording) {
2973                 error << _("Cannot cleanup peak-files while recording") << endmsg;
2974                 return false;
2975         }
2976         return true;
2977 }
2978
2979 int
2980 Session::cleanup_peakfiles ()
2981 {
2982         Glib::Threads::Mutex::Lock lm (peak_cleanup_lock, Glib::Threads::TRY_LOCK);
2983         if (!lm.locked()) {
2984                 return -1;
2985         }
2986
2987         assert (can_cleanup_peakfiles ());
2988         assert (!peaks_cleanup_in_progres());
2989
2990         _state_of_the_state = StateOfTheState (_state_of_the_state | PeakCleanup);
2991
2992         int timeout = 5000; // 5 seconds
2993         while (!SourceFactory::files_with_peaks.empty()) {
2994                 Glib::usleep (1000);
2995                 if (--timeout < 0) {
2996                         warning << _("Timeout waiting for peak-file creation to terminate before cleanup, please try again later.") << endmsg;
2997                         _state_of_the_state = StateOfTheState (_state_of_the_state & (~PeakCleanup));
2998                         return -1;
2999                 }
3000         }
3001
3002         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
3003                 boost::shared_ptr<AudioSource> as;
3004                 if ((as = boost::dynamic_pointer_cast<AudioSource> (i->second)) != 0) {
3005                         as->close_peakfile();
3006                 }
3007         }
3008
3009         PBD::clear_directory (session_directory().peak_path());
3010
3011         _state_of_the_state = StateOfTheState (_state_of_the_state & (~PeakCleanup));
3012
3013         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
3014                 boost::shared_ptr<AudioSource> as;
3015                 if ((as = boost::dynamic_pointer_cast<AudioSource> (i->second)) != 0) {
3016                         SourceFactory::setup_peakfile(as, true);
3017                 }
3018         }
3019         return 0;
3020 }
3021
3022 static void
3023 merge_all_sources (boost::shared_ptr<const Playlist> pl, std::set<boost::shared_ptr<Source> >* all_sources)
3024 {
3025         pl->deep_sources (*all_sources);
3026 }
3027
3028 int
3029 Session::cleanup_sources (CleanupReport& rep)
3030 {
3031         // FIXME: needs adaptation to midi
3032
3033         vector<boost::shared_ptr<Source> > dead_sources;
3034         string audio_path;
3035         string midi_path;
3036         vector<string> candidates;
3037         vector<string> unused;
3038         set<string> sources_used_by_all_snapshots;
3039         string spath;
3040         int ret = -1;
3041         string tmppath1;
3042         string tmppath2;
3043         Searchpath asp;
3044         Searchpath msp;
3045         set<boost::shared_ptr<Source> > sources_used_by_this_snapshot;
3046
3047         _state_of_the_state = (StateOfTheState) (_state_of_the_state | InCleanup);
3048
3049         /* this is mostly for windows which doesn't allow file
3050          * renaming if the file is in use. But we don't special
3051          * case it because we need to know if this causes
3052          * problems, and the easiest way to notice that is to
3053          * keep it in place for all platforms.
3054          */
3055
3056         request_stop (false);
3057         _butler->summon ();
3058         _butler->wait_until_finished ();
3059
3060         /* consider deleting all unused playlists */
3061
3062         if (playlists->maybe_delete_unused (boost::bind (Session::ask_about_playlist_deletion, _1))) {
3063                 ret = 0;
3064                 goto out;
3065         }
3066
3067         /* sync the "all regions" property of each playlist with its current state
3068          */
3069
3070         playlists->sync_all_regions_with_regions ();
3071
3072         /* find all un-used sources */
3073
3074         rep.paths.clear ();
3075         rep.space = 0;
3076
3077         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
3078
3079                 SourceMap::iterator tmp;
3080
3081                 tmp = i;
3082                 ++tmp;
3083
3084                 /* do not bother with files that are zero size, otherwise we remove the current "nascent"
3085                    capture files.
3086                 */
3087
3088                 if (!i->second->used() && (i->second->length(i->second->timeline_position() > 0))) {
3089                         dead_sources.push_back (i->second);
3090                         i->second->drop_references ();
3091                 }
3092
3093                 i = tmp;
3094         }
3095
3096         /* build a list of all the possible audio directories for the session */
3097
3098         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3099                 SessionDirectory sdir ((*i).path);
3100                 asp += sdir.sound_path();
3101         }
3102         audio_path += asp.to_string();
3103
3104
3105         /* build a list of all the possible midi directories for the session */
3106
3107         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3108                 SessionDirectory sdir ((*i).path);
3109                 msp += sdir.midi_path();
3110         }
3111         midi_path += msp.to_string();
3112
3113         find_files_matching_filter (candidates, audio_path, accept_all_audio_files, (void *) 0, true, true);
3114         find_files_matching_filter (candidates, midi_path, accept_all_midi_files, (void *) 0, true, true);
3115
3116         /* add sources from all other snapshots as "used", but don't use this
3117            snapshot because the state file on disk still references sources we
3118            may have already dropped.
3119         */
3120
3121         find_all_sources_across_snapshots (sources_used_by_all_snapshots, true);
3122
3123         /* Although the region factory has a list of all regions ever created
3124          * for this session, we're only interested in regions actually in
3125          * playlists right now. So merge all playlist regions lists together.
3126          *
3127          * This will include the playlists used within compound regions.
3128          */
3129
3130         playlists->foreach (boost::bind (merge_all_sources, _1, &sources_used_by_this_snapshot));
3131
3132         /*  add our current source list
3133          */
3134
3135         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
3136                 boost::shared_ptr<FileSource> fs;
3137                 SourceMap::iterator tmp = i;
3138                 ++tmp;
3139
3140                 if ((fs = boost::dynamic_pointer_cast<FileSource> (i->second)) == 0) {
3141                         /* not a file */
3142                         i = tmp;
3143                         continue;
3144                 }
3145
3146                 /* this is mostly for windows which doesn't allow file
3147                  * renaming if the file is in use. But we do not special
3148                  * case it because we need to know if this causes
3149                  * problems, and the easiest way to notice that is to
3150                  * keep it in place for all platforms.
3151                  */
3152
3153                 fs->close ();
3154
3155                 if (!fs->is_stub()) {
3156
3157                         /* Note that we're checking a list of all
3158                          * sources across all snapshots with the list
3159                          * of sources used by this snapshot.
3160                          */
3161
3162                         if (sources_used_by_this_snapshot.find (i->second) != sources_used_by_this_snapshot.end()) {
3163                                 /* this source is in use by this snapshot */
3164                                 sources_used_by_all_snapshots.insert (fs->path());
3165                                 cerr << "Source from source list found in used_by_this_snapshot (" << fs->path() << ")\n";
3166                         } else {
3167                                 cerr << "Source from source list NOT found in used_by_this_snapshot (" << fs->path() << ")\n";
3168                                 /* this source is NOT in use by this snapshot
3169                                  */
3170
3171                                 /* remove all related regions from RegionFactory master list
3172                                  */
3173
3174                                 RegionFactory::remove_regions_using_source (i->second);
3175
3176                                 /* remove from our current source list
3177                                  * also. We may not remove it from
3178                                  * disk, because it may be used by
3179                                  * other snapshots, but it isn't used inside this
3180                                  * snapshot anymore, so we don't need a
3181                                  * reference to it.
3182                                  */
3183
3184                                 sources.erase (i);
3185                         }
3186                 }
3187
3188                 i = tmp;
3189         }
3190
3191         /* now check each candidate source to see if it exists in the list of
3192            sources_used_by_all_snapshots. If it doesn't, put it into "unused".
3193         */
3194
3195         cerr << "Candidates: " << candidates.size() << endl;
3196         cerr << "Used by others: " << sources_used_by_all_snapshots.size() << endl;
3197
3198         for (vector<string>::iterator x = candidates.begin(); x != candidates.end(); ++x) {
3199
3200                 bool used = false;
3201                 spath = *x;
3202
3203                 for (set<string>::iterator i = sources_used_by_all_snapshots.begin(); i != sources_used_by_all_snapshots.end(); ++i) {
3204
3205                         tmppath1 = canonical_path (spath);
3206                         tmppath2 = canonical_path ((*i));
3207
3208                         cerr << "\t => " << tmppath2 << endl;
3209
3210                         if (tmppath1 == tmppath2) {
3211                                 used = true;
3212                                 break;
3213                         }
3214                 }
3215
3216                 if (!used) {
3217                         unused.push_back (spath);
3218                 }
3219         }
3220
3221         cerr << "Actually unused: " << unused.size() << endl;
3222
3223         if (unused.empty()) {
3224                 /* Nothing to do */
3225                 ret = 0;
3226                 goto out;
3227         }
3228
3229         /* now try to move all unused files into the "dead" directory(ies) */
3230
3231         for (vector<string>::iterator x = unused.begin(); x != unused.end(); ++x) {
3232                 GStatBuf statbuf;
3233
3234                 string newpath;
3235
3236                 /* don't move the file across filesystems, just
3237                    stick it in the `dead_dir_name' directory
3238                    on whichever filesystem it was already on.
3239                 */
3240
3241                 if ((*x).find ("/sounds/") != string::npos) {
3242
3243                         /* old school, go up 1 level */
3244
3245                         newpath = Glib::path_get_dirname (*x);      // "sounds"
3246                         newpath = Glib::path_get_dirname (newpath); // "session-name"
3247
3248                 } else {
3249
3250                         /* new school, go up 4 levels */
3251
3252                         newpath = Glib::path_get_dirname (*x);      // "audiofiles" or "midifiles"
3253                         newpath = Glib::path_get_dirname (newpath); // "session-name"
3254                         newpath = Glib::path_get_dirname (newpath); // "interchange"
3255                         newpath = Glib::path_get_dirname (newpath); // "session-dir"
3256                 }
3257
3258                 newpath = Glib::build_filename (newpath, dead_dir_name);
3259
3260                 if (g_mkdir_with_parents (newpath.c_str(), 0755) < 0) {
3261                         error << string_compose(_("Session: cannot create dead file folder \"%1\" (%2)"), newpath, strerror (errno)) << endmsg;
3262                         return -1;
3263                 }
3264
3265                 newpath = Glib::build_filename (newpath, Glib::path_get_basename ((*x)));
3266
3267                 if (Glib::file_test (newpath, Glib::FILE_TEST_EXISTS)) {
3268
3269                         /* the new path already exists, try versioning */
3270
3271                         char buf[PATH_MAX+1];
3272                         int version = 1;
3273                         string newpath_v;
3274
3275                         snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), version);
3276                         newpath_v = buf;
3277
3278                         while (Glib::file_test (newpath_v.c_str(), Glib::FILE_TEST_EXISTS) && version < 999) {
3279                                 snprintf (buf, sizeof (buf), "%s.%d", newpath.c_str(), ++version);
3280                                 newpath_v = buf;
3281                         }
3282
3283                         if (version == 999) {
3284                                 error << string_compose (_("there are already 1000 files with names like %1; versioning discontinued"),
3285                                                   newpath)
3286                                       << endmsg;
3287                         } else {
3288                                 newpath = newpath_v;
3289                         }
3290
3291                 }
3292
3293                 g_stat ((*x).c_str(), &statbuf);
3294
3295                 if (::rename ((*x).c_str(), newpath.c_str()) != 0) {
3296                         error << string_compose (_("cannot rename unused file source from %1 to %2 (%3)"), (*x), newpath, strerror (errno)) << endmsg;
3297                         continue;
3298                 }
3299
3300                 /* see if there an easy to find peakfile for this file, and remove it.
3301                  */
3302
3303                 string base = Glib::path_get_basename (*x);
3304                 base += "%A"; /* this is what we add for the channel suffix of all native files,
3305                                  or for the first channel of embedded files. it will miss
3306                                  some peakfiles for other channels
3307                               */
3308                 string peakpath = construct_peak_filepath (base);
3309
3310                 if (Glib::file_test (peakpath.c_str(), Glib::FILE_TEST_EXISTS)) {
3311                         if (::g_unlink (peakpath.c_str()) != 0) {
3312                                 error << string_compose (_("cannot remove peakfile %1 for %2 (%3)"),
3313                                                          peakpath, _path, strerror (errno))
3314                                       << endmsg;
3315                                 /* try to back out */
3316                                 ::rename (newpath.c_str(), _path.c_str());
3317                                 goto out;
3318                         }
3319                 }
3320
3321                 rep.paths.push_back (*x);
3322                 rep.space += statbuf.st_size;
3323         }
3324
3325         /* dump the history list */
3326
3327         _history.clear ();
3328
3329         /* save state so we don't end up a session file
3330            referring to non-existent sources.
3331         */
3332
3333         save_state ("");
3334         ret = 0;
3335
3336   out:
3337         _state_of_the_state = (StateOfTheState) (_state_of_the_state & ~InCleanup);
3338
3339         return ret;
3340 }
3341
3342 int
3343 Session::cleanup_trash_sources (CleanupReport& rep)
3344 {
3345         // FIXME: needs adaptation for MIDI
3346
3347         vector<space_and_path>::iterator i;
3348         string dead_dir;
3349
3350         rep.paths.clear ();
3351         rep.space = 0;
3352
3353         for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
3354
3355                 dead_dir = Glib::build_filename ((*i).path, dead_dir_name);
3356
3357                 clear_directory (dead_dir, &rep.space, &rep.paths);
3358         }
3359
3360         return 0;
3361 }
3362
3363 void
3364 Session::set_dirty ()
3365 {
3366         /* never mark session dirty during loading */
3367
3368         if (_state_of_the_state & Loading) {
3369                 return;
3370         }
3371
3372         bool was_dirty = dirty();
3373
3374         _state_of_the_state = StateOfTheState (_state_of_the_state | Dirty);
3375
3376
3377         if (!was_dirty) {
3378                 DirtyChanged(); /* EMIT SIGNAL */
3379         }
3380 }
3381
3382
3383 void
3384 Session::set_clean ()
3385 {
3386         bool was_dirty = dirty();
3387
3388         _state_of_the_state = Clean;
3389
3390
3391         if (was_dirty) {
3392                 DirtyChanged(); /* EMIT SIGNAL */
3393         }
3394 }
3395
3396 void
3397 Session::set_deletion_in_progress ()
3398 {
3399         _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
3400 }
3401
3402 void
3403 Session::clear_deletion_in_progress ()
3404 {
3405         _state_of_the_state = StateOfTheState (_state_of_the_state & (~Deletion));
3406 }
3407
3408 void
3409 Session::add_controllable (boost::shared_ptr<Controllable> c)
3410 {
3411         /* this adds a controllable to the list managed by the Session.
3412            this is a subset of those managed by the Controllable class
3413            itself, and represents the only ones whose state will be saved
3414            as part of the session.
3415         */
3416
3417         Glib::Threads::Mutex::Lock lm (controllables_lock);
3418         controllables.insert (c);
3419 }
3420
3421 struct null_deleter { void operator()(void const *) const {} };
3422
3423 void
3424 Session::remove_controllable (Controllable* c)
3425 {
3426         if (_state_of_the_state & Deletion) {
3427                 return;
3428         }
3429
3430         Glib::Threads::Mutex::Lock lm (controllables_lock);
3431
3432         Controllables::iterator x = controllables.find (boost::shared_ptr<Controllable>(c, null_deleter()));
3433
3434         if (x != controllables.end()) {
3435                 controllables.erase (x);
3436         }
3437 }
3438
3439 boost::shared_ptr<Controllable>
3440 Session::controllable_by_id (const PBD::ID& id)
3441 {
3442         Glib::Threads::Mutex::Lock lm (controllables_lock);
3443
3444         for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
3445                 if ((*i)->id() == id) {
3446                         return *i;
3447                 }
3448         }
3449
3450         return boost::shared_ptr<Controllable>();
3451 }
3452
3453 boost::shared_ptr<Controllable>
3454 Session::controllable_by_descriptor (const ControllableDescriptor& desc)
3455 {
3456         boost::shared_ptr<Controllable> c;
3457         boost::shared_ptr<Stripable> s;
3458         boost::shared_ptr<Route> r;
3459
3460         switch (desc.top_level_type()) {
3461         case ControllableDescriptor::NamedRoute:
3462         {
3463                 std::string str = desc.top_level_name();
3464
3465                 if (str == "Master" || str == "master") {
3466                         s = _master_out;
3467                 } else if (str == "control" || str == "listen" || str == "monitor" || str == "Monitor") {
3468                         s = _monitor_out;
3469                 } else if (str == "auditioner") {
3470                         s = auditioner;
3471                 } else {
3472                         s = route_by_name (desc.top_level_name());
3473                 }
3474
3475                 break;
3476         }
3477
3478         case ControllableDescriptor::PresentationOrderRoute:
3479                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Route);
3480                 break;
3481
3482         case ControllableDescriptor::PresentationOrderTrack:
3483                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Track);
3484                 break;
3485
3486         case ControllableDescriptor::PresentationOrderBus:
3487                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::Bus);
3488                 break;
3489
3490         case ControllableDescriptor::PresentationOrderVCA:
3491                 s = get_remote_nth_stripable (desc.presentation_order(), PresentationInfo::VCA);
3492                 break;
3493
3494         case ControllableDescriptor::SelectionCount:
3495                 s = route_by_selected_count (desc.selection_id());
3496                 break;
3497         }
3498
3499         if (!s) {
3500                 return c;
3501         }
3502
3503         r = boost::dynamic_pointer_cast<Route> (s);
3504
3505         switch (desc.subtype()) {
3506         case ControllableDescriptor::Gain:
3507                 c = s->gain_control ();
3508                 break;
3509
3510         case ControllableDescriptor::Trim:
3511                 c = s->trim_control ();
3512                 break;
3513
3514         case ControllableDescriptor::Solo:
3515                 c = s->solo_control();
3516                 break;
3517
3518         case ControllableDescriptor::Mute:
3519                 c = s->mute_control();
3520                 break;
3521
3522         case ControllableDescriptor::Recenable:
3523                 c = s->rec_enable_control ();
3524                 break;
3525
3526         case ControllableDescriptor::PanDirection:
3527                 c = s->pan_azimuth_control();
3528                 break;
3529
3530         case ControllableDescriptor::PanWidth:
3531                 c = s->pan_width_control();
3532                 break;
3533
3534         case ControllableDescriptor::PanElevation:
3535                 c = s->pan_elevation_control();
3536                 break;
3537
3538         case ControllableDescriptor::Balance:
3539                 /* XXX simple pan control */
3540                 break;
3541
3542         case ControllableDescriptor::PluginParameter:
3543         {
3544                 uint32_t plugin = desc.target (0);
3545                 uint32_t parameter_index = desc.target (1);
3546
3547                 /* revert to zero based counting */
3548
3549                 if (plugin > 0) {
3550                         --plugin;
3551                 }
3552
3553                 if (parameter_index > 0) {
3554                         --parameter_index;
3555                 }
3556
3557                 if (!r) {
3558                         return c;
3559                 }
3560
3561                 boost::shared_ptr<Processor> p = r->nth_plugin (plugin);
3562
3563                 if (p) {
3564                         c = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
3565                                 p->control(Evoral::Parameter(PluginAutomation, 0, parameter_index)));
3566                 }
3567                 break;
3568         }
3569
3570         case ControllableDescriptor::SendGain: {
3571                 uint32_t send = desc.target (0);
3572                 if (send > 0) {
3573                         --send;
3574                 }
3575                 if (!r) {
3576                         return c;
3577                 }
3578                 c = r->send_level_controllable (send);
3579                 break;
3580         }
3581
3582         default:
3583                 /* relax and return a null pointer */
3584                 break;
3585         }
3586
3587         return c;
3588 }
3589
3590 void
3591 Session::add_instant_xml (XMLNode& node, bool write_to_config)
3592 {
3593         if (_writable) {
3594                 Stateful::add_instant_xml (node, _path);
3595         }
3596
3597         if (write_to_config) {
3598                 Config->add_instant_xml (node);
3599         }
3600 }
3601
3602 XMLNode*
3603 Session::instant_xml (const string& node_name)
3604 {
3605         return Stateful::instant_xml (node_name, _path);
3606 }
3607
3608 int
3609 Session::save_history (string snapshot_name)
3610 {
3611         XMLTree tree;
3612
3613         if (!_writable) {
3614                 return 0;
3615         }
3616
3617         if (!Config->get_save_history() || Config->get_saved_history_depth() < 0 ||
3618             (_history.undo_depth() == 0 && _history.redo_depth() == 0)) {
3619                 return 0;
3620         }
3621
3622         if (snapshot_name.empty()) {
3623                 snapshot_name = _current_snapshot_name;
3624         }
3625
3626         const string history_filename = legalize_for_path (snapshot_name) + history_suffix;
3627         const string backup_filename = history_filename + backup_suffix;
3628         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), history_filename));
3629         const std::string backup_path(Glib::build_filename (_session_dir->root_path(), backup_filename));
3630
3631         if (Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3632                 if (::g_rename (xml_path.c_str(), backup_path.c_str()) != 0) {
3633                         error << _("could not backup old history file, current history not saved") << endmsg;
3634                         return -1;
3635                 }
3636         }
3637
3638         tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
3639
3640         if (!tree.write (xml_path))
3641         {
3642                 error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
3643
3644                 if (g_remove (xml_path.c_str()) != 0) {
3645                         error << string_compose(_("Could not remove history file at path \"%1\" (%2)"),
3646                                         xml_path, g_strerror (errno)) << endmsg;
3647                 }
3648                 if (::g_rename (backup_path.c_str(), xml_path.c_str()) != 0) {
3649                         error << string_compose (_("could not restore history file from backup %1 (%2)"),
3650                                         backup_path, g_strerror (errno)) << endmsg;
3651                 }
3652
3653                 return -1;
3654         }
3655
3656         return 0;
3657 }
3658
3659 int
3660 Session::restore_history (string snapshot_name)
3661 {
3662         XMLTree tree;
3663
3664         if (snapshot_name.empty()) {
3665                 snapshot_name = _current_snapshot_name;
3666         }
3667
3668         const std::string xml_filename = legalize_for_path (snapshot_name) + history_suffix;
3669         const std::string xml_path(Glib::build_filename (_session_dir->root_path(), xml_filename));
3670
3671         info << "Loading history from " << xml_path << endmsg;
3672
3673         if (!Glib::file_test (xml_path, Glib::FILE_TEST_EXISTS)) {
3674                 info << string_compose (_("%1: no history file \"%2\" for this session."),
3675                                 _name, xml_path) << endmsg;
3676                 return 1;
3677         }
3678
3679         if (!tree.read (xml_path)) {
3680                 error << string_compose (_("Could not understand session history file \"%1\""),
3681                                 xml_path) << endmsg;
3682                 return -1;
3683         }
3684
3685         // replace history
3686         _history.clear();
3687
3688         for (XMLNodeConstIterator it  = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
3689
3690                 XMLNode *t = *it;
3691                 UndoTransaction* ut = new UndoTransaction ();
3692                 struct timeval tv;
3693
3694                 ut->set_name(t->property("name")->value());
3695                 stringstream ss(t->property("tv-sec")->value());
3696                 ss >> tv.tv_sec;
3697                 ss.str(t->property("tv-usec")->value());
3698                 ss >> tv.tv_usec;
3699                 ut->set_timestamp(tv);
3700
3701                 for (XMLNodeConstIterator child_it  = t->children().begin();
3702                                 child_it != t->children().end(); child_it++)
3703                 {
3704                         XMLNode *n = *child_it;
3705                         Command *c;
3706
3707                         if (n->name() == "MementoCommand" ||
3708                                         n->name() == "MementoUndoCommand" ||
3709                                         n->name() == "MementoRedoCommand") {
3710
3711                                 if ((c = memento_command_factory(n))) {
3712                                         ut->add_command(c);
3713                                 }
3714
3715                         } else if (n->name() == "NoteDiffCommand") {
3716                                 PBD::ID id (n->property("midi-source")->value());
3717                                 boost::shared_ptr<MidiSource> midi_source =
3718                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3719                                 if (midi_source) {
3720                                         ut->add_command (new MidiModel::NoteDiffCommand(midi_source->model(), *n));
3721                                 } else {
3722                                         error << _("Failed to downcast MidiSource for NoteDiffCommand") << endmsg;
3723                                 }
3724
3725                         } else if (n->name() == "SysExDiffCommand") {
3726
3727                                 PBD::ID id (n->property("midi-source")->value());
3728                                 boost::shared_ptr<MidiSource> midi_source =
3729                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3730                                 if (midi_source) {
3731                                         ut->add_command (new MidiModel::SysExDiffCommand (midi_source->model(), *n));
3732                                 } else {
3733                                         error << _("Failed to downcast MidiSource for SysExDiffCommand") << endmsg;
3734                                 }
3735
3736                         } else if (n->name() == "PatchChangeDiffCommand") {
3737
3738                                 PBD::ID id (n->property("midi-source")->value());
3739                                 boost::shared_ptr<MidiSource> midi_source =
3740                                         boost::dynamic_pointer_cast<MidiSource, Source>(source_by_id(id));
3741                                 if (midi_source) {
3742                                         ut->add_command (new MidiModel::PatchChangeDiffCommand (midi_source->model(), *n));
3743                                 } else {
3744                                         error << _("Failed to downcast MidiSource for PatchChangeDiffCommand") << endmsg;
3745                                 }
3746
3747                         } else if (n->name() == "StatefulDiffCommand") {
3748                                 if ((c = stateful_diff_command_factory (n))) {
3749                                         ut->add_command (c);
3750                                 }
3751                         } else {
3752                                 error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
3753                         }
3754                 }
3755
3756                 _history.add (ut);
3757         }
3758
3759         return 0;
3760 }
3761
3762 void
3763 Session::config_changed (std::string p, bool ours)
3764 {
3765         if (ours) {
3766                 set_dirty ();
3767         }
3768
3769         if (p == "seamless-loop") {
3770
3771         } else if (p == "rf-speed") {
3772
3773         } else if (p == "auto-loop") {
3774
3775         } else if (p == "auto-input") {
3776
3777                 if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
3778                         /* auto-input only makes a difference if we're rolling */
3779                         set_track_monitor_input_status (!config.get_auto_input());
3780                 }
3781
3782         } else if (p == "punch-in") {
3783
3784                 Location* location;
3785
3786                 if ((location = _locations->auto_punch_location()) != 0) {
3787
3788                         if (config.get_punch_in ()) {
3789                                 replace_event (SessionEvent::PunchIn, location->start());
3790                         } else {
3791                                 remove_event (location->start(), SessionEvent::PunchIn);
3792                         }
3793                 }
3794
3795         } else if (p == "punch-out") {
3796
3797                 Location* location;
3798
3799                 if ((location = _locations->auto_punch_location()) != 0) {
3800
3801                         if (config.get_punch_out()) {
3802                                 replace_event (SessionEvent::PunchOut, location->end());
3803                         } else {
3804                                 clear_events (SessionEvent::PunchOut);
3805                         }
3806                 }
3807
3808         } else if (p == "edit-mode") {
3809
3810                 Glib::Threads::Mutex::Lock lm (playlists->lock);
3811
3812                 for (SessionPlaylists::List::iterator i = playlists->playlists.begin(); i != playlists->playlists.end(); ++i) {
3813                         (*i)->set_edit_mode (Config->get_edit_mode ());
3814                 }
3815
3816         } else if (p == "use-video-sync") {
3817
3818                 waiting_for_sync_offset = config.get_use_video_sync();
3819
3820         } else if (p == "mmc-control") {
3821
3822                 //poke_midi_thread ();
3823
3824         } else if (p == "mmc-device-id" || p == "mmc-receive-id" || p == "mmc-receive-device-id") {
3825
3826                 _mmc->set_receive_device_id (Config->get_mmc_receive_device_id());
3827
3828         } else if (p == "mmc-send-id" || p == "mmc-send-device-id") {
3829
3830                 _mmc->set_send_device_id (Config->get_mmc_send_device_id());
3831
3832         } else if (p == "midi-control") {
3833
3834                 //poke_midi_thread ();
3835
3836         } else if (p == "raid-path") {
3837
3838                 setup_raid_path (config.get_raid_path());
3839
3840         } else if (p == "timecode-format") {
3841
3842                 sync_time_vars ();
3843
3844         } else if (p == "video-pullup") {
3845
3846                 sync_time_vars ();
3847
3848         } else if (p == "seamless-loop") {
3849
3850                 if (play_loop && transport_rolling()) {
3851                         // to reset diskstreams etc
3852                         request_play_loop (true);
3853                 }
3854
3855         } else if (p == "rf-speed") {
3856
3857                 cumulative_rf_motion = 0;
3858                 reset_rf_scale (0);
3859
3860         } else if (p == "click-sound") {
3861
3862                 setup_click_sounds (1);
3863
3864         } else if (p == "click-emphasis-sound") {
3865
3866                 setup_click_sounds (-1);
3867
3868         } else if (p == "clicking") {
3869
3870                 if (Config->get_clicking()) {
3871                         if (_click_io && click_data) { // don't require emphasis data
3872                                 _clicking = true;
3873                         }
3874                 } else {
3875                         _clicking = false;
3876                 }
3877
3878         } else if (p == "click-gain") {
3879
3880                 if (_click_gain) {
3881                         _click_gain->gain_control()->set_value (Config->get_click_gain(), Controllable::NoGroup);
3882                 }
3883
3884         } else if (p == "send-mtc") {
3885
3886                 if (Config->get_send_mtc ()) {
3887                         /* mark us ready to send */
3888                         next_quarter_frame_to_send = 0;
3889                 }
3890
3891         } else if (p == "send-mmc") {
3892
3893                 _mmc->enable_send (Config->get_send_mmc ());
3894
3895         } else if (p == "midi-feedback") {
3896
3897                 session_midi_feedback = Config->get_midi_feedback();
3898
3899         } else if (p == "jack-time-master") {
3900
3901                 engine().reset_timebase ();
3902
3903         } else if (p == "native-file-header-format") {
3904
3905                 if (!first_file_header_format_reset) {
3906                         reset_native_file_format ();
3907                 }
3908
3909                 first_file_header_format_reset = false;
3910
3911         } else if (p == "native-file-data-format") {
3912
3913                 if (!first_file_data_format_reset) {
3914                         reset_native_file_format ();
3915                 }
3916
3917                 first_file_data_format_reset = false;
3918
3919         } else if (p == "external-sync") {
3920                 if (!config.get_external_sync()) {
3921                         drop_sync_source ();
3922                 } else {
3923                         switch_to_sync_source (Config->get_sync_source());
3924                 }
3925         }  else if (p == "denormal-model") {
3926                 setup_fpu ();
3927         } else if (p == "history-depth") {
3928                 set_history_depth (Config->get_history_depth());
3929         } else if (p == "remote-model") {
3930                 /* XXX DO SOMETHING HERE TO TELL THE GUI THAT WE NEED
3931                    TO SET REMOTE ID'S
3932                 */
3933         } else if (p == "initial-program-change") {
3934
3935                 if (_mmc->output_port() && Config->get_initial_program_change() >= 0) {
3936                         MIDI::byte buf[2];
3937
3938                         buf[0] = MIDI::program; // channel zero by default
3939                         buf[1] = (Config->get_initial_program_change() & 0x7f);
3940
3941                         _mmc->output_port()->midimsg (buf, sizeof (buf), 0);
3942                 }
3943         } else if (p == "solo-mute-override") {
3944                 // catch_up_on_solo_mute_override ();
3945         } else if (p == "listen-position" || p == "pfl-position") {
3946                 listen_position_changed ();
3947         } else if (p == "solo-control-is-listen-control") {
3948                 solo_control_mode_changed ();
3949         } else if (p == "solo-mute-gain") {
3950                 _solo_cut_control->Changed (true, Controllable::NoGroup);
3951         } else if (p == "timecode-offset" || p == "timecode-offset-negative") {
3952                 last_timecode_valid = false;
3953         } else if (p == "playback-buffer-seconds") {
3954                 AudioSource::allocate_working_buffers (frame_rate());
3955         } else if (p == "ltc-source-port") {
3956                 reconnect_ltc_input ();
3957         } else if (p == "ltc-sink-port") {
3958                 reconnect_ltc_output ();
3959         } else if (p == "timecode-generator-offset") {
3960                 ltc_tx_parse_offset();
3961         } else if (p == "auto-return-target-list") {
3962                 follow_playhead_priority ();
3963         }
3964
3965         set_dirty ();
3966 }
3967
3968 void
3969 Session::set_history_depth (uint32_t d)
3970 {
3971         _history.set_depth (d);
3972 }
3973
3974 int
3975 Session::load_diskstreams_2X (XMLNode const & node, int)
3976 {
3977         XMLNodeList          clist;
3978         XMLNodeConstIterator citer;
3979
3980         clist = node.children();
3981
3982         for (citer = clist.begin(); citer != clist.end(); ++citer) {
3983
3984                 try {
3985                         /* diskstreams added automatically by DiskstreamCreated handler */
3986                         if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
3987                                 boost::shared_ptr<AudioDiskstream> dsp (new AudioDiskstream (*this, **citer));
3988                                 _diskstreams_2X.push_back (dsp);
3989                         } else {
3990                                 error << _("Session: unknown diskstream type in XML") << endmsg;
3991                         }
3992                 }
3993
3994                 catch (failed_constructor& err) {
3995                         error << _("Session: could not load diskstream via XML state") << endmsg;
3996                         return -1;
3997                 }
3998         }
3999
4000         return 0;
4001 }
4002
4003 /** Connect things to the MMC object */
4004 void
4005 Session::setup_midi_machine_control ()
4006 {
4007         _mmc = new MIDI::MachineControl;
4008
4009         boost::shared_ptr<AsyncMIDIPort> async_in = boost::dynamic_pointer_cast<AsyncMIDIPort> (_midi_ports->mmc_input_port());
4010         boost::shared_ptr<AsyncMIDIPort> async_out = boost::dynamic_pointer_cast<AsyncMIDIPort> (_midi_ports->mmc_output_port());
4011
4012         if (!async_out || !async_out) {
4013                 return;
4014         }
4015
4016         /* XXXX argh, passing raw pointers back into libmidi++ */
4017
4018         MIDI::Port* mmc_in = async_in.get();
4019         MIDI::Port* mmc_out = async_out.get();
4020
4021         _mmc->set_ports (mmc_in, mmc_out);
4022
4023         _mmc->Play.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
4024         _mmc->DeferredPlay.connect_same_thread (*this, boost::bind (&Session::mmc_deferred_play, this, _1));
4025         _mmc->Stop.connect_same_thread (*this, boost::bind (&Session::mmc_stop, this, _1));
4026         _mmc->FastForward.connect_same_thread (*this, boost::bind (&Session::mmc_fast_forward, this, _1));
4027         _mmc->Rewind.connect_same_thread (*this, boost::bind (&Session::mmc_rewind, this, _1));
4028         _mmc->Pause.connect_same_thread (*this, boost::bind (&Session::mmc_pause, this, _1));
4029         _mmc->RecordPause.connect_same_thread (*this, boost::bind (&Session::mmc_record_pause, this, _1));
4030         _mmc->RecordStrobe.connect_same_thread (*this, boost::bind (&Session::mmc_record_strobe, this, _1));
4031         _mmc->RecordExit.connect_same_thread (*this, boost::bind (&Session::mmc_record_exit, this, _1));
4032         _mmc->Locate.connect_same_thread (*this, boost::bind (&Session::mmc_locate, this, _1, _2));
4033         _mmc->Step.connect_same_thread (*this, boost::bind (&Session::mmc_step, this, _1, _2));
4034         _mmc->Shuttle.connect_same_thread (*this, boost::bind (&Session::mmc_shuttle, this, _1, _2, _3));
4035         _mmc->TrackRecordStatusChange.connect_same_thread (*this, boost::bind (&Session::mmc_record_enable, this, _1, _2, _3));
4036
4037         /* also handle MIDI SPP because its so common */
4038
4039         _mmc->SPPStart.connect_same_thread (*this, boost::bind (&Session::spp_start, this));
4040         _mmc->SPPContinue.connect_same_thread (*this, boost::bind (&Session::spp_continue, this));
4041         _mmc->SPPStop.connect_same_thread (*this, boost::bind (&Session::spp_stop, this));
4042 }
4043
4044 boost::shared_ptr<Controllable>
4045 Session::solo_cut_control() const
4046 {
4047         /* the solo cut control is a bit of an anomaly, at least as of Febrary 2011. There are no other
4048            controls in Ardour that currently get presented to the user in the GUI that require
4049            access as a Controllable and are also NOT owned by some SessionObject (e.g. Route, or MonitorProcessor).
4050
4051            its actually an RCConfiguration parameter, so we use a ProxyControllable to wrap
4052            it up as a Controllable. Changes to the Controllable will just map back to the RCConfiguration
4053            parameter.
4054         */
4055
4056         return _solo_cut_control;
4057 }
4058
4059 void
4060 Session::save_snapshot_name (const std::string & n)
4061 {
4062         /* assure Stateful::_instant_xml is loaded
4063          * add_instant_xml() only adds to existing data and defaults
4064          * to use an empty Tree otherwise
4065          */
4066         instant_xml ("LastUsedSnapshot");
4067
4068         XMLNode* last_used_snapshot = new XMLNode ("LastUsedSnapshot");
4069         last_used_snapshot->add_property ("name", string(n));
4070         add_instant_xml (*last_used_snapshot, false);
4071 }
4072
4073 void
4074 Session::set_snapshot_name (const std::string & n)
4075 {
4076         _current_snapshot_name = n;
4077         save_snapshot_name (n);
4078 }
4079
4080 int
4081 Session::rename (const std::string& new_name)
4082 {
4083         string legal_name = legalize_for_path (new_name);
4084         string new_path;
4085         string oldstr;
4086         string newstr;
4087         bool first = true;
4088
4089         string const old_sources_root = _session_dir->sources_root();
4090
4091         if (!_writable || (_state_of_the_state & CannotSave)) {
4092                 error << _("Cannot rename read-only session.") << endmsg;
4093                 return 0; // don't show "messed up" warning
4094         }
4095         if (record_status() == Recording) {
4096                 error << _("Cannot rename session while recording") << endmsg;
4097                 return 0; // don't show "messed up" warning
4098         }
4099
4100         StateProtector stp (this);
4101
4102         /* Rename:
4103
4104          * session directory
4105          * interchange subdirectory
4106          * session file
4107          * session history
4108
4109          * Backup files are left unchanged and not renamed.
4110          */
4111
4112         /* Windows requires that we close all files before attempting the
4113          * rename. This works on other platforms, but isn't necessary there.
4114          * Leave it in place for all platforms though, since it may help
4115          * catch issues that could arise if the way Source files work ever
4116          * change (since most developers are not using Windows).
4117          */
4118
4119         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4120                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4121                 if (fs) {
4122                         fs->close ();
4123                 }
4124         }
4125
4126         /* pass one: not 100% safe check that the new directory names don't
4127          * already exist ...
4128          */
4129
4130         for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4131
4132                 oldstr = (*i).path;
4133
4134                 /* this is a stupid hack because Glib::path_get_dirname() is
4135                  * lexical-only, and so passing it /a/b/c/ gives a different
4136                  * result than passing it /a/b/c ...
4137                  */
4138
4139                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
4140                         oldstr = oldstr.substr (0, oldstr.length() - 1);
4141                 }
4142
4143                 string base = Glib::path_get_dirname (oldstr);
4144
4145                 newstr = Glib::build_filename (base, legal_name);
4146
4147                 cerr << "Looking for " << newstr << endl;
4148
4149                 if (Glib::file_test (newstr, Glib::FILE_TEST_EXISTS)) {
4150                         cerr << " exists\n";
4151                         return -1;
4152                 }
4153         }
4154
4155         /* Session dirs */
4156
4157         first = true;
4158
4159         for (vector<space_and_path>::iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
4160
4161                 vector<string> v;
4162
4163                 oldstr = (*i).path;
4164
4165                 /* this is a stupid hack because Glib::path_get_dirname() is
4166                  * lexical-only, and so passing it /a/b/c/ gives a different
4167                  * result than passing it /a/b/c ...
4168                  */
4169
4170                 if (oldstr[oldstr.length()-1] == G_DIR_SEPARATOR) {
4171                         oldstr = oldstr.substr (0, oldstr.length() - 1);
4172                 }
4173
4174                 string base = Glib::path_get_dirname (oldstr);
4175                 newstr = Glib::build_filename (base, legal_name);
4176
4177                 cerr << "for " << oldstr << " new dir = " << newstr << endl;
4178
4179                 cerr << "Rename " << oldstr << " => " << newstr << endl;
4180                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4181                         cerr << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4182                         error << string_compose (_("renaming %s as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4183                         return 1;
4184                 }
4185
4186                 /* Reset path in "session dirs" */
4187
4188                 (*i).path = newstr;
4189                 (*i).blocks = 0;
4190
4191                 /* reset primary SessionDirectory object */
4192
4193                 if (first) {
4194                         (*_session_dir) = newstr;
4195                         new_path = newstr;
4196                         first = false;
4197                 }
4198
4199                 /* now rename directory below session_dir/interchange */
4200
4201                 string old_interchange_dir;
4202                 string new_interchange_dir;
4203
4204                 /* use newstr here because we renamed the path
4205                  * (folder/directory) that used to be oldstr to newstr above
4206                  */
4207
4208                 v.push_back (newstr);
4209                 v.push_back (interchange_dir_name);
4210                 v.push_back (Glib::path_get_basename (oldstr));
4211
4212                 old_interchange_dir = Glib::build_filename (v);
4213
4214                 v.clear ();
4215                 v.push_back (newstr);
4216                 v.push_back (interchange_dir_name);
4217                 v.push_back (legal_name);
4218
4219                 new_interchange_dir = Glib::build_filename (v);
4220
4221                 cerr << "Rename " << old_interchange_dir << " => " << new_interchange_dir << endl;
4222
4223                 if (::g_rename (old_interchange_dir.c_str(), new_interchange_dir.c_str()) != 0) {
4224                         cerr << string_compose (_("renaming %s as %2 failed (%3)"),
4225                                                  old_interchange_dir, new_interchange_dir,
4226                                                  g_strerror (errno))
4227                               << endl;
4228                         error << string_compose (_("renaming %s as %2 failed (%3)"),
4229                                                  old_interchange_dir, new_interchange_dir,
4230                                                  g_strerror (errno))
4231                               << endmsg;
4232                         return 1;
4233                 }
4234         }
4235
4236         /* state file */
4237
4238         oldstr = Glib::build_filename (new_path, _current_snapshot_name + statefile_suffix);
4239         newstr= Glib::build_filename (new_path, legal_name + statefile_suffix);
4240
4241         cerr << "Rename " << oldstr << " => " << newstr << endl;
4242
4243         if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4244                 cerr << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4245                 error << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4246                 return 1;
4247         }
4248
4249         /* history file */
4250
4251         oldstr = Glib::build_filename (new_path, _current_snapshot_name) + history_suffix;
4252
4253         if (Glib::file_test (oldstr, Glib::FILE_TEST_EXISTS))  {
4254                 newstr = Glib::build_filename (new_path, legal_name) + history_suffix;
4255
4256                 cerr << "Rename " << oldstr << " => " << newstr << endl;
4257
4258                 if (::g_rename (oldstr.c_str(), newstr.c_str()) != 0) {
4259                         cerr << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endl;
4260                         error << string_compose (_("renaming %1 as %2 failed (%3)"), oldstr, newstr, g_strerror (errno)) << endmsg;
4261                         return 1;
4262                 }
4263         }
4264
4265         /* remove old name from recent sessions */
4266         remove_recent_sessions (_path);
4267         _path = new_path;
4268
4269         /* update file source paths */
4270
4271         for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
4272                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4273                 if (fs) {
4274                         string p = fs->path ();
4275                         boost::replace_all (p, old_sources_root, _session_dir->sources_root());
4276                         fs->set_path (p);
4277                         SourceFactory::setup_peakfile(i->second, true);
4278                 }
4279         }
4280
4281         set_snapshot_name (new_name);
4282         _name = new_name;
4283
4284         set_dirty ();
4285
4286         /* save state again to get everything just right */
4287
4288         save_state (_current_snapshot_name);
4289
4290         /* add to recent sessions */
4291
4292         store_recent_sessions (new_name, _path);
4293
4294         return 0;
4295 }
4296
4297 int
4298 Session::get_session_info_from_path (XMLTree& tree, const string& xmlpath)
4299 {
4300         if (!Glib::file_test (xmlpath, Glib::FILE_TEST_EXISTS)) {
4301                 return -1;
4302         }
4303
4304         if (!tree.read (xmlpath)) {
4305                 return -1;
4306         }
4307
4308         return 0;
4309 }
4310
4311 int
4312 Session::get_info_from_path (const string& xmlpath, float& sample_rate, SampleFormat& data_format)
4313 {
4314         XMLTree tree;
4315         bool found_sr = false;
4316         bool found_data_format = false;
4317
4318         if (get_session_info_from_path (tree, xmlpath)) {
4319                 return -1;
4320         }
4321
4322         /* sample rate */
4323
4324         XMLProperty const * prop;
4325         XMLNode const * root (tree.root());
4326
4327         if ((prop = root->property (X_("sample-rate"))) != 0) {
4328                 sample_rate = atoi (prop->value());
4329                 found_sr = true;
4330         }
4331
4332         const XMLNodeList& children (root->children());
4333         for (XMLNodeList::const_iterator c = children.begin(); c != children.end(); ++c) {
4334                 const XMLNode* child = *c;
4335                 if (child->name() == "Config") {
4336                         const XMLNodeList& options (child->children());
4337                         for (XMLNodeList::const_iterator oc = options.begin(); oc != options.end(); ++oc) {
4338                                 XMLNode const * option = *oc;
4339                                 XMLProperty const * name = option->property("name");
4340
4341                                 if (!name) {
4342                                         continue;
4343                                 }
4344
4345                                 if (name->value() == "native-file-data-format") {
4346                                         XMLProperty const * value = option->property ("value");
4347                                         if (value) {
4348                                                 SampleFormat fmt = (SampleFormat) string_2_enum (option->property ("value")->value(), fmt);
4349                                                 data_format = fmt;
4350                                                 found_data_format = true;
4351                                                 break;
4352                                         }
4353                                 }
4354                         }
4355                 }
4356                 if (found_data_format) {
4357                         break;
4358                 }
4359         }
4360
4361         return !(found_sr && found_data_format); // zero if they are both found
4362 }
4363
4364 std::string
4365 Session::get_snapshot_from_instant (const std::string& session_dir)
4366 {
4367         std::string instant_xml_path = Glib::build_filename (session_dir, "instant.xml");
4368
4369         if (!Glib::file_test (instant_xml_path, Glib::FILE_TEST_EXISTS)) {
4370                 return "";
4371         }
4372
4373         XMLTree tree;
4374         if (!tree.read (instant_xml_path)) {
4375                 return "";
4376         }
4377
4378         XMLProperty const * prop;
4379         XMLNode *last_used_snapshot = tree.root()->child("LastUsedSnapshot");
4380         if (last_used_snapshot && (prop = last_used_snapshot->property ("name")) != 0) {
4381                 return prop->value();
4382         }
4383
4384         return "";
4385 }
4386
4387 typedef std::vector<boost::shared_ptr<FileSource> > SeveralFileSources;
4388 typedef std::map<std::string,SeveralFileSources> SourcePathMap;
4389
4390 int
4391 Session::bring_all_sources_into_session (boost::function<void(uint32_t,uint32_t,string)> callback)
4392 {
4393         uint32_t total = 0;
4394         uint32_t n = 0;
4395         SourcePathMap source_path_map;
4396         string new_path;
4397         boost::shared_ptr<AudioFileSource> afs;
4398         int ret = 0;
4399
4400         {
4401
4402                 Glib::Threads::Mutex::Lock lm (source_lock);
4403
4404                 cerr << " total sources = " << sources.size();
4405
4406                 for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4407                         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4408
4409                         if (!fs) {
4410                                 continue;
4411                         }
4412
4413                         if (fs->within_session()) {
4414                                 continue;
4415                         }
4416
4417                         if (source_path_map.find (fs->path()) != source_path_map.end()) {
4418                                 source_path_map[fs->path()].push_back (fs);
4419                         } else {
4420                                 SeveralFileSources v;
4421                                 v.push_back (fs);
4422                                 source_path_map.insert (make_pair (fs->path(), v));
4423                         }
4424
4425                         total++;
4426                 }
4427
4428                 cerr << " fsources = " << total << endl;
4429
4430                 for (SourcePathMap::iterator i = source_path_map.begin(); i != source_path_map.end(); ++i) {
4431
4432                         /* tell caller where we are */
4433
4434                         string old_path = i->first;
4435
4436                         callback (n, total, old_path);
4437
4438                         cerr << old_path << endl;
4439
4440                         new_path.clear ();
4441
4442                         switch (i->second.front()->type()) {
4443                         case DataType::AUDIO:
4444                                 new_path = new_audio_source_path_for_embedded (old_path);
4445                                 break;
4446
4447                         case DataType::MIDI:
4448                                 /* XXX not implemented yet */
4449                                 break;
4450                         }
4451
4452                         if (new_path.empty()) {
4453                                 continue;
4454                         }
4455
4456                         cerr << "Move " << old_path << " => " << new_path << endl;
4457
4458                         if (!copy_file (old_path, new_path)) {
4459                                 cerr << "failed !\n";
4460                                 ret = -1;
4461                         }
4462
4463                         /* make sure we stop looking in the external
4464                            dir/folder. Remember, this is an all-or-nothing
4465                            operations, it doesn't merge just some files.
4466                         */
4467                         remove_dir_from_search_path (Glib::path_get_dirname (old_path), i->second.front()->type());
4468
4469                         for (SeveralFileSources::iterator f = i->second.begin(); f != i->second.end(); ++f) {
4470                                 (*f)->set_path (new_path);
4471                         }
4472                 }
4473         }
4474
4475         save_state ("", false, false);
4476
4477         return ret;
4478 }
4479
4480 static
4481 bool accept_all_files (string const &, void *)
4482 {
4483         return true;
4484 }
4485
4486 void
4487 Session::save_as_bring_callback (uint32_t,uint32_t,string)
4488 {
4489         /* It would be good if this did something useful vis-a-vis save-as, but the arguments doesn't provide the correct information right now to do this.
4490         */
4491 }
4492
4493 static string
4494 make_new_media_path (string old_path, string new_session_folder, string new_session_path)
4495 {
4496         /* typedir is the "midifiles" or "audiofiles" etc. part of the path. */
4497
4498         string typedir = Glib::path_get_basename (Glib::path_get_dirname (old_path));
4499         vector<string> v;
4500         v.push_back (new_session_folder); /* full path */
4501         v.push_back (interchange_dir_name);
4502         v.push_back (new_session_path);   /* just one directory/folder */
4503         v.push_back (typedir);
4504         v.push_back (Glib::path_get_basename (old_path));
4505
4506         return Glib::build_filename (v);
4507 }
4508
4509 int
4510 Session::save_as (SaveAs& saveas)
4511 {
4512         vector<string> files;
4513         string current_folder = Glib::path_get_dirname (_path);
4514         string new_folder = legalize_for_path (saveas.new_name);
4515         string to_dir = Glib::build_filename (saveas.new_parent_folder, new_folder);
4516         int64_t total_bytes = 0;
4517         int64_t copied = 0;
4518         int64_t cnt = 0;
4519         int64_t all = 0;
4520         int32_t internal_file_cnt = 0;
4521
4522         vector<string> do_not_copy_extensions;
4523         do_not_copy_extensions.push_back (statefile_suffix);
4524         do_not_copy_extensions.push_back (pending_suffix);
4525         do_not_copy_extensions.push_back (backup_suffix);
4526         do_not_copy_extensions.push_back (temp_suffix);
4527         do_not_copy_extensions.push_back (history_suffix);
4528
4529         /* get total size */
4530
4531         for (vector<space_and_path>::const_iterator sd = session_dirs.begin(); sd != session_dirs.end(); ++sd) {
4532
4533                 /* need to clear this because
4534                  * find_files_matching_filter() is cumulative
4535                  */
4536
4537                 files.clear ();
4538
4539                 find_files_matching_filter (files, (*sd).path, accept_all_files, 0, false, true, true);
4540
4541                 all += files.size();
4542
4543                 for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
4544                         GStatBuf gsb;
4545                         g_stat ((*i).c_str(), &gsb);
4546                         total_bytes += gsb.st_size;
4547                 }
4548         }
4549
4550         /* save old values so we can switch back if we are not switching to the new session */
4551
4552         string old_path = _path;
4553         string old_name = _name;
4554         string old_snapshot = _current_snapshot_name;
4555         string old_sd = _session_dir->root_path();
4556         vector<string> old_search_path[DataType::num_types];
4557         string old_config_search_path[DataType::num_types];
4558
4559         old_search_path[DataType::AUDIO] = source_search_path (DataType::AUDIO);
4560         old_search_path[DataType::MIDI] = source_search_path (DataType::MIDI);
4561         old_config_search_path[DataType::AUDIO]  = config.get_audio_search_path ();
4562         old_config_search_path[DataType::MIDI]  = config.get_midi_search_path ();
4563
4564         /* switch session directory */
4565
4566         (*_session_dir) = to_dir;
4567
4568         /* create new tree */
4569
4570         if (!_session_dir->create()) {
4571                 saveas.failure_message = string_compose (_("Cannot create new session folder %1"), to_dir);
4572                 return -1;
4573         }
4574
4575         try {
4576                 /* copy all relevant files. Find each location in session_dirs,
4577                  * and copy files from there to target.
4578                  */
4579
4580                 for (vector<space_and_path>::const_iterator sd = session_dirs.begin(); sd != session_dirs.end(); ++sd) {
4581
4582                         /* need to clear this because
4583                          * find_files_matching_filter() is cumulative
4584                          */
4585
4586                         files.clear ();
4587
4588                         const size_t prefix_len = (*sd).path.size();
4589
4590                         /* Work just on the files within this session dir */
4591
4592                         find_files_matching_filter (files, (*sd).path, accept_all_files, 0, false, true, true);
4593
4594                         /* add dir separator to protect against collisions with
4595                          * track names (e.g. track named "audiofiles" or
4596                          * "analysis".
4597                          */
4598
4599                         static const std::string audiofile_dir_string = string (sound_dir_name) + G_DIR_SEPARATOR;
4600                         static const std::string midifile_dir_string = string (midi_dir_name) + G_DIR_SEPARATOR;
4601                         static const std::string analysis_dir_string = analysis_dir() + G_DIR_SEPARATOR;
4602
4603                         /* copy all the files. Handling is different for media files
4604                            than others because of the *silly* subtree we have below the interchange
4605                            folder. That really was a bad idea, but I'm not fixing it as part of
4606                            implementing ::save_as().
4607                         */
4608
4609                         for (vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
4610
4611                                 std::string from = *i;
4612
4613 #ifdef __APPLE__
4614                                 string filename = Glib::path_get_basename (from);
4615                                 std::transform (filename.begin(), filename.end(), filename.begin(), ::toupper);
4616                                 if (filename == ".DS_STORE") {
4617                                         continue;
4618                                 }
4619 #endif
4620
4621                                 if (from.find (audiofile_dir_string) != string::npos) {
4622
4623                                         /* audio file: only copy if asked */
4624
4625                                         if (saveas.include_media && saveas.copy_media) {
4626
4627                                                 string to = make_new_media_path (*i, to_dir, new_folder);
4628
4629                                                 info << "media file copying from " << from << " to " << to << endmsg;
4630
4631                                                 if (!copy_file (from, to)) {
4632                                                         throw Glib::FileError (Glib::FileError::IO_ERROR,
4633                                                                                                    string_compose(_("\ncopying \"%1\" failed !"), from));
4634                                                 }
4635                                         }
4636
4637                                         /* we found media files inside the session folder */
4638
4639                                         internal_file_cnt++;
4640
4641                                 } else if (from.find (midifile_dir_string) != string::npos) {
4642
4643                                         /* midi file: always copy unless
4644                                          * creating an empty new session
4645                                          */
4646
4647                                         if (saveas.include_media) {
4648
4649                                                 string to = make_new_media_path (*i, to_dir, new_folder);
4650
4651                                                 info << "media file copying from " << from << " to " << to << endmsg;
4652
4653                                                 if (!copy_file (from, to)) {
4654                                                         throw Glib::FileError (Glib::FileError::IO_ERROR, "copy failed");
4655                                                 }
4656                                         }
4657
4658                                         /* we found media files inside the session folder */
4659
4660                                         internal_file_cnt++;
4661
4662                                 } else if (from.find (analysis_dir_string) != string::npos) {
4663
4664                                         /*  make sure analysis dir exists in
4665                                          *  new session folder, but we're not
4666                                          *  copying analysis files here, see
4667                                          *  below
4668                                          */
4669
4670                                         (void) g_mkdir_with_parents (analysis_dir().c_str(), 775);
4671                                         continue;
4672
4673                                 } else {
4674
4675                                         /* normal non-media file. Don't copy state, history, etc.
4676                                          */
4677
4678                                         bool do_copy = true;
4679
4680                                         for (vector<string>::iterator v = do_not_copy_extensions.begin(); v != do_not_copy_extensions.end(); ++v) {
4681                                                 if ((from.length() > (*v).length()) && (from.find (*v) == from.length() - (*v).length())) {
4682                                                         /* end of filename matches extension, do not copy file */
4683                                                         do_copy = false;
4684                                                         break;
4685                                                 }
4686                                         }
4687
4688                                         if (!saveas.copy_media && from.find (peakfile_suffix) != string::npos) {
4689                                                 /* don't copy peakfiles if
4690                                                  * we're not copying media
4691                                                  */
4692                                                 do_copy = false;
4693                                         }
4694
4695                                         if (do_copy) {
4696                                                 string to = Glib::build_filename (to_dir, from.substr (prefix_len));
4697
4698                                                 info << "attempting to make directory/folder " << to << endmsg;
4699
4700                                                 if (g_mkdir_with_parents (Glib::path_get_dirname (to).c_str(), 0755)) {
4701                                                         throw Glib::FileError (Glib::FileError::IO_ERROR, "cannot create required directory");
4702                                                 }
4703
4704                                                 info << "attempting to copy " << from << " to " << to << endmsg;
4705
4706                                                 if (!copy_file (from, to)) {
4707                                                         throw Glib::FileError (Glib::FileError::IO_ERROR,
4708                                                                                                    string_compose(_("\ncopying \"%1\" failed !"), from));
4709                                                 }
4710                                         }
4711                                 }
4712
4713                                 /* measure file size even if we're not going to copy so that our Progress
4714                                    signals are correct, since we included these do-not-copy files
4715                                    in the computation of the total size and file count.
4716                                 */
4717
4718                                 GStatBuf gsb;
4719                                 g_stat (from.c_str(), &gsb);
4720                                 copied += gsb.st_size;
4721                                 cnt++;
4722
4723                                 double fraction = (double) copied / total_bytes;
4724
4725                                 bool keep_going = true;
4726
4727                                 if (saveas.copy_media) {
4728
4729                                         /* no need or expectation of this if
4730                                          * media is not being copied, because
4731                                          * it will be fast(ish).
4732                                          */
4733
4734                                         /* tell someone "X percent, file M of N"; M is one-based */
4735
4736                                         boost::optional<bool> res = saveas.Progress (fraction, cnt, all);
4737
4738                                         if (res) {
4739                                                 keep_going = *res;
4740                                         }
4741                                 }
4742
4743                                 if (!keep_going) {
4744                                         throw Glib::FileError (Glib::FileError::FAILED, "copy cancelled");
4745                                 }
4746                         }
4747
4748                 }
4749
4750                 /* copy optional folders, if any */
4751
4752                 string old = plugins_dir ();
4753                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4754                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4755                         copy_files (old, newdir);
4756                 }
4757
4758                 old = externals_dir ();
4759                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4760                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4761                         copy_files (old, newdir);
4762                 }
4763
4764                 old = automation_dir ();
4765                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4766                         string newdir = Glib::build_filename (to_dir, Glib::path_get_basename (old));
4767                         copy_files (old, newdir);
4768                 }
4769
4770                 if (saveas.include_media) {
4771
4772                         if (saveas.copy_media) {
4773 #ifndef PLATFORM_WINDOWS
4774                                 /* There are problems with analysis files on
4775                                  * Windows, because they used a colon in their
4776                                  * names as late as 4.0. Colons are not legal
4777                                  * under Windows even if NTFS allows them.
4778                                  *
4779                                  * This is a tricky problem to solve so for
4780                                  * just don't copy these files. They will be
4781                                  * regenerated as-needed anyway, subject to the
4782                                  * existing issue that the filenames will be
4783                                  * rejected by Windows, which is a separate
4784                                  * problem (though related).
4785                                  */
4786
4787                                 /* only needed if we are copying media, since the
4788                                  * analysis data refers to media data
4789                                  */
4790
4791                                 old = analysis_dir ();
4792                                 if (Glib::file_test (old, Glib::FILE_TEST_EXISTS)) {
4793                                         string newdir = Glib::build_filename (to_dir, "analysis");
4794                                         copy_files (old, newdir);
4795                                 }
4796 #endif /* PLATFORM_WINDOWS */
4797                         }
4798                 }
4799
4800
4801                 _path = to_dir;
4802                 set_snapshot_name (saveas.new_name);
4803                 _name = saveas.new_name;
4804
4805                 if (saveas.include_media && !saveas.copy_media) {
4806
4807                         /* reset search paths of the new session (which we're pretending to be right now) to
4808                            include the original session search path, so we can still find all audio.
4809                         */
4810
4811                         if (internal_file_cnt) {
4812                                 for (vector<string>::iterator s = old_search_path[DataType::AUDIO].begin(); s != old_search_path[DataType::AUDIO].end(); ++s) {
4813                                         ensure_search_path_includes (*s, DataType::AUDIO);
4814                                         cerr << "be sure to include " << *s << "  for audio" << endl;
4815                                 }
4816
4817                                 /* we do not do this for MIDI because we copy
4818                                    all MIDI files if saveas.include_media is
4819                                    true
4820                                 */
4821                         }
4822                 }
4823
4824                 bool was_dirty = dirty ();
4825
4826                 save_state ("", false, false, !saveas.include_media);
4827                 save_default_options ();
4828
4829                 if (saveas.copy_media && saveas.copy_external) {
4830                         if (bring_all_sources_into_session (boost::bind (&Session::save_as_bring_callback, this, _1, _2, _3))) {
4831                                 throw Glib::FileError (Glib::FileError::NO_SPACE_LEFT, "consolidate failed");
4832                         }
4833                 }
4834
4835                 saveas.final_session_folder_name = _path;
4836
4837                 store_recent_sessions (_name, _path);
4838
4839                 if (!saveas.switch_to) {
4840
4841                         /* switch back to the way things were */
4842
4843                         _path = old_path;
4844                         _name = old_name;
4845                         set_snapshot_name (old_snapshot);
4846
4847                         (*_session_dir) = old_sd;
4848
4849                         if (was_dirty) {
4850                                 set_dirty ();
4851                         }
4852
4853                         if (internal_file_cnt) {
4854                                 /* reset these to their original values */
4855                                 config.set_audio_search_path (old_config_search_path[DataType::AUDIO]);
4856                                 config.set_midi_search_path (old_config_search_path[DataType::MIDI]);
4857                         }
4858
4859                 } else {
4860
4861                         /* prune session dirs, and update disk space statistics
4862                          */
4863
4864                         space_and_path sp;
4865                         sp.path = _path;
4866                         session_dirs.clear ();
4867                         session_dirs.push_back (sp);
4868                         refresh_disk_space ();
4869
4870                         /* ensure that all existing tracks reset their current capture source paths
4871                          */
4872                         reset_write_sources (true, true);
4873
4874                         /* the copying above was based on actually discovering files, not just iterating over the sources list.
4875                            But if we're going to switch to the new (copied) session, we need to change the paths in the sources also.
4876                         */
4877
4878                         for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) {
4879                                 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (i->second);
4880
4881                                 if (!fs) {
4882                                         continue;
4883                                 }
4884
4885                                 if (fs->within_session()) {
4886                                         string newpath = make_new_media_path (fs->path(), to_dir, new_folder);
4887                                         fs->set_path (newpath);
4888                                 }
4889                         }
4890                 }
4891
4892         } catch (Glib::FileError& e) {
4893
4894                 saveas.failure_message = e.what();
4895
4896                 /* recursively remove all the directories */
4897
4898                 remove_directory (to_dir);
4899
4900                 /* return error */
4901
4902                 return -1;
4903
4904         } catch (...) {
4905
4906                 saveas.failure_message = _("unknown reason");
4907
4908                 /* recursively remove all the directories */
4909
4910                 remove_directory (to_dir);
4911
4912                 /* return error */
4913
4914                 return -1;
4915         }
4916
4917         return 0;
4918 }