2010-02-09 09:44:24 -05:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-03-01 19:00:00 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-02-09 09:44:24 -05:00
|
|
|
#include "pbd/stateful_diff_command.h"
|
2010-03-01 19:00:00 -05:00
|
|
|
#include "pbd/property_list.h"
|
2010-03-02 14:12:01 -05:00
|
|
|
#include "pbd/demangle.h"
|
2010-02-09 17:28:46 -05:00
|
|
|
#include "i18n.h"
|
2010-02-09 09:44:24 -05:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
|
|
|
|
* since the last time that clear_history was called on it.
|
|
|
|
* @param s Stateful object.
|
|
|
|
*/
|
|
|
|
|
2010-02-11 18:10:29 -05:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<Stateful> s)
|
2010-02-09 09:44:24 -05:00
|
|
|
: _object (s)
|
2010-03-01 19:00:00 -05:00
|
|
|
, _before (new PropertyList)
|
|
|
|
, _after (new PropertyList)
|
2010-02-09 09:44:24 -05:00
|
|
|
{
|
2010-03-01 19:00:00 -05:00
|
|
|
s->diff (*_before, *_after);
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
2010-02-11 18:10:29 -05:00
|
|
|
StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<Stateful> s, XMLNode const & n)
|
2010-02-09 17:28:46 -05:00
|
|
|
: _object (s)
|
2010-03-01 19:00:00 -05:00
|
|
|
, _before (0)
|
|
|
|
, _after (0)
|
2010-02-09 17:28:46 -05:00
|
|
|
{
|
2010-03-01 19:00:00 -05:00
|
|
|
const XMLNodeList& children (n.children());
|
|
|
|
|
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
|
|
|
if ((*i)->name() == X_("Undo")) {
|
|
|
|
_before = s->property_factory (**i);
|
|
|
|
} else if ((*i)->name() == X_("Do")) {
|
|
|
|
_after = s->property_factory (**i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (_before != 0);
|
|
|
|
assert (_after != 0);
|
2010-02-09 17:28:46 -05:00
|
|
|
}
|
|
|
|
|
2010-02-09 09:44:24 -05:00
|
|
|
StatefulDiffCommand::~StatefulDiffCommand ()
|
|
|
|
{
|
2010-03-01 19:00:00 -05:00
|
|
|
delete _before;
|
|
|
|
delete _after;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::operator() ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-03-01 19:00:00 -05:00
|
|
|
PropertyChange changed = s->set_properties (*_after);
|
|
|
|
if (!changed.empty()) {
|
|
|
|
s->PropertyChanged (changed);
|
|
|
|
}
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulDiffCommand::undo ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (s) {
|
2010-03-01 19:00:00 -05:00
|
|
|
std::cerr << "Undoing a stateful diff command\n";
|
|
|
|
PropertyChange changed = s->set_properties (*_before);
|
|
|
|
if (!changed.empty()) {
|
|
|
|
std::cerr << "Sending changed\n";
|
|
|
|
s->PropertyChanged (changed);
|
|
|
|
}
|
2010-02-11 18:10:29 -05:00
|
|
|
}
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode&
|
|
|
|
StatefulDiffCommand::get_state ()
|
|
|
|
{
|
2010-02-11 18:10:29 -05:00
|
|
|
boost::shared_ptr<Stateful> s (_object.lock());
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
/* XXX should we throw? */
|
|
|
|
return * new XMLNode("");
|
|
|
|
}
|
|
|
|
|
2010-02-09 17:28:46 -05:00
|
|
|
XMLNode* node = new XMLNode (X_("StatefulDiffCommand"));
|
|
|
|
|
2010-02-11 18:10:29 -05:00
|
|
|
node->add_property ("obj-id", s->id().to_s());
|
2010-03-02 14:12:01 -05:00
|
|
|
node->add_property ("type-name", demangled_name (*s.get()));
|
2010-03-01 19:00:00 -05:00
|
|
|
|
|
|
|
XMLNode* before = new XMLNode (X_("Undo"));
|
|
|
|
XMLNode* after = new XMLNode (X_("Do"));
|
|
|
|
|
|
|
|
_before->add_history_state (before);
|
|
|
|
_after->add_history_state (after);
|
|
|
|
|
|
|
|
node->add_child_nocopy (*before);
|
|
|
|
node->add_child_nocopy (*after);
|
2010-02-09 17:28:46 -05:00
|
|
|
|
|
|
|
return *node;
|
2010-02-09 09:44:24 -05:00
|
|
|
}
|