+MidiModel::NoteDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
+{
+ if (diff_command.name() != string (NOTE_DIFF_COMMAND_ELEMENT)) {
+ return 1;
+ }
+
+ /* additions */
+
+ _added_notes.clear();
+ XMLNode* added_notes = diff_command.child(ADDED_NOTES_ELEMENT);
+ if (added_notes) {
+ XMLNodeList notes = added_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_added_notes),
+ boost::bind (&NoteDiffCommand::unmarshal_note, this, _1));
+ }
+
+
+ /* removals */
+
+ _removed_notes.clear();
+ XMLNode* removed_notes = diff_command.child(REMOVED_NOTES_ELEMENT);
+ if (removed_notes) {
+ XMLNodeList notes = removed_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
+ boost::bind (&NoteDiffCommand::unmarshal_note, this, _1));
+ }
+
+
+ /* changes */
+
+ _changes.clear();
+
+ XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
+
+ if (changed_notes) {
+ XMLNodeList notes = changed_notes->children();
+ transform (notes.begin(), notes.end(), back_inserter(_changes),
+ boost::bind (&NoteDiffCommand::unmarshal_change, this, _1));
+
+ }
+
+ /* side effect removals caused by changes */
+
+ side_effect_removals.clear();
+
+ XMLNode* side_effect_notes = diff_command.child(SIDE_EFFECT_REMOVALS_ELEMENT);
+
+ if (side_effect_notes) {
+ XMLNodeList notes = side_effect_notes->children();
+ for (XMLNodeList::iterator n = notes.begin(); n != notes.end(); ++n) {
+ side_effect_removals.insert (unmarshal_note (*n));
+ }
+ }
+
+ return 0;
+}
+
+XMLNode&
+MidiModel::NoteDiffCommand::get_state ()
+{
+ XMLNode* diff_command = new XMLNode (NOTE_DIFF_COMMAND_ELEMENT);
+ diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
+
+ XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
+ for_each(_changes.begin(), _changes.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, changes, _1),
+ boost::bind (&NoteDiffCommand::marshal_change, this, _1)));
+
+ XMLNode* added_notes = diff_command->add_child(ADDED_NOTES_ELEMENT);
+ for_each(_added_notes.begin(), _added_notes.end(),
+ boost::bind(
+ boost::bind (&XMLNode::add_child_nocopy, added_notes, _1),
+ boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
+
+ XMLNode* removed_notes = diff_command->add_child(REMOVED_NOTES_ELEMENT);
+ for_each(_removed_notes.begin(), _removed_notes.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, removed_notes, _1),
+ boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
+
+ /* if this command had side-effects, store that state too
+ */
+
+ if (!side_effect_removals.empty()) {
+ XMLNode* side_effect_notes = diff_command->add_child(SIDE_EFFECT_REMOVALS_ELEMENT);
+ for_each(side_effect_removals.begin(), side_effect_removals.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, side_effect_notes, _1),
+ boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
+ }
+
+ return *diff_command;
+}
+
+MidiModel::SysExDiffCommand::SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node)
+ : DiffCommand (m, "")
+{
+ assert (_model);
+ set_state (node, Stateful::loading_state_version);
+}
+
+void
+MidiModel::SysExDiffCommand::change (boost::shared_ptr<Evoral::Event<TimeType> > s, TimeType new_time)
+{
+ Change change;
+
+ change.sysex = s;
+ change.property = Time;
+ change.old_time = s->time ();
+ change.new_time = new_time;
+
+ _changes.push_back (change);
+}
+
+void
+MidiModel::SysExDiffCommand::operator() ()
+{
+ {
+ MidiModel::WriteLock lock (_model->edit_lock ());
+
+ for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+ _model->remove_sysex_unlocked (*i);
+ }
+
+ /* find any sysex events that were missing when unmarshalling */
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ if (!i->sysex) {
+ i->sysex = _model->find_sysex (i->sysex_id);
+ assert (i->sysex);
+ }
+ }
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ switch (i->property) {
+ case Time:
+ i->sysex->set_time (i->new_time);
+ }
+ }
+ }
+
+ _model->ContentsChanged (); /* EMIT SIGNAL */
+}
+
+void
+MidiModel::SysExDiffCommand::undo ()
+{
+ {
+ MidiModel::WriteLock lock (_model->edit_lock ());
+
+ for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+ _model->add_sysex_unlocked (*i);
+ }
+
+ /* find any sysex events that were missing when unmarshalling */
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ if (!i->sysex) {
+ i->sysex = _model->find_sysex (i->sysex_id);
+ assert (i->sysex);
+ }
+ }
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ switch (i->property) {
+ case Time:
+ i->sysex->set_time (i->old_time);
+ break;
+ }
+ }
+
+ }
+
+ _model->ContentsChanged(); /* EMIT SIGNAL */
+}
+
+void
+MidiModel::SysExDiffCommand::remove (SysExPtr sysex)
+{
+ _removed.push_back(sysex);
+}
+
+XMLNode&
+MidiModel::SysExDiffCommand::marshal_change (const Change& change)
+{
+ XMLNode* xml_change = new XMLNode ("Change");
+
+ /* first, the change itself */
+
+ xml_change->add_property ("property", enum_2_string (change.property));
+
+ {
+ ostringstream old_value_str (ios::ate);
+ old_value_str << change.old_time;
+ xml_change->add_property ("old", old_value_str.str());
+ }
+
+ {
+ ostringstream new_value_str (ios::ate);
+ new_value_str << change.new_time;
+ xml_change->add_property ("new", new_value_str.str());
+ }
+
+ ostringstream id_str;
+ id_str << change.sysex->id();
+ xml_change->add_property ("id", id_str.str());
+
+ return *xml_change;
+}
+
+MidiModel::SysExDiffCommand::Change
+MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
+{
+ XMLProperty const * prop;
+ Change change;
+
+ if ((prop = xml_change->property ("property")) != 0) {
+ change.property = (Property) string_2_enum (prop->value(), change.property);
+ } else {
+ fatal << "!!!" << endmsg;
+ abort(); /*NOTREACHED*/
+ }
+
+ if ((prop = xml_change->property ("id")) == 0) {
+ error << _("No SysExID found for sys-ex property change - ignored") << endmsg;
+ return change;
+ }
+
+ gint sysex_id = atoi (prop->value().c_str());
+
+ if ((prop = xml_change->property ("old")) != 0) {
+ istringstream old_str (prop->value());
+ old_str >> change.old_time;
+ } else {
+ fatal << "!!!" << endmsg;
+ abort(); /*NOTREACHED*/
+ }
+
+ if ((prop = xml_change->property ("new")) != 0) {
+ istringstream new_str (prop->value());
+ new_str >> change.new_time;
+ } else {
+ fatal << "!!!" << endmsg;
+ abort(); /*NOTREACHED*/
+ }
+
+ /* we must point at the instance of the sysex that is actually in the model.
+ so go look for it ...
+ */
+
+ change.sysex = _model->find_sysex (sysex_id);
+ change.sysex_id = sysex_id;
+
+ return change;
+}
+
+int
+MidiModel::SysExDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
+{
+ if (diff_command.name() != string (SYSEX_DIFF_COMMAND_ELEMENT)) {
+ return 1;
+ }
+
+ /* changes */
+
+ _changes.clear();
+
+ XMLNode* changed_sysexes = diff_command.child (DIFF_SYSEXES_ELEMENT);
+
+ if (changed_sysexes) {
+ XMLNodeList sysexes = changed_sysexes->children();
+ transform (sysexes.begin(), sysexes.end(), back_inserter (_changes),
+ boost::bind (&SysExDiffCommand::unmarshal_change, this, _1));
+
+ }
+
+ return 0;
+}
+
+XMLNode&
+MidiModel::SysExDiffCommand::get_state ()
+{
+ XMLNode* diff_command = new XMLNode (SYSEX_DIFF_COMMAND_ELEMENT);
+ diff_command->add_property ("midi-source", _model->midi_source()->id().to_s());
+
+ XMLNode* changes = diff_command->add_child(DIFF_SYSEXES_ELEMENT);
+ for_each (_changes.begin(), _changes.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, changes, _1),
+ boost::bind (&SysExDiffCommand::marshal_change, this, _1)));
+
+ return *diff_command;
+}
+
+MidiModel::PatchChangeDiffCommand::PatchChangeDiffCommand (boost::shared_ptr<MidiModel> m, const string& name)
+ : DiffCommand (m, name)
+{
+ assert (_model);
+}
+
+MidiModel::PatchChangeDiffCommand::PatchChangeDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode & node)
+ : DiffCommand (m, "")
+{
+ assert (_model);
+ set_state (node, Stateful::loading_state_version);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::add (PatchChangePtr p)
+{
+ _added.push_back (p);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::remove (PatchChangePtr p)
+{
+ _removed.push_back (p);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::change_time (PatchChangePtr patch, TimeType t)