+void
+Session::set_deletion_in_progress ()
+{
+ _state_of_the_state = StateOfTheState (_state_of_the_state | Deletion);
+}
+
+void
+Session::add_controllable (Controllable* c)
+{
+ /* this adds a controllable to the list managed by the Session.
+ this is a subset of those managed by the Controllable class
+ itself, and represents the only ones whose state will be saved
+ as part of the session.
+ */
+
+ Glib::Mutex::Lock lm (controllables_lock);
+ controllables.insert (c);
+}
+
+void
+Session::remove_controllable (Controllable* c)
+{
+ if (_state_of_the_state | Deletion) {
+ return;
+ }
+
+ Glib::Mutex::Lock lm (controllables_lock);
+
+ Controllables::iterator x = controllables.find (c);
+
+ if (x != controllables.end()) {
+ controllables.erase (x);
+ }
+}
+
+Controllable*
+Session::controllable_by_id (const PBD::ID& id)
+{
+ Glib::Mutex::Lock lm (controllables_lock);
+
+ for (Controllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
+ if ((*i)->id() == id) {
+ return *i;
+ }
+ }
+
+ return 0;
+}
+
+void
+Session::add_instant_xml (XMLNode& node, const std::string& dir)
+{
+ Stateful::add_instant_xml (node, dir);
+ Config->add_instant_xml (node, get_user_ardour_path());
+}
+
+
+int
+Session::save_history (string snapshot_name)
+{
+ XMLTree tree;
+ string xml_path;
+ string bak_path;
+
+ tree.set_root (&_history.get_state (Config->get_saved_history_depth()));
+
+ if (snapshot_name.empty()) {
+ snapshot_name = _current_snapshot_name;
+ }
+
+ xml_path = _path + snapshot_name + ".history";
+
+ bak_path = xml_path + ".bak";
+
+ if ((access (xml_path.c_str(), F_OK) == 0) &&
+ (rename (xml_path.c_str(), bak_path.c_str())))
+ {
+ error << _("could not backup old history file, current history not saved.") << endmsg;
+ return -1;
+ }
+
+ if (!tree.write (xml_path))
+ {
+ error << string_compose (_("history could not be saved to %1"), xml_path) << endmsg;
+
+ /* don't leave a corrupt file lying around if it is
+ * possible to fix.
+ */
+
+ if (unlink (xml_path.c_str())) {
+ error << string_compose (_("could not remove corrupt history file %1"), xml_path) << endmsg;
+ } else {
+ if (rename (bak_path.c_str(), xml_path.c_str()))
+ {
+ error << string_compose (_("could not restore history file from backup %1"), bak_path) << endmsg;
+ }
+ }
+
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+Session::restore_history (string snapshot_name)
+{
+ XMLTree tree;
+ string xmlpath;
+
+ if (snapshot_name.empty()) {
+ snapshot_name = _current_snapshot_name;
+ }
+
+ /* read xml */
+ xmlpath = _path + snapshot_name + ".history";
+ cerr << string_compose(_("Loading history from '%1'."), xmlpath) << endmsg;
+
+ if (access (xmlpath.c_str(), F_OK)) {
+ info << string_compose (_("%1: no history file \"%2\" for this session."), _name, xmlpath) << endmsg;
+ return 1;
+ }
+
+ if (!tree.read (xmlpath)) {
+ error << string_compose (_("Could not understand session history file \"%1\""), xmlpath) << endmsg;
+ return -1;
+ }
+
+ /* replace history */
+ _history.clear();
+
+ for (XMLNodeConstIterator it = tree.root()->children().begin(); it != tree.root()->children().end(); it++) {
+
+ XMLNode *t = *it;
+ UndoTransaction* ut = new UndoTransaction ();
+ struct timeval tv;
+
+ ut->set_name(t->property("name")->value());
+ stringstream ss(t->property("tv_sec")->value());
+ ss >> tv.tv_sec;
+ ss.str(t->property("tv_usec")->value());
+ ss >> tv.tv_usec;
+ ut->set_timestamp(tv);
+
+ for (XMLNodeConstIterator child_it = t->children().begin();
+ child_it != t->children().end();
+ child_it++)
+ {
+ XMLNode *n = *child_it;
+ Command *c;
+
+ if (n->name() == "MementoCommand" ||
+ n->name() == "MementoUndoCommand" ||
+ n->name() == "MementoRedoCommand") {
+
+ if ((c = memento_command_factory(n))) {
+ ut->add_command(c);
+ }
+
+ } else if (n->name() == X_("GlobalRouteStateCommand")) {
+
+ if ((c = global_state_command_factory (*n))) {
+ ut->add_command (c);
+ }
+
+ } else {
+
+ error << string_compose(_("Couldn't figure out how to make a Command out of a %1 XMLNode."), n->name()) << endmsg;
+ }
+ }
+
+ _history.add (ut);
+ }
+
+ return 0;
+}
+
+void
+Session::config_changed (const char* parameter_name)
+{
+#define PARAM_IS(x) (!strcmp (parameter_name, (x)))
+
+ if (PARAM_IS ("seamless-loop")) {
+
+ } else if (PARAM_IS ("rf-speed")) {
+
+ } else if (PARAM_IS ("auto-loop")) {
+
+ } else if (PARAM_IS ("auto-input")) {
+
+ if (Config->get_monitoring_model() == HardwareMonitoring && transport_rolling()) {
+ /* auto-input only makes a difference if we're rolling */
+
+ boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+
+ for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
+ if ((*i)->record_enabled ()) {
+ (*i)->monitor_input (!Config->get_auto_input());
+ }
+ }
+ }
+
+ } else if (PARAM_IS ("punch-in")) {
+
+ Location* location;
+
+ if ((location = _locations.auto_punch_location()) != 0) {
+
+ if (Config->get_punch_in ()) {
+ replace_event (Event::PunchIn, location->start());
+ } else {
+ remove_event (location->start(), Event::PunchIn);
+ }
+ }
+
+ } else if (PARAM_IS ("punch-out")) {
+
+ Location* location;
+
+ if ((location = _locations.auto_punch_location()) != 0) {
+
+ if (Config->get_punch_out()) {
+ replace_event (Event::PunchOut, location->end());
+ } else {
+ clear_events (Event::PunchOut);
+ }
+ }
+
+ } else if (PARAM_IS ("edit-mode")) {
+
+ Glib::Mutex::Lock lm (playlist_lock);
+
+ for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+ (*i)->set_edit_mode (Config->get_edit_mode ());
+ }
+
+ } else if (PARAM_IS ("use-video-sync")) {
+
+ if (transport_stopped()) {
+ if (Config->get_use_video_sync()) {
+ waiting_for_sync_offset = true;
+ }
+ }
+
+ } else if (PARAM_IS ("mmc-control")) {
+
+ poke_midi_thread ();
+
+ } else if (PARAM_IS ("mmc-device-id")) {
+
+ if (mmc) {
+ mmc->set_device_id (Config->get_mmc_device_id());
+ }
+
+ } else if (PARAM_IS ("midi-control")) {
+
+ poke_midi_thread ();
+
+ } else if (PARAM_IS ("raid-path")) {
+
+ setup_raid_path (Config->get_raid_path());
+
+ } else if (PARAM_IS ("smpte-format")) {
+
+ sync_time_vars ();
+
+ } else if (PARAM_IS ("video-pullup")) {
+
+ sync_time_vars ();
+
+ } else if (PARAM_IS ("seamless-loop")) {
+
+ if (play_loop && transport_rolling()) {
+ // to reset diskstreams etc
+ request_play_loop (true);
+ }
+
+ } else if (PARAM_IS ("rf-speed")) {
+
+ cumulative_rf_motion = 0;
+ reset_rf_scale (0);
+
+ } else if (PARAM_IS ("click-sound")) {
+
+ setup_click_sounds (1);
+
+ } else if (PARAM_IS ("click-emphasis-sound")) {
+
+ setup_click_sounds (-1);
+
+ } else if (PARAM_IS ("clicking")) {
+
+ if (Config->get_clicking()) {
+ if (_click_io && click_data) { // don't require emphasis data
+ _clicking = true;
+ }
+ } else {
+ _clicking = false;
+ }
+
+ } else if (PARAM_IS ("send-mtc")) {
+
+ /* only set the internal flag if we have
+ a port.
+ */
+
+ if (_mtc_port != 0) {
+ session_send_mtc = Config->get_send_mtc();
+ if (session_send_mtc) {
+ /* mark us ready to send */
+ next_quarter_frame_to_send = 0;
+ }
+ } else {
+ session_send_mtc = false;
+ }
+
+ } else if (PARAM_IS ("send-mmc")) {
+
+ /* only set the internal flag if we have
+ a port.
+ */
+
+ if (_mmc_port != 0) {
+ session_send_mmc = Config->get_send_mmc();
+ } else {
+ mmc = 0;
+ session_send_mmc = false;
+ }
+
+ } else if (PARAM_IS ("midi-feedback")) {
+
+ /* only set the internal flag if we have
+ a port.
+ */
+
+ if (_mtc_port != 0) {
+ session_midi_feedback = Config->get_midi_feedback();
+ }
+
+ } else if (PARAM_IS ("jack-time-master")) {
+
+ engine().reset_timebase ();
+
+ } else if (PARAM_IS ("native-file-header-format")) {
+
+ if (!first_file_header_format_reset) {
+ reset_native_file_format ();
+ }
+
+ first_file_header_format_reset = false;
+
+ } else if (PARAM_IS ("native-file-data-format")) {
+
+ if (!first_file_data_format_reset) {
+ reset_native_file_format ();
+ }
+
+ first_file_data_format_reset = false;
+
+ } else if (PARAM_IS ("slave-source")) {
+ set_slave_source (Config->get_slave_source());
+ } else if (PARAM_IS ("remote-model")) {
+ set_remote_control_ids ();
+ }
+
+ set_dirty ();
+
+#undef PARAM_IS
+
+}