summaryrefslogtreecommitdiff
path: root/libs/pbd/stateful_diff_command.cc
blob: e3a65ffc12c69f5551c5d10fc9d7353ffd199056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    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.

*/

#include "pbd/stateful_diff_command.h"
#include "pbd/types_convert.h"
#include "pbd/property_list.h"
#include "pbd/demangle.h"
#include "pbd/i18n.h"

using namespace std;
using namespace PBD;

/** Create a new StatefulDiffCommand by examining the changes made to a Stateful
 *  since the last time that clear_changes was called on it.
 *  @param s Stateful object.
 */

StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s)
        : _object (s)
        , _changes (0)
{
	_changes = s->get_changes_as_properties (this);

        /* if the stateful object that this command refers to goes away,
           be sure to notify owners of this command.
        */

        s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
}

StatefulDiffCommand::StatefulDiffCommand (boost::shared_ptr<StatefulDestructible> s, XMLNode const & n)
	: _object (s)
        , _changes (0)
{
        const XMLNodeList& children (n.children());

        for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
                if ((*i)->name() == X_("Changes")) {
                        _changes = s->property_factory (**i);
                }
	}

        assert (_changes != 0);

        /* if the stateful object that this command refers to goes away,
           be sure to notify owners of this command.
        */

        s->DropReferences.connect_same_thread (*this, boost::bind (&Destructible::drop_references, this));
}

StatefulDiffCommand::~StatefulDiffCommand ()
{
        drop_references ();

        delete _changes;
}

void
StatefulDiffCommand::operator() ()
{
	boost::shared_ptr<Stateful> s (_object.lock());

	if (s) {
                s->apply_changes (*_changes);
	}
}

void
StatefulDiffCommand::undo ()
{
	boost::shared_ptr<Stateful> s (_object.lock());

	if (s) {
		PropertyList p = *_changes;
		p.invert ();
                s->apply_changes (p);
	}
}

XMLNode&
StatefulDiffCommand::get_state ()
{
	boost::shared_ptr<Stateful> s (_object.lock());

	if (!s) {
		/* XXX should we throw? */
		return * new XMLNode("");
	}

	XMLNode* node = new XMLNode (X_("StatefulDiffCommand"));

	node->set_property ("obj-id", s->id());
	node->set_property ("type-name", demangled_name (*s.get()));

        XMLNode* changes = new XMLNode (X_("Changes"));

        _changes->get_changes_as_xml (changes);

        node->add_child_nocopy (*changes);

	return *node;
}

bool
StatefulDiffCommand::empty () const
{
	return _changes->empty();
}