+ XMLNode* diff_command = new XMLNode (SYSEX_DIFF_COMMAND_ELEMENT);
+ diff_command->set_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)
+{
+ Change c;
+ c.property = Time;
+ c.patch = patch;
+ c.old_time = patch->time ();
+ c.new_time = t;
+
+ _changes.push_back (c);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::change_channel (PatchChangePtr patch, uint8_t channel)
+{
+ Change c;
+ c.property = Channel;
+ c.patch = patch;
+ c.old_channel = patch->channel ();
+ c.new_channel = channel;
+ c.patch_id = patch->id();
+
+ _changes.push_back (c);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::change_program (PatchChangePtr patch, uint8_t program)
+{
+ Change c;
+ c.property = Program;
+ c.patch = patch;
+ c.old_program = patch->program ();
+ c.new_program = program;
+ c.patch_id = patch->id();
+
+ _changes.push_back (c);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::change_bank (PatchChangePtr patch, int bank)
+{
+ Change c;
+ c.property = Bank;
+ c.patch = patch;
+ c.old_bank = patch->bank ();
+ c.new_bank = bank;
+
+ _changes.push_back (c);
+}
+
+void
+MidiModel::PatchChangeDiffCommand::operator() ()
+{
+ {
+ MidiModel::WriteLock lock (_model->edit_lock ());
+
+ for (list<PatchChangePtr>::iterator i = _added.begin(); i != _added.end(); ++i) {
+ _model->add_patch_change_unlocked (*i);
+ }
+
+ for (list<PatchChangePtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+ _model->remove_patch_change_unlocked (*i);
+ }
+
+ /* find any patch change events that were missing when unmarshalling */
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ if (!i->patch) {
+ i->patch = _model->find_patch_change (i->patch_id);
+ assert (i->patch);
+ }
+ }
+
+ set<PatchChangePtr> temporary_removals;
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ switch (i->property) {
+ case Time:
+ if (temporary_removals.find (i->patch) == temporary_removals.end()) {
+ _model->remove_patch_change_unlocked (i->patch);
+ temporary_removals.insert (i->patch);
+ }
+ i->patch->set_time (i->new_time);
+ break;
+
+ case Channel:
+ i->patch->set_channel (i->new_channel);
+ break;
+
+ case Program:
+ i->patch->set_program (i->new_program);
+ break;
+
+ case Bank:
+ i->patch->set_bank (i->new_bank);
+ break;
+ }
+ }
+
+ for (set<PatchChangePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
+ _model->add_patch_change_unlocked (*i);
+ }
+ }
+
+ _model->ContentsChanged (); /* EMIT SIGNAL */
+}
+
+void
+MidiModel::PatchChangeDiffCommand::undo ()
+{
+ {
+ MidiModel::WriteLock lock (_model->edit_lock());
+
+ for (list<PatchChangePtr>::iterator i = _added.begin(); i != _added.end(); ++i) {
+ _model->remove_patch_change_unlocked (*i);
+ }
+
+ for (list<PatchChangePtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+ _model->add_patch_change_unlocked (*i);
+ }
+
+ /* find any patch change events that were missing when unmarshalling */
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ if (!i->patch) {
+ i->patch = _model->find_patch_change (i->patch_id);
+ assert (i->patch);
+ }
+ }
+
+ set<PatchChangePtr> temporary_removals;
+
+ for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+ switch (i->property) {
+ case Time:
+ if (temporary_removals.find (i->patch) == temporary_removals.end()) {
+ _model->remove_patch_change_unlocked (i->patch);
+ temporary_removals.insert (i->patch);
+ }
+ i->patch->set_time (i->old_time);
+ break;
+
+ case Channel:
+ i->patch->set_channel (i->old_channel);
+ break;
+
+ case Program:
+ i->patch->set_program (i->old_program);
+ break;
+
+ case Bank:
+ i->patch->set_bank (i->old_bank);
+ break;
+ }
+ }
+
+ for (set<PatchChangePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
+ _model->add_patch_change_unlocked (*i);
+ }
+
+ }
+
+ _model->ContentsChanged (); /* EMIT SIGNAL */
+}
+
+XMLNode &
+MidiModel::PatchChangeDiffCommand::marshal_patch_change (constPatchChangePtr p)
+{
+ XMLNode* n = new XMLNode ("patch-change");
+
+ n->set_property ("id", p->id ());
+ n->set_property ("time", p->time ());
+ n->set_property ("channel", p->channel ());
+ n->set_property ("program", p->program ());
+ n->set_property ("bank", p->bank ());
+
+ return *n;
+}
+
+XMLNode&
+MidiModel::PatchChangeDiffCommand::marshal_change (const Change& c)
+{
+ XMLNode* n = new XMLNode (X_("Change"));
+
+ n->set_property (X_("property"), c.property);
+
+ if (c.property == Time) {
+ n->set_property (X_("old"), c.old_time);
+ } else if (c.property == Channel) {
+ n->set_property (X_("old"), c.old_channel);
+ } else if (c.property == Program) {
+ n->set_property (X_("old"), c.old_program);
+ } else if (c.property == Bank) {
+ n->set_property (X_("old"), c.old_bank);
+ }
+
+ if (c.property == Time) {
+ n->set_property (X_ ("new"), c.new_time);
+ } else if (c.property == Channel) {
+ n->set_property (X_ ("new"), c.new_channel);
+ } else if (c.property == Program) {
+ n->set_property (X_ ("new"), c.new_program);
+ } else if (c.property == Bank) {
+ n->set_property (X_ ("new"), c.new_bank);
+ }
+
+ n->set_property ("id", c.patch->id ());
+
+ return *n;
+}
+
+MidiModel::PatchChangePtr
+MidiModel::PatchChangeDiffCommand::unmarshal_patch_change (XMLNode* n)
+{
+ Evoral::event_id_t id = 0;
+ if (!n->get_property ("id", id)) {
+ assert(false);
+ }
+
+ Evoral::Beats time = Evoral::Beats();
+ if (!n->get_property ("time", time)) {
+ // warning??
+ }
+
+ uint8_t channel = 0;
+ if (!n->get_property ("channel", channel)) {
+ // warning??
+ }
+
+ int program = 0;
+ if (!n->get_property ("program", program)) {
+ // warning??
+ }
+
+ int bank = 0;
+ if (!n->get_property ("bank", bank)) {
+ // warning??
+ }
+
+ PatchChangePtr p (new Evoral::PatchChange<TimeType> (time, channel, program, bank));
+ p->set_id (id);
+ return p;
+}
+
+MidiModel::PatchChangeDiffCommand::Change
+MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
+{
+ Change c;
+ Evoral::event_id_t id;
+
+ if (!n->get_property ("property", c.property) || !n->get_property ("id", id)) {
+ assert(false);
+ }
+
+ if ((c.property == Time && !n->get_property ("old", c.old_time)) ||
+ (c.property == Channel && !n->get_property ("old", c.old_channel)) ||
+ (c.property == Program && !n->get_property ("old", c.old_program)) ||
+ (c.property == Bank && !n->get_property ("old", c.old_bank))) {
+ assert (false);
+ }
+
+ if ((c.property == Time && !n->get_property ("new", c.new_time)) ||
+ (c.property == Channel && !n->get_property ("new", c.new_channel)) ||
+ (c.property == Program && !n->get_property ("new", c.new_program)) ||
+ (c.property == Bank && !n->get_property ("new", c.new_bank))) {
+ assert (false);
+ }
+
+ c.patch = _model->find_patch_change (id);
+ c.patch_id = id;
+
+ return c;
+}
+
+int
+MidiModel::PatchChangeDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
+{
+ if (diff_command.name() != PATCH_CHANGE_DIFF_COMMAND_ELEMENT) {
+ return 1;
+ }
+
+ _added.clear ();
+ XMLNode* added = diff_command.child (ADDED_PATCH_CHANGES_ELEMENT);
+ if (added) {
+ XMLNodeList p = added->children ();
+ transform (p.begin(), p.end(), back_inserter (_added), boost::bind (&PatchChangeDiffCommand::unmarshal_patch_change, this, _1));
+ }
+
+ _removed.clear ();
+ XMLNode* removed = diff_command.child (REMOVED_PATCH_CHANGES_ELEMENT);
+ if (removed) {
+ XMLNodeList p = removed->children ();
+ transform (p.begin(), p.end(), back_inserter (_removed), boost::bind (&PatchChangeDiffCommand::unmarshal_patch_change, this, _1));
+ }
+
+ _changes.clear ();
+ XMLNode* changed = diff_command.child (DIFF_PATCH_CHANGES_ELEMENT);
+ if (changed) {
+ XMLNodeList p = changed->children ();
+ transform (p.begin(), p.end(), back_inserter (_changes), boost::bind (&PatchChangeDiffCommand::unmarshal_change, this, _1));
+ }
+
+ return 0;
+}
+
+XMLNode &
+MidiModel::PatchChangeDiffCommand::get_state ()
+{
+ XMLNode* diff_command = new XMLNode (PATCH_CHANGE_DIFF_COMMAND_ELEMENT);
+ diff_command->set_property("midi-source", _model->midi_source()->id().to_s());
+
+ XMLNode* added = diff_command->add_child (ADDED_PATCH_CHANGES_ELEMENT);
+ for_each (_added.begin(), _added.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, added, _1),
+ boost::bind (&PatchChangeDiffCommand::marshal_patch_change, this, _1)
+ )
+ );
+
+ XMLNode* removed = diff_command->add_child (REMOVED_PATCH_CHANGES_ELEMENT);
+ for_each (_removed.begin(), _removed.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, removed, _1),
+ boost::bind (&PatchChangeDiffCommand::marshal_patch_change, this, _1)
+ )
+ );